@pinkparrot/qsafe-mayo-wasm 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/.gitmodules +3 -0
  2. package/.vscode/launch.json +12 -0
  3. package/LICENSE +201 -0
  4. package/bridge/mayo1_bridge.c +26 -0
  5. package/bridge/mayo2_bridge.c +26 -0
  6. package/bridge/randombytes_inject.c +44 -0
  7. package/build_mayo1.ps1 +36 -0
  8. package/build_mayo2.ps1 +36 -0
  9. package/dist/mayo.browser.min.js +216 -0
  10. package/dist/mayo1.js +0 -0
  11. package/dist/mayo2.js +0 -0
  12. package/dist/mayo_api.js +139 -0
  13. package/dist/package.json +1 -0
  14. package/gitignore +2 -0
  15. package/index.mjs +1 -0
  16. package/mayo-c/.astylerc +16 -0
  17. package/mayo-c/.cmake/flags.cmake +45 -0
  18. package/mayo-c/.cmake/sanitizers.cmake +81 -0
  19. package/mayo-c/.cmake/target.cmake +71 -0
  20. package/mayo-c/.github/workflows/ci_clang.yml +61 -0
  21. package/mayo-c/.github/workflows/ci_gcc.yml +60 -0
  22. package/mayo-c/.github/workflows/cmake.yml +160 -0
  23. package/mayo-c/.github/workflows/macos_m1.yml +68 -0
  24. package/mayo-c/CMakeLists.txt +35 -0
  25. package/mayo-c/KAT/PQCsignKAT_24_MAYO_1.req +900 -0
  26. package/mayo-c/KAT/PQCsignKAT_24_MAYO_1.rsp +902 -0
  27. package/mayo-c/KAT/PQCsignKAT_24_MAYO_2.req +900 -0
  28. package/mayo-c/KAT/PQCsignKAT_24_MAYO_2.rsp +902 -0
  29. package/mayo-c/KAT/PQCsignKAT_32_MAYO_3.req +900 -0
  30. package/mayo-c/KAT/PQCsignKAT_32_MAYO_3.rsp +902 -0
  31. package/mayo-c/KAT/PQCsignKAT_40_MAYO_5.req +900 -0
  32. package/mayo-c/KAT/PQCsignKAT_40_MAYO_5.rsp +902 -0
  33. package/mayo-c/LICENSE +202 -0
  34. package/mayo-c/META/MAYO-1_META.yml +52 -0
  35. package/mayo-c/META/MAYO-2_META.yml +52 -0
  36. package/mayo-c/META/MAYO-3_META.yml +52 -0
  37. package/mayo-c/META/MAYO-5_META.yml +52 -0
  38. package/mayo-c/NOTICE +13 -0
  39. package/mayo-c/README.md +183 -0
  40. package/mayo-c/apps/CMakeLists.txt +31 -0
  41. package/mayo-c/apps/PQCgenKAT_sign.c +281 -0
  42. package/mayo-c/apps/example.c +151 -0
  43. package/mayo-c/apps/example_nistapi.c +124 -0
  44. package/mayo-c/include/mayo.h +442 -0
  45. package/mayo-c/include/mem.h +25 -0
  46. package/mayo-c/include/randombytes.h +31 -0
  47. package/mayo-c/scripts/contstants.py +141 -0
  48. package/mayo-c/scripts/find_irred_poly.sage +39 -0
  49. package/mayo-c/src/AVX2/arithmetic_common.h +159 -0
  50. package/mayo-c/src/AVX2/echelon_form.h +91 -0
  51. package/mayo-c/src/AVX2/echelon_form_loop.h +58 -0
  52. package/mayo-c/src/AVX2/shuffle_arithmetic.h +442 -0
  53. package/mayo-c/src/CMakeLists.txt +98 -0
  54. package/mayo-c/src/arithmetic.c +128 -0
  55. package/mayo-c/src/arithmetic.h +124 -0
  56. package/mayo-c/src/common/aes128ctr.c +293 -0
  57. package/mayo-c/src/common/aes_c.c +741 -0
  58. package/mayo-c/src/common/aes_ctr.h +32 -0
  59. package/mayo-c/src/common/aes_neon.c +201 -0
  60. package/mayo-c/src/common/debug_bench_tools.h +69 -0
  61. package/mayo-c/src/common/fips202.c +1093 -0
  62. package/mayo-c/src/common/fips202.h +12 -0
  63. package/mayo-c/src/common/mem.c +19 -0
  64. package/mayo-c/src/common/randombytes_ctrdrbg.c +141 -0
  65. package/mayo-c/src/common/randombytes_system.c +399 -0
  66. package/mayo-c/src/generic/arithmetic_dynamic.h +68 -0
  67. package/mayo-c/src/generic/arithmetic_fixed.h +84 -0
  68. package/mayo-c/src/generic/echelon_form.h +152 -0
  69. package/mayo-c/src/generic/ef_inner_loop.h +56 -0
  70. package/mayo-c/src/generic/generic_arithmetic.h +294 -0
  71. package/mayo-c/src/mayo.c +675 -0
  72. package/mayo-c/src/mayo_1/api.c +46 -0
  73. package/mayo-c/src/mayo_1/api.h +43 -0
  74. package/mayo-c/src/mayo_2/api.c +46 -0
  75. package/mayo-c/src/mayo_2/api.h +43 -0
  76. package/mayo-c/src/mayo_3/api.c +46 -0
  77. package/mayo-c/src/mayo_3/api.h +43 -0
  78. package/mayo-c/src/mayo_5/api.c +46 -0
  79. package/mayo-c/src/mayo_5/api.h +43 -0
  80. package/mayo-c/src/neon/arithmetic_common.h +132 -0
  81. package/mayo-c/src/neon/echelon_form.h +55 -0
  82. package/mayo-c/src/neon/echelon_form_loop.h +58 -0
  83. package/mayo-c/src/neon/shuffle_arithmetic.h +462 -0
  84. package/mayo-c/src/params.c +42 -0
  85. package/mayo-c/src/simple_arithmetic.h +138 -0
  86. package/mayo-c/test/CMakeLists.txt +51 -0
  87. package/mayo-c/test/bench.c +166 -0
  88. package/mayo-c/test/m1cycles.c +155 -0
  89. package/mayo-c/test/m1cycles.h +13 -0
  90. package/mayo-c/test/test_kat.c +271 -0
  91. package/mayo-c/test/test_mayo.c +139 -0
  92. package/mayo-c/test/test_sample_solution.c +75 -0
  93. package/mayo-c/test/test_various.c +680 -0
  94. package/package.json +39 -0
  95. package/publish.bat +22 -0
  96. package/readme.md +80 -0
  97. package/test/test.mjs +42 -0
