Latest compatible version of Classicube from the original GitHub repository (https://github.com/ClassiCube/ClassiCube) that can be compiled on Classicube for PowerMac PPC running Mac OS X 10.4.
This commit is contained in:
140
third_party/bearssl/inc/bearssl.h
vendored
Normal file
140
third_party/bearssl/inc/bearssl.h
vendored
Normal file
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef BR_BEARSSL_H__
|
||||
#define BR_BEARSSL_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/** \mainpage BearSSL API
|
||||
*
|
||||
* # API Layout
|
||||
*
|
||||
* The functions and structures defined by the BearSSL API are located
|
||||
* in various header files:
|
||||
*
|
||||
* | Header file | Elements |
|
||||
* | :-------------- | :------------------------------------------------ |
|
||||
* | bearssl_hash.h | Hash functions |
|
||||
* | bearssl_hmac.h | HMAC |
|
||||
* | bearssl_rand.h | Pseudorandom byte generators |
|
||||
* | bearssl_prf.h | PRF implementations (for SSL/TLS) |
|
||||
* | bearssl_block.h | Symmetric encryption |
|
||||
* | bearssl_aead.h | AEAD algorithms (combined encryption + MAC) |
|
||||
* | bearssl_rsa.h | RSA encryption and signatures |
|
||||
* | bearssl_ec.h | Elliptic curves support (including ECDSA) |
|
||||
* | bearssl_ssl.h | SSL/TLS engine interface |
|
||||
* | bearssl_x509.h | X.509 certificate decoding and validation |
|
||||
*
|
||||
* Applications using BearSSL are supposed to simply include `bearssl.h`
|
||||
* as follows:
|
||||
*
|
||||
* #include <bearssl.h>
|
||||
*
|
||||
* The `bearssl.h` file itself includes all the other header files. It is
|
||||
* possible to include specific header files, but it has no practical
|
||||
* advantage for the application. The API is separated into separate
|
||||
* header files only for documentation convenience.
|
||||
*
|
||||
*
|
||||
* # Conventions
|
||||
*
|
||||
* ## MUST and SHALL
|
||||
*
|
||||
* In all descriptions, the usual "MUST", "SHALL", "MAY",... terminology
|
||||
* is used. Failure to meet requirements expressed with a "MUST" or
|
||||
* "SHALL" implies undefined behaviour, which means that segmentation
|
||||
* faults, buffer overflows, and other similar adverse events, may occur.
|
||||
*
|
||||
* In general, BearSSL is not very forgiving of programming errors, and
|
||||
* does not include much failsafes or error reporting when the problem
|
||||
* does not arise from external transient conditions, and can be fixed
|
||||
* only in the application code. This is done so in order to make the
|
||||
* total code footprint lighter.
|
||||
*
|
||||
*
|
||||
* ## `NULL` values
|
||||
*
|
||||
* Function parameters with a pointer type shall not be `NULL` unless
|
||||
* explicitly authorised by the documentation. As an exception, when
|
||||
* the pointer aims at a sequence of bytes and is accompanied with
|
||||
* a length parameter, and the length is zero (meaning that there is
|
||||
* no byte at all to retrieve), then the pointer may be `NULL` even if
|
||||
* not explicitly allowed.
|
||||
*
|
||||
*
|
||||
* ## Memory Allocation
|
||||
*
|
||||
* BearSSL does not perform dynamic memory allocation. This implies that
|
||||
* for any functionality that requires a non-transient state, the caller
|
||||
* is responsible for allocating the relevant context structure. Such
|
||||
* allocation can be done in any appropriate area, including static data
|
||||
* segments, the heap, and the stack, provided that proper alignment is
|
||||
* respected. The header files define these context structures
|
||||
* (including size and contents), so the C compiler should handle
|
||||
* alignment automatically.
|
||||
*
|
||||
* Since there is no dynamic resource allocation, there is also nothing to
|
||||
* release. When the calling code is done with a BearSSL feature, it
|
||||
* may simple release the context structures it allocated itself, with
|
||||
* no "close function" to call. If the context structures were allocated
|
||||
* on the stack (as local variables), then even that release operation is
|
||||
* implicit.
|
||||
*
|
||||
*
|
||||
* ## Structure Contents
|
||||
*
|
||||
* Except when explicitly indicated, structure contents are opaque: they
|
||||
* are included in the header files so that calling code may know the
|
||||
* structure sizes and alignment requirements, but callers SHALL NOT
|
||||
* access individual fields directly. For fields that are supposed to
|
||||
* be read from or written to, the API defines accessor functions (the
|
||||
* simplest of these accessor functions are defined as `static inline`
|
||||
* functions, and the C compiler will optimise them away).
|
||||
*
|
||||
*
|
||||
* # API Usage
|
||||
*
|
||||
* BearSSL usage for running a SSL/TLS client or server is described
|
||||
* on the [BearSSL Web site](https://www.bearssl.org/api1.html). The
|
||||
* BearSSL source archive also comes with sample code.
|
||||
*/
|
||||
|
||||
#if defined __ARMCC__ && defined __SYMBIAN32__
|
||||
#define inline __inline
|
||||
#endif
|
||||
|
||||
#include "bearssl_hash.h"
|
||||
#include "bearssl_hmac.h"
|
||||
#include "bearssl_rand.h"
|
||||
#include "bearssl_prf.h"
|
||||
#include "bearssl_block.h"
|
||||
#include "bearssl_aead.h"
|
||||
#include "bearssl_rsa.h"
|
||||
#include "bearssl_ec.h"
|
||||
#include "bearssl_ssl.h"
|
||||
#include "bearssl_x509.h"
|
||||
|
||||
#endif
|
||||
746
third_party/bearssl/inc/bearssl_aead.h
vendored
Normal file
746
third_party/bearssl/inc/bearssl_aead.h
vendored
Normal file
@@ -0,0 +1,746 @@
|
||||
/*
|
||||
* Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef BR_BEARSSL_AEAD_H__
|
||||
#define BR_BEARSSL_AEAD_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "bearssl_block.h"
|
||||
#include "bearssl_hash.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** \file bearssl_aead.h
|
||||
*
|
||||
* # Authenticated Encryption with Additional Data
|
||||
*
|
||||
* This file documents the API for AEAD encryption.
|
||||
*
|
||||
*
|
||||
* ## Procedural API
|
||||
*
|
||||
* An AEAD algorithm processes messages and provides confidentiality
|
||||
* (encryption) and checked integrity (MAC). It uses the following
|
||||
* parameters:
|
||||
*
|
||||
* - A symmetric key. Exact size depends on the AEAD algorithm.
|
||||
*
|
||||
* - A nonce (IV). Size depends on the AEAD algorithm; for most
|
||||
* algorithms, it is crucial for security that any given nonce
|
||||
* value is never used twice for the same key and distinct
|
||||
* messages.
|
||||
*
|
||||
* - Data to encrypt and protect.
|
||||
*
|
||||
* - Additional authenticated data, which is covered by the MAC but
|
||||
* otherwise left untouched (i.e. not encrypted).
|
||||
*
|
||||
* The AEAD algorithm encrypts the data, and produces an authentication
|
||||
* tag. It is assumed that the encrypted data, the tag, the additional
|
||||
* authenticated data and the nonce are sent to the receiver; the
|
||||
* additional data and the nonce may be implicit (e.g. using elements of
|
||||
* the underlying transport protocol, such as record sequence numbers).
|
||||
* The receiver will recompute the tag value and compare it with the one
|
||||
* received; if they match, then the data is correct, and can be
|
||||
* decrypted and used; otherwise, at least one of the elements was
|
||||
* altered in transit, normally leading to wholesale rejection of the
|
||||
* complete message.
|
||||
*
|
||||
* For each AEAD algorithm, identified by a symbolic name (hereafter
|
||||
* denoted as "`xxx`"), the following functions are defined:
|
||||
*
|
||||
* - `br_xxx_init()`
|
||||
*
|
||||
* Initialise the AEAD algorithm, on a provided context structure.
|
||||
* Exact parameters depend on the algorithm, and may include
|
||||
* pointers to extra implementations and context structures. The
|
||||
* secret key is provided at this point, either directly or
|
||||
* indirectly.
|
||||
*
|
||||
* - `br_xxx_reset()`
|
||||
*
|
||||
* Start a new AEAD computation. The nonce value is provided as
|
||||
* parameter to this function.
|
||||
*
|
||||
* - `br_xxx_aad_inject()`
|
||||
*
|
||||
* Inject some additional authenticated data. Additional data may
|
||||
* be provided in several chunks of arbitrary length.
|
||||
*
|
||||
* - `br_xxx_flip()`
|
||||
*
|
||||
* This function MUST be called after injecting all additional
|
||||
* authenticated data, and before beginning to encrypt the plaintext
|
||||
* (or decrypt the ciphertext).
|
||||
*
|
||||
* - `br_xxx_run()`
|
||||
*
|
||||
* Process some plaintext (to encrypt) or ciphertext (to decrypt).
|
||||
* Encryption/decryption is done in place. Data may be provided in
|
||||
* several chunks of arbitrary length.
|
||||
*
|
||||
* - `br_xxx_get_tag()`
|
||||
*
|
||||
* Compute the authentication tag. All message data (encrypted or
|
||||
* decrypted) must have been injected at that point. Also, this
|
||||
* call may modify internal context elements, so it may be called
|
||||
* only once for a given AEAD computation.
|
||||
*
|
||||
* - `br_xxx_check_tag()`
|
||||
*
|
||||
* An alternative to `br_xxx_get_tag()`, meant to be used by the
|
||||
* receiver: the authentication tag is internally recomputed, and
|
||||
* compared with the one provided as parameter.
|
||||
*
|
||||
* This API makes the following assumptions on the AEAD algorithm:
|
||||
*
|
||||
* - Encryption does not expand the size of the ciphertext; there is
|
||||
* no padding. This is true of most modern AEAD modes such as GCM.
|
||||
*
|
||||
* - The additional authenticated data must be processed first,
|
||||
* before the encrypted/decrypted data.
|
||||
*
|
||||
* - Nonce, plaintext and additional authenticated data all consist
|
||||
* in an integral number of bytes. There is no provision to use
|
||||
* elements whose length in bits is not a multiple of 8.
|
||||
*
|
||||
* Each AEAD algorithm has its own requirements and limits on the sizes
|
||||
* of additional data and plaintext. This API does not provide any
|
||||
* way to report invalid usage; it is up to the caller to ensure that
|
||||
* the provided key, nonce, and data elements all fit the algorithm's
|
||||
* requirements.
|
||||
*
|
||||
*
|
||||
* ## Object-Oriented API
|
||||
*
|
||||
* Each context structure begins with a field (called `vtable`) that
|
||||
* points to an instance of a structure that references the relevant
|
||||
* functions through pointers. Each such structure contains the
|
||||
* following:
|
||||
*
|
||||
* - `reset`
|
||||
*
|
||||
* Pointer to the reset function, that allows starting a new
|
||||
* computation.
|
||||
*
|
||||
* - `aad_inject`
|
||||
*
|
||||
* Pointer to the additional authenticated data injection function.
|
||||
*
|
||||
* - `flip`
|
||||
*
|
||||
* Pointer to the function that transitions from additional data
|
||||
* to main message data processing.
|
||||
*
|
||||
* - `get_tag`
|
||||
*
|
||||
* Pointer to the function that computes and returns the tag.
|
||||
*
|
||||
* - `check_tag`
|
||||
*
|
||||
* Pointer to the function that computes and verifies the tag against
|
||||
* a received value.
|
||||
*
|
||||
* Note that there is no OOP method for context initialisation: the
|
||||
* various AEAD algorithms have different requirements that would not
|
||||
* map well to a single initialisation API.
|
||||
*
|
||||
* The OOP API is not provided for CCM, due to its specific requirements
|
||||
* (length of plaintext must be known in advance).
|
||||
*/
|
||||
|
||||
/**
|
||||
* \brief Class type of an AEAD algorithm.
|
||||
*/
|
||||
typedef struct br_aead_class_ br_aead_class;
|
||||
struct br_aead_class_ {
|
||||
|
||||
/**
|
||||
* \brief Size (in bytes) of authentication tags created by
|
||||
* this AEAD algorithm.
|
||||
*/
|
||||
size_t tag_size;
|
||||
|
||||
/**
|
||||
* \brief Reset an AEAD context.
|
||||
*
|
||||
* This function resets an already initialised AEAD context for
|
||||
* a new computation run. Implementations and keys are
|
||||
* conserved. This function can be called at any time; it
|
||||
* cancels any ongoing AEAD computation that uses the provided
|
||||
* context structure.
|
||||
|
||||
* The provided IV is a _nonce_. Each AEAD algorithm has its
|
||||
* own requirements on IV size and contents; for most of them,
|
||||
* it is crucial to security that each nonce value is used
|
||||
* only once for a given secret key.
|
||||
*
|
||||
* \param cc AEAD context structure.
|
||||
* \param iv AEAD nonce to use.
|
||||
* \param len AEAD nonce length (in bytes).
|
||||
*/
|
||||
void (*reset)(const br_aead_class **cc, const void *iv, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Inject additional authenticated data.
|
||||
*
|
||||
* The provided data is injected into a running AEAD
|
||||
* computation. Additional data must be injected _before_ the
|
||||
* call to `flip()`. Additional data can be injected in several
|
||||
* chunks of arbitrary length.
|
||||
*
|
||||
* \param cc AEAD context structure.
|
||||
* \param data pointer to additional authenticated data.
|
||||
* \param len length of additional authenticated data (in bytes).
|
||||
*/
|
||||
void (*aad_inject)(const br_aead_class **cc,
|
||||
const void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Finish injection of additional authenticated data.
|
||||
*
|
||||
* This function MUST be called before beginning the actual
|
||||
* encryption or decryption (with `run()`), even if no
|
||||
* additional authenticated data was injected. No additional
|
||||
* authenticated data may be injected after this function call.
|
||||
*
|
||||
* \param cc AEAD context structure.
|
||||
*/
|
||||
void (*flip)(const br_aead_class **cc);
|
||||
|
||||
/**
|
||||
* \brief Encrypt or decrypt some data.
|
||||
*
|
||||
* Data encryption or decryption can be done after `flip()` has
|
||||
* been called on the context. If `encrypt` is non-zero, then
|
||||
* the provided data shall be plaintext, and it is encrypted in
|
||||
* place. Otherwise, the data shall be ciphertext, and it is
|
||||
* decrypted in place.
|
||||
*
|
||||
* Data may be provided in several chunks of arbitrary length.
|
||||
*
|
||||
* \param cc AEAD context structure.
|
||||
* \param encrypt non-zero for encryption, zero for decryption.
|
||||
* \param data data to encrypt or decrypt.
|
||||
* \param len data length (in bytes).
|
||||
*/
|
||||
void (*run)(const br_aead_class **cc, int encrypt,
|
||||
void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Compute authentication tag.
|
||||
*
|
||||
* Compute the AEAD authentication tag. The tag length depends
|
||||
* on the AEAD algorithm; it is written in the provided `tag`
|
||||
* buffer. This call terminates the AEAD run: no data may be
|
||||
* processed with that AEAD context afterwards, until `reset()`
|
||||
* is called to initiate a new AEAD run.
|
||||
*
|
||||
* The tag value must normally be sent along with the encrypted
|
||||
* data. When decrypting, the tag value must be recomputed and
|
||||
* compared with the received tag: if the two tag values differ,
|
||||
* then either the tag or the encrypted data was altered in
|
||||
* transit. As an alternative to this function, the
|
||||
* `check_tag()` function may be used to compute and check the
|
||||
* tag value.
|
||||
*
|
||||
* Tag length depends on the AEAD algorithm.
|
||||
*
|
||||
* \param cc AEAD context structure.
|
||||
* \param tag destination buffer for the tag.
|
||||
*/
|
||||
void (*get_tag)(const br_aead_class **cc, void *tag);
|
||||
|
||||
/**
|
||||
* \brief Compute and check authentication tag.
|
||||
*
|
||||
* This function is an alternative to `get_tag()`, and is
|
||||
* normally used on the receiving end (i.e. when decrypting
|
||||
* messages). The tag value is recomputed and compared with the
|
||||
* provided tag value. If they match, 1 is returned; on
|
||||
* mismatch, 0 is returned. A returned value of 0 means that the
|
||||
* data or the tag was altered in transit, normally leading to
|
||||
* wholesale rejection of the complete message.
|
||||
*
|
||||
* Tag length depends on the AEAD algorithm.
|
||||
*
|
||||
* \param cc AEAD context structure.
|
||||
* \param tag tag value to compare with.
|
||||
* \return 1 on success (exact match of tag value), 0 otherwise.
|
||||
*/
|
||||
uint32_t (*check_tag)(const br_aead_class **cc, const void *tag);
|
||||
|
||||
/**
|
||||
* \brief Compute authentication tag (with truncation).
|
||||
*
|
||||
* This function is similar to `get_tag()`, except that the tag
|
||||
* length is provided. Some AEAD algorithms allow several tag
|
||||
* lengths, usually by truncating the normal tag. Shorter tags
|
||||
* mechanically increase success probability of forgeries.
|
||||
* The range of allowed tag lengths depends on the algorithm.
|
||||
*
|
||||
* \param cc AEAD context structure.
|
||||
* \param tag destination buffer for the tag.
|
||||
* \param len tag length (in bytes).
|
||||
*/
|
||||
void (*get_tag_trunc)(const br_aead_class **cc, void *tag, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Compute and check authentication tag (with truncation).
|
||||
*
|
||||
* This function is similar to `check_tag()` except that it
|
||||
* works over an explicit tag length. See `get_tag()` for a
|
||||
* discussion of explicit tag lengths; the range of allowed tag
|
||||
* lengths depends on the algorithm.
|
||||
*
|
||||
* \param cc AEAD context structure.
|
||||
* \param tag tag value to compare with.
|
||||
* \param len tag length (in bytes).
|
||||
* \return 1 on success (exact match of tag value), 0 otherwise.
|
||||
*/
|
||||
uint32_t (*check_tag_trunc)(const br_aead_class **cc,
|
||||
const void *tag, size_t len);
|
||||
};
|
||||
|
||||
/**
|
||||
* \brief Context structure for GCM.
|
||||
*
|
||||
* GCM is an AEAD mode that combines a block cipher in CTR mode with a
|
||||
* MAC based on GHASH, to provide authenticated encryption:
|
||||
*
|
||||
* - Any block cipher with 16-byte blocks can be used with GCM.
|
||||
*
|
||||
* - The nonce can have any length, from 0 up to 2^64-1 bits; however,
|
||||
* 96-bit nonces (12 bytes) are recommended (nonces with a length
|
||||
* distinct from 12 bytes are internally hashed, which risks reusing
|
||||
* nonce value with a small but not always negligible probability).
|
||||
*
|
||||
* - Additional authenticated data may have length up to 2^64-1 bits.
|
||||
*
|
||||
* - Message length may range up to 2^39-256 bits at most.
|
||||
*
|
||||
* - The authentication tag has length 16 bytes.
|
||||
*
|
||||
* The GCM initialisation function receives as parameter an
|
||||
* _initialised_ block cipher implementation context, with the secret
|
||||
* key already set. A pointer to that context will be kept within the
|
||||
* GCM context structure. It is up to the caller to allocate and
|
||||
* initialise that block cipher context.
|
||||
*/
|
||||
typedef struct {
|
||||
/** \brief Pointer to vtable for this context. */
|
||||
const br_aead_class *vtable;
|
||||
|
||||
#ifndef BR_DOXYGEN_IGNORE
|
||||
const br_block_ctr_class **bctx;
|
||||
br_ghash gh;
|
||||
unsigned char h[16];
|
||||
unsigned char j0_1[12];
|
||||
unsigned char buf[16];
|
||||
unsigned char y[16];
|
||||
uint32_t j0_2, jc;
|
||||
uint64_t count_aad, count_ctr;
|
||||
#endif
|
||||
} br_gcm_context;
|
||||
|
||||
/**
|
||||
* \brief Initialize a GCM context.
|
||||
*
|
||||
* A block cipher implementation, with its initialised context structure,
|
||||
* is provided. The block cipher MUST use 16-byte blocks in CTR mode,
|
||||
* and its secret key MUST have been already set in the provided context.
|
||||
* A GHASH implementation must also be provided. The parameters are linked
|
||||
* in the GCM context.
|
||||
*
|
||||
* After this function has been called, the `br_gcm_reset()` function must
|
||||
* be called, to provide the IV for GCM computation.
|
||||
*
|
||||
* \param ctx GCM context structure.
|
||||
* \param bctx block cipher context (already initialised with secret key).
|
||||
* \param gh GHASH implementation.
|
||||
*/
|
||||
void br_gcm_init(br_gcm_context *ctx,
|
||||
const br_block_ctr_class **bctx, br_ghash gh);
|
||||
|
||||
/**
|
||||
* \brief Reset a GCM context.
|
||||
*
|
||||
* This function resets an already initialised GCM context for a new
|
||||
* computation run. Implementations and keys are conserved. This function
|
||||
* can be called at any time; it cancels any ongoing GCM computation that
|
||||
* uses the provided context structure.
|
||||
*
|
||||
* The provided IV is a _nonce_. It is critical to GCM security that IV
|
||||
* values are not repeated for the same encryption key. IV can have
|
||||
* arbitrary length (up to 2^64-1 bits), but the "normal" length is
|
||||
* 96 bits (12 bytes).
|
||||
*
|
||||
* \param ctx GCM context structure.
|
||||
* \param iv GCM nonce to use.
|
||||
* \param len GCM nonce length (in bytes).
|
||||
*/
|
||||
void br_gcm_reset(br_gcm_context *ctx, const void *iv, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Inject additional authenticated data into GCM.
|
||||
*
|
||||
* The provided data is injected into a running GCM computation. Additional
|
||||
* data must be injected _before_ the call to `br_gcm_flip()`.
|
||||
* Additional data can be injected in several chunks of arbitrary length;
|
||||
* the maximum total size of additional authenticated data is 2^64-1
|
||||
* bits.
|
||||
*
|
||||
* \param ctx GCM context structure.
|
||||
* \param data pointer to additional authenticated data.
|
||||
* \param len length of additional authenticated data (in bytes).
|
||||
*/
|
||||
void br_gcm_aad_inject(br_gcm_context *ctx, const void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Finish injection of additional authenticated data into GCM.
|
||||
*
|
||||
* This function MUST be called before beginning the actual encryption
|
||||
* or decryption (with `br_gcm_run()`), even if no additional authenticated
|
||||
* data was injected. No additional authenticated data may be injected
|
||||
* after this function call.
|
||||
*
|
||||
* \param ctx GCM context structure.
|
||||
*/
|
||||
void br_gcm_flip(br_gcm_context *ctx);
|
||||
|
||||
/**
|
||||
* \brief Encrypt or decrypt some data with GCM.
|
||||
*
|
||||
* Data encryption or decryption can be done after `br_gcm_flip()`
|
||||
* has been called on the context. If `encrypt` is non-zero, then the
|
||||
* provided data shall be plaintext, and it is encrypted in place.
|
||||
* Otherwise, the data shall be ciphertext, and it is decrypted in place.
|
||||
*
|
||||
* Data may be provided in several chunks of arbitrary length. The maximum
|
||||
* total length for data is 2^39-256 bits, i.e. about 65 gigabytes.
|
||||
*
|
||||
* \param ctx GCM context structure.
|
||||
* \param encrypt non-zero for encryption, zero for decryption.
|
||||
* \param data data to encrypt or decrypt.
|
||||
* \param len data length (in bytes).
|
||||
*/
|
||||
void br_gcm_run(br_gcm_context *ctx, int encrypt, void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Compute GCM authentication tag.
|
||||
*
|
||||
* Compute the GCM authentication tag. The tag is a 16-byte value which
|
||||
* is written in the provided `tag` buffer. This call terminates the
|
||||
* GCM run: no data may be processed with that GCM context afterwards,
|
||||
* until `br_gcm_reset()` is called to initiate a new GCM run.
|
||||
*
|
||||
* The tag value must normally be sent along with the encrypted data.
|
||||
* When decrypting, the tag value must be recomputed and compared with
|
||||
* the received tag: if the two tag values differ, then either the tag
|
||||
* or the encrypted data was altered in transit. As an alternative to
|
||||
* this function, the `br_gcm_check_tag()` function can be used to
|
||||
* compute and check the tag value.
|
||||
*
|
||||
* \param ctx GCM context structure.
|
||||
* \param tag destination buffer for the tag (16 bytes).
|
||||
*/
|
||||
void br_gcm_get_tag(br_gcm_context *ctx, void *tag);
|
||||
|
||||
/**
|
||||
* \brief Compute and check GCM authentication tag.
|
||||
*
|
||||
* This function is an alternative to `br_gcm_get_tag()`, normally used
|
||||
* on the receiving end (i.e. when decrypting value). The tag value is
|
||||
* recomputed and compared with the provided tag value. If they match, 1
|
||||
* is returned; on mismatch, 0 is returned. A returned value of 0 means
|
||||
* that the data or the tag was altered in transit, normally leading to
|
||||
* wholesale rejection of the complete message.
|
||||
*
|
||||
* \param ctx GCM context structure.
|
||||
* \param tag tag value to compare with (16 bytes).
|
||||
* \return 1 on success (exact match of tag value), 0 otherwise.
|
||||
*/
|
||||
uint32_t br_gcm_check_tag(br_gcm_context *ctx, const void *tag);
|
||||
|
||||
/**
|
||||
* \brief Compute GCM authentication tag (with truncation).
|
||||
*
|
||||
* This function is similar to `br_gcm_get_tag()`, except that it allows
|
||||
* the tag to be truncated to a smaller length. The intended tag length
|
||||
* is provided as `len` (in bytes); it MUST be no more than 16, but
|
||||
* it may be smaller. Note that decreasing tag length mechanically makes
|
||||
* forgeries easier; NIST SP 800-38D specifies that the tag length shall
|
||||
* lie between 12 and 16 bytes (inclusive), but may be truncated down to
|
||||
* 4 or 8 bytes, for specific applications that can tolerate it. It must
|
||||
* also be noted that successful forgeries leak information on the
|
||||
* authentication key, making subsequent forgeries easier. Therefore,
|
||||
* tag truncation, and in particular truncation to sizes lower than 12
|
||||
* bytes, shall be envisioned only with great care.
|
||||
*
|
||||
* The tag is written in the provided `tag` buffer. This call terminates
|
||||
* the GCM run: no data may be processed with that GCM context
|
||||
* afterwards, until `br_gcm_reset()` is called to initiate a new GCM
|
||||
* run.
|
||||
*
|
||||
* The tag value must normally be sent along with the encrypted data.
|
||||
* When decrypting, the tag value must be recomputed and compared with
|
||||
* the received tag: if the two tag values differ, then either the tag
|
||||
* or the encrypted data was altered in transit. As an alternative to
|
||||
* this function, the `br_gcm_check_tag_trunc()` function can be used to
|
||||
* compute and check the tag value.
|
||||
*
|
||||
* \param ctx GCM context structure.
|
||||
* \param tag destination buffer for the tag.
|
||||
* \param len tag length (16 bytes or less).
|
||||
*/
|
||||
void br_gcm_get_tag_trunc(br_gcm_context *ctx, void *tag, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Compute and check GCM authentication tag (with truncation).
|
||||
*
|
||||
* This function is an alternative to `br_gcm_get_tag_trunc()`, normally used
|
||||
* on the receiving end (i.e. when decrypting value). The tag value is
|
||||
* recomputed and compared with the provided tag value. If they match, 1
|
||||
* is returned; on mismatch, 0 is returned. A returned value of 0 means
|
||||
* that the data or the tag was altered in transit, normally leading to
|
||||
* wholesale rejection of the complete message.
|
||||
*
|
||||
* Tag length MUST be 16 bytes or less. The normal GCM tag length is 16
|
||||
* bytes. See `br_check_tag_trunc()` for some discussion on the potential
|
||||
* perils of truncating authentication tags.
|
||||
*
|
||||
* \param ctx GCM context structure.
|
||||
* \param tag tag value to compare with.
|
||||
* \param len tag length (in bytes).
|
||||
* \return 1 on success (exact match of tag value), 0 otherwise.
|
||||
*/
|
||||
uint32_t br_gcm_check_tag_trunc(br_gcm_context *ctx,
|
||||
const void *tag, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Class instance for GCM.
|
||||
*/
|
||||
extern const br_aead_class br_gcm_vtable;
|
||||
|
||||
/**
|
||||
* \brief Context structure for CCM.
|
||||
*
|
||||
* CCM is an AEAD mode that combines a block cipher in CTR mode with
|
||||
* CBC-MAC using the same block cipher and the same key, to provide
|
||||
* authenticated encryption:
|
||||
*
|
||||
* - Any block cipher with 16-byte blocks can be used with CCM
|
||||
* (technically, other block sizes are defined as well, but this
|
||||
* is not implemented by these functions; shorter blocks also
|
||||
* imply numerous security issues).
|
||||
*
|
||||
* - The authentication tag length, and plaintext length, MUST be
|
||||
* known when starting processing data. Plaintext and ciphertext
|
||||
* can still be provided by chunks, but the total size must match
|
||||
* the value provided upon initialisation.
|
||||
*
|
||||
* - The nonce length is constrained between 7 and 13 bytes (inclusive).
|
||||
* Furthermore, the plaintext length, when encoded, must fit over
|
||||
* 15-nonceLen bytes; thus, if the nonce has length 13 bytes, then
|
||||
* the plaintext length cannot exceed 65535 bytes.
|
||||
*
|
||||
* - Additional authenticated data length is practically unlimited
|
||||
* (formal limit is at 2^64 bytes).
|
||||
*
|
||||
* - The authentication tag has length 4 to 16 bytes (even values only).
|
||||
*
|
||||
* The CCM initialisation function receives as parameter an
|
||||
* _initialised_ block cipher implementation context, with the secret
|
||||
* key already set. A pointer to that context will be kept within the
|
||||
* CCM context structure. It is up to the caller to allocate and
|
||||
* initialise that block cipher context.
|
||||
*/
|
||||
typedef struct {
|
||||
#ifndef BR_DOXYGEN_IGNORE
|
||||
const br_block_ctrcbc_class **bctx;
|
||||
unsigned char ctr[16];
|
||||
unsigned char cbcmac[16];
|
||||
unsigned char tagmask[16];
|
||||
unsigned char buf[16];
|
||||
size_t ptr;
|
||||
size_t tag_len;
|
||||
#endif
|
||||
} br_ccm_context;
|
||||
|
||||
/**
|
||||
* \brief Initialize a CCM context.
|
||||
*
|
||||
* A block cipher implementation, with its initialised context
|
||||
* structure, is provided. The block cipher MUST use 16-byte blocks in
|
||||
* CTR + CBC-MAC mode, and its secret key MUST have been already set in
|
||||
* the provided context. The parameters are linked in the CCM context.
|
||||
*
|
||||
* After this function has been called, the `br_ccm_reset()` function must
|
||||
* be called, to provide the nonce for CCM computation.
|
||||
*
|
||||
* \param ctx CCM context structure.
|
||||
* \param bctx block cipher context (already initialised with secret key).
|
||||
*/
|
||||
void br_ccm_init(br_ccm_context *ctx, const br_block_ctrcbc_class **bctx);
|
||||
|
||||
/**
|
||||
* \brief Reset a CCM context.
|
||||
*
|
||||
* This function resets an already initialised CCM context for a new
|
||||
* computation run. Implementations and keys are conserved. This function
|
||||
* can be called at any time; it cancels any ongoing CCM computation that
|
||||
* uses the provided context structure.
|
||||
*
|
||||
* The `aad_len` parameter contains the total length, in bytes, of the
|
||||
* additional authenticated data. It may be zero. That length MUST be
|
||||
* exact.
|
||||
*
|
||||
* The `data_len` parameter contains the total length, in bytes, of the
|
||||
* data that will be injected (plaintext or ciphertext). That length MUST
|
||||
* be exact. Moreover, that length MUST be less than 2^(8*(15-nonce_len)).
|
||||
*
|
||||
* The nonce length (`nonce_len`), in bytes, must be in the 7..13 range
|
||||
* (inclusive).
|
||||
*
|
||||
* The tag length (`tag_len`), in bytes, must be in the 4..16 range, and
|
||||
* be an even integer. Short tags mechanically allow for higher forgery
|
||||
* probabilities; hence, tag sizes smaller than 12 bytes shall be used only
|
||||
* with care.
|
||||
*
|
||||
* It is critical to CCM security that nonce values are not repeated for
|
||||
* the same encryption key. Random generation of nonces is not generally
|
||||
* recommended, due to the relatively small maximum nonce value.
|
||||
*
|
||||
* Returned value is 1 on success, 0 on error. An error is reported if
|
||||
* the tag or nonce length is out of range, or if the
|
||||
* plaintext/ciphertext length cannot be encoded with the specified
|
||||
* nonce length.
|
||||
*
|
||||
* \param ctx CCM context structure.
|
||||
* \param nonce CCM nonce to use.
|
||||
* \param nonce_len CCM nonce length (in bytes, 7 to 13).
|
||||
* \param aad_len additional authenticated data length (in bytes).
|
||||
* \param data_len plaintext/ciphertext length (in bytes).
|
||||
* \param tag_len tag length (in bytes).
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
int br_ccm_reset(br_ccm_context *ctx, const void *nonce, size_t nonce_len,
|
||||
uint64_t aad_len, uint64_t data_len, size_t tag_len);
|
||||
|
||||
/**
|
||||
* \brief Inject additional authenticated data into CCM.
|
||||
*
|
||||
* The provided data is injected into a running CCM computation. Additional
|
||||
* data must be injected _before_ the call to `br_ccm_flip()`.
|
||||
* Additional data can be injected in several chunks of arbitrary length,
|
||||
* but the total amount MUST exactly match the value which was provided
|
||||
* to `br_ccm_reset()`.
|
||||
*
|
||||
* \param ctx CCM context structure.
|
||||
* \param data pointer to additional authenticated data.
|
||||
* \param len length of additional authenticated data (in bytes).
|
||||
*/
|
||||
void br_ccm_aad_inject(br_ccm_context *ctx, const void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Finish injection of additional authenticated data into CCM.
|
||||
*
|
||||
* This function MUST be called before beginning the actual encryption
|
||||
* or decryption (with `br_ccm_run()`), even if no additional authenticated
|
||||
* data was injected. No additional authenticated data may be injected
|
||||
* after this function call.
|
||||
*
|
||||
* \param ctx CCM context structure.
|
||||
*/
|
||||
void br_ccm_flip(br_ccm_context *ctx);
|
||||
|
||||
/**
|
||||
* \brief Encrypt or decrypt some data with CCM.
|
||||
*
|
||||
* Data encryption or decryption can be done after `br_ccm_flip()`
|
||||
* has been called on the context. If `encrypt` is non-zero, then the
|
||||
* provided data shall be plaintext, and it is encrypted in place.
|
||||
* Otherwise, the data shall be ciphertext, and it is decrypted in place.
|
||||
*
|
||||
* Data may be provided in several chunks of arbitrary length, provided
|
||||
* that the total length exactly matches the length provided to the
|
||||
* `br_ccm_reset()` call.
|
||||
*
|
||||
* \param ctx CCM context structure.
|
||||
* \param encrypt non-zero for encryption, zero for decryption.
|
||||
* \param data data to encrypt or decrypt.
|
||||
* \param len data length (in bytes).
|
||||
*/
|
||||
void br_ccm_run(br_ccm_context *ctx, int encrypt, void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Compute CCM authentication tag.
|
||||
*
|
||||
* Compute the CCM authentication tag. This call terminates the CCM
|
||||
* run: all data must have been injected with `br_ccm_run()` (in zero,
|
||||
* one or more successive calls). After this function has been called,
|
||||
* no more data can br processed; a `br_ccm_reset()` call is required
|
||||
* to start a new message.
|
||||
*
|
||||
* The tag length was provided upon context initialisation (last call
|
||||
* to `br_ccm_reset()`); it is returned by this function.
|
||||
*
|
||||
* The tag value must normally be sent along with the encrypted data.
|
||||
* When decrypting, the tag value must be recomputed and compared with
|
||||
* the received tag: if the two tag values differ, then either the tag
|
||||
* or the encrypted data was altered in transit. As an alternative to
|
||||
* this function, the `br_ccm_check_tag()` function can be used to
|
||||
* compute and check the tag value.
|
||||
*
|
||||
* \param ctx CCM context structure.
|
||||
* \param tag destination buffer for the tag (up to 16 bytes).
|
||||
* \return the tag length (in bytes).
|
||||
*/
|
||||
size_t br_ccm_get_tag(br_ccm_context *ctx, void *tag);
|
||||
|
||||
/**
|
||||
* \brief Compute and check CCM authentication tag.
|
||||
*
|
||||
* This function is an alternative to `br_ccm_get_tag()`, normally used
|
||||
* on the receiving end (i.e. when decrypting value). The tag value is
|
||||
* recomputed and compared with the provided tag value. If they match, 1
|
||||
* is returned; on mismatch, 0 is returned. A returned value of 0 means
|
||||
* that the data or the tag was altered in transit, normally leading to
|
||||
* wholesale rejection of the complete message.
|
||||
*
|
||||
* \param ctx CCM context structure.
|
||||
* \param tag tag value to compare with (up to 16 bytes).
|
||||
* \return 1 on success (exact match of tag value), 0 otherwise.
|
||||
*/
|
||||
uint32_t br_ccm_check_tag(br_ccm_context *ctx, const void *tag);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
1348
third_party/bearssl/inc/bearssl_block.h
vendored
Normal file
1348
third_party/bearssl/inc/bearssl_block.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
667
third_party/bearssl/inc/bearssl_ec.h
vendored
Normal file
667
third_party/bearssl/inc/bearssl_ec.h
vendored
Normal file
@@ -0,0 +1,667 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef BR_BEARSSL_EC_H__
|
||||
#define BR_BEARSSL_EC_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "bearssl_rand.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** \file bearssl_ec.h
|
||||
*
|
||||
* # Elliptic Curves
|
||||
*
|
||||
* This file documents the EC implementations provided with BearSSL, and
|
||||
* ECDSA.
|
||||
*
|
||||
* ## Elliptic Curve API
|
||||
*
|
||||
* Only "named curves" are supported. Each EC implementation supports
|
||||
* one or several named curves, identified by symbolic identifiers.
|
||||
* These identifiers are small integers, that correspond to the values
|
||||
* registered by the
|
||||
* [IANA](http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8).
|
||||
*
|
||||
* Since all currently defined elliptic curve identifiers are in the 0..31
|
||||
* range, it is convenient to encode support of some curves in a 32-bit
|
||||
* word, such that bit x corresponds to curve of identifier x.
|
||||
*
|
||||
* An EC implementation is incarnated by a `br_ec_impl` instance, that
|
||||
* offers the following fields:
|
||||
*
|
||||
* - `supported_curves`
|
||||
*
|
||||
* A 32-bit word that documents the identifiers of the curves supported
|
||||
* by this implementation.
|
||||
*
|
||||
* - `generator()`
|
||||
*
|
||||
* Callback method that returns a pointer to the conventional generator
|
||||
* point for that curve.
|
||||
*
|
||||
* - `order()`
|
||||
*
|
||||
* Callback method that returns a pointer to the subgroup order for
|
||||
* that curve. That value uses unsigned big-endian encoding.
|
||||
*
|
||||
* - `xoff()`
|
||||
*
|
||||
* Callback method that returns the offset and length of the X
|
||||
* coordinate in an encoded point.
|
||||
*
|
||||
* - `mul()`
|
||||
*
|
||||
* Multiply a curve point with an integer.
|
||||
*
|
||||
* - `mulgen()`
|
||||
*
|
||||
* Multiply the curve generator with an integer. This may be faster
|
||||
* than the generic `mul()`.
|
||||
*
|
||||
* - `muladd()`
|
||||
*
|
||||
* Multiply two curve points by two integers, and return the sum of
|
||||
* the two products.
|
||||
*
|
||||
* All curve points are represented in uncompressed format. The `mul()`
|
||||
* and `muladd()` methods take care to validate that the provided points
|
||||
* are really part of the relevant curve subgroup.
|
||||
*
|
||||
* For all point multiplication functions, the following holds:
|
||||
*
|
||||
* - Functions validate that the provided points are valid members
|
||||
* of the relevant curve subgroup. An error is reported if that is
|
||||
* not the case.
|
||||
*
|
||||
* - Processing is constant-time, even if the point operands are not
|
||||
* valid. This holds for both the source and resulting points, and
|
||||
* the multipliers (integers). Only the byte length of the provided
|
||||
* multiplier arrays (not their actual value length in bits) may
|
||||
* leak through timing-based side channels.
|
||||
*
|
||||
* - The multipliers (integers) MUST be lower than the subgroup order.
|
||||
* If this property is not met, then the result is indeterminate,
|
||||
* but an error value is not necessarily returned.
|
||||
*
|
||||
*
|
||||
* ## ECDSA
|
||||
*
|
||||
* ECDSA signatures have two standard formats, called "raw" and "asn1".
|
||||
* Internally, such a signature is a pair of modular integers `(r,s)`.
|
||||
* The "raw" format is the concatenation of the unsigned big-endian
|
||||
* encodings of these two integers, possibly left-padded with zeros so
|
||||
* that they have the same encoded length. The "asn1" format is the
|
||||
* DER encoding of an ASN.1 structure that contains the two integer
|
||||
* values:
|
||||
*
|
||||
* ECDSASignature ::= SEQUENCE {
|
||||
* r INTEGER,
|
||||
* s INTEGER
|
||||
* }
|
||||
*
|
||||
* In general, in all of X.509 and SSL/TLS, the "asn1" format is used.
|
||||
* BearSSL offers ECDSA implementations for both formats; conversion
|
||||
* functions between the two formats are also provided. Conversion of a
|
||||
* "raw" format signature into "asn1" may enlarge a signature by no more
|
||||
* than 9 bytes for all supported curves; conversely, conversion of an
|
||||
* "asn1" signature to "raw" may expand the signature but the "raw"
|
||||
* length will never be more than twice the length of the "asn1" length
|
||||
* (and usually it will be shorter).
|
||||
*
|
||||
* Note that for a given signature, the "raw" format is not fully
|
||||
* deterministic, in that it does not enforce a minimal common length.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Standard curve ID. These ID are equal to the assigned numerical
|
||||
* identifiers assigned to these curves for TLS:
|
||||
* http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
|
||||
*/
|
||||
|
||||
/** \brief Identifier for named curve sect163k1. */
|
||||
#define BR_EC_sect163k1 1
|
||||
|
||||
/** \brief Identifier for named curve sect163r1. */
|
||||
#define BR_EC_sect163r1 2
|
||||
|
||||
/** \brief Identifier for named curve sect163r2. */
|
||||
#define BR_EC_sect163r2 3
|
||||
|
||||
/** \brief Identifier for named curve sect193r1. */
|
||||
#define BR_EC_sect193r1 4
|
||||
|
||||
/** \brief Identifier for named curve sect193r2. */
|
||||
#define BR_EC_sect193r2 5
|
||||
|
||||
/** \brief Identifier for named curve sect233k1. */
|
||||
#define BR_EC_sect233k1 6
|
||||
|
||||
/** \brief Identifier for named curve sect233r1. */
|
||||
#define BR_EC_sect233r1 7
|
||||
|
||||
/** \brief Identifier for named curve sect239k1. */
|
||||
#define BR_EC_sect239k1 8
|
||||
|
||||
/** \brief Identifier for named curve sect283k1. */
|
||||
#define BR_EC_sect283k1 9
|
||||
|
||||
/** \brief Identifier for named curve sect283r1. */
|
||||
#define BR_EC_sect283r1 10
|
||||
|
||||
/** \brief Identifier for named curve sect409k1. */
|
||||
#define BR_EC_sect409k1 11
|
||||
|
||||
/** \brief Identifier for named curve sect409r1. */
|
||||
#define BR_EC_sect409r1 12
|
||||
|
||||
/** \brief Identifier for named curve sect571k1. */
|
||||
#define BR_EC_sect571k1 13
|
||||
|
||||
/** \brief Identifier for named curve sect571r1. */
|
||||
#define BR_EC_sect571r1 14
|
||||
|
||||
/** \brief Identifier for named curve secp160k1. */
|
||||
#define BR_EC_secp160k1 15
|
||||
|
||||
/** \brief Identifier for named curve secp160r1. */
|
||||
#define BR_EC_secp160r1 16
|
||||
|
||||
/** \brief Identifier for named curve secp160r2. */
|
||||
#define BR_EC_secp160r2 17
|
||||
|
||||
/** \brief Identifier for named curve secp192k1. */
|
||||
#define BR_EC_secp192k1 18
|
||||
|
||||
/** \brief Identifier for named curve secp192r1. */
|
||||
#define BR_EC_secp192r1 19
|
||||
|
||||
/** \brief Identifier for named curve secp224k1. */
|
||||
#define BR_EC_secp224k1 20
|
||||
|
||||
/** \brief Identifier for named curve secp224r1. */
|
||||
#define BR_EC_secp224r1 21
|
||||
|
||||
/** \brief Identifier for named curve secp256k1. */
|
||||
#define BR_EC_secp256k1 22
|
||||
|
||||
/** \brief Identifier for named curve secp256r1. */
|
||||
#define BR_EC_secp256r1 23
|
||||
|
||||
/** \brief Identifier for named curve secp384r1. */
|
||||
#define BR_EC_secp384r1 24
|
||||
|
||||
/** \brief Identifier for named curve secp521r1. */
|
||||
#define BR_EC_secp521r1 25
|
||||
|
||||
/** \brief Identifier for named curve brainpoolP256r1. */
|
||||
#define BR_EC_brainpoolP256r1 26
|
||||
|
||||
/** \brief Identifier for named curve brainpoolP384r1. */
|
||||
#define BR_EC_brainpoolP384r1 27
|
||||
|
||||
/** \brief Identifier for named curve brainpoolP512r1. */
|
||||
#define BR_EC_brainpoolP512r1 28
|
||||
|
||||
/** \brief Identifier for named curve Curve25519. */
|
||||
#define BR_EC_curve25519 29
|
||||
|
||||
/** \brief Identifier for named curve Curve448. */
|
||||
#define BR_EC_curve448 30
|
||||
|
||||
/**
|
||||
* \brief Structure for an EC public key.
|
||||
*/
|
||||
typedef struct {
|
||||
/** \brief Identifier for the curve used by this key. */
|
||||
int curve;
|
||||
/** \brief Public curve point (uncompressed format). */
|
||||
unsigned char *q;
|
||||
/** \brief Length of public curve point (in bytes). */
|
||||
size_t qlen;
|
||||
} br_ec_public_key;
|
||||
|
||||
/**
|
||||
* \brief Structure for an EC private key.
|
||||
*
|
||||
* The private key is an integer modulo the curve subgroup order. The
|
||||
* encoding below tolerates extra leading zeros. In general, it is
|
||||
* recommended that the private key has the same length as the curve
|
||||
* subgroup order.
|
||||
*/
|
||||
typedef struct {
|
||||
/** \brief Identifier for the curve used by this key. */
|
||||
int curve;
|
||||
/** \brief Private key (integer, unsigned big-endian encoding). */
|
||||
unsigned char *x;
|
||||
/** \brief Private key length (in bytes). */
|
||||
size_t xlen;
|
||||
} br_ec_private_key;
|
||||
|
||||
/**
|
||||
* \brief Type for an EC implementation.
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* \brief Supported curves.
|
||||
*
|
||||
* This word is a bitfield: bit `x` is set if the curve of ID `x`
|
||||
* is supported. E.g. an implementation supporting both NIST P-256
|
||||
* (secp256r1, ID 23) and NIST P-384 (secp384r1, ID 24) will have
|
||||
* value `0x01800000` in this field.
|
||||
*/
|
||||
uint32_t supported_curves;
|
||||
|
||||
/**
|
||||
* \brief Get the conventional generator.
|
||||
*
|
||||
* This function returns the conventional generator (encoded
|
||||
* curve point) for the specified curve. This function MUST NOT
|
||||
* be called if the curve is not supported.
|
||||
*
|
||||
* \param curve curve identifier.
|
||||
* \param len receiver for the encoded generator length (in bytes).
|
||||
* \return the encoded generator.
|
||||
*/
|
||||
const unsigned char *(*generator)(int curve, size_t *len);
|
||||
|
||||
/**
|
||||
* \brief Get the subgroup order.
|
||||
*
|
||||
* This function returns the order of the subgroup generated by
|
||||
* the conventional generator, for the specified curve. Unsigned
|
||||
* big-endian encoding is used. This function MUST NOT be called
|
||||
* if the curve is not supported.
|
||||
*
|
||||
* \param curve curve identifier.
|
||||
* \param len receiver for the encoded order length (in bytes).
|
||||
* \return the encoded order.
|
||||
*/
|
||||
const unsigned char *(*order)(int curve, size_t *len);
|
||||
|
||||
/**
|
||||
* \brief Get the offset and length for the X coordinate.
|
||||
*
|
||||
* This function returns the offset and length (in bytes) of
|
||||
* the X coordinate in an encoded non-zero point.
|
||||
*
|
||||
* \param curve curve identifier.
|
||||
* \param len receiver for the X coordinate length (in bytes).
|
||||
* \return the offset for the X coordinate (in bytes).
|
||||
*/
|
||||
size_t (*xoff)(int curve, size_t *len);
|
||||
|
||||
/**
|
||||
* \brief Multiply a curve point by an integer.
|
||||
*
|
||||
* The source point is provided in array `G` (of size `Glen` bytes);
|
||||
* the multiplication result is written over it. The multiplier
|
||||
* `x` (of size `xlen` bytes) uses unsigned big-endian encoding.
|
||||
*
|
||||
* Rules:
|
||||
*
|
||||
* - The specified curve MUST be supported.
|
||||
*
|
||||
* - The source point must be a valid point on the relevant curve
|
||||
* subgroup (and not the "point at infinity" either). If this is
|
||||
* not the case, then this function returns an error (0).
|
||||
*
|
||||
* - The multiplier integer MUST be non-zero and less than the
|
||||
* curve subgroup order. If this property does not hold, then
|
||||
* the result is indeterminate and an error code is not
|
||||
* guaranteed.
|
||||
*
|
||||
* Returned value is 1 on success, 0 on error. On error, the
|
||||
* contents of `G` are indeterminate.
|
||||
*
|
||||
* \param G point to multiply.
|
||||
* \param Glen length of the encoded point (in bytes).
|
||||
* \param x multiplier (unsigned big-endian).
|
||||
* \param xlen multiplier length (in bytes).
|
||||
* \param curve curve identifier.
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
uint32_t (*mul)(unsigned char *G, size_t Glen,
|
||||
const unsigned char *x, size_t xlen, int curve);
|
||||
|
||||
/**
|
||||
* \brief Multiply the generator by an integer.
|
||||
*
|
||||
* The multiplier MUST be non-zero and less than the curve
|
||||
* subgroup order. Results are indeterminate if this property
|
||||
* does not hold.
|
||||
*
|
||||
* \param R output buffer for the point.
|
||||
* \param x multiplier (unsigned big-endian).
|
||||
* \param xlen multiplier length (in bytes).
|
||||
* \param curve curve identifier.
|
||||
* \return encoded result point length (in bytes).
|
||||
*/
|
||||
size_t (*mulgen)(unsigned char *R,
|
||||
const unsigned char *x, size_t xlen, int curve);
|
||||
|
||||
/**
|
||||
* \brief Multiply two points by two integers and add the
|
||||
* results.
|
||||
*
|
||||
* The point `x*A + y*B` is computed and written back in the `A`
|
||||
* array.
|
||||
*
|
||||
* Rules:
|
||||
*
|
||||
* - The specified curve MUST be supported.
|
||||
*
|
||||
* - The source points (`A` and `B`) must be valid points on
|
||||
* the relevant curve subgroup (and not the "point at
|
||||
* infinity" either). If this is not the case, then this
|
||||
* function returns an error (0).
|
||||
*
|
||||
* - If the `B` pointer is `NULL`, then the conventional
|
||||
* subgroup generator is used. With some implementations,
|
||||
* this may be faster than providing a pointer to the
|
||||
* generator.
|
||||
*
|
||||
* - The multiplier integers (`x` and `y`) MUST be non-zero
|
||||
* and less than the curve subgroup order. If either integer
|
||||
* is zero, then an error is reported, but if one of them is
|
||||
* not lower than the subgroup order, then the result is
|
||||
* indeterminate and an error code is not guaranteed.
|
||||
*
|
||||
* - If the final result is the point at infinity, then an
|
||||
* error is returned.
|
||||
*
|
||||
* Returned value is 1 on success, 0 on error. On error, the
|
||||
* contents of `A` are indeterminate.
|
||||
*
|
||||
* \param A first point to multiply.
|
||||
* \param B second point to multiply (`NULL` for the generator).
|
||||
* \param len common length of the encoded points (in bytes).
|
||||
* \param x multiplier for `A` (unsigned big-endian).
|
||||
* \param xlen length of multiplier for `A` (in bytes).
|
||||
* \param y multiplier for `A` (unsigned big-endian).
|
||||
* \param ylen length of multiplier for `A` (in bytes).
|
||||
* \param curve curve identifier.
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
uint32_t (*muladd)(unsigned char *A, const unsigned char *B, size_t len,
|
||||
const unsigned char *x, size_t xlen,
|
||||
const unsigned char *y, size_t ylen, int curve);
|
||||
} br_ec_impl;
|
||||
|
||||
/**
|
||||
* \brief EC implementation "i31".
|
||||
*
|
||||
* This implementation internally uses generic code for modular integers,
|
||||
* with a representation as sequences of 31-bit words. It supports secp256r1,
|
||||
* secp384r1 and secp521r1 (aka NIST curves P-256, P-384 and P-521).
|
||||
*/
|
||||
extern const br_ec_impl br_ec_prime_i31;
|
||||
|
||||
/**
|
||||
* \brief EC implementation "m31" for P-256.
|
||||
*
|
||||
* This implementation uses specialised code for curve secp256r1 (also
|
||||
* known as NIST P-256), relying on multiplications of 31-bit values
|
||||
* (MUL31).
|
||||
*/
|
||||
extern const br_ec_impl br_ec_p256_m31;
|
||||
|
||||
/**
|
||||
* \brief EC implementation "m62" (specialised code) for P-256.
|
||||
*
|
||||
* This implementation uses custom code relying on multiplication of
|
||||
* integers up to 64 bits, with a 128-bit result. This implementation is
|
||||
* defined only on platforms that offer the 64x64->128 multiplication
|
||||
* support; use `br_ec_p256_m62_get()` to dynamically obtain a pointer
|
||||
* to that implementation.
|
||||
*/
|
||||
extern const br_ec_impl br_ec_p256_m62;
|
||||
|
||||
/**
|
||||
* \brief Get the "m62" implementation of P-256, if available.
|
||||
*
|
||||
* \return the implementation, or 0.
|
||||
*/
|
||||
const br_ec_impl *br_ec_p256_m62_get(void);
|
||||
|
||||
/**
|
||||
* \brief EC implementation "m64" (specialised code) for P-256.
|
||||
*
|
||||
* This implementation uses custom code relying on multiplication of
|
||||
* integers up to 64 bits, with a 128-bit result. This implementation is
|
||||
* defined only on platforms that offer the 64x64->128 multiplication
|
||||
* support; use `br_ec_p256_m64_get()` to dynamically obtain a pointer
|
||||
* to that implementation.
|
||||
*/
|
||||
extern const br_ec_impl br_ec_p256_m64;
|
||||
|
||||
/**
|
||||
* \brief Get the "m64" implementation of P-256, if available.
|
||||
*
|
||||
* \return the implementation, or 0.
|
||||
*/
|
||||
const br_ec_impl *br_ec_p256_m64_get(void);
|
||||
|
||||
/**
|
||||
* \brief EC implementation "i31" (generic code) for Curve25519.
|
||||
*
|
||||
* This implementation uses the generic code for modular integers (with
|
||||
* 31-bit words) to support Curve25519. Due to the specificities of the
|
||||
* curve definition, the following applies:
|
||||
*
|
||||
* - `muladd()` is not implemented (the function returns 0 systematically).
|
||||
* - `order()` returns 2^255-1, since the point multiplication algorithm
|
||||
* accepts any 32-bit integer as input (it clears the top bit and low
|
||||
* three bits systematically).
|
||||
*/
|
||||
extern const br_ec_impl br_ec_c25519_i31;
|
||||
|
||||
/**
|
||||
* \brief EC implementation "m31" (specialised code) for Curve25519.
|
||||
*
|
||||
* This implementation uses custom code relying on multiplication of
|
||||
* integers up to 31 bits. Due to the specificities of the curve
|
||||
* definition, the following applies:
|
||||
*
|
||||
* - `muladd()` is not implemented (the function returns 0 systematically).
|
||||
* - `order()` returns 2^255-1, since the point multiplication algorithm
|
||||
* accepts any 32-bit integer as input (it clears the top bit and low
|
||||
* three bits systematically).
|
||||
*/
|
||||
extern const br_ec_impl br_ec_c25519_m31;
|
||||
|
||||
/**
|
||||
* \brief EC implementation "m62" (specialised code) for Curve25519.
|
||||
*
|
||||
* This implementation uses custom code relying on multiplication of
|
||||
* integers up to 62 bits, with a 124-bit result. This implementation is
|
||||
* defined only on platforms that offer the 64x64->128 multiplication
|
||||
* support; use `br_ec_c25519_m62_get()` to dynamically obtain a pointer
|
||||
* to that implementation. Due to the specificities of the curve
|
||||
* definition, the following applies:
|
||||
*
|
||||
* - `muladd()` is not implemented (the function returns 0 systematically).
|
||||
* - `order()` returns 2^255-1, since the point multiplication algorithm
|
||||
* accepts any 32-bit integer as input (it clears the top bit and low
|
||||
* three bits systematically).
|
||||
*/
|
||||
extern const br_ec_impl br_ec_c25519_m62;
|
||||
|
||||
/**
|
||||
* \brief Get the "m62" implementation of Curve25519, if available.
|
||||
*
|
||||
* \return the implementation, or 0.
|
||||
*/
|
||||
const br_ec_impl *br_ec_c25519_m62_get(void);
|
||||
|
||||
/**
|
||||
* \brief EC implementation "m64" (specialised code) for Curve25519.
|
||||
*
|
||||
* This implementation uses custom code relying on multiplication of
|
||||
* integers up to 64 bits, with a 128-bit result. This implementation is
|
||||
* defined only on platforms that offer the 64x64->128 multiplication
|
||||
* support; use `br_ec_c25519_m64_get()` to dynamically obtain a pointer
|
||||
* to that implementation. Due to the specificities of the curve
|
||||
* definition, the following applies:
|
||||
*
|
||||
* - `muladd()` is not implemented (the function returns 0 systematically).
|
||||
* - `order()` returns 2^255-1, since the point multiplication algorithm
|
||||
* accepts any 32-bit integer as input (it clears the top bit and low
|
||||
* three bits systematically).
|
||||
*/
|
||||
extern const br_ec_impl br_ec_c25519_m64;
|
||||
|
||||
/**
|
||||
* \brief Get the "m64" implementation of Curve25519, if available.
|
||||
*
|
||||
* \return the implementation, or 0.
|
||||
*/
|
||||
const br_ec_impl *br_ec_c25519_m64_get(void);
|
||||
|
||||
/**
|
||||
* \brief Aggregate EC implementation "m31".
|
||||
*
|
||||
* This implementation is a wrapper for:
|
||||
*
|
||||
* - `br_ec_c25519_m31` for Curve25519
|
||||
* - `br_ec_p256_m31` for NIST P-256
|
||||
* - `br_ec_prime_i31` for other curves (NIST P-384 and NIST-P512)
|
||||
*/
|
||||
extern const br_ec_impl br_ec_all_m31;
|
||||
|
||||
/**
|
||||
* \brief Get the "default" EC implementation for the current system.
|
||||
*
|
||||
* This returns a pointer to the preferred implementation on the
|
||||
* current system.
|
||||
*
|
||||
* \return the default EC implementation.
|
||||
*/
|
||||
const br_ec_impl *br_ec_get_default(void);
|
||||
|
||||
/**
|
||||
* \brief Convert a signature from "asn1" to "raw".
|
||||
*
|
||||
* Conversion is done "in place" and the new length is returned.
|
||||
* Conversion may enlarge the signature, but the new signature length
|
||||
* will be less than twice the source length at most. On error, 0 is
|
||||
* returned (error conditions include an invalid ASN.1 structure or an
|
||||
* oversized integer).
|
||||
*
|
||||
* \param sig signature to convert.
|
||||
* \param sig_len signature length (in bytes).
|
||||
* \return the new signature length, or 0 on error.
|
||||
*/
|
||||
size_t br_ecdsa_asn1_to_raw(void *sig, size_t sig_len);
|
||||
|
||||
/**
|
||||
* \brief Type for an ECDSA signature verification function.
|
||||
*
|
||||
* A pointer to the EC implementation is provided. The hashed value,
|
||||
* computed over the purportedly signed data, is also provided with
|
||||
* its length.
|
||||
*
|
||||
* The signature format is either "raw" or "asn1", depending on the
|
||||
* implementation.
|
||||
*
|
||||
* Returned value is 1 on success (valid signature), 0 on error. This
|
||||
* function returns 0 if the specified curve is not supported by the
|
||||
* provided EC implementation.
|
||||
*
|
||||
* \param impl EC implementation to use.
|
||||
* \param hash signed data (hashed).
|
||||
* \param hash_len hash value length (in bytes).
|
||||
* \param pk EC public key.
|
||||
* \param sig signature.
|
||||
* \param sig_len signature length (in bytes).
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
typedef uint32_t (*br_ecdsa_vrfy)(const br_ec_impl *impl,
|
||||
const void *hash, size_t hash_len,
|
||||
const br_ec_public_key *pk, const void *sig, size_t sig_len);
|
||||
|
||||
/**
|
||||
* \brief ECDSA signature verifier, "i31" implementation, "asn1" format.
|
||||
*
|
||||
* \see br_ecdsa_vrfy()
|
||||
*
|
||||
* \param impl EC implementation to use.
|
||||
* \param hash signed data (hashed).
|
||||
* \param hash_len hash value length (in bytes).
|
||||
* \param pk EC public key.
|
||||
* \param sig signature.
|
||||
* \param sig_len signature length (in bytes).
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
uint32_t br_ecdsa_i31_vrfy_asn1(const br_ec_impl *impl,
|
||||
const void *hash, size_t hash_len,
|
||||
const br_ec_public_key *pk, const void *sig, size_t sig_len);
|
||||
|
||||
/**
|
||||
* \brief ECDSA signature verifier, "i31" implementation, "raw" format.
|
||||
*
|
||||
* \see br_ecdsa_vrfy()
|
||||
*
|
||||
* \param impl EC implementation to use.
|
||||
* \param hash signed data (hashed).
|
||||
* \param hash_len hash value length (in bytes).
|
||||
* \param pk EC public key.
|
||||
* \param sig signature.
|
||||
* \param sig_len signature length (in bytes).
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
uint32_t br_ecdsa_i31_vrfy_raw(const br_ec_impl *impl,
|
||||
const void *hash, size_t hash_len,
|
||||
const br_ec_public_key *pk, const void *sig, size_t sig_len);
|
||||
|
||||
/**
|
||||
* \brief Get "default" ECDSA implementation (verifier, asn1 format).
|
||||
*
|
||||
* This returns the preferred implementation of ECDSA signature verification
|
||||
* ("asn1" output format) on the current system.
|
||||
*
|
||||
* \return the default implementation.
|
||||
*/
|
||||
br_ecdsa_vrfy br_ecdsa_vrfy_asn1_get_default(void);
|
||||
|
||||
/**
|
||||
* \brief Get "default" ECDSA implementation (verifier, raw format).
|
||||
*
|
||||
* This returns the preferred implementation of ECDSA signature verification
|
||||
* ("raw" output format) on the current system.
|
||||
*
|
||||
* \return the default implementation.
|
||||
*/
|
||||
br_ecdsa_vrfy br_ecdsa_vrfy_raw_get_default(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
1308
third_party/bearssl/inc/bearssl_hash.h
vendored
Normal file
1308
third_party/bearssl/inc/bearssl_hash.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
241
third_party/bearssl/inc/bearssl_hmac.h
vendored
Normal file
241
third_party/bearssl/inc/bearssl_hmac.h
vendored
Normal file
@@ -0,0 +1,241 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef BR_BEARSSL_HMAC_H__
|
||||
#define BR_BEARSSL_HMAC_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "bearssl_hash.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** \file bearssl_hmac.h
|
||||
*
|
||||
* # HMAC
|
||||
*
|
||||
* HMAC is initialized with a key and an underlying hash function; it
|
||||
* then fills a "key context". That context contains the processed
|
||||
* key.
|
||||
*
|
||||
* With the key context, a HMAC context can be initialized to process
|
||||
* the input bytes and obtain the MAC output. The key context is not
|
||||
* modified during that process, and can be reused.
|
||||
*
|
||||
* IMPORTANT: HMAC shall be used only with functions that have the
|
||||
* following properties:
|
||||
*
|
||||
* - hash output size does not exceed 64 bytes;
|
||||
* - hash internal state size does not exceed 64 bytes;
|
||||
* - internal block length is a power of 2 between 16 and 256 bytes.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \brief HMAC key context.
|
||||
*
|
||||
* The HMAC key context is initialised with a hash function implementation
|
||||
* and a secret key. Contents are opaque (callers should not access them
|
||||
* directly). The caller is responsible for allocating the context where
|
||||
* appropriate. Context initialisation and usage incurs no dynamic
|
||||
* allocation, so there is no release function.
|
||||
*/
|
||||
typedef struct {
|
||||
#ifndef BR_DOXYGEN_IGNORE
|
||||
const br_hash_class *dig_vtable;
|
||||
unsigned char ksi[64], kso[64];
|
||||
#endif
|
||||
} br_hmac_key_context;
|
||||
|
||||
/**
|
||||
* \brief HMAC key context initialisation.
|
||||
*
|
||||
* Initialise the key context with the provided key, using the hash function
|
||||
* identified by `digest_vtable`. This supports arbitrary key lengths.
|
||||
*
|
||||
* \param kc HMAC key context to initialise.
|
||||
* \param digest_vtable pointer to the hash function implementation vtable.
|
||||
* \param key pointer to the HMAC secret key.
|
||||
* \param key_len HMAC secret key length (in bytes).
|
||||
*/
|
||||
void br_hmac_key_init(br_hmac_key_context *kc,
|
||||
const br_hash_class *digest_vtable, const void *key, size_t key_len);
|
||||
|
||||
/*
|
||||
* \brief Get the underlying hash function.
|
||||
*
|
||||
* This function returns a pointer to the implementation vtable of the
|
||||
* hash function used for this HMAC key context.
|
||||
*
|
||||
* \param kc HMAC key context.
|
||||
* \return the hash function implementation.
|
||||
*/
|
||||
static inline const br_hash_class *br_hmac_key_get_digest(
|
||||
const br_hmac_key_context *kc)
|
||||
{
|
||||
return kc->dig_vtable;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief HMAC computation context.
|
||||
*
|
||||
* The HMAC computation context maintains the state for a single HMAC
|
||||
* computation. It is modified as input bytes are injected. The context
|
||||
* is caller-allocated and has no release function since it does not
|
||||
* dynamically allocate external resources. Its contents are opaque.
|
||||
*/
|
||||
typedef struct {
|
||||
#ifndef BR_DOXYGEN_IGNORE
|
||||
br_hash_compat_context dig;
|
||||
unsigned char kso[64];
|
||||
size_t out_len;
|
||||
#endif
|
||||
} br_hmac_context;
|
||||
|
||||
/**
|
||||
* \brief HMAC computation initialisation.
|
||||
*
|
||||
* Initialise a HMAC context with a key context. The key context is
|
||||
* unmodified. Relevant data from the key context is immediately copied;
|
||||
* the key context can thus be independently reused, modified or released
|
||||
* without impacting this HMAC computation.
|
||||
*
|
||||
* An explicit output length can be specified; the actual output length
|
||||
* will be the minimum of that value and the natural HMAC output length.
|
||||
* If `out_len` is 0, then the natural HMAC output length is selected. The
|
||||
* "natural output length" is the output length of the underlying hash
|
||||
* function.
|
||||
*
|
||||
* \param ctx HMAC context to initialise.
|
||||
* \param kc HMAC key context (already initialised with the key).
|
||||
* \param out_len HMAC output length (0 to select "natural length").
|
||||
*/
|
||||
void br_hmac_init(br_hmac_context *ctx,
|
||||
const br_hmac_key_context *kc, size_t out_len);
|
||||
|
||||
/**
|
||||
* \brief Get the HMAC output size.
|
||||
*
|
||||
* The HMAC output size is the number of bytes that will actually be
|
||||
* produced with `br_hmac_out()` with the provided context. This function
|
||||
* MUST NOT be called on a non-initialised HMAC computation context.
|
||||
* The returned value is the minimum of the HMAC natural length (output
|
||||
* size of the underlying hash function) and the `out_len` parameter which
|
||||
* was used with the last `br_hmac_init()` call on that context (if the
|
||||
* initialisation `out_len` parameter was 0, then this function will
|
||||
* return the HMAC natural length).
|
||||
*
|
||||
* \param ctx the (already initialised) HMAC computation context.
|
||||
* \return the HMAC actual output size.
|
||||
*/
|
||||
static inline size_t
|
||||
br_hmac_size(br_hmac_context *ctx)
|
||||
{
|
||||
return ctx->out_len;
|
||||
}
|
||||
|
||||
/*
|
||||
* \brief Get the underlying hash function.
|
||||
*
|
||||
* This function returns a pointer to the implementation vtable of the
|
||||
* hash function used for this HMAC context.
|
||||
*
|
||||
* \param hc HMAC context.
|
||||
* \return the hash function implementation.
|
||||
*/
|
||||
static inline const br_hash_class *br_hmac_get_digest(
|
||||
const br_hmac_context *hc)
|
||||
{
|
||||
return hc->dig.vtable;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Inject some bytes in HMAC.
|
||||
*
|
||||
* The provided `len` bytes are injected as extra input in the HMAC
|
||||
* computation incarnated by the `ctx` HMAC context. It is acceptable
|
||||
* that `len` is zero, in which case `data` is ignored (and may be
|
||||
* `NULL`) and this function does nothing.
|
||||
*/
|
||||
void br_hmac_update(br_hmac_context *ctx, const void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Compute the HMAC output.
|
||||
*
|
||||
* The destination buffer MUST be large enough to accommodate the result;
|
||||
* its length is at most the "natural length" of HMAC (i.e. the output
|
||||
* length of the underlying hash function). The context is NOT modified;
|
||||
* further bytes may be processed. Thus, "partial HMAC" values can be
|
||||
* efficiently obtained.
|
||||
*
|
||||
* Returned value is the output length (in bytes).
|
||||
*
|
||||
* \param ctx HMAC computation context.
|
||||
* \param out destination buffer for the HMAC output.
|
||||
* \return the produced value length (in bytes).
|
||||
*/
|
||||
size_t br_hmac_out(const br_hmac_context *ctx, void *out);
|
||||
|
||||
/**
|
||||
* \brief Constant-time HMAC computation.
|
||||
*
|
||||
* This function compute the HMAC output in constant time. Some extra
|
||||
* input bytes are processed, then the output is computed. The extra
|
||||
* input consists in the `len` bytes pointed to by `data`. The `len`
|
||||
* parameter must lie between `min_len` and `max_len` (inclusive);
|
||||
* `max_len` bytes are actually read from `data`. Computing time (and
|
||||
* memory access pattern) will not depend upon the data byte contents or
|
||||
* the value of `len`.
|
||||
*
|
||||
* The output is written in the `out` buffer, that MUST be large enough
|
||||
* to receive it.
|
||||
*
|
||||
* The difference `max_len - min_len` MUST be less than 2<sup>30</sup>
|
||||
* (i.e. about one gigabyte).
|
||||
*
|
||||
* This function computes the output properly only if the underlying
|
||||
* hash function uses MD padding (i.e. MD5, SHA-1, SHA-224, SHA-256,
|
||||
* SHA-384 or SHA-512).
|
||||
*
|
||||
* The provided context is NOT modified.
|
||||
*
|
||||
* \param ctx the (already initialised) HMAC computation context.
|
||||
* \param data the extra input bytes.
|
||||
* \param len the extra input length (in bytes).
|
||||
* \param min_len minimum extra input length (in bytes).
|
||||
* \param max_len maximum extra input length (in bytes).
|
||||
* \param out destination buffer for the HMAC output.
|
||||
* \return the produced value length (in bytes).
|
||||
*/
|
||||
size_t br_hmac_outCT(const br_hmac_context *ctx,
|
||||
const void *data, size_t len, size_t min_len, size_t max_len,
|
||||
void *out);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
150
third_party/bearssl/inc/bearssl_prf.h
vendored
Normal file
150
third_party/bearssl/inc/bearssl_prf.h
vendored
Normal file
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef BR_BEARSSL_PRF_H__
|
||||
#define BR_BEARSSL_PRF_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** \file bearssl_prf.h
|
||||
*
|
||||
* # The TLS PRF
|
||||
*
|
||||
* The "PRF" is the pseudorandom function used internally during the
|
||||
* SSL/TLS handshake, notably to expand negotiated shared secrets into
|
||||
* the symmetric encryption keys that will be used to process the
|
||||
* application data.
|
||||
*
|
||||
* TLS 1.0 and 1.1 define a PRF that is based on both MD5 and SHA-1. This
|
||||
* is implemented by the `br_tls10_prf()` function.
|
||||
*
|
||||
* TLS 1.2 redefines the PRF, using an explicit hash function. The
|
||||
* `br_tls12_sha256_prf()` and `br_tls12_sha384_prf()` functions apply that
|
||||
* PRF with, respectively, SHA-256 and SHA-384. Most standard cipher suites
|
||||
* rely on the SHA-256 based PRF, but some use SHA-384.
|
||||
*
|
||||
* The PRF always uses as input three parameters: a "secret" (some
|
||||
* bytes), a "label" (ASCII string), and a "seed" (again some bytes). An
|
||||
* arbitrary output length can be produced. The "seed" is provided as an
|
||||
* arbitrary number of binary chunks, that gets internally concatenated.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \brief Type for a seed chunk.
|
||||
*
|
||||
* Each chunk may have an arbitrary length, and may be empty (no byte at
|
||||
* all). If the chunk length is zero, then the pointer to the chunk data
|
||||
* may be `NULL`.
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* \brief Pointer to the chunk data.
|
||||
*/
|
||||
const void *data;
|
||||
|
||||
/**
|
||||
* \brief Chunk length (in bytes).
|
||||
*/
|
||||
size_t len;
|
||||
} br_tls_prf_seed_chunk;
|
||||
|
||||
/**
|
||||
* \brief PRF implementation for TLS 1.0 and 1.1.
|
||||
*
|
||||
* This PRF is the one specified by TLS 1.0 and 1.1. It internally uses
|
||||
* MD5 and SHA-1.
|
||||
*
|
||||
* \param dst destination buffer.
|
||||
* \param len output length (in bytes).
|
||||
* \param secret secret value (key) for this computation.
|
||||
* \param secret_len length of "secret" (in bytes).
|
||||
* \param label PRF label (zero-terminated ASCII string).
|
||||
* \param seed_num number of seed chunks.
|
||||
* \param seed seed chnks for this computation (usually non-secret).
|
||||
*/
|
||||
void br_tls10_prf(void *dst, size_t len,
|
||||
const void *secret, size_t secret_len, const char *label,
|
||||
size_t seed_num, const br_tls_prf_seed_chunk *seed);
|
||||
|
||||
/**
|
||||
* \brief PRF implementation for TLS 1.2, with SHA-256.
|
||||
*
|
||||
* This PRF is the one specified by TLS 1.2, when the underlying hash
|
||||
* function is SHA-256.
|
||||
*
|
||||
* \param dst destination buffer.
|
||||
* \param len output length (in bytes).
|
||||
* \param secret secret value (key) for this computation.
|
||||
* \param secret_len length of "secret" (in bytes).
|
||||
* \param label PRF label (zero-terminated ASCII string).
|
||||
* \param seed_num number of seed chunks.
|
||||
* \param seed seed chnks for this computation (usually non-secret).
|
||||
*/
|
||||
void br_tls12_sha256_prf(void *dst, size_t len,
|
||||
const void *secret, size_t secret_len, const char *label,
|
||||
size_t seed_num, const br_tls_prf_seed_chunk *seed);
|
||||
|
||||
/**
|
||||
* \brief PRF implementation for TLS 1.2, with SHA-384.
|
||||
*
|
||||
* This PRF is the one specified by TLS 1.2, when the underlying hash
|
||||
* function is SHA-384.
|
||||
*
|
||||
* \param dst destination buffer.
|
||||
* \param len output length (in bytes).
|
||||
* \param secret secret value (key) for this computation.
|
||||
* \param secret_len length of "secret" (in bytes).
|
||||
* \param label PRF label (zero-terminated ASCII string).
|
||||
* \param seed_num number of seed chunks.
|
||||
* \param seed seed chnks for this computation (usually non-secret).
|
||||
*/
|
||||
void br_tls12_sha384_prf(void *dst, size_t len,
|
||||
const void *secret, size_t secret_len, const char *label,
|
||||
size_t seed_num, const br_tls_prf_seed_chunk *seed);
|
||||
|
||||
/**
|
||||
* brief A convenient type name for a PRF implementation.
|
||||
*
|
||||
* \param dst destination buffer.
|
||||
* \param len output length (in bytes).
|
||||
* \param secret secret value (key) for this computation.
|
||||
* \param secret_len length of "secret" (in bytes).
|
||||
* \param label PRF label (zero-terminated ASCII string).
|
||||
* \param seed_num number of seed chunks.
|
||||
* \param seed seed chnks for this computation (usually non-secret).
|
||||
*/
|
||||
typedef void (*br_tls_prf_impl)(void *dst, size_t len,
|
||||
const void *secret, size_t secret_len, const char *label,
|
||||
size_t seed_num, const br_tls_prf_seed_chunk *seed);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
397
third_party/bearssl/inc/bearssl_rand.h
vendored
Normal file
397
third_party/bearssl/inc/bearssl_rand.h
vendored
Normal file
@@ -0,0 +1,397 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef BR_BEARSSL_RAND_H__
|
||||
#define BR_BEARSSL_RAND_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "bearssl_block.h"
|
||||
#include "bearssl_hash.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** \file bearssl_rand.h
|
||||
*
|
||||
* # Pseudo-Random Generators
|
||||
*
|
||||
* A PRNG is a state-based engine that outputs pseudo-random bytes on
|
||||
* demand. It is initialized with an initial seed, and additional seed
|
||||
* bytes can be added afterwards. Bytes produced depend on the seeds and
|
||||
* also on the exact sequence of calls (including sizes requested for
|
||||
* each call).
|
||||
*
|
||||
*
|
||||
* ## Procedural and OOP API
|
||||
*
|
||||
* For the PRNG of name "`xxx`", two API are provided. The _procedural_
|
||||
* API defined a context structure `br_xxx_context` and three functions:
|
||||
*
|
||||
* - `br_xxx_init()`
|
||||
*
|
||||
* Initialise the context with an initial seed.
|
||||
*
|
||||
* - `br_xxx_generate()`
|
||||
*
|
||||
* Produce some pseudo-random bytes.
|
||||
*
|
||||
* - `br_xxx_update()`
|
||||
*
|
||||
* Inject some additional seed.
|
||||
*
|
||||
* The initialisation function sets the first context field (`vtable`)
|
||||
* to a pointer to the vtable that supports the OOP API. The OOP API
|
||||
* provides access to the same functions through function pointers,
|
||||
* named `init()`, `generate()` and `update()`.
|
||||
*
|
||||
* Note that the context initialisation method may accept additional
|
||||
* parameters, provided as a 'const void *' pointer at API level. These
|
||||
* additional parameters depend on the implemented PRNG.
|
||||
*
|
||||
*
|
||||
* ## HMAC_DRBG
|
||||
*
|
||||
* HMAC_DRBG is defined in [NIST SP 800-90A Revision
|
||||
* 1](http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-90Ar1.pdf).
|
||||
* It uses HMAC repeatedly, over some configurable underlying hash
|
||||
* function. In BearSSL, it is implemented under the "`hmac_drbg`" name.
|
||||
* The "extra parameters" pointer for context initialisation should be
|
||||
* set to a pointer to the vtable for the underlying hash function (e.g.
|
||||
* pointer to `br_sha256_vtable` to use HMAC_DRBG with SHA-256).
|
||||
*
|
||||
* According to the NIST standard, each request shall produce up to
|
||||
* 2<sup>19</sup> bits (i.e. 64 kB of data); moreover, the context shall
|
||||
* be reseeded at least once every 2<sup>48</sup> requests. This
|
||||
* implementation does not maintain the reseed counter (the threshold is
|
||||
* too high to be reached in practice) and does not object to producing
|
||||
* more than 64 kB in a single request; thus, the code cannot fail,
|
||||
* which corresponds to the fact that the API has no room for error
|
||||
* codes. However, this implies that requesting more than 64 kB in one
|
||||
* `generate()` request, or making more than 2<sup>48</sup> requests
|
||||
* without reseeding, is formally out of NIST specification. There is
|
||||
* no currently known security penalty for exceeding the NIST limits,
|
||||
* and, in any case, HMAC_DRBG usage in implementing SSL/TLS always
|
||||
* stays much below these thresholds.
|
||||
*
|
||||
*
|
||||
* ## AESCTR_DRBG
|
||||
*
|
||||
* AESCTR_DRBG is a custom PRNG based on AES-128 in CTR mode. This is
|
||||
* meant to be used only in situations where you are desperate for
|
||||
* speed, and have an hardware-optimized AES/CTR implementation. Whether
|
||||
* this will yield perceptible improvements depends on what you use the
|
||||
* pseudorandom bytes for, and how many you want; for instance, RSA key
|
||||
* pair generation uses a substantial amount of randomness, and using
|
||||
* AESCTR_DRBG instead of HMAC_DRBG yields a 15 to 20% increase in key
|
||||
* generation speed on a recent x86 CPU (Intel Core i7-6567U at 3.30 GHz).
|
||||
*
|
||||
* Internally, it uses CTR mode with successive counter values, starting
|
||||
* at zero (counter value expressed over 128 bits, big-endian convention).
|
||||
* The counter is not allowed to reach 32768; thus, every 32768*16 bytes
|
||||
* at most, the `update()` function is run (on an empty seed, if none is
|
||||
* provided). The `update()` function computes the new AES-128 key by
|
||||
* applying a custom hash function to the concatenation of a state-dependent
|
||||
* word (encryption of an all-one block with the current key) and the new
|
||||
* seed. The custom hash function uses Hirose's construction over AES-256;
|
||||
* see the comments in `aesctr_drbg.c` for details.
|
||||
*
|
||||
* This DRBG does not follow an existing standard, and thus should be
|
||||
* considered as inadequate for production use until it has been properly
|
||||
* analysed.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \brief Class type for PRNG implementations.
|
||||
*
|
||||
* A `br_prng_class` instance references the methods implementing a PRNG.
|
||||
* Constant instances of this structure are defined for each implemented
|
||||
* PRNG. Such instances are also called "vtables".
|
||||
*/
|
||||
typedef struct br_prng_class_ br_prng_class;
|
||||
struct br_prng_class_ {
|
||||
/**
|
||||
* \brief Size (in bytes) of the context structure appropriate for
|
||||
* running this PRNG.
|
||||
*/
|
||||
size_t context_size;
|
||||
|
||||
/**
|
||||
* \brief Initialisation method.
|
||||
*
|
||||
* The context to initialise is provided as a pointer to its
|
||||
* first field (the vtable pointer); this function sets that
|
||||
* first field to a pointer to the vtable.
|
||||
*
|
||||
* The extra parameters depend on the implementation; each
|
||||
* implementation defines what kind of extra parameters it
|
||||
* expects (if any).
|
||||
*
|
||||
* Requirements on the initial seed depend on the implemented
|
||||
* PRNG.
|
||||
*
|
||||
* \param ctx PRNG context to initialise.
|
||||
* \param params extra parameters for the PRNG.
|
||||
* \param seed initial seed.
|
||||
* \param seed_len initial seed length (in bytes).
|
||||
*/
|
||||
void (*init)(const br_prng_class **ctx, const void *params,
|
||||
const void *seed, size_t seed_len);
|
||||
|
||||
/**
|
||||
* \brief Random bytes generation.
|
||||
*
|
||||
* This method produces `len` pseudorandom bytes, in the `out`
|
||||
* buffer. The context is updated accordingly.
|
||||
*
|
||||
* \param ctx PRNG context.
|
||||
* \param out output buffer.
|
||||
* \param len number of pseudorandom bytes to produce.
|
||||
*/
|
||||
void (*generate)(const br_prng_class **ctx, void *out, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Inject additional seed bytes.
|
||||
*
|
||||
* The provided seed bytes are added into the PRNG internal
|
||||
* entropy pool.
|
||||
*
|
||||
* \param ctx PRNG context.
|
||||
* \param seed additional seed.
|
||||
* \param seed_len additional seed length (in bytes).
|
||||
*/
|
||||
void (*update)(const br_prng_class **ctx,
|
||||
const void *seed, size_t seed_len);
|
||||
};
|
||||
|
||||
/**
|
||||
* \brief Context for HMAC_DRBG.
|
||||
*
|
||||
* The context contents are opaque, except the first field, which
|
||||
* supports OOP.
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* \brief Pointer to the vtable.
|
||||
*
|
||||
* This field is set with the initialisation method/function.
|
||||
*/
|
||||
const br_prng_class *vtable;
|
||||
#ifndef BR_DOXYGEN_IGNORE
|
||||
unsigned char K[64];
|
||||
unsigned char V[64];
|
||||
const br_hash_class *digest_class;
|
||||
#endif
|
||||
} br_hmac_drbg_context;
|
||||
|
||||
/**
|
||||
* \brief Statically allocated, constant vtable for HMAC_DRBG.
|
||||
*/
|
||||
extern const br_prng_class br_hmac_drbg_vtable;
|
||||
|
||||
/**
|
||||
* \brief HMAC_DRBG initialisation.
|
||||
*
|
||||
* The context to initialise is provided as a pointer to its first field
|
||||
* (the vtable pointer); this function sets that first field to a
|
||||
* pointer to the vtable.
|
||||
*
|
||||
* The `seed` value is what is called, in NIST terminology, the
|
||||
* concatenation of the "seed", "nonce" and "personalization string", in
|
||||
* that order.
|
||||
*
|
||||
* The `digest_class` parameter defines the underlying hash function.
|
||||
* Formally, the NIST standard specifies that the hash function shall
|
||||
* be only SHA-1 or one of the SHA-2 functions. This implementation also
|
||||
* works with any other implemented hash function (such as MD5), but
|
||||
* this is non-standard and therefore not recommended.
|
||||
*
|
||||
* \param ctx HMAC_DRBG context to initialise.
|
||||
* \param digest_class vtable for the underlying hash function.
|
||||
* \param seed initial seed.
|
||||
* \param seed_len initial seed length (in bytes).
|
||||
*/
|
||||
void br_hmac_drbg_init(br_hmac_drbg_context *ctx,
|
||||
const br_hash_class *digest_class, const void *seed, size_t seed_len);
|
||||
|
||||
/**
|
||||
* \brief Random bytes generation with HMAC_DRBG.
|
||||
*
|
||||
* This method produces `len` pseudorandom bytes, in the `out`
|
||||
* buffer. The context is updated accordingly. Formally, requesting
|
||||
* more than 65536 bytes in one request falls out of specification
|
||||
* limits (but it won't fail).
|
||||
*
|
||||
* \param ctx HMAC_DRBG context.
|
||||
* \param out output buffer.
|
||||
* \param len number of pseudorandom bytes to produce.
|
||||
*/
|
||||
void br_hmac_drbg_generate(br_hmac_drbg_context *ctx, void *out, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Inject additional seed bytes in HMAC_DRBG.
|
||||
*
|
||||
* The provided seed bytes are added into the HMAC_DRBG internal
|
||||
* entropy pool. The process does not _replace_ existing entropy,
|
||||
* thus pushing non-random bytes (i.e. bytes which are known to the
|
||||
* attackers) does not degrade the overall quality of generated bytes.
|
||||
*
|
||||
* \param ctx HMAC_DRBG context.
|
||||
* \param seed additional seed.
|
||||
* \param seed_len additional seed length (in bytes).
|
||||
*/
|
||||
void br_hmac_drbg_update(br_hmac_drbg_context *ctx,
|
||||
const void *seed, size_t seed_len);
|
||||
|
||||
/**
|
||||
* \brief Get the hash function implementation used by a given instance of
|
||||
* HMAC_DRBG.
|
||||
*
|
||||
* This calls MUST NOT be performed on a context which was not
|
||||
* previously initialised.
|
||||
*
|
||||
* \param ctx HMAC_DRBG context.
|
||||
* \return the hash function vtable.
|
||||
*/
|
||||
static inline const br_hash_class *
|
||||
br_hmac_drbg_get_hash(const br_hmac_drbg_context *ctx)
|
||||
{
|
||||
return ctx->digest_class;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Type for a provider of entropy seeds.
|
||||
*
|
||||
* A "seeder" is a function that is able to obtain random values from
|
||||
* some source and inject them as entropy seed in a PRNG. A seeder
|
||||
* shall guarantee that the total entropy of the injected seed is large
|
||||
* enough to seed a PRNG for purposes of cryptographic key generation
|
||||
* (i.e. at least 128 bits).
|
||||
*
|
||||
* A seeder may report a failure to obtain adequate entropy. Seeders
|
||||
* shall endeavour to fix themselves transient errors by trying again;
|
||||
* thus, callers may consider reported errors as permanent.
|
||||
*
|
||||
* \param ctx PRNG context to seed.
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
typedef int (*br_prng_seeder)(const br_prng_class **ctx);
|
||||
|
||||
/**
|
||||
* \brief Get a seeder backed by the operating system or hardware.
|
||||
*
|
||||
* Get a seeder that feeds on RNG facilities provided by the current
|
||||
* operating system or hardware. If no such facility is known, then 0
|
||||
* is returned.
|
||||
*
|
||||
* If `name` is not `NULL`, then `*name` is set to a symbolic string
|
||||
* that identifies the seeder implementation. If no seeder is returned
|
||||
* and `name` is not `NULL`, then `*name` is set to a pointer to the
|
||||
* constant string `"none"`.
|
||||
*
|
||||
* \param name receiver for seeder name, or `NULL`.
|
||||
* \return the system seeder, if available, or 0.
|
||||
*/
|
||||
br_prng_seeder br_prng_seeder_system(const char **name);
|
||||
|
||||
/**
|
||||
* \brief Context for AESCTR_DRBG.
|
||||
*
|
||||
* The context contents are opaque, except the first field, which
|
||||
* supports OOP.
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* \brief Pointer to the vtable.
|
||||
*
|
||||
* This field is set with the initialisation method/function.
|
||||
*/
|
||||
const br_prng_class *vtable;
|
||||
#ifndef BR_DOXYGEN_IGNORE
|
||||
br_aes_gen_ctr_keys sk;
|
||||
uint32_t cc;
|
||||
#endif
|
||||
} br_aesctr_drbg_context;
|
||||
|
||||
/**
|
||||
* \brief Statically allocated, constant vtable for AESCTR_DRBG.
|
||||
*/
|
||||
extern const br_prng_class br_aesctr_drbg_vtable;
|
||||
|
||||
/**
|
||||
* \brief AESCTR_DRBG initialisation.
|
||||
*
|
||||
* The context to initialise is provided as a pointer to its first field
|
||||
* (the vtable pointer); this function sets that first field to a
|
||||
* pointer to the vtable.
|
||||
*
|
||||
* The internal AES key is first set to the all-zero key; then, the
|
||||
* `br_aesctr_drbg_update()` function is called with the provided `seed`.
|
||||
* The call is performed even if the seed length (`seed_len`) is zero.
|
||||
*
|
||||
* The `aesctr` parameter defines the underlying AES/CTR implementation.
|
||||
*
|
||||
* \param ctx AESCTR_DRBG context to initialise.
|
||||
* \param aesctr vtable for the AES/CTR implementation.
|
||||
* \param seed initial seed (can be `NULL` if `seed_len` is zero).
|
||||
* \param seed_len initial seed length (in bytes).
|
||||
*/
|
||||
void br_aesctr_drbg_init(br_aesctr_drbg_context *ctx,
|
||||
const br_block_ctr_class *aesctr, const void *seed, size_t seed_len);
|
||||
|
||||
/**
|
||||
* \brief Random bytes generation with AESCTR_DRBG.
|
||||
*
|
||||
* This method produces `len` pseudorandom bytes, in the `out`
|
||||
* buffer. The context is updated accordingly.
|
||||
*
|
||||
* \param ctx AESCTR_DRBG context.
|
||||
* \param out output buffer.
|
||||
* \param len number of pseudorandom bytes to produce.
|
||||
*/
|
||||
void br_aesctr_drbg_generate(br_aesctr_drbg_context *ctx,
|
||||
void *out, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Inject additional seed bytes in AESCTR_DRBG.
|
||||
*
|
||||
* The provided seed bytes are added into the AESCTR_DRBG internal
|
||||
* entropy pool. The process does not _replace_ existing entropy,
|
||||
* thus pushing non-random bytes (i.e. bytes which are known to the
|
||||
* attackers) does not degrade the overall quality of generated bytes.
|
||||
*
|
||||
* \param ctx AESCTR_DRBG context.
|
||||
* \param seed additional seed.
|
||||
* \param seed_len additional seed length (in bytes).
|
||||
*/
|
||||
void br_aesctr_drbg_update(br_aesctr_drbg_context *ctx,
|
||||
const void *seed, size_t seed_len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
608
third_party/bearssl/inc/bearssl_rsa.h
vendored
Normal file
608
third_party/bearssl/inc/bearssl_rsa.h
vendored
Normal file
@@ -0,0 +1,608 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef BR_BEARSSL_RSA_H__
|
||||
#define BR_BEARSSL_RSA_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "bearssl_hash.h"
|
||||
#include "bearssl_rand.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** \file bearssl_rsa.h
|
||||
*
|
||||
* # RSA
|
||||
*
|
||||
* This file documents the RSA implementations provided with BearSSL.
|
||||
* Note that the SSL engine accesses these implementations through a
|
||||
* configurable API, so it is possible to, for instance, run a SSL
|
||||
* server which uses a RSA engine which is not based on this code.
|
||||
*
|
||||
* ## Key Elements
|
||||
*
|
||||
* RSA public and private keys consist in lists of big integers. All
|
||||
* such integers are represented with big-endian unsigned notation:
|
||||
* first byte is the most significant, and the value is positive (so
|
||||
* there is no dedicated "sign bit"). Public and private key structures
|
||||
* thus contain, for each such integer, a pointer to the first value byte
|
||||
* (`unsigned char *`), and a length (`size_t`) which is the number of
|
||||
* relevant bytes. As a general rule, minimal-length encoding is not
|
||||
* enforced: values may have extra leading bytes of value 0.
|
||||
*
|
||||
* RSA public keys consist in two integers:
|
||||
*
|
||||
* - the modulus (`n`);
|
||||
* - the public exponent (`e`).
|
||||
*
|
||||
* RSA private keys, as defined in
|
||||
* [PKCS#1](https://tools.ietf.org/html/rfc3447), contain eight integers:
|
||||
*
|
||||
* - the modulus (`n`);
|
||||
* - the public exponent (`e`);
|
||||
* - the private exponent (`d`);
|
||||
* - the first prime factor (`p`);
|
||||
* - the second prime factor (`q`);
|
||||
* - the first reduced exponent (`dp`, which is `d` modulo `p-1`);
|
||||
* - the second reduced exponent (`dq`, which is `d` modulo `q-1`);
|
||||
* - the CRT coefficient (`iq`, the inverse of `q` modulo `p`).
|
||||
*
|
||||
* However, the implementations defined in BearSSL use only five of
|
||||
* these integers: `p`, `q`, `dp`, `dq` and `iq`.
|
||||
*
|
||||
* ## Security Features and Limitations
|
||||
*
|
||||
* The implementations contained in BearSSL have the following limitations
|
||||
* and features:
|
||||
*
|
||||
* - They are constant-time. This means that the execution time and
|
||||
* memory access pattern may depend on the _lengths_ of the private
|
||||
* key components, but not on their value, nor on the value of
|
||||
* the operand. Note that this property is not achieved through
|
||||
* random masking, but "true" constant-time code.
|
||||
*
|
||||
* - They support only private keys with two prime factors. RSA private
|
||||
* keys with three or more prime factors are nominally supported, but
|
||||
* rarely used; they may offer faster operations, at the expense of
|
||||
* more code and potentially a reduction in security if there are
|
||||
* "too many" prime factors.
|
||||
*
|
||||
* - The public exponent may have arbitrary length. Of course, it is
|
||||
* a good idea to keep public exponents small, so that public key
|
||||
* operations are fast; but, contrary to some widely deployed
|
||||
* implementations, BearSSL has no problem with public exponents
|
||||
* longer than 32 bits.
|
||||
*
|
||||
* - The two prime factors of the modulus need not have the same length
|
||||
* (but severely imbalanced factor lengths might reduce security).
|
||||
* Similarly, there is no requirement that the first factor (`p`)
|
||||
* be greater than the second factor (`q`).
|
||||
*
|
||||
* - Prime factors and modulus must be smaller than a compile-time limit.
|
||||
* This is made necessary by the use of fixed-size stack buffers, and
|
||||
* the limit has been adjusted to keep stack usage under 2 kB for the
|
||||
* RSA operations. Currently, the maximum modulus size is 4096 bits,
|
||||
* and the maximum prime factor size is 2080 bits.
|
||||
*
|
||||
* - The RSA functions themselves do not enforce lower size limits,
|
||||
* except that which is absolutely necessary for the operation to
|
||||
* mathematically make sense (e.g. a PKCS#1 v1.5 signature with
|
||||
* SHA-1 requires a modulus of at least 361 bits). It is up to users
|
||||
* of this code to enforce size limitations when appropriate (e.g.
|
||||
* the X.509 validation engine, by default, rejects RSA keys of
|
||||
* less than 1017 bits).
|
||||
*
|
||||
* - Within the size constraints expressed above, arbitrary bit lengths
|
||||
* are supported. There is no requirement that prime factors or
|
||||
* modulus have a size multiple of 8 or 16.
|
||||
*
|
||||
* - When verifying PKCS#1 v1.5 signatures, both variants of the hash
|
||||
* function identifying header (with and without the ASN.1 NULL) are
|
||||
* supported. When producing such signatures, the variant with the
|
||||
* ASN.1 NULL is used.
|
||||
*
|
||||
* ## Implementations
|
||||
*
|
||||
* Three RSA implementations are included:
|
||||
*
|
||||
* - The **i32** implementation internally represents big integers
|
||||
* as arrays of 32-bit integers. It is perfunctory and portable,
|
||||
* but not very efficient.
|
||||
*
|
||||
* - The **i31** implementation uses 32-bit integers, each containing
|
||||
* 31 bits worth of integer data. The i31 implementation is somewhat
|
||||
* faster than the i32 implementation (the reduced integer size makes
|
||||
* carry propagation easier) for a similar code footprint, but uses
|
||||
* very slightly larger stack buffers (about 4% bigger).
|
||||
*
|
||||
* - The **i62** implementation is similar to the i31 implementation,
|
||||
* except that it internally leverages the 64x64->128 multiplication
|
||||
* opcode. This implementation is available only on architectures
|
||||
* where such an opcode exists. It is much faster than i31.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \brief RSA public key.
|
||||
*
|
||||
* The structure references the modulus and the public exponent. Both
|
||||
* integers use unsigned big-endian representation; extra leading bytes
|
||||
* of value 0 are allowed.
|
||||
*/
|
||||
typedef struct {
|
||||
/** \brief Modulus. */
|
||||
unsigned char *n;
|
||||
/** \brief Modulus length (in bytes). */
|
||||
size_t nlen;
|
||||
/** \brief Public exponent. */
|
||||
unsigned char *e;
|
||||
/** \brief Public exponent length (in bytes). */
|
||||
size_t elen;
|
||||
} br_rsa_public_key;
|
||||
|
||||
/**
|
||||
* \brief RSA private key.
|
||||
*
|
||||
* The structure references the private factors, reduced private
|
||||
* exponents, and CRT coefficient. It also contains the bit length of
|
||||
* the modulus. The big integers use unsigned big-endian representation;
|
||||
* extra leading bytes of value 0 are allowed. However, the modulus bit
|
||||
* length (`n_bitlen`) MUST be exact.
|
||||
*/
|
||||
typedef struct {
|
||||
/** \brief Modulus bit length (in bits, exact value). */
|
||||
uint32_t n_bitlen;
|
||||
/** \brief First prime factor. */
|
||||
unsigned char *p;
|
||||
/** \brief First prime factor length (in bytes). */
|
||||
size_t plen;
|
||||
/** \brief Second prime factor. */
|
||||
unsigned char *q;
|
||||
/** \brief Second prime factor length (in bytes). */
|
||||
size_t qlen;
|
||||
/** \brief First reduced private exponent. */
|
||||
unsigned char *dp;
|
||||
/** \brief First reduced private exponent length (in bytes). */
|
||||
size_t dplen;
|
||||
/** \brief Second reduced private exponent. */
|
||||
unsigned char *dq;
|
||||
/** \brief Second reduced private exponent length (in bytes). */
|
||||
size_t dqlen;
|
||||
/** \brief CRT coefficient. */
|
||||
unsigned char *iq;
|
||||
/** \brief CRT coefficient length (in bytes). */
|
||||
size_t iqlen;
|
||||
} br_rsa_private_key;
|
||||
|
||||
/**
|
||||
* \brief Type for a RSA public key engine.
|
||||
*
|
||||
* The public key engine performs the modular exponentiation of the
|
||||
* provided value with the public exponent. The value is modified in
|
||||
* place.
|
||||
*
|
||||
* The value length (`xlen`) is verified to have _exactly_ the same
|
||||
* length as the modulus (actual modulus length, without extra leading
|
||||
* zeros in the modulus representation in memory). If the length does
|
||||
* not match, then this function returns 0 and `x[]` is unmodified.
|
||||
*
|
||||
* It `xlen` is correct, then `x[]` is modified. Returned value is 1
|
||||
* on success, 0 on error. Error conditions include an oversized `x[]`
|
||||
* (the array has the same length as the modulus, but the numerical value
|
||||
* is not lower than the modulus) and an invalid modulus (e.g. an even
|
||||
* integer). If an error is reported, then the new contents of `x[]` are
|
||||
* unspecified.
|
||||
*
|
||||
* \param x operand to exponentiate.
|
||||
* \param xlen length of the operand (in bytes).
|
||||
* \param pk RSA public key.
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
typedef uint32_t (*br_rsa_public)(unsigned char *x, size_t xlen,
|
||||
const br_rsa_public_key *pk);
|
||||
|
||||
/**
|
||||
* \brief Type for a RSA signature verification engine (PKCS#1 v1.5).
|
||||
*
|
||||
* Parameters are:
|
||||
*
|
||||
* - The signature itself. The provided array is NOT modified.
|
||||
*
|
||||
* - The encoded OID for the hash function. The provided array must begin
|
||||
* with a single byte that contains the length of the OID value (in
|
||||
* bytes), followed by exactly that many bytes. This parameter may
|
||||
* also be `NULL`, in which case the raw hash value should be used
|
||||
* with the PKCS#1 v1.5 "type 1" padding (as used in SSL/TLS up
|
||||
* to TLS-1.1, with a 36-byte hash value).
|
||||
*
|
||||
* - The hash output length, in bytes.
|
||||
*
|
||||
* - The public key.
|
||||
*
|
||||
* - An output buffer for the hash value. The caller must still compare
|
||||
* it with the hash of the data over which the signature is computed.
|
||||
*
|
||||
* **Constraints:**
|
||||
*
|
||||
* - Hash length MUST be no more than 64 bytes.
|
||||
*
|
||||
* - OID value length MUST be no more than 32 bytes (i.e. `hash_oid[0]`
|
||||
* must have a value in the 0..32 range, inclusive).
|
||||
*
|
||||
* This function verifies that the signature length (`xlen`) matches the
|
||||
* modulus length (this function returns 0 on mismatch). If the modulus
|
||||
* size exceeds the maximum supported RSA size, then the function also
|
||||
* returns 0.
|
||||
*
|
||||
* Returned value is 1 on success, 0 on error.
|
||||
*
|
||||
* Implementations of this type need not be constant-time.
|
||||
*
|
||||
* \param x signature buffer.
|
||||
* \param xlen signature length (in bytes).
|
||||
* \param hash_oid encoded hash algorithm OID (or `NULL`).
|
||||
* \param hash_len expected hash value length (in bytes).
|
||||
* \param pk RSA public key.
|
||||
* \param hash_out output buffer for the hash value.
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
typedef uint32_t (*br_rsa_pkcs1_vrfy)(const unsigned char *x, size_t xlen,
|
||||
const unsigned char *hash_oid, size_t hash_len,
|
||||
const br_rsa_public_key *pk, unsigned char *hash_out);
|
||||
|
||||
/**
|
||||
* \brief Type for a RSA private key engine.
|
||||
*
|
||||
* The `x[]` buffer is modified in place, and its length is inferred from
|
||||
* the modulus length (`x[]` is assumed to have a length of
|
||||
* `(sk->n_bitlen+7)/8` bytes).
|
||||
*
|
||||
* Returned value is 1 on success, 0 on error.
|
||||
*
|
||||
* \param x operand to exponentiate.
|
||||
* \param sk RSA private key.
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
typedef uint32_t (*br_rsa_private)(unsigned char *x,
|
||||
const br_rsa_private_key *sk);
|
||||
|
||||
/**
|
||||
* \brief Type for a RSA signature generation engine (PKCS#1 v1.5).
|
||||
*
|
||||
* Parameters are:
|
||||
*
|
||||
* - The encoded OID for the hash function. The provided array must begin
|
||||
* with a single byte that contains the length of the OID value (in
|
||||
* bytes), followed by exactly that many bytes. This parameter may
|
||||
* also be `NULL`, in which case the raw hash value should be used
|
||||
* with the PKCS#1 v1.5 "type 1" padding (as used in SSL/TLS up
|
||||
* to TLS-1.1, with a 36-byte hash value).
|
||||
*
|
||||
* - The hash value computes over the data to sign (its length is
|
||||
* expressed in bytes).
|
||||
*
|
||||
* - The RSA private key.
|
||||
*
|
||||
* - The output buffer, that receives the signature.
|
||||
*
|
||||
* Returned value is 1 on success, 0 on error. Error conditions include
|
||||
* a too small modulus for the provided hash OID and value, or some
|
||||
* invalid key parameters. The signature length is exactly
|
||||
* `(sk->n_bitlen+7)/8` bytes.
|
||||
*
|
||||
* This function is expected to be constant-time with regards to the
|
||||
* private key bytes (lengths of the modulus and the individual factors
|
||||
* may leak, though) and to the hashed data.
|
||||
*
|
||||
* \param hash_oid encoded hash algorithm OID (or `NULL`).
|
||||
* \param hash hash value.
|
||||
* \param hash_len hash value length (in bytes).
|
||||
* \param sk RSA private key.
|
||||
* \param x output buffer for the signature value.
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
typedef uint32_t (*br_rsa_pkcs1_sign)(const unsigned char *hash_oid,
|
||||
const unsigned char *hash, size_t hash_len,
|
||||
const br_rsa_private_key *sk, unsigned char *x);
|
||||
|
||||
/**
|
||||
* \brief Encoded OID for SHA-1 (in RSA PKCS#1 signatures).
|
||||
*/
|
||||
#define BR_HASH_OID_SHA1 \
|
||||
((const unsigned char *)"\x05\x2B\x0E\x03\x02\x1A")
|
||||
|
||||
/**
|
||||
* \brief Encoded OID for SHA-224 (in RSA PKCS#1 signatures).
|
||||
*/
|
||||
#define BR_HASH_OID_SHA224 \
|
||||
((const unsigned char *)"\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04")
|
||||
|
||||
/**
|
||||
* \brief Encoded OID for SHA-256 (in RSA PKCS#1 signatures).
|
||||
*/
|
||||
#define BR_HASH_OID_SHA256 \
|
||||
((const unsigned char *)"\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01")
|
||||
|
||||
/**
|
||||
* \brief Encoded OID for SHA-384 (in RSA PKCS#1 signatures).
|
||||
*/
|
||||
#define BR_HASH_OID_SHA384 \
|
||||
((const unsigned char *)"\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02")
|
||||
|
||||
/**
|
||||
* \brief Encoded OID for SHA-512 (in RSA PKCS#1 signatures).
|
||||
*/
|
||||
#define BR_HASH_OID_SHA512 \
|
||||
((const unsigned char *)"\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03")
|
||||
|
||||
/*
|
||||
* RSA "i32" engine. Integers are internally represented as arrays of
|
||||
* 32-bit integers, and the core multiplication primitive is the
|
||||
* 32x32->64 multiplication.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \brief RSA public key engine "i32".
|
||||
*
|
||||
* \see br_rsa_public
|
||||
*
|
||||
* \param x operand to exponentiate.
|
||||
* \param xlen length of the operand (in bytes).
|
||||
* \param pk RSA public key.
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
uint32_t br_rsa_i32_public(unsigned char *x, size_t xlen,
|
||||
const br_rsa_public_key *pk);
|
||||
|
||||
/**
|
||||
* \brief RSA signature verification engine "i32" (PKCS#1 v1.5 signatures).
|
||||
*
|
||||
* \see br_rsa_pkcs1_vrfy
|
||||
*
|
||||
* \param x signature buffer.
|
||||
* \param xlen signature length (in bytes).
|
||||
* \param hash_oid encoded hash algorithm OID (or `NULL`).
|
||||
* \param hash_len expected hash value length (in bytes).
|
||||
* \param pk RSA public key.
|
||||
* \param hash_out output buffer for the hash value.
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
uint32_t br_rsa_i32_pkcs1_vrfy(const unsigned char *x, size_t xlen,
|
||||
const unsigned char *hash_oid, size_t hash_len,
|
||||
const br_rsa_public_key *pk, unsigned char *hash_out);
|
||||
|
||||
/**
|
||||
* \brief RSA private key engine "i32".
|
||||
*
|
||||
* \see br_rsa_private
|
||||
*
|
||||
* \param x operand to exponentiate.
|
||||
* \param sk RSA private key.
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
uint32_t br_rsa_i32_private(unsigned char *x,
|
||||
const br_rsa_private_key *sk);
|
||||
|
||||
/**
|
||||
* \brief RSA signature generation engine "i32" (PKCS#1 v1.5 signatures).
|
||||
*
|
||||
* \see br_rsa_pkcs1_sign
|
||||
*
|
||||
* \param hash_oid encoded hash algorithm OID (or `NULL`).
|
||||
* \param hash hash value.
|
||||
* \param hash_len hash value length (in bytes).
|
||||
* \param sk RSA private key.
|
||||
* \param x output buffer for the hash value.
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
uint32_t br_rsa_i32_pkcs1_sign(const unsigned char *hash_oid,
|
||||
const unsigned char *hash, size_t hash_len,
|
||||
const br_rsa_private_key *sk, unsigned char *x);
|
||||
|
||||
/*
|
||||
* RSA "i31" engine. Similar to i32, but only 31 bits are used per 32-bit
|
||||
* word. This uses slightly more stack space (about 4% more) and code
|
||||
* space, but it quite faster.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \brief RSA public key engine "i31".
|
||||
*
|
||||
* \see br_rsa_public
|
||||
*
|
||||
* \param x operand to exponentiate.
|
||||
* \param xlen length of the operand (in bytes).
|
||||
* \param pk RSA public key.
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
uint32_t br_rsa_i31_public(unsigned char *x, size_t xlen,
|
||||
const br_rsa_public_key *pk);
|
||||
|
||||
/**
|
||||
* \brief RSA signature verification engine "i31" (PKCS#1 v1.5 signatures).
|
||||
*
|
||||
* \see br_rsa_pkcs1_vrfy
|
||||
*
|
||||
* \param x signature buffer.
|
||||
* \param xlen signature length (in bytes).
|
||||
* \param hash_oid encoded hash algorithm OID (or `NULL`).
|
||||
* \param hash_len expected hash value length (in bytes).
|
||||
* \param pk RSA public key.
|
||||
* \param hash_out output buffer for the hash value.
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
uint32_t br_rsa_i31_pkcs1_vrfy(const unsigned char *x, size_t xlen,
|
||||
const unsigned char *hash_oid, size_t hash_len,
|
||||
const br_rsa_public_key *pk, unsigned char *hash_out);
|
||||
|
||||
/**
|
||||
* \brief RSA private key engine "i31".
|
||||
*
|
||||
* \see br_rsa_private
|
||||
*
|
||||
* \param x operand to exponentiate.
|
||||
* \param sk RSA private key.
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
uint32_t br_rsa_i31_private(unsigned char *x,
|
||||
const br_rsa_private_key *sk);
|
||||
|
||||
/**
|
||||
* \brief RSA signature generation engine "i31" (PKCS#1 v1.5 signatures).
|
||||
*
|
||||
* \see br_rsa_pkcs1_sign
|
||||
*
|
||||
* \param hash_oid encoded hash algorithm OID (or `NULL`).
|
||||
* \param hash hash value.
|
||||
* \param hash_len hash value length (in bytes).
|
||||
* \param sk RSA private key.
|
||||
* \param x output buffer for the hash value.
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
uint32_t br_rsa_i31_pkcs1_sign(const unsigned char *hash_oid,
|
||||
const unsigned char *hash, size_t hash_len,
|
||||
const br_rsa_private_key *sk, unsigned char *x);
|
||||
|
||||
/*
|
||||
* RSA "i62" engine. Similar to i31, but internal multiplication use
|
||||
* 64x64->128 multiplications. This is available only on architecture
|
||||
* that offer such an opcode.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \brief RSA public key engine "i62".
|
||||
*
|
||||
* This function is defined only on architecture that offer a 64x64->128
|
||||
* opcode. Use `br_rsa_i62_public_get()` to dynamically obtain a pointer
|
||||
* to that function.
|
||||
*
|
||||
* \see br_rsa_public
|
||||
*
|
||||
* \param x operand to exponentiate.
|
||||
* \param xlen length of the operand (in bytes).
|
||||
* \param pk RSA public key.
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
uint32_t br_rsa_i62_public(unsigned char *x, size_t xlen,
|
||||
const br_rsa_public_key *pk);
|
||||
|
||||
/**
|
||||
* \brief RSA signature verification engine "i62" (PKCS#1 v1.5 signatures).
|
||||
*
|
||||
* This function is defined only on architecture that offer a 64x64->128
|
||||
* opcode. Use `br_rsa_i62_pkcs1_vrfy_get()` to dynamically obtain a pointer
|
||||
* to that function.
|
||||
*
|
||||
* \see br_rsa_pkcs1_vrfy
|
||||
*
|
||||
* \param x signature buffer.
|
||||
* \param xlen signature length (in bytes).
|
||||
* \param hash_oid encoded hash algorithm OID (or `NULL`).
|
||||
* \param hash_len expected hash value length (in bytes).
|
||||
* \param pk RSA public key.
|
||||
* \param hash_out output buffer for the hash value.
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
uint32_t br_rsa_i62_pkcs1_vrfy(const unsigned char *x, size_t xlen,
|
||||
const unsigned char *hash_oid, size_t hash_len,
|
||||
const br_rsa_public_key *pk, unsigned char *hash_out);
|
||||
|
||||
/**
|
||||
* \brief RSA private key engine "i62".
|
||||
*
|
||||
* This function is defined only on architecture that offer a 64x64->128
|
||||
* opcode. Use `br_rsa_i62_private_get()` to dynamically obtain a pointer
|
||||
* to that function.
|
||||
*
|
||||
* \see br_rsa_private
|
||||
*
|
||||
* \param x operand to exponentiate.
|
||||
* \param sk RSA private key.
|
||||
* \return 1 on success, 0 on error.
|
||||
*/
|
||||
uint32_t br_rsa_i62_private(unsigned char *x,
|
||||
const br_rsa_private_key *sk);
|
||||
|
||||
/**
|
||||
* \brief Get the RSA "i62" implementation (public key operations),
|
||||
* if available.
|
||||
*
|
||||
* \return the implementation, or 0.
|
||||
*/
|
||||
br_rsa_public br_rsa_i62_public_get(void);
|
||||
|
||||
/**
|
||||
* \brief Get the RSA "i62" implementation (PKCS#1 v1.5 signature verification),
|
||||
* if available.
|
||||
*
|
||||
* \return the implementation, or 0.
|
||||
*/
|
||||
br_rsa_pkcs1_vrfy br_rsa_i62_pkcs1_vrfy_get(void);
|
||||
|
||||
/**
|
||||
* \brief Get the RSA "i62" implementation (private key operations),
|
||||
* if available.
|
||||
*
|
||||
* \return the implementation, or 0.
|
||||
*/
|
||||
br_rsa_private br_rsa_i62_private_get(void);
|
||||
|
||||
/**
|
||||
* \brief Get "default" RSA implementation (public-key operations).
|
||||
*
|
||||
* This returns the preferred implementation of RSA (public-key operations)
|
||||
* on the current system.
|
||||
*
|
||||
* \return the default implementation.
|
||||
*/
|
||||
br_rsa_public br_rsa_public_get_default(void);
|
||||
|
||||
/**
|
||||
* \brief Get "default" RSA implementation (private-key operations).
|
||||
*
|
||||
* This returns the preferred implementation of RSA (private-key operations)
|
||||
* on the current system.
|
||||
*
|
||||
* \return the default implementation.
|
||||
*/
|
||||
br_rsa_private br_rsa_private_get_default(void);
|
||||
|
||||
/**
|
||||
* \brief Get "default" RSA implementation (PKCS#1 v1.5 signature verification).
|
||||
*
|
||||
* This returns the preferred implementation of RSA (signature verification)
|
||||
* on the current system.
|
||||
*
|
||||
* \return the default implementation.
|
||||
*/
|
||||
br_rsa_pkcs1_vrfy br_rsa_pkcs1_vrfy_get_default(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
3125
third_party/bearssl/inc/bearssl_ssl.h
vendored
Normal file
3125
third_party/bearssl/inc/bearssl_ssl.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1006
third_party/bearssl/inc/bearssl_x509.h
vendored
Normal file
1006
third_party/bearssl/inc/bearssl_x509.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user