f1bdf2b8e2adc274130e1efa729f37cc531bbcc4
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 * Two 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).
138 * \brief RSA public key.
140 * The structure references the modulus and the public exponent. Both
141 * integers use unsigned big-endian representation; extra leading bytes
142 * of value 0 are allowed.
145 /** \brief Modulus. */
147 /** \brief Modulus length (in bytes). */
149 /** \brief Public exponent. */
151 /** \brief Public exponent length (in bytes). */
156 * \brief RSA private key.
158 * The structure references the primvate factors, reduced private
159 * exponents, and CRT coefficient. It also contains the bit length of
160 * the modulus. The big integers use unsigned big-endian representation;
161 * extra leading bytes of value 0 are allowed. However, the modulus bit
162 * length (`n_bitlen`) MUST be exact.
165 /** \brief Modulus bit length (in bits, exact value). */
167 /** \brief First prime factor. */
169 /** \brief First prime factor length (in bytes). */
171 /** \brief Second prime factor. */
173 /** \brief Second prime factor length (in bytes). */
175 /** \brief First reduced private exponent. */
177 /** \brief First reduced private exponent length (in bytes). */
179 /** \brief Second reduced private exponent. */
181 /** \brief Second reduced private exponent length (in bytes). */
183 /** \brief CRT coefficient. */
185 /** \brief CRT coefficient length (in bytes). */
187 } br_rsa_private_key
;
190 * \brief Type for a RSA public key engine.
192 * The public key engine performs the modular exponentiation of the
193 * provided value with the public exponent. The value is modified in
196 * The value length (`xlen`) is verified to have _exactly_ the same
197 * length as the modulus (actual modulus length, without extra leading
198 * zeros in the modulus representation in memory). If the length does
199 * not match, then this function returns 0 and `x[]` is unmodified.
201 * It `xlen` is correct, then `x[]` is modified. Returned value is 1
202 * on success, 0 on error. Error conditions include an oversized `x[]`
203 * (the array has the same length as the modulus, but the numerical value
204 * is not lower than the modulus) and an invalid modulus (e.g. an even
205 * integer). If an error is reported, then the new contents of `x[]` are
208 * \param x operand to exponentiate.
209 * \param xlen length of the operand (in bytes).
210 * \param pk RSA public key.
211 * \return 1 on success, 0 on error.
213 typedef uint32_t (*br_rsa_public
)(unsigned char *x
, size_t xlen
,
214 const br_rsa_public_key
*pk
);
217 * \brief Type for a RSA signature verification engine (PKCS#1 v1.5).
221 * - The signature itself. The provided array is NOT modified.
223 * - The encoded OID for the hash function. The provided array must begin
224 * with a single byte that contains the length of the OID value (in
225 * bytes), followed by exactly that many bytes. This parameter may
226 * also be `NULL`, in which case the raw hash value should be used
227 * with the PKCS#1 v1.5 "type 1" padding (as used in SSL/TLS up
228 * to TLS-1.1, with a 36-byte hash value).
230 * - The hash output length, in bytes.
234 * - An output buffer for the hash value. The caller must still compare
235 * it with the hash of the data over which the signature is computed.
239 * - Hash length MUST be no more than 64 bytes.
241 * - OID value length MUST be no more than 32 bytes (i.e. `hash_oid[0]`
242 * must have a value in the 0..32 range, inclusive).
244 * This function verifies that the signature length (`xlen`) matches the
245 * modulus length (this function returns 0 on mismatch). If the modulus
246 * size exceeds the maximum supported RSA size, then the function also
249 * Returned value is 1 on success, 0 on error.
251 * Implementations of this type need not be constant-time.
253 * \param x signature buffer.
254 * \param xlen signature length (in bytes).
255 * \param hash_oid encoded hash algorithm OID (or `NULL`).
256 * \param hash_len expected hash value length (in bytes).
257 * \param pk RSA public key.
258 * \param hash_out output buffer for the hash value.
259 * \return 1 on success, 0 on error.
261 typedef uint32_t (*br_rsa_pkcs1_vrfy
)(const unsigned char *x
, size_t xlen
,
262 const unsigned char *hash_oid
, size_t hash_len
,
263 const br_rsa_public_key
*pk
, unsigned char *hash_out
);
266 * \brief Type for a RSA private key engine.
268 * The `x[]` buffer is modified in place, and its length is inferred from
269 * the modulus length (`x[]` is assumed to have a length of
270 * `(sk->n_bitlen+7)/8` bytes).
272 * Returned value is 1 on success, 0 on error.
274 * \param x operand to exponentiate.
275 * \param sk RSA private key.
276 * \return 1 on success, 0 on error.
278 typedef uint32_t (*br_rsa_private
)(unsigned char *x
,
279 const br_rsa_private_key
*sk
);
282 * \brief Type for a RSA signature generation engine (PKCS#1 v1.5).
286 * - The encoded OID for the hash function. The provided array must begin
287 * with a single byte that contains the length of the OID value (in
288 * bytes), followed by exactly that many bytes. This parameter may
289 * also be `NULL`, in which case the raw hash value should be used
290 * with the PKCS#1 v1.5 "type 1" padding (as used in SSL/TLS up
291 * to TLS-1.1, with a 36-byte hash value).
293 * - The hash value computes over the data to sign (its length is
294 * expressed in bytes).
296 * - The RSA private key.
298 * - The output buffer, that receives the signature.
300 * Returned value is 1 on success, 0 on error. Error conditions include
301 * a too small modulus for the provided hash OID and value, or some
302 * invalid key parameters. The signature length is exactly
303 * `(sk->n_bitlen+7)/8` bytes.
305 * This function is expected to be constant-time with regards to the
306 * private key bytes (lengths of the modulus and the individual factors
307 * may leak, though) and to the hashed data.
309 * \param hash_oid encoded hash algorithm OID (or `NULL`).
310 * \param hash hash value.
311 * \param hash_len hash value length (in bytes).
312 * \param sk RSA private key.
313 * \param x output buffer for the signature value.
314 * \return 1 on success, 0 on error.
316 typedef uint32_t (*br_rsa_pkcs1_sign
)(const unsigned char *hash_oid
,
317 const unsigned char *hash
, size_t hash_len
,
318 const br_rsa_private_key
*sk
, unsigned char *x
);
321 * RSA "i32" engine. Integers are internally represented as arrays of
322 * 32-bit integers, and the core multiplication primitive is the
323 * 32x32->64 multiplication.
327 * \brief RSA public key engine "i32".
331 * \param x operand to exponentiate.
332 * \param xlen length of the operand (in bytes).
333 * \param pk RSA public key.
334 * \return 1 on success, 0 on error.
336 uint32_t br_rsa_i32_public(unsigned char *x
, size_t xlen
,
337 const br_rsa_public_key
*pk
);
340 * \brief RSA signature verification engine "i32".
342 * \see br_rsa_pkcs1_vrfy
344 * \param x signature buffer.
345 * \param xlen signature length (in bytes).
346 * \param hash_oid encoded hash algorithm OID (or `NULL`).
347 * \param hash_len expected hash value length (in bytes).
348 * \param pk RSA public key.
349 * \param hash_out output buffer for the hash value.
350 * \return 1 on success, 0 on error.
352 uint32_t br_rsa_i32_pkcs1_vrfy(const unsigned char *x
, size_t xlen
,
353 const unsigned char *hash_oid
, size_t hash_len
,
354 const br_rsa_public_key
*pk
, unsigned char *hash_out
);
357 * \brief RSA private key engine "i32".
359 * \see br_rsa_private
361 * \param x operand to exponentiate.
362 * \param sk RSA private key.
363 * \return 1 on success, 0 on error.
365 uint32_t br_rsa_i32_private(unsigned char *x
,
366 const br_rsa_private_key
*sk
);
369 * \brief RSA signature generation engine "i32".
371 * \see br_rsa_pkcs1_sign
373 * \param hash_oid encoded hash algorithm OID (or `NULL`).
374 * \param hash hash value.
375 * \param hash_len hash value length (in bytes).
376 * \param sk RSA private key.
377 * \param x output buffer for the hash value.
378 * \return 1 on success, 0 on error.
380 uint32_t br_rsa_i32_pkcs1_sign(const unsigned char *hash_oid
,
381 const unsigned char *hash
, size_t hash_len
,
382 const br_rsa_private_key
*sk
, unsigned char *x
);
385 * RSA "i31" engine. Similar to i32, but only 31 bits are used per 32-bit
386 * word. This uses slightly more stack space (about 4% more) and code
387 * space, but it quite faster.
391 * \brief RSA public key engine "i31".
395 * \param x operand to exponentiate.
396 * \param xlen length of the operand (in bytes).
397 * \param pk RSA public key.
398 * \return 1 on success, 0 on error.
400 uint32_t br_rsa_i31_public(unsigned char *x
, size_t xlen
,
401 const br_rsa_public_key
*pk
);
404 * \brief RSA signature verification engine "i31".
406 * \see br_rsa_pkcs1_vrfy
408 * \param x signature buffer.
409 * \param xlen signature length (in bytes).
410 * \param hash_oid encoded hash algorithm OID (or `NULL`).
411 * \param hash_len expected hash value length (in bytes).
412 * \param pk RSA public key.
413 * \param hash_out output buffer for the hash value.
414 * \return 1 on success, 0 on error.
416 uint32_t br_rsa_i31_pkcs1_vrfy(const unsigned char *x
, size_t xlen
,
417 const unsigned char *hash_oid
, size_t hash_len
,
418 const br_rsa_public_key
*pk
, unsigned char *hash_out
);
421 * \brief RSA private key engine "i31".
423 * \see br_rsa_private
425 * \param x operand to exponentiate.
426 * \param sk RSA private key.
427 * \return 1 on success, 0 on error.
429 uint32_t br_rsa_i31_private(unsigned char *x
,
430 const br_rsa_private_key
*sk
);
433 * \brief RSA signature generation engine "i31".
435 * \see br_rsa_pkcs1_sign
437 * \param hash_oid encoded hash algorithm OID (or `NULL`).
438 * \param hash hash value.
439 * \param hash_len hash value length (in bytes).
440 * \param sk RSA private key.
441 * \param x output buffer for the hash value.
442 * \return 1 on success, 0 on error.
444 uint32_t br_rsa_i31_pkcs1_sign(const unsigned char *hash_oid
,
445 const unsigned char *hash
, size_t hash_len
,
446 const br_rsa_private_key
*sk
, unsigned char *x
);
449 * \brief RSA decryption helper, for SSL/TLS.
451 * This function performs the RSA decryption for a RSA-based key exchange
452 * in a SSL/TLS server. The provided RSA engine is used. The `data`
453 * parameter points to the value to decrypt, of length `len` bytes. On
454 * success, the 48-byte pre-master secret is copied into `data`, starting
455 * at the first byte of that buffer; on error, the contents of `data`
456 * become indeterminate.
458 * This function first checks that the provided value length (`len`) is
459 * not lower than 59 bytes, and matches the RSA modulus length; if neither
460 * of this property is met, then this function returns 0 and the buffer
463 * Otherwise, decryption and then padding verification are performed, both
464 * in constant-time. A decryption error, or a bad padding, or an
465 * incorrect decrypted value length are reported with a returned value of
466 * 0; on success, 1 is returned. The caller (SSL server engine) is supposed
467 * to proceed with a random pre-master secret in case of error.
469 * \param core RSA private key engine.
470 * \param sk RSA private key.
471 * \param data input/output buffer.
472 * \param len length (in bytes) of the data to decrypt.
473 * \return 1 on success, 0 on error.
475 uint32_t br_rsa_ssl_decrypt(br_rsa_private core
, const br_rsa_private_key
*sk
,
476 unsigned char *data
, size_t len
);