Software APIs
rsa_modexp.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_RSA_RSA_MODEXP_H_
6 #define OPENTITAN_SW_DEVICE_LIB_CRYPTO_IMPL_RSA_RSA_MODEXP_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
12 #include "sw/device/lib/crypto/impl/rsa/rsa_datatypes.h"
13 #include "sw/device/lib/crypto/impl/status.h"
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif // __cplusplus
18 
19 /**
20  * Block until a modexp operation is complete and get the result size.
21  *
22  * After OTBN finishes processing, this function reads the mode and infers the
23  * size of the modulus/signature for the just-finished operation. It then
24  * populates the `num_words` parameter with this size (expressed in 32b words).
25  * This is designed so that callers can call `rsa_modexp_wait()` and then use
26  * the size to select the appropriate `finalize()` call.
27  *
28  * @param[out] num_words Number of words for result buffers.
29  * @return Status of the operation (OK or error).
30  */
31 status_t rsa_modexp_wait(size_t *num_words);
32 
33 /**
34  * Start a constant-time RSA-2048 modular exponentiation.
35  *
36  * This construct is for secret exponents, and is much slower than the
37  * variable-time version.
38  *
39  * Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
40  *
41  * @param base Exponentiation base.
42  * @param exp Exponent to raise the base to.
43  * @param modulus Modulus for exponentiation.
44  * @return Status of the operation (OK or error).
45  */
46 status_t rsa_modexp_consttime_2048_start(const rsa_2048_int_t *base,
47  const rsa_2048_int_t *exp,
48  const rsa_2048_int_t *modulus);
49 
50 /**
51  * Start a variable-time RSA-2048 modular exponentiation.
52  *
53  * Do not use this construct with secret exponents; its timing depends on the
54  * exponent.
55  *
56  * Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
57  *
58  * @param base Exponentiation base.
59  * @param exp Exponent to raise the base to.
60  * @param modulus Modulus for exponentiation.
61  * @return Status of the operation (OK or error).
62  */
63 status_t rsa_modexp_vartime_2048_start(const rsa_2048_int_t *base,
64  const uint32_t exp,
65  const rsa_2048_int_t *modulus);
66 
67 /**
68  * Waits for an RSA-2048 modular exponentiation to complete.
69  *
70  * Can be used after either:
71  * - `rsa_modexp_consttime_2048_start()`
72  * - `rsa_modexp_vartime_2048_start()`
73  *
74  * @param[out] result Exponentiation result = (base ^ exp) mod modulus.
75  * @return Status of the operation (OK or error).
76  */
77 status_t rsa_modexp_2048_finalize(rsa_2048_int_t *result);
78 
79 /**
80  * Start a constant-time RSA-3072 modular exponentiation.
81  *
82  * This construct is for secret exponents, and is much slower than the
83  * variable-time version.
84  *
85  * Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
86  *
87  * @param base Exponentiation base.
88  * @param exp Exponent to raise the base to.
89  * @param modulus Modulus for exponentiation.
90  * @return Status of the operation (OK or error).
91  */
92 status_t rsa_modexp_consttime_3072_start(const rsa_3072_int_t *base,
93  const rsa_3072_int_t *exp,
94  const rsa_3072_int_t *modulus);
95 
96 /**
97  * Start a variable-time RSA-3072 modular exponentiation.
98  *
99  * Do not use this construct with secret exponents; its timing depends on the
100  * exponent.
101  *
102  * Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
103  *
104  * @param base Exponentiation base.
105  * @param exp Exponent to raise the base to.
106  * @param modulus Modulus for exponentiation.
107  * @return Status of the operation (OK or error).
108  */
109 status_t rsa_modexp_vartime_3072_start(const rsa_3072_int_t *base,
110  const uint32_t exp,
111  const rsa_3072_int_t *modulus);
112 
113 /**
114  * Waits for an RSA-3072 modular exponentiation to complete.
115  *
116  * Can be used after either:
117  * - `rsa_modexp_consttime_3072_start()`
118  * - `rsa_modexp_vartime_3072_start()`
119  *
120  * @param[out] result Exponentiation result = (base ^ exp) mod modulus.
121  * @return Status of the operation (OK or error).
122  */
123 status_t rsa_modexp_3072_finalize(rsa_3072_int_t *result);
124 
125 /**
126  * Start a constant-time RSA-4096 modular exponentiation.
127  *
128  * This construct is for secret exponents, and is much slower than the
129  * variable-time version.
130  *
131  * Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
132  *
133  * @param base Exponentiation base.
134  * @param exp Exponent to raise the base to.
135  * @param modulus Modulus for exponentiation.
136  * @return Status of the operation (OK or error).
137  */
138 status_t rsa_modexp_consttime_4096_start(const rsa_4096_int_t *base,
139  const rsa_4096_int_t *exp,
140  const rsa_4096_int_t *modulus);
141 
142 /**
143  * Start a variable-time RSA-4096 modular exponentiation.
144  *
145  * Do not use this construct with secret exponents; its timing depends on the
146  * exponent.
147  *
148  * Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
149  *
150  * @param base Exponentiation base.
151  * @param exp Exponent to raise the base to.
152  * @param modulus Modulus for exponentiation.
153  * @return Status of the operation (OK or error).
154  */
155 status_t rsa_modexp_vartime_4096_start(const rsa_4096_int_t *base,
156  const uint32_t exp,
157  const rsa_4096_int_t *modulus);
158 
159 /**
160  * Waits for an RSA-4096 modular exponentiation to complete.
161  *
162  * Can be used after either:
163  * - `rsa_modexp_consttime_4096_start()`
164  * - `rsa_modexp_vartime_4096_start()`
165  *
166  * @param[out] result Exponentiation result = (base ^ exp) mod modulus.
167  * @return Status of the operation (OK or error).
168  */
169 status_t rsa_modexp_4096_finalize(rsa_4096_int_t *result);
170 
171 #ifdef __cplusplus
172 } // extern "C"
173 #endif // __cplusplus
174 
175 #endif // OPENTITAN_SW_DEVICE_LIB_CRYPTO_IMPL_RSA_RSA_MODEXP_H_