forked from lowRISC/opentitan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurve25519.h
More file actions
389 lines (362 loc) · 11.1 KB
/
Copy pathcurve25519.h
File metadata and controls
389 lines (362 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0
#ifndef OPENTITAN_SW_DEVICE_LIB_CRYPTO_IMPL_ECC_CURVE25519_H_
#define OPENTITAN_SW_DEVICE_LIB_CRYPTO_IMPL_ECC_CURVE25519_H_
#include <stddef.h>
#include <stdint.h>
#include "sw/device/lib/base/hardened.h"
#include "sw/device/lib/crypto/drivers/otbn.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
enum {
/**
* Number of words needed to encode a mode of operation.
*/
kCurve25519ModeWords = 1,
/**
* Number of words needed to encode the verification result.
*/
kCurve25519ResultWords = 1,
/**
* Number of words needed to hold an encoded point for Curve25519.
*/
kCurve25519PointWords = 8,
/**
* Number of bytes needed to hold an encoded point for Curve25519.
*/
kCurve25519PointBytes = kCurve25519PointWords * 4,
/**
* Number of bytes needed to hold a SHA512 hash.
*/
kCurve25519HashBytes = 64,
/**
* Number of words needed to hold a SHA512 hash.
*/
kCurve25519HashWords = kCurve25519HashBytes / 4,
/**
* Number of words needed to hold half of a SHA512 hash.
*/
kCurve25519HalfHashWords = kCurve25519HashWords / 2,
/**
* Number of bytes needed to hold a encoded public or private key.
*/
kCurve25519KeyBytes = 32,
/**
* Number of bytes needed to hold a scalar.
*/
kCurve25519ScalarBytes = 32,
/**
* Number of words needed to hold a scalar.
*/
kCurve25519ScalarWords = kCurve25519ScalarBytes / 4,
/**
* Number of bytes needed to hold a masked scalar s.
*/
kCurve25519MaskedScalarSBytes = 48,
/**
* Number of words needed to hold a masked scalar s.
*/
kCurve25519MaskedScalarSWords = kCurve25519MaskedScalarSBytes / 4,
/**
* Number of bytes needed to hold a masked scalar r.
*/
kCurve25519MaskedScalarRBytes = 80,
/**
* Number of words needed to hold a masked scalar r.
*/
kCurve25519MaskedScalarRWords = kCurve25519MaskedScalarRBytes / 4,
/**
* Number of zero padding words after a masked scalar share.
*/
kCurve25519MaskedScalarPaddingWords = 4,
/**
* Length of a Curve25519 curve point coordinate in bits.
*/
kCurve25519CoordBits = 256,
/**
* Length of a Curve25519 curve point coordinate in bytes.
*/
kCurve25519CoordBytes = kCurve25519CoordBits / 8,
/**
* Length of a Curve25519 curve point coordinate in words.
*/
kCurve25519CoordWords = kCurve25519CoordBytes / sizeof(uint32_t),
/**
* Length of an element in the Curve25519 scalar field in bits.
*/
kCurve25519ScalarBits = 256,
/**
* Length of a masked secret scalar share.
*
* Ed25519 uses no extra redundant bits for the initial seed sharing.
*/
kCurve25519MaskedScalarShareBits = kCurve25519ScalarBits,
/**
* Length of a masked secret scalar share in bytes.
*/
kCurve25519MaskedScalarShareBytes = kCurve25519MaskedScalarShareBits / 8,
/**
* Length of masked secret scalar share in words.
*/
kCurve25519MaskedScalarShareWords =
kCurve25519MaskedScalarShareBytes / sizeof(uint32_t),
/**
* Number of shares for the scalar.
*/
kCurve25519MaskedScalarNumShares = 2,
/**
* Length of the full masked secret scalar share in bits.
*/
kCurve25519MaskedScalarTotalShareBits =
kCurve25519MaskedScalarNumShares * kCurve25519MaskedScalarShareBits,
/**
* Length of the full masked secret scalar share in bytes.
*/
kCurve25519MaskedScalarTotalShareBytes =
kCurve25519MaskedScalarNumShares * kCurve25519MaskedScalarShareBytes,
/**
* Length of the full masked secret scalar share in words.
*/
kCurve25519MaskedScalarTotalShareWords =
kCurve25519MaskedScalarNumShares * kCurve25519MaskedScalarShareWords,
/**
* Number of words needed to hold a masked encoded point for Curve25519.
* (2 boolean shares * 8 words)
*/
kCurve25519MaskedPointWords = kCurve25519PointWords * 2,
};
/**
* A type that holds an Ed25519 signature.
*
* The signature consists of a curve point R and a scalar S.
*/
typedef struct curve25519_signature_t {
uint32_t r[kCurve25519PointWords];
uint32_t s[kCurve25519ScalarWords];
} curve25519_signature_t;
/**
* A type that holds the arithmetically masked shares of s.
*
* s is a 256-bit value secret scalar represesented by two 384-bit arithmetic
* shares (s0, s1) such that s = s0 - s1.
*/
typedef struct curve25519_masked_scalar_s {
/**
* First share of the secret scalar.
*/
uint32_t share0[kCurve25519MaskedScalarSWords];
/**
* Second share of the secret scalar.
*/
uint32_t share1[kCurve25519MaskedScalarSWords];
} curve25519_masked_scalar_s_t;
/**
* A type that holds the arithmetically masked shares of r.
*
* s is a 512-bit value secret scalar represesented by two 640-bit arithmetic
* shares (r0, r1) such that r = r0 - r1.
*/
typedef struct curve25519_masked_scalar_r {
/**
* First share of the secret scalar.
*/
uint32_t share0[kCurve25519MaskedScalarRWords];
/**
* Second share of the secret scalar.
*/
uint32_t share1[kCurve25519MaskedScalarRWords];
} curve25519_masked_scalar_r_t;
/**
* A type that holds a masked value from the Curve25519 scalar field.
*
* This struct is used to represent secret keys or shared secrets.
* The scalar is represented in two 256-bit shares, share0 and share1.
*/
typedef struct curve25519_masked_scalar {
/**
* First share of the secret scalar.
*/
uint32_t share0[kCurve25519MaskedScalarShareWords];
/**
* Second share of the secret scalar.
*/
uint32_t share1[kCurve25519MaskedScalarShareWords];
/**
* Checksum over share0.
*/
uint32_t checksum;
} curve25519_masked_scalar_t;
/**
* Compute the checksum of a curve25519 masked scalar.
*
* Call this routine after creating or modifying the curve25519 scalar
* structure.
*
* @param key curve25519 masked scalar.
* @returns Checksum value.
*/
uint32_t curve25519_masked_scalar_checksum(
const curve25519_masked_scalar_t *scalar);
/**
* Start an async Ed25519 keygen operation on OTBN.
*
* Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
*
* @param hash_h_low 32 low bytes of the key hash.
* @return Result of the operation (OK or error).
*/
status_t curve25519_keygen_start(const curve25519_masked_scalar_s_t *s);
/**
* Finish an async Ed25519 keygen operation on OTBN.
*
* Blocks until OTBN is idle.
*
* @param public_key Pointer for public key A.
* @return Result of the operation (OK or error).
*/
status_t curve25519_keygen_finalize(uint32_t public_key[kCurve25519PointWords]);
/**
* Start stage 1 of an async Ed25519 sign operation on OTBN.
*
* Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
*
* @param r The masked scalar r.
* @param s The masked scalar s.
* @return Result of the operation (OK or error).
*/
OT_WARN_UNUSED_RESULT
status_t curve25519_sign_stage1_start(const curve25519_masked_scalar_r_t *r,
const curve25519_masked_scalar_s_t *s);
/**
* Finish stage 1 of an async Ed25519 sign operation on OTBN.
*
* Blocks until OTBN is idle.
*
* @param sig_r Pointer for signature commitment R.
* @param public_key Pointer for public key A.
* @return Result of the operation (OK or error).
*/
OT_WARN_UNUSED_RESULT
status_t curve25519_sign_stage1_finalize(
curve25519_signature_t *sig, uint32_t public_key[kCurve25519PointWords]);
/**
* Start stage 2 of an async Ed25519 sign operation on OTBN.
*
* Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
*
* @param hash_k Challenge hash k.
* @param r The masked scalar r.
* @param s The masked scalar s.
* @return Result of the operation (OK or error).
*/
OT_WARN_UNUSED_RESULT
status_t curve25519_sign_stage2_start(
const uint32_t hash_k[kCurve25519HashWords],
const curve25519_masked_scalar_r_t *r,
const curve25519_masked_scalar_s_t *s);
/**
* Finish stage 2 of an async Ed25519 sign operation on OTBN.
*
* Blocks until OTBN is idle.
*
* @param sig_s Pointer for signature response S.
* @return Result of the operation (OK or error).
*/
OT_WARN_UNUSED_RESULT
status_t curve25519_sign_stage2_finalize(curve25519_signature_t *sig);
/**
* Start an async Ed25519 verify operation on OTBN.
*
* Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
*
* @param hash_k Challenge hash k.
* @param sig_r Signature commitment R.
* @param sig_s Signature response S.
* @param public_key Public Key A.
* @return Result of the operation (OK or error).
*/
OT_WARN_UNUSED_RESULT
status_t curve25519_verify_start(
const uint32_t hash_k[kCurve25519HashWords], curve25519_signature_t *sig,
const uint32_t public_key[kCurve25519PointWords]);
/**
* Finish an async Ed25519 verify operation on OTBN.
*
* Blocks until OTBN is idle.
*
* @param result Output buffer (true if signature is valid, false otherwise)
* @return Result of the operation (OK or error).
*/
OT_WARN_UNUSED_RESULT
status_t curve25519_verify_finalize(hardened_bool_t *result);
/**
* Start an async X25519 key exchange operation on OTBN.
*
* Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
*
* @param scalar The masked private key.
* @param public_key The public key from the other party.
* @return Result of the operation (OK or error).
*/
OT_WARN_UNUSED_RESULT
status_t curve25519_x25519_start(
const curve25519_masked_scalar_t *scalar,
const uint32_t public_key[kCurve25519PointWords]);
/**
* Start the X25519 keygen operation on OTBN using a sideloaded hardware key.
*
* @return Result of the operation.
*/
status_t curve25519_x25519_keygen_sideload_start(void);
/**
* Start the X25519 operation on OTBN using a sideloaded hardware key.
*
* This routine writes the public key and mode to OTBN. It assumes the
* Key Manager has already programmed the private key into OTBN's
* sideload registers (KEY_S0_L and KEY_S1_L).
*
* @param public_key Public key u-coordinate.
* @return Result of the operation.
*/
OT_WARN_UNUSED_RESULT
status_t curve25519_x25519_sideload_start(
const uint32_t public_key[kCurve25519PointWords]);
/**
* Finish an async X25519 key exchange operation on OTBN.
*
* Blocks until OTBN is idle.
*
* @param shared_secret Output buffer for the computed shared secret.
* @return Result of the operation (OK or error).
*/
OT_WARN_UNUSED_RESULT
status_t curve25519_x25519_finalize(
uint32_t shared_secret[kCurve25519MaskedPointWords]);
/**
* Start an async X25519 keygen operation on OTBN.
*
* Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
*
* @param scalar The masked private key.
* @return Result of the operation (OK or error).
*/
OT_WARN_UNUSED_RESULT
status_t curve25519_x25519_keygen_start(
const curve25519_masked_scalar_t *scalar);
/**
* Finish an async X25519 keygen operation on OTBN.
*
* Blocks until OTBN is idle.
*
* @param public_key Output buffer for the generated public key.
* @return Result of the operation (OK or error).
*/
OT_WARN_UNUSED_RESULT
status_t curve25519_x25519_keygen_finalize(
uint32_t public_key[kCurve25519PointWords]);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
#endif // OPENTITAN_SW_DEVICE_LIB_CRYPTO_IMPL_ECC_CURVE25519_H_