API参考
载入中...
搜索中...
未找到
mceliece_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 MCELIECE_LOCAL_H
17#define MCELIECE_LOCAL_H
18
19#include <stdint.h>
20#include <stddef.h>
21#include <stdlib.h>
22#include <string.h>
23#include "securec.h"
24
25#include "crypt_errno.h"
26#include "crypt_algid.h"
27#include "crypt_drbg.h"
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33#define MCELIECE_GF_POLY 0x201B
34#define MCELIECE_SEED_BYTES 48
35
36#define MCELIECE_L 256
37#define MCELIECE_SIGMA1 16
38#define MCELIECE_SIGMA2 32
39#define MCELIECE_MU 32
40#define MCELIECE_NU 64
41
42#define MCELIECE_Q 8192 // Q = 2^m
43#define MCELIECE_Q_1 8191 // Q-1
44
45#define MCELIECE_PARA_6688_N 6688
46#define MCELIECE_PARA_6960_N 6960
47#define MCELIECE_PARA_8192_N 8192
48
49#define MCELIECE_L_BYTES ((MCELIECE_L) / (8))
50
51#define SAME_MASK(k, val) ((uint64_t)(-(int64_t)(((((uint32_t)((k) ^ (val)))) - (1U)) >> (31))))
52
53typedef uint16_t GFElement;
54typedef struct {
55 int32_t rows;
56 int32_t cols;
57 GFElement *data;
59
60typedef struct {
61 GFElement *coeffs;
62 int32_t degree;
63 int32_t maxDegree;
65
66typedef struct {
67 uint8_t *data;
68 int32_t rows;
69 int32_t cols;
70 int32_t colsBytes;
71} GFMatrix;
72
73typedef struct {
74 uint8_t delta[MCELIECE_L_BYTES];
75 uint64_t c;
77 GFElement *alpha;
78 uint8_t *s;
79 uint8_t *controlbits;
80 size_t controlbitsLen;
82
83typedef struct {
84 GFMatrix matT;
86
87typedef struct McelieceParams {
88 int32_t algId;
89
90 int32_t m;
91 int32_t n;
92 int32_t t;
93
94 int32_t mt;
95 int32_t k;
96 int32_t q;
97 int32_t q1;
98
99 int32_t nBytes;
100 int32_t mtBytes;
101 int32_t kBytes;
102
103 int32_t privateKeyBytes;
104 int32_t publicKeyBytes;
105 int32_t sharedKeyBytes;
106 int32_t cipherBytes;
107
108 uint8_t semi;
109 uint8_t pc;
110
112
113typedef struct Mceliece_Ctx {
114 McelieceParams *para;
115 CMPublicKey *publicKey;
116 CMPrivateKey *privateKey;
117 void *libCtx;
118} CRYPT_MCELIECE_Ctx;
119
120static inline uint64_t CMMakeMask(uint64_t x)
121{
122 int64_t sx = (int64_t)x;
123 uint64_t nz = (uint64_t)((sx >> 63) | ((-sx) >> 63));
124 return ~nz;
125}
126
127static inline uint64_t CMLoad8(const uint8_t *x)
128{
129 uint64_t r = 0;
130 memcpy_s(&r, 8, x, 8);
131 return r;
132}
133
134static inline void CMStore8(uint8_t *x, uint64_t v)
135{
136 memcpy_s(x, 8, &v, 8);
137}
138
139// trailing zero count
140static inline int32_t CMCtz64(uint64_t x)
141{
142 int32_t c = 0;
143 while ((x & 1) == 0) {
144 c++;
145 x >>= 1;
146 }
147 return c;
148}
149
150// =================================================================================
151// Control Bits and Support Functions
152// =================================================================================
153/* Compute control bits for a Benes network from a permutation pi of size n=2^w.
154 * out must point to ((2*w-1)*n/16) bytes, zeroed by the caller or by the impl. */
155int32_t CbitsFromPermNs(uint8_t *out, const int16_t *pi, const int64_t w, const int64_t n);
156
157// Derive support L[0..N-1] from control bits
158int32_t SupportFromCbits(GFElement *L, const uint8_t *cbits, const int64_t w, const int32_t lenN);
159
160// =================================================================================
161// Goppa Encode and Decode Functions
162// =================================================================================
163// Goppa code decoding - recovers error vector from syndrome
164int32_t DecodeGoppa(const uint8_t *received, const GFPolynomial *g, const GFElement *alpha, uint8_t *errorVector,
165 int32_t errorVecLen, int32_t *decodeSuccess, const McelieceParams *params);
166
167// Generate a random vector with fixed Hamming weight t
168// Used in the encapsulation phase to generate the error vector e
169int32_t FixedWeightVector(CRYPT_MCELIECE_Ctx *ctx, uint8_t *output);
170
171// Encode an error vector using the public key matrix T
172// Computes C = H * e where H = [I_mt | T]
173int32_t EncodeVector(const uint8_t *errorVector, const GFMatrix *matT, uint8_t *ciphertext,
174 const McelieceParams *params);
175
176// =================================================================================
177// Poly Functions
178// =================================================================================
179// Compute the minimal/connection polynomial g(x) of f over GF(2^m)
180// out[0..t-1] are coefficients g_0..g_{t-1} with monic leading coeff implied
181// f[0..t-1] are coefficients of f(x) in GF(2^m)
182// Returns 0 on success, -1 on failure (singular system)
183int32_t GenPolyOverGF(GFElement *out, const GFElement *f, const int32_t t, const int32_t m);
184// GF(2^13) add(/xor)
185GFElement GFAddtion(GFElement a, GFElement b);
186// GF(2^13) mul
187GFElement GFMultiplication(GFElement a, GFElement b);
188// GF(2^13) inverse
189GFElement GFInverse(GFElement a);
190// GF(2^13) division
191GFElement GFDivision(GFElement a, GFElement b);
192// GF(2^13) power
193GFElement GFPower(GFElement base, int32_t exp);
194
195// Polynomial creation and destruction
196GFPolynomial *PolynomialCreate(const int32_t maxDegree);
197void PolynomialFree(GFPolynomial *poly);
198
199void PolynomialRoots(GFElement *out, const GFElement *f, const GFElement *L, const int32_t n, const int32_t t);
200
201GFElement PolynomialEval(const GFPolynomial *poly, GFElement x);
202
203int32_t PolynomialSetCoeff(GFPolynomial *poly, const int32_t degree, const GFElement coeff);
204
205int32_t PolynomialCopy(GFPolynomial *dst, const GFPolynomial *src);
206
207// =================================================================================
208// Kem Functions
209// =================================================================================
210int32_t SeededKeyGenInternal(const uint8_t *delta, CMPublicKey *pk, CMPrivateKey *sk, const McelieceParams *params,
211 bool isSemi);
212int32_t McElieceEncapsInternal(CRYPT_MCELIECE_Ctx *ctx, uint8_t *ciphertext, uint8_t *sessionKey, bool isPc);
213int32_t McElieceDecapsInternal(const uint8_t *ciphertext, const CMPrivateKey *sk, uint8_t *sessionKey,
214 const McelieceParams *params, bool isPc);
215McelieceParams *McelieceGetParamsById(int32_t algID);
216
217// Matrix creation and destruction
218GFMatrix *MatrixCreate(const int32_t rows, const int32_t cols);
219void MatrixFree(GFMatrix *mat);
220
221// Matrix element access (bit-level operations)
222void MatrixSetBit(GFMatrix *mat, const int32_t row, const int32_t col, const int32_t value);
223int32_t MatrixGetBit(const GFMatrix *mat, const int32_t row, const int32_t col);
224
225// Reference-style matrix operations (matching NIST implementation)
226int32_t BuildParityCheckMatrixReferenceStyle(GFMatrix *matH, const GFPolynomial *g, const GFElement *support,
227 const McelieceParams *params);
228int32_t ReduceToSystematicFormReferenceStyle(GFMatrix *matH);
229
230int32_t ColsRermutation(uint8_t *mat, const int32_t colsBytes, int16_t *pi, uint64_t *pivots, const int32_t mt);
231int32_t GaussPartialSemiSystematic(uint8_t *mat, const int32_t colsBytes, int16_t *pi, uint64_t *pivots,
232 const int32_t mt, const int32_t paramN);
233
234// High-level SHAKE256 function
235int32_t McElieceShake256(uint8_t *output, const size_t outlen, const uint8_t *input, size_t inlen);
236
237// Bit manipulation functions for binary vectors
238void VectorSetBit(uint8_t *vec, const uint32_t bitIdx, const uint32_t value);
239uint32_t VectorGetBit(const uint8_t *vec, const uint32_t bitIdx);
240
241// Vector utility functions
242int32_t VectorWeight(const uint8_t *vec, const int32_t lenBytes); // Calculate Hamming weight
243
244#ifdef __cplusplus
245}
246#endif
247
248#endif // MCELIECE_LOCAL_H
定义 mceliece_local.h:73
定义 mceliece_local.h:83
定义 mceliece_local.h:54
定义 mceliece_local.h:66
定义 mceliece_local.h:60
定义 mceliece_local.h:87
定义 mceliece_local.h:113