Debian Codegraph @

cipher/dilithium.h

raw
1 /* dilithium.h - the Dilithium (header)
2 * Copyright (C) 2025 g10 Code GmbH
3 *
4 * This file was modified for use by Libgcrypt.
5 *
6 * This file is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This file is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <https://www.gnu.org/licenses/>.
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 *
20 * You can also use this file under the same licence of original code.
21 * SPDX-License-Identifier: CC0 OR Apache-2.0
22 *
23 */
24 /*
25 Original code from:
26
27 Repository: https://github.com/pq-crystals/dilithium.git
28 Branch: master
29 Commit: 444cdcc84eb36b66fe27b3a2529ee48f6d8150c2
30
31 Licence:
32 Public Domain (https://creativecommons.org/share-your-work/public-domain/cc0/);
33 or Apache 2.0 License (https://www.apache.org/licenses/LICENSE-2.0.html).
34
35 Authors:
36 Léo Ducas
37 Eike Kiltz
38 Tancrède Lepoint
39 Vadim Lyubashevsky
40 Gregor Seiler
41 Peter Schwabe
42 Damien Stehlé
43
44 Dilithium Home: https://github.com/pq-crystals/dilithium.git
45 */
46 /* Standalone use is possible either with DILITHIUM_MODE defined with
47 * the value (2, 3, or 5), or not defined. For the latter, routines
48 * for three variants are available.
49 */
50 #ifndef DILITHIUM_H
51 #define DILITHIUM_H
52
53 #define SEEDBYTES 32
54 #define RNDBYTES 32
55
56 #ifdef _GCRYPT_IN_LIBGCRYPT
57 /**** Start of the glue code to libgcrypt ****/
58 #define dilithium_keypair _gcry_mldsa_keypair
59 #define dilithium_sign _gcry_mldsa_sign
60 #define dilithium_verify _gcry_mldsa_verify
61 /**** End of the glue code ****/
62
63 #define DILITHIUM_KEYPAIR_STACK_BURN (128 * 1024)
64 #define DILITHIUM_SIGN_STACK_BURN (161 * 1024)
65 #define DILITHIUM_VERIFY_STACK_BURN (122 * 1024)
66
67 gpg_err_code_t dilithium_keypair (int algo, uint8_t *pk, uint8_t *sk,
68 const uint8_t seed[SEEDBYTES]);
69 gpg_err_code_t dilithium_sign (int algo, uint8_t *sig, size_t siglen,
70 const uint8_t *m, size_t mlen,
71 const uint8_t *ctx, size_t ctxlen,
72 const uint8_t *sk, const uint8_t rnd[RNDBYTES]);
73 gpg_err_code_t dilithium_verify (int algo, const uint8_t *sig, size_t siglen,
74 const uint8_t *m, size_t mlen,
75 const uint8_t *ctx, size_t ctxlen,
76 const uint8_t *pk);
77 #endif
78
79 #if defined(DILITHIUM_MODE)
80 #ifndef DILITHIUM_INTERNAL_API_ONLY
81 int crypto_sign_keypair (uint8_t *pk, uint8_t *sk);
82 int crypto_sign_signature(uint8_t *sig, size_t *siglen,
83 const uint8_t *m, size_t mlen,
84 const uint8_t *ctx, size_t ctxlen,
85 const uint8_t *sk);
86 int crypto_sign (uint8_t *sm, size_t *smlen,
87 const uint8_t *m, size_t mlen,
88 const uint8_t *ctx, size_t ctxlen,
89 const uint8_t *sk);
90 int crypto_sign_verify(const uint8_t *sig, size_t siglen,
91 const uint8_t *m, size_t mlen,
92 const uint8_t *ctx, size_t ctxlen,
93 const uint8_t *pk);
94 int crypto_sign_open (uint8_t *m, size_t *mlen,
95 const uint8_t *sm, size_t smlen,
96 const uint8_t *ctx, size_t ctxlen,
97 const uint8_t *pk);
98 #endif
99 int crypto_sign_keypair_internal (uint8_t *pk, uint8_t *sk,
100 const uint8_t seed[SEEDBYTES]);
101 int crypto_sign_signature_internal (uint8_t *sig, size_t *siglen,
102 const uint8_t *m, size_t mlen,
103 const uint8_t *pre, size_t prelen,
104 const uint8_t rnd[RNDBYTES],
105 const uint8_t *sk);
106 int crypto_sign_verify_internal (const uint8_t *sig, size_t siglen,
107 const uint8_t *m, size_t mlen,
108 const uint8_t *pre, size_t prelen,
109 const uint8_t *pk);
110
111 # if DILITHIUM_MODE == 2
112 # define CRYPTO_PUBLICKEYBYTES (SEEDBYTES + 4*320)
113 # define CRYPTO_SECRETKEYBYTES (2*SEEDBYTES \
114 + 64 \
115 + 4*96 \
116 + 4*96 \
117 + 4*416)
118 # define CRYPTO_BYTES (32 + 4*576 + 80 + 4)
119 # elif DILITHIUM_MODE == 3
120 # define CRYPTO_PUBLICKEYBYTES (SEEDBYTES + 6*320)
121 # define CRYPTO_SECRETKEYBYTES (2*SEEDBYTES \
122 + 64 \
123 + 5*128 \
124 + 6*128 \
125 + 6*416)
126 # define CRYPTO_BYTES (48 + 5*640 + 55 + 6)
127 # elif DILITHIUM_MODE == 5
128 # define CRYPTO_PUBLICKEYBYTES (SEEDBYTES + 8*320)
129 # define CRYPTO_SECRETKEYBYTES (2*SEEDBYTES \
130 + 64 \
131 + 7*96 \
132 + 8*96 \
133 + 8*416)
134 # define CRYPTO_BYTES (64 + 7*640 + 75 + 8)
135 # else
136 # error "DILITHIUM_MODE should be either 2, 3 or 5"
137 # endif
138 #else
139 # ifndef DILITHIUM_INTERNAL_API_ONLY
140 int crypto_sign_keypair_2 (uint8_t *pk, uint8_t *sk);
141 int crypto_sign_keypair_3 (uint8_t *pk, uint8_t *sk);
142 int crypto_sign_keypair_5 (uint8_t *pk, uint8_t *sk);
143 int crypto_sign_2 (uint8_t *sm, size_t *smlen,
144 const uint8_t *m, size_t mlen,
145 const uint8_t *ctx, size_t ctxlen,
146 const uint8_t *sk);
147 int crypto_sign_3 (uint8_t *sm, size_t *smlen,
148 const uint8_t *m, size_t mlen,
149 const uint8_t *ctx, size_t ctxlen,
150 const uint8_t *sk);
151 int crypto_sign_5 (uint8_t *sm, size_t *smlen,
152 const uint8_t *m, size_t mlen,
153 const uint8_t *ctx, size_t ctxlen,
154 const uint8_t *sk);
155 int crypto_sign_open_2 (uint8_t *m, size_t *mlen,
156 const uint8_t *sm, size_t smlen,
157 const uint8_t *ctx, size_t ctxlen,
158 const uint8_t *pk);
159 int crypto_sign_open_3 (uint8_t *m, size_t *mlen,
160 const uint8_t *sm, size_t smlen,
161 const uint8_t *ctx, size_t ctxlen,
162 const uint8_t *pk);
163 int crypto_sign_open_5 (uint8_t *m, size_t *mlen,
164 const uint8_t *sm, size_t smlen,
165 const uint8_t *ctx, size_t ctxlen,
166 const uint8_t *pk);
167 # endif
168
169 # define CRYPTO_PUBLICKEYBYTES_2 (SEEDBYTES + 4*320)
170 # define CRYPTO_SECRETKEYBYTES_2 (2*SEEDBYTES \
171 + 64 \
172 + 4*96 \
173 + 4*96 \
174 + 4*416)
175 # define CRYPTO_BYTES_2 (32 + 4*576 + 80 + 4)
176
177 # define CRYPTO_PUBLICKEYBYTES_3 (SEEDBYTES + 6*320)
178 # define CRYPTO_SECRETKEYBYTES_3 (2*SEEDBYTES \
179 + 64 \
180 + 5*128 \
181 + 6*128 \
182 + 6*416)
183 # define CRYPTO_BYTES_3 (48 + 5*640 + 55 + 6)
184
185 # define CRYPTO_PUBLICKEYBYTES_5 (SEEDBYTES + 8*320)
186 # define CRYPTO_SECRETKEYBYTES_5 (2*SEEDBYTES \
187 + 64 \
188 + 7*96 \
189 + 8*96 \
190 + 8*416)
191 # define CRYPTO_BYTES_5 (64 + 7*640 + 75 + 8)
192 #endif
193
194 #endif