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_EC_H__
26 #define BR_BEARSSL_EC_H__
31 #include "bearssl_rand.h"
37 /** \file bearssl_ec.h
41 * This file documents the EC implementations provided with BearSSL, and
44 * ## Elliptic Curve API
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
50 * [IANA](http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8).
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.
56 * An EC implementation is incarnated by a `br_ec_impl` instance, that
57 * offers the following fields:
59 * - `supported_curves`
61 * A 32-bit word that documents the identifiers of the curves supported
62 * by this implementation.
66 * Callback method that returns a pointer to the conventional generator
67 * point for that curve.
71 * Callback method that returns a pointer to the subgroup order for
72 * that curve. That value uses unsigned big-endian encoding.
76 * Callback method that returns the offset and length of the X
77 * coordinate in an encoded point.
81 * Multiply a curve point with an integer.
85 * Multiply the curve generator with an integer. This may be faster
86 * than the generic `mul()`.
90 * Multiply two curve points by two integers, and return the sum of
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.
97 * For all point multiplication functions, the following holds:
99 * - Functions validate that the provided points are valid members
100 * of the relevant curve subgroup. An error is reported if that is
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.
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.
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
124 * ECDSASignature ::= SEQUENCE {
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).
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.
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
148 /** \brief Identifier for named curve sect163k1. */
149 #define BR_EC_sect163k1 1
151 /** \brief Identifier for named curve sect163r1. */
152 #define BR_EC_sect163r1 2
154 /** \brief Identifier for named curve sect163r2. */
155 #define BR_EC_sect163r2 3
157 /** \brief Identifier for named curve sect193r1. */
158 #define BR_EC_sect193r1 4
160 /** \brief Identifier for named curve sect193r2. */
161 #define BR_EC_sect193r2 5
163 /** \brief Identifier for named curve sect233k1. */
164 #define BR_EC_sect233k1 6
166 /** \brief Identifier for named curve sect233r1. */
167 #define BR_EC_sect233r1 7
169 /** \brief Identifier for named curve sect239k1. */
170 #define BR_EC_sect239k1 8
172 /** \brief Identifier for named curve sect283k1. */
173 #define BR_EC_sect283k1 9
175 /** \brief Identifier for named curve sect283r1. */
176 #define BR_EC_sect283r1 10
178 /** \brief Identifier for named curve sect409k1. */
179 #define BR_EC_sect409k1 11
181 /** \brief Identifier for named curve sect409r1. */
182 #define BR_EC_sect409r1 12
184 /** \brief Identifier for named curve sect571k1. */
185 #define BR_EC_sect571k1 13
187 /** \brief Identifier for named curve sect571r1. */
188 #define BR_EC_sect571r1 14
190 /** \brief Identifier for named curve secp160k1. */
191 #define BR_EC_secp160k1 15
193 /** \brief Identifier for named curve secp160r1. */
194 #define BR_EC_secp160r1 16
196 /** \brief Identifier for named curve secp160r2. */
197 #define BR_EC_secp160r2 17
199 /** \brief Identifier for named curve secp192k1. */
200 #define BR_EC_secp192k1 18
202 /** \brief Identifier for named curve secp192r1. */
203 #define BR_EC_secp192r1 19
205 /** \brief Identifier for named curve secp224k1. */
206 #define BR_EC_secp224k1 20
208 /** \brief Identifier for named curve secp224r1. */
209 #define BR_EC_secp224r1 21
211 /** \brief Identifier for named curve secp256k1. */
212 #define BR_EC_secp256k1 22
214 /** \brief Identifier for named curve secp256r1. */
215 #define BR_EC_secp256r1 23
217 /** \brief Identifier for named curve secp384r1. */
218 #define BR_EC_secp384r1 24
220 /** \brief Identifier for named curve secp521r1. */
221 #define BR_EC_secp521r1 25
223 /** \brief Identifier for named curve brainpoolP256r1. */
224 #define BR_EC_brainpoolP256r1 26
226 /** \brief Identifier for named curve brainpoolP384r1. */
227 #define BR_EC_brainpoolP384r1 27
229 /** \brief Identifier for named curve brainpoolP512r1. */
230 #define BR_EC_brainpoolP512r1 28
232 /** \brief Identifier for named curve Curve25519. */
233 #define BR_EC_curve25519 29
235 /** \brief Identifier for named curve Curve448. */
236 #define BR_EC_curve448 30
239 * \brief Structure for an EC public key.
242 /** \brief Identifier for the curve used by this key. */
244 /** \brief Public curve point (uncompressed format). */
246 /** \brief Length of public curve point (in bytes). */
251 * \brief Structure for an EC private key.
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
259 /** \brief Identifier for the curve used by this key. */
261 /** \brief Private key (integer, unsigned big-endian encoding). */
263 /** \brief Private key length (in bytes). */
268 * \brief Type for an EC implementation.
272 * \brief Supported curves.
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.
279 uint32_t supported_curves
;
282 * \brief Get the conventional generator.
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.
288 * \param curve curve identifier.
289 * \param len receiver for the encoded generator length (in bytes).
290 * \return the encoded generator.
292 const unsigned char *(*generator
)(int curve
, size_t *len
);
295 * \brief Get the subgroup order.
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.
302 * \param curve curve identifier.
303 * \param len receiver for the encoded order length (in bytes).
304 * \return the encoded order.
306 const unsigned char *(*order
)(int curve
, size_t *len
);
309 * \brief Get the offset and length for the X coordinate.
311 * This function returns the offset and length (in bytes) of
312 * the X coordinate in an encoded non-zero point.
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).
318 size_t (*xoff
)(int curve
, size_t *len
);
321 * \brief Multiply a curve point by an integer.
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.
329 * - The specified curve MUST be supported.
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).
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
340 * Returned value is 1 on success, 0 on error. On error, the
341 * contents of `G` are indeterminate.
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.
350 uint32_t (*mul
)(unsigned char *G
, size_t Glen
,
351 const unsigned char *x
, size_t xlen
, int curve
);
354 * \brief Multiply the generator by an integer.
356 * The multiplier MUST be non-zero and less than the curve
357 * subgroup order. Results are indeterminate if this property
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).
366 size_t (*mulgen
)(unsigned char *R
,
367 const unsigned char *x
, size_t xlen
, int curve
);
370 * \brief Multiply two points by two integers and add the
373 * The point `x*A + y*B` is computed and written back in the `A`
378 * - The specified curve MUST be supported.
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).
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
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.
396 * - If the final result is the point at infinity, then an
399 * Returned value is 1 on success, 0 on error. On error, the
400 * contents of `A` are indeterminate.
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.
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
);
418 * \brief EC implementation "i31".
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).
424 extern const br_ec_impl br_ec_prime_i31
;
427 * \brief EC implementation "i15".
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).
433 extern const br_ec_impl br_ec_prime_i15
;
436 * \brief EC implementation "m15" for P-256.
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).
443 extern const br_ec_impl br_ec_p256_m15
;
446 * \brief EC implementation "m31" for P-256.
448 * This implementation uses specialised code for curve secp256r1 (also
449 * known as NIST P-256), relying on multiplications of 31-bit values
452 extern const br_ec_impl br_ec_p256_m31
;
455 * \brief EC implementation "i15" (generic code) for Curve25519.
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:
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).
466 extern const br_ec_impl br_ec_c25519_i15
;
469 * \brief EC implementation "i31" (generic code) for Curve25519.
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:
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).
480 extern const br_ec_impl br_ec_c25519_i31
;
483 * \brief EC implementation "m15" (specialised code) for Curve25519.
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:
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).
494 extern const br_ec_impl br_ec_c25519_m15
;
497 * \brief EC implementation "m31" (specialised code) for Curve25519.
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:
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).
508 extern const br_ec_impl br_ec_c25519_m31
;
511 * \brief Aggregate EC implementation "m15".
513 * This implementation is a wrapper for:
515 * - `br_ec_c25519_m15` for Curve25519
516 * - `br_ec_p256_m15` for NIST P-256
517 * - `br_ec_prime_i15` for other curves (NIST P-384 and NIST-P512)
519 extern const br_ec_impl br_ec_all_m15
;
522 * \brief Aggregate EC implementation "m31".
524 * This implementation is a wrapper for:
526 * - `br_ec_c25519_m31` for Curve25519
527 * - `br_ec_p256_m31` for NIST P-256
528 * - `br_ec_prime_i31` for other curves (NIST P-384 and NIST-P512)
530 extern const br_ec_impl br_ec_all_m31
;
533 * \brief Get the "default" EC implementation for the current system.
535 * This returns a pointer to the preferred implementation on the
538 * \return the default EC implementation.
540 const br_ec_impl
*br_ec_get_default(void);
543 * \brief Convert a signature from "raw" to "asn1".
545 * Conversion is done "in place" and the new length is returned.
546 * Conversion may enlarge the signature, but by no more than 9 bytes at
547 * most. On error, 0 is returned (error conditions include an odd raw
548 * signature length, or an oversized integer).
550 * \param sig signature to convert.
551 * \param sig_len signature length (in bytes).
552 * \return the new signature length, or 0 on error.
554 size_t br_ecdsa_raw_to_asn1(void *sig
, size_t sig_len
);
557 * \brief Convert a signature from "asn1" to "raw".
559 * Conversion is done "in place" and the new length is returned.
560 * Conversion may enlarge the signature, but the new signature length
561 * will be less than twice the source length at most. On error, 0 is
562 * returned (error conditions include an invalid ASN.1 structure or an
563 * oversized integer).
565 * \param sig signature to convert.
566 * \param sig_len signature length (in bytes).
567 * \return the new signature length, or 0 on error.
569 size_t br_ecdsa_asn1_to_raw(void *sig
, size_t sig_len
);
572 * \brief Type for an ECDSA signer function.
574 * A pointer to the EC implementation is provided. The hash value is
575 * assumed to have the length inferred from the designated hash function
578 * Signature is written in the buffer pointed to by `sig`, and the length
579 * (in bytes) is returned. On error, nothing is written in the buffer,
580 * and 0 is returned. This function returns 0 if the specified curve is
581 * not supported by the provided EC implementation.
583 * The signature format is either "raw" or "asn1", depending on the
584 * implementation; maximum length is predictable from the implemented
587 * | curve | raw | asn1 |
588 * | :--------- | --: | ---: |
589 * | NIST P-256 | 64 | 72 |
590 * | NIST P-384 | 96 | 104 |
591 * | NIST P-521 | 132 | 139 |
593 * \param impl EC implementation to use.
594 * \param hf hash function used to process the data.
595 * \param hash_value signed data (hashed).
596 * \param sk EC private key.
597 * \param sig destination buffer.
598 * \return the signature length (in bytes), or 0 on error.
600 typedef size_t (*br_ecdsa_sign
)(const br_ec_impl
*impl
,
601 const br_hash_class
*hf
, const void *hash_value
,
602 const br_ec_private_key
*sk
, void *sig
);
605 * \brief Type for an ECDSA signature verification function.
607 * A pointer to the EC implementation is provided. The hashed value,
608 * computed over the purportedly signed data, is also provided with
611 * The signature format is either "raw" or "asn1", depending on the
614 * Returned value is 1 on success (valid signature), 0 on error. This
615 * function returns 0 if the specified curve is not supported by the
616 * provided EC implementation.
618 * \param impl EC implementation to use.
619 * \param hash signed data (hashed).
620 * \param hash_len hash value length (in bytes).
621 * \param pk EC public key.
622 * \param sig signature.
623 * \param sig_len signature length (in bytes).
624 * \return 1 on success, 0 on error.
626 typedef uint32_t (*br_ecdsa_vrfy
)(const br_ec_impl
*impl
,
627 const void *hash
, size_t hash_len
,
628 const br_ec_public_key
*pk
, const void *sig
, size_t sig_len
);
631 * \brief ECDSA signature generator, "i31" implementation, "asn1" format.
633 * \see br_ecdsa_sign()
635 * \param impl EC implementation to use.
636 * \param hf hash function used to process the data.
637 * \param hash_value signed data (hashed).
638 * \param sk EC private key.
639 * \param sig destination buffer.
640 * \return the signature length (in bytes), or 0 on error.
642 size_t br_ecdsa_i31_sign_asn1(const br_ec_impl
*impl
,
643 const br_hash_class
*hf
, const void *hash_value
,
644 const br_ec_private_key
*sk
, void *sig
);
647 * \brief ECDSA signature generator, "i31" implementation, "raw" format.
649 * \see br_ecdsa_sign()
651 * \param impl EC implementation to use.
652 * \param hf hash function used to process the data.
653 * \param hash_value signed data (hashed).
654 * \param sk EC private key.
655 * \param sig destination buffer.
656 * \return the signature length (in bytes), or 0 on error.
658 size_t br_ecdsa_i31_sign_raw(const br_ec_impl
*impl
,
659 const br_hash_class
*hf
, const void *hash_value
,
660 const br_ec_private_key
*sk
, void *sig
);
663 * \brief ECDSA signature verifier, "i31" implementation, "asn1" format.
665 * \see br_ecdsa_vrfy()
667 * \param impl EC implementation to use.
668 * \param hash signed data (hashed).
669 * \param hash_len hash value length (in bytes).
670 * \param pk EC public key.
671 * \param sig signature.
672 * \param sig_len signature length (in bytes).
673 * \return 1 on success, 0 on error.
675 uint32_t br_ecdsa_i31_vrfy_asn1(const br_ec_impl
*impl
,
676 const void *hash
, size_t hash_len
,
677 const br_ec_public_key
*pk
, const void *sig
, size_t sig_len
);
680 * \brief ECDSA signature verifier, "i31" implementation, "raw" format.
682 * \see br_ecdsa_vrfy()
684 * \param impl EC implementation to use.
685 * \param hash signed data (hashed).
686 * \param hash_len hash value length (in bytes).
687 * \param pk EC public key.
688 * \param sig signature.
689 * \param sig_len signature length (in bytes).
690 * \return 1 on success, 0 on error.
692 uint32_t br_ecdsa_i31_vrfy_raw(const br_ec_impl
*impl
,
693 const void *hash
, size_t hash_len
,
694 const br_ec_public_key
*pk
, const void *sig
, size_t sig_len
);
697 * \brief ECDSA signature generator, "i15" implementation, "asn1" format.
699 * \see br_ecdsa_sign()
701 * \param impl EC implementation to use.
702 * \param hf hash function used to process the data.
703 * \param hash_value signed data (hashed).
704 * \param sk EC private key.
705 * \param sig destination buffer.
706 * \return the signature length (in bytes), or 0 on error.
708 size_t br_ecdsa_i15_sign_asn1(const br_ec_impl
*impl
,
709 const br_hash_class
*hf
, const void *hash_value
,
710 const br_ec_private_key
*sk
, void *sig
);
713 * \brief ECDSA signature generator, "i15" implementation, "raw" format.
715 * \see br_ecdsa_sign()
717 * \param impl EC implementation to use.
718 * \param hf hash function used to process the data.
719 * \param hash_value signed data (hashed).
720 * \param sk EC private key.
721 * \param sig destination buffer.
722 * \return the signature length (in bytes), or 0 on error.
724 size_t br_ecdsa_i15_sign_raw(const br_ec_impl
*impl
,
725 const br_hash_class
*hf
, const void *hash_value
,
726 const br_ec_private_key
*sk
, void *sig
);
729 * \brief ECDSA signature verifier, "i15" implementation, "asn1" format.
731 * \see br_ecdsa_vrfy()
733 * \param impl EC implementation to use.
734 * \param hash signed data (hashed).
735 * \param hash_len hash value length (in bytes).
736 * \param pk EC public key.
737 * \param sig signature.
738 * \param sig_len signature length (in bytes).
739 * \return 1 on success, 0 on error.
741 uint32_t br_ecdsa_i15_vrfy_asn1(const br_ec_impl
*impl
,
742 const void *hash
, size_t hash_len
,
743 const br_ec_public_key
*pk
, const void *sig
, size_t sig_len
);
746 * \brief ECDSA signature verifier, "i15" implementation, "raw" format.
748 * \see br_ecdsa_vrfy()
750 * \param impl EC implementation to use.
751 * \param hash signed data (hashed).
752 * \param hash_len hash value length (in bytes).
753 * \param pk EC public key.
754 * \param sig signature.
755 * \param sig_len signature length (in bytes).
756 * \return 1 on success, 0 on error.
758 uint32_t br_ecdsa_i15_vrfy_raw(const br_ec_impl
*impl
,
759 const void *hash
, size_t hash_len
,
760 const br_ec_public_key
*pk
, const void *sig
, size_t sig_len
);
763 * \brief Get "default" ECDSA implementation (signer, asn1 format).
765 * This returns the preferred implementation of ECDSA signature generation
766 * ("asn1" output format) on the current system.
768 * \return the default implementation.
770 br_ecdsa_sign
br_ecdsa_sign_asn1_get_default(void);
773 * \brief Get "default" ECDSA implementation (signer, raw format).
775 * This returns the preferred implementation of ECDSA signature generation
776 * ("raw" output format) on the current system.
778 * \return the default implementation.
780 br_ecdsa_sign
br_ecdsa_sign_raw_get_default(void);
783 * \brief Get "default" ECDSA implementation (verifier, asn1 format).
785 * This returns the preferred implementation of ECDSA signature verification
786 * ("asn1" output format) on the current system.
788 * \return the default implementation.
790 br_ecdsa_vrfy
br_ecdsa_vrfy_asn1_get_default(void);
793 * \brief Get "default" ECDSA implementation (verifier, raw format).
795 * This returns the preferred implementation of ECDSA signature verification
796 * ("raw" output format) on the current system.
798 * \return the default implementation.
800 br_ecdsa_vrfy
br_ecdsa_vrfy_raw_get_default(void);
803 * \brief Maximum size for EC private key element buffer.
805 * This is the largest number of bytes that `br_ec_keygen()` may need or
808 #define BR_EC_KBUF_PRIV_MAX_SIZE 72
811 * \brief Maximum size for EC public key element buffer.
813 * This is the largest number of bytes that `br_ec_compute_public()` may
814 * need or ever return.
816 #define BR_EC_KBUF_PUB_MAX_SIZE 145
819 * \brief Generate a new EC private key.
821 * If the specified `curve` is not supported by the elliptic curve
822 * implementation (`impl`), then this function returns zero.
824 * The `sk` structure fields are set to the new private key data. In
825 * particular, `sk.x` is made to point to the provided key buffer (`kbuf`),
826 * in which the actual private key data is written. That buffer is assumed
827 * to be large enough. The `BR_EC_KBUF_PRIV_MAX_SIZE` defines the maximum
828 * size for all supported curves.
830 * The number of bytes used in `kbuf` is returned. If `kbuf` is `NULL`, then
831 * the private key is not actually generated, and `sk` may also be `NULL`;
832 * the minimum length for `kbuf` is still computed and returned.
834 * If `sk` is `NULL` but `kbuf` is not `NULL`, then the private key is
835 * still generated and stored in `kbuf`.
837 * \param rng_ctx source PRNG context (already initialized).
838 * \param impl the elliptic curve implementation.
839 * \param sk the private key structure to fill, or `NULL`.
840 * \param kbuf the key element buffer, or `NULL`.
841 * \param curve the curve identifier.
842 * \return the key data length (in bytes), or zero.
844 size_t br_ec_keygen(const br_prng_class
**rng_ctx
,
845 const br_ec_impl
*impl
, br_ec_private_key
*sk
,
846 void *kbuf
, int curve
);
849 * \brief Compute EC public key from EC private key.
851 * This function uses the provided elliptic curve implementation (`impl`)
852 * to compute the public key corresponding to the private key held in `sk`.
853 * The public key point is written into `kbuf`, which is then linked from
854 * the `*pk` structure. The size of the public key point, i.e. the number
855 * of bytes used in `kbuf`, is returned.
857 * If `kbuf` is `NULL`, then the public key point is NOT computed, and
858 * the public key structure `*pk` is unmodified (`pk` may be `NULL` in
859 * that case). The size of the public key point is still returned.
861 * If `pk` is `NULL` but `kbuf` is not `NULL`, then the public key
862 * point is computed and stored in `kbuf`, and its size is returned.
864 * If the curve used by the private key is not supported by the curve
865 * implementation, then this function returns zero.
867 * The private key MUST be valid. An off-range private key value is not
868 * necessarily detected, and leads to unpredictable results.
870 * \param impl the elliptic curve implementation.
871 * \param pk the public key structure to fill (or `NULL`).
872 * \param kbuf the public key point buffer (or `NULL`).
873 * \param sk the source private key.
874 * \return the public key point length (in bytes), or zero.
876 size_t br_ec_compute_pub(const br_ec_impl
*impl
, br_ec_public_key
*pk
,
877 void *kbuf
, const br_ec_private_key
*sk
);