Software APIs
ecdh_p256.h
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_LIB_CRYPTO_IMPL_ECC_ECDH_P256_H_
6 #define OPENTITAN_SW_DEVICE_LIB_CRYPTO_IMPL_ECC_ECDH_P256_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
12 #include "sw/device/lib/crypto/drivers/otbn.h"
13 #include "sw/device/lib/crypto/impl/ecc/p256_common.h"
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif // __cplusplus
18 
19 /**
20  * A type that holds a blinded ECDH shared secret key.
21  *
22  * The key is boolean-masked (XOR of the two shares).
23  */
24 typedef struct ecdh_p256_shared_key {
25  uint32_t share0[kP256CoordWords];
26  uint32_t share1[kP256CoordWords];
28 
29 /**
30  * Start an async ECDH/P-256 keypair generation operation on OTBN.
31  *
32  * Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
33  *
34  * @return Result of the operation (OK or error).
35  */
37 status_t ecdh_p256_keypair_start(void);
38 
39 /**
40  * Finish an async ECDH/P-256 keypair generation operation on OTBN.
41  *
42  * Blocks until OTBN is idle.
43  *
44  * @param[out] private_key Generated private key.
45  * @param[out] public_key Generated public key.
46  * @return Result of the operation (OK or error).
47  */
49 status_t ecdh_p256_keypair_finalize(p256_masked_scalar_t *private_key,
50  p256_point_t *public_key);
51 
52 /**
53  * Start an async ECDH/P-256 shared key generation operation on OTBN.
54  *
55  * Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
56  *
57  * @param private_key Private key (d).
58  * @param public_key Public key (Q).
59  * @return Result of the operation (OK or error).
60  */
62 status_t ecdh_p256_shared_key_start(const p256_masked_scalar_t *private_key,
63  const p256_point_t *public_key);
64 
65 /**
66  * Finish an async ECDH/P-256 shared key generation operation on OTBN.
67  *
68  * Blocks until OTBN is idle. May be used after either
69  * `ecdh_p256_shared_key_start` or `ecdh_p256_sideload_shared_key_start`; the
70  * operation is the same.
71  *
72  * @param[out] shared_key Shared secret key (x-coordinate of d*Q).
73  * @return Result of the operation (OK or error).
74  */
76 status_t ecdh_p256_shared_key_finalize(ecdh_p256_shared_key_t *shared_key);
77 
78 /**
79  * Start an async ECDH/P-256 sideloaded keypair generation operation on OTBN.
80  *
81  * Generates the keypair from a key manager seed. The key manager should
82  * already have sideloaded the key into OTBN before this operation is called.
83  *
84  * Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
85  *
86  * @return Result of the operation (OK or error).
87  */
89 status_t ecdh_p256_sideload_keypair_start(void);
90 
91 /**
92  * Finish an async ECDH/P-256 sideloaded keypair generation operation on OTBN.
93  *
94  * Blocks until OTBN is idle. Returns only the public key.
95  *
96  * @param[out] public_key Generated public key.
97  * @return Result of the operation (OK or error).
98  */
100 status_t ecdh_p256_sideload_keypair_finalize(p256_point_t *public_key);
101 
102 /**
103  * Start an async ECDH/P-256 shared key generation operation on OTBN.
104  *
105  * Uses a private key generated from a key manager seed. The key manager should
106  * already have sideloaded the key into OTBN before this operation is called.
107  *
108  * Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
109  *
110  * @param public_key Public key (Q).
111  * @return Result of the operation (OK or error).
112  */
114 status_t ecdh_p256_sideload_shared_key_start(const p256_point_t *public_key);
115 
116 #ifdef __cplusplus
117 } // extern "C"
118 #endif // __cplusplus
119 
120 #endif // OPENTITAN_SW_DEVICE_LIB_CRYPTO_IMPL_ECC_ECDH_P256_H_