Software APIs
sw
device
lib
crypto
impl
rsa
rsa_encryption.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_ENCRYPTION_H_
6
#define OPENTITAN_SW_DEVICE_LIB_CRYPTO_IMPL_RSA_RSA_ENCRYPTION_H_
7
8
#include <
stddef.h
>
9
#include <
stdint.h
>
10
11
#include "
sw/device/lib/base/hardened.h
"
12
#include "sw/device/lib/crypto/impl/rsa/rsa_datatypes.h"
13
#include "sw/device/lib/crypto/impl/status.h"
14
#include "
sw/device/lib/crypto/include/datatypes.h
"
15
16
#ifdef __cplusplus
17
extern
"C"
{
18
#endif
// __cplusplus
19
20
/**
21
* Starts encrypting a message with RSA-2048; returns immediately.
22
*
23
* The key exponent must be F4=65537; no other exponents are supported. The
24
* padding scheme is OAEP, and the mask generation function (MGF) is MGF1 with
25
* the hash function indicated by `hash_mode` and a salt the same length as the
26
* hash function output.
27
*
28
* Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
29
*
30
* @param public_key RSA public key.
31
* @param hash_mode Hash function to use for message encoding.
32
* @param message Message to encrypt.
33
* @param message_bytelen Message length in bytes.
34
* @param label Label for OAEP padding.
35
* @param label_bytelen Length of label in bytes.
36
* @return Result of the operation (OK or error).
37
*/
38
OT_WARN_UNUSED_RESULT
39
status_t
rsa_encrypt_2048_start(
const
rsa_2048_public_key_t
*public_key,
40
const
otcrypto_hash_mode_t
hash_mode,
41
const
uint8_t *message,
size_t
message_bytelen,
42
const
uint8_t *label,
size_t
label_bytelen);
43
44
/**
45
* Waits for an RSA-2048 encryption to complete.
46
*
47
* Should be invoked only after a `rsa_encrypt_2048_start` call. Blocks until
48
* OTBN is done processing.
49
*
50
* @param[out] ciphertext Encrypted message.
51
* @return Result of the operation (OK or error).
52
*/
53
OT_WARN_UNUSED_RESULT
54
status_t
rsa_encrypt_2048_finalize(
rsa_2048_int_t
*ciphertext);
55
56
/**
57
* Start decrypting a message with RSA-2048; returns immediately.
58
*
59
* Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
60
*
61
* @param private_key RSA private key.
62
* @param ciphertext Encrypted message.
63
* @return Result of the operation (OK or error).
64
*/
65
OT_WARN_UNUSED_RESULT
66
status_t
rsa_decrypt_2048_start(
const
rsa_2048_private_key_t
*private_key,
67
const
rsa_2048_int_t
*ciphertext);
68
69
/**
70
* Waits for an RSA decryption to complete.
71
*
72
* Should be invoked only after an `rsa_decrypt_{size}_start`, but works for
73
* any RSA size. Blocks until OTBN is done processing, and then infers the size
74
* from the OTBN application mode.
75
*
76
* The only supported padding mode is OAEP (see IETF RFC 8017, section 7.1.2).
77
* Only fixed-length hash functions (i.e. the SHA-2 or SHA-3 families) are
78
* supported. The mask generation function (MGF) is MGF1 with the hash function
79
* indicated by `hash_mode`.
80
*
81
* The caller must ensure that enough space is allocated at `plaintext` to hold
82
* the largest possible plaintext, which is (as described in IETF RFC 8017,
83
* section 7.1.2):
84
* <length of modulus> - 2 * <length of hash function> - 2 bytes
85
*
86
* For example, if the hash function here is SHA-512 (64-byte digest), the
87
* maximum plaintext byte-length would be:
88
* (2048 / 8) - 2 * 64 - 2 = 126 bytes
89
*
90
* This routine will not necessarily use all available bytes in the plaintext,
91
* and will write the actual length of the plaintext into `plaintext_bytelen`.
92
*
93
* @param hash_mode Hash function to use for message decoding.
94
* @param label Label for OAEP padding.
95
* @param label_bytelen Length of label in bytes.
96
* @param plaintext_max_bytelen Space allocated for the plaintext in bytes.
97
* @param[out] plaintext Decrypted message.
98
* @param[out] plaintext_bytelen Length of plaintext in bytes.
99
* @return Result of the operation (OK or error).
100
*/
101
OT_WARN_UNUSED_RESULT
102
status_t
rsa_decrypt_finalize(
const
otcrypto_hash_mode_t
hash_mode,
103
const
uint8_t *label,
size_t
label_bytelen,
104
size_t
plaintext_max_bytelen, uint8_t *plaintext,
105
size_t
*plaintext_bytelen);
106
107
/**
108
* Starts encrypting a message with RSA-3072; returns immediately.
109
*
110
* The key exponent must be F4=65537; no other exponents are supported. The
111
* padding scheme is OAEP, and the mask generation function (MGF) is MGF1 with
112
* the hash function indicated by `hash_mode` and a salt the same length as the
113
* hash function output.
114
*
115
* Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
116
*
117
* @param public_key RSA public key.
118
* @param hash_mode Hash function to use for message encoding.
119
* @param message Message to encrypt.
120
* @param message_bytelen Message length in bytes.
121
* @param label Label for OAEP padding.
122
* @param label_bytelen Length of label in bytes.
123
* @return Result of the operation (OK or error).
124
*/
125
OT_WARN_UNUSED_RESULT
126
status_t
rsa_encrypt_3072_start(
const
rsa_3072_public_key_t
*public_key,
127
const
otcrypto_hash_mode_t
hash_mode,
128
const
uint8_t *message,
size_t
message_bytelen,
129
const
uint8_t *label,
size_t
label_bytelen);
130
131
/**
132
* Waits for an RSA-3072 encryption to complete.
133
*
134
* Should be invoked only after a `rsa_encrypt_3072_start` call. Blocks until
135
* OTBN is done processing.
136
*
137
* @param[out] ciphertext Encrypted message.
138
* @return Result of the operation (OK or error).
139
*/
140
OT_WARN_UNUSED_RESULT
141
status_t
rsa_encrypt_3072_finalize(
rsa_3072_int_t
*ciphertext);
142
143
/**
144
* Start decrypting a message with RSA-3072; returns immediately.
145
*
146
* Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
147
*
148
* @param private_key RSA private key.
149
* @param ciphertext Encrypted message.
150
* @return Result of the operation (OK or error).
151
*/
152
OT_WARN_UNUSED_RESULT
153
status_t
rsa_decrypt_3072_start(
const
rsa_3072_private_key_t
*private_key,
154
const
rsa_3072_int_t
*ciphertext);
155
156
/**
157
* Starts encrypting a message with RSA-4096; returns immediately.
158
*
159
* The key exponent must be F4=65537; no other exponents are supported. The
160
* padding scheme is OAEP, and the mask generation function (MGF) is MGF1 with
161
* the hash function indicated by `hash_mode` and a salt the same length as the
162
* hash function output.
163
*
164
* Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
165
*
166
* @param public_key RSA public key.
167
* @param hash_mode Hash function to use for message encoding.
168
* @param message Message to encrypt.
169
* @param message_bytelen Message length in bytes.
170
* @param label Label for OAEP padding.
171
* @param label_bytelen Length of label in bytes.
172
* @return Result of the operation (OK or error).
173
*/
174
OT_WARN_UNUSED_RESULT
175
status_t
rsa_encrypt_4096_start(
const
rsa_4096_public_key_t
*public_key,
176
const
otcrypto_hash_mode_t
hash_mode,
177
const
uint8_t *message,
size_t
message_bytelen,
178
const
uint8_t *label,
size_t
label_bytelen);
179
180
/**
181
* Waits for an RSA-4096 encryption to complete.
182
*
183
* Should be invoked only after a `rsa_encrypt_4096_start` call. Blocks until
184
* OTBN is done processing.
185
*
186
* @param[out] ciphertext Encrypted message.
187
* @return Result of the operation (OK or error).
188
*/
189
OT_WARN_UNUSED_RESULT
190
status_t
rsa_encrypt_4096_finalize(
rsa_4096_int_t
*ciphertext);
191
192
/**
193
* Start decrypting a message with RSA-4096; returns immediately.
194
*
195
* Returns an `OTCRYPTO_ASYNC_INCOMPLETE` error if OTBN is busy.
196
*
197
* @param private_key RSA private key.
198
* @param ciphertext Encrypted message.
199
* @return Result of the operation (OK or error).
200
*/
201
OT_WARN_UNUSED_RESULT
202
status_t
rsa_decrypt_4096_start(
const
rsa_4096_private_key_t
*private_key,
203
const
rsa_4096_int_t
*ciphertext);
204
205
#ifdef __cplusplus
206
}
// extern "C"
207
#endif
// __cplusplus
208
209
#endif
// OPENTITAN_SW_DEVICE_LIB_CRYPTO_IMPL_RSA_RSA_ENCRYPTION_H_
Return to
OpenTitan Documentation