|
| 1 | +/* |
| 2 | +20141017 |
| 3 | +Jan Mojzis |
| 4 | +Public domain. |
| 5 | +*/ |
| 6 | + |
| 7 | +#include "misc.h" |
| 8 | +#include "crypto_scalarmult_curve25519.h" |
| 9 | + |
| 10 | +#define BYTES crypto_scalarmult_curve25519_BYTES |
| 11 | +#define SCALARBYTES crypto_scalarmult_curve25519_SCALARBYTES |
| 12 | + |
| 13 | +static unsigned char pk[BYTES]; |
| 14 | + |
| 15 | +static unsigned char S[BYTES] = { 9 }; |
| 16 | + |
| 17 | +static unsigned char R[BYTES] = { |
| 18 | + 0xf9, 0xc3, 0xda, 0xc2, 0x10, 0x4c, 0x80, 0xb2, |
| 19 | + 0x52, 0xd0, 0xae, 0xec, 0x37, 0x7a, 0xfd, 0x5d, |
| 20 | + 0x1e, 0xf2, 0xc8, 0xc3, 0x48, 0xc2, 0x9e, 0x12, |
| 21 | + 0xdd, 0xb2, 0xd0, 0xc8, 0xb1, 0x98, 0xff, 0x7f |
| 22 | +}; |
| 23 | + |
| 24 | +static unsigned char d[SCALARBYTES] = { |
| 25 | + 0x56, 0x2c, 0x1e, 0xb5, 0xfd, 0xb2, 0x81, 0x29, |
| 26 | + 0xbd, 0x37, 0x49, 0x58, 0x35, 0xd4, 0xb1, 0x30, |
| 27 | + 0x7d, 0xdb, 0x57, 0x38, 0x80, 0x12, 0x17, 0x42, |
| 28 | + 0xf7, 0x13, 0xf1, 0x05, 0x67, 0x69, 0xd5, 0xbf |
| 29 | +}; |
| 30 | + |
| 31 | +static void test_vector(void) { |
| 32 | + |
| 33 | + long long j; |
| 34 | + unsigned char r[BYTES]; |
| 35 | + |
| 36 | + if (crypto_scalarmult_curve25519(r, d, S) != 0) fail("crypto_scalarmult_curve25519() failure"); |
| 37 | + for (j = 0; j < BYTES; ++j) if (r[j] != R[j]) fail("crypto_scalarmult_curve25519() failure"); |
| 38 | +} |
| 39 | + |
| 40 | +static unsigned char skdata[1080][32] = { |
| 41 | +#include "precomp.data" |
| 42 | +}; |
| 43 | + |
| 44 | +static unsigned char pkdata[1080][32] = { |
| 45 | +#include "precomp_curve25519.data" |
| 46 | +}; |
| 47 | + |
| 48 | +static void test_base(void) { |
| 49 | + |
| 50 | + long long i, j; |
| 51 | + |
| 52 | + checksum_zero(); |
| 53 | + for (i = 0; i < 1080; ++i) { |
| 54 | + if (crypto_scalarmult_curve25519_base(pk, skdata[i]) != 0) { |
| 55 | + fail_printdata("sk", skdata[i], SCALARBYTES); |
| 56 | + fail("crypto_scalarmult_curve25519_base() failure, please report it !!!!!!!!!"); |
| 57 | + } |
| 58 | + for (j = 0; j < BYTES; ++j) if (pk[j] != pkdata[i][j]) { |
| 59 | + fail_printdata("pk_computed", pk, BYTES); |
| 60 | + fail_printdata("pk_expected", pkdata[i], BYTES); |
| 61 | + fail_printdata("sk", skdata[i], SCALARBYTES); |
| 62 | + fail("crypto_scalarmult_curve25519() failure, please report it !!!!!!!!!"); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +static const unsigned char basepoint[BYTES] = { 9 }; |
| 68 | + |
| 69 | +static unsigned char test_scalarmult_checksum[32] = { |
| 70 | + 0xca, 0xf4, 0xa3, 0xbe, 0x00, 0x9c, 0x6f, 0x01, |
| 71 | + 0xb8, 0x4e, 0xf8, 0x0d, 0x0a, 0x17, 0x16, 0x42, |
| 72 | + 0xfe, 0x01, 0x59, 0x40, 0x74, 0xd4, 0xa6, 0x48, |
| 73 | + 0x07, 0x94, 0x95, 0x94, 0xab, 0xa4, 0x5e, 0x6d |
| 74 | +}; |
| 75 | + |
| 76 | +static void test_scalarmult(void) { |
| 77 | + |
| 78 | + long long i, j; |
| 79 | + unsigned char outpk[BYTES]; |
| 80 | + |
| 81 | + for (i = 0; i < BYTES; ++i) pk[i] = basepoint[i]; |
| 82 | + |
| 83 | + checksum_zero(); |
| 84 | + for (i = 0; i < 1080; ++i) { |
| 85 | + pk[31] |= 128; |
| 86 | + if (crypto_scalarmult_curve25519(outpk, skdata[i], pk) != 0) { |
| 87 | + fail_printdata("pk", pk, BYTES); |
| 88 | + fail_printdata("sk", skdata[i], SCALARBYTES); |
| 89 | + fail("crypto_scalarmult_curve25519() failure, please report it !!!!!!!!!"); |
| 90 | + } |
| 91 | + checksum(outpk, BYTES); |
| 92 | + for (j = 0; j < BYTES; ++j) pk[j] = outpk[j]; |
| 93 | + } |
| 94 | + fail_whenbadchecksum(test_scalarmult_checksum); |
| 95 | +} |
| 96 | + |
| 97 | +static void test_random(void) { |
| 98 | + |
| 99 | + long long i, j; |
| 100 | + unsigned char sk1[SCALARBYTES + 16]; |
| 101 | + unsigned char pk1[BYTES + 16]; |
| 102 | + unsigned char k1[BYTES + 16]; |
| 103 | + unsigned char sk2[SCALARBYTES + 16]; |
| 104 | + unsigned char pk2[BYTES + 16]; |
| 105 | + unsigned char k2[BYTES + 16]; |
| 106 | + |
| 107 | + |
| 108 | + for (i = 0; i < 16; ++i) { |
| 109 | + unsaferandombytes(sk1 + i, SCALARBYTES); |
| 110 | + unsaferandombytes(sk2 + i, SCALARBYTES); |
| 111 | + if (crypto_scalarmult_curve25519_base(pk1 + i, sk1 + i) != 0) goto fail; |
| 112 | + pk1[31 + i] |= 128; |
| 113 | + if (crypto_scalarmult_curve25519_base(pk2 + i, sk2 + i) != 0) goto fail; |
| 114 | + pk2[31 + i] |= 128; |
| 115 | + if (crypto_scalarmult_curve25519(k1 + i, sk1 + i, pk2 + i) != 0) goto fail; |
| 116 | + if (crypto_scalarmult_curve25519(k2 + i, sk2 + i, pk1 + i) != 0) goto fail; |
| 117 | + for (j = 0; j < BYTES; ++j) if (k1[j + i] != k2[j + i]) goto fail; |
| 118 | + } |
| 119 | + return; |
| 120 | + |
| 121 | +fail: |
| 122 | + fail_printdata("sk1", sk1 + i, SCALARBYTES); |
| 123 | + fail_printdata("sk2", sk2 + i, SCALARBYTES); |
| 124 | + fail("crypto_scalarmult_curve25519() failure, please report it !!!!!!!!!"); |
| 125 | +} |
| 126 | + |
| 127 | +int main(void) { |
| 128 | + |
| 129 | + test_vector(); |
| 130 | + test_base(); |
| 131 | + test_scalarmult(); |
| 132 | + test_random(); |
| 133 | + _exit(0); |
| 134 | +} |
0 commit comments