Debian Codegraph @

test/test_signing.c

raw symbols SCIP
1 #include <stdbool.h>
2 #include <stdio.h>
3
4 #include <sodium.h>
5 #include <xeddsa.h>
6
7 #include <test.h>
8
9 #define NUM_MESSAGES 64
10 #define NUM_KEY_PAIRS 64
11
12 int test_signing() {
13 uint8_t curve_priv[32], curve_pub[32], priv[32];
14 uint8_t ed_pub[32], ed_seed[32];
15 uint8_t sig[64];
16 uint8_t nonce[64];
17
18 uint8_t* msg = NULL;
19 uint16_t msg_size;
20
21 bool failed = false;
22
23 // Test on a set of different messages
24 for (uint32_t i = 0; i < NUM_MESSAGES; i++) {
25 // Randomize the size of the message (0 to 65535 bytes)
26 randombytes_buf(&msg_size, sizeof(uint16_t));
27
28 // Allocate the buffer for the message
29 if ((msg = calloc(msg_size, sizeof(uint8_t))) == NULL) {
30 fprintf(stderr, "Can't allocate enough memory.\n");
31 return -2;
32 }
33
34 // Fill the message with random bytes
35 randombytes_buf(msg, msg_size);
36
37 // Test each message on a set of different key pairs
38 for (uint32_t j = 0; j < NUM_KEY_PAIRS; j++) {
39 // Generate a random nonce
40 randombytes_buf(nonce, 64);
41
42 // Generate a Curve25519 key pair
43 randombytes_buf(curve_priv, 32);
44 priv_to_curve25519_pub(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_sign(priv, curve_priv, 0);
48
49 // Sign the message using the Curve25519 private key
50 ed25519_priv_sign(sig, priv, msg, msg_size, nonce);
51
52 // Convert the Curve25519 public key to an Ed25519 public key
53 curve25519_pub_to_ed25519_pub(ed_pub, curve_pub, 0);
54
55 // Verify the message using the converted public key.
56 if (ed25519_verify(sig, ed_pub, msg, msg_size) != 0) {
57 fprintf(stderr, "Signature built by private key signing is not valid.\n");
58 failed = true;
59 }
60
61 // Convert the Curve25519 public key to an Ed25519 public key, intentionally choosing the wrong
62 // sign
63 curve25519_pub_to_ed25519_pub(ed_pub, curve_pub, 1);
64
65 // Verify the message using the converted public key.
66 if (ed25519_verify(sig, ed_pub, msg, msg_size) == 0) {
67 fprintf(stderr, "Signature that should be invalid was validated.\n");
68 failed = true;
69 }
70
71 // Adjust the private key such that the sign of the derived Ed25519 public key is one
72 priv_force_sign(priv, curve_priv, 1);
73
74 // Sign the message using the Curve25519 private key
75 ed25519_priv_sign(sig, priv, msg, msg_size, nonce);
76
77 // Convert the Curve25519 public key to an Ed25519 public key
78 curve25519_pub_to_ed25519_pub(ed_pub, curve_pub, 1);
79
80 // Verify the message using the converted public key.
81 if (ed25519_verify(sig, ed_pub, msg, msg_size) != 0) {
82 fprintf(stderr, "Signature built by private key signing is not valid.\n");
83 failed = true;
84 }
85
86 // Convert the Curve25519 public key to an Ed25519 public key, intentionally choosing the wrong
87 // sign
88 curve25519_pub_to_ed25519_pub(ed_pub, curve_pub, 0);
89
90 // Verify the message using the converted public key.
91 if (ed25519_verify(sig, ed_pub, msg, msg_size) == 0) {
92 fprintf(stderr, "Signature that should be invalid was validated.\n");
93 failed = true;
94 }
95
96 // Generate an Ed25519 key pair
97 randombytes_buf(ed_seed, 32);
98 seed_to_ed25519_pub(ed_pub, ed_seed);
99
100 // Sign the message normally
101 ed25519_seed_sign(sig, ed_seed, msg, msg_size);
102
103 // Verify the message normally
104 if (ed25519_verify(sig, ed_pub, msg, msg_size) != 0) {
105 fprintf(stderr, "Signature built by seed signing is not valid.\n");
106 failed = true;
107 }
108 }
109
110 // Release the memory allocated for the message
111 free(msg);
112 msg = NULL;
113 }
114
115 return failed ? -1 : 0;
116 }