f1bdf2b8e2adc274130e1efa729f37cc531bbcc4

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 */

25 #ifndef BR_BEARSSL_RSA_H__

26 #define BR_BEARSSL_RSA_H__

28 #include <stddef.h>

29 #include <stdint.h>

31 /** \file bearssl_rsa.h

32 *

33 * # RSA

34 *

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.

39 *

40 * ## Key Elements

41 *

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.

50 *

51 * RSA public keys consist in two integers:

52 *

53 * - the modulus (`n`);

54 * - the public exponent (`e`).

55 *

56 * RSA private keys, as defined in

57 * [PKCS#1](https://tools.ietf.org/html/rfc3447), contain eight integers:

58 *

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`).

67 *

68 * However, the implementations defined in BearSSL use only five of

69 * these integers: `p`, `q`, `dp`, `dq` and `iq`.

70 *

71 * ## Security Features and Limitations

72 *

73 * The implementations contained in BearSSL have the following limitations

74 * and features:

75 *

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.

81 *

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.

87 *

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.

93 *

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`).

98 *

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.

104 *

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).

112 *

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.

116 *

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.

121 *

122 * ## Implementations

123 *

124 * Two RSA implementations are included:

125 *

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.

129 *

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).

135 */

137 /**

138 * \brief RSA public key.

139 *

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.

143 */

145 /** \brief Modulus. */

147 /** \brief Modulus length (in bytes). */

149 /** \brief Public exponent. */

151 /** \brief Public exponent length (in bytes). */

155 /**

156 * \brief RSA private key.

157 *

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.

163 */

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). */

189 /**

190 * \brief Type for a RSA public key engine.

191 *

192 * The public key engine performs the modular exponentiation of the

193 * provided value with the public exponent. The value is modified in

194 * place.

195 *

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.

200 *

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

206 * unspecified.

207 *

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.

212 */

216 /**

217 * \brief Type for a RSA signature verification engine (PKCS#1 v1.5).

218 *

219 * Parameters are:

220 *

221 * - The signature itself. The provided array is NOT modified.

222 *

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).

229 *

230 * - The hash output length, in bytes.

231 *

232 * - The public key.

233 *

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.

236 *

237 * **Constraints:**

238 *

239 * - Hash length MUST be no more than 64 bytes.

240 *

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).

243 *

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

247 * returns 0.

248 *

249 * Returned value is 1 on success, 0 on error.

250 *

251 * Implementations of this type need not be constant-time.

252 *

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.

260 */

265 /**

266 * \brief Type for a RSA private key engine.

267 *

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).

271 *

272 * Returned value is 1 on success, 0 on error.

273 *

274 * \param x operand to exponentiate.

275 * \param sk RSA private key.

276 * \return 1 on success, 0 on error.

277 */

281 /**

282 * \brief Type for a RSA signature generation engine (PKCS#1 v1.5).

283 *

284 * Parameters are:

285 *

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).

292 *

293 * - The hash value computes over the data to sign (its length is

294 * expressed in bytes).

295 *

296 * - The RSA private key.

297 *

298 * - The output buffer, that receives the signature.

299 *

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.

304 *

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.

308 *

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.

315 */

320 /*

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.

324 */

326 /**

327 * \brief RSA public key engine "i32".

328 *

329 * \see br_rsa_public

330 *

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.

335 */

339 /**

340 * \brief RSA signature verification engine "i32".

341 *

342 * \see br_rsa_pkcs1_vrfy

343 *

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.

351 */

356 /**

357 * \brief RSA private key engine "i32".

358 *

359 * \see br_rsa_private

360 *

361 * \param x operand to exponentiate.

362 * \param sk RSA private key.

363 * \return 1 on success, 0 on error.

364 */

368 /**

369 * \brief RSA signature generation engine "i32".

370 *

371 * \see br_rsa_pkcs1_sign

372 *

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.

379 */

384 /*

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.

388 */

390 /**

391 * \brief RSA public key engine "i31".

392 *

393 * \see br_rsa_public

394 *

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.

399 */

403 /**

404 * \brief RSA signature verification engine "i31".

405 *

406 * \see br_rsa_pkcs1_vrfy

407 *

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.

415 */

420 /**

421 * \brief RSA private key engine "i31".

422 *

423 * \see br_rsa_private

424 *

425 * \param x operand to exponentiate.

426 * \param sk RSA private key.

427 * \return 1 on success, 0 on error.

428 */

432 /**

433 * \brief RSA signature generation engine "i31".

434 *

435 * \see br_rsa_pkcs1_sign

436 *

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.

443 */

448 /**

449 * \brief RSA decryption helper, for SSL/TLS.

450 *

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.

457 *

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

461 * is unmodified.

462 *

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.

468 *

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.

474 */

478 #endif