xref: /freebsd/contrib/bearssl/inc/bearssl_ec.h (revision cc9e6590773dba57440750c124173ed531349a06)
10957b409SSimon J. Gerraty /*
20957b409SSimon J. Gerraty  * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
30957b409SSimon J. Gerraty  *
40957b409SSimon J. Gerraty  * Permission is hereby granted, free of charge, to any person obtaining
50957b409SSimon J. Gerraty  * a copy of this software and associated documentation files (the
60957b409SSimon J. Gerraty  * "Software"), to deal in the Software without restriction, including
70957b409SSimon J. Gerraty  * without limitation the rights to use, copy, modify, merge, publish,
80957b409SSimon J. Gerraty  * distribute, sublicense, and/or sell copies of the Software, and to
90957b409SSimon J. Gerraty  * permit persons to whom the Software is furnished to do so, subject to
100957b409SSimon J. Gerraty  * the following conditions:
110957b409SSimon J. Gerraty  *
120957b409SSimon J. Gerraty  * The above copyright notice and this permission notice shall be
130957b409SSimon J. Gerraty  * included in all copies or substantial portions of the Software.
140957b409SSimon J. Gerraty  *
150957b409SSimon J. Gerraty  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
160957b409SSimon J. Gerraty  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
170957b409SSimon J. Gerraty  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
180957b409SSimon J. Gerraty  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
190957b409SSimon J. Gerraty  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
200957b409SSimon J. Gerraty  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
210957b409SSimon J. Gerraty  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
220957b409SSimon J. Gerraty  * SOFTWARE.
230957b409SSimon J. Gerraty  */
240957b409SSimon J. Gerraty 
250957b409SSimon J. Gerraty #ifndef BR_BEARSSL_EC_H__
260957b409SSimon J. Gerraty #define BR_BEARSSL_EC_H__
270957b409SSimon J. Gerraty 
280957b409SSimon J. Gerraty #include <stddef.h>
290957b409SSimon J. Gerraty #include <stdint.h>
300957b409SSimon J. Gerraty 
310957b409SSimon J. Gerraty #include "bearssl_rand.h"
320957b409SSimon J. Gerraty 
330957b409SSimon J. Gerraty #ifdef __cplusplus
340957b409SSimon J. Gerraty extern "C" {
350957b409SSimon J. Gerraty #endif
360957b409SSimon J. Gerraty 
370957b409SSimon J. Gerraty /** \file bearssl_ec.h
380957b409SSimon J. Gerraty  *
390957b409SSimon J. Gerraty  * # Elliptic Curves
400957b409SSimon J. Gerraty  *
410957b409SSimon J. Gerraty  * This file documents the EC implementations provided with BearSSL, and
420957b409SSimon J. Gerraty  * ECDSA.
430957b409SSimon J. Gerraty  *
440957b409SSimon J. Gerraty  * ## Elliptic Curve API
450957b409SSimon J. Gerraty  *
460957b409SSimon J. Gerraty  * Only "named curves" are supported. Each EC implementation supports
470957b409SSimon J. Gerraty  * one or several named curves, identified by symbolic identifiers.
480957b409SSimon J. Gerraty  * These identifiers are small integers, that correspond to the values
490957b409SSimon J. Gerraty  * registered by the
500957b409SSimon J. Gerraty  * [IANA](http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8).
510957b409SSimon J. Gerraty  *
520957b409SSimon J. Gerraty  * Since all currently defined elliptic curve identifiers are in the 0..31
530957b409SSimon J. Gerraty  * range, it is convenient to encode support of some curves in a 32-bit
540957b409SSimon J. Gerraty  * word, such that bit x corresponds to curve of identifier x.
550957b409SSimon J. Gerraty  *
560957b409SSimon J. Gerraty  * An EC implementation is incarnated by a `br_ec_impl` instance, that
570957b409SSimon J. Gerraty  * offers the following fields:
580957b409SSimon J. Gerraty  *
590957b409SSimon J. Gerraty  *   - `supported_curves`
600957b409SSimon J. Gerraty  *
610957b409SSimon J. Gerraty  *      A 32-bit word that documents the identifiers of the curves supported
620957b409SSimon J. Gerraty  *      by this implementation.
630957b409SSimon J. Gerraty  *
640957b409SSimon J. Gerraty  *   - `generator()`
650957b409SSimon J. Gerraty  *
660957b409SSimon J. Gerraty  *      Callback method that returns a pointer to the conventional generator
670957b409SSimon J. Gerraty  *      point for that curve.
680957b409SSimon J. Gerraty  *
690957b409SSimon J. Gerraty  *   - `order()`
700957b409SSimon J. Gerraty  *
710957b409SSimon J. Gerraty  *      Callback method that returns a pointer to the subgroup order for
720957b409SSimon J. Gerraty  *      that curve. That value uses unsigned big-endian encoding.
730957b409SSimon J. Gerraty  *
740957b409SSimon J. Gerraty  *   - `xoff()`
750957b409SSimon J. Gerraty  *
760957b409SSimon J. Gerraty  *      Callback method that returns the offset and length of the X
770957b409SSimon J. Gerraty  *      coordinate in an encoded point.
780957b409SSimon J. Gerraty  *
790957b409SSimon J. Gerraty  *   - `mul()`
800957b409SSimon J. Gerraty  *
810957b409SSimon J. Gerraty  *      Multiply a curve point with an integer.
820957b409SSimon J. Gerraty  *
830957b409SSimon J. Gerraty  *   - `mulgen()`
840957b409SSimon J. Gerraty  *
850957b409SSimon J. Gerraty  *      Multiply the curve generator with an integer. This may be faster
860957b409SSimon J. Gerraty  *      than the generic `mul()`.
870957b409SSimon J. Gerraty  *
880957b409SSimon J. Gerraty  *   - `muladd()`
890957b409SSimon J. Gerraty  *
900957b409SSimon J. Gerraty  *      Multiply two curve points by two integers, and return the sum of
910957b409SSimon J. Gerraty  *      the two products.
920957b409SSimon J. Gerraty  *
930957b409SSimon J. Gerraty  * All curve points are represented in uncompressed format. The `mul()`
940957b409SSimon J. Gerraty  * and `muladd()` methods take care to validate that the provided points
950957b409SSimon J. Gerraty  * are really part of the relevant curve subgroup.
960957b409SSimon J. Gerraty  *
970957b409SSimon J. Gerraty  * For all point multiplication functions, the following holds:
980957b409SSimon J. Gerraty  *
990957b409SSimon J. Gerraty  *   - Functions validate that the provided points are valid members
1000957b409SSimon J. Gerraty  *     of the relevant curve subgroup. An error is reported if that is
1010957b409SSimon J. Gerraty  *     not the case.
1020957b409SSimon J. Gerraty  *
1030957b409SSimon J. Gerraty  *   - Processing is constant-time, even if the point operands are not
1040957b409SSimon J. Gerraty  *     valid. This holds for both the source and resulting points, and
1050957b409SSimon J. Gerraty  *     the multipliers (integers). Only the byte length of the provided
1060957b409SSimon J. Gerraty  *     multiplier arrays (not their actual value length in bits) may
1070957b409SSimon J. Gerraty  *     leak through timing-based side channels.
1080957b409SSimon J. Gerraty  *
1090957b409SSimon J. Gerraty  *   - The multipliers (integers) MUST be lower than the subgroup order.
1100957b409SSimon J. Gerraty  *     If this property is not met, then the result is indeterminate,
111*cc9e6590SSimon J. Gerraty  *     but an error value is not necessarily returned.
1120957b409SSimon J. Gerraty  *
1130957b409SSimon J. Gerraty  *
1140957b409SSimon J. Gerraty  * ## ECDSA
1150957b409SSimon J. Gerraty  *
1160957b409SSimon J. Gerraty  * ECDSA signatures have two standard formats, called "raw" and "asn1".
1170957b409SSimon J. Gerraty  * Internally, such a signature is a pair of modular integers `(r,s)`.
1180957b409SSimon J. Gerraty  * The "raw" format is the concatenation of the unsigned big-endian
1190957b409SSimon J. Gerraty  * encodings of these two integers, possibly left-padded with zeros so
1200957b409SSimon J. Gerraty  * that they have the same encoded length. The "asn1" format is the
1210957b409SSimon J. Gerraty  * DER encoding of an ASN.1 structure that contains the two integer
1220957b409SSimon J. Gerraty  * values:
1230957b409SSimon J. Gerraty  *
1240957b409SSimon J. Gerraty  *     ECDSASignature ::= SEQUENCE {
1250957b409SSimon J. Gerraty  *         r   INTEGER,
1260957b409SSimon J. Gerraty  *         s   INTEGER
1270957b409SSimon J. Gerraty  *     }
1280957b409SSimon J. Gerraty  *
1290957b409SSimon J. Gerraty  * In general, in all of X.509 and SSL/TLS, the "asn1" format is used.
1300957b409SSimon J. Gerraty  * BearSSL offers ECDSA implementations for both formats; conversion
1310957b409SSimon J. Gerraty  * functions between the two formats are also provided. Conversion of a
1320957b409SSimon J. Gerraty  * "raw" format signature into "asn1" may enlarge a signature by no more
1330957b409SSimon J. Gerraty  * than 9 bytes for all supported curves; conversely, conversion of an
1340957b409SSimon J. Gerraty  * "asn1" signature to "raw" may expand the signature but the "raw"
1350957b409SSimon J. Gerraty  * length will never be more than twice the length of the "asn1" length
1360957b409SSimon J. Gerraty  * (and usually it will be shorter).
1370957b409SSimon J. Gerraty  *
1380957b409SSimon J. Gerraty  * Note that for a given signature, the "raw" format is not fully
1390957b409SSimon J. Gerraty  * deterministic, in that it does not enforce a minimal common length.
1400957b409SSimon J. Gerraty  */
1410957b409SSimon J. Gerraty 
1420957b409SSimon J. Gerraty /*
1430957b409SSimon J. Gerraty  * Standard curve ID. These ID are equal to the assigned numerical
1440957b409SSimon J. Gerraty  * identifiers assigned to these curves for TLS:
1450957b409SSimon J. Gerraty  *    http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
1460957b409SSimon J. Gerraty  */
1470957b409SSimon J. Gerraty 
1480957b409SSimon J. Gerraty /** \brief Identifier for named curve sect163k1. */
1490957b409SSimon J. Gerraty #define BR_EC_sect163k1           1
1500957b409SSimon J. Gerraty 
1510957b409SSimon J. Gerraty /** \brief Identifier for named curve sect163r1. */
1520957b409SSimon J. Gerraty #define BR_EC_sect163r1           2
1530957b409SSimon J. Gerraty 
1540957b409SSimon J. Gerraty /** \brief Identifier for named curve sect163r2. */
1550957b409SSimon J. Gerraty #define BR_EC_sect163r2           3
1560957b409SSimon J. Gerraty 
1570957b409SSimon J. Gerraty /** \brief Identifier for named curve sect193r1. */
1580957b409SSimon J. Gerraty #define BR_EC_sect193r1           4
1590957b409SSimon J. Gerraty 
1600957b409SSimon J. Gerraty /** \brief Identifier for named curve sect193r2. */
1610957b409SSimon J. Gerraty #define BR_EC_sect193r2           5
1620957b409SSimon J. Gerraty 
1630957b409SSimon J. Gerraty /** \brief Identifier for named curve sect233k1. */
1640957b409SSimon J. Gerraty #define BR_EC_sect233k1           6
1650957b409SSimon J. Gerraty 
1660957b409SSimon J. Gerraty /** \brief Identifier for named curve sect233r1. */
1670957b409SSimon J. Gerraty #define BR_EC_sect233r1           7
1680957b409SSimon J. Gerraty 
1690957b409SSimon J. Gerraty /** \brief Identifier for named curve sect239k1. */
1700957b409SSimon J. Gerraty #define BR_EC_sect239k1           8
1710957b409SSimon J. Gerraty 
1720957b409SSimon J. Gerraty /** \brief Identifier for named curve sect283k1. */
1730957b409SSimon J. Gerraty #define BR_EC_sect283k1           9
1740957b409SSimon J. Gerraty 
1750957b409SSimon J. Gerraty /** \brief Identifier for named curve sect283r1. */
1760957b409SSimon J. Gerraty #define BR_EC_sect283r1          10
1770957b409SSimon J. Gerraty 
1780957b409SSimon J. Gerraty /** \brief Identifier for named curve sect409k1. */
1790957b409SSimon J. Gerraty #define BR_EC_sect409k1          11
1800957b409SSimon J. Gerraty 
1810957b409SSimon J. Gerraty /** \brief Identifier for named curve sect409r1. */
1820957b409SSimon J. Gerraty #define BR_EC_sect409r1          12
1830957b409SSimon J. Gerraty 
1840957b409SSimon J. Gerraty /** \brief Identifier for named curve sect571k1. */
1850957b409SSimon J. Gerraty #define BR_EC_sect571k1          13
1860957b409SSimon J. Gerraty 
1870957b409SSimon J. Gerraty /** \brief Identifier for named curve sect571r1. */
1880957b409SSimon J. Gerraty #define BR_EC_sect571r1          14
1890957b409SSimon J. Gerraty 
1900957b409SSimon J. Gerraty /** \brief Identifier for named curve secp160k1. */
1910957b409SSimon J. Gerraty #define BR_EC_secp160k1          15
1920957b409SSimon J. Gerraty 
1930957b409SSimon J. Gerraty /** \brief Identifier for named curve secp160r1. */
1940957b409SSimon J. Gerraty #define BR_EC_secp160r1          16
1950957b409SSimon J. Gerraty 
1960957b409SSimon J. Gerraty /** \brief Identifier for named curve secp160r2. */
1970957b409SSimon J. Gerraty #define BR_EC_secp160r2          17
1980957b409SSimon J. Gerraty 
1990957b409SSimon J. Gerraty /** \brief Identifier for named curve secp192k1. */
2000957b409SSimon J. Gerraty #define BR_EC_secp192k1          18
2010957b409SSimon J. Gerraty 
2020957b409SSimon J. Gerraty /** \brief Identifier for named curve secp192r1. */
2030957b409SSimon J. Gerraty #define BR_EC_secp192r1          19
2040957b409SSimon J. Gerraty 
2050957b409SSimon J. Gerraty /** \brief Identifier for named curve secp224k1. */
2060957b409SSimon J. Gerraty #define BR_EC_secp224k1          20
2070957b409SSimon J. Gerraty 
2080957b409SSimon J. Gerraty /** \brief Identifier for named curve secp224r1. */
2090957b409SSimon J. Gerraty #define BR_EC_secp224r1          21
2100957b409SSimon J. Gerraty 
2110957b409SSimon J. Gerraty /** \brief Identifier for named curve secp256k1. */
2120957b409SSimon J. Gerraty #define BR_EC_secp256k1          22
2130957b409SSimon J. Gerraty 
2140957b409SSimon J. Gerraty /** \brief Identifier for named curve secp256r1. */
2150957b409SSimon J. Gerraty #define BR_EC_secp256r1          23
2160957b409SSimon J. Gerraty 
2170957b409SSimon J. Gerraty /** \brief Identifier for named curve secp384r1. */
2180957b409SSimon J. Gerraty #define BR_EC_secp384r1          24
2190957b409SSimon J. Gerraty 
2200957b409SSimon J. Gerraty /** \brief Identifier for named curve secp521r1. */
2210957b409SSimon J. Gerraty #define BR_EC_secp521r1          25
2220957b409SSimon J. Gerraty 
2230957b409SSimon J. Gerraty /** \brief Identifier for named curve brainpoolP256r1. */
2240957b409SSimon J. Gerraty #define BR_EC_brainpoolP256r1    26
2250957b409SSimon J. Gerraty 
2260957b409SSimon J. Gerraty /** \brief Identifier for named curve brainpoolP384r1. */
2270957b409SSimon J. Gerraty #define BR_EC_brainpoolP384r1    27
2280957b409SSimon J. Gerraty 
2290957b409SSimon J. Gerraty /** \brief Identifier for named curve brainpoolP512r1. */
2300957b409SSimon J. Gerraty #define BR_EC_brainpoolP512r1    28
2310957b409SSimon J. Gerraty 
2320957b409SSimon J. Gerraty /** \brief Identifier for named curve Curve25519. */
2330957b409SSimon J. Gerraty #define BR_EC_curve25519         29
2340957b409SSimon J. Gerraty 
2350957b409SSimon J. Gerraty /** \brief Identifier for named curve Curve448. */
2360957b409SSimon J. Gerraty #define BR_EC_curve448           30
2370957b409SSimon J. Gerraty 
2380957b409SSimon J. Gerraty /**
2390957b409SSimon J. Gerraty  * \brief Structure for an EC public key.
2400957b409SSimon J. Gerraty  */
2410957b409SSimon J. Gerraty typedef struct {
2420957b409SSimon J. Gerraty 	/** \brief Identifier for the curve used by this key. */
2430957b409SSimon J. Gerraty 	int curve;
2440957b409SSimon J. Gerraty 	/** \brief Public curve point (uncompressed format). */
2450957b409SSimon J. Gerraty 	unsigned char *q;
2460957b409SSimon J. Gerraty 	/** \brief Length of public curve point (in bytes). */
2470957b409SSimon J. Gerraty 	size_t qlen;
2480957b409SSimon J. Gerraty } br_ec_public_key;
2490957b409SSimon J. Gerraty 
2500957b409SSimon J. Gerraty /**
2510957b409SSimon J. Gerraty  * \brief Structure for an EC private key.
2520957b409SSimon J. Gerraty  *
2530957b409SSimon J. Gerraty  * The private key is an integer modulo the curve subgroup order. The
2540957b409SSimon J. Gerraty  * encoding below tolerates extra leading zeros. In general, it is
2550957b409SSimon J. Gerraty  * recommended that the private key has the same length as the curve
2560957b409SSimon J. Gerraty  * subgroup order.
2570957b409SSimon J. Gerraty  */
2580957b409SSimon J. Gerraty typedef struct {
2590957b409SSimon J. Gerraty 	/** \brief Identifier for the curve used by this key. */
2600957b409SSimon J. Gerraty 	int curve;
2610957b409SSimon J. Gerraty 	/** \brief Private key (integer, unsigned big-endian encoding). */
2620957b409SSimon J. Gerraty 	unsigned char *x;
2630957b409SSimon J. Gerraty 	/** \brief Private key length (in bytes). */
2640957b409SSimon J. Gerraty 	size_t xlen;
2650957b409SSimon J. Gerraty } br_ec_private_key;
2660957b409SSimon J. Gerraty 
2670957b409SSimon J. Gerraty /**
2680957b409SSimon J. Gerraty  * \brief Type for an EC implementation.
2690957b409SSimon J. Gerraty  */
2700957b409SSimon J. Gerraty typedef struct {
2710957b409SSimon J. Gerraty 	/**
2720957b409SSimon J. Gerraty 	 * \brief Supported curves.
2730957b409SSimon J. Gerraty 	 *
2740957b409SSimon J. Gerraty 	 * This word is a bitfield: bit `x` is set if the curve of ID `x`
2750957b409SSimon J. Gerraty 	 * is supported. E.g. an implementation supporting both NIST P-256
2760957b409SSimon J. Gerraty 	 * (secp256r1, ID 23) and NIST P-384 (secp384r1, ID 24) will have
2770957b409SSimon J. Gerraty 	 * value `0x01800000` in this field.
2780957b409SSimon J. Gerraty 	 */
2790957b409SSimon J. Gerraty 	uint32_t supported_curves;
2800957b409SSimon J. Gerraty 
2810957b409SSimon J. Gerraty 	/**
2820957b409SSimon J. Gerraty 	 * \brief Get the conventional generator.
2830957b409SSimon J. Gerraty 	 *
2840957b409SSimon J. Gerraty 	 * This function returns the conventional generator (encoded
2850957b409SSimon J. Gerraty 	 * curve point) for the specified curve. This function MUST NOT
2860957b409SSimon J. Gerraty 	 * be called if the curve is not supported.
2870957b409SSimon J. Gerraty 	 *
2880957b409SSimon J. Gerraty 	 * \param curve   curve identifier.
2890957b409SSimon J. Gerraty 	 * \param len     receiver for the encoded generator length (in bytes).
2900957b409SSimon J. Gerraty 	 * \return  the encoded generator.
2910957b409SSimon J. Gerraty 	 */
2920957b409SSimon J. Gerraty 	const unsigned char *(*generator)(int curve, size_t *len);
2930957b409SSimon J. Gerraty 
2940957b409SSimon J. Gerraty 	/**
2950957b409SSimon J. Gerraty 	 * \brief Get the subgroup order.
2960957b409SSimon J. Gerraty 	 *
2970957b409SSimon J. Gerraty 	 * This function returns the order of the subgroup generated by
2980957b409SSimon J. Gerraty 	 * the conventional generator, for the specified curve. Unsigned
2990957b409SSimon J. Gerraty 	 * big-endian encoding is used. This function MUST NOT be called
3000957b409SSimon J. Gerraty 	 * if the curve is not supported.
3010957b409SSimon J. Gerraty 	 *
3020957b409SSimon J. Gerraty 	 * \param curve   curve identifier.
3030957b409SSimon J. Gerraty 	 * \param len     receiver for the encoded order length (in bytes).
3040957b409SSimon J. Gerraty 	 * \return  the encoded order.
3050957b409SSimon J. Gerraty 	 */
3060957b409SSimon J. Gerraty 	const unsigned char *(*order)(int curve, size_t *len);
3070957b409SSimon J. Gerraty 
3080957b409SSimon J. Gerraty 	/**
3090957b409SSimon J. Gerraty 	 * \brief Get the offset and length for the X coordinate.
3100957b409SSimon J. Gerraty 	 *
3110957b409SSimon J. Gerraty 	 * This function returns the offset and length (in bytes) of
3120957b409SSimon J. Gerraty 	 * the X coordinate in an encoded non-zero point.
3130957b409SSimon J. Gerraty 	 *
3140957b409SSimon J. Gerraty 	 * \param curve   curve identifier.
3150957b409SSimon J. Gerraty 	 * \param len     receiver for the X coordinate length (in bytes).
3160957b409SSimon J. Gerraty 	 * \return  the offset for the X coordinate (in bytes).
3170957b409SSimon J. Gerraty 	 */
3180957b409SSimon J. Gerraty 	size_t (*xoff)(int curve, size_t *len);
3190957b409SSimon J. Gerraty 
3200957b409SSimon J. Gerraty 	/**
3210957b409SSimon J. Gerraty 	 * \brief Multiply a curve point by an integer.
3220957b409SSimon J. Gerraty 	 *
3230957b409SSimon J. Gerraty 	 * The source point is provided in array `G` (of size `Glen` bytes);
3240957b409SSimon J. Gerraty 	 * the multiplication result is written over it. The multiplier
3250957b409SSimon J. Gerraty 	 * `x` (of size `xlen` bytes) uses unsigned big-endian encoding.
3260957b409SSimon J. Gerraty 	 *
3270957b409SSimon J. Gerraty 	 * Rules:
3280957b409SSimon J. Gerraty 	 *
3290957b409SSimon J. Gerraty 	 *   - The specified curve MUST be supported.
3300957b409SSimon J. Gerraty 	 *
3310957b409SSimon J. Gerraty 	 *   - The source point must be a valid point on the relevant curve
3320957b409SSimon J. Gerraty 	 *     subgroup (and not the "point at infinity" either). If this is
3330957b409SSimon J. Gerraty 	 *     not the case, then this function returns an error (0).
3340957b409SSimon J. Gerraty 	 *
3350957b409SSimon J. Gerraty 	 *   - The multiplier integer MUST be non-zero and less than the
3360957b409SSimon J. Gerraty 	 *     curve subgroup order. If this property does not hold, then
3370957b409SSimon J. Gerraty 	 *     the result is indeterminate and an error code is not
3380957b409SSimon J. Gerraty 	 *     guaranteed.
3390957b409SSimon J. Gerraty 	 *
3400957b409SSimon J. Gerraty 	 * Returned value is 1 on success, 0 on error. On error, the
3410957b409SSimon J. Gerraty 	 * contents of `G` are indeterminate.
3420957b409SSimon J. Gerraty 	 *
3430957b409SSimon J. Gerraty 	 * \param G       point to multiply.
3440957b409SSimon J. Gerraty 	 * \param Glen    length of the encoded point (in bytes).
3450957b409SSimon J. Gerraty 	 * \param x       multiplier (unsigned big-endian).
3460957b409SSimon J. Gerraty 	 * \param xlen    multiplier length (in bytes).
3470957b409SSimon J. Gerraty 	 * \param curve   curve identifier.
3480957b409SSimon J. Gerraty 	 * \return  1 on success, 0 on error.
3490957b409SSimon J. Gerraty 	 */
3500957b409SSimon J. Gerraty 	uint32_t (*mul)(unsigned char *G, size_t Glen,
3510957b409SSimon J. Gerraty 		const unsigned char *x, size_t xlen, int curve);
3520957b409SSimon J. Gerraty 
3530957b409SSimon J. Gerraty 	/**
3540957b409SSimon J. Gerraty 	 * \brief Multiply the generator by an integer.
3550957b409SSimon J. Gerraty 	 *
3560957b409SSimon J. Gerraty 	 * The multiplier MUST be non-zero and less than the curve
3570957b409SSimon J. Gerraty 	 * subgroup order. Results are indeterminate if this property
3580957b409SSimon J. Gerraty 	 * does not hold.
3590957b409SSimon J. Gerraty 	 *
3600957b409SSimon J. Gerraty 	 * \param R       output buffer for the point.
3610957b409SSimon J. Gerraty 	 * \param x       multiplier (unsigned big-endian).
3620957b409SSimon J. Gerraty 	 * \param xlen    multiplier length (in bytes).
3630957b409SSimon J. Gerraty 	 * \param curve   curve identifier.
3640957b409SSimon J. Gerraty 	 * \return  encoded result point length (in bytes).
3650957b409SSimon J. Gerraty 	 */
3660957b409SSimon J. Gerraty 	size_t (*mulgen)(unsigned char *R,
3670957b409SSimon J. Gerraty 		const unsigned char *x, size_t xlen, int curve);
3680957b409SSimon J. Gerraty 
3690957b409SSimon J. Gerraty 	/**
3700957b409SSimon J. Gerraty 	 * \brief Multiply two points by two integers and add the
3710957b409SSimon J. Gerraty 	 * results.
3720957b409SSimon J. Gerraty 	 *
3730957b409SSimon J. Gerraty 	 * The point `x*A + y*B` is computed and written back in the `A`
3740957b409SSimon J. Gerraty 	 * array.
3750957b409SSimon J. Gerraty 	 *
3760957b409SSimon J. Gerraty 	 * Rules:
3770957b409SSimon J. Gerraty 	 *
3780957b409SSimon J. Gerraty 	 *   - The specified curve MUST be supported.
3790957b409SSimon J. Gerraty 	 *
3800957b409SSimon J. Gerraty 	 *   - The source points (`A` and `B`)  must be valid points on
3810957b409SSimon J. Gerraty 	 *     the relevant curve subgroup (and not the "point at
3820957b409SSimon J. Gerraty 	 *     infinity" either). If this is not the case, then this
3830957b409SSimon J. Gerraty 	 *     function returns an error (0).
3840957b409SSimon J. Gerraty 	 *
3850957b409SSimon J. Gerraty 	 *   - If the `B` pointer is `NULL`, then the conventional
3860957b409SSimon J. Gerraty 	 *     subgroup generator is used. With some implementations,
3870957b409SSimon J. Gerraty 	 *     this may be faster than providing a pointer to the
3880957b409SSimon J. Gerraty 	 *     generator.
3890957b409SSimon J. Gerraty 	 *
3900957b409SSimon J. Gerraty 	 *   - The multiplier integers (`x` and `y`) MUST be non-zero
3910957b409SSimon J. Gerraty 	 *     and less than the curve subgroup order. If either integer
3920957b409SSimon J. Gerraty 	 *     is zero, then an error is reported, but if one of them is
3930957b409SSimon J. Gerraty 	 *     not lower than the subgroup order, then the result is
3940957b409SSimon J. Gerraty 	 *     indeterminate and an error code is not guaranteed.
3950957b409SSimon J. Gerraty 	 *
3960957b409SSimon J. Gerraty 	 *   - If the final result is the point at infinity, then an
3970957b409SSimon J. Gerraty 	 *     error is returned.
3980957b409SSimon J. Gerraty 	 *
3990957b409SSimon J. Gerraty 	 * Returned value is 1 on success, 0 on error. On error, the
4000957b409SSimon J. Gerraty 	 * contents of `A` are indeterminate.
4010957b409SSimon J. Gerraty 	 *
4020957b409SSimon J. Gerraty 	 * \param A       first point to multiply.
4030957b409SSimon J. Gerraty 	 * \param B       second point to multiply (`NULL` for the generator).
4040957b409SSimon J. Gerraty 	 * \param len     common length of the encoded points (in bytes).
4050957b409SSimon J. Gerraty 	 * \param x       multiplier for `A` (unsigned big-endian).
4060957b409SSimon J. Gerraty 	 * \param xlen    length of multiplier for `A` (in bytes).
4070957b409SSimon J. Gerraty 	 * \param y       multiplier for `A` (unsigned big-endian).
4080957b409SSimon J. Gerraty 	 * \param ylen    length of multiplier for `A` (in bytes).
4090957b409SSimon J. Gerraty 	 * \param curve   curve identifier.
4100957b409SSimon J. Gerraty 	 * \return  1 on success, 0 on error.
4110957b409SSimon J. Gerraty 	 */
4120957b409SSimon J. Gerraty 	uint32_t (*muladd)(unsigned char *A, const unsigned char *B, size_t len,
4130957b409SSimon J. Gerraty 		const unsigned char *x, size_t xlen,
4140957b409SSimon J. Gerraty 		const unsigned char *y, size_t ylen, int curve);
4150957b409SSimon J. Gerraty } br_ec_impl;
4160957b409SSimon J. Gerraty 
4170957b409SSimon J. Gerraty /**
4180957b409SSimon J. Gerraty  * \brief EC implementation "i31".
4190957b409SSimon J. Gerraty  *
4200957b409SSimon J. Gerraty  * This implementation internally uses generic code for modular integers,
4210957b409SSimon J. Gerraty  * with a representation as sequences of 31-bit words. It supports secp256r1,
4220957b409SSimon J. Gerraty  * secp384r1 and secp521r1 (aka NIST curves P-256, P-384 and P-521).
4230957b409SSimon J. Gerraty  */
4240957b409SSimon J. Gerraty extern const br_ec_impl br_ec_prime_i31;
4250957b409SSimon J. Gerraty 
4260957b409SSimon J. Gerraty /**
4270957b409SSimon J. Gerraty  * \brief EC implementation "i15".
4280957b409SSimon J. Gerraty  *
4290957b409SSimon J. Gerraty  * This implementation internally uses generic code for modular integers,
4300957b409SSimon J. Gerraty  * with a representation as sequences of 15-bit words. It supports secp256r1,
4310957b409SSimon J. Gerraty  * secp384r1 and secp521r1 (aka NIST curves P-256, P-384 and P-521).
4320957b409SSimon J. Gerraty  */
4330957b409SSimon J. Gerraty extern const br_ec_impl br_ec_prime_i15;
4340957b409SSimon J. Gerraty 
4350957b409SSimon J. Gerraty /**
4360957b409SSimon J. Gerraty  * \brief EC implementation "m15" for P-256.
4370957b409SSimon J. Gerraty  *
4380957b409SSimon J. Gerraty  * This implementation uses specialised code for curve secp256r1 (also
4390957b409SSimon J. Gerraty  * known as NIST P-256), with optional Karatsuba decomposition, and fast
4400957b409SSimon J. Gerraty  * modular reduction thanks to the field modulus special format. Only
4410957b409SSimon J. Gerraty  * 32-bit multiplications are used (with 32-bit results, not 64-bit).
4420957b409SSimon J. Gerraty  */
4430957b409SSimon J. Gerraty extern const br_ec_impl br_ec_p256_m15;
4440957b409SSimon J. Gerraty 
4450957b409SSimon J. Gerraty /**
4460957b409SSimon J. Gerraty  * \brief EC implementation "m31" for P-256.
4470957b409SSimon J. Gerraty  *
4480957b409SSimon J. Gerraty  * This implementation uses specialised code for curve secp256r1 (also
4490957b409SSimon J. Gerraty  * known as NIST P-256), relying on multiplications of 31-bit values
4500957b409SSimon J. Gerraty  * (MUL31).
4510957b409SSimon J. Gerraty  */
4520957b409SSimon J. Gerraty extern const br_ec_impl br_ec_p256_m31;
4530957b409SSimon J. Gerraty 
4540957b409SSimon J. Gerraty /**
4550957b409SSimon J. Gerraty  * \brief EC implementation "m62" (specialised code) for P-256.
4560957b409SSimon J. Gerraty  *
4570957b409SSimon J. Gerraty  * This implementation uses custom code relying on multiplication of
4580957b409SSimon J. Gerraty  * integers up to 64 bits, with a 128-bit result. This implementation is
4590957b409SSimon J. Gerraty  * defined only on platforms that offer the 64x64->128 multiplication
4600957b409SSimon J. Gerraty  * support; use `br_ec_p256_m62_get()` to dynamically obtain a pointer
4610957b409SSimon J. Gerraty  * to that implementation.
4620957b409SSimon J. Gerraty  */
4630957b409SSimon J. Gerraty extern const br_ec_impl br_ec_p256_m62;
4640957b409SSimon J. Gerraty 
4650957b409SSimon J. Gerraty /**
4660957b409SSimon J. Gerraty  * \brief Get the "m62" implementation of P-256, if available.
4670957b409SSimon J. Gerraty  *
4680957b409SSimon J. Gerraty  * \return  the implementation, or 0.
4690957b409SSimon J. Gerraty  */
4700957b409SSimon J. Gerraty const br_ec_impl *br_ec_p256_m62_get(void);
4710957b409SSimon J. Gerraty 
4720957b409SSimon J. Gerraty /**
4730957b409SSimon J. Gerraty  * \brief EC implementation "m64" (specialised code) for P-256.
4740957b409SSimon J. Gerraty  *
4750957b409SSimon J. Gerraty  * This implementation uses custom code relying on multiplication of
4760957b409SSimon J. Gerraty  * integers up to 64 bits, with a 128-bit result. This implementation is
4770957b409SSimon J. Gerraty  * defined only on platforms that offer the 64x64->128 multiplication
4780957b409SSimon J. Gerraty  * support; use `br_ec_p256_m64_get()` to dynamically obtain a pointer
4790957b409SSimon J. Gerraty  * to that implementation.
4800957b409SSimon J. Gerraty  */
4810957b409SSimon J. Gerraty extern const br_ec_impl br_ec_p256_m64;
4820957b409SSimon J. Gerraty 
4830957b409SSimon J. Gerraty /**
4840957b409SSimon J. Gerraty  * \brief Get the "m64" implementation of P-256, if available.
4850957b409SSimon J. Gerraty  *
4860957b409SSimon J. Gerraty  * \return  the implementation, or 0.
4870957b409SSimon J. Gerraty  */
4880957b409SSimon J. Gerraty const br_ec_impl *br_ec_p256_m64_get(void);
4890957b409SSimon J. Gerraty 
4900957b409SSimon J. Gerraty /**
4910957b409SSimon J. Gerraty  * \brief EC implementation "i15" (generic code) for Curve25519.
4920957b409SSimon J. Gerraty  *
4930957b409SSimon J. Gerraty  * This implementation uses the generic code for modular integers (with
4940957b409SSimon J. Gerraty  * 15-bit words) to support Curve25519. Due to the specificities of the
4950957b409SSimon J. Gerraty  * curve definition, the following applies:
4960957b409SSimon J. Gerraty  *
4970957b409SSimon J. Gerraty  *   - `muladd()` is not implemented (the function returns 0 systematically).
4980957b409SSimon J. Gerraty  *   - `order()` returns 2^255-1, since the point multiplication algorithm
4990957b409SSimon J. Gerraty  *     accepts any 32-bit integer as input (it clears the top bit and low
5000957b409SSimon J. Gerraty  *     three bits systematically).
5010957b409SSimon J. Gerraty  */
5020957b409SSimon J. Gerraty extern const br_ec_impl br_ec_c25519_i15;
5030957b409SSimon J. Gerraty 
5040957b409SSimon J. Gerraty /**
5050957b409SSimon J. Gerraty  * \brief EC implementation "i31" (generic code) for Curve25519.
5060957b409SSimon J. Gerraty  *
5070957b409SSimon J. Gerraty  * This implementation uses the generic code for modular integers (with
5080957b409SSimon J. Gerraty  * 31-bit words) to support Curve25519. Due to the specificities of the
5090957b409SSimon J. Gerraty  * curve definition, the following applies:
5100957b409SSimon J. Gerraty  *
5110957b409SSimon J. Gerraty  *   - `muladd()` is not implemented (the function returns 0 systematically).
5120957b409SSimon J. Gerraty  *   - `order()` returns 2^255-1, since the point multiplication algorithm
5130957b409SSimon J. Gerraty  *     accepts any 32-bit integer as input (it clears the top bit and low
5140957b409SSimon J. Gerraty  *     three bits systematically).
5150957b409SSimon J. Gerraty  */
5160957b409SSimon J. Gerraty extern const br_ec_impl br_ec_c25519_i31;
5170957b409SSimon J. Gerraty 
5180957b409SSimon J. Gerraty /**
5190957b409SSimon J. Gerraty  * \brief EC implementation "m15" (specialised code) for Curve25519.
5200957b409SSimon J. Gerraty  *
5210957b409SSimon J. Gerraty  * This implementation uses custom code relying on multiplication of
5220957b409SSimon J. Gerraty  * integers up to 15 bits. Due to the specificities of the curve
5230957b409SSimon J. Gerraty  * definition, the following applies:
5240957b409SSimon J. Gerraty  *
5250957b409SSimon J. Gerraty  *   - `muladd()` is not implemented (the function returns 0 systematically).
5260957b409SSimon J. Gerraty  *   - `order()` returns 2^255-1, since the point multiplication algorithm
5270957b409SSimon J. Gerraty  *     accepts any 32-bit integer as input (it clears the top bit and low
5280957b409SSimon J. Gerraty  *     three bits systematically).
5290957b409SSimon J. Gerraty  */
5300957b409SSimon J. Gerraty extern const br_ec_impl br_ec_c25519_m15;
5310957b409SSimon J. Gerraty 
5320957b409SSimon J. Gerraty /**
5330957b409SSimon J. Gerraty  * \brief EC implementation "m31" (specialised code) for Curve25519.
5340957b409SSimon J. Gerraty  *
5350957b409SSimon J. Gerraty  * This implementation uses custom code relying on multiplication of
5360957b409SSimon J. Gerraty  * integers up to 31 bits. Due to the specificities of the curve
5370957b409SSimon J. Gerraty  * definition, the following applies:
5380957b409SSimon J. Gerraty  *
5390957b409SSimon J. Gerraty  *   - `muladd()` is not implemented (the function returns 0 systematically).
5400957b409SSimon J. Gerraty  *   - `order()` returns 2^255-1, since the point multiplication algorithm
5410957b409SSimon J. Gerraty  *     accepts any 32-bit integer as input (it clears the top bit and low
5420957b409SSimon J. Gerraty  *     three bits systematically).
5430957b409SSimon J. Gerraty  */
5440957b409SSimon J. Gerraty extern const br_ec_impl br_ec_c25519_m31;
5450957b409SSimon J. Gerraty 
5460957b409SSimon J. Gerraty /**
5470957b409SSimon J. Gerraty  * \brief EC implementation "m62" (specialised code) for Curve25519.
5480957b409SSimon J. Gerraty  *
5490957b409SSimon J. Gerraty  * This implementation uses custom code relying on multiplication of
5500957b409SSimon J. Gerraty  * integers up to 62 bits, with a 124-bit result. This implementation is
5510957b409SSimon J. Gerraty  * defined only on platforms that offer the 64x64->128 multiplication
5520957b409SSimon J. Gerraty  * support; use `br_ec_c25519_m62_get()` to dynamically obtain a pointer
5530957b409SSimon J. Gerraty  * to that implementation. Due to the specificities of the curve
5540957b409SSimon J. Gerraty  * definition, the following applies:
5550957b409SSimon J. Gerraty  *
5560957b409SSimon J. Gerraty  *   - `muladd()` is not implemented (the function returns 0 systematically).
5570957b409SSimon J. Gerraty  *   - `order()` returns 2^255-1, since the point multiplication algorithm
5580957b409SSimon J. Gerraty  *     accepts any 32-bit integer as input (it clears the top bit and low
5590957b409SSimon J. Gerraty  *     three bits systematically).
5600957b409SSimon J. Gerraty  */
5610957b409SSimon J. Gerraty extern const br_ec_impl br_ec_c25519_m62;
5620957b409SSimon J. Gerraty 
5630957b409SSimon J. Gerraty /**
5640957b409SSimon J. Gerraty  * \brief Get the "m62" implementation of Curve25519, if available.
5650957b409SSimon J. Gerraty  *
5660957b409SSimon J. Gerraty  * \return  the implementation, or 0.
5670957b409SSimon J. Gerraty  */
5680957b409SSimon J. Gerraty const br_ec_impl *br_ec_c25519_m62_get(void);
5690957b409SSimon J. Gerraty 
5700957b409SSimon J. Gerraty /**
5710957b409SSimon J. Gerraty  * \brief EC implementation "m64" (specialised code) for Curve25519.
5720957b409SSimon J. Gerraty  *
5730957b409SSimon J. Gerraty  * This implementation uses custom code relying on multiplication of
5740957b409SSimon J. Gerraty  * integers up to 64 bits, with a 128-bit result. This implementation is
5750957b409SSimon J. Gerraty  * defined only on platforms that offer the 64x64->128 multiplication
5760957b409SSimon J. Gerraty  * support; use `br_ec_c25519_m64_get()` to dynamically obtain a pointer
5770957b409SSimon J. Gerraty  * to that implementation. Due to the specificities of the curve
5780957b409SSimon J. Gerraty  * definition, the following applies:
5790957b409SSimon J. Gerraty  *
5800957b409SSimon J. Gerraty  *   - `muladd()` is not implemented (the function returns 0 systematically).
5810957b409SSimon J. Gerraty  *   - `order()` returns 2^255-1, since the point multiplication algorithm
5820957b409SSimon J. Gerraty  *     accepts any 32-bit integer as input (it clears the top bit and low
5830957b409SSimon J. Gerraty  *     three bits systematically).
5840957b409SSimon J. Gerraty  */
5850957b409SSimon J. Gerraty extern const br_ec_impl br_ec_c25519_m64;
5860957b409SSimon J. Gerraty 
5870957b409SSimon J. Gerraty /**
5880957b409SSimon J. Gerraty  * \brief Get the "m64" implementation of Curve25519, if available.
5890957b409SSimon J. Gerraty  *
5900957b409SSimon J. Gerraty  * \return  the implementation, or 0.
5910957b409SSimon J. Gerraty  */
5920957b409SSimon J. Gerraty const br_ec_impl *br_ec_c25519_m64_get(void);
5930957b409SSimon J. Gerraty 
5940957b409SSimon J. Gerraty /**
5950957b409SSimon J. Gerraty  * \brief Aggregate EC implementation "m15".
5960957b409SSimon J. Gerraty  *
5970957b409SSimon J. Gerraty  * This implementation is a wrapper for:
5980957b409SSimon J. Gerraty  *
5990957b409SSimon J. Gerraty  *   - `br_ec_c25519_m15` for Curve25519
6000957b409SSimon J. Gerraty  *   - `br_ec_p256_m15` for NIST P-256
6010957b409SSimon J. Gerraty  *   - `br_ec_prime_i15` for other curves (NIST P-384 and NIST-P512)
6020957b409SSimon J. Gerraty  */
6030957b409SSimon J. Gerraty extern const br_ec_impl br_ec_all_m15;
6040957b409SSimon J. Gerraty 
6050957b409SSimon J. Gerraty /**
6060957b409SSimon J. Gerraty  * \brief Aggregate EC implementation "m31".
6070957b409SSimon J. Gerraty  *
6080957b409SSimon J. Gerraty  * This implementation is a wrapper for:
6090957b409SSimon J. Gerraty  *
6100957b409SSimon J. Gerraty  *   - `br_ec_c25519_m31` for Curve25519
6110957b409SSimon J. Gerraty  *   - `br_ec_p256_m31` for NIST P-256
6120957b409SSimon J. Gerraty  *   - `br_ec_prime_i31` for other curves (NIST P-384 and NIST-P512)
6130957b409SSimon J. Gerraty  */
6140957b409SSimon J. Gerraty extern const br_ec_impl br_ec_all_m31;
6150957b409SSimon J. Gerraty 
6160957b409SSimon J. Gerraty /**
6170957b409SSimon J. Gerraty  * \brief Get the "default" EC implementation for the current system.
6180957b409SSimon J. Gerraty  *
6190957b409SSimon J. Gerraty  * This returns a pointer to the preferred implementation on the
6200957b409SSimon J. Gerraty  * current system.
6210957b409SSimon J. Gerraty  *
6220957b409SSimon J. Gerraty  * \return  the default EC implementation.
6230957b409SSimon J. Gerraty  */
6240957b409SSimon J. Gerraty const br_ec_impl *br_ec_get_default(void);
6250957b409SSimon J. Gerraty 
6260957b409SSimon J. Gerraty /**
6270957b409SSimon J. Gerraty  * \brief Convert a signature from "raw" to "asn1".
6280957b409SSimon J. Gerraty  *
6290957b409SSimon J. Gerraty  * Conversion is done "in place" and the new length is returned.
6300957b409SSimon J. Gerraty  * Conversion may enlarge the signature, but by no more than 9 bytes at
6310957b409SSimon J. Gerraty  * most. On error, 0 is returned (error conditions include an odd raw
6320957b409SSimon J. Gerraty  * signature length, or an oversized integer).
6330957b409SSimon J. Gerraty  *
6340957b409SSimon J. Gerraty  * \param sig       signature to convert.
6350957b409SSimon J. Gerraty  * \param sig_len   signature length (in bytes).
6360957b409SSimon J. Gerraty  * \return  the new signature length, or 0 on error.
6370957b409SSimon J. Gerraty  */
6380957b409SSimon J. Gerraty size_t br_ecdsa_raw_to_asn1(void *sig, size_t sig_len);
6390957b409SSimon J. Gerraty 
6400957b409SSimon J. Gerraty /**
6410957b409SSimon J. Gerraty  * \brief Convert a signature from "asn1" to "raw".
6420957b409SSimon J. Gerraty  *
6430957b409SSimon J. Gerraty  * Conversion is done "in place" and the new length is returned.
6440957b409SSimon J. Gerraty  * Conversion may enlarge the signature, but the new signature length
6450957b409SSimon J. Gerraty  * will be less than twice the source length at most. On error, 0 is
6460957b409SSimon J. Gerraty  * returned (error conditions include an invalid ASN.1 structure or an
6470957b409SSimon J. Gerraty  * oversized integer).
6480957b409SSimon J. Gerraty  *
6490957b409SSimon J. Gerraty  * \param sig       signature to convert.
6500957b409SSimon J. Gerraty  * \param sig_len   signature length (in bytes).
6510957b409SSimon J. Gerraty  * \return  the new signature length, or 0 on error.
6520957b409SSimon J. Gerraty  */
6530957b409SSimon J. Gerraty size_t br_ecdsa_asn1_to_raw(void *sig, size_t sig_len);
6540957b409SSimon J. Gerraty 
6550957b409SSimon J. Gerraty /**
6560957b409SSimon J. Gerraty  * \brief Type for an ECDSA signer function.
6570957b409SSimon J. Gerraty  *
6580957b409SSimon J. Gerraty  * A pointer to the EC implementation is provided. The hash value is
6590957b409SSimon J. Gerraty  * assumed to have the length inferred from the designated hash function
6600957b409SSimon J. Gerraty  * class.
6610957b409SSimon J. Gerraty  *
6620957b409SSimon J. Gerraty  * Signature is written in the buffer pointed to by `sig`, and the length
6630957b409SSimon J. Gerraty  * (in bytes) is returned. On error, nothing is written in the buffer,
6640957b409SSimon J. Gerraty  * and 0 is returned. This function returns 0 if the specified curve is
6650957b409SSimon J. Gerraty  * not supported by the provided EC implementation.
6660957b409SSimon J. Gerraty  *
6670957b409SSimon J. Gerraty  * The signature format is either "raw" or "asn1", depending on the
6680957b409SSimon J. Gerraty  * implementation; maximum length is predictable from the implemented
6690957b409SSimon J. Gerraty  * curve:
6700957b409SSimon J. Gerraty  *
6710957b409SSimon J. Gerraty  * | curve      | raw | asn1 |
6720957b409SSimon J. Gerraty  * | :--------- | --: | ---: |
6730957b409SSimon J. Gerraty  * | NIST P-256 |  64 |   72 |
6740957b409SSimon J. Gerraty  * | NIST P-384 |  96 |  104 |
6750957b409SSimon J. Gerraty  * | NIST P-521 | 132 |  139 |
6760957b409SSimon J. Gerraty  *
6770957b409SSimon J. Gerraty  * \param impl         EC implementation to use.
6780957b409SSimon J. Gerraty  * \param hf           hash function used to process the data.
6790957b409SSimon J. Gerraty  * \param hash_value   signed data (hashed).
6800957b409SSimon J. Gerraty  * \param sk           EC private key.
6810957b409SSimon J. Gerraty  * \param sig          destination buffer.
6820957b409SSimon J. Gerraty  * \return  the signature length (in bytes), or 0 on error.
6830957b409SSimon J. Gerraty  */
6840957b409SSimon J. Gerraty typedef size_t (*br_ecdsa_sign)(const br_ec_impl *impl,
6850957b409SSimon J. Gerraty 	const br_hash_class *hf, const void *hash_value,
6860957b409SSimon J. Gerraty 	const br_ec_private_key *sk, void *sig);
6870957b409SSimon J. Gerraty 
6880957b409SSimon J. Gerraty /**
6890957b409SSimon J. Gerraty  * \brief Type for an ECDSA signature verification function.
6900957b409SSimon J. Gerraty  *
6910957b409SSimon J. Gerraty  * A pointer to the EC implementation is provided. The hashed value,
6920957b409SSimon J. Gerraty  * computed over the purportedly signed data, is also provided with
6930957b409SSimon J. Gerraty  * its length.
6940957b409SSimon J. Gerraty  *
6950957b409SSimon J. Gerraty  * The signature format is either "raw" or "asn1", depending on the
6960957b409SSimon J. Gerraty  * implementation.
6970957b409SSimon J. Gerraty  *
6980957b409SSimon J. Gerraty  * Returned value is 1 on success (valid signature), 0 on error. This
6990957b409SSimon J. Gerraty  * function returns 0 if the specified curve is not supported by the
7000957b409SSimon J. Gerraty  * provided EC implementation.
7010957b409SSimon J. Gerraty  *
7020957b409SSimon J. Gerraty  * \param impl       EC implementation to use.
7030957b409SSimon J. Gerraty  * \param hash       signed data (hashed).
7040957b409SSimon J. Gerraty  * \param hash_len   hash value length (in bytes).
7050957b409SSimon J. Gerraty  * \param pk         EC public key.
7060957b409SSimon J. Gerraty  * \param sig        signature.
7070957b409SSimon J. Gerraty  * \param sig_len    signature length (in bytes).
7080957b409SSimon J. Gerraty  * \return  1 on success, 0 on error.
7090957b409SSimon J. Gerraty  */
7100957b409SSimon J. Gerraty typedef uint32_t (*br_ecdsa_vrfy)(const br_ec_impl *impl,
7110957b409SSimon J. Gerraty 	const void *hash, size_t hash_len,
7120957b409SSimon J. Gerraty 	const br_ec_public_key *pk, const void *sig, size_t sig_len);
7130957b409SSimon J. Gerraty 
7140957b409SSimon J. Gerraty /**
7150957b409SSimon J. Gerraty  * \brief ECDSA signature generator, "i31" implementation, "asn1" format.
7160957b409SSimon J. Gerraty  *
7170957b409SSimon J. Gerraty  * \see br_ecdsa_sign()
7180957b409SSimon J. Gerraty  *
7190957b409SSimon J. Gerraty  * \param impl         EC implementation to use.
7200957b409SSimon J. Gerraty  * \param hf           hash function used to process the data.
7210957b409SSimon J. Gerraty  * \param hash_value   signed data (hashed).
7220957b409SSimon J. Gerraty  * \param sk           EC private key.
7230957b409SSimon J. Gerraty  * \param sig          destination buffer.
7240957b409SSimon J. Gerraty  * \return  the signature length (in bytes), or 0 on error.
7250957b409SSimon J. Gerraty  */
7260957b409SSimon J. Gerraty size_t br_ecdsa_i31_sign_asn1(const br_ec_impl *impl,
7270957b409SSimon J. Gerraty 	const br_hash_class *hf, const void *hash_value,
7280957b409SSimon J. Gerraty 	const br_ec_private_key *sk, void *sig);
7290957b409SSimon J. Gerraty 
7300957b409SSimon J. Gerraty /**
7310957b409SSimon J. Gerraty  * \brief ECDSA signature generator, "i31" implementation, "raw" format.
7320957b409SSimon J. Gerraty  *
7330957b409SSimon J. Gerraty  * \see br_ecdsa_sign()
7340957b409SSimon J. Gerraty  *
7350957b409SSimon J. Gerraty  * \param impl         EC implementation to use.
7360957b409SSimon J. Gerraty  * \param hf           hash function used to process the data.
7370957b409SSimon J. Gerraty  * \param hash_value   signed data (hashed).
7380957b409SSimon J. Gerraty  * \param sk           EC private key.
7390957b409SSimon J. Gerraty  * \param sig          destination buffer.
7400957b409SSimon J. Gerraty  * \return  the signature length (in bytes), or 0 on error.
7410957b409SSimon J. Gerraty  */
7420957b409SSimon J. Gerraty size_t br_ecdsa_i31_sign_raw(const br_ec_impl *impl,
7430957b409SSimon J. Gerraty 	const br_hash_class *hf, const void *hash_value,
7440957b409SSimon J. Gerraty 	const br_ec_private_key *sk, void *sig);
7450957b409SSimon J. Gerraty 
7460957b409SSimon J. Gerraty /**
7470957b409SSimon J. Gerraty  * \brief ECDSA signature verifier, "i31" implementation, "asn1" format.
7480957b409SSimon J. Gerraty  *
7490957b409SSimon J. Gerraty  * \see br_ecdsa_vrfy()
7500957b409SSimon J. Gerraty  *
7510957b409SSimon J. Gerraty  * \param impl       EC implementation to use.
7520957b409SSimon J. Gerraty  * \param hash       signed data (hashed).
7530957b409SSimon J. Gerraty  * \param hash_len   hash value length (in bytes).
7540957b409SSimon J. Gerraty  * \param pk         EC public key.
7550957b409SSimon J. Gerraty  * \param sig        signature.
7560957b409SSimon J. Gerraty  * \param sig_len    signature length (in bytes).
7570957b409SSimon J. Gerraty  * \return  1 on success, 0 on error.
7580957b409SSimon J. Gerraty  */
7590957b409SSimon J. Gerraty uint32_t br_ecdsa_i31_vrfy_asn1(const br_ec_impl *impl,
7600957b409SSimon J. Gerraty 	const void *hash, size_t hash_len,
7610957b409SSimon J. Gerraty 	const br_ec_public_key *pk, const void *sig, size_t sig_len);
7620957b409SSimon J. Gerraty 
7630957b409SSimon J. Gerraty /**
7640957b409SSimon J. Gerraty  * \brief ECDSA signature verifier, "i31" implementation, "raw" format.
7650957b409SSimon J. Gerraty  *
7660957b409SSimon J. Gerraty  * \see br_ecdsa_vrfy()
7670957b409SSimon J. Gerraty  *
7680957b409SSimon J. Gerraty  * \param impl       EC implementation to use.
7690957b409SSimon J. Gerraty  * \param hash       signed data (hashed).
7700957b409SSimon J. Gerraty  * \param hash_len   hash value length (in bytes).
7710957b409SSimon J. Gerraty  * \param pk         EC public key.
7720957b409SSimon J. Gerraty  * \param sig        signature.
7730957b409SSimon J. Gerraty  * \param sig_len    signature length (in bytes).
7740957b409SSimon J. Gerraty  * \return  1 on success, 0 on error.
7750957b409SSimon J. Gerraty  */
7760957b409SSimon J. Gerraty uint32_t br_ecdsa_i31_vrfy_raw(const br_ec_impl *impl,
7770957b409SSimon J. Gerraty 	const void *hash, size_t hash_len,
7780957b409SSimon J. Gerraty 	const br_ec_public_key *pk, const void *sig, size_t sig_len);
7790957b409SSimon J. Gerraty 
7800957b409SSimon J. Gerraty /**
7810957b409SSimon J. Gerraty  * \brief ECDSA signature generator, "i15" implementation, "asn1" format.
7820957b409SSimon J. Gerraty  *
7830957b409SSimon J. Gerraty  * \see br_ecdsa_sign()
7840957b409SSimon J. Gerraty  *
7850957b409SSimon J. Gerraty  * \param impl         EC implementation to use.
7860957b409SSimon J. Gerraty  * \param hf           hash function used to process the data.
7870957b409SSimon J. Gerraty  * \param hash_value   signed data (hashed).
7880957b409SSimon J. Gerraty  * \param sk           EC private key.
7890957b409SSimon J. Gerraty  * \param sig          destination buffer.
7900957b409SSimon J. Gerraty  * \return  the signature length (in bytes), or 0 on error.
7910957b409SSimon J. Gerraty  */
7920957b409SSimon J. Gerraty size_t br_ecdsa_i15_sign_asn1(const br_ec_impl *impl,
7930957b409SSimon J. Gerraty 	const br_hash_class *hf, const void *hash_value,
7940957b409SSimon J. Gerraty 	const br_ec_private_key *sk, void *sig);
7950957b409SSimon J. Gerraty 
7960957b409SSimon J. Gerraty /**
7970957b409SSimon J. Gerraty  * \brief ECDSA signature generator, "i15" implementation, "raw" format.
7980957b409SSimon J. Gerraty  *
7990957b409SSimon J. Gerraty  * \see br_ecdsa_sign()
8000957b409SSimon J. Gerraty  *
8010957b409SSimon J. Gerraty  * \param impl         EC implementation to use.
8020957b409SSimon J. Gerraty  * \param hf           hash function used to process the data.
8030957b409SSimon J. Gerraty  * \param hash_value   signed data (hashed).
8040957b409SSimon J. Gerraty  * \param sk           EC private key.
8050957b409SSimon J. Gerraty  * \param sig          destination buffer.
8060957b409SSimon J. Gerraty  * \return  the signature length (in bytes), or 0 on error.
8070957b409SSimon J. Gerraty  */
8080957b409SSimon J. Gerraty size_t br_ecdsa_i15_sign_raw(const br_ec_impl *impl,
8090957b409SSimon J. Gerraty 	const br_hash_class *hf, const void *hash_value,
8100957b409SSimon J. Gerraty 	const br_ec_private_key *sk, void *sig);
8110957b409SSimon J. Gerraty 
8120957b409SSimon J. Gerraty /**
8130957b409SSimon J. Gerraty  * \brief ECDSA signature verifier, "i15" implementation, "asn1" format.
8140957b409SSimon J. Gerraty  *
8150957b409SSimon J. Gerraty  * \see br_ecdsa_vrfy()
8160957b409SSimon J. Gerraty  *
8170957b409SSimon J. Gerraty  * \param impl       EC implementation to use.
8180957b409SSimon J. Gerraty  * \param hash       signed data (hashed).
8190957b409SSimon J. Gerraty  * \param hash_len   hash value length (in bytes).
8200957b409SSimon J. Gerraty  * \param pk         EC public key.
8210957b409SSimon J. Gerraty  * \param sig        signature.
8220957b409SSimon J. Gerraty  * \param sig_len    signature length (in bytes).
8230957b409SSimon J. Gerraty  * \return  1 on success, 0 on error.
8240957b409SSimon J. Gerraty  */
8250957b409SSimon J. Gerraty uint32_t br_ecdsa_i15_vrfy_asn1(const br_ec_impl *impl,
8260957b409SSimon J. Gerraty 	const void *hash, size_t hash_len,
8270957b409SSimon J. Gerraty 	const br_ec_public_key *pk, const void *sig, size_t sig_len);
8280957b409SSimon J. Gerraty 
8290957b409SSimon J. Gerraty /**
8300957b409SSimon J. Gerraty  * \brief ECDSA signature verifier, "i15" implementation, "raw" format.
8310957b409SSimon J. Gerraty  *
8320957b409SSimon J. Gerraty  * \see br_ecdsa_vrfy()
8330957b409SSimon J. Gerraty  *
8340957b409SSimon J. Gerraty  * \param impl       EC implementation to use.
8350957b409SSimon J. Gerraty  * \param hash       signed data (hashed).
8360957b409SSimon J. Gerraty  * \param hash_len   hash value length (in bytes).
8370957b409SSimon J. Gerraty  * \param pk         EC public key.
8380957b409SSimon J. Gerraty  * \param sig        signature.
8390957b409SSimon J. Gerraty  * \param sig_len    signature length (in bytes).
8400957b409SSimon J. Gerraty  * \return  1 on success, 0 on error.
8410957b409SSimon J. Gerraty  */
8420957b409SSimon J. Gerraty uint32_t br_ecdsa_i15_vrfy_raw(const br_ec_impl *impl,
8430957b409SSimon J. Gerraty 	const void *hash, size_t hash_len,
8440957b409SSimon J. Gerraty 	const br_ec_public_key *pk, const void *sig, size_t sig_len);
8450957b409SSimon J. Gerraty 
8460957b409SSimon J. Gerraty /**
8470957b409SSimon J. Gerraty  * \brief Get "default" ECDSA implementation (signer, asn1 format).
8480957b409SSimon J. Gerraty  *
8490957b409SSimon J. Gerraty  * This returns the preferred implementation of ECDSA signature generation
8500957b409SSimon J. Gerraty  * ("asn1" output format) on the current system.
8510957b409SSimon J. Gerraty  *
8520957b409SSimon J. Gerraty  * \return  the default implementation.
8530957b409SSimon J. Gerraty  */
8540957b409SSimon J. Gerraty br_ecdsa_sign br_ecdsa_sign_asn1_get_default(void);
8550957b409SSimon J. Gerraty 
8560957b409SSimon J. Gerraty /**
8570957b409SSimon J. Gerraty  * \brief Get "default" ECDSA implementation (signer, raw format).
8580957b409SSimon J. Gerraty  *
8590957b409SSimon J. Gerraty  * This returns the preferred implementation of ECDSA signature generation
8600957b409SSimon J. Gerraty  * ("raw" output format) on the current system.
8610957b409SSimon J. Gerraty  *
8620957b409SSimon J. Gerraty  * \return  the default implementation.
8630957b409SSimon J. Gerraty  */
8640957b409SSimon J. Gerraty br_ecdsa_sign br_ecdsa_sign_raw_get_default(void);
8650957b409SSimon J. Gerraty 
8660957b409SSimon J. Gerraty /**
8670957b409SSimon J. Gerraty  * \brief Get "default" ECDSA implementation (verifier, asn1 format).
8680957b409SSimon J. Gerraty  *
8690957b409SSimon J. Gerraty  * This returns the preferred implementation of ECDSA signature verification
8700957b409SSimon J. Gerraty  * ("asn1" output format) on the current system.
8710957b409SSimon J. Gerraty  *
8720957b409SSimon J. Gerraty  * \return  the default implementation.
8730957b409SSimon J. Gerraty  */
8740957b409SSimon J. Gerraty br_ecdsa_vrfy br_ecdsa_vrfy_asn1_get_default(void);
8750957b409SSimon J. Gerraty 
8760957b409SSimon J. Gerraty /**
8770957b409SSimon J. Gerraty  * \brief Get "default" ECDSA implementation (verifier, raw format).
8780957b409SSimon J. Gerraty  *
8790957b409SSimon J. Gerraty  * This returns the preferred implementation of ECDSA signature verification
8800957b409SSimon J. Gerraty  * ("raw" output format) on the current system.
8810957b409SSimon J. Gerraty  *
8820957b409SSimon J. Gerraty  * \return  the default implementation.
8830957b409SSimon J. Gerraty  */
8840957b409SSimon J. Gerraty br_ecdsa_vrfy br_ecdsa_vrfy_raw_get_default(void);
8850957b409SSimon J. Gerraty 
8860957b409SSimon J. Gerraty /**
8870957b409SSimon J. Gerraty  * \brief Maximum size for EC private key element buffer.
8880957b409SSimon J. Gerraty  *
8890957b409SSimon J. Gerraty  * This is the largest number of bytes that `br_ec_keygen()` may need or
8900957b409SSimon J. Gerraty  * ever return.
8910957b409SSimon J. Gerraty  */
8920957b409SSimon J. Gerraty #define BR_EC_KBUF_PRIV_MAX_SIZE   72
8930957b409SSimon J. Gerraty 
8940957b409SSimon J. Gerraty /**
8950957b409SSimon J. Gerraty  * \brief Maximum size for EC public key element buffer.
8960957b409SSimon J. Gerraty  *
8970957b409SSimon J. Gerraty  * This is the largest number of bytes that `br_ec_compute_public()` may
8980957b409SSimon J. Gerraty  * need or ever return.
8990957b409SSimon J. Gerraty  */
9000957b409SSimon J. Gerraty #define BR_EC_KBUF_PUB_MAX_SIZE    145
9010957b409SSimon J. Gerraty 
9020957b409SSimon J. Gerraty /**
9030957b409SSimon J. Gerraty  * \brief Generate a new EC private key.
9040957b409SSimon J. Gerraty  *
9050957b409SSimon J. Gerraty  * If the specified `curve` is not supported by the elliptic curve
9060957b409SSimon J. Gerraty  * implementation (`impl`), then this function returns zero.
9070957b409SSimon J. Gerraty  *
9080957b409SSimon J. Gerraty  * The `sk` structure fields are set to the new private key data. In
9090957b409SSimon J. Gerraty  * particular, `sk.x` is made to point to the provided key buffer (`kbuf`),
9100957b409SSimon J. Gerraty  * in which the actual private key data is written. That buffer is assumed
9110957b409SSimon J. Gerraty  * to be large enough. The `BR_EC_KBUF_PRIV_MAX_SIZE` defines the maximum
9120957b409SSimon J. Gerraty  * size for all supported curves.
9130957b409SSimon J. Gerraty  *
9140957b409SSimon J. Gerraty  * The number of bytes used in `kbuf` is returned. If `kbuf` is `NULL`, then
9150957b409SSimon J. Gerraty  * the private key is not actually generated, and `sk` may also be `NULL`;
9160957b409SSimon J. Gerraty  * the minimum length for `kbuf` is still computed and returned.
9170957b409SSimon J. Gerraty  *
9180957b409SSimon J. Gerraty  * If `sk` is `NULL` but `kbuf` is not `NULL`, then the private key is
9190957b409SSimon J. Gerraty  * still generated and stored in `kbuf`.
9200957b409SSimon J. Gerraty  *
9210957b409SSimon J. Gerraty  * \param rng_ctx   source PRNG context (already initialized).
9220957b409SSimon J. Gerraty  * \param impl      the elliptic curve implementation.
9230957b409SSimon J. Gerraty  * \param sk        the private key structure to fill, or `NULL`.
9240957b409SSimon J. Gerraty  * \param kbuf      the key element buffer, or `NULL`.
9250957b409SSimon J. Gerraty  * \param curve     the curve identifier.
9260957b409SSimon J. Gerraty  * \return  the key data length (in bytes), or zero.
9270957b409SSimon J. Gerraty  */
9280957b409SSimon J. Gerraty size_t br_ec_keygen(const br_prng_class **rng_ctx,
9290957b409SSimon J. Gerraty 	const br_ec_impl *impl, br_ec_private_key *sk,
9300957b409SSimon J. Gerraty 	void *kbuf, int curve);
9310957b409SSimon J. Gerraty 
9320957b409SSimon J. Gerraty /**
9330957b409SSimon J. Gerraty  * \brief Compute EC public key from EC private key.
9340957b409SSimon J. Gerraty  *
9350957b409SSimon J. Gerraty  * This function uses the provided elliptic curve implementation (`impl`)
9360957b409SSimon J. Gerraty  * to compute the public key corresponding to the private key held in `sk`.
9370957b409SSimon J. Gerraty  * The public key point is written into `kbuf`, which is then linked from
9380957b409SSimon J. Gerraty  * the `*pk` structure. The size of the public key point, i.e. the number
9390957b409SSimon J. Gerraty  * of bytes used in `kbuf`, is returned.
9400957b409SSimon J. Gerraty  *
9410957b409SSimon J. Gerraty  * If `kbuf` is `NULL`, then the public key point is NOT computed, and
9420957b409SSimon J. Gerraty  * the public key structure `*pk` is unmodified (`pk` may be `NULL` in
9430957b409SSimon J. Gerraty  * that case). The size of the public key point is still returned.
9440957b409SSimon J. Gerraty  *
9450957b409SSimon J. Gerraty  * If `pk` is `NULL` but `kbuf` is not `NULL`, then the public key
9460957b409SSimon J. Gerraty  * point is computed and stored in `kbuf`, and its size is returned.
9470957b409SSimon J. Gerraty  *
9480957b409SSimon J. Gerraty  * If the curve used by the private key is not supported by the curve
9490957b409SSimon J. Gerraty  * implementation, then this function returns zero.
9500957b409SSimon J. Gerraty  *
9510957b409SSimon J. Gerraty  * The private key MUST be valid. An off-range private key value is not
9520957b409SSimon J. Gerraty  * necessarily detected, and leads to unpredictable results.
9530957b409SSimon J. Gerraty  *
9540957b409SSimon J. Gerraty  * \param impl   the elliptic curve implementation.
9550957b409SSimon J. Gerraty  * \param pk     the public key structure to fill (or `NULL`).
9560957b409SSimon J. Gerraty  * \param kbuf   the public key point buffer (or `NULL`).
9570957b409SSimon J. Gerraty  * \param sk     the source private key.
9580957b409SSimon J. Gerraty  * \return  the public key point length (in bytes), or zero.
9590957b409SSimon J. Gerraty  */
9600957b409SSimon J. Gerraty size_t br_ec_compute_pub(const br_ec_impl *impl, br_ec_public_key *pk,
9610957b409SSimon J. Gerraty 	void *kbuf, const br_ec_private_key *sk);
9620957b409SSimon J. Gerraty 
9630957b409SSimon J. Gerraty #ifdef __cplusplus
9640957b409SSimon J. Gerraty }
9650957b409SSimon J. Gerraty #endif
9660957b409SSimon J. Gerraty 
9670957b409SSimon J. Gerraty #endif
968