|
10 | 10 |
|
11 | 11 | #include <sodium.h> |
12 | 12 |
|
| 13 | +int init_and_check_sodium() |
| 14 | +{ |
| 15 | + if (sodium_init() == -1) { |
| 16 | + return -1; |
| 17 | + } |
| 18 | + |
| 19 | + // What follows is a runtime test that ensures the version of libsodium |
| 20 | + // we're linked against checks that signatures are canonical (s < L). |
| 21 | + const unsigned char message[1] = { 0 }; |
| 22 | + |
| 23 | + unsigned char pk[crypto_sign_PUBLICKEYBYTES]; |
| 24 | + unsigned char sk[crypto_sign_SECRETKEYBYTES]; |
| 25 | + unsigned char sig[crypto_sign_BYTES]; |
| 26 | + |
| 27 | + crypto_sign_keypair(pk, sk); |
| 28 | + crypto_sign_detached(sig, NULL, message, sizeof(message), sk); |
| 29 | + |
| 30 | + assert(crypto_sign_verify_detached(sig, message, sizeof(message), pk) == 0); |
| 31 | + |
| 32 | + // Copied from libsodium/crypto_sign/ed25519/ref10/open.c |
| 33 | + static const unsigned char L[32] = |
| 34 | + { 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, |
| 35 | + 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, |
| 36 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 37 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 }; |
| 38 | + |
| 39 | + // Add L to S, which starts at sig[32]. |
| 40 | + unsigned int s = 0; |
| 41 | + for (size_t i = 0; i < 32; i++) { |
| 42 | + s = sig[32 + i] + L[i] + (s >> 8); |
| 43 | + sig[32 + i] = s & 0xff; |
| 44 | + } |
| 45 | + |
| 46 | + assert(crypto_sign_verify_detached(sig, message, sizeof(message), pk) != 0); |
| 47 | + |
| 48 | + return 0; |
| 49 | +} |
| 50 | + |
13 | 51 | void TestLibsodiumEd25519SignatureVerification( |
14 | 52 | const std::string &scope, |
15 | 53 | const std::string &msg, |
|
0 commit comments