Fixed endianness in Curve25519 implementation (no consequence on security). Also...
[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 #include "bearssl_rand.h"
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /** \file bearssl_ec.h
38 *
39 * # Elliptic Curves
40 *
41 * This file documents the EC implementations provided with BearSSL, and
42 * ECDSA.
43 *
44 * ## Elliptic Curve API
45 *
46 * Only "named curves" are supported. Each EC implementation supports
47 * one or several named curves, identified by symbolic identifiers.
48 * These identifiers are small integers, that correspond to the values
49 * registered by the
50 * [IANA](http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8).
51 *
52 * Since all currently defined elliptic curve identifiers are in the 0..31
53 * range, it is convenient to encode support of some curves in a 32-bit
54 * word, such that bit x corresponds to curve of identifier x.
55 *
56 * An EC implementation is incarnated by a `br_ec_impl` instance, that
57 * offers the following fields:
58 *
59 * - `supported_curves`
60 *
61 * A 32-bit word that documents the identifiers of the curves supported
62 * by this implementation.
63 *
64 * - `generator()`
65 *
66 * Callback method that returns a pointer to the conventional generator
67 * point for that curve.
68 *
69 * - `order()`
70 *
71 * Callback method that returns a pointer to the subgroup order for
72 * that curve. That value uses unsigned big-endian encoding.
73 *
74 * - `xoff()`
75 *
76 * Callback method that returns the offset and length of the X
77 * coordinate in an encoded point.
78 *
79 * - `mul()`
80 *
81 * Multiply a curve point with an integer.
82 *
83 * - `mulgen()`
84 *
85 * Multiply the curve generator with an integer. This may be faster
86 * than the generic `mul()`.
87 *
88 * - `muladd()`
89 *
90 * Multiply two curve points by two integers, and return the sum of
91 * the two products.
92 *
93 * All curve points are represented in uncompressed format. The `mul()`
94 * and `muladd()` methods take care to validate that the provided points
95 * are really part of the relevant curve subgroup.
96 *
97 * For all point multiplication functions, the following holds:
98 *
99 * - Functions validate that the provided points are valid members
100 * of the relevant curve subgroup. An error is reported if that is
101 * not the case.
102 *
103 * - Processing is constant-time, even if the point operands are not
104 * valid. This holds for both the source and resulting points, and
105 * the multipliers (integers). Only the byte length of the provided
106 * multiplier arrays (not their actual value length in bits) may
107 * leak through timing-based side channels.
108 *
109 * - The multipliers (integers) MUST be lower than the subgroup order.
110 * If this property is not met, then the result is indeterminate,
111 * but an error value is not ncessearily returned.
112 *
113 *
114 * ## ECDSA
115 *
116 * ECDSA signatures have two standard formats, called "raw" and "asn1".
117 * Internally, such a signature is a pair of modular integers `(r,s)`.
118 * The "raw" format is the concatenation of the unsigned big-endian
119 * encodings of these two integers, possibly left-padded with zeros so
120 * that they have the same encoded length. The "asn1" format is the
121 * DER encoding of an ASN.1 structure that contains the two integer
122 * values:
123 *
124 * ECDSASignature ::= SEQUENCE {
125 * r INTEGER,
126 * s INTEGER
127 * }
128 *
129 * In general, in all of X.509 and SSL/TLS, the "asn1" format is used.
130 * BearSSL offers ECDSA implementations for both formats; conversion
131 * functions between the two formats are also provided. Conversion of a
132 * "raw" format signature into "asn1" may enlarge a signature by no more
133 * than 9 bytes for all supported curves; conversely, conversion of an
134 * "asn1" signature to "raw" may expand the signature but the "raw"
135 * length will never be more than twice the length of the "asn1" length
136 * (and usually it will be shorter).
137 *
138 * Note that for a given signature, the "raw" format is not fully
139 * deterministic, in that it does not enforce a minimal common length.
140 */
141
142 /*
143 * Standard curve ID. These ID are equal to the assigned numerical
144 * identifiers assigned to these curves for TLS:
145 * http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
146 */
147
148 /** \brief Identifier for named curve sect163k1. */
149 #define BR_EC_sect163k1 1
150
151 /** \brief Identifier for named curve sect163r1. */
152 #define BR_EC_sect163r1 2
153
154 /** \brief Identifier for named curve sect163r2. */
155 #define BR_EC_sect163r2 3
156
157 /** \brief Identifier for named curve sect193r1. */
158 #define BR_EC_sect193r1 4
159
160 /** \brief Identifier for named curve sect193r2. */
161 #define BR_EC_sect193r2 5
162
163 /** \brief Identifier for named curve sect233k1. */
164 #define BR_EC_sect233k1 6
165
166 /** \brief Identifier for named curve sect233r1. */
167 #define BR_EC_sect233r1 7
168
169 /** \brief Identifier for named curve sect239k1. */
170 #define BR_EC_sect239k1 8
171
172 /** \brief Identifier for named curve sect283k1. */
173 #define BR_EC_sect283k1 9
174
175 /** \brief Identifier for named curve sect283r1. */
176 #define BR_EC_sect283r1 10
177
178 /** \brief Identifier for named curve sect409k1. */
179 #define BR_EC_sect409k1 11
180
181 /** \brief Identifier for named curve sect409r1. */
182 #define BR_EC_sect409r1 12
183
184 /** \brief Identifier for named curve sect571k1. */
185 #define BR_EC_sect571k1 13
186
187 /** \brief Identifier for named curve sect571r1. */
188 #define BR_EC_sect571r1 14
189
190 /** \brief Identifier for named curve secp160k1. */
191 #define BR_EC_secp160k1 15
192
193 /** \brief Identifier for named curve secp160r1. */
194 #define BR_EC_secp160r1 16
195
196 /** \brief Identifier for named curve secp160r2. */
197 #define BR_EC_secp160r2 17
198
199 /** \brief Identifier for named curve secp192k1. */
200 #define BR_EC_secp192k1 18
201
202 /** \brief Identifier for named curve secp192r1. */
203 #define BR_EC_secp192r1 19
204
205 /** \brief Identifier for named curve secp224k1. */
206 #define BR_EC_secp224k1 20
207
208 /** \brief Identifier for named curve secp224r1. */
209 #define BR_EC_secp224r1 21
210
211 /** \brief Identifier for named curve secp256k1. */
212 #define BR_EC_secp256k1 22
213
214 /** \brief Identifier for named curve secp256r1. */
215 #define BR_EC_secp256r1 23
216
217 /** \brief Identifier for named curve secp384r1. */
218 #define BR_EC_secp384r1 24
219
220 /** \brief Identifier for named curve secp521r1. */
221 #define BR_EC_secp521r1 25
222
223 /** \brief Identifier for named curve brainpoolP256r1. */
224 #define BR_EC_brainpoolP256r1 26
225
226 /** \brief Identifier for named curve brainpoolP384r1. */
227 #define BR_EC_brainpoolP384r1 27
228
229 /** \brief Identifier for named curve brainpoolP512r1. */
230 #define BR_EC_brainpoolP512r1 28
231
232 /** \brief Identifier for named curve Curve25519. */
233 #define BR_EC_curve25519 29
234
235 /** \brief Identifier for named curve Curve448. */
236 #define BR_EC_curve448 30
237
238 /**
239 * \brief Structure for an EC public key.
240 */
241 typedef struct {
242 /** \brief Identifier for the curve used by this key. */
243 int curve;
244 /** \brief Public curve point (uncompressed format). */
245 unsigned char *q;
246 /** \brief Length of public curve point (in bytes). */
247 size_t qlen;
248 } br_ec_public_key;
249
250 /**
251 * \brief Structure for an EC private key.
252 *
253 * The private key is an integer modulo the curve subgroup order. The
254 * encoding below tolerates extra leading zeros. In general, it is
255 * recommended that the private key has the same length as the curve
256 * subgroup order.
257 */
258 typedef struct {
259 /** \brief Identifier for the curve used by this key. */
260 int curve;
261 /** \brief Private key (integer, unsigned big-endian encoding). */
262 unsigned char *x;
263 /** \brief Private key length (in bytes). */
264 size_t xlen;
265 } br_ec_private_key;
266
267 /**
268 * \brief Type for an EC implementation.
269 */
270 typedef struct {
271 /**
272 * \brief Supported curves.
273 *
274 * This word is a bitfield: bit `x` is set if the curve of ID `x`
275 * is supported. E.g. an implementation supporting both NIST P-256
276 * (secp256r1, ID 23) and NIST P-384 (secp384r1, ID 24) will have
277 * value `0x01800000` in this field.
278 */
279 uint32_t supported_curves;
280
281 /**
282 * \brief Get the conventional generator.
283 *
284 * This function returns the conventional generator (encoded
285 * curve point) for the specified curve. This function MUST NOT
286 * be called if the curve is not supported.
287 *
288 * \param curve curve identifier.
289 * \param len receiver for the encoded generator length (in bytes).
290 * \return the encoded generator.
291 */
292 const unsigned char *(*generator)(int curve, size_t *len);
293
294 /**
295 * \brief Get the subgroup order.
296 *
297 * This function returns the order of the subgroup generated by
298 * the conventional generator, for the specified curve. Unsigned
299 * big-endian encoding is used. This function MUST NOT be called
300 * if the curve is not supported.
301 *
302 * \param curve curve identifier.
303 * \param len receiver for the encoded order length (in bytes).
304 * \return the encoded order.
305 */
306 const unsigned char *(*order)(int curve, size_t *len);
307
308 /**
309 * \brief Get the offset and length for the X coordinate.
310 *
311 * This function returns the offset and length (in bytes) of
312 * the X coordinate in an encoded non-zero point.
313 *
314 * \param curve curve identifier.
315 * \param len receiver for the X coordinate length (in bytes).
316 * \return the offset for the X coordinate (in bytes).
317 */
318 size_t (*xoff)(int curve, size_t *len);
319
320 /**
321 * \brief Multiply a curve point by an integer.
322 *
323 * The source point is provided in array `G` (of size `Glen` bytes);
324 * the multiplication result is written over it. The multiplier
325 * `x` (of size `xlen` bytes) uses unsigned big-endian encoding.
326 *
327 * Rules:
328 *
329 * - The specified curve MUST be supported.
330 *
331 * - The source point must be a valid point on the relevant curve
332 * subgroup (and not the "point at infinity" either). If this is
333 * not the case, then this function returns an error (0).
334 *
335 * - The multiplier integer MUST be non-zero and less than the
336 * curve subgroup order. If this property does not hold, then
337 * the result is indeterminate and an error code is not
338 * guaranteed.
339 *
340 * Returned value is 1 on success, 0 on error. On error, the
341 * contents of `G` are indeterminate.
342 *
343 * \param G point to multiply.
344 * \param Glen length of the encoded point (in bytes).
345 * \param x multiplier (unsigned big-endian).
346 * \param xlen multiplier length (in bytes).
347 * \param curve curve identifier.
348 * \return 1 on success, 0 on error.
349 */
350 uint32_t (*mul)(unsigned char *G, size_t Glen,
351 const unsigned char *x, size_t xlen, int curve);
352
353 /**
354 * \brief Multiply the generator by an integer.
355 *
356 * The multiplier MUST be non-zero and less than the curve
357 * subgroup order. Results are indeterminate if this property
358 * does not hold.
359 *
360 * \param R output buffer for the point.
361 * \param x multiplier (unsigned big-endian).
362 * \param xlen multiplier length (in bytes).
363 * \param curve curve identifier.
364 * \return encoded result point length (in bytes).
365 */
366 size_t (*mulgen)(unsigned char *R,
367 const unsigned char *x, size_t xlen, int curve);
368
369 /**
370 * \brief Multiply two points by two integers and add the
371 * results.
372 *
373 * The point `x*A + y*B` is computed and written back in the `A`
374 * array.
375 *
376 * Rules:
377 *
378 * - The specified curve MUST be supported.
379 *
380 * - The source points (`A` and `B`) must be valid points on
381 * the relevant curve subgroup (and not the "point at
382 * infinity" either). If this is not the case, then this
383 * function returns an error (0).
384 *
385 * - If the `B` pointer is `NULL`, then the conventional
386 * subgroup generator is used. With some implementations,
387 * this may be faster than providing a pointer to the
388 * generator.
389 *
390 * - The multiplier integers (`x` and `y`) MUST be non-zero
391 * and less than the curve subgroup order. If either integer
392 * is zero, then an error is reported, but if one of them is
393 * not lower than the subgroup order, then the result is
394 * indeterminate and an error code is not guaranteed.
395 *
396 * - If the final result is the point at infinity, then an
397 * error is returned.
398 *
399 * Returned value is 1 on success, 0 on error. On error, the
400 * contents of `A` are indeterminate.
401 *
402 * \param A first point to multiply.
403 * \param B second point to multiply (`NULL` for the generator).
404 * \param len common length of the encoded points (in bytes).
405 * \param x multiplier for `A` (unsigned big-endian).
406 * \param xlen length of multiplier for `A` (in bytes).
407 * \param y multiplier for `A` (unsigned big-endian).
408 * \param ylen length of multiplier for `A` (in bytes).
409 * \param curve curve identifier.
410 * \return 1 on success, 0 on error.
411 */
412 uint32_t (*muladd)(unsigned char *A, const unsigned char *B, size_t len,
413 const unsigned char *x, size_t xlen,
414 const unsigned char *y, size_t ylen, int curve);
415 } br_ec_impl;
416
417 /**
418 * \brief EC implementation "i31".
419 *
420 * This implementation internally uses generic code for modular integers,
421 * with a representation as sequences of 31-bit words. It supports secp256r1,
422 * secp384r1 and secp521r1 (aka NIST curves P-256, P-384 and P-521).
423 */
424 extern const br_ec_impl br_ec_prime_i31;
425
426 /**
427 * \brief EC implementation "i15".
428 *
429 * This implementation internally uses generic code for modular integers,
430 * with a representation as sequences of 15-bit words. It supports secp256r1,
431 * secp384r1 and secp521r1 (aka NIST curves P-256, P-384 and P-521).
432 */
433 extern const br_ec_impl br_ec_prime_i15;
434
435 /**
436 * \brief EC implementation "m15" for P-256.
437 *
438 * This implementation uses specialised code for curve secp256r1 (also
439 * known as NIST P-256), with optional Karatsuba decomposition, and fast
440 * modular reduction thanks to the field modulus special format. Only
441 * 32-bit multiplications are used (with 32-bit results, not 64-bit).
442 */
443 extern const br_ec_impl br_ec_p256_m15;
444
445 /**
446 * \brief EC implementation "m31" for P-256.
447 *
448 * This implementation uses specialised code for curve secp256r1 (also
449 * known as NIST P-256), relying on multiplications of 31-bit values
450 * (MUL31).
451 */
452 extern const br_ec_impl br_ec_p256_m31;
453
454 /**
455 * \brief EC implementation "i15" (generic code) for Curve25519.
456 *
457 * This implementation uses the generic code for modular integers (with
458 * 15-bit words) to support Curve25519. Due to the specificities of the
459 * curve definition, the following applies:
460 *
461 * - `muladd()` is not implemented (the function returns 0 systematically).
462 * - `order()` returns 2^255-1, since the point multiplication algorithm
463 * accepts any 32-bit integer as input (it clears the top bit and low
464 * three bits systematically).
465 */
466 extern const br_ec_impl br_ec_c25519_i15;
467
468 /**
469 * \brief EC implementation "i31" (generic code) for Curve25519.
470 *
471 * This implementation uses the generic code for modular integers (with
472 * 31-bit words) to support Curve25519. Due to the specificities of the
473 * curve definition, the following applies:
474 *
475 * - `muladd()` is not implemented (the function returns 0 systematically).
476 * - `order()` returns 2^255-1, since the point multiplication algorithm
477 * accepts any 32-bit integer as input (it clears the top bit and low
478 * three bits systematically).
479 */
480 extern const br_ec_impl br_ec_c25519_i31;
481
482 /**
483 * \brief EC implementation "m15" (specialised code) for Curve25519.
484 *
485 * This implementation uses custom code relying on multiplication of
486 * integers up to 15 bits. Due to the specificities of the curve
487 * definition, the following applies:
488 *
489 * - `muladd()` is not implemented (the function returns 0 systematically).
490 * - `order()` returns 2^255-1, since the point multiplication algorithm
491 * accepts any 32-bit integer as input (it clears the top bit and low
492 * three bits systematically).
493 */
494 extern const br_ec_impl br_ec_c25519_m15;
495
496 /**
497 * \brief EC implementation "m31" (specialised code) for Curve25519.
498 *
499 * This implementation uses custom code relying on multiplication of
500 * integers up to 31 bits. Due to the specificities of the curve
501 * definition, the following applies:
502 *
503 * - `muladd()` is not implemented (the function returns 0 systematically).
504 * - `order()` returns 2^255-1, since the point multiplication algorithm
505 * accepts any 32-bit integer as input (it clears the top bit and low
506 * three bits systematically).
507 */
508 extern const br_ec_impl br_ec_c25519_m31;
509
510 /**
511 * \brief EC implementation "m62" (specialised code) for Curve25519.
512 *
513 * This implementation uses custom code relying on multiplication of
514 * integers up to 62 bits, with a 124-bit result. This implementation is
515 * defined only on platforms that offer the 64x64->128 multiplication
516 * support; use `br_ec_c25519_m62_get()` to dynamically obtain a pointer
517 * to that implementation. Due to the specificities of the curve
518 * definition, the following applies:
519 *
520 * - `muladd()` is not implemented (the function returns 0 systematically).
521 * - `order()` returns 2^255-1, since the point multiplication algorithm
522 * accepts any 32-bit integer as input (it clears the top bit and low
523 * three bits systematically).
524 */
525 extern const br_ec_impl br_ec_c25519_m62;
526
527 /**
528 * \brief Get the "m62" implementation of Curve25519, if available.
529 *
530 * \return the implementation, or 0.
531 */
532 const br_ec_impl *br_ec_c25519_m62_get(void);
533
534 /**
535 * \brief Aggregate EC implementation "m15".
536 *
537 * This implementation is a wrapper for:
538 *
539 * - `br_ec_c25519_m15` for Curve25519
540 * - `br_ec_p256_m15` for NIST P-256
541 * - `br_ec_prime_i15` for other curves (NIST P-384 and NIST-P512)
542 */
543 extern const br_ec_impl br_ec_all_m15;
544
545 /**
546 * \brief Aggregate EC implementation "m31".
547 *
548 * This implementation is a wrapper for:
549 *
550 * - `br_ec_c25519_m31` for Curve25519
551 * - `br_ec_p256_m31` for NIST P-256
552 * - `br_ec_prime_i31` for other curves (NIST P-384 and NIST-P512)
553 */
554 extern const br_ec_impl br_ec_all_m31;
555
556 /**
557 * \brief Get the "default" EC implementation for the current system.
558 *
559 * This returns a pointer to the preferred implementation on the
560 * current system.
561 *
562 * \return the default EC implementation.
563 */
564 const br_ec_impl *br_ec_get_default(void);
565
566 /**
567 * \brief Convert a signature from "raw" to "asn1".
568 *
569 * Conversion is done "in place" and the new length is returned.
570 * Conversion may enlarge the signature, but by no more than 9 bytes at
571 * most. On error, 0 is returned (error conditions include an odd raw
572 * signature length, or an oversized integer).
573 *
574 * \param sig signature to convert.
575 * \param sig_len signature length (in bytes).
576 * \return the new signature length, or 0 on error.
577 */
578 size_t br_ecdsa_raw_to_asn1(void *sig, size_t sig_len);
579
580 /**
581 * \brief Convert a signature from "asn1" to "raw".
582 *
583 * Conversion is done "in place" and the new length is returned.
584 * Conversion may enlarge the signature, but the new signature length
585 * will be less than twice the source length at most. On error, 0 is
586 * returned (error conditions include an invalid ASN.1 structure or an
587 * oversized integer).
588 *
589 * \param sig signature to convert.
590 * \param sig_len signature length (in bytes).
591 * \return the new signature length, or 0 on error.
592 */
593 size_t br_ecdsa_asn1_to_raw(void *sig, size_t sig_len);
594
595 /**
596 * \brief Type for an ECDSA signer function.
597 *
598 * A pointer to the EC implementation is provided. The hash value is
599 * assumed to have the length inferred from the designated hash function
600 * class.
601 *
602 * Signature is written in the buffer pointed to by `sig`, and the length
603 * (in bytes) is returned. On error, nothing is written in the buffer,
604 * and 0 is returned. This function returns 0 if the specified curve is
605 * not supported by the provided EC implementation.
606 *
607 * The signature format is either "raw" or "asn1", depending on the
608 * implementation; maximum length is predictable from the implemented
609 * curve:
610 *
611 * | curve | raw | asn1 |
612 * | :--------- | --: | ---: |
613 * | NIST P-256 | 64 | 72 |
614 * | NIST P-384 | 96 | 104 |
615 * | NIST P-521 | 132 | 139 |
616 *
617 * \param impl EC implementation to use.
618 * \param hf hash function used to process the data.
619 * \param hash_value signed data (hashed).
620 * \param sk EC private key.
621 * \param sig destination buffer.
622 * \return the signature length (in bytes), or 0 on error.
623 */
624 typedef size_t (*br_ecdsa_sign)(const br_ec_impl *impl,
625 const br_hash_class *hf, const void *hash_value,
626 const br_ec_private_key *sk, void *sig);
627
628 /**
629 * \brief Type for an ECDSA signature verification function.
630 *
631 * A pointer to the EC implementation is provided. The hashed value,
632 * computed over the purportedly signed data, is also provided with
633 * its length.
634 *
635 * The signature format is either "raw" or "asn1", depending on the
636 * implementation.
637 *
638 * Returned value is 1 on success (valid signature), 0 on error. This
639 * function returns 0 if the specified curve is not supported by the
640 * provided EC implementation.
641 *
642 * \param impl EC implementation to use.
643 * \param hash signed data (hashed).
644 * \param hash_len hash value length (in bytes).
645 * \param pk EC public key.
646 * \param sig signature.
647 * \param sig_len signature length (in bytes).
648 * \return 1 on success, 0 on error.
649 */
650 typedef uint32_t (*br_ecdsa_vrfy)(const br_ec_impl *impl,
651 const void *hash, size_t hash_len,
652 const br_ec_public_key *pk, const void *sig, size_t sig_len);
653
654 /**
655 * \brief ECDSA signature generator, "i31" implementation, "asn1" format.
656 *
657 * \see br_ecdsa_sign()
658 *
659 * \param impl EC implementation to use.
660 * \param hf hash function used to process the data.
661 * \param hash_value signed data (hashed).
662 * \param sk EC private key.
663 * \param sig destination buffer.
664 * \return the signature length (in bytes), or 0 on error.
665 */
666 size_t br_ecdsa_i31_sign_asn1(const br_ec_impl *impl,
667 const br_hash_class *hf, const void *hash_value,
668 const br_ec_private_key *sk, void *sig);
669
670 /**
671 * \brief ECDSA signature generator, "i31" implementation, "raw" format.
672 *
673 * \see br_ecdsa_sign()
674 *
675 * \param impl EC implementation to use.
676 * \param hf hash function used to process the data.
677 * \param hash_value signed data (hashed).
678 * \param sk EC private key.
679 * \param sig destination buffer.
680 * \return the signature length (in bytes), or 0 on error.
681 */
682 size_t br_ecdsa_i31_sign_raw(const br_ec_impl *impl,
683 const br_hash_class *hf, const void *hash_value,
684 const br_ec_private_key *sk, void *sig);
685
686 /**
687 * \brief ECDSA signature verifier, "i31" implementation, "asn1" format.
688 *
689 * \see br_ecdsa_vrfy()
690 *
691 * \param impl EC implementation to use.
692 * \param hash signed data (hashed).
693 * \param hash_len hash value length (in bytes).
694 * \param pk EC public key.
695 * \param sig signature.
696 * \param sig_len signature length (in bytes).
697 * \return 1 on success, 0 on error.
698 */
699 uint32_t br_ecdsa_i31_vrfy_asn1(const br_ec_impl *impl,
700 const void *hash, size_t hash_len,
701 const br_ec_public_key *pk, const void *sig, size_t sig_len);
702
703 /**
704 * \brief ECDSA signature verifier, "i31" implementation, "raw" format.
705 *
706 * \see br_ecdsa_vrfy()
707 *
708 * \param impl EC implementation to use.
709 * \param hash signed data (hashed).
710 * \param hash_len hash value length (in bytes).
711 * \param pk EC public key.
712 * \param sig signature.
713 * \param sig_len signature length (in bytes).
714 * \return 1 on success, 0 on error.
715 */
716 uint32_t br_ecdsa_i31_vrfy_raw(const br_ec_impl *impl,
717 const void *hash, size_t hash_len,
718 const br_ec_public_key *pk, const void *sig, size_t sig_len);
719
720 /**
721 * \brief ECDSA signature generator, "i15" implementation, "asn1" format.
722 *
723 * \see br_ecdsa_sign()
724 *
725 * \param impl EC implementation to use.
726 * \param hf hash function used to process the data.
727 * \param hash_value signed data (hashed).
728 * \param sk EC private key.
729 * \param sig destination buffer.
730 * \return the signature length (in bytes), or 0 on error.
731 */
732 size_t br_ecdsa_i15_sign_asn1(const br_ec_impl *impl,
733 const br_hash_class *hf, const void *hash_value,
734 const br_ec_private_key *sk, void *sig);
735
736 /**
737 * \brief ECDSA signature generator, "i15" implementation, "raw" format.
738 *
739 * \see br_ecdsa_sign()
740 *
741 * \param impl EC implementation to use.
742 * \param hf hash function used to process the data.
743 * \param hash_value signed data (hashed).
744 * \param sk EC private key.
745 * \param sig destination buffer.
746 * \return the signature length (in bytes), or 0 on error.
747 */
748 size_t br_ecdsa_i15_sign_raw(const br_ec_impl *impl,
749 const br_hash_class *hf, const void *hash_value,
750 const br_ec_private_key *sk, void *sig);
751
752 /**
753 * \brief ECDSA signature verifier, "i15" implementation, "asn1" format.
754 *
755 * \see br_ecdsa_vrfy()
756 *
757 * \param impl EC implementation to use.
758 * \param hash signed data (hashed).
759 * \param hash_len hash value length (in bytes).
760 * \param pk EC public key.
761 * \param sig signature.
762 * \param sig_len signature length (in bytes).
763 * \return 1 on success, 0 on error.
764 */
765 uint32_t br_ecdsa_i15_vrfy_asn1(const br_ec_impl *impl,
766 const void *hash, size_t hash_len,
767 const br_ec_public_key *pk, const void *sig, size_t sig_len);
768
769 /**
770 * \brief ECDSA signature verifier, "i15" implementation, "raw" format.
771 *
772 * \see br_ecdsa_vrfy()
773 *
774 * \param impl EC implementation to use.
775 * \param hash signed data (hashed).
776 * \param hash_len hash value length (in bytes).
777 * \param pk EC public key.
778 * \param sig signature.
779 * \param sig_len signature length (in bytes).
780 * \return 1 on success, 0 on error.
781 */
782 uint32_t br_ecdsa_i15_vrfy_raw(const br_ec_impl *impl,
783 const void *hash, size_t hash_len,
784 const br_ec_public_key *pk, const void *sig, size_t sig_len);
785
786 /**
787 * \brief Get "default" ECDSA implementation (signer, asn1 format).
788 *
789 * This returns the preferred implementation of ECDSA signature generation
790 * ("asn1" output format) on the current system.
791 *
792 * \return the default implementation.
793 */
794 br_ecdsa_sign br_ecdsa_sign_asn1_get_default(void);
795
796 /**
797 * \brief Get "default" ECDSA implementation (signer, raw format).
798 *
799 * This returns the preferred implementation of ECDSA signature generation
800 * ("raw" output format) on the current system.
801 *
802 * \return the default implementation.
803 */
804 br_ecdsa_sign br_ecdsa_sign_raw_get_default(void);
805
806 /**
807 * \brief Get "default" ECDSA implementation (verifier, asn1 format).
808 *
809 * This returns the preferred implementation of ECDSA signature verification
810 * ("asn1" output format) on the current system.
811 *
812 * \return the default implementation.
813 */
814 br_ecdsa_vrfy br_ecdsa_vrfy_asn1_get_default(void);
815
816 /**
817 * \brief Get "default" ECDSA implementation (verifier, raw format).
818 *
819 * This returns the preferred implementation of ECDSA signature verification
820 * ("raw" output format) on the current system.
821 *
822 * \return the default implementation.
823 */
824 br_ecdsa_vrfy br_ecdsa_vrfy_raw_get_default(void);
825
826 /**
827 * \brief Maximum size for EC private key element buffer.
828 *
829 * This is the largest number of bytes that `br_ec_keygen()` may need or
830 * ever return.
831 */
832 #define BR_EC_KBUF_PRIV_MAX_SIZE 72
833
834 /**
835 * \brief Maximum size for EC public key element buffer.
836 *
837 * This is the largest number of bytes that `br_ec_compute_public()` may
838 * need or ever return.
839 */
840 #define BR_EC_KBUF_PUB_MAX_SIZE 145
841
842 /**
843 * \brief Generate a new EC private key.
844 *
845 * If the specified `curve` is not supported by the elliptic curve
846 * implementation (`impl`), then this function returns zero.
847 *
848 * The `sk` structure fields are set to the new private key data. In
849 * particular, `sk.x` is made to point to the provided key buffer (`kbuf`),
850 * in which the actual private key data is written. That buffer is assumed
851 * to be large enough. The `BR_EC_KBUF_PRIV_MAX_SIZE` defines the maximum
852 * size for all supported curves.
853 *
854 * The number of bytes used in `kbuf` is returned. If `kbuf` is `NULL`, then
855 * the private key is not actually generated, and `sk` may also be `NULL`;
856 * the minimum length for `kbuf` is still computed and returned.
857 *
858 * If `sk` is `NULL` but `kbuf` is not `NULL`, then the private key is
859 * still generated and stored in `kbuf`.
860 *
861 * \param rng_ctx source PRNG context (already initialized).
862 * \param impl the elliptic curve implementation.
863 * \param sk the private key structure to fill, or `NULL`.
864 * \param kbuf the key element buffer, or `NULL`.
865 * \param curve the curve identifier.
866 * \return the key data length (in bytes), or zero.
867 */
868 size_t br_ec_keygen(const br_prng_class **rng_ctx,
869 const br_ec_impl *impl, br_ec_private_key *sk,
870 void *kbuf, int curve);
871
872 /**
873 * \brief Compute EC public key from EC private key.
874 *
875 * This function uses the provided elliptic curve implementation (`impl`)
876 * to compute the public key corresponding to the private key held in `sk`.
877 * The public key point is written into `kbuf`, which is then linked from
878 * the `*pk` structure. The size of the public key point, i.e. the number
879 * of bytes used in `kbuf`, is returned.
880 *
881 * If `kbuf` is `NULL`, then the public key point is NOT computed, and
882 * the public key structure `*pk` is unmodified (`pk` may be `NULL` in
883 * that case). The size of the public key point is still returned.
884 *
885 * If `pk` is `NULL` but `kbuf` is not `NULL`, then the public key
886 * point is computed and stored in `kbuf`, and its size is returned.
887 *
888 * If the curve used by the private key is not supported by the curve
889 * implementation, then this function returns zero.
890 *
891 * The private key MUST be valid. An off-range private key value is not
892 * necessarily detected, and leads to unpredictable results.
893 *
894 * \param impl the elliptic curve implementation.
895 * \param pk the public key structure to fill (or `NULL`).
896 * \param kbuf the public key point buffer (or `NULL`).
897 * \param sk the source private key.
898 * \return the public key point length (in bytes), or zero.
899 */
900 size_t br_ec_compute_pub(const br_ec_impl *impl, br_ec_public_key *pk,
901 void *kbuf, const br_ec_private_key *sk);
902
903 #ifdef __cplusplus
904 }
905 #endif
906
907 #endif