API参考
载入中...
搜索中...
未找到
ml_dsa_local.h
1/*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15#ifndef CRYPT_ML_DSA_LOCAL_H
16#define CRYPT_ML_DSA_LOCAL_H
17#include "crypt_mldsa.h"
18#include "sal_atomic.h"
19#include "crypt_local_types.h"
20
21#define MLDSA_SEED_BYTES_LEN 32
22#define MLDSA_PUBLIC_SEED_LEN 32
23#define MLDSA_PRIVATE_SEED_LEN 64
24#define MLDSA_SIGNING_SEED_LEN 32
25#define MLDSA_EXPANDED_SEED_BYTES_LEN (MLDSA_PUBLIC_SEED_LEN + MLDSA_PRIVATE_SEED_LEN + MLDSA_SIGNING_SEED_LEN)
26#define MLDSA_SEED_EXTEND_BYTES_LEN (MLDSA_SEED_BYTES_LEN + 2)
27
28#define MLDSA_K_MAX 8
29#define MLDSA_L_MAX 7
30
31#define MLDSA_TR_MSG_LEN 64
32#define MLDSA_XOF_MSG_LEN 64
33#define MLDSA_N 256
34#define MLDSA_N_HALF (MLDSA_N >> 1)
35#define MLDSA_N_BYTE 32
36
37#define GAMMA_BITS_OF_MLDSA_44 18
38#define GAMMA_BITS_OF_MLDSA_65_87 20
39#define K_VALUE_OF_MLDSA_44 4
40
41#define MLDSA_Q 8380417
42#define MLDSA_QINV 58728449 // MLDSA_Q^(-1) mod 2^32
43#define MLDSA_D 13
44#define MLDSA_PUBKEY_POLYT_PACKEDBYTES 320
45#define MLDSA_MAX_CTX_BYTES 255
46#define MLDSA_SIGN_PREFIX_BYTES 2
47
48// Reference: https://eprint.iacr.org/2022/956.pdf
49// 3.1 Improved Plantard Multiplication
50#define MLDSA_PLANTARD_L 32
51#define MLDSA_PLANTARD_ALPHA 3
52#define MLDSA_PLANTARD_INV 1732267787797143553 // inverse_mod(q, 1 << 64)
53
54// 1783^{bit_rev(1)} * 256^{-1} * (-2^{64}) mod Q, then converted to Plantard domin
55#define MLDSA_LAST_ROUND_ZETA (-8751230424634003605LL)
56
57// 8338439 = 256^{-1} * (-2^{64}) mod Q and 8338439 in Plantard domin is -92400822384635461
58// -2^{64} because the inputs have factor -2^{64} when multiplying polys using MLDSA_PlantardMulReduce
59#define MLDSA_PLANTARD_8338439 (-92400822384635461LL)
60
61// This is Barrett Modular Multiplication, mod is MLDSA_Q.
62#define MLDSA_MOD_Q(val) {int32_t m = ((val) + (1 << 22u)) >> 23u; (val) = (val) - m * MLDSA_Q;}
63
64typedef struct {
65 int32_t paramId;
66 uint8_t k;
67 uint8_t l;
68 uint8_t eta;
69 uint8_t tau;
70 uint32_t beta;
71 uint32_t gamma1;
72 uint32_t gamma2;
73 uint8_t omega;
74 uint32_t secBits;
75 uint32_t publicKeyLen;
76 uint32_t privateKeyLen;
77 uint32_t signatureLen;
79
81 const CRYPT_ML_DSA_Info *info;
82 uint8_t *pubKey;
83 uint32_t pubLen;
84 uint8_t *prvKey;
85 uint32_t prvLen;
86 uint8_t *ctxInfo;
87 uint32_t ctxLen;
88 bool isMuMsg;
89 bool needEncodeCtx;
90 bool needPreHash;
91 bool deterministicSignFlag;
92 BSL_SAL_RefCount references;
93 void *libCtx;
94 CRYPT_ALGO_MLDSA_PRIV_KEY_FORMAT_TYPE prvKeyFormat;
95 bool hasSeed;
96 uint8_t seed[MLDSA_SEED_BYTES_LEN];
97};
98
99void MLDSA_ComputesNTT(int32_t w[MLDSA_N]);
100void MLDSA_ComputesINVNTT(int32_t w[MLDSA_N]);
101
102static inline int32_t MLDSA_PlantardMulReduce(int64_t a)
103{
104 int64_t tmp = a;
105 tmp >>= MLDSA_PLANTARD_L;
106 tmp = (tmp + (1 << MLDSA_PLANTARD_ALPHA)) * MLDSA_Q;
107 tmp >>= MLDSA_PLANTARD_L;
108 return (int32_t)tmp;
109}
110
111int32_t MLDSA_KeyGenInternal(CRYPT_ML_DSA_Ctx *ctx, const uint8_t *d);
112
113int32_t MLDSA_SignInternal(const CRYPT_ML_DSA_Ctx *ctx, const CRYPT_Data *msg, uint8_t *out, uint32_t *outLen,
114 const uint8_t *rand);
115
116int32_t MLDSA_VerifyInternal(const CRYPT_ML_DSA_Ctx *ctx, const CRYPT_Data *msg, const uint8_t *sign, uint32_t signLen);
117
118// calculate public key from private key
119int32_t MLDSA_CalPub(const CRYPT_ML_DSA_Ctx *ctx, uint8_t *pub, uint32_t pubLen);
120int32_t MLDSA_KeyConsistenceCheck(CRYPT_ML_DSA_Ctx *ctx);
121
122#endif // ML_DSA_LOCAL_H
定义 sal_atomic.h:88
定义 crypt_types.h:38
定义 ml_dsa_local.h:64
定义 ml_dsa_local.h:80