8c79157ab7d682ba015e048d920970a4d624aa49
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__
31 /** \file bearssl_rsa.h
35 * This file documents the RSA implementations provided with BearSSL.
36 * Note that the SSL engine accesses these implementations through a
37 * configurable API, so it is possible to, for instance, run a SSL
38 * server which uses a RSA engine which is not based on this code.
42 * RSA public and private keys consist in lists of big integers. All
43 * such integers are represented with big-endian unsigned notation:
44 * first byte is the most significant, and the value is positive (so
45 * there is no dedicated "sign bit"). Public and private key structures
46 * thus contain, for each such integer, a pointer to the first value byte
47 * (`unsigned char *`), and a length (`size_t`) which is the number of
48 * relevant bytes. As a general rule, minimal-length encoding is not
49 * enforced: values may have extra leading bytes of value 0.
51 * RSA public keys consist in two integers:
53 * - the modulus (`n`);
54 * - the public exponent (`e`).
56 * RSA private keys, as defined in
57 * [PKCS#1](https://tools.ietf.org/html/rfc3447), contain eight integers:
59 * - the modulus (`n`);
60 * - the public exponent (`e`);
61 * - the private exponent (`d`);
62 * - the first prime factor (`p`);
63 * - the second prime factor (`q`);
64 * - the first reduced exponent (`dp`, which is `d` modulo `p-1`);
65 * - the second reduced exponent (`dq`, which is `d` modulo `q-1`);
66 * - the CRT coefficient (`iq`, the inverse of `q` modulo `p`).
68 * However, the implementations defined in BearSSL use only five of
69 * these integers: `p`, `q`, `dp`, `dq` and `iq`.
71 * ## Security Features and Limitations
73 * The implementations contained in BearSSL have the following limitations
76 * - They are constant-time. This means that the execution time and
77 * memory access pattern may depend on the _lengths_ of the private
78 * key components, but not on their value, nor on the value of
79 * the operand. Note that this property is not achieved through
80 * random masking, but "true" constant-time code.
82 * - They support only private keys with two prime factors. RSA private
83 * key with three or more prime factors are nominally supported, but
84 * rarely used; they may offer faster operations, at the expense of
85 * more code and potentially a reduction in security if there are
86 * "too many" prime factors.
88 * - The public exponent may have arbitrary length. Of course, it is
89 * a good idea to keep public exponents small, so that public key
90 * operations are fast; but, contrary to some widely deployed
91 * implementations, BearSSL has no problem with public exponent
92 * longer than 32 bits.
94 * - The two prime factors of the modulus need not have the same length
95 * (but severely imbalanced factor lengths might reduce security).
96 * Similarly, there is no requirement that the first factor (`p`)
97 * be greater than the second factor (`q`).
99 * - Prime factors and modulus must be smaller than a compile-time limit.
100 * This is made necessary by the use of fixed-size stack buffers, and
101 * the limit has been adjusted to keep stack usage under 2 kB for the
102 * RSA operations. Currently, the maximum modulus size is 4096 bits,
103 * and the maximum prime factor size is 2080 bits.
105 * - The RSA functions themselves do not enforce lower size limits,
106 * except that which is absolutely necessary for the operation to
107 * mathematically make sense (e.g. a PKCS#1 v1.5 signature with
108 * SHA-1 requires a modulus of at least 361 bits). It is up to users
109 * of this code to enforce size limitations when appropriate (e.g.
110 * the X.509 validation engine, by default, rejects RSA keys of
111 * less than 1017 bits).
113 * - Within the size constraints expressed above, arbitrary bit lengths
114 * are supported. There is no requirement that prime factors or
115 * modulus have a size multiple of 8 or 16.
117 * - When verifying PKCS#1 v1.5 signatures, both variants of the hash
118 * function identifying header (with and without the ASN.1 NULL) are
119 * supported. When producing such signatures, the variant with the
120 * ASN.1 NULL is used.
124 * Three RSA implementations are included:
126 * - The **i32** implementation internally represents big integers
127 * as arrays of 32-bit integers. It is perfunctory and portable,
128 * but not very efficient.
130 * - The **i31** implementation uses 32-bit integers, each containing
131 * 31 bits worth of integer data. The i31 implementation is somewhat
132 * faster than the i32 implementation (the reduced integer size makes
133 * carry propagation easier) for a similar code footprint, but uses
134 * very slightly larger stack buffers (about 4% bigger).
136 * - The **i62** implementation is similar to the i31 implementation,
137 * except that it internally leverages the 64x64->128 multiplication
138 * opcode. This implementation is available only on architectures
139 * where such an opcode exists. It is much faster than i31.
141 * - The **i15** implementation uses 16-bit integers, each containing
142 * 15 bits worth of integer data. Multiplication results fit on
143 * 32 bits, so this won't use the "widening" multiplication routine
144 * on ARM Cortex M0/M0+, for much better performance and constant-time
149 * \brief RSA public key.
151 * The structure references the modulus and the public exponent. Both
152 * integers use unsigned big-endian representation; extra leading bytes
153 * of value 0 are allowed.
156 /** \brief Modulus. */
158 /** \brief Modulus length (in bytes). */
160 /** \brief Public exponent. */
162 /** \brief Public exponent length (in bytes). */
167 * \brief RSA private key.
169 * The structure references the primvate factors, reduced private
170 * exponents, and CRT coefficient. It also contains the bit length of
171 * the modulus. The big integers use unsigned big-endian representation;
172 * extra leading bytes of value 0 are allowed. However, the modulus bit
173 * length (`n_bitlen`) MUST be exact.
176 /** \brief Modulus bit length (in bits, exact value). */
178 /** \brief First prime factor. */
180 /** \brief First prime factor length (in bytes). */
182 /** \brief Second prime factor. */
184 /** \brief Second prime factor length (in bytes). */
186 /** \brief First reduced private exponent. */
188 /** \brief First reduced private exponent length (in bytes). */
190 /** \brief Second reduced private exponent. */
192 /** \brief Second reduced private exponent length (in bytes). */
194 /** \brief CRT coefficient. */
196 /** \brief CRT coefficient length (in bytes). */
198 } br_rsa_private_key
;
201 * \brief Type for a RSA public key engine.
203 * The public key engine performs the modular exponentiation of the
204 * provided value with the public exponent. The value is modified in
207 * The value length (`xlen`) is verified to have _exactly_ the same
208 * length as the modulus (actual modulus length, without extra leading
209 * zeros in the modulus representation in memory). If the length does
210 * not match, then this function returns 0 and `x[]` is unmodified.
212 * It `xlen` is correct, then `x[]` is modified. Returned value is 1
213 * on success, 0 on error. Error conditions include an oversized `x[]`
214 * (the array has the same length as the modulus, but the numerical value
215 * is not lower than the modulus) and an invalid modulus (e.g. an even
216 * integer). If an error is reported, then the new contents of `x[]` are
219 * \param x operand to exponentiate.
220 * \param xlen length of the operand (in bytes).
221 * \param pk RSA public key.
222 * \return 1 on success, 0 on error.
224 typedef uint32_t (*br_rsa_public
)(unsigned char *x
, size_t xlen
,
225 const br_rsa_public_key
*pk
);
228 * \brief Type for a RSA signature verification engine (PKCS#1 v1.5).
232 * - The signature itself. The provided array is NOT modified.
234 * - The encoded OID for the hash function. The provided array must begin
235 * with a single byte that contains the length of the OID value (in
236 * bytes), followed by exactly that many bytes. This parameter may
237 * also be `NULL`, in which case the raw hash value should be used
238 * with the PKCS#1 v1.5 "type 1" padding (as used in SSL/TLS up
239 * to TLS-1.1, with a 36-byte hash value).
241 * - The hash output length, in bytes.
245 * - An output buffer for the hash value. The caller must still compare
246 * it with the hash of the data over which the signature is computed.
250 * - Hash length MUST be no more than 64 bytes.
252 * - OID value length MUST be no more than 32 bytes (i.e. `hash_oid[0]`
253 * must have a value in the 0..32 range, inclusive).
255 * This function verifies that the signature length (`xlen`) matches the
256 * modulus length (this function returns 0 on mismatch). If the modulus
257 * size exceeds the maximum supported RSA size, then the function also
260 * Returned value is 1 on success, 0 on error.
262 * Implementations of this type need not be constant-time.
264 * \param x signature buffer.
265 * \param xlen signature length (in bytes).
266 * \param hash_oid encoded hash algorithm OID (or `NULL`).
267 * \param hash_len expected hash value length (in bytes).
268 * \param pk RSA public key.
269 * \param hash_out output buffer for the hash value.
270 * \return 1 on success, 0 on error.
272 typedef uint32_t (*br_rsa_pkcs1_vrfy
)(const unsigned char *x
, size_t xlen
,
273 const unsigned char *hash_oid
, size_t hash_len
,
274 const br_rsa_public_key
*pk
, unsigned char *hash_out
);
277 * \brief Type for a RSA private key engine.
279 * The `x[]` buffer is modified in place, and its length is inferred from
280 * the modulus length (`x[]` is assumed to have a length of
281 * `(sk->n_bitlen+7)/8` bytes).
283 * Returned value is 1 on success, 0 on error.
285 * \param x operand to exponentiate.
286 * \param sk RSA private key.
287 * \return 1 on success, 0 on error.
289 typedef uint32_t (*br_rsa_private
)(unsigned char *x
,
290 const br_rsa_private_key
*sk
);
293 * \brief Type for a RSA signature generation engine (PKCS#1 v1.5).
297 * - The encoded OID for the hash function. The provided array must begin
298 * with a single byte that contains the length of the OID value (in
299 * bytes), followed by exactly that many bytes. This parameter may
300 * also be `NULL`, in which case the raw hash value should be used
301 * with the PKCS#1 v1.5 "type 1" padding (as used in SSL/TLS up
302 * to TLS-1.1, with a 36-byte hash value).
304 * - The hash value computes over the data to sign (its length is
305 * expressed in bytes).
307 * - The RSA private key.
309 * - The output buffer, that receives the signature.
311 * Returned value is 1 on success, 0 on error. Error conditions include
312 * a too small modulus for the provided hash OID and value, or some
313 * invalid key parameters. The signature length is exactly
314 * `(sk->n_bitlen+7)/8` bytes.
316 * This function is expected to be constant-time with regards to the
317 * private key bytes (lengths of the modulus and the individual factors
318 * may leak, though) and to the hashed data.
320 * \param hash_oid encoded hash algorithm OID (or `NULL`).
321 * \param hash hash value.
322 * \param hash_len hash value length (in bytes).
323 * \param sk RSA private key.
324 * \param x output buffer for the signature value.
325 * \return 1 on success, 0 on error.
327 typedef uint32_t (*br_rsa_pkcs1_sign
)(const unsigned char *hash_oid
,
328 const unsigned char *hash
, size_t hash_len
,
329 const br_rsa_private_key
*sk
, unsigned char *x
);
332 * RSA "i32" engine. Integers are internally represented as arrays of
333 * 32-bit integers, and the core multiplication primitive is the
334 * 32x32->64 multiplication.
338 * \brief RSA public key engine "i32".
342 * \param x operand to exponentiate.
343 * \param xlen length of the operand (in bytes).
344 * \param pk RSA public key.
345 * \return 1 on success, 0 on error.
347 uint32_t br_rsa_i32_public(unsigned char *x
, size_t xlen
,
348 const br_rsa_public_key
*pk
);
351 * \brief RSA signature verification engine "i32".
353 * \see br_rsa_pkcs1_vrfy
355 * \param x signature buffer.
356 * \param xlen signature length (in bytes).
357 * \param hash_oid encoded hash algorithm OID (or `NULL`).
358 * \param hash_len expected hash value length (in bytes).
359 * \param pk RSA public key.
360 * \param hash_out output buffer for the hash value.
361 * \return 1 on success, 0 on error.
363 uint32_t br_rsa_i32_pkcs1_vrfy(const unsigned char *x
, size_t xlen
,
364 const unsigned char *hash_oid
, size_t hash_len
,
365 const br_rsa_public_key
*pk
, unsigned char *hash_out
);
368 * \brief RSA private key engine "i32".
370 * \see br_rsa_private
372 * \param x operand to exponentiate.
373 * \param sk RSA private key.
374 * \return 1 on success, 0 on error.
376 uint32_t br_rsa_i32_private(unsigned char *x
,
377 const br_rsa_private_key
*sk
);
380 * \brief RSA signature generation engine "i32".
382 * \see br_rsa_pkcs1_sign
384 * \param hash_oid encoded hash algorithm OID (or `NULL`).
385 * \param hash hash value.
386 * \param hash_len hash value length (in bytes).
387 * \param sk RSA private key.
388 * \param x output buffer for the hash value.
389 * \return 1 on success, 0 on error.
391 uint32_t br_rsa_i32_pkcs1_sign(const unsigned char *hash_oid
,
392 const unsigned char *hash
, size_t hash_len
,
393 const br_rsa_private_key
*sk
, unsigned char *x
);
396 * RSA "i31" engine. Similar to i32, but only 31 bits are used per 32-bit
397 * word. This uses slightly more stack space (about 4% more) and code
398 * space, but it quite faster.
402 * \brief RSA public key engine "i31".
406 * \param x operand to exponentiate.
407 * \param xlen length of the operand (in bytes).
408 * \param pk RSA public key.
409 * \return 1 on success, 0 on error.
411 uint32_t br_rsa_i31_public(unsigned char *x
, size_t xlen
,
412 const br_rsa_public_key
*pk
);
415 * \brief RSA signature verification engine "i31".
417 * \see br_rsa_pkcs1_vrfy
419 * \param x signature buffer.
420 * \param xlen signature length (in bytes).
421 * \param hash_oid encoded hash algorithm OID (or `NULL`).
422 * \param hash_len expected hash value length (in bytes).
423 * \param pk RSA public key.
424 * \param hash_out output buffer for the hash value.
425 * \return 1 on success, 0 on error.
427 uint32_t br_rsa_i31_pkcs1_vrfy(const unsigned char *x
, size_t xlen
,
428 const unsigned char *hash_oid
, size_t hash_len
,
429 const br_rsa_public_key
*pk
, unsigned char *hash_out
);
432 * \brief RSA private key engine "i31".
434 * \see br_rsa_private
436 * \param x operand to exponentiate.
437 * \param sk RSA private key.
438 * \return 1 on success, 0 on error.
440 uint32_t br_rsa_i31_private(unsigned char *x
,
441 const br_rsa_private_key
*sk
);
444 * \brief RSA signature generation engine "i31".
446 * \see br_rsa_pkcs1_sign
448 * \param hash_oid encoded hash algorithm OID (or `NULL`).
449 * \param hash hash value.
450 * \param hash_len hash value length (in bytes).
451 * \param sk RSA private key.
452 * \param x output buffer for the hash value.
453 * \return 1 on success, 0 on error.
455 uint32_t br_rsa_i31_pkcs1_sign(const unsigned char *hash_oid
,
456 const unsigned char *hash
, size_t hash_len
,
457 const br_rsa_private_key
*sk
, unsigned char *x
);
460 * RSA "i62" engine. Similar to i31, but internal multiplication use
461 * 64x64->128 multiplications. This is available only on architecture
462 * that offer such an opcode.
466 * \brief RSA public key engine "i62".
468 * This function is defined only on architecture that offer a 64x64->128
469 * opcode. Use `br_rsa_i62_public_get()` to dynamically obtain a pointer
474 * \param x operand to exponentiate.
475 * \param xlen length of the operand (in bytes).
476 * \param pk RSA public key.
477 * \return 1 on success, 0 on error.
479 uint32_t br_rsa_i62_public(unsigned char *x
, size_t xlen
,
480 const br_rsa_public_key
*pk
);
483 * \brief RSA signature verification engine "i62".
485 * This function is defined only on architecture that offer a 64x64->128
486 * opcode. Use `br_rsa_i62_pkcs1_vrfy_get()` to dynamically obtain a pointer
489 * \see br_rsa_pkcs1_vrfy
491 * \param x signature buffer.
492 * \param xlen signature length (in bytes).
493 * \param hash_oid encoded hash algorithm OID (or `NULL`).
494 * \param hash_len expected hash value length (in bytes).
495 * \param pk RSA public key.
496 * \param hash_out output buffer for the hash value.
497 * \return 1 on success, 0 on error.
499 uint32_t br_rsa_i62_pkcs1_vrfy(const unsigned char *x
, size_t xlen
,
500 const unsigned char *hash_oid
, size_t hash_len
,
501 const br_rsa_public_key
*pk
, unsigned char *hash_out
);
504 * \brief RSA private key engine "i62".
506 * This function is defined only on architecture that offer a 64x64->128
507 * opcode. Use `br_rsa_i62_private_get()` to dynamically obtain a pointer
510 * \see br_rsa_private
512 * \param x operand to exponentiate.
513 * \param sk RSA private key.
514 * \return 1 on success, 0 on error.
516 uint32_t br_rsa_i62_private(unsigned char *x
,
517 const br_rsa_private_key
*sk
);
520 * \brief RSA signature generation engine "i62".
522 * This function is defined only on architecture that offer a 64x64->128
523 * opcode. Use `br_rsa_i62_pkcs1_sign_get()` to dynamically obtain a pointer
526 * \see br_rsa_pkcs1_sign
528 * \param hash_oid encoded hash algorithm OID (or `NULL`).
529 * \param hash hash value.
530 * \param hash_len hash value length (in bytes).
531 * \param sk RSA private key.
532 * \param x output buffer for the hash value.
533 * \return 1 on success, 0 on error.
535 uint32_t br_rsa_i62_pkcs1_sign(const unsigned char *hash_oid
,
536 const unsigned char *hash
, size_t hash_len
,
537 const br_rsa_private_key
*sk
, unsigned char *x
);
540 * \brief Get the RSA "i62" implementation (public key operations),
543 * \return the implementation, or 0.
545 br_rsa_public
br_rsa_i62_public_get(void);
548 * \brief Get the RSA "i62" implementation (PKCS#1 signature verification),
551 * \return the implementation, or 0.
553 br_rsa_pkcs1_vrfy
br_rsa_i62_pkcs1_vrfy_get(void);
556 * \brief Get the RSA "i62" implementation (private key operations),
559 * \return the implementation, or 0.
561 br_rsa_private
br_rsa_i62_private_get(void);
564 * \brief Get the RSA "i62" implementation (PKCS#1 signature generation),
567 * \return the implementation, or 0.
569 br_rsa_pkcs1_sign
br_rsa_i62_pkcs1_sign_get(void);
572 * RSA "i15" engine. Integers are represented as 15-bit integers, so
573 * the code uses only 32-bit multiplication (no 64-bit result), which
574 * is vastly faster (and constant-time) on the ARM Cortex M0/M0+.
578 * \brief RSA public key engine "i15".
582 * \param x operand to exponentiate.
583 * \param xlen length of the operand (in bytes).
584 * \param pk RSA public key.
585 * \return 1 on success, 0 on error.
587 uint32_t br_rsa_i15_public(unsigned char *x
, size_t xlen
,
588 const br_rsa_public_key
*pk
);
591 * \brief RSA signature verification engine "i15".
593 * \see br_rsa_pkcs1_vrfy
595 * \param x signature buffer.
596 * \param xlen signature length (in bytes).
597 * \param hash_oid encoded hash algorithm OID (or `NULL`).
598 * \param hash_len expected hash value length (in bytes).
599 * \param pk RSA public key.
600 * \param hash_out output buffer for the hash value.
601 * \return 1 on success, 0 on error.
603 uint32_t br_rsa_i15_pkcs1_vrfy(const unsigned char *x
, size_t xlen
,
604 const unsigned char *hash_oid
, size_t hash_len
,
605 const br_rsa_public_key
*pk
, unsigned char *hash_out
);
608 * \brief RSA private key engine "i15".
610 * \see br_rsa_private
612 * \param x operand to exponentiate.
613 * \param sk RSA private key.
614 * \return 1 on success, 0 on error.
616 uint32_t br_rsa_i15_private(unsigned char *x
,
617 const br_rsa_private_key
*sk
);
620 * \brief RSA signature generation engine "i15".
622 * \see br_rsa_pkcs1_sign
624 * \param hash_oid encoded hash algorithm OID (or `NULL`).
625 * \param hash hash value.
626 * \param hash_len hash value length (in bytes).
627 * \param sk RSA private key.
628 * \param x output buffer for the hash value.
629 * \return 1 on success, 0 on error.
631 uint32_t br_rsa_i15_pkcs1_sign(const unsigned char *hash_oid
,
632 const unsigned char *hash
, size_t hash_len
,
633 const br_rsa_private_key
*sk
, unsigned char *x
);
636 * \brief Get "default" RSA implementation (public-key operations).
638 * This returns the preferred implementation of RSA (public-key operations)
639 * on the current system.
641 * \return the default implementation.
643 br_rsa_public
br_rsa_public_get_default(void);
646 * \brief Get "default" RSA implementation (private-key operations).
648 * This returns the preferred implementation of RSA (private-key operations)
649 * on the current system.
651 * \return the default implementation.
653 br_rsa_private
br_rsa_private_get_default(void);
656 * \brief Get "default" RSA implementation (PKCS#1 signature verification).
658 * This returns the preferred implementation of RSA (signature verification)
659 * on the current system.
661 * \return the default implementation.
663 br_rsa_pkcs1_vrfy
br_rsa_pkcs1_vrfy_get_default(void);
666 * \brief Get "default" RSA implementation (PKCS#1 signature generation).
668 * This returns the preferred implementation of RSA (signature generation)
669 * on the current system.
671 * \return the default implementation.
673 br_rsa_pkcs1_sign
br_rsa_pkcs1_sign_get_default(void);
676 * \brief RSA decryption helper, for SSL/TLS.
678 * This function performs the RSA decryption for a RSA-based key exchange
679 * in a SSL/TLS server. The provided RSA engine is used. The `data`
680 * parameter points to the value to decrypt, of length `len` bytes. On
681 * success, the 48-byte pre-master secret is copied into `data`, starting
682 * at the first byte of that buffer; on error, the contents of `data`
683 * become indeterminate.
685 * This function first checks that the provided value length (`len`) is
686 * not lower than 59 bytes, and matches the RSA modulus length; if neither
687 * of this property is met, then this function returns 0 and the buffer
690 * Otherwise, decryption and then padding verification are performed, both
691 * in constant-time. A decryption error, or a bad padding, or an
692 * incorrect decrypted value length are reported with a returned value of
693 * 0; on success, 1 is returned. The caller (SSL server engine) is supposed
694 * to proceed with a random pre-master secret in case of error.
696 * \param core RSA private key engine.
697 * \param sk RSA private key.
698 * \param data input/output buffer.
699 * \param len length (in bytes) of the data to decrypt.
700 * \return 1 on success, 0 on error.
702 uint32_t br_rsa_ssl_decrypt(br_rsa_private core
, const br_rsa_private_key
*sk
,
703 unsigned char *data
, size_t len
);