From 6ea71cbabe96c5382dda51553271570535d94cbd Mon Sep 17 00:00:00 2001 From: Matt Caswell Date: Tue, 28 Nov 2017 09:53:58 +0000 Subject: [PATCH] Add tests for Ed448ph Reviewed-by: Bernd Edlinger (Merged from https://github.com/openssl/openssl/pull/5105) --- crypto/ec/curve448/curve448_lcl.h | 12 ++-- crypto/ec/curve448/curve448_test.c | 102 +++++++++++++++++++++++++++++ crypto/ec/curve448/ed448.h | 4 +- crypto/ec/curve448/eddsa.c | 69 +++++++------------ 4 files changed, 135 insertions(+), 52 deletions(-) diff --git a/crypto/ec/curve448/curve448_lcl.h b/crypto/ec/curve448/curve448_lcl.h index afe0ca38c3..13dff0df01 100644 --- a/crypto/ec/curve448/curve448_lcl.h +++ b/crypto/ec/curve448/curve448_lcl.h @@ -1,4 +1,7 @@ #include "curve448utils.h" +#include "shake.h" + +#define decaf_ed448_prehash_ctx_t decaf_shake256_ctx_t int X448(uint8_t out_shared_key[56], const uint8_t private_key[56], const uint8_t peer_public_value[56]); @@ -14,14 +17,13 @@ int ED448_verify(const uint8_t *message, size_t message_len, const uint8_t signature[112], const uint8_t public_key[56], const uint8_t *context, size_t context_len); -int ED448ph_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len, +int ED448ph_sign(uint8_t *out_sig, const uint8_t hash[64], const uint8_t public_key[56], const uint8_t private_key[56], const uint8_t *context, size_t context_len); - -int ED448ph_verify(const uint8_t *message, size_t message_len, - const uint8_t signature[112], const uint8_t public_key[56], - const uint8_t *context, size_t context_len); +int ED448ph_verify(const uint8_t hash[64], const uint8_t signature[112], + const uint8_t public_key[56], const uint8_t *context, + size_t context_len); void ED448_public_from_private(uint8_t out_public_key[56], const uint8_t private_key[56]); diff --git a/crypto/ec/curve448/curve448_test.c b/crypto/ec/curve448/curve448_test.c index 0e2e58eedd..22684f3c13 100644 --- a/crypto/ec/curve448/curve448_test.c +++ b/crypto/ec/curve448/curve448_test.c @@ -85,6 +85,8 @@ const uint8_t out_u3[3][56] = { /* Test vectors from RFC8032 for Ed448 */ +/* Pure Ed448 */ + const uint8_t privkey1[57] = { 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f, @@ -494,6 +496,92 @@ const uint8_t sig9[114] = { 0xc3, 0x20, 0x86, 0x9e, 0x1a, 0x00 }; +/* Prehash Ed448 */ + +const uint8_t phprivkey1[57] = { + 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d, 0x62, 0xec, 0x77, 0x58, + 0x75, 0x20, 0x91, 0x1e, 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b, + 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42, 0xef, 0x78, 0x22, 0xe0, + 0xd5, 0x10, 0x41, 0x27, 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3, + 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c, 0x49 +}; + +const uint8_t phpubkey1[57] = { + 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77, 0xa7, 0xab, 0xd2, 0x65, + 0x24, 0xcb, 0xdb, 0x31, 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde, + 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43, 0xc0, 0x94, 0x28, 0xa1, + 0x31, 0xd6, 0xb1, 0xb5, 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76, + 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38, 0x80 +}; + +const uint8_t phmsg1[3] = { + 0x61, 0x62, 0x63 +}; + +const uint8_t phsig1[114] = { + 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d, 0x5f, 0x56, 0x2c, 0x59, + 0x29, 0x94, 0xd9, 0x69, 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56, + 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38, 0x1f, 0x54, 0xd6, 0xbc, + 0xe2, 0xea, 0x91, 0x15, 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd, + 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b, 0x80, 0x1a, 0x0d, 0x9b, + 0x3f, 0x40, 0x30, 0xcd, 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94, + 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5, 0x7d, 0xd3, 0x2d, 0xbc, + 0x56, 0x0a, 0x9a, 0x94, 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3, + 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60, 0xc3, 0xcd, 0x36, 0x96, + 0xd9, 0xd9, 0xfa, 0xb9, 0x0f, 0x00 +}; + +const uint8_t phprivkey2[57] = { + 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d, 0x62, 0xec, 0x77, 0x58, + 0x75, 0x20, 0x91, 0x1e, 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b, + 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42, 0xef, 0x78, 0x22, 0xe0, + 0xd5, 0x10, 0x41, 0x27, 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3, + 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c, 0x49 +}; + +const uint8_t phpubkey2[57] = { + 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77, 0xa7, 0xab, 0xd2, 0x65, + 0x24, 0xcb, 0xdb, 0x31, 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde, + 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43, 0xc0, 0x94, 0x28, 0xa1, + 0x31, 0xd6, 0xb1, 0xb5, 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76, + 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38, 0x80 +}; + +const uint8_t phmsg2[3] = { + 0x61, 0x62, 0x63 +}; + +const uint8_t phcontext2[3] = { + 0x66, 0x6f, 0x6f +}; + +const uint8_t phsig2[114] = { + 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02, 0xb5, 0x45, 0x40, 0x98, + 0x28, 0x14, 0xdc, 0xe9, 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49, + 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48, 0x10, 0x65, 0xb1, 0x58, + 0x04, 0x23, 0xef, 0x92, 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda, + 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2, 0x80, 0xd9, 0x12, 0x24, + 0xba, 0x99, 0x11, 0xa3, 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23, + 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30, 0x4b, 0xb7, 0x74, 0x5a, + 0x73, 0x51, 0x4c, 0xdb, 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28, + 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a, 0x6b, 0x62, 0xd9, 0x7b, + 0xeb, 0x51, 0x1d, 0x13, 0x21, 0x00 +}; + +static const uint8_t *dohash(const uint8_t *msg, size_t msglen) +{ + decaf_ed448_prehash_ctx_t hashctx; + static uint8_t hashout[64]; + + decaf_shake256_init(hashctx); + decaf_shake256_update(hashctx, msg, msglen); + decaf_shake256_final(hashctx,hashout,sizeof(hashout)); + decaf_shake256_destroy(hashctx); + + return hashout; +} + + static int test_eddsa(void) { uint8_t outsig[114]; @@ -552,6 +640,20 @@ static int test_eddsa(void) return 0; } + ED448ph_sign(outsig, dohash(phmsg1, sizeof(phmsg1)), phpubkey1, + phprivkey1, NULL, 0); + if (memcmp(phsig1, outsig, sizeof(phsig1)) != 0) { + printf("Calculated sig and expected sig differ (ph 1)\n"); + return 0; + } + + ED448ph_sign(outsig, dohash(phmsg2, sizeof(phmsg2)), phpubkey2, + phprivkey2, phcontext2, sizeof(phcontext2)); + if (memcmp(phsig2, outsig, sizeof(phsig2)) != 0) { + printf("Calculated sig and expected sig differ (ph 2)\n"); + return 0; + } + return 1; } diff --git a/crypto/ec/curve448/ed448.h b/crypto/ec/curve448/ed448.h index 631ba9d532..ebab7c2985 100644 --- a/crypto/ec/curve448/ed448.h +++ b/crypto/ec/curve448/ed448.h @@ -105,7 +105,7 @@ void decaf_ed448_sign_prehash ( uint8_t signature[DECAF_EDDSA_448_SIGNATURE_BYTES], const uint8_t privkey[DECAF_EDDSA_448_PRIVATE_BYTES], const uint8_t pubkey[DECAF_EDDSA_448_PUBLIC_BYTES], - const decaf_ed448_prehash_ctx_t hash, + const uint8_t hash[64], const uint8_t *context, uint8_t context_len ) __attribute__((nonnull(1,2,3,4))); @@ -166,7 +166,7 @@ decaf_error_t decaf_ed448_verify ( decaf_error_t decaf_ed448_verify_prehash ( const uint8_t signature[DECAF_EDDSA_448_SIGNATURE_BYTES], const uint8_t pubkey[DECAF_EDDSA_448_PUBLIC_BYTES], - const decaf_ed448_prehash_ctx_t hash, + const uint8_t hash[64], const uint8_t *context, uint8_t context_len ) __attribute__((nonnull(1,2))); diff --git a/crypto/ec/curve448/eddsa.c b/crypto/ec/curve448/eddsa.c index fe47857995..bf348a921e 100644 --- a/crypto/ec/curve448/eddsa.c +++ b/crypto/ec/curve448/eddsa.c @@ -235,20 +235,12 @@ void decaf_ed448_sign_prehash ( uint8_t signature[DECAF_EDDSA_448_SIGNATURE_BYTES], const uint8_t privkey[DECAF_EDDSA_448_PRIVATE_BYTES], const uint8_t pubkey[DECAF_EDDSA_448_PUBLIC_BYTES], - const decaf_ed448_prehash_ctx_t hash, + const uint8_t hash[64], const uint8_t *context, uint8_t context_len ) { - uint8_t hash_output[EDDSA_PREHASH_BYTES]; - { - decaf_ed448_prehash_ctx_t hash_too; - memcpy(hash_too,hash,sizeof(hash_too)); - hash_final(hash_too,hash_output,sizeof(hash_output)); - hash_destroy(hash_too); - } - - decaf_ed448_sign(signature,privkey,pubkey,hash_output,sizeof(hash_output),1,context,context_len); - OPENSSL_cleanse(hash_output,sizeof(hash_output)); + decaf_ed448_sign(signature,privkey,pubkey,hash,64,1,context,context_len); + /*OPENSSL_cleanse(hash,sizeof(hash));*/ } decaf_error_t decaf_ed448_verify ( @@ -309,28 +301,21 @@ decaf_error_t decaf_ed448_verify ( decaf_error_t decaf_ed448_verify_prehash ( const uint8_t signature[DECAF_EDDSA_448_SIGNATURE_BYTES], const uint8_t pubkey[DECAF_EDDSA_448_PUBLIC_BYTES], - const decaf_ed448_prehash_ctx_t hash, + const uint8_t hash[64], const uint8_t *context, uint8_t context_len ) { decaf_error_t ret; - uint8_t hash_output[EDDSA_PREHASH_BYTES]; - { - decaf_ed448_prehash_ctx_t hash_too; - memcpy(hash_too,hash,sizeof(hash_too)); - hash_final(hash_too,hash_output,sizeof(hash_output)); - hash_destroy(hash_too); - } - - ret = decaf_ed448_verify(signature,pubkey,hash_output,sizeof(hash_output),1,context,context_len); + ret = decaf_ed448_verify(signature,pubkey,hash,64,1,context,context_len); return ret; } int ED448_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len, const uint8_t public_key[56], const uint8_t private_key[56], - const uint8_t *context, size_t context_len) { + const uint8_t *context, size_t context_len) +{ decaf_ed448_sign(out_sig, private_key, public_key, message, message_len, 0, context, context_len); @@ -341,38 +326,32 @@ int ED448_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len, int ED448_verify(const uint8_t *message, size_t message_len, const uint8_t signature[112], const uint8_t public_key[56], - const uint8_t *context, size_t context_len) { + const uint8_t *context, size_t context_len) +{ return decaf_ed448_verify(signature, public_key, message, message_len, 0, context, context_len) == DECAF_SUCCESS; } -int ED448ph_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len, +int ED448ph_sign(uint8_t *out_sig, const uint8_t hash[64], const uint8_t public_key[56], const uint8_t private_key[56], - const uint8_t *context, size_t context_len) { - (void)out_sig; - (void)message; - (void)message_len; - (void)public_key; - (void)private_key; - (void)context; - (void)context_len; - return 0; -} + const uint8_t *context, size_t context_len) +{ + decaf_ed448_sign_prehash(out_sig, private_key, public_key, hash, context, + context_len); + return 1; +} -int ED448ph_verify(const uint8_t *message, size_t message_len, - const uint8_t signature[112], const uint8_t public_key[56], - const uint8_t *context, size_t context_len) { - (void)message; - (void)message_len; - (void)signature; - (void)public_key; - (void)context; - (void)context_len; - return 0; +int ED448ph_verify(const uint8_t hash[64], const uint8_t signature[112], + const uint8_t public_key[56], const uint8_t *context, + size_t context_len) +{ + return decaf_ed448_verify_prehash(signature, public_key, hash, context, + context_len) == DECAF_SUCCESS; } void ED448_public_from_private(uint8_t out_public_key[56], - const uint8_t private_key[56]) { + const uint8_t private_key[56]) +{ decaf_ed448_derive_public_key(out_public_key, private_key); } -- 2.25.1