Debian Codegraph @

others/mbedtls/tf-psa-crypto/drivers/pqcp/mldsa-native/test/src/gen_KAT.c

raw symbols SCIP
1 /*
2 * Copyright (c) The mldsa-native project authors
3 * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
4 */
5
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include "src/common.h"
10
11 #include "../notrandombytes/notrandombytes.h"
12 #include "mldsa_native.h"
13 #include "src/fips202/fips202.h"
14 #include "src/sys.h"
15
16 /* Additional SUPERCOP-style macros for functions not in the standard set */
17 #define crypto_sign_keypair_internal MLD_API_NAMESPACE(keypair_internal)
18 #define crypto_sign_signature_internal MLD_API_NAMESPACE(signature_internal)
19
20 #if defined(MLD_SYS_WINDOWS)
21 #include <fcntl.h>
22 #include <io.h>
23 #endif
24
25 #define MAXMLEN 2048
26 #define CTXLEN 0
27
28 #define CHECK(x) \
29 do \
30 { \
31 int r; \
32 r = (x); \
33 if (!r) \
34 { \
35 fprintf(stderr, "ERROR (%s,%d)\n", __FILE__, __LINE__); \
36 return 1; \
37 } \
38 } while (0)
39
40
41
42 static void print_hex(const uint8_t *data, size_t size)
43 {
44 size_t i;
45 for (i = 0; i < size; i++)
46 {
47 printf("%02x", data[i]);
48 }
49 printf("\n");
50 }
51
52 int main(void)
53 {
54 unsigned i;
55 int rc;
56 uint8_t pk[CRYPTO_PUBLICKEYBYTES];
57 uint8_t sk[CRYPTO_SECRETKEYBYTES];
58 uint8_t s[CRYPTO_BYTES];
59 uint8_t *m;
60 /* empty ctx */
61 uint8_t pre[2] = {0, 0};
62 size_t slen;
63
64 const uint8_t seed[64] = {
65 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
66 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
67 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
68 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
69 };
70 uint8_t coins[MLDSA_SEEDBYTES + MLDSA_RNDBYTES + MAXMLEN];
71
72 #if defined(MLD_SYS_WINDOWS)
73 /* Disable automatic CRLF conversion on Windows to match testvector hashes */
74 _setmode(_fileno(stdout), _O_BINARY);
75 #endif
76
77 /*
78 * We cannot rely on randombytes in the KAT test as randombytes() is used
79 * inside of crypto_sign_signature() which is called as a part of
80 * key generation in case PCT (pairwise-consistency test) is enabled.
81 * To allow KAT tests to still pass successfully, we derandomize the
82 * KAT test to only use deterministic randomness derived using SHAKE.
83 */
84
85 mld_shake256(coins, sizeof(coins), seed, sizeof(seed));
86
87 for (i = 0; i < MAXMLEN; i = (i == 0) ? i + 1 : i << 2)
88 {
89 mld_shake256(coins, sizeof(coins), coins, sizeof(coins));
90 m = coins + MLDSA_SEEDBYTES + MLDSA_RNDBYTES;
91
92 CHECK(crypto_sign_keypair_internal(pk, sk, coins) == 0);
93
94 print_hex(pk, CRYPTO_PUBLICKEYBYTES);
95 print_hex(sk, CRYPTO_SECRETKEYBYTES);
96
97 CHECK(crypto_sign_signature_internal(s, &slen, m, i, pre, sizeof(pre),
98 coins + MLDSA_SEEDBYTES, sk, 0) == 0);
99
100 print_hex(s, slen);
101
102 rc = crypto_sign_verify(s, slen, m, i, NULL, CTXLEN, pk);
103
104 if (rc)
105 {
106 printf("ERROR: signature verification failed\n");
107 return -1;
108 }
109 }
110 return 0;
111 }