Software APIs
p384_common.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_P384_COMMON_H_
6 #define OPENTITAN_SW_DEVICE_LIB_CRYPTO_IMPL_ECC_P384_COMMON_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include "sw/device/lib/crypto/drivers/otbn.h"
12 #include "sw/device/lib/crypto/impl/status.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif // __cplusplus
17 
18 enum {
19  /**
20  * Length of a P-384 curve point coordinate in bits (modulo p).
21  */
22  kP384CoordBits = 384,
23  /**
24  * Length of a P-384 curve point coordinate in bytes.
25  */
26  kP384CoordBytes = kP384CoordBits / 8,
27  /**
28  * Length of a P-384 curve point coordinate in words.
29  */
30  kP384CoordWords = kP384CoordBytes / sizeof(uint32_t),
31  /**
32  * Length of an element in the P-384 scalar field (modulo the curve order n).
33  */
34  kP384ScalarBits = 384,
35  /**
36  * Length of a secret scalar share in bytes.
37  */
38  kP384ScalarBytes = kP384ScalarBits / 8,
39  /**
40  * Length of secret scalar share in words.
41  */
42  kP384ScalarWords = kP384ScalarBytes / sizeof(uint32_t),
43  /**
44  * Length of a masked secret scalar share.
45  *
46  * This implementation uses extra redundant bits for side-channel protection.
47  */
48  kP384MaskedScalarShareBits = kP384ScalarBits + 64,
49  /**
50  * Length of a masked secret scalar share in bytes.
51  */
52  kP384MaskedScalarShareBytes = kP384MaskedScalarShareBits / 8,
53  /**
54  * Length of masked secret scalar share in words.
55  */
56  kP384MaskedScalarShareWords = kP384MaskedScalarShareBytes / sizeof(uint32_t),
57 };
58 
59 /**
60  * A type that holds a masked value from the P-384 scalar field.
61  *
62  * This struct is used to represent secret keys, which are integers modulo n.
63  * The key d is represented in two 320-bit shares, d0 and d1, such that d = (d0
64  * + d1) mod n. Mathematically, d0 and d1 could also be reduced modulo n, but
65  * the extra bits provide side-channel protection.
66  */
67 typedef struct p384_masked_scalar {
68  /**
69  * First share of the secret scalar.
70  */
71  uint32_t share0[kP384MaskedScalarShareWords];
72  /**
73  * Second share of the secret scalar.
74  */
75  uint32_t share1[kP384MaskedScalarShareWords];
77 
78 /**
79  * A type that holds a P-384 curve point.
80  */
81 typedef struct p384_point {
82  /**
83  * Affine x-coordinate.
84  */
85  uint32_t x[kP384CoordWords];
86  /**
87  * Affine y-coordinate.
88  */
89  uint32_t y[kP384CoordWords];
90 } p384_point_t;
91 
92 /**
93  * A type that holds an ECDSA/P-384 signature.
94  *
95  * The signature consists of two integers r and s, computed modulo n.
96  */
97 typedef struct ecdsa_p384_signature_t {
98  uint32_t r[kP384ScalarWords];
99  uint32_t s[kP384ScalarWords];
101 
102 /**
103  * Write a masked P-384 scalar to OTBN's data memory.
104  *
105  * OTBN actually requires that 512 bits be written, even though only 320 are
106  * used; the others are ignored but must be set to avoid an error when OTBN
107  * attempts to read uninitialized memory.
108  *
109  * @param src Masked scalar to write.
110  * @param share0_addr DMEM address of the first share.
111  * @param share1_addr DMEM address of the second share.
112  * @return Result of the operation.
113  */
115 status_t p384_masked_scalar_write(const p384_masked_scalar_t *src,
116  const otbn_addr_t share0_addr,
117  const otbn_addr_t share1_addr);
118 
119 /**
120  * Set the message digest for signature generation or verification.
121  *
122  * OTBN requires the digest in little-endian form, so this routine flips the
123  * bytes.
124  *
125  * @param digest Digest to set (big-endian).
126  * @return OK or error.
127  */
129 status_t set_message_digest(const uint32_t digest[kP384ScalarWords],
130  const otbn_addr_t kOtbnVarEcdsaMsg);
131 
132 #ifdef __cplusplus
133 } // extern "C"
134 #endif // __cplusplus
135 
136 #endif // OPENTITAN_SW_DEVICE_LIB_CRYPTO_IMPL_ECC_P384_COMMON_H_