diff options
Diffstat (limited to 'test/default/sign.c')
-rw-r--r-- | test/default/sign.c | 114 |
1 files changed, 80 insertions, 34 deletions
diff --git a/test/default/sign.c b/test/default/sign.c index 7267491..99ed085 100644 --- a/test/default/sign.c +++ b/test/default/sign.c @@ -1,23 +1,14 @@ -#include <stdio.h> -#include <string.h> #define TEST_NAME "sign" #include "cmptest.h" -#define KEYPAIR_SEED "1@ABCFGHLOPRSTUWabcdefghiklmnopq" - -#define crypto_sign_SECRETKEYBYTES_WITHOUT_PK \ - (crypto_sign_SECRETKEYBYTES - crypto_sign_PUBLICKEYBYTES) - -static const unsigned char keypair_seed[] = { - 0x42, 0x11, 0x51, 0xa4, 0x59, 0xfa, 0xea, 0xde, - 0x3d, 0x24, 0x71, 0x15, 0xf9, 0x4a, 0xed, 0xae, - 0x42, 0x31, 0x81, 0x24, 0x09, 0x5a, 0xfa, 0xbe, - 0x4d, 0x14, 0x51, 0xa5, 0x59, 0xfa, 0xed, 0xee -}; +static const unsigned char keypair_seed[] + = { 0x42, 0x11, 0x51, 0xa4, 0x59, 0xfa, 0xea, 0xde, 0x3d, 0x24, 0x71, + 0x15, 0xf9, 0x4a, 0xed, 0xae, 0x42, 0x31, 0x81, 0x24, 0x09, 0x5a, + 0xfa, 0xbe, 0x4d, 0x14, 0x51, 0xa5, 0x59, 0xfa, 0xed, 0xee }; typedef struct TestData_ { - const unsigned char sk[crypto_sign_SECRETKEYBYTES_WITHOUT_PK]; + const unsigned char sk[crypto_sign_SEEDBYTES]; const unsigned char pk[crypto_sign_PUBLICKEYBYTES]; const unsigned char sig[crypto_sign_BYTES]; const char *m; @@ -1052,26 +1043,29 @@ static TestData test_data[] = { int main(void) { - unsigned char sig[crypto_sign_BYTES]; - unsigned char sm[1024 + crypto_sign_BYTES]; - unsigned char m[1024]; - unsigned char skpk[crypto_sign_SECRETKEYBYTES + - crypto_sign_PUBLICKEYBYTES]; - unsigned char pk[crypto_sign_PUBLICKEYBYTES]; - unsigned char sk[crypto_sign_SECRETKEYBYTES]; - char pk_hex[crypto_sign_PUBLICKEYBYTES * 2 + 1]; - char sk_hex[crypto_sign_SECRETKEYBYTES * 2 + 1]; + unsigned char extracted_seed[crypto_sign_ed25519_SEEDBYTES]; + unsigned char extracted_pk[crypto_sign_ed25519_PUBLICKEYBYTES]; + unsigned char sig[crypto_sign_BYTES]; + unsigned char sm[1024 + crypto_sign_BYTES]; + unsigned char m[1024]; + unsigned char skpk[crypto_sign_SECRETKEYBYTES]; + unsigned char pk[crypto_sign_PUBLICKEYBYTES]; + unsigned char sk[crypto_sign_SECRETKEYBYTES]; + char pk_hex[crypto_sign_PUBLICKEYBYTES * 2 + 1]; + char sk_hex[crypto_sign_SECRETKEYBYTES * 2 + 1]; unsigned long long siglen; unsigned long long smlen; unsigned long long mlen; - unsigned int i; + unsigned int i; + unsigned int j; + memset(sig, 0, sizeof sig); for (i = 0U; i < (sizeof test_data) / (sizeof test_data[0]); i++) { - memcpy(skpk, test_data[i].sk, crypto_sign_SECRETKEYBYTES_WITHOUT_PK); - memcpy(skpk + crypto_sign_SECRETKEYBYTES_WITHOUT_PK, - test_data[i].pk, crypto_sign_PUBLICKEYBYTES); - if (crypto_sign(sm, &smlen, - (const unsigned char *) test_data[i].m, i, skpk) != 0) { + memcpy(skpk, test_data[i].sk, crypto_sign_SEEDBYTES); + memcpy(skpk + crypto_sign_SEEDBYTES, test_data[i].pk, + crypto_sign_PUBLICKEYBYTES); + if (crypto_sign(sm, &smlen, (const unsigned char *)test_data[i].m, i, + skpk) != 0) { printf("crypto_sign() failure: [%u]\n", i); continue; } @@ -1083,7 +1077,7 @@ int main(void) printf("crypto_sign_open() failure: [%u]\n", i); continue; } - if (memcmp(test_data[i].m, m, (size_t) mlen) != 0) { + if (memcmp(test_data[i].m, m, (size_t)mlen) != 0) { printf("message verification failure: [%u]\n", i); continue; } @@ -1095,12 +1089,12 @@ int main(void) if (crypto_sign_open(m, &mlen, sm, i % crypto_sign_BYTES, test_data[i].pk) == 0) { printf("short signed message verifies: [%u]\n", - i % crypto_sign_BYTES); + i % crypto_sign_BYTES); continue; } if (crypto_sign_detached(sig, &siglen, - (const unsigned char *) test_data[i].m, i, - test_data[i].sk) != 0) { + (const unsigned char *)test_data[i].m, i, skpk) + != 0) { printf("detached signature failed: [%u]\n", i); continue; } @@ -1113,7 +1107,7 @@ int main(void) continue; } if (crypto_sign_verify_detached(sig, - (const unsigned char *) test_data[i].m, + (const unsigned char *)test_data[i].m, i, test_data[i].pk) != 0) { printf("detached signature verification failed: [%u]\n", i); continue; @@ -1121,6 +1115,37 @@ int main(void) } printf("%u tests\n", i); + i--; + + memcpy(sm, test_data[i].m, i); + if (crypto_sign(sm, &smlen, sm, i, skpk) != 0) { + printf("crypto_sign() with overlap failed\n"); + } + if (crypto_sign_open(sm, &mlen, sm, smlen, test_data[i].pk) != 0) { + printf("crypto_sign_open() with overlap failed\n"); + } + if (memcmp(test_data[i].m, sm, (size_t)mlen) != 0) { + printf("crypto_sign_open() with overlap failed (content)\n"); + } + + for (j = 1U; j < 8U; j++) { + sig[63] ^= (j << 5); + if (crypto_sign_verify_detached(sig, + (const unsigned char *)test_data[i].m, + i, test_data[i].pk) != -1) { + printf("detached signature verification should have failed\n"); + continue; + } + sig[63] ^= (j << 5); + } + + memset(pk, 0, sizeof pk); + if (crypto_sign_verify_detached(sig, + (const unsigned char *)test_data[i].m, + i, pk) != -1) { + printf("detached signature verification should have failed\n"); + } + if (crypto_sign_keypair(pk, sk) != 0) { printf("crypto_sign_keypair() failure\n"); } @@ -1128,11 +1153,32 @@ int main(void) printf("crypto_sign_seed_keypair() failure\n"); return -1; } + crypto_sign_ed25519_sk_to_seed(extracted_seed, sk); + if (memcmp(extracted_seed, keypair_seed, crypto_sign_ed25519_SEEDBYTES) + != 0) { + printf("crypto_sign_ed25519_sk_to_seed() failure\n"); + } + crypto_sign_ed25519_sk_to_pk(extracted_pk, sk); + if (memcmp(extracted_pk, pk, crypto_sign_ed25519_PUBLICKEYBYTES) != 0) { + printf("crypto_sign_ed25519_sk_to_pk() failure\n"); + } sodium_bin2hex(pk_hex, sizeof pk_hex, pk, sizeof pk); sodium_bin2hex(sk_hex, sizeof sk_hex, sk, sizeof sk); printf("pk: [%s]\n", pk_hex); printf("sk: [%s]\n", sk_hex); + assert(crypto_sign_bytes() > 0U); + assert(crypto_sign_seedbytes() > 0U); + assert(crypto_sign_publickeybytes() > 0U); + assert(crypto_sign_secretkeybytes() > 0U); + assert(strcmp(crypto_sign_primitive(), "ed25519") == 0); + assert(crypto_sign_bytes() == crypto_sign_ed25519_bytes()); + assert(crypto_sign_seedbytes() == crypto_sign_ed25519_seedbytes()); + assert(crypto_sign_publickeybytes() + == crypto_sign_ed25519_publickeybytes()); + assert(crypto_sign_secretkeybytes() + == crypto_sign_ed25519_secretkeybytes()); + return 0; } |