Debian Codegraph @

cipher/kyber.h

raw
1 /* kyber.h - the Kyber key encapsulation mechanism (header)
2 * Copyright (C) 2024 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/kyber.git
28 Branch: standard
29 Commit: 11d00ff1f20cfca1f72d819e5a45165c1e0a2816
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 Joppe Bos
37 Léo Ducas
38 Eike Kiltz
39 Tancrède Lepoint
40 Vadim Lyubashevsky
41 John Schanck
42 Peter Schwabe
43 Gregor Seiler
44 Damien Stehlé
45
46 Kyber Home: https://www.pq-crystals.org/kyber/
47 */
48 /* Standalone use is possible either with KYBER_K defined with the
49 * value (2, 3, or 4), or not defined. For the latter, routines for
50 * three variants are available.
51 */
52
53 #ifndef KYBER_H
54 #define KYBER_H
55
56 #ifdef _GCRYPT_IN_LIBGCRYPT
57 /**** Start of the glue code to libgcrypt ****/
58 #define kyber_keypair _gcry_mlkem_keypair
59 #define kyber_encap _gcry_mlkem_encap
60 #define kyber_decap _gcry_mlkem_decap
61 /**** End of the glue code ****/
62
63 #define KYBER_KEYPAIR_STACK_BURN(algo) \
64 ((algo) == GCRY_KEM_MLKEM512 ? (9 * 1024) : \
65 (algo) == GCRY_KEM_MLKEM768 ? (15 * 1024) : \
66 /* MLKEM1024 */ (21 * 1024))
67 #define KYBER_ENCAP_STACK_BURN(algo) \
68 ((algo) == GCRY_KEM_MLKEM512 ? (13 * 1024) : \
69 (algo) == GCRY_KEM_MLKEM768 ? (19 * 1024) : \
70 /* MLKEM1024 */ (26 * 1024))
71 #define KYBER_DECAP_STACK_BURN(algo) \
72 ((algo) == GCRY_KEM_MLKEM512 ? (18 * 1024) : \
73 (algo) == GCRY_KEM_MLKEM768 ? (25 * 1024) : \
74 /* MLKEM1024 */ (35 * 1024))
75
76 void kyber_keypair (int algo, uint8_t *pk, uint8_t *sk, const uint8_t *coins);
77 void kyber_encap (int algo, uint8_t *ct, uint8_t *ss, const uint8_t *pk,
78 const uint8_t *coins);
79 void kyber_decap (int algo, uint8_t *ss, const uint8_t *ct, const uint8_t *sk);
80 #elif defined(KYBER_K)
81 int crypto_kem_keypair_derand (uint8_t *pk, uint8_t *sk, const uint8_t *coins);
82 int crypto_kem_keypair (uint8_t *pk, uint8_t *sk);
83 int crypto_kem_enc_derand (uint8_t *ct, uint8_t *ss, const uint8_t *pk,
84 const uint8_t *coins);
85 int crypto_kem_enc (uint8_t *ct, uint8_t *ss, const uint8_t *pk);
86 int crypto_kem_dec (uint8_t *ss, const uint8_t *ct, const uint8_t *sk);
87 # if KYBER_K == 2
88 # define CRYPTO_SECRETKEYBYTES (2*384+2*384+32+2*32)
89 # define CRYPTO_PUBLICKEYBYTES (2*384+32)
90 # define CRYPTO_CIPHERTEXTBYTES (128+2*320)
91 # define CRYPTO_BYTES 32
92 # define CRYPTO_ALGNAME "Kyber512"
93 # elif KYBER_K == 3
94 # define CRYPTO_SECRETKEYBYTES (3*384+3*384+32+2*32)
95 # define CRYPTO_PUBLICKEYBYTES (3*384+32)
96 # define CRYPTO_CIPHERTEXTBYTES (128+3*320)
97 # define CRYPTO_BYTES 32
98 # define CRYPTO_ALGNAME "Kyber768"
99 # elif KYBER_K == 4
100 # define CRYPTO_SECRETKEYBYTES (4*384+2*384+32+2*32)
101 # define CRYPTO_PUBLICKEYBYTES (4*384+32)
102 # define CRYPTO_CIPHERTEXTBYTES (160+2*352)
103 # define CRYPTO_BYTES 32
104 # define CRYPTO_ALGNAME "Kyber1024"
105 # else
106 # define CRYPTO_SECRETKEYBYTES_512 (2*384+2*384+32+2*32)
107 # define CRYPTO_PUBLICKEYBYTES_512 (2*384+32)
108 # define CRYPTO_CIPHERTEXTBYTES_512 (128+2*320)
109 # define CRYPTO_BYTES_512 32
110
111 # define CRYPTO_SECRETKEYBYTES_768 (3*384+3*384+32+2*32)
112 # define CRYPTO_PUBLICKEYBYTES_768 (3*384+32)
113 # define CRYPTO_CIPHERTEXTBYTES_768 (128+3*320)
114 # define CRYPTO_BYTES_768 32
115
116 # define CRYPTO_SECRETKEYBYTES_1024 (4*384+2*384+32+2*32)
117 # define CRYPTO_PUBLICKEYBYTES_1024 (4*384+32)
118 # define CRYPTO_CIPHERTEXTBYTES_1024 (160+2*352)
119 # define CRYPTO_BYTES_1024 32
120
121 # define CRYPTO_ALGNAME "Kyber"
122
123 # define crypto_kem_keypair_derand_2 crypto_kem_keypair_derand_512
124 # define crypto_kem_keypair_derand_3 crypto_kem_keypair_derand_768
125 # define crypto_kem_keypair_derand_4 crypto_kem_keypair_derand_1024
126
127 int crypto_kem_keypair_derand_2 (uint8_t *pk, uint8_t *sk,
128 const uint8_t *coins);
129 int crypto_kem_keypair_derand_3 (uint8_t *pk, uint8_t *sk,
130 const uint8_t *coins);
131 int crypto_kem_keypair_derand_4 (uint8_t *pk, uint8_t *sk,
132 const uint8_t *coins);
133
134 # define crypto_kem_keypair_2 crypto_kem_keypair_512
135 # define crypto_kem_keypair_3 crypto_kem_keypair_768
136 # define crypto_kem_keypair_4 crypto_kem_keypair_1024
137
138 int crypto_kem_keypair_2 (uint8_t *pk, uint8_t *sk);
139 int crypto_kem_keypair_3 (uint8_t *pk, uint8_t *sk);
140 int crypto_kem_keypair_4 (uint8_t *pk, uint8_t *sk);
141
142 # define crypto_kem_enc_derand_2 crypto_kem_enc_derand_512
143 # define crypto_kem_enc_derand_3 crypto_kem_enc_derand_768
144 # define crypto_kem_enc_derand_4 crypto_kem_enc_derand_1024
145 int crypto_kem_enc_derand_2 (uint8_t *ct, uint8_t *ss, const uint8_t *pk,
146 const uint8_t *coins);
147 int crypto_kem_enc_derand_3 (uint8_t *ct, uint8_t *ss, const uint8_t *pk,
148 const uint8_t *coins);
149 int crypto_kem_enc_derand_4 (uint8_t *ct, uint8_t *ss, const uint8_t *pk,
150 const uint8_t *coins);
151
152 # define crypto_kem_enc_2 crypto_kem_enc_512
153 # define crypto_kem_enc_3 crypto_kem_enc_768
154 # define crypto_kem_enc_4 crypto_kem_enc_1024
155 int crypto_kem_enc_2 (uint8_t *ct, uint8_t *ss, const uint8_t *pk);
156 int crypto_kem_enc_3 (uint8_t *ct, uint8_t *ss, const uint8_t *pk);
157 int crypto_kem_enc_4 (uint8_t *ct, uint8_t *ss, const uint8_t *pk);
158
159 # define crypto_kem_dec_2 crypto_kem_dec_512
160 # define crypto_kem_dec_3 crypto_kem_dec_768
161 # define crypto_kem_dec_4 crypto_kem_dec_1024
162 int crypto_kem_dec_2 (uint8_t *ss, const uint8_t *ct, const uint8_t *sk);
163 int crypto_kem_dec_3 (uint8_t *ss, const uint8_t *ct, const uint8_t *sk);
164 int crypto_kem_dec_4 (uint8_t *ss, const uint8_t *ct, const uint8_t *sk);
165 # endif
166 #endif
167
168 #endif /* KYBER_H */