/src/openssl/crypto/ml_dsa/ml_dsa_local.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2024-2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #ifndef OSSL_CRYPTO_ML_DSA_LOCAL_H |
11 | | #define OSSL_CRYPTO_ML_DSA_LOCAL_H |
12 | | |
13 | | #include "crypto/ml_dsa.h" |
14 | | #include "internal/constant_time.h" |
15 | | #include "internal/packet.h" |
16 | | |
17 | | /* The following constants are shared by ML-DSA-44, ML-DSA-65 & ML-DSA-87 */ |
18 | 0 | #define ML_DSA_Q 8380417 /* The modulus is 23 bits (2^23 - 2^13 + 1) */ |
19 | 0 | #define ML_DSA_Q_MINUS1_DIV2 ((ML_DSA_Q - 1) / 2) |
20 | | |
21 | | #define ML_DSA_Q_BITS 23 |
22 | | #define ML_DSA_Q_INV 58728449 /* q^-1 satisfies: q^-1 * q = 1 mod 2^32 */ |
23 | 0 | #define ML_DSA_Q_NEG_INV 4236238847 /* Inverse of -q modulo 2^32 */ |
24 | | #define ML_DSA_DEGREE_INV_MONTGOMERY 41978 /* Inverse of 256 mod q, in Montgomery form. */ |
25 | | |
26 | 0 | #define ML_DSA_D_BITS 13 /* The number of bits dropped from the public vector t */ |
27 | | #define ML_DSA_NUM_POLY_COEFFICIENTS 256 /* The number of coefficients in the polynomials */ |
28 | 0 | #define ML_DSA_RHO_BYTES 32 /* p = Public Random Seed */ |
29 | 0 | #define ML_DSA_PRIV_SEED_BYTES 64 /* p' = Private random seed */ |
30 | | #define ML_DSA_K_BYTES 32 /* K = Private random seed for signing */ |
31 | | #define ML_DSA_TR_BYTES 64 /* Size of the Hash of the public key used for signing */ |
32 | 0 | #define ML_DSA_RHO_PRIME_BYTES 64 /* private random seed size */ |
33 | | |
34 | | /* |
35 | | * There is special case code related to encoding/decoding that tests the |
36 | | * for the following values. |
37 | | */ |
38 | | /* |
39 | | * The possible value for eta - If a new value is added, then all code |
40 | | * that accesses ML_DSA_ETA_4 would need to be modified. |
41 | | */ |
42 | 0 | #define ML_DSA_ETA_4 4 |
43 | | #define ML_DSA_ETA_2 2 |
44 | | /* |
45 | | * The possible values of gamma1 - If a new value is added, then all code |
46 | | * that accesses ML_DSA_GAMMA1_TWO_POWER_19 would need to be modified. |
47 | | */ |
48 | 0 | #define ML_DSA_GAMMA1_TWO_POWER_19 (1 << 19) |
49 | | #define ML_DSA_GAMMA1_TWO_POWER_17 (1 << 17) |
50 | | /* |
51 | | * The possible values for gamma2 - If a new value is added, then all code |
52 | | * that accesses ML_DSA_GAMMA2_Q_MINUS1_DIV32 would need to be modified. |
53 | | */ |
54 | 0 | #define ML_DSA_GAMMA2_Q_MINUS1_DIV32 ((ML_DSA_Q - 1) / 32) |
55 | 0 | #define ML_DSA_GAMMA2_Q_MINUS1_DIV88 ((ML_DSA_Q - 1) / 88) |
56 | | |
57 | | typedef struct poly_st POLY; |
58 | | typedef struct vector_st VECTOR; |
59 | | typedef struct matrix_st MATRIX; |
60 | | typedef struct ml_dsa_sig_st ML_DSA_SIG; |
61 | | |
62 | | typedef int(ML_DSA_MATRIX_EXPAND_A_FN)(EVP_MD_CTX *g_ctx, const EVP_MD *md, |
63 | | const uint8_t *rho, MATRIX *out); |
64 | | typedef int(ML_DSA_VECTOR_EXPAND_S_FN)(EVP_MD_CTX *h_ctx, const EVP_MD *md, |
65 | | int eta, const uint8_t *seed, VECTOR *s1, VECTOR *s2); |
66 | | typedef void(ML_DSA_VECTOR_EXPAND_MASK_FN)(VECTOR *out, |
67 | | const uint8_t rho_prime[ML_DSA_RHO_PRIME_BYTES], uint32_t kappa, uint32_t gamma1, |
68 | | EVP_MD_CTX *h_ctx, const EVP_MD *md); |
69 | | |
70 | | typedef struct ossl_ml_dsa_sample_ops_st { |
71 | | ML_DSA_MATRIX_EXPAND_A_FN *matrix_expand_A; |
72 | | ML_DSA_VECTOR_EXPAND_S_FN *vector_expand_S; |
73 | | ML_DSA_VECTOR_EXPAND_MASK_FN *vector_expand_mask; |
74 | | } OSSL_ML_DSA_SAMPLE_OPS; |
75 | | |
76 | | const OSSL_ML_DSA_SAMPLE_OPS *ossl_ml_dsa_sample_ops(void); |
77 | | void ossl_ml_dsa_matrix_mult_vector(const MATRIX *matrix_kl, const VECTOR *vl, |
78 | | VECTOR *vk); |
79 | | int ossl_ml_dsa_poly_expand_mask(POLY *out, const uint8_t *seed, size_t seed_len, |
80 | | uint32_t gamma1, |
81 | | EVP_MD_CTX *h_ctx, const EVP_MD *md); |
82 | | int ossl_ml_dsa_poly_sample_in_ball(POLY *out_c, const uint8_t *seed, int seed_len, |
83 | | EVP_MD_CTX *h_ctx, const EVP_MD *md, |
84 | | uint32_t tau); |
85 | | |
86 | | void ossl_ml_dsa_poly_ntt(POLY *s); |
87 | | void ossl_ml_dsa_poly_ntt_inverse(POLY *s); |
88 | | void ossl_ml_dsa_poly_ntt_mult(const POLY *lhs, const POLY *rhs, POLY *out); |
89 | | |
90 | | /* Optimization for s390x */ |
91 | | /* |
92 | | * The forward declarations below must be visible in every TU that includes |
93 | | * this header while compiling for s390x with the VX object enabled — |
94 | | * specifically in ml_dsa_ntt.c (the dispatcher) and in ml_dsa_ntt_vec128.c |
95 | | * (the implementation). OPENSSL_ML_DSA_S390X is injected by the build |
96 | | * system for all asm-enabled s390x targets; it is sufficient on its own — |
97 | | * no additional __s390x__ predefined-macro check is needed because the |
98 | | * define is never emitted for non-s390x targets. |
99 | | */ |
100 | | #if defined(OPENSSL_ML_DSA_S390X) |
101 | | #include "arch/s390x_arch.h" |
102 | | void ossl_ml_dsa_poly_ntt_scalar(POLY *p); |
103 | | void ossl_ml_dsa_poly_ntt_vec128(POLY *p); |
104 | | void ossl_ml_dsa_poly_ntt_inverse_vec128(POLY *p); |
105 | | void ossl_poly_ntt_mult_scalar_vec128(const POLY *lhs, const POLY *rhs, POLY *out); |
106 | | #endif |
107 | | |
108 | | void ossl_ml_dsa_key_compress_power2_round(uint32_t r, uint32_t *r1, uint32_t *r0); |
109 | | uint32_t ossl_ml_dsa_key_compress_high_bits(uint32_t r, uint32_t gamma2); |
110 | | void ossl_ml_dsa_key_compress_decompose(uint32_t r, uint32_t gamma2, |
111 | | uint32_t *r1, int32_t *r0); |
112 | | void ossl_ml_dsa_key_compress_decompose(uint32_t r, uint32_t gamma2, |
113 | | uint32_t *r1, int32_t *r0); |
114 | | int32_t ossl_ml_dsa_key_compress_low_bits(uint32_t r, uint32_t gamma2); |
115 | | int32_t ossl_ml_dsa_key_compress_make_hint(uint32_t ct0, uint32_t cs2, |
116 | | uint32_t gamma2, uint32_t w); |
117 | | uint32_t ossl_ml_dsa_key_compress_use_hint(uint32_t hint, uint32_t r, |
118 | | uint32_t gamma2); |
119 | | |
120 | | int ossl_ml_dsa_pk_encode(ML_DSA_KEY *key); |
121 | | int ossl_ml_dsa_sk_encode(ML_DSA_KEY *key); |
122 | | |
123 | | int ossl_ml_dsa_sig_encode(const ML_DSA_SIG *sig, const ML_DSA_PARAMS *params, |
124 | | uint8_t *out); |
125 | | int ossl_ml_dsa_sig_decode(ML_DSA_SIG *sig, const uint8_t *in, size_t in_len, |
126 | | const ML_DSA_PARAMS *params); |
127 | | int ossl_ml_dsa_w1_encode(const VECTOR *w1, uint32_t gamma2, |
128 | | uint8_t *out, size_t out_len); |
129 | | int ossl_ml_dsa_poly_decode_expand_mask(POLY *out, |
130 | | const uint8_t *in, size_t in_len, |
131 | | uint32_t gamma1); |
132 | | |
133 | | /*- |
134 | | * @brief Reduces 0 <= x < 2*q, mod q. |
135 | | * i.e. return x < q ? x : x - q; |
136 | | * |
137 | | * Subtract |q| if the input is larger, without exposing a side-channel, |
138 | | * avoiding the "clangover" attack. See |constish_time_true| for a discussion |
139 | | * on why the value barrier is by default omitted. |
140 | | * |
141 | | * @returns the difference in the range 0..q-1 |
142 | | */ |
143 | | static ossl_inline ossl_unused __owur uint32_t reduce_once(uint32_t x) |
144 | 0 | { |
145 | 0 | const uint32_t subtracted = x - ML_DSA_Q; |
146 | 0 | uint32_t mask = constish_time_true(subtracted >> 31); |
147 | |
|
148 | 0 | return (mask & x) | (~mask & subtracted); |
149 | 0 | } Unexecuted instantiation: ml_dsa_encoders.c:reduce_once Unexecuted instantiation: ml_dsa_key.c:reduce_once Unexecuted instantiation: ml_dsa_key_compress.c:reduce_once Unexecuted instantiation: ml_dsa_matrix.c:reduce_once Unexecuted instantiation: ml_dsa_ntt.c:reduce_once Unexecuted instantiation: ml_dsa_params.c:reduce_once Unexecuted instantiation: ml_dsa_sample.c:reduce_once Unexecuted instantiation: ml_dsa_sign.c:reduce_once |
150 | | |
151 | | /* |
152 | | * @brief Calculates the positive value of (a-b) mod q in constant time. |
153 | | * |
154 | | * a - b mod q gives a value in the range -(q-1)..(q-1) |
155 | | * By adding q we get a range of 1..(2q-1). |
156 | | * Reducing this once then gives the range 0..q-1 |
157 | | * |
158 | | * @param a The minuend assumed to be in the range 0..q-1 |
159 | | * @param b The subtracthend assumed to be in the range 0..q-1. |
160 | | * @returns The value (q + a - b) mod q |
161 | | */ |
162 | | static ossl_inline ossl_unused uint32_t mod_sub(uint32_t a, uint32_t b) |
163 | 0 | { |
164 | 0 | return reduce_once(ML_DSA_Q + a - b); |
165 | 0 | } Unexecuted instantiation: ml_dsa_encoders.c:mod_sub Unexecuted instantiation: ml_dsa_key.c:mod_sub Unexecuted instantiation: ml_dsa_key_compress.c:mod_sub Unexecuted instantiation: ml_dsa_matrix.c:mod_sub Unexecuted instantiation: ml_dsa_ntt.c:mod_sub Unexecuted instantiation: ml_dsa_params.c:mod_sub Unexecuted instantiation: ml_dsa_sample.c:mod_sub Unexecuted instantiation: ml_dsa_sign.c:mod_sub |
166 | | |
167 | | /* |
168 | | * @brief Returns the absolute value in constant time. |
169 | | * i.e. return is_negative(x) ? -x : x; |
170 | | */ |
171 | | static ossl_inline ossl_unused uint32_t abs_signed(uint32_t x) |
172 | 0 | { |
173 | 0 | uint32_t mask = 0u - (x >> 31); |
174 | |
|
175 | 0 | return constant_time_select_32(mask, 0u - x, x); |
176 | 0 | } Unexecuted instantiation: ml_dsa_encoders.c:abs_signed Unexecuted instantiation: ml_dsa_key.c:abs_signed Unexecuted instantiation: ml_dsa_key_compress.c:abs_signed Unexecuted instantiation: ml_dsa_matrix.c:abs_signed Unexecuted instantiation: ml_dsa_ntt.c:abs_signed Unexecuted instantiation: ml_dsa_params.c:abs_signed Unexecuted instantiation: ml_dsa_sample.c:abs_signed Unexecuted instantiation: ml_dsa_sign.c:abs_signed |
177 | | |
178 | | /* |
179 | | * @brief Returns the absolute value modulo q in constant time |
180 | | * i.e return x <= (q-1)/2 ? x : q - x; |
181 | | */ |
182 | | static ossl_inline ossl_unused uint32_t abs_mod_prime(uint32_t x) |
183 | 0 | { |
184 | 0 | uint32_t mask = x - ML_DSA_Q_MINUS1_DIV2; |
185 | |
|
186 | 0 | mask = 0u - (mask >> 31); |
187 | 0 | return constant_time_select_32(mask, x, ML_DSA_Q - x); |
188 | 0 | } Unexecuted instantiation: ml_dsa_encoders.c:abs_mod_prime Unexecuted instantiation: ml_dsa_key.c:abs_mod_prime Unexecuted instantiation: ml_dsa_key_compress.c:abs_mod_prime Unexecuted instantiation: ml_dsa_matrix.c:abs_mod_prime Unexecuted instantiation: ml_dsa_ntt.c:abs_mod_prime Unexecuted instantiation: ml_dsa_params.c:abs_mod_prime Unexecuted instantiation: ml_dsa_sample.c:abs_mod_prime Unexecuted instantiation: ml_dsa_sign.c:abs_mod_prime |
189 | | |
190 | | /* |
191 | | * @brief Returns the maximum of two values in constant time. |
192 | | * i.e return x < y ? y : x; |
193 | | */ |
194 | | static ossl_inline ossl_unused uint32_t maximum(uint32_t x, uint32_t y) |
195 | 0 | { |
196 | 0 | uint32_t mask = x - y; |
197 | 0 | mask = 0u - (mask >> 31); |
198 | 0 | return constant_time_select_int(mask, y, x); |
199 | 0 | } Unexecuted instantiation: ml_dsa_encoders.c:maximum Unexecuted instantiation: ml_dsa_key.c:maximum Unexecuted instantiation: ml_dsa_key_compress.c:maximum Unexecuted instantiation: ml_dsa_matrix.c:maximum Unexecuted instantiation: ml_dsa_ntt.c:maximum Unexecuted instantiation: ml_dsa_params.c:maximum Unexecuted instantiation: ml_dsa_sample.c:maximum Unexecuted instantiation: ml_dsa_sign.c:maximum |
200 | | |
201 | | #endif /* OSSL_CRYPTO_ML_DSA_LOCAL_H */ |