/*1* Copyright (c) 2016 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#ifndef BR_BEARSSL_RSA_H__25#define BR_BEARSSL_RSA_H__2627#include <stddef.h>28#include <stdint.h>2930#include "bearssl_hash.h"31#include "bearssl_rand.h"3233#ifdef __cplusplus34extern "C" {35#endif3637/** \file bearssl_rsa.h38*39* # RSA40*41* This file documents the RSA implementations provided with BearSSL.42* Note that the SSL engine accesses these implementations through a43* configurable API, so it is possible to, for instance, run a SSL44* server which uses a RSA engine which is not based on this code.45*46* ## Key Elements47*48* RSA public and private keys consist in lists of big integers. All49* such integers are represented with big-endian unsigned notation:50* first byte is the most significant, and the value is positive (so51* there is no dedicated "sign bit"). Public and private key structures52* thus contain, for each such integer, a pointer to the first value byte53* (`unsigned char *`), and a length (`size_t`) which is the number of54* relevant bytes. As a general rule, minimal-length encoding is not55* enforced: values may have extra leading bytes of value 0.56*57* RSA public keys consist in two integers:58*59* - the modulus (`n`);60* - the public exponent (`e`).61*62* RSA private keys, as defined in63* [PKCS#1](https://tools.ietf.org/html/rfc3447), contain eight integers:64*65* - the modulus (`n`);66* - the public exponent (`e`);67* - the private exponent (`d`);68* - the first prime factor (`p`);69* - the second prime factor (`q`);70* - the first reduced exponent (`dp`, which is `d` modulo `p-1`);71* - the second reduced exponent (`dq`, which is `d` modulo `q-1`);72* - the CRT coefficient (`iq`, the inverse of `q` modulo `p`).73*74* However, the implementations defined in BearSSL use only five of75* these integers: `p`, `q`, `dp`, `dq` and `iq`.76*77* ## Security Features and Limitations78*79* The implementations contained in BearSSL have the following limitations80* and features:81*82* - They are constant-time. This means that the execution time and83* memory access pattern may depend on the _lengths_ of the private84* key components, but not on their value, nor on the value of85* the operand. Note that this property is not achieved through86* random masking, but "true" constant-time code.87*88* - They support only private keys with two prime factors. RSA private89* keys with three or more prime factors are nominally supported, but90* rarely used; they may offer faster operations, at the expense of91* more code and potentially a reduction in security if there are92* "too many" prime factors.93*94* - The public exponent may have arbitrary length. Of course, it is95* a good idea to keep public exponents small, so that public key96* operations are fast; but, contrary to some widely deployed97* implementations, BearSSL has no problem with public exponents98* longer than 32 bits.99*100* - The two prime factors of the modulus need not have the same length101* (but severely imbalanced factor lengths might reduce security).102* Similarly, there is no requirement that the first factor (`p`)103* be greater than the second factor (`q`).104*105* - Prime factors and modulus must be smaller than a compile-time limit.106* This is made necessary by the use of fixed-size stack buffers, and107* the limit has been adjusted to keep stack usage under 2 kB for the108* RSA operations. Currently, the maximum modulus size is 4096 bits,109* and the maximum prime factor size is 2080 bits.110*111* - The RSA functions themselves do not enforce lower size limits,112* except that which is absolutely necessary for the operation to113* mathematically make sense (e.g. a PKCS#1 v1.5 signature with114* SHA-1 requires a modulus of at least 361 bits). It is up to users115* of this code to enforce size limitations when appropriate (e.g.116* the X.509 validation engine, by default, rejects RSA keys of117* less than 1017 bits).118*119* - Within the size constraints expressed above, arbitrary bit lengths120* are supported. There is no requirement that prime factors or121* modulus have a size multiple of 8 or 16.122*123* - When verifying PKCS#1 v1.5 signatures, both variants of the hash124* function identifying header (with and without the ASN.1 NULL) are125* supported. When producing such signatures, the variant with the126* ASN.1 NULL is used.127*128* ## Implementations129*130* Three RSA implementations are included:131*132* - The **i32** implementation internally represents big integers133* as arrays of 32-bit integers. It is perfunctory and portable,134* but not very efficient.135*136* - The **i31** implementation uses 32-bit integers, each containing137* 31 bits worth of integer data. The i31 implementation is somewhat138* faster than the i32 implementation (the reduced integer size makes139* carry propagation easier) for a similar code footprint, but uses140* very slightly larger stack buffers (about 4% bigger).141*142* - The **i62** implementation is similar to the i31 implementation,143* except that it internally leverages the 64x64->128 multiplication144* opcode. This implementation is available only on architectures145* where such an opcode exists. It is much faster than i31.146*147* - The **i15** implementation uses 16-bit integers, each containing148* 15 bits worth of integer data. Multiplication results fit on149* 32 bits, so this won't use the "widening" multiplication routine150* on ARM Cortex M0/M0+, for much better performance and constant-time151* execution.152*/153154/**155* \brief RSA public key.156*157* The structure references the modulus and the public exponent. Both158* integers use unsigned big-endian representation; extra leading bytes159* of value 0 are allowed.160*/161typedef struct {162/** \brief Modulus. */163unsigned char *n;164/** \brief Modulus length (in bytes). */165size_t nlen;166/** \brief Public exponent. */167unsigned char *e;168/** \brief Public exponent length (in bytes). */169size_t elen;170} br_rsa_public_key;171172/**173* \brief RSA private key.174*175* The structure references the private factors, reduced private176* exponents, and CRT coefficient. It also contains the bit length of177* the modulus. The big integers use unsigned big-endian representation;178* extra leading bytes of value 0 are allowed. However, the modulus bit179* length (`n_bitlen`) MUST be exact.180*/181typedef struct {182/** \brief Modulus bit length (in bits, exact value). */183uint32_t n_bitlen;184/** \brief First prime factor. */185unsigned char *p;186/** \brief First prime factor length (in bytes). */187size_t plen;188/** \brief Second prime factor. */189unsigned char *q;190/** \brief Second prime factor length (in bytes). */191size_t qlen;192/** \brief First reduced private exponent. */193unsigned char *dp;194/** \brief First reduced private exponent length (in bytes). */195size_t dplen;196/** \brief Second reduced private exponent. */197unsigned char *dq;198/** \brief Second reduced private exponent length (in bytes). */199size_t dqlen;200/** \brief CRT coefficient. */201unsigned char *iq;202/** \brief CRT coefficient length (in bytes). */203size_t iqlen;204} br_rsa_private_key;205206/**207* \brief Type for a RSA public key engine.208*209* The public key engine performs the modular exponentiation of the210* provided value with the public exponent. The value is modified in211* place.212*213* The value length (`xlen`) is verified to have _exactly_ the same214* length as the modulus (actual modulus length, without extra leading215* zeros in the modulus representation in memory). If the length does216* not match, then this function returns 0 and `x[]` is unmodified.217*218* It `xlen` is correct, then `x[]` is modified. Returned value is 1219* on success, 0 on error. Error conditions include an oversized `x[]`220* (the array has the same length as the modulus, but the numerical value221* is not lower than the modulus) and an invalid modulus (e.g. an even222* integer). If an error is reported, then the new contents of `x[]` are223* unspecified.224*225* \param x operand to exponentiate.226* \param xlen length of the operand (in bytes).227* \param pk RSA public key.228* \return 1 on success, 0 on error.229*/230typedef uint32_t (*br_rsa_public)(unsigned char *x, size_t xlen,231const br_rsa_public_key *pk);232233/**234* \brief Type for a RSA signature verification engine (PKCS#1 v1.5).235*236* Parameters are:237*238* - The signature itself. The provided array is NOT modified.239*240* - The encoded OID for the hash function. The provided array must begin241* with a single byte that contains the length of the OID value (in242* bytes), followed by exactly that many bytes. This parameter may243* also be `NULL`, in which case the raw hash value should be used244* with the PKCS#1 v1.5 "type 1" padding (as used in SSL/TLS up245* to TLS-1.1, with a 36-byte hash value).246*247* - The hash output length, in bytes.248*249* - The public key.250*251* - An output buffer for the hash value. The caller must still compare252* it with the hash of the data over which the signature is computed.253*254* **Constraints:**255*256* - Hash length MUST be no more than 64 bytes.257*258* - OID value length MUST be no more than 32 bytes (i.e. `hash_oid[0]`259* must have a value in the 0..32 range, inclusive).260*261* This function verifies that the signature length (`xlen`) matches the262* modulus length (this function returns 0 on mismatch). If the modulus263* size exceeds the maximum supported RSA size, then the function also264* returns 0.265*266* Returned value is 1 on success, 0 on error.267*268* Implementations of this type need not be constant-time.269*270* \param x signature buffer.271* \param xlen signature length (in bytes).272* \param hash_oid encoded hash algorithm OID (or `NULL`).273* \param hash_len expected hash value length (in bytes).274* \param pk RSA public key.275* \param hash_out output buffer for the hash value.276* \return 1 on success, 0 on error.277*/278typedef uint32_t (*br_rsa_pkcs1_vrfy)(const unsigned char *x, size_t xlen,279const unsigned char *hash_oid, size_t hash_len,280const br_rsa_public_key *pk, unsigned char *hash_out);281282/**283* \brief Type for a RSA signature verification engine (PSS).284*285* Parameters are:286*287* - The signature itself. The provided array is NOT modified.288*289* - The hash function which was used to hash the message.290*291* - The hash function to use with MGF1 within the PSS padding. This292* is not necessarily the same hash function as the one which was293* used to hash the signed message.294*295* - The hashed message (as an array of bytes).296*297* - The PSS salt length (in bytes).298*299* - The public key.300*301* **Constraints:**302*303* - Hash message length MUST be no more than 64 bytes.304*305* Note that, contrary to PKCS#1 v1.5 signature, the hash value of the306* signed data cannot be extracted from the signature; it must be307* provided to the verification function.308*309* This function verifies that the signature length (`xlen`) matches the310* modulus length (this function returns 0 on mismatch). If the modulus311* size exceeds the maximum supported RSA size, then the function also312* returns 0.313*314* Returned value is 1 on success, 0 on error.315*316* Implementations of this type need not be constant-time.317*318* \param x signature buffer.319* \param xlen signature length (in bytes).320* \param hf_data hash function applied on the message.321* \param hf_mgf1 hash function to use with MGF1.322* \param hash hash value of the signed message.323* \param salt_len PSS salt length (in bytes).324* \param pk RSA public key.325* \return 1 on success, 0 on error.326*/327typedef uint32_t (*br_rsa_pss_vrfy)(const unsigned char *x, size_t xlen,328const br_hash_class *hf_data, const br_hash_class *hf_mgf1,329const void *hash, size_t salt_len, const br_rsa_public_key *pk);330331/**332* \brief Type for a RSA encryption engine (OAEP).333*334* Parameters are:335*336* - A source of random bytes. The source must be already initialized.337*338* - A hash function, used internally with the mask generation function339* (MGF1).340*341* - A label. The `label` pointer may be `NULL` if `label_len` is zero342* (an empty label, which is the default in PKCS#1 v2.2).343*344* - The public key.345*346* - The destination buffer. Its maximum length (in bytes) is provided;347* if that length is lower than the public key length, then an error348* is reported.349*350* - The source message.351*352* The encrypted message output has exactly the same length as the modulus353* (mathematical length, in bytes, not counting extra leading zeros in the354* modulus representation in the public key).355*356* The source message (`src`, length `src_len`) may overlap with the357* destination buffer (`dst`, length `dst_max_len`).358*359* This function returns the actual encrypted message length, in bytes;360* on error, zero is returned. An error is reported if the output buffer361* is not large enough, or the public is invalid, or the public key362* modulus exceeds the maximum supported RSA size.363*364* \param rnd source of random bytes.365* \param dig hash function to use with MGF1.366* \param label label value (may be `NULL` if `label_len` is zero).367* \param label_len label length, in bytes.368* \param pk RSA public key.369* \param dst destination buffer.370* \param dst_max_len destination buffer length (maximum encrypted data size).371* \param src message to encrypt.372* \param src_len source message length (in bytes).373* \return encrypted message length (in bytes), or 0 on error.374*/375typedef size_t (*br_rsa_oaep_encrypt)(376const br_prng_class **rnd, const br_hash_class *dig,377const void *label, size_t label_len,378const br_rsa_public_key *pk,379void *dst, size_t dst_max_len,380const void *src, size_t src_len);381382/**383* \brief Type for a RSA private key engine.384*385* The `x[]` buffer is modified in place, and its length is inferred from386* the modulus length (`x[]` is assumed to have a length of387* `(sk->n_bitlen+7)/8` bytes).388*389* Returned value is 1 on success, 0 on error.390*391* \param x operand to exponentiate.392* \param sk RSA private key.393* \return 1 on success, 0 on error.394*/395typedef uint32_t (*br_rsa_private)(unsigned char *x,396const br_rsa_private_key *sk);397398/**399* \brief Type for a RSA signature generation engine (PKCS#1 v1.5).400*401* Parameters are:402*403* - The encoded OID for the hash function. The provided array must begin404* with a single byte that contains the length of the OID value (in405* bytes), followed by exactly that many bytes. This parameter may406* also be `NULL`, in which case the raw hash value should be used407* with the PKCS#1 v1.5 "type 1" padding (as used in SSL/TLS up408* to TLS-1.1, with a 36-byte hash value).409*410* - The hash value computes over the data to sign (its length is411* expressed in bytes).412*413* - The RSA private key.414*415* - The output buffer, that receives the signature.416*417* Returned value is 1 on success, 0 on error. Error conditions include418* a too small modulus for the provided hash OID and value, or some419* invalid key parameters. The signature length is exactly420* `(sk->n_bitlen+7)/8` bytes.421*422* This function is expected to be constant-time with regards to the423* private key bytes (lengths of the modulus and the individual factors424* may leak, though) and to the hashed data.425*426* \param hash_oid encoded hash algorithm OID (or `NULL`).427* \param hash hash value.428* \param hash_len hash value length (in bytes).429* \param sk RSA private key.430* \param x output buffer for the signature value.431* \return 1 on success, 0 on error.432*/433typedef uint32_t (*br_rsa_pkcs1_sign)(const unsigned char *hash_oid,434const unsigned char *hash, size_t hash_len,435const br_rsa_private_key *sk, unsigned char *x);436437/**438* \brief Type for a RSA signature generation engine (PSS).439*440* Parameters are:441*442* - An initialized PRNG for salt generation. If the salt length is443* zero (`salt_len` parameter), then the PRNG is optional (this is444* not the typical case, as the security proof of RSA/PSS is445* tighter when a non-empty salt is used).446*447* - The hash function which was used to hash the message.448*449* - The hash function to use with MGF1 within the PSS padding. This450* is not necessarily the same function as the one used to hash the451* message.452*453* - The hashed message.454*455* - The salt length, in bytes.456*457* - The RSA private key.458*459* - The output buffer, that receives the signature.460*461* Returned value is 1 on success, 0 on error. Error conditions include462* a too small modulus for the provided hash and salt lengths, or some463* invalid key parameters. The signature length is exactly464* `(sk->n_bitlen+7)/8` bytes.465*466* This function is expected to be constant-time with regards to the467* private key bytes (lengths of the modulus and the individual factors468* may leak, though) and to the hashed data.469*470* \param rng PRNG for salt generation (`NULL` if `salt_len` is zero).471* \param hf_data hash function used to hash the signed data.472* \param hf_mgf1 hash function to use with MGF1.473* \param hash hashed message.474* \param salt_len salt length (in bytes).475* \param sk RSA private key.476* \param x output buffer for the signature value.477* \return 1 on success, 0 on error.478*/479typedef uint32_t (*br_rsa_pss_sign)(const br_prng_class **rng,480const br_hash_class *hf_data, const br_hash_class *hf_mgf1,481const unsigned char *hash_value, size_t salt_len,482const br_rsa_private_key *sk, unsigned char *x);483484/**485* \brief Encoded OID for SHA-1 (in RSA PKCS#1 signatures).486*/487#define BR_HASH_OID_SHA1 \488((const unsigned char *)"\x05\x2B\x0E\x03\x02\x1A")489490/**491* \brief Encoded OID for SHA-224 (in RSA PKCS#1 signatures).492*/493#define BR_HASH_OID_SHA224 \494((const unsigned char *)"\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04")495496/**497* \brief Encoded OID for SHA-256 (in RSA PKCS#1 signatures).498*/499#define BR_HASH_OID_SHA256 \500((const unsigned char *)"\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01")501502/**503* \brief Encoded OID for SHA-384 (in RSA PKCS#1 signatures).504*/505#define BR_HASH_OID_SHA384 \506((const unsigned char *)"\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02")507508/**509* \brief Encoded OID for SHA-512 (in RSA PKCS#1 signatures).510*/511#define BR_HASH_OID_SHA512 \512((const unsigned char *)"\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03")513514/**515* \brief Type for a RSA decryption engine (OAEP).516*517* Parameters are:518*519* - A hash function, used internally with the mask generation function520* (MGF1).521*522* - A label. The `label` pointer may be `NULL` if `label_len` is zero523* (an empty label, which is the default in PKCS#1 v2.2).524*525* - The private key.526*527* - The source and destination buffer. The buffer initially contains528* the encrypted message; the buffer contents are altered, and the529* decrypted message is written at the start of that buffer530* (decrypted message is always shorter than the encrypted message).531*532* If decryption fails in any way, then `*len` is unmodified, and the533* function returns 0. Otherwise, `*len` is set to the decrypted message534* length, and 1 is returned. The implementation is responsible for535* checking that the input message length matches the key modulus length,536* and that the padding is correct.537*538* Implementations MUST use constant-time check of the validity of the539* OAEP padding, at least until the leading byte and hash value have540* been checked. Whether overall decryption worked, and the length of541* the decrypted message, may leak.542*543* \param dig hash function to use with MGF1.544* \param label label value (may be `NULL` if `label_len` is zero).545* \param label_len label length, in bytes.546* \param sk RSA private key.547* \param data input/output buffer.548* \param len encrypted/decrypted message length.549* \return 1 on success, 0 on error.550*/551typedef uint32_t (*br_rsa_oaep_decrypt)(552const br_hash_class *dig, const void *label, size_t label_len,553const br_rsa_private_key *sk, void *data, size_t *len);554555/*556* RSA "i32" engine. Integers are internally represented as arrays of557* 32-bit integers, and the core multiplication primitive is the558* 32x32->64 multiplication.559*/560561/**562* \brief RSA public key engine "i32".563*564* \see br_rsa_public565*566* \param x operand to exponentiate.567* \param xlen length of the operand (in bytes).568* \param pk RSA public key.569* \return 1 on success, 0 on error.570*/571uint32_t br_rsa_i32_public(unsigned char *x, size_t xlen,572const br_rsa_public_key *pk);573574/**575* \brief RSA signature verification engine "i32" (PKCS#1 v1.5 signatures).576*577* \see br_rsa_pkcs1_vrfy578*579* \param x signature buffer.580* \param xlen signature length (in bytes).581* \param hash_oid encoded hash algorithm OID (or `NULL`).582* \param hash_len expected hash value length (in bytes).583* \param pk RSA public key.584* \param hash_out output buffer for the hash value.585* \return 1 on success, 0 on error.586*/587uint32_t br_rsa_i32_pkcs1_vrfy(const unsigned char *x, size_t xlen,588const unsigned char *hash_oid, size_t hash_len,589const br_rsa_public_key *pk, unsigned char *hash_out);590591/**592* \brief RSA signature verification engine "i32" (PSS signatures).593*594* \see br_rsa_pss_vrfy595*596* \param x signature buffer.597* \param xlen signature length (in bytes).598* \param hf_data hash function applied on the message.599* \param hf_mgf1 hash function to use with MGF1.600* \param hash hash value of the signed message.601* \param salt_len PSS salt length (in bytes).602* \param pk RSA public key.603* \return 1 on success, 0 on error.604*/605uint32_t br_rsa_i32_pss_vrfy(const unsigned char *x, size_t xlen,606const br_hash_class *hf_data, const br_hash_class *hf_mgf1,607const void *hash, size_t salt_len, const br_rsa_public_key *pk);608609/**610* \brief RSA private key engine "i32".611*612* \see br_rsa_private613*614* \param x operand to exponentiate.615* \param sk RSA private key.616* \return 1 on success, 0 on error.617*/618uint32_t br_rsa_i32_private(unsigned char *x,619const br_rsa_private_key *sk);620621/**622* \brief RSA signature generation engine "i32" (PKCS#1 v1.5 signatures).623*624* \see br_rsa_pkcs1_sign625*626* \param hash_oid encoded hash algorithm OID (or `NULL`).627* \param hash hash value.628* \param hash_len hash value length (in bytes).629* \param sk RSA private key.630* \param x output buffer for the hash value.631* \return 1 on success, 0 on error.632*/633uint32_t br_rsa_i32_pkcs1_sign(const unsigned char *hash_oid,634const unsigned char *hash, size_t hash_len,635const br_rsa_private_key *sk, unsigned char *x);636637/**638* \brief RSA signature generation engine "i32" (PSS signatures).639*640* \see br_rsa_pss_sign641*642* \param rng PRNG for salt generation (`NULL` if `salt_len` is zero).643* \param hf_data hash function used to hash the signed data.644* \param hf_mgf1 hash function to use with MGF1.645* \param hash hashed message.646* \param salt_len salt length (in bytes).647* \param sk RSA private key.648* \param x output buffer for the signature value.649* \return 1 on success, 0 on error.650*/651uint32_t br_rsa_i32_pss_sign(const br_prng_class **rng,652const br_hash_class *hf_data, const br_hash_class *hf_mgf1,653const unsigned char *hash_value, size_t salt_len,654const br_rsa_private_key *sk, unsigned char *x);655656/*657* RSA "i31" engine. Similar to i32, but only 31 bits are used per 32-bit658* word. This uses slightly more stack space (about 4% more) and code659* space, but it quite faster.660*/661662/**663* \brief RSA public key engine "i31".664*665* \see br_rsa_public666*667* \param x operand to exponentiate.668* \param xlen length of the operand (in bytes).669* \param pk RSA public key.670* \return 1 on success, 0 on error.671*/672uint32_t br_rsa_i31_public(unsigned char *x, size_t xlen,673const br_rsa_public_key *pk);674675/**676* \brief RSA signature verification engine "i31" (PKCS#1 v1.5 signatures).677*678* \see br_rsa_pkcs1_vrfy679*680* \param x signature buffer.681* \param xlen signature length (in bytes).682* \param hash_oid encoded hash algorithm OID (or `NULL`).683* \param hash_len expected hash value length (in bytes).684* \param pk RSA public key.685* \param hash_out output buffer for the hash value.686* \return 1 on success, 0 on error.687*/688uint32_t br_rsa_i31_pkcs1_vrfy(const unsigned char *x, size_t xlen,689const unsigned char *hash_oid, size_t hash_len,690const br_rsa_public_key *pk, unsigned char *hash_out);691692/**693* \brief RSA signature verification engine "i31" (PSS signatures).694*695* \see br_rsa_pss_vrfy696*697* \param x signature buffer.698* \param xlen signature length (in bytes).699* \param hf_data hash function applied on the message.700* \param hf_mgf1 hash function to use with MGF1.701* \param hash hash value of the signed message.702* \param salt_len PSS salt length (in bytes).703* \param pk RSA public key.704* \return 1 on success, 0 on error.705*/706uint32_t br_rsa_i31_pss_vrfy(const unsigned char *x, size_t xlen,707const br_hash_class *hf_data, const br_hash_class *hf_mgf1,708const void *hash, size_t salt_len, const br_rsa_public_key *pk);709710/**711* \brief RSA private key engine "i31".712*713* \see br_rsa_private714*715* \param x operand to exponentiate.716* \param sk RSA private key.717* \return 1 on success, 0 on error.718*/719uint32_t br_rsa_i31_private(unsigned char *x,720const br_rsa_private_key *sk);721722/**723* \brief RSA signature generation engine "i31" (PKCS#1 v1.5 signatures).724*725* \see br_rsa_pkcs1_sign726*727* \param hash_oid encoded hash algorithm OID (or `NULL`).728* \param hash hash value.729* \param hash_len hash value length (in bytes).730* \param sk RSA private key.731* \param x output buffer for the hash value.732* \return 1 on success, 0 on error.733*/734uint32_t br_rsa_i31_pkcs1_sign(const unsigned char *hash_oid,735const unsigned char *hash, size_t hash_len,736const br_rsa_private_key *sk, unsigned char *x);737738/**739* \brief RSA signature generation engine "i31" (PSS signatures).740*741* \see br_rsa_pss_sign742*743* \param rng PRNG for salt generation (`NULL` if `salt_len` is zero).744* \param hf_data hash function used to hash the signed data.745* \param hf_mgf1 hash function to use with MGF1.746* \param hash hashed message.747* \param salt_len salt length (in bytes).748* \param sk RSA private key.749* \param x output buffer for the signature value.750* \return 1 on success, 0 on error.751*/752uint32_t br_rsa_i31_pss_sign(const br_prng_class **rng,753const br_hash_class *hf_data, const br_hash_class *hf_mgf1,754const unsigned char *hash_value, size_t salt_len,755const br_rsa_private_key *sk, unsigned char *x);756757/*758* RSA "i62" engine. Similar to i31, but internal multiplication use759* 64x64->128 multiplications. This is available only on architecture760* that offer such an opcode.761*/762763/**764* \brief RSA public key engine "i62".765*766* This function is defined only on architecture that offer a 64x64->128767* opcode. Use `br_rsa_i62_public_get()` to dynamically obtain a pointer768* to that function.769*770* \see br_rsa_public771*772* \param x operand to exponentiate.773* \param xlen length of the operand (in bytes).774* \param pk RSA public key.775* \return 1 on success, 0 on error.776*/777uint32_t br_rsa_i62_public(unsigned char *x, size_t xlen,778const br_rsa_public_key *pk);779780/**781* \brief RSA signature verification engine "i62" (PKCS#1 v1.5 signatures).782*783* This function is defined only on architecture that offer a 64x64->128784* opcode. Use `br_rsa_i62_pkcs1_vrfy_get()` to dynamically obtain a pointer785* to that function.786*787* \see br_rsa_pkcs1_vrfy788*789* \param x signature buffer.790* \param xlen signature length (in bytes).791* \param hash_oid encoded hash algorithm OID (or `NULL`).792* \param hash_len expected hash value length (in bytes).793* \param pk RSA public key.794* \param hash_out output buffer for the hash value.795* \return 1 on success, 0 on error.796*/797uint32_t br_rsa_i62_pkcs1_vrfy(const unsigned char *x, size_t xlen,798const unsigned char *hash_oid, size_t hash_len,799const br_rsa_public_key *pk, unsigned char *hash_out);800801/**802* \brief RSA signature verification engine "i62" (PSS signatures).803*804* This function is defined only on architecture that offer a 64x64->128805* opcode. Use `br_rsa_i62_pss_vrfy_get()` to dynamically obtain a pointer806* to that function.807*808* \see br_rsa_pss_vrfy809*810* \param x signature buffer.811* \param xlen signature length (in bytes).812* \param hf_data hash function applied on the message.813* \param hf_mgf1 hash function to use with MGF1.814* \param hash hash value of the signed message.815* \param salt_len PSS salt length (in bytes).816* \param pk RSA public key.817* \return 1 on success, 0 on error.818*/819uint32_t br_rsa_i62_pss_vrfy(const unsigned char *x, size_t xlen,820const br_hash_class *hf_data, const br_hash_class *hf_mgf1,821const void *hash, size_t salt_len, const br_rsa_public_key *pk);822823/**824* \brief RSA private key engine "i62".825*826* This function is defined only on architecture that offer a 64x64->128827* opcode. Use `br_rsa_i62_private_get()` to dynamically obtain a pointer828* to that function.829*830* \see br_rsa_private831*832* \param x operand to exponentiate.833* \param sk RSA private key.834* \return 1 on success, 0 on error.835*/836uint32_t br_rsa_i62_private(unsigned char *x,837const br_rsa_private_key *sk);838839/**840* \brief RSA signature generation engine "i62" (PKCS#1 v1.5 signatures).841*842* This function is defined only on architecture that offer a 64x64->128843* opcode. Use `br_rsa_i62_pkcs1_sign_get()` to dynamically obtain a pointer844* to that function.845*846* \see br_rsa_pkcs1_sign847*848* \param hash_oid encoded hash algorithm OID (or `NULL`).849* \param hash hash value.850* \param hash_len hash value length (in bytes).851* \param sk RSA private key.852* \param x output buffer for the hash value.853* \return 1 on success, 0 on error.854*/855uint32_t br_rsa_i62_pkcs1_sign(const unsigned char *hash_oid,856const unsigned char *hash, size_t hash_len,857const br_rsa_private_key *sk, unsigned char *x);858859/**860* \brief RSA signature generation engine "i62" (PSS signatures).861*862* This function is defined only on architecture that offer a 64x64->128863* opcode. Use `br_rsa_i62_pss_sign_get()` to dynamically obtain a pointer864* to that function.865*866* \see br_rsa_pss_sign867*868* \param rng PRNG for salt generation (`NULL` if `salt_len` is zero).869* \param hf_data hash function used to hash the signed data.870* \param hf_mgf1 hash function to use with MGF1.871* \param hash hashed message.872* \param salt_len salt length (in bytes).873* \param sk RSA private key.874* \param x output buffer for the signature value.875* \return 1 on success, 0 on error.876*/877uint32_t br_rsa_i62_pss_sign(const br_prng_class **rng,878const br_hash_class *hf_data, const br_hash_class *hf_mgf1,879const unsigned char *hash_value, size_t salt_len,880const br_rsa_private_key *sk, unsigned char *x);881882/**883* \brief Get the RSA "i62" implementation (public key operations),884* if available.885*886* \return the implementation, or 0.887*/888br_rsa_public br_rsa_i62_public_get(void);889890/**891* \brief Get the RSA "i62" implementation (PKCS#1 v1.5 signature verification),892* if available.893*894* \return the implementation, or 0.895*/896br_rsa_pkcs1_vrfy br_rsa_i62_pkcs1_vrfy_get(void);897898/**899* \brief Get the RSA "i62" implementation (PSS signature verification),900* if available.901*902* \return the implementation, or 0.903*/904br_rsa_pss_vrfy br_rsa_i62_pss_vrfy_get(void);905906/**907* \brief Get the RSA "i62" implementation (private key operations),908* if available.909*910* \return the implementation, or 0.911*/912br_rsa_private br_rsa_i62_private_get(void);913914/**915* \brief Get the RSA "i62" implementation (PKCS#1 v1.5 signature generation),916* if available.917*918* \return the implementation, or 0.919*/920br_rsa_pkcs1_sign br_rsa_i62_pkcs1_sign_get(void);921922/**923* \brief Get the RSA "i62" implementation (PSS signature generation),924* if available.925*926* \return the implementation, or 0.927*/928br_rsa_pss_sign br_rsa_i62_pss_sign_get(void);929930/**931* \brief Get the RSA "i62" implementation (OAEP encryption),932* if available.933*934* \return the implementation, or 0.935*/936br_rsa_oaep_encrypt br_rsa_i62_oaep_encrypt_get(void);937938/**939* \brief Get the RSA "i62" implementation (OAEP decryption),940* if available.941*942* \return the implementation, or 0.943*/944br_rsa_oaep_decrypt br_rsa_i62_oaep_decrypt_get(void);945946/*947* RSA "i15" engine. Integers are represented as 15-bit integers, so948* the code uses only 32-bit multiplication (no 64-bit result), which949* is vastly faster (and constant-time) on the ARM Cortex M0/M0+.950*/951952/**953* \brief RSA public key engine "i15".954*955* \see br_rsa_public956*957* \param x operand to exponentiate.958* \param xlen length of the operand (in bytes).959* \param pk RSA public key.960* \return 1 on success, 0 on error.961*/962uint32_t br_rsa_i15_public(unsigned char *x, size_t xlen,963const br_rsa_public_key *pk);964965/**966* \brief RSA signature verification engine "i15" (PKCS#1 v1.5 signatures).967*968* \see br_rsa_pkcs1_vrfy969*970* \param x signature buffer.971* \param xlen signature length (in bytes).972* \param hash_oid encoded hash algorithm OID (or `NULL`).973* \param hash_len expected hash value length (in bytes).974* \param pk RSA public key.975* \param hash_out output buffer for the hash value.976* \return 1 on success, 0 on error.977*/978uint32_t br_rsa_i15_pkcs1_vrfy(const unsigned char *x, size_t xlen,979const unsigned char *hash_oid, size_t hash_len,980const br_rsa_public_key *pk, unsigned char *hash_out);981982/**983* \brief RSA signature verification engine "i15" (PSS signatures).984*985* \see br_rsa_pss_vrfy986*987* \param x signature buffer.988* \param xlen signature length (in bytes).989* \param hf_data hash function applied on the message.990* \param hf_mgf1 hash function to use with MGF1.991* \param hash hash value of the signed message.992* \param salt_len PSS salt length (in bytes).993* \param pk RSA public key.994* \return 1 on success, 0 on error.995*/996uint32_t br_rsa_i15_pss_vrfy(const unsigned char *x, size_t xlen,997const br_hash_class *hf_data, const br_hash_class *hf_mgf1,998const void *hash, size_t salt_len, const br_rsa_public_key *pk);9991000/**1001* \brief RSA private key engine "i15".1002*1003* \see br_rsa_private1004*1005* \param x operand to exponentiate.1006* \param sk RSA private key.1007* \return 1 on success, 0 on error.1008*/1009uint32_t br_rsa_i15_private(unsigned char *x,1010const br_rsa_private_key *sk);10111012/**1013* \brief RSA signature generation engine "i15" (PKCS#1 v1.5 signatures).1014*1015* \see br_rsa_pkcs1_sign1016*1017* \param hash_oid encoded hash algorithm OID (or `NULL`).1018* \param hash hash value.1019* \param hash_len hash value length (in bytes).1020* \param sk RSA private key.1021* \param x output buffer for the hash value.1022* \return 1 on success, 0 on error.1023*/1024uint32_t br_rsa_i15_pkcs1_sign(const unsigned char *hash_oid,1025const unsigned char *hash, size_t hash_len,1026const br_rsa_private_key *sk, unsigned char *x);10271028/**1029* \brief RSA signature generation engine "i15" (PSS signatures).1030*1031* \see br_rsa_pss_sign1032*1033* \param rng PRNG for salt generation (`NULL` if `salt_len` is zero).1034* \param hf_data hash function used to hash the signed data.1035* \param hf_mgf1 hash function to use with MGF1.1036* \param hash hashed message.1037* \param salt_len salt length (in bytes).1038* \param sk RSA private key.1039* \param x output buffer for the signature value.1040* \return 1 on success, 0 on error.1041*/1042uint32_t br_rsa_i15_pss_sign(const br_prng_class **rng,1043const br_hash_class *hf_data, const br_hash_class *hf_mgf1,1044const unsigned char *hash_value, size_t salt_len,1045const br_rsa_private_key *sk, unsigned char *x);10461047/**1048* \brief Get "default" RSA implementation (public-key operations).1049*1050* This returns the preferred implementation of RSA (public-key operations)1051* on the current system.1052*1053* \return the default implementation.1054*/1055br_rsa_public br_rsa_public_get_default(void);10561057/**1058* \brief Get "default" RSA implementation (private-key operations).1059*1060* This returns the preferred implementation of RSA (private-key operations)1061* on the current system.1062*1063* \return the default implementation.1064*/1065br_rsa_private br_rsa_private_get_default(void);10661067/**1068* \brief Get "default" RSA implementation (PKCS#1 v1.5 signature verification).1069*1070* This returns the preferred implementation of RSA (signature verification)1071* on the current system.1072*1073* \return the default implementation.1074*/1075br_rsa_pkcs1_vrfy br_rsa_pkcs1_vrfy_get_default(void);10761077/**1078* \brief Get "default" RSA implementation (PSS signature verification).1079*1080* This returns the preferred implementation of RSA (signature verification)1081* on the current system.1082*1083* \return the default implementation.1084*/1085br_rsa_pss_vrfy br_rsa_pss_vrfy_get_default(void);10861087/**1088* \brief Get "default" RSA implementation (PKCS#1 v1.5 signature generation).1089*1090* This returns the preferred implementation of RSA (signature generation)1091* on the current system.1092*1093* \return the default implementation.1094*/1095br_rsa_pkcs1_sign br_rsa_pkcs1_sign_get_default(void);10961097/**1098* \brief Get "default" RSA implementation (PSS signature generation).1099*1100* This returns the preferred implementation of RSA (signature generation)1101* on the current system.1102*1103* \return the default implementation.1104*/1105br_rsa_pss_sign br_rsa_pss_sign_get_default(void);11061107/**1108* \brief Get "default" RSA implementation (OAEP encryption).1109*1110* This returns the preferred implementation of RSA (OAEP encryption)1111* on the current system.1112*1113* \return the default implementation.1114*/1115br_rsa_oaep_encrypt br_rsa_oaep_encrypt_get_default(void);11161117/**1118* \brief Get "default" RSA implementation (OAEP decryption).1119*1120* This returns the preferred implementation of RSA (OAEP decryption)1121* on the current system.1122*1123* \return the default implementation.1124*/1125br_rsa_oaep_decrypt br_rsa_oaep_decrypt_get_default(void);11261127/**1128* \brief RSA decryption helper, for SSL/TLS.1129*1130* This function performs the RSA decryption for a RSA-based key exchange1131* in a SSL/TLS server. The provided RSA engine is used. The `data`1132* parameter points to the value to decrypt, of length `len` bytes. On1133* success, the 48-byte pre-master secret is copied into `data`, starting1134* at the first byte of that buffer; on error, the contents of `data`1135* become indeterminate.1136*1137* This function first checks that the provided value length (`len`) is1138* not lower than 59 bytes, and matches the RSA modulus length; if neither1139* of this property is met, then this function returns 0 and the buffer1140* is unmodified.1141*1142* Otherwise, decryption and then padding verification are performed, both1143* in constant-time. A decryption error, or a bad padding, or an1144* incorrect decrypted value length are reported with a returned value of1145* 0; on success, 1 is returned. The caller (SSL server engine) is supposed1146* to proceed with a random pre-master secret in case of error.1147*1148* \param core RSA private key engine.1149* \param sk RSA private key.1150* \param data input/output buffer.1151* \param len length (in bytes) of the data to decrypt.1152* \return 1 on success, 0 on error.1153*/1154uint32_t br_rsa_ssl_decrypt(br_rsa_private core, const br_rsa_private_key *sk,1155unsigned char *data, size_t len);11561157/**1158* \brief RSA encryption (OAEP) with the "i15" engine.1159*1160* \see br_rsa_oaep_encrypt1161*1162* \param rnd source of random bytes.1163* \param dig hash function to use with MGF1.1164* \param label label value (may be `NULL` if `label_len` is zero).1165* \param label_len label length, in bytes.1166* \param pk RSA public key.1167* \param dst destination buffer.1168* \param dst_max_len destination buffer length (maximum encrypted data size).1169* \param src message to encrypt.1170* \param src_len source message length (in bytes).1171* \return encrypted message length (in bytes), or 0 on error.1172*/1173size_t br_rsa_i15_oaep_encrypt(1174const br_prng_class **rnd, const br_hash_class *dig,1175const void *label, size_t label_len,1176const br_rsa_public_key *pk,1177void *dst, size_t dst_max_len,1178const void *src, size_t src_len);11791180/**1181* \brief RSA decryption (OAEP) with the "i15" engine.1182*1183* \see br_rsa_oaep_decrypt1184*1185* \param dig hash function to use with MGF1.1186* \param label label value (may be `NULL` if `label_len` is zero).1187* \param label_len label length, in bytes.1188* \param sk RSA private key.1189* \param data input/output buffer.1190* \param len encrypted/decrypted message length.1191* \return 1 on success, 0 on error.1192*/1193uint32_t br_rsa_i15_oaep_decrypt(1194const br_hash_class *dig, const void *label, size_t label_len,1195const br_rsa_private_key *sk, void *data, size_t *len);11961197/**1198* \brief RSA encryption (OAEP) with the "i31" engine.1199*1200* \see br_rsa_oaep_encrypt1201*1202* \param rnd source of random bytes.1203* \param dig hash function to use with MGF1.1204* \param label label value (may be `NULL` if `label_len` is zero).1205* \param label_len label length, in bytes.1206* \param pk RSA public key.1207* \param dst destination buffer.1208* \param dst_max_len destination buffer length (maximum encrypted data size).1209* \param src message to encrypt.1210* \param src_len source message length (in bytes).1211* \return encrypted message length (in bytes), or 0 on error.1212*/1213size_t br_rsa_i31_oaep_encrypt(1214const br_prng_class **rnd, const br_hash_class *dig,1215const void *label, size_t label_len,1216const br_rsa_public_key *pk,1217void *dst, size_t dst_max_len,1218const void *src, size_t src_len);12191220/**1221* \brief RSA decryption (OAEP) with the "i31" engine.1222*1223* \see br_rsa_oaep_decrypt1224*1225* \param dig hash function to use with MGF1.1226* \param label label value (may be `NULL` if `label_len` is zero).1227* \param label_len label length, in bytes.1228* \param sk RSA private key.1229* \param data input/output buffer.1230* \param len encrypted/decrypted message length.1231* \return 1 on success, 0 on error.1232*/1233uint32_t br_rsa_i31_oaep_decrypt(1234const br_hash_class *dig, const void *label, size_t label_len,1235const br_rsa_private_key *sk, void *data, size_t *len);12361237/**1238* \brief RSA encryption (OAEP) with the "i32" engine.1239*1240* \see br_rsa_oaep_encrypt1241*1242* \param rnd source of random bytes.1243* \param dig hash function to use with MGF1.1244* \param label label value (may be `NULL` if `label_len` is zero).1245* \param label_len label length, in bytes.1246* \param pk RSA public key.1247* \param dst destination buffer.1248* \param dst_max_len destination buffer length (maximum encrypted data size).1249* \param src message to encrypt.1250* \param src_len source message length (in bytes).1251* \return encrypted message length (in bytes), or 0 on error.1252*/1253size_t br_rsa_i32_oaep_encrypt(1254const br_prng_class **rnd, const br_hash_class *dig,1255const void *label, size_t label_len,1256const br_rsa_public_key *pk,1257void *dst, size_t dst_max_len,1258const void *src, size_t src_len);12591260/**1261* \brief RSA decryption (OAEP) with the "i32" engine.1262*1263* \see br_rsa_oaep_decrypt1264*1265* \param dig hash function to use with MGF1.1266* \param label label value (may be `NULL` if `label_len` is zero).1267* \param label_len label length, in bytes.1268* \param sk RSA private key.1269* \param data input/output buffer.1270* \param len encrypted/decrypted message length.1271* \return 1 on success, 0 on error.1272*/1273uint32_t br_rsa_i32_oaep_decrypt(1274const br_hash_class *dig, const void *label, size_t label_len,1275const br_rsa_private_key *sk, void *data, size_t *len);12761277/**1278* \brief RSA encryption (OAEP) with the "i62" engine.1279*1280* This function is defined only on architecture that offer a 64x64->1281281* opcode. Use `br_rsa_i62_oaep_encrypt_get()` to dynamically obtain a pointer1282* to that function.1283*1284* \see br_rsa_oaep_encrypt1285*1286* \param rnd source of random bytes.1287* \param dig hash function to use with MGF1.1288* \param label label value (may be `NULL` if `label_len` is zero).1289* \param label_len label length, in bytes.1290* \param pk RSA public key.1291* \param dst destination buffer.1292* \param dst_max_len destination buffer length (maximum encrypted data size).1293* \param src message to encrypt.1294* \param src_len source message length (in bytes).1295* \return encrypted message length (in bytes), or 0 on error.1296*/1297size_t br_rsa_i62_oaep_encrypt(1298const br_prng_class **rnd, const br_hash_class *dig,1299const void *label, size_t label_len,1300const br_rsa_public_key *pk,1301void *dst, size_t dst_max_len,1302const void *src, size_t src_len);13031304/**1305* \brief RSA decryption (OAEP) with the "i62" engine.1306*1307* This function is defined only on architecture that offer a 64x64->1281308* opcode. Use `br_rsa_i62_oaep_decrypt_get()` to dynamically obtain a pointer1309* to that function.1310*1311* \see br_rsa_oaep_decrypt1312*1313* \param dig hash function to use with MGF1.1314* \param label label value (may be `NULL` if `label_len` is zero).1315* \param label_len label length, in bytes.1316* \param sk RSA private key.1317* \param data input/output buffer.1318* \param len encrypted/decrypted message length.1319* \return 1 on success, 0 on error.1320*/1321uint32_t br_rsa_i62_oaep_decrypt(1322const br_hash_class *dig, const void *label, size_t label_len,1323const br_rsa_private_key *sk, void *data, size_t *len);13241325/**1326* \brief Get buffer size to hold RSA private key elements.1327*1328* This macro returns the length (in bytes) of the buffer needed to1329* receive the elements of a RSA private key, as generated by one of1330* the `br_rsa_*_keygen()` functions. If the provided size is a constant1331* expression, then the whole macro evaluates to a constant expression.1332*1333* \param size target key size (modulus size, in bits)1334* \return the length of the private key buffer, in bytes.1335*/1336#define BR_RSA_KBUF_PRIV_SIZE(size) (5 * (((size) + 15) >> 4))13371338/**1339* \brief Get buffer size to hold RSA public key elements.1340*1341* This macro returns the length (in bytes) of the buffer needed to1342* receive the elements of a RSA public key, as generated by one of1343* the `br_rsa_*_keygen()` functions. If the provided size is a constant1344* expression, then the whole macro evaluates to a constant expression.1345*1346* \param size target key size (modulus size, in bits)1347* \return the length of the public key buffer, in bytes.1348*/1349#define BR_RSA_KBUF_PUB_SIZE(size) (4 + (((size) + 7) >> 3))13501351/**1352* \brief Type for RSA key pair generator implementation.1353*1354* This function generates a new RSA key pair whose modulus has bit1355* length `size` bits. The private key elements are written in the1356* `kbuf_priv` buffer, and pointer values and length fields to these1357* elements are populated in the provided private key structure `sk`.1358* Similarly, the public key elements are written in `kbuf_pub`, with1359* pointers and lengths set in `pk`.1360*1361* If `pk` is `NULL`, then `kbuf_pub` may be `NULL`, and only the1362* private key is set.1363*1364* If `pubexp` is not zero, then its value will be used as public1365* exponent. Valid RSA public exponent values are odd integers1366* greater than 1. If `pubexp` is zero, then the public exponent will1367* have value 3.1368*1369* The provided PRNG (`rng_ctx`) must have already been initialized1370* and seeded.1371*1372* Returned value is 1 on success, 0 on error. An error is reported1373* if the requested range is outside of the supported key sizes, or1374* if an invalid non-zero public exponent value is provided. Supported1375* range starts at 512 bits, and up to an implementation-defined1376* maximum (by default 4096 bits). Note that key sizes up to 768 bits1377* have been broken in practice, and sizes lower than 2048 bits are1378* usually considered to be weak and should not be used.1379*1380* \param rng_ctx source PRNG context (already initialized)1381* \param sk RSA private key structure (destination)1382* \param kbuf_priv buffer for private key elements1383* \param pk RSA public key structure (destination), or `NULL`1384* \param kbuf_pub buffer for public key elements, or `NULL`1385* \param size target RSA modulus size (in bits)1386* \param pubexp public exponent to use, or zero1387* \return 1 on success, 0 on error (invalid parameters)1388*/1389typedef uint32_t (*br_rsa_keygen)(1390const br_prng_class **rng_ctx,1391br_rsa_private_key *sk, void *kbuf_priv,1392br_rsa_public_key *pk, void *kbuf_pub,1393unsigned size, uint32_t pubexp);13941395/**1396* \brief RSA key pair generation with the "i15" engine.1397*1398* \see br_rsa_keygen1399*1400* \param rng_ctx source PRNG context (already initialized)1401* \param sk RSA private key structure (destination)1402* \param kbuf_priv buffer for private key elements1403* \param pk RSA public key structure (destination), or `NULL`1404* \param kbuf_pub buffer for public key elements, or `NULL`1405* \param size target RSA modulus size (in bits)1406* \param pubexp public exponent to use, or zero1407* \return 1 on success, 0 on error (invalid parameters)1408*/1409uint32_t br_rsa_i15_keygen(1410const br_prng_class **rng_ctx,1411br_rsa_private_key *sk, void *kbuf_priv,1412br_rsa_public_key *pk, void *kbuf_pub,1413unsigned size, uint32_t pubexp);14141415/**1416* \brief RSA key pair generation with the "i31" engine.1417*1418* \see br_rsa_keygen1419*1420* \param rng_ctx source PRNG context (already initialized)1421* \param sk RSA private key structure (destination)1422* \param kbuf_priv buffer for private key elements1423* \param pk RSA public key structure (destination), or `NULL`1424* \param kbuf_pub buffer for public key elements, or `NULL`1425* \param size target RSA modulus size (in bits)1426* \param pubexp public exponent to use, or zero1427* \return 1 on success, 0 on error (invalid parameters)1428*/1429uint32_t br_rsa_i31_keygen(1430const br_prng_class **rng_ctx,1431br_rsa_private_key *sk, void *kbuf_priv,1432br_rsa_public_key *pk, void *kbuf_pub,1433unsigned size, uint32_t pubexp);14341435/**1436* \brief RSA key pair generation with the "i62" engine.1437*1438* This function is defined only on architecture that offer a 64x64->1281439* opcode. Use `br_rsa_i62_keygen_get()` to dynamically obtain a pointer1440* to that function.1441*1442* \see br_rsa_keygen1443*1444* \param rng_ctx source PRNG context (already initialized)1445* \param sk RSA private key structure (destination)1446* \param kbuf_priv buffer for private key elements1447* \param pk RSA public key structure (destination), or `NULL`1448* \param kbuf_pub buffer for public key elements, or `NULL`1449* \param size target RSA modulus size (in bits)1450* \param pubexp public exponent to use, or zero1451* \return 1 on success, 0 on error (invalid parameters)1452*/1453uint32_t br_rsa_i62_keygen(1454const br_prng_class **rng_ctx,1455br_rsa_private_key *sk, void *kbuf_priv,1456br_rsa_public_key *pk, void *kbuf_pub,1457unsigned size, uint32_t pubexp);14581459/**1460* \brief Get the RSA "i62" implementation (key pair generation),1461* if available.1462*1463* \return the implementation, or 0.1464*/1465br_rsa_keygen br_rsa_i62_keygen_get(void);14661467/**1468* \brief Get "default" RSA implementation (key pair generation).1469*1470* This returns the preferred implementation of RSA (key pair generation)1471* on the current system.1472*1473* \return the default implementation.1474*/1475br_rsa_keygen br_rsa_keygen_get_default(void);14761477/**1478* \brief Type for a modulus computing function.1479*1480* Such a function computes the public modulus from the private key. The1481* encoded modulus (unsigned big-endian) is written on `n`, and the size1482* (in bytes) is returned. If `n` is `NULL`, then the size is returned but1483* the modulus itself is not computed.1484*1485* If the key size exceeds an internal limit, 0 is returned.1486*1487* \param n destination buffer (or `NULL`).1488* \param sk RSA private key.1489* \return the modulus length (in bytes), or 0.1490*/1491typedef size_t (*br_rsa_compute_modulus)(void *n, const br_rsa_private_key *sk);14921493/**1494* \brief Recompute RSA modulus ("i15" engine).1495*1496* \see br_rsa_compute_modulus1497*1498* \param n destination buffer (or `NULL`).1499* \param sk RSA private key.1500* \return the modulus length (in bytes), or 0.1501*/1502size_t br_rsa_i15_compute_modulus(void *n, const br_rsa_private_key *sk);15031504/**1505* \brief Recompute RSA modulus ("i31" engine).1506*1507* \see br_rsa_compute_modulus1508*1509* \param n destination buffer (or `NULL`).1510* \param sk RSA private key.1511* \return the modulus length (in bytes), or 0.1512*/1513size_t br_rsa_i31_compute_modulus(void *n, const br_rsa_private_key *sk);15141515/**1516* \brief Get "default" RSA implementation (recompute modulus).1517*1518* This returns the preferred implementation of RSA (recompute modulus)1519* on the current system.1520*1521* \return the default implementation.1522*/1523br_rsa_compute_modulus br_rsa_compute_modulus_get_default(void);15241525/**1526* \brief Type for a public exponent computing function.1527*1528* Such a function recomputes the public exponent from the private key.1529* 0 is returned if any of the following occurs:1530*1531* - Either `p` or `q` is not equal to 3 modulo 4.1532*1533* - The public exponent does not fit on 32 bits.1534*1535* - An internal limit is exceeded.1536*1537* - The private key is invalid in some way.1538*1539* For all private keys produced by the key generator functions1540* (`br_rsa_keygen` type), this function succeeds and returns the true1541* public exponent. The public exponent is always an odd integer greater1542* than 1.1543*1544* \return the public exponent, or 0.1545*/1546typedef uint32_t (*br_rsa_compute_pubexp)(const br_rsa_private_key *sk);15471548/**1549* \brief Recompute RSA public exponent ("i15" engine).1550*1551* \see br_rsa_compute_pubexp1552*1553* \return the public exponent, or 0.1554*/1555uint32_t br_rsa_i15_compute_pubexp(const br_rsa_private_key *sk);15561557/**1558* \brief Recompute RSA public exponent ("i31" engine).1559*1560* \see br_rsa_compute_pubexp1561*1562* \return the public exponent, or 0.1563*/1564uint32_t br_rsa_i31_compute_pubexp(const br_rsa_private_key *sk);15651566/**1567* \brief Get "default" RSA implementation (recompute public exponent).1568*1569* This returns the preferred implementation of RSA (recompute public1570* exponent) on the current system.1571*1572* \return the default implementation.1573*/1574br_rsa_compute_pubexp br_rsa_compute_pubexp_get_default(void);15751576/**1577* \brief Type for a private exponent computing function.1578*1579* An RSA private key (`br_rsa_private_key`) contains two reduced1580* private exponents, which are sufficient to perform private key1581* operations. However, standard encoding formats for RSA private keys1582* require also a copy of the complete private exponent (non-reduced),1583* which this function recomputes.1584*1585* This function suceeds if all the following conditions hold:1586*1587* - Both private factors `p` and `q` are equal to 3 modulo 4.1588*1589* - The provided public exponent `pubexp` is correct, and, in particular,1590* is odd, relatively prime to `p-1` and `q-1`, and greater than 1.1591*1592* - No internal storage limit is exceeded.1593*1594* For all private keys produced by the key generator functions1595* (`br_rsa_keygen` type), this function succeeds. Note that the API1596* restricts the public exponent to a maximum size of 32 bits.1597*1598* The encoded private exponent is written in `d` (unsigned big-endian1599* convention), and the length (in bytes) is returned. If `d` is `NULL`,1600* then the exponent is not written anywhere, but the length is still1601* returned. On error, 0 is returned.1602*1603* Not all error conditions are detected when `d` is `NULL`; therefore, the1604* returned value shall be checked also when actually producing the value.1605*1606* \param d destination buffer (or `NULL`).1607* \param sk RSA private key.1608* \param pubexp the public exponent.1609* \return the private exponent length (in bytes), or 0.1610*/1611typedef size_t (*br_rsa_compute_privexp)(void *d,1612const br_rsa_private_key *sk, uint32_t pubexp);16131614/**1615* \brief Recompute RSA private exponent ("i15" engine).1616*1617* \see br_rsa_compute_privexp1618*1619* \param d destination buffer (or `NULL`).1620* \param sk RSA private key.1621* \param pubexp the public exponent.1622* \return the private exponent length (in bytes), or 0.1623*/1624size_t br_rsa_i15_compute_privexp(void *d,1625const br_rsa_private_key *sk, uint32_t pubexp);16261627/**1628* \brief Recompute RSA private exponent ("i31" engine).1629*1630* \see br_rsa_compute_privexp1631*1632* \param d destination buffer (or `NULL`).1633* \param sk RSA private key.1634* \param pubexp the public exponent.1635* \return the private exponent length (in bytes), or 0.1636*/1637size_t br_rsa_i31_compute_privexp(void *d,1638const br_rsa_private_key *sk, uint32_t pubexp);16391640/**1641* \brief Get "default" RSA implementation (recompute private exponent).1642*1643* This returns the preferred implementation of RSA (recompute private1644* exponent) on the current system.1645*1646* \return the default implementation.1647*/1648br_rsa_compute_privexp br_rsa_compute_privexp_get_default(void);16491650#ifdef __cplusplus1651}1652#endif16531654#endif165516561657