API参考
载入中...
搜索中...
未找到
slh_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
16#ifndef SLH_DSA_LOCAL_H
17#define SLH_DSA_LOCAL_H
18
19#include "hitls_build.h"
20#ifdef HITLS_CRYPTO_SLH_DSA
21
22#include <stdint.h>
23#include "bsl_params.h"
24#include "crypt_algid.h"
25#include "crypt_types.h"
26#include "crypt_utils.h"
27#include "xmss_common.h"
28#include "xmss_tree.h"
29
30#define SLH_DSA_ADRS_LEN 32
31#define SLH_DSA_ADRS_COMPRESSED_LEN 22
32#define SLH_DSA_MAX_N 32 // Security parameter (hash output length)
33#define SLH_DSA_MAX_M 49
34#define SLH_DSA_LGW 4
35#define SLH_DSA_W 16 // 2^SLH_DSA_LGW
36
37#define SLH_DSA_PRVKEY 0x1
38#define SLH_DSA_PUBKEY 0x10
39
40typedef union Adrs SlhDsaAdrs;
41typedef struct SlhDsaCtx CryptSlhDsaCtx;
42
43typedef enum {
44 WOTS_HASH,
45 WOTS_PK,
46 TREE,
47 FORS_TREE,
48 FORS_ROOTS,
49 WOTS_PRF,
50 FORS_PRF,
51} AdrsType;
52
71union Adrs {
72 struct {
73 uint8_t layerAddr[4];
74 uint8_t treeAddr[12];
75 uint8_t type[4];
76 uint8_t padding[12];
77 } uc;
78 struct {
79 uint8_t layerAddr;
80 uint8_t treeAddr[8];
81 uint8_t type;
82 uint8_t padding[12];
83 } c;
84 uint8_t bytes[SLH_DSA_ADRS_LEN];
85};
86
87// b can be 4, 6, 8, 9, 12, 14
88// so use uint32_t to receive the BaseB value
89void BaseB(const uint8_t *x, uint32_t xLen, uint32_t b, uint32_t *out, uint32_t outLen);
90
91typedef struct {
92 int32_t algId; // CRYPT_PKEY_ParaId (SLH_DSA_AlgId or XMSS_AlgId)
93 bool isCompressed;
94 uint32_t n;
95 uint32_t h;
96 uint32_t d;
97 uint32_t hp;
98 uint32_t a;
99 uint32_t k;
100 uint32_t m;
101 uint32_t secCategory;
102 uint32_t pkBytes;
103 uint32_t sigBytes;
104} SlhDsaPara;
105
106typedef struct {
107 uint8_t seed[MAX_MDSIZE]; // pubkey seed for generating keys
108 uint8_t root[MAX_MDSIZE]; // pubkey root for generating keys
109} SlhDsaPubKey;
113typedef struct {
114 uint8_t seed[MAX_MDSIZE]; // prvkey seed for generating keys
115 uint8_t prf[MAX_MDSIZE]; // prvkey prf for generating keys
116 uint64_t index; // the next unused WOTS+ key index, for XMSS only
117 SlhDsaPubKey pub;
118} SlhDsaPrvKey;
119
120struct SlhDsaCtx {
121 SlhDsaPara para;
122 uint8_t *context; // user specific context
123 uint32_t contextLen; // length of the user specific context
124 bool isDeterministic;
125 uint8_t *addrand; // optional random bytes, can be set through CTRL interface, or comes from RNG
126 uint32_t addrandLen; // length of the optional random bytes
127 bool isPrehash;
128 SlhDsaPrvKey prvKey;
129 const CryptHashFuncs *hashFuncs; // Generic hash function table pointer
130 CryptAdrsOps adrsOps; // Generic address operation function pointers
131 uint8_t keyType; /* specify the key type */
132 void *sha256MdCtx;
133 void *sha512MdCtx;
134 void *libCtx;
135};
136
137void InitTreeCtxFromSlhDsaCtx(TreeCtx* treeCtx, const CryptSlhDsaCtx *ctx);
138#endif // HITLS_CRYPTO_SLH_DSA
139#endif // SLH_DSA_LOCAL_H