Leancrypto 0.12.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
lc_dilithium_44.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 - 2024, Stephan Mueller <smueller@chronox.de>
3 *
4 * License: see LICENSE file in root directory
5 *
6 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
7 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
9 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
10 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
12 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
13 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
14 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 */
19/*
20 * This code is derived in parts from the code distribution provided with
21 * https://github.com/pq-crystals/dilithium
22 *
23 * That code is released under Public Domain
24 * (https://creativecommons.org/share-your-work/public-domain/cc0/);
25 * or Apache 2.0 License (https://www.apache.org/licenses/LICENSE-2.0.html).
26 */
27
28#ifndef LC_DILITHIUM_44_H
29#define LC_DILITHIUM_44_H
30
31#ifndef __ASSEMBLER__
32
33#include "ext_headers.h"
34#include "lc_hash.h"
35#include "lc_rng.h"
36#include "lc_sha3.h"
37
38#endif /* __ASSEMBLER__ */
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
45/*
46 * Dilithium Security Levels
47 * 2 -> 192 bits of security strength
48 * 3 -> 225 bits of security strength
49 * 5 -> 257 bits of security strength
50 */
51#define LC_DILITHIUM_MODE 2
52
53#define LC_DILITHIUM_SEEDBYTES 32
54#define LC_DILITHIUM_CRHBYTES 64
55#define LC_DILITHIUM_TRBYTES 64
56#define LC_DILITHIUM_RNDBYTES 32
57#define LC_DILITHIUM_N 256
58#define LC_DILITHIUM_Q 8380417
59#define LC_DILITHIUM_D 13
60#define LC_DILITHIUM_ROOT_OF_UNITY 1753
61
62#if LC_DILITHIUM_MODE == 2
63#define LC_DILITHIUM_LAMBDA 128
64#define LC_DILITHIUM_K 4
65#define LC_DILITHIUM_L 4
66#define LC_DILITHIUM_ETA 2
67#define LC_DILITHIUM_TAU 39
68#define LC_DILITHIUM_BETA 78
69#define LC_DILITHIUM_GAMMA1 (1 << 17)
70#define LC_DILITHIUM_GAMMA2 ((LC_DILITHIUM_Q - 1) / 88)
71#define LC_DILITHIUM_OMEGA 80
72
73#elif LC_DILITHIUM_MODE == 3
74#define LC_DILITHIUM_LAMBDA 192
75#define LC_DILITHIUM_K 6
76#define LC_DILITHIUM_L 5
77#define LC_DILITHIUM_ETA 4
78#define LC_DILITHIUM_TAU 49
79#define LC_DILITHIUM_BETA 196
80#define LC_DILITHIUM_GAMMA1 (1 << 19)
81#define LC_DILITHIUM_GAMMA2 ((LC_DILITHIUM_Q - 1) / 32)
82#define LC_DILITHIUM_OMEGA 55
83
84#elif LC_DILITHIUM_MODE == 5
85#define LC_DILITHIUM_LAMBDA 256
86#define LC_DILITHIUM_K 8
87#define LC_DILITHIUM_L 7
88#define LC_DILITHIUM_ETA 2
89#define LC_DILITHIUM_TAU 60
90#define LC_DILITHIUM_BETA 120
91#define LC_DILITHIUM_GAMMA1 (1 << 19)
92#define LC_DILITHIUM_GAMMA2 ((LC_DILITHIUM_Q - 1) / 32)
93#define LC_DILITHIUM_OMEGA 75
94
95#endif
96
97#define LC_DILITHIUM_CTILDE_BYTES (LC_DILITHIUM_LAMBDA * 2 / 8)
98#define LC_DILITHIUM_POLYT1_PACKEDBYTES 320
99#define LC_DILITHIUM_POLYT0_PACKEDBYTES 416
100#define LC_DILITHIUM_POLYVECH_PACKEDBYTES (LC_DILITHIUM_OMEGA + LC_DILITHIUM_K)
101
102#if LC_DILITHIUM_GAMMA1 == (1 << 17)
103#define LC_DILITHIUM_POLYZ_PACKEDBYTES 576
104#elif LC_DILITHIUM_GAMMA1 == (1 << 19)
105#define LC_DILITHIUM_POLYZ_PACKEDBYTES 640
106#endif
107
108#if LC_DILITHIUM_GAMMA2 == (LC_DILITHIUM_Q - 1) / 88
109#define LC_DILITHIUM_POLYW1_PACKEDBYTES 192
110#elif LC_DILITHIUM_GAMMA2 == (LC_DILITHIUM_Q - 1) / 32
111#define LC_DILITHIUM_POLYW1_PACKEDBYTES 128
112#endif
113
114#if LC_DILITHIUM_ETA == 2
115#define LC_DILITHIUM_POLYETA_PACKEDBYTES 96
116#elif LC_DILITHIUM_ETA == 4
117#define LC_DILITHIUM_POLYETA_PACKEDBYTES 128
118#endif
119
120/*
121 * Sizes of the different Dilithium buffer types.
122 *
123 * WARNING: Do not use these defines in your code. If you need the sizes of
124 * the different variable sizes, use sizeof of the different variable structs or
125 * use the different *_size functions documented below to retrieve the data size
126 * of a particular Dilithium component.
127 */
128#define LC_DILITHIUM_PUBLICKEYBYTES \
129 (LC_DILITHIUM_SEEDBYTES + \
130 LC_DILITHIUM_K * LC_DILITHIUM_POLYT1_PACKEDBYTES)
131#define LC_DILITHIUM_SECRETKEYBYTES \
132 (2 * LC_DILITHIUM_SEEDBYTES + LC_DILITHIUM_TRBYTES + \
133 LC_DILITHIUM_L * LC_DILITHIUM_POLYETA_PACKEDBYTES + \
134 LC_DILITHIUM_K * LC_DILITHIUM_POLYETA_PACKEDBYTES + \
135 LC_DILITHIUM_K * LC_DILITHIUM_POLYT0_PACKEDBYTES)
136
137#define LC_DILITHIUM_CRYPTO_BYTES \
138 (LC_DILITHIUM_CTILDE_BYTES + \
139 LC_DILITHIUM_L * LC_DILITHIUM_POLYZ_PACKEDBYTES + \
140 LC_DILITHIUM_POLYVECH_PACKEDBYTES)
142
143#ifndef __ASSEMBLER__
148 uint8_t sk[LC_DILITHIUM_SECRETKEYBYTES];
149};
150
155 uint8_t pk[LC_DILITHIUM_PUBLICKEYBYTES];
156};
157
162 uint8_t sig[LC_DILITHIUM_CRYPTO_BYTES];
163};
164
171#ifndef LC_DILITHIUM_CTX_ON_STACK
173 struct lc_hash_ctx dilithium_hash_ctx;
174 uint8_t shake_state[LC_SHA3_STATE_SIZE_ALIGN(LC_SHA3_256_CTX_SIZE)];
175};
176#endif
177
179#ifndef LC_DILITHIUM_CTX_ON_STACK
180#define LC_DILITHIUM_CTX_SIZE \
181 sizeof(struct lc_dilithium_ctx)
182#endif
184
190#ifndef LC_DILITHIUM_CTX_ON_STACK
191#define LC_DILITHIUM_CTX_ON_STACK(name) \
192 _Pragma("GCC diagnostic push") _Pragma( \
193 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
194 LC_ALIGNED_BUFFER( \
195 name##_ctx_buf, LC_DILITHIUM_ED25519_CTX_SIZE, \
196 LC_HASH_COMMON_ALIGNMENT); \
197 struct lc_dilithium_ctx *name = \
198 (struct lc_dilithium_ctx *)name##_ctx_buf; \
199 LC_SHAKE_256_CTX((&(name)->dilithium_hash_ctx)); \
200 _Pragma("GCC diagnostic pop")
201#endif
202
209static inline void lc_dilithium_44_ctx_zero(struct lc_dilithium_ctx *ctx)
210{
211 if (!ctx)
212 return;
214}
215
224
231
235LC_PURE
236static inline unsigned int lc_dilithium_44_sk_size(void)
237{
238 return lc_member_size(struct lc_dilithium_44_sk, sk);
239}
240
244LC_PURE
245static inline unsigned int lc_dilithium_44_pk_size(void)
246{
247 return lc_member_size(struct lc_dilithium_44_pk, pk);
248}
249
253LC_PURE
254static inline unsigned int lc_dilithium_44_sig_size(void)
255{
256 return lc_member_size(struct lc_dilithium_44_sig, sig);
257}
258
269 struct lc_rng_ctx *rng_ctx);
270
291 struct lc_dilithium_44_sk *sk,
292 const uint8_t *seed, size_t seedlen);
293
307int lc_dilithium_44_sign(struct lc_dilithium_44_sig *sig, const uint8_t *m,
308 size_t mlen, const struct lc_dilithium_44_sk *sk,
309 struct lc_rng_ctx *rng_ctx);
310
325 const struct lc_dilithium_44_sk *sk);
326
341int lc_dilithium_44_sign_update(struct lc_dilithium_ctx *ctx, const uint8_t *m,
342 size_t mlen);
343
359 struct lc_dilithium_ctx *ctx,
360 const struct lc_dilithium_44_sk *sk,
361 struct lc_rng_ctx *rng_ctx);
362
374int lc_dilithium_44_verify(const struct lc_dilithium_44_sig *sig, const uint8_t *m,
375 size_t mlen, const struct lc_dilithium_44_pk *pk);
376
392 const struct lc_dilithium_44_pk *pk);
393
409int lc_dilithium_44_verify_update(struct lc_dilithium_ctx *ctx, const uint8_t *m,
410 size_t mlen);
411
425 struct lc_dilithium_ctx *ctx,
426 const struct lc_dilithium_44_pk *pk);
427
428/****************************** Dilithium ED25510 *****************************/
429/* Macro set during leancrypto compile time for target platform */
430#define LC_DILITHIUM_ED25519_SIG
431#ifdef LC_DILITHIUM_ED25519_SIG
432
433#include "lc_ed25519.h"
434#include "lc_sha512.h"
435
441 struct lc_ed25519_sk sk_ed25519;
442};
443
449 struct lc_ed25519_pk pk_ed25519;
450};
451
457 struct lc_ed25519_sig sig_ed25519;
458};
459
466#ifndef LC_DILITHIUM_ED25519_CTX_ON_STACK
469 struct lc_hash_ctx ed25519_hash_ctx;
470 uint8_t sha512_state[LC_SHA512_STATE_SIZE + LC_HASH_COMMON_ALIGNMENT];
471};
472#endif
473
475#ifndef LC_DILITHIUM_ED25519_CTX_ON_STACK
476#define LC_DILITHIUM_ED25519_CTX_SIZE \
477 sizeof(struct lc_dilithium_ed25519_ctx)
478#endif
480
486#ifndef LC_DILITHIUM_ED25519_CTX_ON_STACK
487#define LC_DILITHIUM_ED25519_CTX_ON_STACK(name) \
488 _Pragma("GCC diagnostic push") _Pragma( \
489 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
490 LC_ALIGNED_BUFFER( \
491 name##_ctx_buf, LC_DILITHIUM_ED25519_CTX_SIZE, \
492 LC_HASH_COMMON_ALIGNMENT); \
493 struct lc_dilithium_ed25519_ctx *name = \
494 (struct lc_dilithium_ed25519_ctx *)name##_ctx_buf; \
495 LC_SHAKE_256_CTX((&(name)->dilithium_ctx.dilithium_hash_ctx)); \
496 LC_SHA512_CTX((&(name)->ed25519_hash_ctx)); \
497 _Pragma("GCC diagnostic pop")
498#endif
499
507 struct lc_dilithium_ed25519_ctx *ctx)
508{
509 if (!ctx)
510 return;
513}
514
523 struct lc_dilithium_ed25519_ctx **ctx);
524
531 struct lc_dilithium_ed25519_ctx *ctx);
532
544 struct lc_rng_ctx *rng_ctx);
545
560 const uint8_t *m, size_t mlen,
561 const struct lc_dilithium_44_ed25519_sk *sk,
562 struct lc_rng_ctx *rng_ctx);
563
565 struct lc_dilithium_ed25519_ctx *ctx,
566 const struct lc_dilithium_44_ed25519_sk *sk);
567
569 struct lc_dilithium_ed25519_ctx *ctx, const uint8_t *m,
570 size_t mlen);
571
573 struct lc_dilithium_44_ed25519_sig *sig,
574 struct lc_dilithium_ed25519_ctx *ctx,
575 const struct lc_dilithium_44_ed25519_sk *sk,
576 struct lc_rng_ctx *rng_ctx);
577
590 const uint8_t *m, size_t mlen,
591 const struct lc_dilithium_44_ed25519_pk *pk);
592
594 struct lc_dilithium_ed25519_ctx *ctx,
595 const struct lc_dilithium_44_ed25519_pk *pk);
597 struct lc_dilithium_ed25519_ctx *ctx, const uint8_t *m,
598 size_t mlen);
600 const struct lc_dilithium_44_ed25519_sig *sig,
601 struct lc_dilithium_ed25519_ctx *ctx,
602 const struct lc_dilithium_44_ed25519_pk *pk);
603
604#endif /* LC_DILITHIUM_ED25519_SIG */
605
606#endif /* __ASSEMBLER__ */
607
608/*
609 * To allow including the different lc_dilithium_*.h files, these macros need to
610 * be undefined. Only during compilation of leancrypto, these macros remain
611 * defined as this header file is not included multiple times.
612 */
613#ifndef LC_DILITHIUM_INTERNAL
614#undef LC_DILITHIUM_MODE
615#undef LC_DILITHIUM_SEEDBYTES
616#undef LC_DILITHIUM_CRHBYTES
617#undef LC_DILITHIUM_TRBYTES
618#undef LC_DILITHIUM_RNDBYTES
619#undef LC_DILITHIUM_N
620#undef LC_DILITHIUM_Q
621#undef LC_DILITHIUM_D
622#undef LC_DILITHIUM_ROOT_OF_UNITY
623#undef LC_DILITHIUM_LAMBDA
624#undef LC_DILITHIUM_K
625#undef LC_DILITHIUM_L
626#undef LC_DILITHIUM_ETA
627#undef LC_DILITHIUM_TAU
628#undef LC_DILITHIUM_BETA
629#undef LC_DILITHIUM_GAMMA1
630#undef LC_DILITHIUM_GAMMA2
631#undef LC_DILITHIUM_OMEGA
632#undef LC_DILITHIUM_CTILDE_BYTES
633#undef LC_DILITHIUM_POLYT1_PACKEDBYTES
634#undef LC_DILITHIUM_POLYT0_PACKEDBYTES
635#undef LC_DILITHIUM_POLYVECH_PACKEDBYTES
636#undef LC_DILITHIUM_POLYZ_PACKEDBYTES
637#undef LC_DILITHIUM_POLYW1_PACKEDBYTES
638#undef LC_DILITHIUM_POLYETA_PACKEDBYTES
639#undef LC_DILITHIUM_PUBLICKEYBYTES
640#undef LC_DILITHIUM_SECRETKEYBYTES
641#undef LC_DILITHIUM_CRYPTO_BYTES
642#endif /* LC_DILITHIUM_INTERNAL */
643
644#ifdef __cplusplus
645}
646#endif
647
648#endif /* LC_DILITHIUM_44_H */
static void lc_hash_zero(struct lc_hash_ctx *hash_ctx)
Zeroize Hash context allocated with either LC_HASH_CTX_ON_STACK or lc_hmac_alloc.
Definition lc_hash.h:286
struct lc_ed25519_pk pk_ed25519
int lc_dilithium_44_ed25519_sign_update(struct lc_dilithium_ed25519_ctx *ctx, const uint8_t *m, size_t mlen)
int lc_dilithium_44_verify(const struct lc_dilithium_44_sig *sig, const uint8_t *m, size_t mlen, const struct lc_dilithium_44_pk *pk)
Verifies signature in one shot.
struct lc_dilithium_44_sig sig
void lc_dilithium_44_ed25519_ctx_zero_free(struct lc_dilithium_ed25519_ctx *ctx)
Zeroize and free Dilithium-ED25519 stream context.
int lc_dilithium_44_keypair_from_seed(struct lc_dilithium_44_pk *pk, struct lc_dilithium_44_sk *sk, const uint8_t *seed, size_t seedlen)
Generates Dilithium public and private key from a given seed.
int lc_dilithium_44_keypair(struct lc_dilithium_44_pk *pk, struct lc_dilithium_44_sk *sk, struct lc_rng_ctx *rng_ctx)
Generates Dilithium public and private key.
int lc_dilithium_44_ed25519_sign_final(struct lc_dilithium_44_ed25519_sig *sig, struct lc_dilithium_ed25519_ctx *ctx, const struct lc_dilithium_44_ed25519_sk *sk, struct lc_rng_ctx *rng_ctx)
int lc_dilithium_44_ctx_alloc(struct lc_dilithium_ctx **ctx)
Allocate Dilithium stream context on heap.
int lc_dilithium_44_sign_final(struct lc_dilithium_44_sig *sig, struct lc_dilithium_ctx *ctx, const struct lc_dilithium_44_sk *sk, struct lc_rng_ctx *rng_ctx)
Computes signature.
int lc_dilithium_44_sign(struct lc_dilithium_44_sig *sig, const uint8_t *m, size_t mlen, const struct lc_dilithium_44_sk *sk, struct lc_rng_ctx *rng_ctx)
Computes signature in one shot.
static LC_PURE unsigned int lc_dilithium_44_sk_size(void)
Return the size of the Dilithium secret key.
struct lc_ed25519_sk sk_ed25519
int lc_dilithium_44_ed25519_verify_init(struct lc_dilithium_ed25519_ctx *ctx, const struct lc_dilithium_44_ed25519_pk *pk)
int lc_dilithium_44_ed25519_verify_update(struct lc_dilithium_ed25519_ctx *ctx, const uint8_t *m, size_t mlen)
int lc_dilithium_44_verify_update(struct lc_dilithium_ctx *ctx, const uint8_t *m, size_t mlen)
Add more data to an already initialized signature state.
static void lc_dilithium_44_ed25519_ctx_zero(struct lc_dilithium_ed25519_ctx *ctx)
Zeroize Dilithium-ED25519 context allocated with LC_DILITHIUM_ED25519_CTX_ON_STACK lc_dilithium_ed255...
int lc_dilithium_44_ed25519_sign_init(struct lc_dilithium_ed25519_ctx *ctx, const struct lc_dilithium_44_ed25519_sk *sk)
int lc_dilithium_44_sign_update(struct lc_dilithium_ctx *ctx, const uint8_t *m, size_t mlen)
Add more data to an already initialized signature state.
uint8_t sk[LC_DILITHIUM_SECRETKEYBYTES]
int lc_dilithium_44_ed25519_ctx_alloc(struct lc_dilithium_ed25519_ctx **ctx)
Allocate Dilithium-ED25519 stream context on heap.
struct lc_dilithium_44_pk pk
static LC_PURE unsigned int lc_dilithium_44_pk_size(void)
Return the size of the Dilithium public key.
uint8_t sig[LC_DILITHIUM_CRYPTO_BYTES]
int lc_dilithium_44_ed25519_verify(const struct lc_dilithium_44_ed25519_sig *sig, const uint8_t *m, size_t mlen, const struct lc_dilithium_44_ed25519_pk *pk)
Verifies signature in one shot.
struct lc_dilithium_44_sk sk
static LC_PURE unsigned int lc_dilithium_44_sig_size(void)
Return the size of the Dilithium signature.
int lc_dilithium_44_ed25519_sign(struct lc_dilithium_44_ed25519_sig *sig, const uint8_t *m, size_t mlen, const struct lc_dilithium_44_ed25519_sk *sk, struct lc_rng_ctx *rng_ctx)
Computes signature in one shot.
int lc_dilithium_44_verify_final(const struct lc_dilithium_44_sig *sig, struct lc_dilithium_ctx *ctx, const struct lc_dilithium_44_pk *pk)
Verifies signature.
int lc_dilithium_44_verify_init(struct lc_dilithium_ctx *ctx, const struct lc_dilithium_44_pk *pk)
Initializes a signature verification operation.
int lc_dilithium_44_ed25519_keypair(struct lc_dilithium_44_ed25519_pk *pk, struct lc_dilithium_44_ed25519_sk *sk, struct lc_rng_ctx *rng_ctx)
Generates Dilithium public and private key.
static void lc_dilithium_44_ctx_zero(struct lc_dilithium_ctx *ctx)
Zeroize Dilithium context allocated with LC_DILITHIUM_CTX_ON_STACK lc_dilithium_ed25519_alloc.
int lc_dilithium_44_sign_init(struct lc_dilithium_ctx *ctx, const struct lc_dilithium_44_sk *sk)
Initializes a signature operation.
void lc_dilithium_44_ctx_zero_free(struct lc_dilithium_ctx *ctx)
Zeroize and free Dilithium stream context.
struct lc_ed25519_sig sig_ed25519
int lc_dilithium_44_ed25519_verify_final(const struct lc_dilithium_44_ed25519_sig *sig, struct lc_dilithium_ed25519_ctx *ctx, const struct lc_dilithium_44_ed25519_pk *pk)
uint8_t pk[LC_DILITHIUM_PUBLICKEYBYTES]
Dilithium public key.
Dilithium secret key.
Dilithium public key.
Dilithium signature.
Dilithium secret key.
struct lc_hash_ctx ed25519_hash_ctx
uint8_t shake_state[LC_SHA3_STATE_SIZE_ALIGN(LC_SHA3_256_CTX_SIZE)]
struct lc_hash_ctx dilithium_hash_ctx
struct lc_dilithium_ctx dilithium_ctx
uint8_t sha512_state[LC_SHA512_STATE_SIZE+LC_HASH_COMMON_ALIGNMENT]
Dilithium stream context.
Dilithium stream context.