2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
25 #ifndef BR_BEARSSL_RSA_H__
26 #define BR_BEARSSL_RSA_H__
35 /** \file bearssl_rsa.h
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.
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.
55 * RSA public keys consist in two integers:
57 * - the modulus (`n`);
58 * - the public exponent (`e`).
60 * RSA private keys, as defined in
61 * [PKCS#1](https://tools.ietf.org/html/rfc3447), contain eight integers:
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`).
72 * However, the implementations defined in BearSSL use only five of
73 * these integers: `p`, `q`, `dp`, `dq` and `iq`.
75 * ## Security Features and Limitations
77 * The implementations contained in BearSSL have the following limitations
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.
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.
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.
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`).
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.
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).
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.
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.
128 * Three RSA implementations are included:
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.
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).
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.
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
153 * \brief RSA public key.
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.
160 /** \brief Modulus. */
162 /** \brief Modulus length (in bytes). */
164 /** \brief Public exponent. */
166 /** \brief Public exponent length (in bytes). */
171 * \brief RSA private key.
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.
180 /** \brief Modulus bit length (in bits, exact value). */
182 /** \brief First prime factor. */
184 /** \brief First prime factor length (in bytes). */
186 /** \brief Second prime factor. */
188 /** \brief Second prime factor length (in bytes). */
190 /** \brief First reduced private exponent. */
192 /** \brief First reduced private exponent length (in bytes). */
194 /** \brief Second reduced private exponent. */
196 /** \brief Second reduced private exponent length (in bytes). */
198 /** \brief CRT coefficient. */
200 /** \brief CRT coefficient length (in bytes). */
202 } br_rsa_private_key
;
205 * \brief Type for a RSA public key engine.
207 * The public key engine performs the modular exponentiation of the
208 * provided value with the public exponent. The value is modified in
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.
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
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.
228 typedef uint32_t (*br_rsa_public
)(unsigned char *x
, size_t xlen
,
229 const br_rsa_public_key
*pk
);
232 * \brief Type for a RSA signature verification engine (PKCS#1 v1.5).
236 * - The signature itself. The provided array is NOT modified.
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).
245 * - The hash output length, in bytes.
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.
254 * - Hash length MUST be no more than 64 bytes.
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).
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
264 * Returned value is 1 on success, 0 on error.
266 * Implementations of this type need not be constant-time.
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.
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
);
281 * \brief Type for a RSA private key engine.
283 * The `x[]` buffer is modified in place, and its length is inferred from
284 * the modulus length (`x[]` is assumed to have a length of
285 * `(sk->n_bitlen+7)/8` bytes).
287 * Returned value is 1 on success, 0 on error.
289 * \param x operand to exponentiate.
290 * \param sk RSA private key.
291 * \return 1 on success, 0 on error.
293 typedef uint32_t (*br_rsa_private
)(unsigned char *x
,
294 const br_rsa_private_key
*sk
);
297 * \brief Type for a RSA signature generation engine (PKCS#1 v1.5).
301 * - The encoded OID for the hash function. The provided array must begin
302 * with a single byte that contains the length of the OID value (in
303 * bytes), followed by exactly that many bytes. This parameter may
304 * also be `NULL`, in which case the raw hash value should be used
305 * with the PKCS#1 v1.5 "type 1" padding (as used in SSL/TLS up
306 * to TLS-1.1, with a 36-byte hash value).
308 * - The hash value computes over the data to sign (its length is
309 * expressed in bytes).
311 * - The RSA private key.
313 * - The output buffer, that receives the signature.
315 * Returned value is 1 on success, 0 on error. Error conditions include
316 * a too small modulus for the provided hash OID and value, or some
317 * invalid key parameters. The signature length is exactly
318 * `(sk->n_bitlen+7)/8` bytes.
320 * This function is expected to be constant-time with regards to the
321 * private key bytes (lengths of the modulus and the individual factors
322 * may leak, though) and to the hashed data.
324 * \param hash_oid encoded hash algorithm OID (or `NULL`).
325 * \param hash hash value.
326 * \param hash_len hash value length (in bytes).
327 * \param sk RSA private key.
328 * \param x output buffer for the signature value.
329 * \return 1 on success, 0 on error.
331 typedef uint32_t (*br_rsa_pkcs1_sign
)(const unsigned char *hash_oid
,
332 const unsigned char *hash
, size_t hash_len
,
333 const br_rsa_private_key
*sk
, unsigned char *x
);
336 * RSA "i32" engine. Integers are internally represented as arrays of
337 * 32-bit integers, and the core multiplication primitive is the
338 * 32x32->64 multiplication.
342 * \brief RSA public key engine "i32".
346 * \param x operand to exponentiate.
347 * \param xlen length of the operand (in bytes).
348 * \param pk RSA public key.
349 * \return 1 on success, 0 on error.
351 uint32_t br_rsa_i32_public(unsigned char *x
, size_t xlen
,
352 const br_rsa_public_key
*pk
);
355 * \brief RSA signature verification engine "i32".
357 * \see br_rsa_pkcs1_vrfy
359 * \param x signature buffer.
360 * \param xlen signature length (in bytes).
361 * \param hash_oid encoded hash algorithm OID (or `NULL`).
362 * \param hash_len expected hash value length (in bytes).
363 * \param pk RSA public key.
364 * \param hash_out output buffer for the hash value.
365 * \return 1 on success, 0 on error.
367 uint32_t br_rsa_i32_pkcs1_vrfy(const unsigned char *x
, size_t xlen
,
368 const unsigned char *hash_oid
, size_t hash_len
,
369 const br_rsa_public_key
*pk
, unsigned char *hash_out
);
372 * \brief RSA private key engine "i32".
374 * \see br_rsa_private
376 * \param x operand to exponentiate.
377 * \param sk RSA private key.
378 * \return 1 on success, 0 on error.
380 uint32_t br_rsa_i32_private(unsigned char *x
,
381 const br_rsa_private_key
*sk
);
384 * \brief RSA signature generation engine "i32".
386 * \see br_rsa_pkcs1_sign
388 * \param hash_oid encoded hash algorithm OID (or `NULL`).
389 * \param hash hash value.
390 * \param hash_len hash value length (in bytes).
391 * \param sk RSA private key.
392 * \param x output buffer for the hash value.
393 * \return 1 on success, 0 on error.
395 uint32_t br_rsa_i32_pkcs1_sign(const unsigned char *hash_oid
,
396 const unsigned char *hash
, size_t hash_len
,
397 const br_rsa_private_key
*sk
, unsigned char *x
);
400 * RSA "i31" engine. Similar to i32, but only 31 bits are used per 32-bit
401 * word. This uses slightly more stack space (about 4% more) and code
402 * space, but it quite faster.
406 * \brief RSA public key engine "i31".
410 * \param x operand to exponentiate.
411 * \param xlen length of the operand (in bytes).
412 * \param pk RSA public key.
413 * \return 1 on success, 0 on error.
415 uint32_t br_rsa_i31_public(unsigned char *x
, size_t xlen
,
416 const br_rsa_public_key
*pk
);
419 * \brief RSA signature verification engine "i31".
421 * \see br_rsa_pkcs1_vrfy
423 * \param x signature buffer.
424 * \param xlen signature length (in bytes).
425 * \param hash_oid encoded hash algorithm OID (or `NULL`).
426 * \param hash_len expected hash value length (in bytes).
427 * \param pk RSA public key.
428 * \param hash_out output buffer for the hash value.
429 * \return 1 on success, 0 on error.
431 uint32_t br_rsa_i31_pkcs1_vrfy(const unsigned char *x
, size_t xlen
,
432 const unsigned char *hash_oid
, size_t hash_len
,
433 const br_rsa_public_key
*pk
, unsigned char *hash_out
);
436 * \brief RSA private key engine "i31".
438 * \see br_rsa_private
440 * \param x operand to exponentiate.
441 * \param sk RSA private key.
442 * \return 1 on success, 0 on error.
444 uint32_t br_rsa_i31_private(unsigned char *x
,
445 const br_rsa_private_key
*sk
);
448 * \brief RSA signature generation engine "i31".
450 * \see br_rsa_pkcs1_sign
452 * \param hash_oid encoded hash algorithm OID (or `NULL`).
453 * \param hash hash value.
454 * \param hash_len hash value length (in bytes).
455 * \param sk RSA private key.
456 * \param x output buffer for the hash value.
457 * \return 1 on success, 0 on error.
459 uint32_t br_rsa_i31_pkcs1_sign(const unsigned char *hash_oid
,
460 const unsigned char *hash
, size_t hash_len
,
461 const br_rsa_private_key
*sk
, unsigned char *x
);
464 * RSA "i62" engine. Similar to i31, but internal multiplication use
465 * 64x64->128 multiplications. This is available only on architecture
466 * that offer such an opcode.
470 * \brief RSA public key engine "i62".
472 * This function is defined only on architecture that offer a 64x64->128
473 * opcode. Use `br_rsa_i62_public_get()` to dynamically obtain a pointer
478 * \param x operand to exponentiate.
479 * \param xlen length of the operand (in bytes).
480 * \param pk RSA public key.
481 * \return 1 on success, 0 on error.
483 uint32_t br_rsa_i62_public(unsigned char *x
, size_t xlen
,
484 const br_rsa_public_key
*pk
);
487 * \brief RSA signature verification engine "i62".
489 * This function is defined only on architecture that offer a 64x64->128
490 * opcode. Use `br_rsa_i62_pkcs1_vrfy_get()` to dynamically obtain a pointer
493 * \see br_rsa_pkcs1_vrfy
495 * \param x signature buffer.
496 * \param xlen signature length (in bytes).
497 * \param hash_oid encoded hash algorithm OID (or `NULL`).
498 * \param hash_len expected hash value length (in bytes).
499 * \param pk RSA public key.
500 * \param hash_out output buffer for the hash value.
501 * \return 1 on success, 0 on error.
503 uint32_t br_rsa_i62_pkcs1_vrfy(const unsigned char *x
, size_t xlen
,
504 const unsigned char *hash_oid
, size_t hash_len
,
505 const br_rsa_public_key
*pk
, unsigned char *hash_out
);
508 * \brief RSA private key engine "i62".
510 * This function is defined only on architecture that offer a 64x64->128
511 * opcode. Use `br_rsa_i62_private_get()` to dynamically obtain a pointer
514 * \see br_rsa_private
516 * \param x operand to exponentiate.
517 * \param sk RSA private key.
518 * \return 1 on success, 0 on error.
520 uint32_t br_rsa_i62_private(unsigned char *x
,
521 const br_rsa_private_key
*sk
);
524 * \brief RSA signature generation engine "i62".
526 * This function is defined only on architecture that offer a 64x64->128
527 * opcode. Use `br_rsa_i62_pkcs1_sign_get()` to dynamically obtain a pointer
530 * \see br_rsa_pkcs1_sign
532 * \param hash_oid encoded hash algorithm OID (or `NULL`).
533 * \param hash hash value.
534 * \param hash_len hash value length (in bytes).
535 * \param sk RSA private key.
536 * \param x output buffer for the hash value.
537 * \return 1 on success, 0 on error.
539 uint32_t br_rsa_i62_pkcs1_sign(const unsigned char *hash_oid
,
540 const unsigned char *hash
, size_t hash_len
,
541 const br_rsa_private_key
*sk
, unsigned char *x
);
544 * \brief Get the RSA "i62" implementation (public key operations),
547 * \return the implementation, or 0.
549 br_rsa_public
br_rsa_i62_public_get(void);
552 * \brief Get the RSA "i62" implementation (PKCS#1 signature verification),
555 * \return the implementation, or 0.
557 br_rsa_pkcs1_vrfy
br_rsa_i62_pkcs1_vrfy_get(void);
560 * \brief Get the RSA "i62" implementation (private key operations),
563 * \return the implementation, or 0.
565 br_rsa_private
br_rsa_i62_private_get(void);
568 * \brief Get the RSA "i62" implementation (PKCS#1 signature generation),
571 * \return the implementation, or 0.
573 br_rsa_pkcs1_sign
br_rsa_i62_pkcs1_sign_get(void);
576 * RSA "i15" engine. Integers are represented as 15-bit integers, so
577 * the code uses only 32-bit multiplication (no 64-bit result), which
578 * is vastly faster (and constant-time) on the ARM Cortex M0/M0+.
582 * \brief RSA public key engine "i15".
586 * \param x operand to exponentiate.
587 * \param xlen length of the operand (in bytes).
588 * \param pk RSA public key.
589 * \return 1 on success, 0 on error.
591 uint32_t br_rsa_i15_public(unsigned char *x
, size_t xlen
,
592 const br_rsa_public_key
*pk
);
595 * \brief RSA signature verification engine "i15".
597 * \see br_rsa_pkcs1_vrfy
599 * \param x signature buffer.
600 * \param xlen signature length (in bytes).
601 * \param hash_oid encoded hash algorithm OID (or `NULL`).
602 * \param hash_len expected hash value length (in bytes).
603 * \param pk RSA public key.
604 * \param hash_out output buffer for the hash value.
605 * \return 1 on success, 0 on error.
607 uint32_t br_rsa_i15_pkcs1_vrfy(const unsigned char *x
, size_t xlen
,
608 const unsigned char *hash_oid
, size_t hash_len
,
609 const br_rsa_public_key
*pk
, unsigned char *hash_out
);
612 * \brief RSA private key engine "i15".
614 * \see br_rsa_private
616 * \param x operand to exponentiate.
617 * \param sk RSA private key.
618 * \return 1 on success, 0 on error.
620 uint32_t br_rsa_i15_private(unsigned char *x
,
621 const br_rsa_private_key
*sk
);
624 * \brief RSA signature generation engine "i15".
626 * \see br_rsa_pkcs1_sign
628 * \param hash_oid encoded hash algorithm OID (or `NULL`).
629 * \param hash hash value.
630 * \param hash_len hash value length (in bytes).
631 * \param sk RSA private key.
632 * \param x output buffer for the hash value.
633 * \return 1 on success, 0 on error.
635 uint32_t br_rsa_i15_pkcs1_sign(const unsigned char *hash_oid
,
636 const unsigned char *hash
, size_t hash_len
,
637 const br_rsa_private_key
*sk
, unsigned char *x
);
640 * \brief Get "default" RSA implementation (public-key operations).
642 * This returns the preferred implementation of RSA (public-key operations)
643 * on the current system.
645 * \return the default implementation.
647 br_rsa_public
br_rsa_public_get_default(void);
650 * \brief Get "default" RSA implementation (private-key operations).
652 * This returns the preferred implementation of RSA (private-key operations)
653 * on the current system.
655 * \return the default implementation.
657 br_rsa_private
br_rsa_private_get_default(void);
660 * \brief Get "default" RSA implementation (PKCS#1 signature verification).
662 * This returns the preferred implementation of RSA (signature verification)
663 * on the current system.
665 * \return the default implementation.
667 br_rsa_pkcs1_vrfy
br_rsa_pkcs1_vrfy_get_default(void);
670 * \brief Get "default" RSA implementation (PKCS#1 signature generation).
672 * This returns the preferred implementation of RSA (signature generation)
673 * on the current system.
675 * \return the default implementation.
677 br_rsa_pkcs1_sign
br_rsa_pkcs1_sign_get_default(void);
680 * \brief RSA decryption helper, for SSL/TLS.
682 * This function performs the RSA decryption for a RSA-based key exchange
683 * in a SSL/TLS server. The provided RSA engine is used. The `data`
684 * parameter points to the value to decrypt, of length `len` bytes. On
685 * success, the 48-byte pre-master secret is copied into `data`, starting
686 * at the first byte of that buffer; on error, the contents of `data`
687 * become indeterminate.
689 * This function first checks that the provided value length (`len`) is
690 * not lower than 59 bytes, and matches the RSA modulus length; if neither
691 * of this property is met, then this function returns 0 and the buffer
694 * Otherwise, decryption and then padding verification are performed, both
695 * in constant-time. A decryption error, or a bad padding, or an
696 * incorrect decrypted value length are reported with a returned value of
697 * 0; on success, 1 is returned. The caller (SSL server engine) is supposed
698 * to proceed with a random pre-master secret in case of error.
700 * \param core RSA private key engine.
701 * \param sk RSA private key.
702 * \param data input/output buffer.
703 * \param len length (in bytes) of the data to decrypt.
704 * \return 1 on success, 0 on error.
706 uint32_t br_rsa_ssl_decrypt(br_rsa_private core
, const br_rsa_private_key
*sk
,
707 unsigned char *data
, size_t len
);