API参考
载入中...
搜索中...
未找到
ml_kem_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
16#ifndef CRYPT_ML_KEM_LOCAL_H
17#define CRYPT_ML_KEM_LOCAL_H
18#include "crypt_mlkem.h"
19#include "sal_atomic.h"
20#include "crypt_local_types.h"
21
22#define MLKEM_N 256
23#define MLKEM_N_HALF 128
24#define MLKEM_CIPHER_LEN 384
25
26#define MLKEM_SEED_LEN 32
27#define MLKEM_SHARED_KEY_LEN 32
28#define MLKEM_PRF_BLOCKSIZE 64
29#define MLKEM_ENCODE_BLOCKSIZE 32
30
31#define MLKEM_Q 3329
32#define MLKEM_Q_INV_BETA (-3327) //(-MLKEM_Q) ^{-1} mod BETA, BETA = 2^{16}
33#define MLKEM_Q_HALF ((MLKEM_Q + 1) / 2)
34#define MLKEM_BITS_OF_Q 12
35#define MLKEM_INVN 3303 // MLKEM_N_HALF * MLKEM_INVN = 1 mod MLKEM_Q
36#define MLKEM_K_MAX 4
37
38// Reference: https://eprint.iacr.org/2022/956.pdf
39// Section 4.1. Efficient Plantard Arithmetic for 16-bit Modulus
40#define MLKEM_PLANTARD_L 16
41#define MLKEM_PLANTARD_ALPHA 3
42
43// 1729 * 128^{-1} mod 3329 converted to Plantard domin
44// 1729 is the last round ztea
45#define MLKEM_LAST_ROUND_ZETA 2131356556
46#define MLKEM_HALF_DEGREE_INVERSE_MOD_Q (-33544352) // 128^{-1} mod 3329 = 3303 converted to Plantard domin
47
48typedef int32_t (*MlKemHashFunc)(uint32_t id, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen);
49
50
51static inline int16_t BarrettReduction(int32_t a)
52{
53 const int32_t v = ((1 << 27) + MLKEM_Q / 2) / MLKEM_Q;
54 int32_t t = ((int64_t)v * a + (1 << 26)) >> 27;
55 t *= MLKEM_Q;
56 return (int16_t)(a - t);
57}
58
59static inline int16_t PlantardReduction(int32_t a)
60{
61 int32_t tmp = a;
62 tmp >>= MLKEM_PLANTARD_L;
63 tmp = (tmp + (1 << MLKEM_PLANTARD_ALPHA)) * MLKEM_Q;
64 tmp >>= MLKEM_PLANTARD_L;
65 return tmp;
66}
67
68typedef struct {
69 int16_t *bufAddr;
70 int16_t *matrix[MLKEM_K_MAX][MLKEM_K_MAX];
71 int16_t *vectorS[MLKEM_K_MAX];
72 int16_t *vectorE[MLKEM_K_MAX];
73 int16_t *vectorT[MLKEM_K_MAX];
75
76typedef struct {
77 int32_t paramId; // Algorithm parameter ID (CRYPT_KEM_TYPE_MLKEM_512/768/1024)
78 uint8_t k;
79 uint8_t eta1;
80 uint8_t eta2;
81 uint8_t du;
82 uint8_t dv;
83 uint32_t secBits;
84 uint32_t encapsKeyLen;
85 uint32_t decapsKeyLen;
86 uint32_t cipherLen;
87 uint32_t sharedLen;
88 uint32_t bits;
90
92 int32_t algId;
93 const CRYPT_MlKemInfo *info;
94 uint8_t *ek;
95 uint32_t ekLen;
96 uint8_t *dk;
97 uint32_t dkLen;
98 BSL_SAL_RefCount references;
99 void *libCtx;
100 MLKEM_MatrixSt keyData;
101 CRYPT_ALGO_MLKEM_DK_FORMAT_TYPE dkFormat;
102 bool hasSeed; // Flag indicating if seed is stored
103 uint8_t seed[MLKEM_SEED_LEN * 2]; // Store 64-byte seed (d || z)
104};
105int32_t MLKEM_DecodeDk(CRYPT_ML_KEM_Ctx *ctx, const uint8_t *dk, uint32_t dkLen);
106int32_t MLKEM_DecodeEk(CRYPT_ML_KEM_Ctx *ctx, const uint8_t *ek, uint32_t ekLen);
107void MLKEM_ComputNTT(int16_t *a, const int32_t *psi);
108void MLKEM_ComputINTT(int16_t *a, const int32_t *psi);
109void MLKEM_SamplePolyCBD(int16_t *polyF, uint8_t *buf, uint8_t eta);
110void MLKEM_TransposeMatrixMulAdd(uint8_t k, int16_t **matrix, int16_t **polyVec, int16_t **polyVecOut,
111 const int16_t mulCache[MLKEM_K_MAX][MLKEM_N_HALF]);
112void MLKEM_MatrixMulAdd(uint8_t k, int16_t **matrix, int16_t **polyVec, int16_t **polyVecOut,
113 const int16_t mulCache[MLKEM_K_MAX][MLKEM_N_HALF]);
114void MLKEM_VectorInnerProductAdd(uint8_t k, int16_t **polyVec1, int16_t **polyVec2, int16_t *polyOut,
115 const int32_t *factor);
116void MLKEM_VectorInnerProductAddUseCache(uint8_t k, int16_t **polyVec1, int16_t **polyVec2, int16_t *polyOut,
117 const int16_t mulCache[MLKEM_K_MAX][MLKEM_N_HALF]);
118
119void MLKEM_ComputeMulCache(uint8_t k, int16_t **input, int16_t output[MLKEM_K_MAX][MLKEM_N_HALF],
120 const int32_t *factor);
121
122int32_t MLKEM_KeyGenInternal(CRYPT_ML_KEM_Ctx *ctx, uint8_t *d, uint8_t *z);
123
124int32_t MLKEM_EncapsInternal(CRYPT_ML_KEM_Ctx *ctx, uint8_t *ct, uint32_t *ctLen, uint8_t *sk, uint32_t *skLen,
125 uint8_t *m);
126
127int32_t MLKEM_DecapsInternal(CRYPT_ML_KEM_Ctx *ctx, uint8_t *ct, uint32_t ctLen, uint8_t *sk, uint32_t *skLen);
128
129int32_t MLKEM_CreateMatrixBuf(uint8_t k, MLKEM_MatrixSt *st);
130
131#endif // ML_KEM_LOCAL_H
定义 sal_atomic.h:88
定义 ml_kem_local.h:76
定义 ml_kem_local.h:91
定义 ml_kem_local.h:68