More optimisations for EC P-256 "i15" (specialised squaring function, mixed coordinat...
[BearSSL] / inc / bearssl_ec.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_EC_H__
26 #define BR_BEARSSL_EC_H__
27
28 #include <stddef.h>
29 #include <stdint.h>
30
31 /** \file bearssl_ec.h
32 *
33 * # Elliptic Curves
34 *
35 * This file documents the EC implementations provided with BearSSL, and
36 * ECDSA.
37 *
38 * ## Elliptic Curve API
39 *
40 * Only "named curves" are supported. Each EC implementation supports
41 * one or several named curves, identified by symbolic identifiers.
42 * These identifiers are small integers, that correspond to the values
43 * registered by the
44 * [IANA](http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8).
45 *
46 * Since all currently defined elliptic curve identifiers are in the 0..31
47 * range, it is convenient to encode support of some curves in a 32-bit
48 * word, such that bit x corresponds to curve of identifier x.
49 *
50 * An EC implementation is incarnated by a `br_ec_impl` instance, that
51 * offers the following fields:
52 *
53 * - `supported_curves`
54 *
55 * A 32-bit word that documents the identifiers of the curves supported
56 * by this implementation.
57 *
58 * - `generator()`
59 *
60 * Callback method that returns a pointer to the conventional generator
61 * point for that curve.
62 *
63 * - `order()`
64 *
65 * Callback method that returns a pointer to the subgroup order for
66 * that curve. That value uses unsigned big-endian encoding.
67 *
68 * - `mul()`
69 *
70 * Multiply a curve point with an integer.
71 *
72 * - `mulgen()`
73 *
74 * Multiply the curve generator with an integer. This may be faster
75 * than the generic `mul()`.
76 *
77 * - `muladd()`
78 *
79 * Multiply two curve points by two integers, and return the sum of
80 * the two products.
81 *
82 * All curve points are represented in uncompressed format. The `mul()`
83 * and `muladd()` methods take care to validate that the provided points
84 * are really part of the relevant curve subgroup.
85 *
86 * For all point multiplication functions, the following holds:
87 *
88 * - Functions validate that the provided points are valid members
89 * of the relevant curve subgroup. An error is reported if that is
90 * not the case.
91 *
92 * - Processing is constant-time, even if the point operands are not
93 * valid. This holds for both the source and resulting points, and
94 * the multipliers (integers). Only the byte length of the provided
95 * multiplier arrays (not their actual value length in bits) may
96 * leak through timing-based side channels.
97 *
98 * - The multipliers (integers) MUST be lower than the subgroup order.
99 * If this property is not met, then the result is indeterminate,
100 * but an error value is not ncessearily returned.
101 *
102 *
103 * ## ECDSA
104 *
105 * ECDSA signatures have two standard formats, called "raw" and "asn1".
106 * Internally, such a signature is a pair of modular integers `(r,s)`.
107 * The "raw" format is the concatenation of the unsigned big-endian
108 * encodings of these two integers, possibly left-padded with zeros so
109 * that they have the same encoded length. The "asn1" format is the
110 * DER encoding of an ASN.1 structure that contains the two integer
111 * values:
112 *
113 * ECDSASignature ::= SEQUENCE {
114 * r INTEGER,
115 * s INTEGER
116 * }
117 *
118 * In general, in all of X.509 and SSL/TLS, the "asn1" format is used.
119 * BearSSL offers ECDSA implementations for both formats; conversion
120 * functions between the two formats are also provided. Conversion of a
121 * "raw" format signature into "asn1" may enlarge a signature by no more
122 * than 9 bytes for all supported curves; conversely, conversion of an
123 * "asn1" signature to "raw" may expand the signature but the "raw"
124 * length will never be more than twice the length of the "asn1" length
125 * (and usually it will be shorter).
126 *
127 * Note that for a given signature, the "raw" format is not fully
128 * deterministic, in that it does not enforce a minimal common length.
129 */
130
131 /*
132 * Standard curve ID. These ID are equal to the assigned numerical
133 * identifiers assigned to these curves for TLS:
134 * http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
135 */
136
137 /** \brief Identifier for named curve sect163k1. */
138 #define BR_EC_sect163k1 1
139
140 /** \brief Identifier for named curve sect163r1. */
141 #define BR_EC_sect163r1 2
142
143 /** \brief Identifier for named curve sect163r2. */
144 #define BR_EC_sect163r2 3
145
146 /** \brief Identifier for named curve sect193r1. */
147 #define BR_EC_sect193r1 4
148
149 /** \brief Identifier for named curve sect193r2. */
150 #define BR_EC_sect193r2 5
151
152 /** \brief Identifier for named curve sect233k1. */
153 #define BR_EC_sect233k1 6
154
155 /** \brief Identifier for named curve sect233r1. */
156 #define BR_EC_sect233r1 7
157
158 /** \brief Identifier for named curve sect239k1. */
159 #define BR_EC_sect239k1 8
160
161 /** \brief Identifier for named curve sect283k1. */
162 #define BR_EC_sect283k1 9
163
164 /** \brief Identifier for named curve sect283r1. */
165 #define BR_EC_sect283r1 10
166
167 /** \brief Identifier for named curve sect409k1. */
168 #define BR_EC_sect409k1 11
169
170 /** \brief Identifier for named curve sect409r1. */
171 #define BR_EC_sect409r1 12
172
173 /** \brief Identifier for named curve sect571k1. */
174 #define BR_EC_sect571k1 13
175
176 /** \brief Identifier for named curve sect571r1. */
177 #define BR_EC_sect571r1 14
178
179 /** \brief Identifier for named curve secp160k1. */
180 #define BR_EC_secp160k1 15
181
182 /** \brief Identifier for named curve secp160r1. */
183 #define BR_EC_secp160r1 16
184
185 /** \brief Identifier for named curve secp160r2. */
186 #define BR_EC_secp160r2 17
187
188 /** \brief Identifier for named curve secp192k1. */
189 #define BR_EC_secp192k1 18
190
191 /** \brief Identifier for named curve secp192r1. */
192 #define BR_EC_secp192r1 19
193
194 /** \brief Identifier for named curve secp224k1. */
195 #define BR_EC_secp224k1 20
196
197 /** \brief Identifier for named curve secp224r1. */
198 #define BR_EC_secp224r1 21
199
200 /** \brief Identifier for named curve secp256k1. */
201 #define BR_EC_secp256k1 22
202
203 /** \brief Identifier for named curve secp256r1. */
204 #define BR_EC_secp256r1 23
205
206 /** \brief Identifier for named curve secp384r1. */
207 #define BR_EC_secp384r1 24
208
209 /** \brief Identifier for named curve secp521r1. */
210 #define BR_EC_secp521r1 25
211
212 /** \brief Identifier for named curve brainpoolP256r1. */
213 #define BR_EC_brainpoolP256r1 26
214
215 /** \brief Identifier for named curve brainpoolP384r1. */
216 #define BR_EC_brainpoolP384r1 27
217
218 /** \brief Identifier for named curve brainpoolP512r1. */
219 #define BR_EC_brainpoolP512r1 28
220
221 /**
222 * \brief Structure for an EC public key.
223 */
224 typedef struct {
225 /** \brief Identifier for the curve used by this key. */
226 int curve;
227 /** \brief Public curve point (uncompressed format). */
228 unsigned char *q;
229 /** \brief Length of public curve point (in bytes). */
230 size_t qlen;
231 } br_ec_public_key;
232
233 /**
234 * \brief Structure for an EC private key.
235 *
236 * The private key is an integer modulo the curve subgroup order. The
237 * encoding below tolerates extra leading zeros. In general, it is
238 * recommended that the private key has the same length as the curve
239 * subgroup order.
240 */
241 typedef struct {
242 /** \brief Identifier for the curve used by this key. */
243 int curve;
244 /** \brief Private key (integer, unsigned big-endian encoding). */
245 unsigned char *x;
246 /** \brief Private key length (in bytes). */
247 size_t xlen;
248 } br_ec_private_key;
249
250 /**
251 * \brief Type for an EC implementation.
252 */
253 typedef struct {
254 /**
255 * \brief Supported curves.
256 *
257 * This word is a bitfield: bit `x` is set if the curve of ID `x`
258 * is supported. E.g. an implementation supporting both NIST P-256
259 * (secp256r1, ID 23) and NIST P-384 (secp384r1, ID 24) will have
260 * value `0x01800000` in this field.
261 */
262 uint32_t supported_curves;
263
264 /**
265 * \brief Get the conventional generator.
266 *
267 * This function returns the conventional generator (encoded
268 * curve point) for the specified curve. This function MUST NOT
269 * be called if the curve is not supported.
270 *
271 * \param curve curve identifier.
272 * \param len receiver for the encoded generator length (in bytes).
273 * \return the encoded generator.
274 */
275 const unsigned char *(*generator)(int curve, size_t *len);
276
277 /**
278 * \brief Get the subgroup order.
279 *
280 * This function returns the order of the subgroup generated by
281 * the conventional generator, for the specified curve. Unsigned
282 * big-endian encoding is used. This function MUST NOT be called
283 * if the curve is not supported.
284 *
285 * \param curve curve identifier.
286 * \param len receiver for the encoded order length (in bytes).
287 * \return the encoded order.
288 */
289 const unsigned char *(*order)(int curve, size_t *len);
290
291 /**
292 * \brief Multiply a curve point by an integer.
293 *
294 * The source point is provided in array `G` (of size `Glen` bytes);
295 * the multiplication result is written over it. The multiplier
296 * `x` (of size `xlen` bytes) uses unsigned big-endian encoding.
297 *
298 * Rules:
299 *
300 * - The specified curve MUST be supported.
301 *
302 * - The source point must be a valid point on the relevant curve
303 * subgroup (and not the "point at infinity" either). If this is
304 * not the case, then this function returns an error (0).
305 *
306 * - The multiplier integer MUST be non-zero and less than the
307 * curve subgroup order. If this property does not hold, then
308 * the result is indeterminate and an error code is not
309 * guaranteed.
310 *
311 * Returned value is 1 on success, 0 on error. On error, the
312 * contents of `G` are indeterminate.
313 *
314 * \param G point to multiply.
315 * \param Glen length of the encoded point (in bytes).
316 * \param x multiplier (unsigned big-endian).
317 * \param xlen multiplier length (in bytes).
318 * \param curve curve identifier.
319 * \return 1 on success, 0 on error.
320 */
321 uint32_t (*mul)(unsigned char *G, size_t Glen,
322 const unsigned char *x, size_t xlen, int curve);
323
324 /**
325 * \brief Multiply the generator by an integer.
326 *
327 * The multiplier MUST be non-zero and less than the curve
328 * subgroup order. Results are indeterminate if this property
329 * does not hold.
330 *
331 * \param R output buffer for the point.
332 * \param x multiplier (unsigned big-endian).
333 * \param xlen multiplier length (in bytes).
334 * \param curve curve identifier.
335 * \return encoded result point length (in bytes).
336 */
337 size_t (*mulgen)(unsigned char *R,
338 const unsigned char *x, size_t xlen, int curve);
339
340 /**
341 * \brief Multiply two points by two integers and add the
342 * results.
343 *
344 * The point `x*A + y*B` is computed and written back in the `A`
345 * array.
346 *
347 * Rules:
348 *
349 * - The specified curve MUST be supported.
350 *
351 * - The source points (`A` and `B`) must be valid points on
352 * the relevant curve subgroup (and not the "point at
353 * infinity" either). If this is not the case, then this
354 * function returns an error (0).
355 *
356 * - If the `B` pointer is `NULL`, then the conventional
357 * subgroup generator is used. With some implementations,
358 * this may be faster than providing a pointer to the
359 * generator.
360 *
361 * - The multiplier integers (`x` and `y`) MUST be non-zero
362 * and less than the curve subgroup order. If either integer
363 * is zero, then an error is reported, but if one of them is
364 * not lower than the subgroup order, then the result is
365 * indeterminate and an error code is not guaranteed.
366 *
367 * - If the final result is the point at infinity, then an
368 * error is returned.
369 *
370 * Returned value is 1 on success, 0 on error. On error, the
371 * contents of `A` are indeterminate.
372 *
373 * \param A first point to multiply.
374 * \param B second point to multiply (`NULL` for the generator).
375 * \param len common length of the encoded points (in bytes).
376 * \param x multiplier for `A` (unsigned big-endian).
377 * \param xlen length of multiplier for `A` (in bytes).
378 * \param y multiplier for `A` (unsigned big-endian).
379 * \param ylen length of multiplier for `A` (in bytes).
380 * \param curve curve identifier.
381 * \return 1 on success, 0 on error.
382 */
383 uint32_t (*muladd)(unsigned char *A, const unsigned char *B, size_t len,
384 const unsigned char *x, size_t xlen,
385 const unsigned char *y, size_t ylen, int curve);
386 } br_ec_impl;
387
388 /**
389 * \brief EC implementation "i31".
390 *
391 * This implementation internally uses generic code for modular integers,
392 * with a representation as sequences of 31-bit words. It supports secp256r1,
393 * secp384r1 and secp521r1 (aka NIST curves P-256, P-384 and P-521).
394 */
395 extern const br_ec_impl br_ec_prime_i31;
396
397 /**
398 * \brief EC implementation "i15".
399 *
400 * This implementation internally uses generic code for modular integers,
401 * with a representation as sequences of 15-bit words. It supports secp256r1,
402 * secp384r1 and secp521r1 (aka NIST curves P-256, P-384 and P-521).
403 */
404 extern const br_ec_impl br_ec_prime_i15;
405
406 /**
407 * \brief EC implementation "i15" for P-256.
408 *
409 * This implementation uses specialised code for curve secp256r1 (also
410 * known as NIST P-256), with Karatsuba decomposition, and fast modular
411 * reduction thanks to the field modulus special format. Only 32-bit
412 * multiplications are used (with 32-bit results, not 64-bit).
413 */
414 extern const br_ec_impl br_ec_p256_i15;
415
416 /**
417 * \brief Convert a signature from "raw" to "asn1".
418 *
419 * Conversion is done "in place" and the new length is returned.
420 * Conversion may enlarge the signature, but by no more than 9 bytes at
421 * most. On error, 0 is returned (error conditions include an odd raw
422 * signature length, or an oversized integer).
423 *
424 * \param sig signature to convert.
425 * \param sig_len signature length (in bytes).
426 * \return the new signature length, or 0 on error.
427 */
428 size_t br_ecdsa_raw_to_asn1(void *sig, size_t sig_len);
429
430 /**
431 * \brief Convert a signature from "asn1" to "raw".
432 *
433 * Conversion is done "in place" and the new length is returned.
434 * Conversion may enlarge the signature, but the new signature length
435 * will be less than twice the source length at most. On error, 0 is
436 * returned (error conditions include an invalid ASN.1 structure or an
437 * oversized integer).
438 *
439 * \param sig signature to convert.
440 * \param sig_len signature length (in bytes).
441 * \return the new signature length, or 0 on error.
442 */
443 size_t br_ecdsa_asn1_to_raw(void *sig, size_t sig_len);
444
445 /**
446 * \brief Type for an ECDSA signer function.
447 *
448 * A pointer to the EC implementation is provided. The hash value is
449 * assumed to have the length inferred from the designated hash function
450 * class.
451 *
452 * Signature is written in the buffer pointed to by `sig`, and the length
453 * (in bytes) is returned. On error, nothing is written in the buffer,
454 * and 0 is returned. This function returns 0 if the specified curve is
455 * not supported by the provided EC implementation.
456 *
457 * The signature format is either "raw" or "asn1", depending on the
458 * implementation; maximum length is predictable from the implemented
459 * curve:
460 *
461 * | curve | raw | asn1 |
462 * | :--------- | --: | ---: |
463 * | NIST P-256 | 64 | 72 |
464 * | NIST P-384 | 96 | 104 |
465 * | NIST P-521 | 132 | 139 |
466 *
467 * \param impl EC implementation to use.
468 * \param hf hash function used to process the data.
469 * \param hash_value signed data (hashed).
470 * \param sk EC private key.
471 * \param sig destination buffer.
472 * \return the signature length (in bytes), or 0 on error.
473 */
474 typedef size_t (*br_ecdsa_sign)(const br_ec_impl *impl,
475 const br_hash_class *hf, const void *hash_value,
476 const br_ec_private_key *sk, void *sig);
477
478 /**
479 * \brief Type for an ECDSA signature verification function.
480 *
481 * A pointer to the EC implementation is provided. The hashed value,
482 * computed over the purportedly signed data, is also provided with
483 * its length.
484 *
485 * The signature format is either "raw" or "asn1", depending on the
486 * implementation.
487 *
488 * Returned value is 1 on success (valid signature), 0 on error. This
489 * function returns 0 if the specified curve is not supported by the
490 * provided EC implementation.
491 *
492 * \param impl EC implementation to use.
493 * \param hash signed data (hashed).
494 * \param hash_len hash value length (in bytes).
495 * \param pk EC public key.
496 * \param sig signature.
497 * \param sig_len signature length (in bytes).
498 * \return 1 on success, 0 on error.
499 */
500 typedef uint32_t (*br_ecdsa_vrfy)(const br_ec_impl *impl,
501 const void *hash, size_t hash_len,
502 const br_ec_public_key *pk, const void *sig, size_t sig_len);
503
504 /**
505 * \brief ECDSA signature generator, "i31" implementation, "asn1" format.
506 *
507 * \see br_ecdsa_sign()
508 *
509 * \param impl EC implementation to use.
510 * \param hf hash function used to process the data.
511 * \param hash_value signed data (hashed).
512 * \param sk EC private key.
513 * \param sig destination buffer.
514 * \return the signature length (in bytes), or 0 on error.
515 */
516 size_t br_ecdsa_i31_sign_asn1(const br_ec_impl *impl,
517 const br_hash_class *hf, const void *hash_value,
518 const br_ec_private_key *sk, void *sig);
519
520 /**
521 * \brief ECDSA signature generator, "i31" implementation, "raw" format.
522 *
523 * \see br_ecdsa_sign()
524 *
525 * \param impl EC implementation to use.
526 * \param hf hash function used to process the data.
527 * \param hash_value signed data (hashed).
528 * \param sk EC private key.
529 * \param sig destination buffer.
530 * \return the signature length (in bytes), or 0 on error.
531 */
532 size_t br_ecdsa_i31_sign_raw(const br_ec_impl *impl,
533 const br_hash_class *hf, const void *hash_value,
534 const br_ec_private_key *sk, void *sig);
535
536 /**
537 * \brief ECDSA signature verifier, "i31" implementation, "asn1" format.
538 *
539 * \see br_ecdsa_vrfy()
540 *
541 * \param impl EC implementation to use.
542 * \param hash signed data (hashed).
543 * \param hash_len hash value length (in bytes).
544 * \param pk EC public key.
545 * \param sig signature.
546 * \param sig_len signature length (in bytes).
547 * \return 1 on success, 0 on error.
548 */
549 uint32_t br_ecdsa_i31_vrfy_asn1(const br_ec_impl *impl,
550 const void *hash, size_t hash_len,
551 const br_ec_public_key *pk, const void *sig, size_t sig_len);
552
553 /**
554 * \brief ECDSA signature verifier, "i31" implementation, "raw" format.
555 *
556 * \see br_ecdsa_vrfy()
557 *
558 * \param impl EC implementation to use.
559 * \param hash signed data (hashed).
560 * \param hash_len hash value length (in bytes).
561 * \param pk EC public key.
562 * \param sig signature.
563 * \param sig_len signature length (in bytes).
564 * \return 1 on success, 0 on error.
565 */
566 uint32_t br_ecdsa_i31_vrfy_raw(const br_ec_impl *impl,
567 const void *hash, size_t hash_len,
568 const br_ec_public_key *pk, const void *sig, size_t sig_len);
569
570 /**
571 * \brief ECDSA signature generator, "i15" implementation, "asn1" format.
572 *
573 * \see br_ecdsa_sign()
574 *
575 * \param impl EC implementation to use.
576 * \param hf hash function used to process the data.
577 * \param hash_value signed data (hashed).
578 * \param sk EC private key.
579 * \param sig destination buffer.
580 * \return the signature length (in bytes), or 0 on error.
581 */
582 size_t br_ecdsa_i15_sign_asn1(const br_ec_impl *impl,
583 const br_hash_class *hf, const void *hash_value,
584 const br_ec_private_key *sk, void *sig);
585
586 /**
587 * \brief ECDSA signature generator, "i15" implementation, "raw" format.
588 *
589 * \see br_ecdsa_sign()
590 *
591 * \param impl EC implementation to use.
592 * \param hf hash function used to process the data.
593 * \param hash_value signed data (hashed).
594 * \param sk EC private key.
595 * \param sig destination buffer.
596 * \return the signature length (in bytes), or 0 on error.
597 */
598 size_t br_ecdsa_i15_sign_raw(const br_ec_impl *impl,
599 const br_hash_class *hf, const void *hash_value,
600 const br_ec_private_key *sk, void *sig);
601
602 /**
603 * \brief ECDSA signature verifier, "i15" implementation, "asn1" format.
604 *
605 * \see br_ecdsa_vrfy()
606 *
607 * \param impl EC implementation to use.
608 * \param hash signed data (hashed).
609 * \param hash_len hash value length (in bytes).
610 * \param pk EC public key.
611 * \param sig signature.
612 * \param sig_len signature length (in bytes).
613 * \return 1 on success, 0 on error.
614 */
615 uint32_t br_ecdsa_i15_vrfy_asn1(const br_ec_impl *impl,
616 const void *hash, size_t hash_len,
617 const br_ec_public_key *pk, const void *sig, size_t sig_len);
618
619 /**
620 * \brief ECDSA signature verifier, "i15" implementation, "raw" format.
621 *
622 * \see br_ecdsa_vrfy()
623 *
624 * \param impl EC implementation to use.
625 * \param hash signed data (hashed).
626 * \param hash_len hash value length (in bytes).
627 * \param pk EC public key.
628 * \param sig signature.
629 * \param sig_len signature length (in bytes).
630 * \return 1 on success, 0 on error.
631 */
632 uint32_t br_ecdsa_i15_vrfy_raw(const br_ec_impl *impl,
633 const void *hash, size_t hash_len,
634 const br_ec_public_key *pk, const void *sig, size_t sig_len);
635
636 #endif