Skip to content

Commit 8b81336

Browse files
committed
[crypto] Rename otcrypto_ed25519_keygen function
As this function creates a public key from a given private one, reflect this in the function comment header as well as the name. Signed-off-by: Pascal Nasahl <nasahlpa@lowrisc.org>
1 parent 8163f8d commit 8b81336

6 files changed

Lines changed: 47 additions & 37 deletions

File tree

doc/security/cryptolib/cryptolib_api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ Each party should generate a key pair, exchange public keys, and then generate t
469469

470470
For Ed25519 (a curve-specialized version of EdDSA, the Edwards curve digital signature algorithm), the cryptography library supports keypair generation, signature generation, and signature verification.
471471

472-
{{#header-snippet sw/device/lib/crypto/include/ecc_curve25519.h otcrypto_ed25519_keygen }}
472+
{{#header-snippet sw/device/lib/crypto/include/ecc_curve25519.h otcrypto_ed25519_public_key_from_private }}
473473
{{#header-snippet sw/device/lib/crypto/include/ecc_curve25519.h otcrypto_ed25519_sign }}
474474
{{#header-snippet sw/device/lib/crypto/include/ecc_curve25519.h otcrypto_ed25519_verify }}
475475
{{#header-snippet sw/device/lib/crypto/include/ecc_curve25519.h otcrypto_ed25519_sign_verify }}
@@ -520,8 +520,8 @@ Each party should generate a key pair, exchange public keys, and then generate t
520520

521521
#### Ed25519
522522

523-
{{#header-snippet sw/device/lib/crypto/include/ecc_curve25519.h otcrypto_ed25519_keygen_async_start }}
524-
{{#header-snippet sw/device/lib/crypto/include/ecc_curve25519.h otcrypto_ed25519_keygen_async_finalize }}
523+
{{#header-snippet sw/device/lib/crypto/include/ecc_curve25519.h otcrypto_ed25519_public_key_from_private_async_start }}
524+
{{#header-snippet sw/device/lib/crypto/include/ecc_curve25519.h otcrypto_ed25519_public_key_from_private_async_finalize }}
525525

526526
{{#header-snippet sw/device/lib/crypto/include/ecc_curve25519.h otcrypto_ed25519_sign_part1_async_start }}
527527
{{#header-snippet sw/device/lib/crypto/include/ecc_curve25519.h otcrypto_ed25519_sign_part2_async_start }}

sw/device/lib/crypto/impl/ecc_curve25519.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,17 @@ static status_t ed25519_mask_scalar(uint32_t *scalar, size_t scalar_len,
231231
return OTCRYPTO_OK;
232232
}
233233

234-
otcrypto_status_t otcrypto_ed25519_keygen(
234+
otcrypto_status_t otcrypto_ed25519_public_key_from_private(
235235
const otcrypto_unblinded_key_t *private_key,
236236
otcrypto_unblinded_key_t *public_key) {
237237
if (public_key == NULL || public_key->key == NULL) {
238238
return OTCRYPTO_BAD_ARGS;
239239
}
240240
// Start the execution of the key generation.
241-
HARDENED_TRY(otcrypto_ed25519_keygen_async_start(private_key));
241+
HARDENED_TRY(
242+
otcrypto_ed25519_public_key_from_private_async_start(private_key));
242243
// Finish the keygen operation and get the public key.
243-
return otcrypto_ed25519_keygen_async_finalize(public_key);
244+
return otcrypto_ed25519_public_key_from_private_async_finalize(public_key);
244245
}
245246

246247
otcrypto_status_t otcrypto_ed25519_sign(
@@ -334,7 +335,7 @@ otcrypto_status_t otcrypto_ed25519_sign_verify(
334335
return OTCRYPTO_OK;
335336
}
336337

337-
otcrypto_status_t otcrypto_ed25519_keygen_async_start(
338+
otcrypto_status_t otcrypto_ed25519_public_key_from_private_async_start(
338339
const otcrypto_unblinded_key_t *private_key) {
339340
// Check the private key.
340341
HARDENED_TRY(ed25519_key_check(private_key));
@@ -367,7 +368,7 @@ otcrypto_status_t otcrypto_ed25519_keygen_async_start(
367368
return otcrypto_eval_exit(OTCRYPTO_OK);
368369
}
369370

370-
otcrypto_status_t otcrypto_ed25519_keygen_async_finalize(
371+
otcrypto_status_t otcrypto_ed25519_public_key_from_private_async_finalize(
371372
otcrypto_unblinded_key_t *public_key) {
372373
// Finalize the keygen operation and retrieve the public key.
373374
HARDENED_TRY_WIPE_DMEM(curve25519_keygen_finalize(public_key->key));

sw/device/lib/crypto/include/ecc_curve25519.h

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,23 @@ typedef enum otcrypto_eddsa_sign_mode {
2929
} otcrypto_eddsa_sign_mode_t;
3030

3131
/**
32-
* Generates a key pair for Ed25519.
32+
* Derives the Ed25519 public key corresponding to a given private key.
3333
*
34-
* The caller should allocate and partially populate the public key struct,
35-
* including populating the key configuration and allocating space for the
36-
* keyblob. For a hardware-backed key, use the private key handle returned by
37-
* `otcrypto_hw_backed_key`. Otherwise, the mode should indicate Ed25519 and the
38-
* keyblob should be 80 bytes. The value in the `checksum` field of the
39-
* key struct will be populated by the key generation function.
34+
* The private key must be a pre-existing 32-byte Ed25519 seed with a valid
35+
* checksum (e.g. as returned by an import function). The public key is
36+
* derived by SHA-512-hashing the seed, clamping the result to obtain the
37+
* secret scalar, and multiplying the Ed25519 base point by that scalar.
4038
*
41-
* @param[out] private_key Pointer to the unblinded private key struct.
42-
* @param[out] public_key Pointer to the unblinded public key struct.
43-
* @return Result of the Ed25519 key generation.
39+
* The caller must allocate the public key struct and its `key` buffer
40+
* (32 bytes) before calling this function. The `checksum` field will be
41+
* populated on success.
42+
*
43+
* @param[in] private_key Pointer to the private key seed struct.
44+
* @param[out] public_key Pointer to the public key struct to populate.
45+
* @return Result of the Ed25519 public key derivation.
4446
*/
4547
OT_WARN_UNUSED_RESULT
46-
otcrypto_status_t otcrypto_ed25519_keygen(
48+
otcrypto_status_t otcrypto_ed25519_public_key_from_private(
4749
const otcrypto_unblinded_key_t *private_key,
4850
otcrypto_unblinded_key_t *public_key);
4951

@@ -106,27 +108,29 @@ otcrypto_status_t otcrypto_ed25519_sign_verify(
106108
/**
107109
* Starts asynchronous key generation for Ed25519.
108110
*
109-
* See `otcrypto_ed25519_keygen` for requirements on input values.
111+
* See `otcrypto_ed25519_public_key_from_private` for requirements on input
112+
* values.
110113
*
111114
* @param private_key Source structure for private key, or key handle.
112115
* @return Result of asynchronous Ed25519 keygen start operation.
113116
*/
114117
OT_WARN_UNUSED_RESULT
115-
otcrypto_status_t otcrypto_ed25519_keygen_async_start(
118+
otcrypto_status_t otcrypto_ed25519_public_key_from_private_async_start(
116119
const otcrypto_unblinded_key_t *private_key);
117120

118121
/**
119122
* Finalizes asynchronous key generation for Ed25519.
120123
*
121-
* See `otcrypto_ed25519_keygen` for requirements on input values.
124+
* See `otcrypto_ed25519_public_key_from_private` for requirements on input
125+
* values.
122126
*
123127
* May block until the operation is complete.
124128
*
125129
* @param[out] public_key Pointer to the unblinded public key struct.
126130
* @return Result of asynchronous ed25519 keygen finalize operation.
127131
*/
128132
OT_WARN_UNUSED_RESULT
129-
otcrypto_status_t otcrypto_ed25519_keygen_async_finalize(
133+
otcrypto_status_t otcrypto_ed25519_public_key_from_private_async_finalize(
130134
otcrypto_unblinded_key_t *public_key);
131135

132136
/**

sw/device/tests/crypto/ed25519_functest.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ status_t ed25519_kat_test(void) {
110110
};
111111

112112
// Run ed25519 key generation.
113-
CHECK_STATUS_OK(otcrypto_ed25519_keygen(&private_key, &public_key));
113+
CHECK_STATUS_OK(
114+
otcrypto_ed25519_public_key_from_private(&private_key, &public_key));
114115
// Check the ed25519 key generation result.
115116
TRY_CHECK_ARRAYS_EQ(kPublicKey, public_key.key, kEd25519PublicKeyWords);
116117

@@ -168,7 +169,8 @@ static status_t hasheddsa_test(void) {
168169
.key = public_key_buf,
169170
};
170171

171-
CHECK_STATUS_OK(otcrypto_ed25519_keygen(&private_key, &public_key));
172+
CHECK_STATUS_OK(
173+
otcrypto_ed25519_public_key_from_private(&private_key, &public_key));
172174

173175
otcrypto_const_byte_buf_t input_message =
174176
OTCRYPTO_MAKE_BUF(otcrypto_const_byte_buf_t, (const uint8_t *)kMessage,
@@ -279,26 +281,26 @@ static status_t run_negative_tests(void) {
279281
otcrypto_unblinded_key_t bad_key = valid_priv;
280282
bad_key.key_length = 31;
281283
bad_key.checksum = integrity_unblinded_checksum(&bad_key);
282-
CHECK(otcrypto_ed25519_keygen(&bad_key, &valid_pub).value ==
284+
CHECK(otcrypto_ed25519_public_key_from_private(&bad_key, &valid_pub).value ==
283285
OTCRYPTO_BAD_ARGS.value);
284286

285287
bad_key = valid_priv;
286288
bad_key.key_mode = kOtcryptoKeyModeEcdsaP256;
287289
bad_key.checksum = integrity_unblinded_checksum(&bad_key);
288-
CHECK(otcrypto_ed25519_keygen(&bad_key, &valid_pub).value ==
290+
CHECK(otcrypto_ed25519_public_key_from_private(&bad_key, &valid_pub).value ==
289291
OTCRYPTO_BAD_ARGS.value);
290292

291293
bad_key = valid_priv;
292294
bad_key.key = NULL;
293-
CHECK(otcrypto_ed25519_keygen(&bad_key, &valid_pub).value ==
295+
CHECK(otcrypto_ed25519_public_key_from_private(&bad_key, &valid_pub).value ==
294296
OTCRYPTO_BAD_ARGS.value);
295297

296298
bad_key = valid_priv;
297299
bad_key.checksum ^= 0xFFFFFFFF;
298-
CHECK(otcrypto_ed25519_keygen(&bad_key, &valid_pub).value ==
300+
CHECK(otcrypto_ed25519_public_key_from_private(&bad_key, &valid_pub).value ==
299301
OTCRYPTO_BAD_ARGS.value);
300302

301-
CHECK(otcrypto_ed25519_keygen(NULL, &valid_pub).value ==
303+
CHECK(otcrypto_ed25519_public_key_from_private(NULL, &valid_pub).value ==
302304
OTCRYPTO_BAD_ARGS.value);
303305

304306
// Test NULL data with len > 0 or invalid mode

sw/device/tests/crypto/otcrypto_interface.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,17 @@ volatile otcrypto_interface_t otcrypto = {
119119
.cshake256 = &otcrypto_cshake256,
120120

121121
// Ed25519 (blocking).
122-
.ed25519_keygen = &otcrypto_ed25519_keygen,
122+
.ed25519_public_key_from_private =
123+
&otcrypto_ed25519_public_key_from_private,
123124
.ed25519_sign = &otcrypto_ed25519_sign,
124125
.ed25519_sign_verify = &otcrypto_ed25519_sign_verify,
125126
.ed25519_verify = &otcrypto_ed25519_verify,
126127

127128
// Ed25519 (async).
128-
.ed25519_keygen_async_start = &otcrypto_ed25519_keygen_async_start,
129-
.ed25519_keygen_async_finalize = &otcrypto_ed25519_keygen_async_finalize,
129+
.ed25519_public_key_from_private_async_start =
130+
&otcrypto_ed25519_public_key_from_private_async_start,
131+
.ed25519_public_key_from_private_async_finalize =
132+
&otcrypto_ed25519_public_key_from_private_async_finalize,
130133
.ed25519_sign_part1_async_start = &otcrypto_ed25519_sign_part1_async_start,
131134
.ed25519_sign_part2_async_start = &otcrypto_ed25519_sign_part2_async_start,
132135
.ed25519_sign_async_finalize = &otcrypto_ed25519_sign_async_finalize,

sw/device/tests/crypto/otcrypto_interface.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ typedef struct otcrypto_interface_t {
198198
otcrypto_hash_digest_t *);
199199

200200
// ED25519
201-
otcrypto_status_t (*ed25519_keygen)(const otcrypto_unblinded_key_t *,
202-
otcrypto_unblinded_key_t *);
201+
otcrypto_status_t (*ed25519_public_key_from_private)(
202+
const otcrypto_unblinded_key_t *, otcrypto_unblinded_key_t *);
203203
otcrypto_status_t (*ed25519_sign)(const otcrypto_unblinded_key_t *,
204204
const otcrypto_const_byte_buf_t *,
205205
otcrypto_eddsa_sign_mode_t,
@@ -214,9 +214,9 @@ typedef struct otcrypto_interface_t {
214214
otcrypto_eddsa_sign_mode_t,
215215
const otcrypto_const_word32_buf_t *,
216216
hardened_bool_t *);
217-
otcrypto_status_t (*ed25519_keygen_async_start)(
217+
otcrypto_status_t (*ed25519_public_key_from_private_async_start)(
218218
const otcrypto_unblinded_key_t *);
219-
otcrypto_status_t (*ed25519_keygen_async_finalize)(
219+
otcrypto_status_t (*ed25519_public_key_from_private_async_finalize)(
220220
otcrypto_unblinded_key_t *);
221221
otcrypto_status_t (*ed25519_sign_async_start)(
222222
const otcrypto_blinded_key_t *, const otcrypto_const_byte_buf_t *,

0 commit comments

Comments
 (0)