| 1 | #cxx . . $ `<file>/test/test_signing.c`/include <stdbool.h> |
| 2 | #include <stdio.h> |
| 3 | |
| 4 | #include <sodium.h> |
| 5 | #include <xeddsa.h>cxx . . $ `<file>/include/xeddsa.h`/ |
| 6 | |
| 7 | #include <test.h>cxx . . $ `<file>/test/test.h`/ |
| 8 | |
| 9 | #define NUM_MESSAGEScxx . . $ `test/test_signing.c:9:9`! 64 |
| 10 | #define NUM_KEY_PAIRScxx . . $ `test/test_signing.c:10:9`! 64 |
| 11 | |
| 12 | int test_signingcxx . . $ test_signing(d1b40a211a1a4e4c).() { |
| 13 | uint8_tcxx . . $ uint8_t# curve_priv[32], curve_pub[32], priv[32]; |
| 14 | uint8_tcxx . . $ uint8_t# ed_pub[32], ed_seed[32]; |
| 15 | uint8_tcxx . . $ uint8_t# sig[64]; |
| 16 | uint8_tcxx . . $ uint8_t# nonce[64]; |
| 17 | |
| 18 | uint8_tcxx . . $ uint8_t#* msg = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 19 | uint16_tcxx . . $ uint16_t# msg_size; |
| 20 | |
| 21 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! failed = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 22 | |
| 23 | // Test on a set of different messages |
| 24 | for (uint32_tcxx . . $ uint32_t# i = 0; i < NUM_MESSAGEScxx . . $ `test/test_signing.c:9:9`!; i++) { |
| 25 | // Randomize the size of the message (0 to 65535 bytes) |
| 26 | randombytes_bufcxx . . $ randombytes_buf(5500c4293b1bbef0).(&msg_size, sizeof(uint16_tcxx . . $ uint16_t#)); |
| 27 | |
| 28 | // Allocate the buffer for the message |
| 29 | if ((msg = calloccxx . . $ calloc(63a7bf2941bfcd7a).(msg_size, sizeof(uint8_tcxx . . $ uint8_t#))) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 30 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Can't allocate enough memory.\n"); |
| 31 | return -2; |
| 32 | } |
| 33 | |
| 34 | // Fill the message with random bytes |
| 35 | randombytes_bufcxx . . $ randombytes_buf(5500c4293b1bbef0).(msg, msg_size); |
| 36 | |
| 37 | // Test each message on a set of different key pairs |
| 38 | for (uint32_tcxx . . $ uint32_t# j = 0; j < NUM_KEY_PAIRScxx . . $ `test/test_signing.c:10:9`!; j++) { |
| 39 | // Generate a random nonce |
| 40 | randombytes_bufcxx . . $ randombytes_buf(5500c4293b1bbef0).(nonce, 64); |
| 41 | |
| 42 | // Generate a Curve25519 key pair |
| 43 | randombytes_bufcxx . . $ randombytes_buf(5500c4293b1bbef0).(curve_priv, 32); |
| 44 | priv_to_curve25519_pubcxx . . $ priv_to_curve25519_pub(225f8250db12d4ac).(curve_pub, curve_priv); |
| 45 | |
| 46 | // Adjust the private key such that the sign of the derived Ed25519 public key is zero |
| 47 | priv_force_signcxx . . $ priv_force_sign(1f7a79e8cc347955).(priv, curve_priv, 0); |
| 48 | |
| 49 | // Sign the message using the Curve25519 private key |
| 50 | ed25519_priv_signcxx . . $ ed25519_priv_sign(185a75904022be09).(sig, priv, msg, msg_size, nonce); |
| 51 | |
| 52 | // Convert the Curve25519 public key to an Ed25519 public key |
| 53 | curve25519_pub_to_ed25519_pubcxx . . $ curve25519_pub_to_ed25519_pub(1f7a79e8cc347955).(ed_pub, curve_pub, 0); |
| 54 | |
| 55 | // Verify the message using the converted public key. |
| 56 | if (ed25519_verifycxx . . $ ed25519_verify(e3561976544fbcbb).(sig, ed_pub, msg, msg_size) != 0) { |
| 57 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Signature built by private key signing is not valid.\n"); |
| 58 | failed = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 59 | } |
| 60 | |
| 61 | // Convert the Curve25519 public key to an Ed25519 public key, intentionally choosing the wrong |
| 62 | // sign |
| 63 | curve25519_pub_to_ed25519_pubcxx . . $ curve25519_pub_to_ed25519_pub(1f7a79e8cc347955).(ed_pub, curve_pub, 1); |
| 64 | |
| 65 | // Verify the message using the converted public key. |
| 66 | if (ed25519_verifycxx . . $ ed25519_verify(e3561976544fbcbb).(sig, ed_pub, msg, msg_size) == 0) { |
| 67 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Signature that should be invalid was validated.\n"); |
| 68 | failed = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 69 | } |
| 70 | |
| 71 | // Adjust the private key such that the sign of the derived Ed25519 public key is one |
| 72 | priv_force_signcxx . . $ priv_force_sign(1f7a79e8cc347955).(priv, curve_priv, 1); |
| 73 | |
| 74 | // Sign the message using the Curve25519 private key |
| 75 | ed25519_priv_signcxx . . $ ed25519_priv_sign(185a75904022be09).(sig, priv, msg, msg_size, nonce); |
| 76 | |
| 77 | // Convert the Curve25519 public key to an Ed25519 public key |
| 78 | curve25519_pub_to_ed25519_pubcxx . . $ curve25519_pub_to_ed25519_pub(1f7a79e8cc347955).(ed_pub, curve_pub, 1); |
| 79 | |
| 80 | // Verify the message using the converted public key. |
| 81 | if (ed25519_verifycxx . . $ ed25519_verify(e3561976544fbcbb).(sig, ed_pub, msg, msg_size) != 0) { |
| 82 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Signature built by private key signing is not valid.\n"); |
| 83 | failed = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 84 | } |
| 85 | |
| 86 | // Convert the Curve25519 public key to an Ed25519 public key, intentionally choosing the wrong |
| 87 | // sign |
| 88 | curve25519_pub_to_ed25519_pubcxx . . $ curve25519_pub_to_ed25519_pub(1f7a79e8cc347955).(ed_pub, curve_pub, 0); |
| 89 | |
| 90 | // Verify the message using the converted public key. |
| 91 | if (ed25519_verifycxx . . $ ed25519_verify(e3561976544fbcbb).(sig, ed_pub, msg, msg_size) == 0) { |
| 92 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Signature that should be invalid was validated.\n"); |
| 93 | failed = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 94 | } |
| 95 | |
| 96 | // Generate an Ed25519 key pair |
| 97 | randombytes_bufcxx . . $ randombytes_buf(5500c4293b1bbef0).(ed_seed, 32); |
| 98 | seed_to_ed25519_pubcxx . . $ seed_to_ed25519_pub(225f8250db12d4ac).(ed_pub, ed_seed); |
| 99 | |
| 100 | // Sign the message normally |
| 101 | ed25519_seed_signcxx . . $ ed25519_seed_sign(3dcce8d6aa664ff2).(sig, ed_seed, msg, msg_size); |
| 102 | |
| 103 | // Verify the message normally |
| 104 | if (ed25519_verifycxx . . $ ed25519_verify(e3561976544fbcbb).(sig, ed_pub, msg, msg_size) != 0) { |
| 105 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Signature built by seed signing is not valid.\n"); |
| 106 | failed = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Release the memory allocated for the message |
| 111 | freecxx . . $ free(5d22bdacc48458e8).(msg); |
| 112 | msg = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 113 | } |
| 114 | |
| 115 | return failed ? -1 : 0; |
| 116 | } |