@pulse-compute/crypto 1.0.0-beta.4 → 1.0.0-beta.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +89 -8
- package/as/digest-text.as.ts +34 -0
- package/as/pulse-hmac-as.ts +7 -1
- package/conformance/digest-text.json +38 -0
- package/conformance/hs256.json +85 -0
- package/dist/contracts.d.ts +34 -12
- package/dist/contracts.d.ts.map +1 -1
- package/dist/contracts.js +9 -3
- package/dist/contracts.js.map +1 -1
- package/dist/digest.d.ts +14 -0
- package/dist/digest.d.ts.map +1 -0
- package/dist/digest.js +17 -0
- package/dist/digest.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/rsa.d.ts +58 -0
- package/dist/internal/rsa.d.ts.map +1 -0
- package/dist/internal/rsa.js +185 -0
- package/dist/internal/rsa.js.map +1 -0
- package/dist/internal/verification.d.ts +3 -3
- package/dist/internal/verification.d.ts.map +1 -1
- package/dist/internal/verification.js +2 -2
- package/dist/internal/verification.js.map +1 -1
- package/guests/es256-rustcrypto/README.md +43 -0
- package/guests/es256-rustcrypto/build.cjs +297 -0
- package/guests/es256-rustcrypto/prebuilt/es256-verifier.unoptimized.wasm +0 -0
- package/guests/es256-rustcrypto/prebuilt/es256-verifier.wasm +0 -0
- package/guests/es256-rustcrypto/probe/.cargo/config.toml +18 -0
- package/guests/es256-rustcrypto/probe/Cargo.lock +238 -0
- package/guests/es256-rustcrypto/probe/Cargo.toml +26 -0
- package/guests/es256-rustcrypto/probe/README.md +35 -0
- package/guests/es256-rustcrypto/probe/rust-toolchain.toml +5 -0
- package/guests/es256-rustcrypto/probe/src/lib.rs +108 -0
- package/guests/es256-rustcrypto/pulse.guest-unit.json +119 -0
- package/guests/es256-rustcrypto/source/.cargo/config.toml +18 -0
- package/guests/es256-rustcrypto/source/Cargo.lock +265 -0
- package/guests/es256-rustcrypto/source/Cargo.toml +23 -0
- package/guests/es256-rustcrypto/source/README.md +26 -0
- package/guests/es256-rustcrypto/source/bearssl/LICENSE.txt +21 -0
- package/guests/es256-rustcrypto/source/bearssl/UPSTREAM.json +49 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl.h +170 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_aead.h +1059 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_block.h +2618 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_ec.h +883 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_hash.h +1346 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_hmac.h +241 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_kdf.h +185 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_pem.h +294 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_prf.h +150 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_rand.h +397 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_rsa.h +1366 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_ssl.h +4296 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_x509.h +1397 -0
- package/guests/es256-rustcrypto/source/bearssl/src/codec/ccopy.c +44 -0
- package/guests/es256-rustcrypto/source/bearssl/src/codec/dec32be.c +38 -0
- package/guests/es256-rustcrypto/source/bearssl/src/codec/enc32be.c +38 -0
- package/guests/es256-rustcrypto/source/bearssl/src/config.h +229 -0
- package/guests/es256-rustcrypto/source/bearssl/src/inner.h +2532 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_add.c +46 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_bitlen.c +44 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_decmod.c +124 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_decode.c +57 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_decred.c +103 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_encode.c +79 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_fmont.c +60 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_iszero.c +39 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_moddiv.c +488 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_modpow.c +65 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_modpow2.c +160 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_montmul.c +93 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_mulacc.c +61 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_muladd.c +157 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_ninv31.c +39 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_reduce.c +66 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_rshift.c +47 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_sub.c +46 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_tmont.c +36 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i32_div32.c +56 -0
- package/guests/es256-rustcrypto/source/bearssl/src/rsa/rsa_i31_priv.c +203 -0
- package/guests/es256-rustcrypto/source/bearssl/src/rsa/rsa_i31_pub.c +106 -0
- package/guests/es256-rustcrypto/source/bearssl/src/rsa/rsa_pkcs1_sig_pad.c +100 -0
- package/guests/es256-rustcrypto/source/build.rs +38 -0
- package/guests/es256-rustcrypto/source/freestanding/string.h +8 -0
- package/guests/es256-rustcrypto/source/rs256.c +103 -0
- package/guests/es256-rustcrypto/source/rust-toolchain.toml +5 -0
- package/guests/es256-rustcrypto/source/src/lib.rs +308 -0
- package/package.json +22 -4
- package/pulse.package.json +64 -0
- package/pulsewasm.compiler.cjs +89 -0
- package/pulsewasm.manifest.cjs +15 -0
- package/pulsewasm.native.cjs +79 -7
- package/src/provider.cjs +141 -2
- package/src/provider.d.cts +6 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
5
|
+
* a copy of this software and associated documentation files (the
|
|
6
|
+
* "Software"), to deal in the Software without restriction, including
|
|
7
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
|
8
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
9
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
|
10
|
+
* the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be
|
|
13
|
+
* included in all copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
19
|
+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
20
|
+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
21
|
+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
#ifndef BR_BEARSSL_HMAC_H__
|
|
26
|
+
#define BR_BEARSSL_HMAC_H__
|
|
27
|
+
|
|
28
|
+
#include <stddef.h>
|
|
29
|
+
#include <stdint.h>
|
|
30
|
+
|
|
31
|
+
#include "bearssl_hash.h"
|
|
32
|
+
|
|
33
|
+
#ifdef __cplusplus
|
|
34
|
+
extern "C" {
|
|
35
|
+
#endif
|
|
36
|
+
|
|
37
|
+
/** \file bearssl_hmac.h
|
|
38
|
+
*
|
|
39
|
+
* # HMAC
|
|
40
|
+
*
|
|
41
|
+
* HMAC is initialized with a key and an underlying hash function; it
|
|
42
|
+
* then fills a "key context". That context contains the processed
|
|
43
|
+
* key.
|
|
44
|
+
*
|
|
45
|
+
* With the key context, a HMAC context can be initialized to process
|
|
46
|
+
* the input bytes and obtain the MAC output. The key context is not
|
|
47
|
+
* modified during that process, and can be reused.
|
|
48
|
+
*
|
|
49
|
+
* IMPORTANT: HMAC shall be used only with functions that have the
|
|
50
|
+
* following properties:
|
|
51
|
+
*
|
|
52
|
+
* - hash output size does not exceed 64 bytes;
|
|
53
|
+
* - hash internal state size does not exceed 64 bytes;
|
|
54
|
+
* - internal block length is a power of 2 between 16 and 256 bytes.
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* \brief HMAC key context.
|
|
59
|
+
*
|
|
60
|
+
* The HMAC key context is initialised with a hash function implementation
|
|
61
|
+
* and a secret key. Contents are opaque (callers should not access them
|
|
62
|
+
* directly). The caller is responsible for allocating the context where
|
|
63
|
+
* appropriate. Context initialisation and usage incurs no dynamic
|
|
64
|
+
* allocation, so there is no release function.
|
|
65
|
+
*/
|
|
66
|
+
typedef struct {
|
|
67
|
+
#ifndef BR_DOXYGEN_IGNORE
|
|
68
|
+
const br_hash_class *dig_vtable;
|
|
69
|
+
unsigned char ksi[64], kso[64];
|
|
70
|
+
#endif
|
|
71
|
+
} br_hmac_key_context;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* \brief HMAC key context initialisation.
|
|
75
|
+
*
|
|
76
|
+
* Initialise the key context with the provided key, using the hash function
|
|
77
|
+
* identified by `digest_vtable`. This supports arbitrary key lengths.
|
|
78
|
+
*
|
|
79
|
+
* \param kc HMAC key context to initialise.
|
|
80
|
+
* \param digest_vtable pointer to the hash function implementation vtable.
|
|
81
|
+
* \param key pointer to the HMAC secret key.
|
|
82
|
+
* \param key_len HMAC secret key length (in bytes).
|
|
83
|
+
*/
|
|
84
|
+
void br_hmac_key_init(br_hmac_key_context *kc,
|
|
85
|
+
const br_hash_class *digest_vtable, const void *key, size_t key_len);
|
|
86
|
+
|
|
87
|
+
/*
|
|
88
|
+
* \brief Get the underlying hash function.
|
|
89
|
+
*
|
|
90
|
+
* This function returns a pointer to the implementation vtable of the
|
|
91
|
+
* hash function used for this HMAC key context.
|
|
92
|
+
*
|
|
93
|
+
* \param kc HMAC key context.
|
|
94
|
+
* \return the hash function implementation.
|
|
95
|
+
*/
|
|
96
|
+
static inline const br_hash_class *br_hmac_key_get_digest(
|
|
97
|
+
const br_hmac_key_context *kc)
|
|
98
|
+
{
|
|
99
|
+
return kc->dig_vtable;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* \brief HMAC computation context.
|
|
104
|
+
*
|
|
105
|
+
* The HMAC computation context maintains the state for a single HMAC
|
|
106
|
+
* computation. It is modified as input bytes are injected. The context
|
|
107
|
+
* is caller-allocated and has no release function since it does not
|
|
108
|
+
* dynamically allocate external resources. Its contents are opaque.
|
|
109
|
+
*/
|
|
110
|
+
typedef struct {
|
|
111
|
+
#ifndef BR_DOXYGEN_IGNORE
|
|
112
|
+
br_hash_compat_context dig;
|
|
113
|
+
unsigned char kso[64];
|
|
114
|
+
size_t out_len;
|
|
115
|
+
#endif
|
|
116
|
+
} br_hmac_context;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* \brief HMAC computation initialisation.
|
|
120
|
+
*
|
|
121
|
+
* Initialise a HMAC context with a key context. The key context is
|
|
122
|
+
* unmodified. Relevant data from the key context is immediately copied;
|
|
123
|
+
* the key context can thus be independently reused, modified or released
|
|
124
|
+
* without impacting this HMAC computation.
|
|
125
|
+
*
|
|
126
|
+
* An explicit output length can be specified; the actual output length
|
|
127
|
+
* will be the minimum of that value and the natural HMAC output length.
|
|
128
|
+
* If `out_len` is 0, then the natural HMAC output length is selected. The
|
|
129
|
+
* "natural output length" is the output length of the underlying hash
|
|
130
|
+
* function.
|
|
131
|
+
*
|
|
132
|
+
* \param ctx HMAC context to initialise.
|
|
133
|
+
* \param kc HMAC key context (already initialised with the key).
|
|
134
|
+
* \param out_len HMAC output length (0 to select "natural length").
|
|
135
|
+
*/
|
|
136
|
+
void br_hmac_init(br_hmac_context *ctx,
|
|
137
|
+
const br_hmac_key_context *kc, size_t out_len);
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* \brief Get the HMAC output size.
|
|
141
|
+
*
|
|
142
|
+
* The HMAC output size is the number of bytes that will actually be
|
|
143
|
+
* produced with `br_hmac_out()` with the provided context. This function
|
|
144
|
+
* MUST NOT be called on a non-initialised HMAC computation context.
|
|
145
|
+
* The returned value is the minimum of the HMAC natural length (output
|
|
146
|
+
* size of the underlying hash function) and the `out_len` parameter which
|
|
147
|
+
* was used with the last `br_hmac_init()` call on that context (if the
|
|
148
|
+
* initialisation `out_len` parameter was 0, then this function will
|
|
149
|
+
* return the HMAC natural length).
|
|
150
|
+
*
|
|
151
|
+
* \param ctx the (already initialised) HMAC computation context.
|
|
152
|
+
* \return the HMAC actual output size.
|
|
153
|
+
*/
|
|
154
|
+
static inline size_t
|
|
155
|
+
br_hmac_size(br_hmac_context *ctx)
|
|
156
|
+
{
|
|
157
|
+
return ctx->out_len;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/*
|
|
161
|
+
* \brief Get the underlying hash function.
|
|
162
|
+
*
|
|
163
|
+
* This function returns a pointer to the implementation vtable of the
|
|
164
|
+
* hash function used for this HMAC context.
|
|
165
|
+
*
|
|
166
|
+
* \param hc HMAC context.
|
|
167
|
+
* \return the hash function implementation.
|
|
168
|
+
*/
|
|
169
|
+
static inline const br_hash_class *br_hmac_get_digest(
|
|
170
|
+
const br_hmac_context *hc)
|
|
171
|
+
{
|
|
172
|
+
return hc->dig.vtable;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* \brief Inject some bytes in HMAC.
|
|
177
|
+
*
|
|
178
|
+
* The provided `len` bytes are injected as extra input in the HMAC
|
|
179
|
+
* computation incarnated by the `ctx` HMAC context. It is acceptable
|
|
180
|
+
* that `len` is zero, in which case `data` is ignored (and may be
|
|
181
|
+
* `NULL`) and this function does nothing.
|
|
182
|
+
*/
|
|
183
|
+
void br_hmac_update(br_hmac_context *ctx, const void *data, size_t len);
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* \brief Compute the HMAC output.
|
|
187
|
+
*
|
|
188
|
+
* The destination buffer MUST be large enough to accommodate the result;
|
|
189
|
+
* its length is at most the "natural length" of HMAC (i.e. the output
|
|
190
|
+
* length of the underlying hash function). The context is NOT modified;
|
|
191
|
+
* further bytes may be processed. Thus, "partial HMAC" values can be
|
|
192
|
+
* efficiently obtained.
|
|
193
|
+
*
|
|
194
|
+
* Returned value is the output length (in bytes).
|
|
195
|
+
*
|
|
196
|
+
* \param ctx HMAC computation context.
|
|
197
|
+
* \param out destination buffer for the HMAC output.
|
|
198
|
+
* \return the produced value length (in bytes).
|
|
199
|
+
*/
|
|
200
|
+
size_t br_hmac_out(const br_hmac_context *ctx, void *out);
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* \brief Constant-time HMAC computation.
|
|
204
|
+
*
|
|
205
|
+
* This function compute the HMAC output in constant time. Some extra
|
|
206
|
+
* input bytes are processed, then the output is computed. The extra
|
|
207
|
+
* input consists in the `len` bytes pointed to by `data`. The `len`
|
|
208
|
+
* parameter must lie between `min_len` and `max_len` (inclusive);
|
|
209
|
+
* `max_len` bytes are actually read from `data`. Computing time (and
|
|
210
|
+
* memory access pattern) will not depend upon the data byte contents or
|
|
211
|
+
* the value of `len`.
|
|
212
|
+
*
|
|
213
|
+
* The output is written in the `out` buffer, that MUST be large enough
|
|
214
|
+
* to receive it.
|
|
215
|
+
*
|
|
216
|
+
* The difference `max_len - min_len` MUST be less than 2<sup>30</sup>
|
|
217
|
+
* (i.e. about one gigabyte).
|
|
218
|
+
*
|
|
219
|
+
* This function computes the output properly only if the underlying
|
|
220
|
+
* hash function uses MD padding (i.e. MD5, SHA-1, SHA-224, SHA-256,
|
|
221
|
+
* SHA-384 or SHA-512).
|
|
222
|
+
*
|
|
223
|
+
* The provided context is NOT modified.
|
|
224
|
+
*
|
|
225
|
+
* \param ctx the (already initialised) HMAC computation context.
|
|
226
|
+
* \param data the extra input bytes.
|
|
227
|
+
* \param len the extra input length (in bytes).
|
|
228
|
+
* \param min_len minimum extra input length (in bytes).
|
|
229
|
+
* \param max_len maximum extra input length (in bytes).
|
|
230
|
+
* \param out destination buffer for the HMAC output.
|
|
231
|
+
* \return the produced value length (in bytes).
|
|
232
|
+
*/
|
|
233
|
+
size_t br_hmac_outCT(const br_hmac_context *ctx,
|
|
234
|
+
const void *data, size_t len, size_t min_len, size_t max_len,
|
|
235
|
+
void *out);
|
|
236
|
+
|
|
237
|
+
#ifdef __cplusplus
|
|
238
|
+
}
|
|
239
|
+
#endif
|
|
240
|
+
|
|
241
|
+
#endif
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
5
|
+
* a copy of this software and associated documentation files (the
|
|
6
|
+
* "Software"), to deal in the Software without restriction, including
|
|
7
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
|
8
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
9
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
|
10
|
+
* the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be
|
|
13
|
+
* included in all copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
19
|
+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
20
|
+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
21
|
+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
#ifndef BR_BEARSSL_KDF_H__
|
|
26
|
+
#define BR_BEARSSL_KDF_H__
|
|
27
|
+
|
|
28
|
+
#include <stddef.h>
|
|
29
|
+
#include <stdint.h>
|
|
30
|
+
|
|
31
|
+
#include "bearssl_hash.h"
|
|
32
|
+
#include "bearssl_hmac.h"
|
|
33
|
+
|
|
34
|
+
#ifdef __cplusplus
|
|
35
|
+
extern "C" {
|
|
36
|
+
#endif
|
|
37
|
+
|
|
38
|
+
/** \file bearssl_kdf.h
|
|
39
|
+
*
|
|
40
|
+
* # Key Derivation Functions
|
|
41
|
+
*
|
|
42
|
+
* KDF are functions that takes a variable length input, and provide a
|
|
43
|
+
* variable length output, meant to be used to derive subkeys from a
|
|
44
|
+
* master key.
|
|
45
|
+
*
|
|
46
|
+
* ## HKDF
|
|
47
|
+
*
|
|
48
|
+
* HKDF is a KDF defined by [RFC 5869](https://tools.ietf.org/html/rfc5869).
|
|
49
|
+
* It is based on HMAC, itself using an underlying hash function. Any
|
|
50
|
+
* hash function can be used, as long as it is compatible with the rules
|
|
51
|
+
* for the HMAC implementation (i.e. output size is 64 bytes or less, hash
|
|
52
|
+
* internal state size is 64 bytes or less, and the internal block length is
|
|
53
|
+
* a power of 2 between 16 and 256 bytes). HKDF has two phases:
|
|
54
|
+
*
|
|
55
|
+
* - HKDF-Extract: the input data in ingested, along with a "salt" value.
|
|
56
|
+
*
|
|
57
|
+
* - HKDF-Expand: the output is produced, from the result of processing
|
|
58
|
+
* the input and salt, and using an extra non-secret parameter called
|
|
59
|
+
* "info".
|
|
60
|
+
*
|
|
61
|
+
* The "salt" and "info" strings are non-secret and can be empty. Their role
|
|
62
|
+
* is normally to bind the input and output, respectively, to conventional
|
|
63
|
+
* identifiers that qualifu them within the used protocol or application.
|
|
64
|
+
*
|
|
65
|
+
* The implementation defined in this file uses the following functions:
|
|
66
|
+
*
|
|
67
|
+
* - `br_hkdf_init()`: initialize an HKDF context, with a hash function,
|
|
68
|
+
* and the salt. This starts the HKDF-Extract process.
|
|
69
|
+
*
|
|
70
|
+
* - `br_hkdf_inject()`: inject more input bytes. This function may be
|
|
71
|
+
* called repeatedly if the input data is provided by chunks.
|
|
72
|
+
*
|
|
73
|
+
* - `br_hkdf_flip()`: end the HKDF-Extract process, and start the
|
|
74
|
+
* HKDF-Expand process.
|
|
75
|
+
*
|
|
76
|
+
* - `br_hkdf_produce()`: get the next bytes of output. This function
|
|
77
|
+
* may be called several times to obtain the full output by chunks.
|
|
78
|
+
* For correct HKDF processing, the same "info" string must be
|
|
79
|
+
* provided for each call.
|
|
80
|
+
*
|
|
81
|
+
* Note that the HKDF total output size (the number of bytes that
|
|
82
|
+
* HKDF-Expand is willing to produce) is limited: if the hash output size
|
|
83
|
+
* is _n_ bytes, then the maximum output size is _255*n_.
|
|
84
|
+
*/
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* \brief HKDF context.
|
|
88
|
+
*
|
|
89
|
+
* The HKDF context is initialized with a hash function implementation
|
|
90
|
+
* and a salt value. Contents are opaque (callers should not access them
|
|
91
|
+
* directly). The caller is responsible for allocating the context where
|
|
92
|
+
* appropriate. Context initialisation and usage incurs no dynamic
|
|
93
|
+
* allocation, so there is no release function.
|
|
94
|
+
*/
|
|
95
|
+
typedef struct {
|
|
96
|
+
#ifndef BR_DOXYGEN_IGNORE
|
|
97
|
+
union {
|
|
98
|
+
br_hmac_context hmac_ctx;
|
|
99
|
+
br_hmac_key_context prk_ctx;
|
|
100
|
+
} u;
|
|
101
|
+
unsigned char buf[64];
|
|
102
|
+
size_t ptr;
|
|
103
|
+
size_t dig_len;
|
|
104
|
+
unsigned chunk_num;
|
|
105
|
+
#endif
|
|
106
|
+
} br_hkdf_context;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* \brief HKDF context initialization.
|
|
110
|
+
*
|
|
111
|
+
* The underlying hash function and salt value are provided. Arbitrary
|
|
112
|
+
* salt lengths can be used.
|
|
113
|
+
*
|
|
114
|
+
* HKDF makes a difference between a salt of length zero, and an
|
|
115
|
+
* absent salt (the latter being equivalent to a salt consisting of
|
|
116
|
+
* bytes of value zero, of the same length as the hash function output).
|
|
117
|
+
* If `salt_len` is zero, then this function assumes that the salt is
|
|
118
|
+
* present but of length zero. To specify an _absent_ salt, use
|
|
119
|
+
* `BR_HKDF_NO_SALT` as `salt` parameter (`salt_len` is then ignored).
|
|
120
|
+
*
|
|
121
|
+
* \param hc HKDF context to initialise.
|
|
122
|
+
* \param digest_vtable pointer to the hash function implementation vtable.
|
|
123
|
+
* \param salt HKDF-Extract salt.
|
|
124
|
+
* \param salt_len HKDF-Extract salt length (in bytes).
|
|
125
|
+
*/
|
|
126
|
+
void br_hkdf_init(br_hkdf_context *hc, const br_hash_class *digest_vtable,
|
|
127
|
+
const void *salt, size_t salt_len);
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* \brief The special "absent salt" value for HKDF.
|
|
131
|
+
*/
|
|
132
|
+
#define BR_HKDF_NO_SALT (&br_hkdf_no_salt)
|
|
133
|
+
|
|
134
|
+
#ifndef BR_DOXYGEN_IGNORE
|
|
135
|
+
extern const unsigned char br_hkdf_no_salt;
|
|
136
|
+
#endif
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* \brief HKDF input injection (HKDF-Extract).
|
|
140
|
+
*
|
|
141
|
+
* This function injects some more input bytes ("key material") into
|
|
142
|
+
* HKDF. This function may be called several times, after `br_hkdf_init()`
|
|
143
|
+
* but before `br_hkdf_flip()`.
|
|
144
|
+
*
|
|
145
|
+
* \param hc HKDF context.
|
|
146
|
+
* \param ikm extra input bytes.
|
|
147
|
+
* \param ikm_len number of extra input bytes.
|
|
148
|
+
*/
|
|
149
|
+
void br_hkdf_inject(br_hkdf_context *hc, const void *ikm, size_t ikm_len);
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* \brief HKDF switch to the HKDF-Expand phase.
|
|
153
|
+
*
|
|
154
|
+
* This call terminates the HKDF-Extract process (input injection), and
|
|
155
|
+
* starts the HKDF-Expand process (output production).
|
|
156
|
+
*
|
|
157
|
+
* \param hc HKDF context.
|
|
158
|
+
*/
|
|
159
|
+
void br_hkdf_flip(br_hkdf_context *hc);
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* \brief HKDF output production (HKDF-Expand).
|
|
163
|
+
*
|
|
164
|
+
* Produce more output bytes from the current state. This function may be
|
|
165
|
+
* called several times, but only after `br_hkdf_flip()`.
|
|
166
|
+
*
|
|
167
|
+
* Returned value is the number of actually produced bytes. The total
|
|
168
|
+
* output length is limited to 255 times the output length of the
|
|
169
|
+
* underlying hash function.
|
|
170
|
+
*
|
|
171
|
+
* \param hc HKDF context.
|
|
172
|
+
* \param info application specific information string.
|
|
173
|
+
* \param info_len application specific information string length (in bytes).
|
|
174
|
+
* \param out destination buffer for the HKDF output.
|
|
175
|
+
* \param out_len the length of the requested output (in bytes).
|
|
176
|
+
* \return the produced output length (in bytes).
|
|
177
|
+
*/
|
|
178
|
+
size_t br_hkdf_produce(br_hkdf_context *hc,
|
|
179
|
+
const void *info, size_t info_len, void *out, size_t out_len);
|
|
180
|
+
|
|
181
|
+
#ifdef __cplusplus
|
|
182
|
+
}
|
|
183
|
+
#endif
|
|
184
|
+
|
|
185
|
+
#endif
|