Fixed some typographic errors in comments.
[BearSSL] / inc / bearssl_rsa.h
1 /*
2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #ifndef BR_BEARSSL_RSA_H__
26 #define BR_BEARSSL_RSA_H__
27
28 #include <stddef.h>
29 #include <stdint.h>
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /** \file bearssl_rsa.h
36 *
37 * # RSA
38 *
39 * This file documents the RSA implementations provided with BearSSL.
40 * Note that the SSL engine accesses these implementations through a
41 * configurable API, so it is possible to, for instance, run a SSL
42 * server which uses a RSA engine which is not based on this code.
43 *
44 * ## Key Elements
45 *
46 * RSA public and private keys consist in lists of big integers. All
47 * such integers are represented with big-endian unsigned notation:
48 * first byte is the most significant, and the value is positive (so
49 * there is no dedicated "sign bit"). Public and private key structures
50 * thus contain, for each such integer, a pointer to the first value byte
51 * (`unsigned char *`), and a length (`size_t`) which is the number of
52 * relevant bytes. As a general rule, minimal-length encoding is not
53 * enforced: values may have extra leading bytes of value 0.
54 *
55 * RSA public keys consist in two integers:
56 *
57 * - the modulus (`n`);
58 * - the public exponent (`e`).
59 *
60 * RSA private keys, as defined in
61 * [PKCS#1](https://tools.ietf.org/html/rfc3447), contain eight integers:
62 *
63 * - the modulus (`n`);
64 * - the public exponent (`e`);
65 * - the private exponent (`d`);
66 * - the first prime factor (`p`);
67 * - the second prime factor (`q`);
68 * - the first reduced exponent (`dp`, which is `d` modulo `p-1`);
69 * - the second reduced exponent (`dq`, which is `d` modulo `q-1`);
70 * - the CRT coefficient (`iq`, the inverse of `q` modulo `p`).
71 *
72 * However, the implementations defined in BearSSL use only five of
73 * these integers: `p`, `q`, `dp`, `dq` and `iq`.
74 *
75 * ## Security Features and Limitations
76 *
77 * The implementations contained in BearSSL have the following limitations
78 * and features:
79 *
80 * - They are constant-time. This means that the execution time and
81 * memory access pattern may depend on the _lengths_ of the private
82 * key components, but not on their value, nor on the value of
83 * the operand. Note that this property is not achieved through
84 * random masking, but "true" constant-time code.
85 *
86 * - They support only private keys with two prime factors. RSA private
87 * key with three or more prime factors are nominally supported, but
88 * rarely used; they may offer faster operations, at the expense of
89 * more code and potentially a reduction in security if there are
90 * "too many" prime factors.
91 *
92 * - The public exponent may have arbitrary length. Of course, it is
93 * a good idea to keep public exponents small, so that public key
94 * operations are fast; but, contrary to some widely deployed
95 * implementations, BearSSL has no problem with public exponent
96 * longer than 32 bits.
97 *
98 * - The two prime factors of the modulus need not have the same length
99 * (but severely imbalanced factor lengths might reduce security).
100 * Similarly, there is no requirement that the first factor (`p`)
101 * be greater than the second factor (`q`).
102 *
103 * - Prime factors and modulus must be smaller than a compile-time limit.
104 * This is made necessary by the use of fixed-size stack buffers, and
105 * the limit has been adjusted to keep stack usage under 2 kB for the
106 * RSA operations. Currently, the maximum modulus size is 4096 bits,
107 * and the maximum prime factor size is 2080 bits.
108 *
109 * - The RSA functions themselves do not enforce lower size limits,
110 * except that which is absolutely necessary for the operation to
111 * mathematically make sense (e.g. a PKCS#1 v1.5 signature with
112 * SHA-1 requires a modulus of at least 361 bits). It is up to users
113 * of this code to enforce size limitations when appropriate (e.g.
114 * the X.509 validation engine, by default, rejects RSA keys of
115 * less than 1017 bits).
116 *
117 * - Within the size constraints expressed above, arbitrary bit lengths
118 * are supported. There is no requirement that prime factors or
119 * modulus have a size multiple of 8 or 16.
120 *
121 * - When verifying PKCS#1 v1.5 signatures, both variants of the hash
122 * function identifying header (with and without the ASN.1 NULL) are
123 * supported. When producing such signatures, the variant with the
124 * ASN.1 NULL is used.
125 *
126 * ## Implementations
127 *
128 * Three RSA implementations are included:
129 *
130 * - The **i32** implementation internally represents big integers
131 * as arrays of 32-bit integers. It is perfunctory and portable,
132 * but not very efficient.
133 *
134 * - The **i31** implementation uses 32-bit integers, each containing
135 * 31 bits worth of integer data. The i31 implementation is somewhat
136 * faster than the i32 implementation (the reduced integer size makes
137 * carry propagation easier) for a similar code footprint, but uses
138 * very slightly larger stack buffers (about 4% bigger).
139 *
140 * - The **i62** implementation is similar to the i31 implementation,
141 * except that it internally leverages the 64x64->128 multiplication
142 * opcode. This implementation is available only on architectures
143 * where such an opcode exists. It is much faster than i31.
144 *
145 * - The **i15** implementation uses 16-bit integers, each containing
146 * 15 bits worth of integer data. Multiplication results fit on
147 * 32 bits, so this won't use the "widening" multiplication routine
148 * on ARM Cortex M0/M0+, for much better performance and constant-time
149 * execution.
150 */
151
152 /**
153 * \brief RSA public key.
154 *
155 * The structure references the modulus and the public exponent. Both
156 * integers use unsigned big-endian representation; extra leading bytes
157 * of value 0 are allowed.
158 */
159 typedef struct {
160 /** \brief Modulus. */
161 unsigned char *n;
162 /** \brief Modulus length (in bytes). */
163 size_t nlen;
164 /** \brief Public exponent. */
165 unsigned char *e;
166 /** \brief Public exponent length (in bytes). */
167 size_t elen;
168 } br_rsa_public_key;
169
170 /**
171 * \brief RSA private key.
172 *
173 * The structure references the primvate factors, reduced private
174 * exponents, and CRT coefficient. It also contains the bit length of
175 * the modulus. The big integers use unsigned big-endian representation;
176 * extra leading bytes of value 0 are allowed. However, the modulus bit
177 * length (`n_bitlen`) MUST be exact.
178 */
179 typedef struct {
180 /** \brief Modulus bit length (in bits, exact value). */
181 uint32_t n_bitlen;
182 /** \brief First prime factor. */
183 unsigned char *p;
184 /** \brief First prime factor length (in bytes). */
185 size_t plen;
186 /** \brief Second prime factor. */
187 unsigned char *q;
188 /** \brief Second prime factor length (in bytes). */
189 size_t qlen;
190 /** \brief First reduced private exponent. */
191 unsigned char *dp;
192 /** \brief First reduced private exponent length (in bytes). */
193 size_t dplen;
194 /** \brief Second reduced private exponent. */
195 unsigned char *dq;
196 /** \brief Second reduced private exponent length (in bytes). */
197 size_t dqlen;
198 /** \brief CRT coefficient. */
199 unsigned char *iq;
200 /** \brief CRT coefficient length (in bytes). */
201 size_t iqlen;
202 } br_rsa_private_key;
203
204 /**
205 * \brief Type for a RSA public key engine.
206 *
207 * The public key engine performs the modular exponentiation of the
208 * provided value with the public exponent. The value is modified in
209 * place.
210 *
211 * The value length (`xlen`) is verified to have _exactly_ the same
212 * length as the modulus (actual modulus length, without extra leading
213 * zeros in the modulus representation in memory). If the length does
214 * not match, then this function returns 0 and `x[]` is unmodified.
215 *
216 * It `xlen` is correct, then `x[]` is modified. Returned value is 1
217 * on success, 0 on error. Error conditions include an oversized `x[]`
218 * (the array has the same length as the modulus, but the numerical value
219 * is not lower than the modulus) and an invalid modulus (e.g. an even
220 * integer). If an error is reported, then the new contents of `x[]` are
221 * unspecified.
222 *
223 * \param x operand to exponentiate.
224 * \param xlen length of the operand (in bytes).
225 * \param pk RSA public key.
226 * \return 1 on success, 0 on error.
227 */
228 typedef uint32_t (*br_rsa_public)(unsigned char *x, size_t xlen,
229 const br_rsa_public_key *pk);
230
231 /**
232 * \brief Type for a RSA signature verification engine (PKCS#1 v1.5).
233 *
234 * Parameters are:
235 *
236 * - The signature itself. The provided array is NOT modified.
237 *
238 * - The encoded OID for the hash function. The provided array must begin
239 * with a single byte that contains the length of the OID value (in
240 * bytes), followed by exactly that many bytes. This parameter may
241 * also be `NULL`, in which case the raw hash value should be used
242 * with the PKCS#1 v1.5 "type 1" padding (as used in SSL/TLS up
243 * to TLS-1.1, with a 36-byte hash value).
244 *
245 * - The hash output length, in bytes.
246 *
247 * - The public key.
248 *
249 * - An output buffer for the hash value. The caller must still compare
250 * it with the hash of the data over which the signature is computed.
251 *
252 * **Constraints:**
253 *
254 * - Hash length MUST be no more than 64 bytes.
255 *
256 * - OID value length MUST be no more than 32 bytes (i.e. `hash_oid[0]`
257 * must have a value in the 0..32 range, inclusive).
258 *
259 * This function verifies that the signature length (`xlen`) matches the
260 * modulus length (this function returns 0 on mismatch). If the modulus
261 * size exceeds the maximum supported RSA size, then the function also
262 * returns 0.
263 *
264 * Returned value is 1 on success, 0 on error.
265 *
266 * Implementations of this type need not be constant-time.
267 *
268 * \param x signature buffer.
269 * \param xlen signature length (in bytes).
270 * \param hash_oid encoded hash algorithm OID (or `NULL`).
271 * \param hash_len expected hash value length (in bytes).
272 * \param pk RSA public key.
273 * \param hash_out output buffer for the hash value.
274 * \return 1 on success, 0 on error.
275 */
276 typedef uint32_t (*br_rsa_pkcs1_vrfy)(const unsigned char *x, size_t xlen,
277 const unsigned char *hash_oid, size_t hash_len,
278 const br_rsa_public_key *pk, unsigned char *hash_out);
279
280 /**
281 * \brief Type for a RSA encryption engine (OAEP).
282 *
283 * Parameters are:
284 *
285 * - A source of random bytes. The source must be already initialized.
286 *
287 * - A hash function, used internally with the mask generation function
288 * (MGF1).
289 *
290 * - A label. The `label` pointer may be `NULL` if `label_len` is zero
291 * (an empty label, which is the default in PKCS#1 v2.2).
292 *
293 * - The public key.
294 *
295 * - The destination buffer. Its maximum length (in bytes) is provided;
296 * if that length is lower than the public key length, then an error
297 * is reported.
298 *
299 * - The source message.
300 *
301 * The encrypted message output has exactly the same length as the modulus
302 * (mathematical length, in bytes, not counting extra leading zeros in the
303 * modulus representation in the public key).
304 *
305 * The source message (`src`, length `src_len`) may overlap with the
306 * destination buffer (`dst`, length `dst_max_len`).
307 *
308 * This function returns the actual encrypted message length, in bytes;
309 * on error, zero is returned. An error is reported if the output buffer
310 * is not large enough, or the public is invalid, or the public key
311 * modulus exceeds the maximum supported RSA size.
312 *
313 * \param rnd source of random bytes.
314 * \param dig hash function to use with MGF1.
315 * \param label label value (may be `NULL` if `label_len` is zero).
316 * \param label_len label length, in bytes.
317 * \param pk RSA public key.
318 * \param dst destination buffer.
319 * \param dst_max_len destination buffer length (maximum encrypted data size).
320 * \param src message to encrypt.
321 * \param src_len source message length (in bytes).
322 * \return encrypted message length (in bytes), or 0 on error.
323 */
324 typedef size_t (*br_rsa_oaep_encrypt)(
325 const br_prng_class **rnd, const br_hash_class *dig,
326 const void *label, size_t label_len,
327 const br_rsa_public_key *pk,
328 void *dst, size_t dst_max_len,
329 const void *src, size_t src_len);
330
331 /**
332 * \brief Type for a RSA private key engine.
333 *
334 * The `x[]` buffer is modified in place, and its length is inferred from
335 * the modulus length (`x[]` is assumed to have a length of
336 * `(sk->n_bitlen+7)/8` bytes).
337 *
338 * Returned value is 1 on success, 0 on error.
339 *
340 * \param x operand to exponentiate.
341 * \param sk RSA private key.
342 * \return 1 on success, 0 on error.
343 */
344 typedef uint32_t (*br_rsa_private)(unsigned char *x,
345 const br_rsa_private_key *sk);
346
347 /**
348 * \brief Type for a RSA signature generation engine (PKCS#1 v1.5).
349 *
350 * Parameters are:
351 *
352 * - The encoded OID for the hash function. The provided array must begin
353 * with a single byte that contains the length of the OID value (in
354 * bytes), followed by exactly that many bytes. This parameter may
355 * also be `NULL`, in which case the raw hash value should be used
356 * with the PKCS#1 v1.5 "type 1" padding (as used in SSL/TLS up
357 * to TLS-1.1, with a 36-byte hash value).
358 *
359 * - The hash value computes over the data to sign (its length is
360 * expressed in bytes).
361 *
362 * - The RSA private key.
363 *
364 * - The output buffer, that receives the signature.
365 *
366 * Returned value is 1 on success, 0 on error. Error conditions include
367 * a too small modulus for the provided hash OID and value, or some
368 * invalid key parameters. The signature length is exactly
369 * `(sk->n_bitlen+7)/8` bytes.
370 *
371 * This function is expected to be constant-time with regards to the
372 * private key bytes (lengths of the modulus and the individual factors
373 * may leak, though) and to the hashed data.
374 *
375 * \param hash_oid encoded hash algorithm OID (or `NULL`).
376 * \param hash hash value.
377 * \param hash_len hash value length (in bytes).
378 * \param sk RSA private key.
379 * \param x output buffer for the signature value.
380 * \return 1 on success, 0 on error.
381 */
382 typedef uint32_t (*br_rsa_pkcs1_sign)(const unsigned char *hash_oid,
383 const unsigned char *hash, size_t hash_len,
384 const br_rsa_private_key *sk, unsigned char *x);
385
386 /**
387 * \brief Encoded OID for SHA-1 (in RSA PKCS#1 signatures).
388 */
389 #define BR_HASH_OID_SHA1 \
390 ((const unsigned char *)"\x05\x2B\x0E\x03\x02\x1A")
391
392 /**
393 * \brief Encoded OID for SHA-224 (in RSA PKCS#1 signatures).
394 */
395 #define BR_HASH_OID_SHA224 \
396 ((const unsigned char *)"\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04")
397
398 /**
399 * \brief Encoded OID for SHA-256 (in RSA PKCS#1 signatures).
400 */
401 #define BR_HASH_OID_SHA256 \
402 ((const unsigned char *)"\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01")
403
404 /**
405 * \brief Encoded OID for SHA-384 (in RSA PKCS#1 signatures).
406 */
407 #define BR_HASH_OID_SHA384 \
408 ((const unsigned char *)"\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02")
409
410 /**
411 * \brief Encoded OID for SHA-512 (in RSA PKCS#1 signatures).
412 */
413 #define BR_HASH_OID_SHA512 \
414 ((const unsigned char *)"\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03")
415
416 /**
417 * \brief Type for a RSA decryption engine (OAEP).
418 *
419 * Parameters are:
420 *
421 * - A hash function, used internally with the mask generation function
422 * (MGF1).
423 *
424 * - A label. The `label` pointer may be `NULL` if `label_len` is zero
425 * (an empty label, which is the default in PKCS#1 v2.2).
426 *
427 * - The private key.
428 *
429 * - The source and destination buffer. The buffer initially contains
430 * the encrypted message; the buffer contents are altered, and the
431 * decrypted message is written at the start of that buffer
432 * (decrypted message is always shorter than the encrypted message).
433 *
434 * If decryption fails in any way, then `*len` is unmodified, and the
435 * function returns 0. Otherwise, `*len` is set to the decrypted message
436 * length, and 1 is returned. The implementation is responsible for
437 * checking that the input message length matches the key modulus length,
438 * and that the padding is correct.
439 *
440 * Implementations MUST use constant-time check of the validity of the
441 * OAEP padding, at least until the leading byte and hash value have
442 * been checked. Whether overall decryption worked, and the length of
443 * the decrypted message, may leak.
444 *
445 * \param dig hash function to use with MGF1.
446 * \param label label value (may be `NULL` if `label_len` is zero).
447 * \param label_len label length, in bytes.
448 * \param sk RSA private key.
449 * \param data input/output buffer.
450 * \param len encrypted/decrypted message length.
451 * \return 1 on success, 0 on error.
452 */
453 typedef uint32_t (*br_rsa_oaep_decrypt)(
454 const br_hash_class *dig, const void *label, size_t label_len,
455 const br_rsa_private_key *sk, void *data, size_t *len);
456
457 /*
458 * RSA "i32" engine. Integers are internally represented as arrays of
459 * 32-bit integers, and the core multiplication primitive is the
460 * 32x32->64 multiplication.
461 */
462
463 /**
464 * \brief RSA public key engine "i32".
465 *
466 * \see br_rsa_public
467 *
468 * \param x operand to exponentiate.
469 * \param xlen length of the operand (in bytes).
470 * \param pk RSA public key.
471 * \return 1 on success, 0 on error.
472 */
473 uint32_t br_rsa_i32_public(unsigned char *x, size_t xlen,
474 const br_rsa_public_key *pk);
475
476 /**
477 * \brief RSA signature verification engine "i32".
478 *
479 * \see br_rsa_pkcs1_vrfy
480 *
481 * \param x signature buffer.
482 * \param xlen signature length (in bytes).
483 * \param hash_oid encoded hash algorithm OID (or `NULL`).
484 * \param hash_len expected hash value length (in bytes).
485 * \param pk RSA public key.
486 * \param hash_out output buffer for the hash value.
487 * \return 1 on success, 0 on error.
488 */
489 uint32_t br_rsa_i32_pkcs1_vrfy(const unsigned char *x, size_t xlen,
490 const unsigned char *hash_oid, size_t hash_len,
491 const br_rsa_public_key *pk, unsigned char *hash_out);
492
493 /**
494 * \brief RSA private key engine "i32".
495 *
496 * \see br_rsa_private
497 *
498 * \param x operand to exponentiate.
499 * \param sk RSA private key.
500 * \return 1 on success, 0 on error.
501 */
502 uint32_t br_rsa_i32_private(unsigned char *x,
503 const br_rsa_private_key *sk);
504
505 /**
506 * \brief RSA signature generation engine "i32".
507 *
508 * \see br_rsa_pkcs1_sign
509 *
510 * \param hash_oid encoded hash algorithm OID (or `NULL`).
511 * \param hash hash value.
512 * \param hash_len hash value length (in bytes).
513 * \param sk RSA private key.
514 * \param x output buffer for the hash value.
515 * \return 1 on success, 0 on error.
516 */
517 uint32_t br_rsa_i32_pkcs1_sign(const unsigned char *hash_oid,
518 const unsigned char *hash, size_t hash_len,
519 const br_rsa_private_key *sk, unsigned char *x);
520
521 /*
522 * RSA "i31" engine. Similar to i32, but only 31 bits are used per 32-bit
523 * word. This uses slightly more stack space (about 4% more) and code
524 * space, but it quite faster.
525 */
526
527 /**
528 * \brief RSA public key engine "i31".
529 *
530 * \see br_rsa_public
531 *
532 * \param x operand to exponentiate.
533 * \param xlen length of the operand (in bytes).
534 * \param pk RSA public key.
535 * \return 1 on success, 0 on error.
536 */
537 uint32_t br_rsa_i31_public(unsigned char *x, size_t xlen,
538 const br_rsa_public_key *pk);
539
540 /**
541 * \brief RSA signature verification engine "i31".
542 *
543 * \see br_rsa_pkcs1_vrfy
544 *
545 * \param x signature buffer.
546 * \param xlen signature length (in bytes).
547 * \param hash_oid encoded hash algorithm OID (or `NULL`).
548 * \param hash_len expected hash value length (in bytes).
549 * \param pk RSA public key.
550 * \param hash_out output buffer for the hash value.
551 * \return 1 on success, 0 on error.
552 */
553 uint32_t br_rsa_i31_pkcs1_vrfy(const unsigned char *x, size_t xlen,
554 const unsigned char *hash_oid, size_t hash_len,
555 const br_rsa_public_key *pk, unsigned char *hash_out);
556
557 /**
558 * \brief RSA private key engine "i31".
559 *
560 * \see br_rsa_private
561 *
562 * \param x operand to exponentiate.
563 * \param sk RSA private key.
564 * \return 1 on success, 0 on error.
565 */
566 uint32_t br_rsa_i31_private(unsigned char *x,
567 const br_rsa_private_key *sk);
568
569 /**
570 * \brief RSA signature generation engine "i31".
571 *
572 * \see br_rsa_pkcs1_sign
573 *
574 * \param hash_oid encoded hash algorithm OID (or `NULL`).
575 * \param hash hash value.
576 * \param hash_len hash value length (in bytes).
577 * \param sk RSA private key.
578 * \param x output buffer for the hash value.
579 * \return 1 on success, 0 on error.
580 */
581 uint32_t br_rsa_i31_pkcs1_sign(const unsigned char *hash_oid,
582 const unsigned char *hash, size_t hash_len,
583 const br_rsa_private_key *sk, unsigned char *x);
584
585 /*
586 * RSA "i62" engine. Similar to i31, but internal multiplication use
587 * 64x64->128 multiplications. This is available only on architecture
588 * that offer such an opcode.
589 */
590
591 /**
592 * \brief RSA public key engine "i62".
593 *
594 * This function is defined only on architecture that offer a 64x64->128
595 * opcode. Use `br_rsa_i62_public_get()` to dynamically obtain a pointer
596 * to that function.
597 *
598 * \see br_rsa_public
599 *
600 * \param x operand to exponentiate.
601 * \param xlen length of the operand (in bytes).
602 * \param pk RSA public key.
603 * \return 1 on success, 0 on error.
604 */
605 uint32_t br_rsa_i62_public(unsigned char *x, size_t xlen,
606 const br_rsa_public_key *pk);
607
608 /**
609 * \brief RSA signature verification engine "i62".
610 *
611 * This function is defined only on architecture that offer a 64x64->128
612 * opcode. Use `br_rsa_i62_pkcs1_vrfy_get()` to dynamically obtain a pointer
613 * to that function.
614 *
615 * \see br_rsa_pkcs1_vrfy
616 *
617 * \param x signature buffer.
618 * \param xlen signature length (in bytes).
619 * \param hash_oid encoded hash algorithm OID (or `NULL`).
620 * \param hash_len expected hash value length (in bytes).
621 * \param pk RSA public key.
622 * \param hash_out output buffer for the hash value.
623 * \return 1 on success, 0 on error.
624 */
625 uint32_t br_rsa_i62_pkcs1_vrfy(const unsigned char *x, size_t xlen,
626 const unsigned char *hash_oid, size_t hash_len,
627 const br_rsa_public_key *pk, unsigned char *hash_out);
628
629 /**
630 * \brief RSA private key engine "i62".
631 *
632 * This function is defined only on architecture that offer a 64x64->128
633 * opcode. Use `br_rsa_i62_private_get()` to dynamically obtain a pointer
634 * to that function.
635 *
636 * \see br_rsa_private
637 *
638 * \param x operand to exponentiate.
639 * \param sk RSA private key.
640 * \return 1 on success, 0 on error.
641 */
642 uint32_t br_rsa_i62_private(unsigned char *x,
643 const br_rsa_private_key *sk);
644
645 /**
646 * \brief RSA signature generation engine "i62".
647 *
648 * This function is defined only on architecture that offer a 64x64->128
649 * opcode. Use `br_rsa_i62_pkcs1_sign_get()` to dynamically obtain a pointer
650 * to that function.
651 *
652 * \see br_rsa_pkcs1_sign
653 *
654 * \param hash_oid encoded hash algorithm OID (or `NULL`).
655 * \param hash hash value.
656 * \param hash_len hash value length (in bytes).
657 * \param sk RSA private key.
658 * \param x output buffer for the hash value.
659 * \return 1 on success, 0 on error.
660 */
661 uint32_t br_rsa_i62_pkcs1_sign(const unsigned char *hash_oid,
662 const unsigned char *hash, size_t hash_len,
663 const br_rsa_private_key *sk, unsigned char *x);
664
665 /**
666 * \brief Get the RSA "i62" implementation (public key operations),
667 * if available.
668 *
669 * \return the implementation, or 0.
670 */
671 br_rsa_public br_rsa_i62_public_get(void);
672
673 /**
674 * \brief Get the RSA "i62" implementation (PKCS#1 signature verification),
675 * if available.
676 *
677 * \return the implementation, or 0.
678 */
679 br_rsa_pkcs1_vrfy br_rsa_i62_pkcs1_vrfy_get(void);
680
681 /**
682 * \brief Get the RSA "i62" implementation (private key operations),
683 * if available.
684 *
685 * \return the implementation, or 0.
686 */
687 br_rsa_private br_rsa_i62_private_get(void);
688
689 /**
690 * \brief Get the RSA "i62" implementation (PKCS#1 signature generation),
691 * if available.
692 *
693 * \return the implementation, or 0.
694 */
695 br_rsa_pkcs1_sign br_rsa_i62_pkcs1_sign_get(void);
696
697 /**
698 * \brief Get the RSA "i62" implementation (OAEP encryption),
699 * if available.
700 *
701 * \return the implementation, or 0.
702 */
703 br_rsa_oaep_encrypt br_rsa_i62_oaep_encrypt_get(void);
704
705 /**
706 * \brief Get the RSA "i62" implementation (OAEP decryption),
707 * if available.
708 *
709 * \return the implementation, or 0.
710 */
711 br_rsa_oaep_decrypt br_rsa_i62_oaep_decrypt_get(void);
712
713 /*
714 * RSA "i15" engine. Integers are represented as 15-bit integers, so
715 * the code uses only 32-bit multiplication (no 64-bit result), which
716 * is vastly faster (and constant-time) on the ARM Cortex M0/M0+.
717 */
718
719 /**
720 * \brief RSA public key engine "i15".
721 *
722 * \see br_rsa_public
723 *
724 * \param x operand to exponentiate.
725 * \param xlen length of the operand (in bytes).
726 * \param pk RSA public key.
727 * \return 1 on success, 0 on error.
728 */
729 uint32_t br_rsa_i15_public(unsigned char *x, size_t xlen,
730 const br_rsa_public_key *pk);
731
732 /**
733 * \brief RSA signature verification engine "i15".
734 *
735 * \see br_rsa_pkcs1_vrfy
736 *
737 * \param x signature buffer.
738 * \param xlen signature length (in bytes).
739 * \param hash_oid encoded hash algorithm OID (or `NULL`).
740 * \param hash_len expected hash value length (in bytes).
741 * \param pk RSA public key.
742 * \param hash_out output buffer for the hash value.
743 * \return 1 on success, 0 on error.
744 */
745 uint32_t br_rsa_i15_pkcs1_vrfy(const unsigned char *x, size_t xlen,
746 const unsigned char *hash_oid, size_t hash_len,
747 const br_rsa_public_key *pk, unsigned char *hash_out);
748
749 /**
750 * \brief RSA private key engine "i15".
751 *
752 * \see br_rsa_private
753 *
754 * \param x operand to exponentiate.
755 * \param sk RSA private key.
756 * \return 1 on success, 0 on error.
757 */
758 uint32_t br_rsa_i15_private(unsigned char *x,
759 const br_rsa_private_key *sk);
760
761 /**
762 * \brief RSA signature generation engine "i15".
763 *
764 * \see br_rsa_pkcs1_sign
765 *
766 * \param hash_oid encoded hash algorithm OID (or `NULL`).
767 * \param hash hash value.
768 * \param hash_len hash value length (in bytes).
769 * \param sk RSA private key.
770 * \param x output buffer for the hash value.
771 * \return 1 on success, 0 on error.
772 */
773 uint32_t br_rsa_i15_pkcs1_sign(const unsigned char *hash_oid,
774 const unsigned char *hash, size_t hash_len,
775 const br_rsa_private_key *sk, unsigned char *x);
776
777 /**
778 * \brief Get "default" RSA implementation (public-key operations).
779 *
780 * This returns the preferred implementation of RSA (public-key operations)
781 * on the current system.
782 *
783 * \return the default implementation.
784 */
785 br_rsa_public br_rsa_public_get_default(void);
786
787 /**
788 * \brief Get "default" RSA implementation (private-key operations).
789 *
790 * This returns the preferred implementation of RSA (private-key operations)
791 * on the current system.
792 *
793 * \return the default implementation.
794 */
795 br_rsa_private br_rsa_private_get_default(void);
796
797 /**
798 * \brief Get "default" RSA implementation (PKCS#1 signature verification).
799 *
800 * This returns the preferred implementation of RSA (signature verification)
801 * on the current system.
802 *
803 * \return the default implementation.
804 */
805 br_rsa_pkcs1_vrfy br_rsa_pkcs1_vrfy_get_default(void);
806
807 /**
808 * \brief Get "default" RSA implementation (PKCS#1 signature generation).
809 *
810 * This returns the preferred implementation of RSA (signature generation)
811 * on the current system.
812 *
813 * \return the default implementation.
814 */
815 br_rsa_pkcs1_sign br_rsa_pkcs1_sign_get_default(void);
816
817 /**
818 * \brief Get "default" RSA implementation (OAEP encryption).
819 *
820 * This returns the preferred implementation of RSA (OAEP encryption)
821 * on the current system.
822 *
823 * \return the default implementation.
824 */
825 br_rsa_oaep_encrypt br_rsa_oaep_encrypt_get_default(void);
826
827 /**
828 * \brief Get "default" RSA implementation (OAEP decryption).
829 *
830 * This returns the preferred implementation of RSA (OAEP decryption)
831 * on the current system.
832 *
833 * \return the default implementation.
834 */
835 br_rsa_oaep_decrypt br_rsa_oaep_decrypt_get_default(void);
836
837 /**
838 * \brief RSA decryption helper, for SSL/TLS.
839 *
840 * This function performs the RSA decryption for a RSA-based key exchange
841 * in a SSL/TLS server. The provided RSA engine is used. The `data`
842 * parameter points to the value to decrypt, of length `len` bytes. On
843 * success, the 48-byte pre-master secret is copied into `data`, starting
844 * at the first byte of that buffer; on error, the contents of `data`
845 * become indeterminate.
846 *
847 * This function first checks that the provided value length (`len`) is
848 * not lower than 59 bytes, and matches the RSA modulus length; if neither
849 * of this property is met, then this function returns 0 and the buffer
850 * is unmodified.
851 *
852 * Otherwise, decryption and then padding verification are performed, both
853 * in constant-time. A decryption error, or a bad padding, or an
854 * incorrect decrypted value length are reported with a returned value of
855 * 0; on success, 1 is returned. The caller (SSL server engine) is supposed
856 * to proceed with a random pre-master secret in case of error.
857 *
858 * \param core RSA private key engine.
859 * \param sk RSA private key.
860 * \param data input/output buffer.
861 * \param len length (in bytes) of the data to decrypt.
862 * \return 1 on success, 0 on error.
863 */
864 uint32_t br_rsa_ssl_decrypt(br_rsa_private core, const br_rsa_private_key *sk,
865 unsigned char *data, size_t len);
866
867 /**
868 * \brief RSA encryption (OAEP) with the "i15" engine.
869 *
870 * \see br_rsa_oaep_encrypt
871 *
872 * \param rnd source of random bytes.
873 * \param dig hash function to use with MGF1.
874 * \param label label value (may be `NULL` if `label_len` is zero).
875 * \param label_len label length, in bytes.
876 * \param pk RSA public key.
877 * \param dst destination buffer.
878 * \param dst_max_len destination buffer length (maximum encrypted data size).
879 * \param src message to encrypt.
880 * \param src_len source message length (in bytes).
881 * \return encrypted message length (in bytes), or 0 on error.
882 */
883 size_t br_rsa_i15_oaep_encrypt(
884 const br_prng_class **rnd, const br_hash_class *dig,
885 const void *label, size_t label_len,
886 const br_rsa_public_key *pk,
887 void *dst, size_t dst_max_len,
888 const void *src, size_t src_len);
889
890 /**
891 * \brief RSA decryption (OAEP) with the "i15" engine.
892 *
893 * \see br_rsa_oaep_decrypt
894 *
895 * \param dig hash function to use with MGF1.
896 * \param label label value (may be `NULL` if `label_len` is zero).
897 * \param label_len label length, in bytes.
898 * \param sk RSA private key.
899 * \param data input/output buffer.
900 * \param len encrypted/decrypted message length.
901 * \return 1 on success, 0 on error.
902 */
903 uint32_t br_rsa_i15_oaep_decrypt(
904 const br_hash_class *dig, const void *label, size_t label_len,
905 const br_rsa_private_key *sk, void *data, size_t *len);
906
907 /**
908 * \brief RSA encryption (OAEP) with the "i31" engine.
909 *
910 * \see br_rsa_oaep_encrypt
911 *
912 * \param rnd source of random bytes.
913 * \param dig hash function to use with MGF1.
914 * \param label label value (may be `NULL` if `label_len` is zero).
915 * \param label_len label length, in bytes.
916 * \param pk RSA public key.
917 * \param dst destination buffer.
918 * \param dst_max_len destination buffer length (maximum encrypted data size).
919 * \param src message to encrypt.
920 * \param src_len source message length (in bytes).
921 * \return encrypted message length (in bytes), or 0 on error.
922 */
923 size_t br_rsa_i31_oaep_encrypt(
924 const br_prng_class **rnd, const br_hash_class *dig,
925 const void *label, size_t label_len,
926 const br_rsa_public_key *pk,
927 void *dst, size_t dst_max_len,
928 const void *src, size_t src_len);
929
930 /**
931 * \brief RSA decryption (OAEP) with the "i31" engine.
932 *
933 * \see br_rsa_oaep_decrypt
934 *
935 * \param dig hash function to use with MGF1.
936 * \param label label value (may be `NULL` if `label_len` is zero).
937 * \param label_len label length, in bytes.
938 * \param sk RSA private key.
939 * \param data input/output buffer.
940 * \param len encrypted/decrypted message length.
941 * \return 1 on success, 0 on error.
942 */
943 uint32_t br_rsa_i31_oaep_decrypt(
944 const br_hash_class *dig, const void *label, size_t label_len,
945 const br_rsa_private_key *sk, void *data, size_t *len);
946
947 /**
948 * \brief RSA encryption (OAEP) with the "i32" engine.
949 *
950 * \see br_rsa_oaep_encrypt
951 *
952 * \param rnd source of random bytes.
953 * \param dig hash function to use with MGF1.
954 * \param label label value (may be `NULL` if `label_len` is zero).
955 * \param label_len label length, in bytes.
956 * \param pk RSA public key.
957 * \param dst destination buffer.
958 * \param dst_max_len destination buffer length (maximum encrypted data size).
959 * \param src message to encrypt.
960 * \param src_len source message length (in bytes).
961 * \return encrypted message length (in bytes), or 0 on error.
962 */
963 size_t br_rsa_i32_oaep_encrypt(
964 const br_prng_class **rnd, const br_hash_class *dig,
965 const void *label, size_t label_len,
966 const br_rsa_public_key *pk,
967 void *dst, size_t dst_max_len,
968 const void *src, size_t src_len);
969
970 /**
971 * \brief RSA decryption (OAEP) with the "i32" engine.
972 *
973 * \see br_rsa_oaep_decrypt
974 *
975 * \param dig hash function to use with MGF1.
976 * \param label label value (may be `NULL` if `label_len` is zero).
977 * \param label_len label length, in bytes.
978 * \param sk RSA private key.
979 * \param data input/output buffer.
980 * \param len encrypted/decrypted message length.
981 * \return 1 on success, 0 on error.
982 */
983 uint32_t br_rsa_i32_oaep_decrypt(
984 const br_hash_class *dig, const void *label, size_t label_len,
985 const br_rsa_private_key *sk, void *data, size_t *len);
986
987 /**
988 * \brief RSA encryption (OAEP) with the "i62" engine.
989 *
990 * This function is defined only on architecture that offer a 64x64->128
991 * opcode. Use `br_rsa_i62_oaep_encrypt_get()` to dynamically obtain a pointer
992 * to that function.
993 *
994 * \see br_rsa_oaep_encrypt
995 *
996 * \param rnd source of random bytes.
997 * \param dig hash function to use with MGF1.
998 * \param label label value (may be `NULL` if `label_len` is zero).
999 * \param label_len label length, in bytes.
1000 * \param pk RSA public key.
1001 * \param dst destination buffer.
1002 * \param dst_max_len destination buffer length (maximum encrypted data size).
1003 * \param src message to encrypt.
1004 * \param src_len source message length (in bytes).
1005 * \return encrypted message length (in bytes), or 0 on error.
1006 */
1007 size_t br_rsa_i62_oaep_encrypt(
1008 const br_prng_class **rnd, const br_hash_class *dig,
1009 const void *label, size_t label_len,
1010 const br_rsa_public_key *pk,
1011 void *dst, size_t dst_max_len,
1012 const void *src, size_t src_len);
1013
1014 /**
1015 * \brief RSA decryption (OAEP) with the "i62" engine.
1016 *
1017 * This function is defined only on architecture that offer a 64x64->128
1018 * opcode. Use `br_rsa_i62_oaep_decrypt_get()` to dynamically obtain a pointer
1019 * to that function.
1020 *
1021 * \see br_rsa_oaep_decrypt
1022 *
1023 * \param dig hash function to use with MGF1.
1024 * \param label label value (may be `NULL` if `label_len` is zero).
1025 * \param label_len label length, in bytes.
1026 * \param sk RSA private key.
1027 * \param data input/output buffer.
1028 * \param len encrypted/decrypted message length.
1029 * \return 1 on success, 0 on error.
1030 */
1031 uint32_t br_rsa_i62_oaep_decrypt(
1032 const br_hash_class *dig, const void *label, size_t label_len,
1033 const br_rsa_private_key *sk, void *data, size_t *len);
1034
1035 #ifdef __cplusplus
1036 }
1037 #endif
1038
1039 #endif