@@ -0,0 +1,281 @@
1
+ // SPDX-License-Identifier: Apache-2.0 and Unknown
2
+
3
+ /*
4
+ NIST-developed software is provided by NIST as a public service. You may use,
5
+ copy, and distribute copies of the software in any medium, provided that you
6
+ keep intact this entire notice. You may improve, modify, and create derivative
7
+ works of the software or any portion of the software, and you may copy and
8
+ distribute such modifications or works. Modified works should carry a notice
9
+ stating that you changed the software and should note the date and nature of any
10
+ such change. Please explicitly acknowledge the National Institute of Standards
11
+ and Technology as the source of the software.
12
+
13
+ NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF
14
+ ANY KIND, EXPRESS, IMPLIED, IN FACT, OR ARISING BY OPERATION OF LAW, INCLUDING,
15
+ WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
16
+ PARTICULAR PURPOSE, NON-INFRINGEMENT, AND DATA ACCURACY. NIST NEITHER REPRESENTS
17
+ NOR WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR
18
+ ERROR-FREE, OR THAT ANY DEFECTS WILL BE CORRECTED. NIST DOES NOT WARRANT OR MAKE
19
+ ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS THEREOF,
20
+ INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR
21
+ USEFULNESS OF THE SOFTWARE.
22
+
23
+ You are solely responsible for determining the appropriateness of using and
24
+ distributing the software and you assume all risks associated with its use,
25
+ including but not limited to the risks and costs of program errors, compliance
26
+ with applicable laws, damage to or loss of data, programs or equipment, and the
27
+ unavailability or interruption of operation. This software is not intended to be
28
+ used in any situation where a failure could cause risk of injury or damage to
29
+ property. The software developed by NIST employees is not subject to copyright
30
+ protection within the United States.
31
+ */
32
+
33
+ #include "api.h"
34
+ #include "randombytes.h"
35
+ #include <ctype.h>
36
+ #include <stdio.h>
37
+ #include <stdlib.h>
38
+ #include <string.h>
39
+
40
+ #define MAX_MARKER_LEN 50
41
+
42
+ #define KAT_SUCCESS 0
43
+ #define KAT_FILE_OPEN_ERROR -1
44
+ #define KAT_DATA_ERROR -3
45
+ #define KAT_CRYPTO_FAILURE -4
46
+
47
+ int FindMarker(FILE *infile, const char *marker);
48
+ int ReadHex(FILE *infile, unsigned char *A, int Length, char *str);
49
+ void fprintBstr(FILE *fp, char *S, unsigned char *A, size_t L);
50
+
51
+ int main(void) {
52
+ char fn_req[32], fn_rsp[32];
53
+ FILE *fp_req, *fp_rsp;
54
+ unsigned char seed[48];
55
+ unsigned char msg[3300];
56
+ unsigned char entropy_input[48];
57
+ unsigned char *m, *sm, *m1;
58
+ size_t mlen, smlen, mlen1;
59
+ int count;
60
+ int done;
61
+ unsigned char pk[CRYPTO_PUBLICKEYBYTES], sk[CRYPTO_SECRETKEYBYTES];
62
+ int ret_val;
63
+
64
+ // Create the REQUEST file
65
+ sprintf(fn_req, "PQCsignKAT_%d_%s.req", CRYPTO_SECRETKEYBYTES,
66
+ CRYPTO_ALGNAME);
67
+ if ((fp_req = fopen(fn_req, "w")) == NULL) {
68
+ printf("Couldn't open <%s> for write\n", fn_req);
69
+ return KAT_FILE_OPEN_ERROR;
70
+ }
71
+ sprintf(fn_rsp, "PQCsignKAT_%d_%s.rsp", CRYPTO_SECRETKEYBYTES,
72
+ CRYPTO_ALGNAME);
73
+ if ((fp_rsp = fopen(fn_rsp, "w")) == NULL) {
74
+ printf("Couldn't open <%s> for write\n", fn_rsp);
75
+ return KAT_FILE_OPEN_ERROR;
76
+ }
77
+
78
+ for (int i = 0; i < 48; i++)
79
+ entropy_input[i] = i;
80
+
81
+ randombytes_init(entropy_input, NULL, 256);
82
+ for (int i = 0; i < 100; i++) {
83
+ fprintf(fp_req, "count = %d\n", i);
84
+ randombytes(seed, 48);
85
+ fprintBstr(fp_req, "seed = ", seed, 48);
86
+ mlen = 33 * (i + 1);
87
+ fprintf(fp_req, "mlen = %zu\n", mlen);
88
+ randombytes(msg, mlen);
89
+ fprintBstr(fp_req, "msg = ", msg, mlen);
90
+ fprintf(fp_req, "pk =\n");
91
+ fprintf(fp_req, "sk =\n");
92
+ fprintf(fp_req, "smlen =\n");
93
+ fprintf(fp_req, "sm =\n\n");
94
+ }
95
+ fclose(fp_req);
96
+
97
+ // Create the RESPONSE file based on what's in the REQUEST file
98
+ if ((fp_req = fopen(fn_req, "r")) == NULL) {
99
+ printf("Couldn't open <%s> for read\n", fn_req);
100
+ return KAT_FILE_OPEN_ERROR;
101
+ }
102
+
103
+ fprintf(fp_rsp, "# %s\n\n", CRYPTO_ALGNAME);
104
+ done = 0;
105
+ do {
106
+ if (FindMarker(fp_req, "count = ")) {
107
+ if (fscanf(fp_req, "%d", &count) != 1)
108
+ return KAT_DATA_ERROR;
109
+ } else {
110
+ done = 1;
111
+ break;
112
+ }
113
+ fprintf(fp_rsp, "count = %d\n", count);
114
+
115
+ if (!ReadHex(fp_req, seed, 48, "seed = ")) {
116
+ printf("ERROR: unable to read 'seed' from <%s>\n", fn_req);
117
+ return KAT_DATA_ERROR;
118
+ }
119
+ fprintBstr(fp_rsp, "seed = ", seed, 48);
120
+
121
+ randombytes_init(seed, NULL, 256);
122
+
123
+ if (FindMarker(fp_req, "mlen = ")) {
124
+ if (fscanf(fp_req, "%zu", &mlen) != 1)
125
+ return KAT_DATA_ERROR;
126
+ } else {
127
+ printf("ERROR: unable to read 'mlen' from <%s>\n", fn_req);
128
+ return KAT_DATA_ERROR;
129
+ }
130
+ fprintf(fp_rsp, "mlen = %zu\n", mlen);
131
+
132
+ m = (unsigned char *)calloc(mlen, sizeof(unsigned char));
133
+ m1 = (unsigned char *)calloc(mlen + CRYPTO_BYTES, sizeof(unsigned char));
134
+ sm = (unsigned char *)calloc(mlen + CRYPTO_BYTES, sizeof(unsigned char));
135
+
136
+ if (!ReadHex(fp_req, m, (int)mlen, "msg = ")) {
137
+ printf("ERROR: unable to read 'msg' from <%s>\n", fn_req);
138
+ return KAT_DATA_ERROR;
139
+ }
140
+ fprintBstr(fp_rsp, "msg = ", m, mlen);
141
+
142
+ // Generate the public/private keypair
143
+ if ((ret_val = crypto_sign_keypair(pk, sk)) != 0) {
144
+ printf("crypto_sign_keypair returned <%d>\n", ret_val);
145
+ return KAT_CRYPTO_FAILURE;
146
+ }
147
+ fprintBstr(fp_rsp, "pk = ", pk, CRYPTO_PUBLICKEYBYTES);
148
+ fprintBstr(fp_rsp, "sk = ", sk, CRYPTO_SECRETKEYBYTES);
149
+
150
+ if ((ret_val = crypto_sign(sm, &smlen, m, mlen, sk)) != 0) {
151
+ printf("crypto_sign returned <%d>\n", ret_val);
152
+ return KAT_CRYPTO_FAILURE;
153
+ }
154
+ fprintf(fp_rsp, "smlen = %zu\n", smlen);
155
+ fprintBstr(fp_rsp, "sm = ", sm, smlen);
156
+ fprintf(fp_rsp, "\n");
157
+
158
+ if ((ret_val = crypto_sign_open(m1, &mlen1, sm, smlen, pk)) != 0) {
159
+ printf("crypto_sign_open returned <%d>\n", ret_val);
160
+ return KAT_CRYPTO_FAILURE;
161
+ }
162
+
163
+ if (mlen != mlen1) {
164
+ printf(
165
+ "crypto_sign_open returned bad 'mlen': Got <%zu>, expected <%zu>\n",
166
+ mlen1, mlen);
167
+ return KAT_CRYPTO_FAILURE;
168
+ }
169
+
170
+ if (memcmp(m, m1, mlen)) {
171
+ printf("crypto_sign_open returned bad 'm' value\n");
172
+ return KAT_CRYPTO_FAILURE;
173
+ }
174
+
175
+ free(m);
176
+ free(m1);
177
+ free(sm);
178
+
179
+ } while (!done);
180
+
181
+ fclose(fp_req);
182
+ fclose(fp_rsp);
183
+
184
+ return KAT_SUCCESS;
185
+ }
186
+
187
+ //
188
+ // ALLOW TO READ HEXADECIMAL ENTRY (KEYS, DATA, TEXT, etc.)
189
+ //
190
+ int FindMarker(FILE *infile, const char *marker) {
191
+ char line[MAX_MARKER_LEN];
192
+ int i, len;
193
+ int curr_line;
194
+
195
+ len = (int)strlen(marker);
196
+ if (len > MAX_MARKER_LEN - 1)
197
+ len = MAX_MARKER_LEN - 1;
198
+
199
+ for (i = 0; i < len; i++) {
200
+ curr_line = fgetc(infile);
201
+ line[i] = curr_line;
202
+ if (curr_line == EOF)
203
+ return 0;
204
+ }
205
+ line[len] = '\0';
206
+
207
+ while (1) {
208
+ if (!strncmp(line, marker, len))
209
+ return 1;
210
+
211
+ for (i = 0; i < len - 1; i++)
212
+ line[i] = line[i + 1];
213
+ curr_line = fgetc(infile);
214
+ line[len - 1] = curr_line;
215
+ if (curr_line == EOF)
216
+ return 0;
217
+ line[len] = '\0';
218
+ }
219
+
220
+ // shouldn't get here
221
+ return 0;
222
+ }
223
+
224
+ //
225
+ // ALLOW TO READ HEXADECIMAL ENTRY (KEYS, DATA, TEXT, etc.)
226
+ //
227
+ int ReadHex(FILE *infile, unsigned char *A, int Length, char *str) {
228
+ int i, ch, started;
229
+ unsigned char ich;
230
+
231
+ if (Length == 0) {
232
+ A[0] = 0x00;
233
+ return 1;
234
+ }
235
+ memset(A, 0x00, Length);
236
+ started = 0;
237
+ if (FindMarker(infile, str))
238
+ while ((ch = fgetc(infile)) != EOF) {
239
+ if (!isxdigit(ch)) {
240
+ if (!started) {
241
+ if (ch == '\n')
242
+ break;
243
+ else
244
+ continue;
245
+ } else
246
+ break;
247
+ }
248
+ started = 1;
249
+ if ((ch >= '0') && (ch <= '9'))
250
+ ich = ch - '0';
251
+ else if ((ch >= 'A') && (ch <= 'F'))
252
+ ich = ch - 'A' + 10;
253
+ else if ((ch >= 'a') && (ch <= 'f'))
254
+ ich = ch - 'a' + 10;
255
+ else // shouldn't ever get here
256
+ ich = 0;
257
+
258
+ for (i = 0; i < Length - 1; i++)
259
+ A[i] = (A[i] << 4) | (A[i + 1] >> 4);
260
+ A[Length - 1] = (A[Length - 1] << 4) | ich;
261
+ }
262
+ else
263
+ return 0;
264
+
265
+ return 1;
266
+ }
267
+
268
+ void fprintBstr(FILE *fp, char *S, unsigned char *A, size_t L) {
269
+ size_t i;
270
+
271
+ fprintf(fp, "%s", S);
272
+
273
+ for (i = 0; i < L; i++)
274
+ fprintf(fp, "%02X", A[i]);
275
+
276
+ if (L == 0)
277
+ fprintf(fp, "00");
278
+
279
+ fprintf(fp, "\n");
280
+ }
281
+
@@ -0,0 +1,151 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+
3
+ /**
4
+ * An example to demonstrate how to use the MAYO API.
5
+ */
6
+
7
+ #include <mayo.h>
8
+ #include <mem.h>
9
+ #include <stdlib.h>
10
+ #include <string.h>
11
+ #include <stdio.h>
12
+
13
+ /**
14
+ * Example for MAYO variant:
15
+ * - mayo_keypair
16
+ * - mayo_expand_pk
17
+ * - mayo_expand_sk
18
+ * - mayo_sign
19
+ * - mayo_verify
20
+ * - mayo_open
21
+ *
22
+ * @param[in] p MAYO parameter
23
+ * @return int return code
24
+ */
25
+ static int example_mayo(const mayo_params_t* p) {
26
+
27
+ size_t msglen = 32;
28
+ size_t smlen = PARAM_sig_bytes(p) + msglen;
29
+
30
+ unsigned char *pk = calloc(PARAM_cpk_bytes(p), 1);
31
+ unsigned char *sk = calloc(PARAM_csk_bytes(p), 1);
32
+
33
+ uint64_t *epk = calloc(1, sizeof(pk_t));
34
+ sk_t *esk = calloc(1, sizeof(sk_t));
35
+
36
+ unsigned char *sig = calloc(PARAM_sig_bytes(p) + msglen, 1);
37
+
38
+ unsigned char msg[32] = { 0xe };
39
+ unsigned char msg2[32] = { 0 };
40
+
41
+ printf("Example with %s\n", PARAM_name(p));
42
+
43
+ printf("mayo_keypair -> ");
44
+ int res = mayo_keypair(p, pk, sk);
45
+ if (res != MAYO_OK) {
46
+ printf("FAIL\n");
47
+ res = -1;
48
+ goto err;
49
+ } else {
50
+ printf("OK\n");
51
+ }
52
+
53
+ printf("mayo_expand_sk -> ");
54
+ res = mayo_expand_sk(p, sk, esk);
55
+ if (res != MAYO_OK) {
56
+ printf("FAIL\n");
57
+ res = -1;
58
+ goto err;
59
+ } else {
60
+ printf("OK\n");
61
+ }
62
+
63
+ printf("mayo_expand_pk -> ");
64
+ res = mayo_expand_pk(p, pk, epk);
65
+ if (res != MAYO_OK) {
66
+ printf("FAIL\n");
67
+ res = -1;
68
+ goto err;
69
+ } else {
70
+ printf("OK\n");
71
+ }
72
+
73
+ printf("mayo_sign -> ");
74
+ res = mayo_sign(p, sig, &smlen, msg, msglen, sk);
75
+ if (res != MAYO_OK) {
76
+ printf("FAIL\n");
77
+ res = -1;
78
+ goto err;
79
+ } else {
80
+ printf("OK\n");
81
+ }
82
+
83
+ printf("mayo_open (with correct signature) -> ");
84
+ res = mayo_open(p, msg2, &msglen, sig, smlen, pk);
85
+ if (res != MAYO_OK || memcmp(msg, msg2, msglen)) {
86
+ printf("FAIL\n");
87
+ res = -1;
88
+ goto err;
89
+ } else {
90
+ res = MAYO_OK;
91
+ printf("OK\n");
92
+ }
93
+
94
+ printf("mayo_verify (with correct signature) -> ");
95
+ res = mayo_verify(p, msg, msglen, sig, pk);
96
+ if (res != MAYO_OK) {
97
+ printf("FAIL\n");
98
+ res = -1;
99
+ goto err;
100
+ } else {
101
+ res = MAYO_OK;
102
+ printf("OK\n");
103
+ }
104
+
105
+ printf("mayo_open (with altered signature) -> ");
106
+ sig[0] = ~sig[0];
107
+ memset(msg2, 0, msglen);
108
+ res = mayo_open(p, msg2, &msglen, sig, smlen, pk);
109
+ if (res != MAYO_ERR || !memcmp(msg, msg2, msglen)) {
110
+ printf("FAIL\n");
111
+ res = -1;
112
+ goto err;
113
+ } else {
114
+ res = MAYO_OK;
115
+ printf("OK\n");
116
+ }
117
+
118
+ printf("mayo_verify (with altered signature) -> ");
119
+ res = mayo_verify(p, msg, msglen, sig, pk);
120
+ if (res == MAYO_OK) {
121
+ printf("FAIL\n");
122
+ res = -1;
123
+ goto err;
124
+ } else {
125
+ res = MAYO_OK;
126
+ printf("OK\n");
127
+ }
128
+
129
+ err:
130
+ free(pk);
131
+ free(epk);
132
+ mayo_secure_free(sk, PARAM_csk_bytes(p));
133
+ mayo_secure_free(esk, sizeof(sk_t));
134
+ free(sig);
135
+ return res;
136
+ }
137
+
138
+ int main(void) {
139
+ #ifdef ENABLE_PARAMS_DYNAMIC
140
+ const mayo_params_t* params[] = {&MAYO_1, &MAYO_2, &MAYO_3, &MAYO_5};
141
+ int paramslen = sizeof(params) / sizeof(params[0]);
142
+ for (int i = 0; i < paramslen; ++i) {
143
+ if (example_mayo(params[i]) != MAYO_OK) {
144
+ return -1;
145
+ }
146
+ }
147
+ #else
148
+ return example_mayo(0);
149
+ #endif
150
+ }
151
+
@@ -0,0 +1,124 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+
3
+ /**
4
+ * An example to demonstrate how to use MAYO with the NIST API.
5
+ */
6
+
7
+ #include <api.h>
8
+ #include <mem.h>
9
+ #include <string.h>
10
+ #include <stdlib.h>
11
+ #include <stdio.h>
12
+
13
+ /**
14
+ * Example for MAYO variant:
15
+ * - crypto_sign_keypair
16
+ * - crypto_sign
17
+ * - crypto_sign_open
18
+ *
19
+ * @return int return code
20
+ */
21
+ static int example_mayo(void) {
22
+
23
+ size_t msglen = 32;
24
+ size_t smlen = CRYPTO_BYTES + msglen;
25
+ size_t siglen = CRYPTO_BYTES;
26
+
27
+ unsigned char *pk = calloc(CRYPTO_PUBLICKEYBYTES, 1);
28
+ unsigned char *sk = calloc(CRYPTO_SECRETKEYBYTES, 1);
29
+
30
+ unsigned char *sig = calloc(smlen, 1);
31
+
32
+ unsigned char msg[32] = { 0xe };
33
+ unsigned char msg2[32] = { 0 };
34
+
35
+ printf("Example with %s\n", CRYPTO_ALGNAME);
36
+
37
+ printf("crypto_sign_keypair -> ");
38
+ int res = crypto_sign_keypair(pk, sk);
39
+ if (res) {
40
+ printf("FAIL\n");
41
+ res = -1;
42
+ goto err;
43
+ } else {
44
+ printf("OK\n");
45
+ }
46
+
47
+ printf("crypto_sign -> ");
48
+ res = crypto_sign(sig, &smlen, msg, msglen, sk);
49
+ if (res) {
50
+ printf("FAIL\n");
51
+ res = -1;
52
+ goto err;
53
+ } else {
54
+ printf("OK\n");
55
+ }
56
+
57
+ printf("crypto_sign_open (with correct signature) -> ");
58
+ res = crypto_sign_open(msg2, &msglen, sig, smlen, pk);
59
+ if (res || memcmp(msg, msg2, msglen)) {
60
+ printf("FAIL\n");
61
+ res = -1;
62
+ goto err;
63
+ } else {
64
+ res = 0;
65
+ printf("OK\n");
66
+ }
67
+
68
+ printf("crypto_sign_open (with altered signature) -> ");
69
+ sig[0] = ~sig[0];
70
+ memset(msg2, 0, msglen);
71
+ res = crypto_sign_open(msg2, &msglen, sig, smlen, pk);
72
+ if (!res || !memcmp(msg, msg2, msglen)) {
73
+ printf("FAIL\n");
74
+ res = -1;
75
+ goto err;
76
+ } else {
77
+ res = 0;
78
+ printf("OK\n");
79
+ }
80
+
81
+ printf("crypto_sign_signature -> ");
82
+ res = crypto_sign_signature(sig, &siglen, msg, msglen, sk);
83
+ if (res) {
84
+ printf("FAIL\n");
85
+ res = -1;
86
+ goto err;
87
+ } else {
88
+ printf("OK\n");
89
+ }
90
+
91
+ printf("crypto_sign_verify (with correct signature) -> ");
92
+ res = crypto_sign_verify(sig, siglen, msg, msglen, pk);
93
+ if (res) {
94
+ printf("FAIL\n");
95
+ res = -1;
96
+ goto err;
97
+ } else {
98
+ res = 0;
99
+ printf("OK\n");
100
+ }
101
+
102
+ printf("crypto_sign_verify (with altered signature) -> ");
103
+ sig[0] = ~sig[0];
104
+ res = crypto_sign_verify(sig, siglen, msg, msglen, pk);
105
+ if (!res) {
106
+ printf("FAIL\n");
107
+ res = -1;
108
+ goto err;
109
+ } else {
110
+ res = 0;
111
+ printf("OK\n");
112
+ }
113
+
114
+ err:
115
+ free(pk);
116
+ mayo_secure_free(sk, CRYPTO_SECRETKEYBYTES);
117
+ free(sig);
118
+ return res;
119
+ }
120
+
121
+ int main(void) {
122
+ return example_mayo();
123
+ }
124
+