xref: /freebsd/contrib/bearssl/inc/bearssl.h (revision 0957b409a90fd597c1e9124cbaf3edd2b488f4ac)
1*0957b409SSimon J. Gerraty /*
2*0957b409SSimon J. Gerraty  * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
3*0957b409SSimon J. Gerraty  *
4*0957b409SSimon J. Gerraty  * Permission is hereby granted, free of charge, to any person obtaining
5*0957b409SSimon J. Gerraty  * a copy of this software and associated documentation files (the
6*0957b409SSimon J. Gerraty  * "Software"), to deal in the Software without restriction, including
7*0957b409SSimon J. Gerraty  * without limitation the rights to use, copy, modify, merge, publish,
8*0957b409SSimon J. Gerraty  * distribute, sublicense, and/or sell copies of the Software, and to
9*0957b409SSimon J. Gerraty  * permit persons to whom the Software is furnished to do so, subject to
10*0957b409SSimon J. Gerraty  * the following conditions:
11*0957b409SSimon J. Gerraty  *
12*0957b409SSimon J. Gerraty  * The above copyright notice and this permission notice shall be
13*0957b409SSimon J. Gerraty  * included in all copies or substantial portions of the Software.
14*0957b409SSimon J. Gerraty  *
15*0957b409SSimon J. Gerraty  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*0957b409SSimon J. Gerraty  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*0957b409SSimon J. Gerraty  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*0957b409SSimon J. Gerraty  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*0957b409SSimon J. Gerraty  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*0957b409SSimon J. Gerraty  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*0957b409SSimon J. Gerraty  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*0957b409SSimon J. Gerraty  * SOFTWARE.
23*0957b409SSimon J. Gerraty  */
24*0957b409SSimon J. Gerraty 
25*0957b409SSimon J. Gerraty #ifndef BR_BEARSSL_H__
26*0957b409SSimon J. Gerraty #define BR_BEARSSL_H__
27*0957b409SSimon J. Gerraty 
28*0957b409SSimon J. Gerraty #include <stddef.h>
29*0957b409SSimon J. Gerraty #include <stdint.h>
30*0957b409SSimon J. Gerraty 
31*0957b409SSimon J. Gerraty /** \mainpage BearSSL API
32*0957b409SSimon J. Gerraty  *
33*0957b409SSimon J. Gerraty  * # API Layout
34*0957b409SSimon J. Gerraty  *
35*0957b409SSimon J. Gerraty  * The functions and structures defined by the BearSSL API are located
36*0957b409SSimon J. Gerraty  * in various header files:
37*0957b409SSimon J. Gerraty  *
38*0957b409SSimon J. Gerraty  * | Header file     | Elements                                          |
39*0957b409SSimon J. Gerraty  * | :-------------- | :------------------------------------------------ |
40*0957b409SSimon J. Gerraty  * | bearssl_hash.h  | Hash functions                                    |
41*0957b409SSimon J. Gerraty  * | bearssl_hmac.h  | HMAC                                              |
42*0957b409SSimon J. Gerraty  * | bearssl_kdf.h   | Key Derivation Functions                          |
43*0957b409SSimon J. Gerraty  * | bearssl_rand.h  | Pseudorandom byte generators                      |
44*0957b409SSimon J. Gerraty  * | bearssl_prf.h   | PRF implementations (for SSL/TLS)                 |
45*0957b409SSimon J. Gerraty  * | bearssl_block.h | Symmetric encryption                              |
46*0957b409SSimon J. Gerraty  * | bearssl_aead.h  | AEAD algorithms (combined encryption + MAC)       |
47*0957b409SSimon J. Gerraty  * | bearssl_rsa.h   | RSA encryption and signatures                     |
48*0957b409SSimon J. Gerraty  * | bearssl_ec.h    | Elliptic curves support (including ECDSA)         |
49*0957b409SSimon J. Gerraty  * | bearssl_ssl.h   | SSL/TLS engine interface                          |
50*0957b409SSimon J. Gerraty  * | bearssl_x509.h  | X.509 certificate decoding and validation         |
51*0957b409SSimon J. Gerraty  * | bearssl_pem.h   | Base64/PEM decoding support functions             |
52*0957b409SSimon J. Gerraty  *
53*0957b409SSimon J. Gerraty  * Applications using BearSSL are supposed to simply include `bearssl.h`
54*0957b409SSimon J. Gerraty  * as follows:
55*0957b409SSimon J. Gerraty  *
56*0957b409SSimon J. Gerraty  *     #include <bearssl.h>
57*0957b409SSimon J. Gerraty  *
58*0957b409SSimon J. Gerraty  * The `bearssl.h` file itself includes all the other header files. It is
59*0957b409SSimon J. Gerraty  * possible to include specific header files, but it has no practical
60*0957b409SSimon J. Gerraty  * advantage for the application. The API is separated into separate
61*0957b409SSimon J. Gerraty  * header files only for documentation convenience.
62*0957b409SSimon J. Gerraty  *
63*0957b409SSimon J. Gerraty  *
64*0957b409SSimon J. Gerraty  * # Conventions
65*0957b409SSimon J. Gerraty  *
66*0957b409SSimon J. Gerraty  * ## MUST and SHALL
67*0957b409SSimon J. Gerraty  *
68*0957b409SSimon J. Gerraty  * In all descriptions, the usual "MUST", "SHALL", "MAY",... terminology
69*0957b409SSimon J. Gerraty  * is used. Failure to meet requirements expressed with a "MUST" or
70*0957b409SSimon J. Gerraty  * "SHALL" implies undefined behaviour, which means that segmentation
71*0957b409SSimon J. Gerraty  * faults, buffer overflows, and other similar adverse events, may occur.
72*0957b409SSimon J. Gerraty  *
73*0957b409SSimon J. Gerraty  * In general, BearSSL is not very forgiving of programming errors, and
74*0957b409SSimon J. Gerraty  * does not include much failsafes or error reporting when the problem
75*0957b409SSimon J. Gerraty  * does not arise from external transient conditions, and can be fixed
76*0957b409SSimon J. Gerraty  * only in the application code. This is done so in order to make the
77*0957b409SSimon J. Gerraty  * total code footprint lighter.
78*0957b409SSimon J. Gerraty  *
79*0957b409SSimon J. Gerraty  *
80*0957b409SSimon J. Gerraty  * ## `NULL` values
81*0957b409SSimon J. Gerraty  *
82*0957b409SSimon J. Gerraty  * Function parameters with a pointer type shall not be `NULL` unless
83*0957b409SSimon J. Gerraty  * explicitly authorised by the documentation. As an exception, when
84*0957b409SSimon J. Gerraty  * the pointer aims at a sequence of bytes and is accompanied with
85*0957b409SSimon J. Gerraty  * a length parameter, and the length is zero (meaning that there is
86*0957b409SSimon J. Gerraty  * no byte at all to retrieve), then the pointer may be `NULL` even if
87*0957b409SSimon J. Gerraty  * not explicitly allowed.
88*0957b409SSimon J. Gerraty  *
89*0957b409SSimon J. Gerraty  *
90*0957b409SSimon J. Gerraty  * ## Memory Allocation
91*0957b409SSimon J. Gerraty  *
92*0957b409SSimon J. Gerraty  * BearSSL does not perform dynamic memory allocation. This implies that
93*0957b409SSimon J. Gerraty  * for any functionality that requires a non-transient state, the caller
94*0957b409SSimon J. Gerraty  * is responsible for allocating the relevant context structure. Such
95*0957b409SSimon J. Gerraty  * allocation can be done in any appropriate area, including static data
96*0957b409SSimon J. Gerraty  * segments, the heap, and the stack, provided that proper alignment is
97*0957b409SSimon J. Gerraty  * respected. The header files define these context structures
98*0957b409SSimon J. Gerraty  * (including size and contents), so the C compiler should handle
99*0957b409SSimon J. Gerraty  * alignment automatically.
100*0957b409SSimon J. Gerraty  *
101*0957b409SSimon J. Gerraty  * Since there is no dynamic resource allocation, there is also nothing to
102*0957b409SSimon J. Gerraty  * release. When the calling code is done with a BearSSL feature, it
103*0957b409SSimon J. Gerraty  * may simple release the context structures it allocated itself, with
104*0957b409SSimon J. Gerraty  * no "close function" to call. If the context structures were allocated
105*0957b409SSimon J. Gerraty  * on the stack (as local variables), then even that release operation is
106*0957b409SSimon J. Gerraty  * implicit.
107*0957b409SSimon J. Gerraty  *
108*0957b409SSimon J. Gerraty  *
109*0957b409SSimon J. Gerraty  * ## Structure Contents
110*0957b409SSimon J. Gerraty  *
111*0957b409SSimon J. Gerraty  * Except when explicitly indicated, structure contents are opaque: they
112*0957b409SSimon J. Gerraty  * are included in the header files so that calling code may know the
113*0957b409SSimon J. Gerraty  * structure sizes and alignment requirements, but callers SHALL NOT
114*0957b409SSimon J. Gerraty  * access individual fields directly. For fields that are supposed to
115*0957b409SSimon J. Gerraty  * be read from or written to, the API defines accessor functions (the
116*0957b409SSimon J. Gerraty  * simplest of these accessor functions are defined as `static inline`
117*0957b409SSimon J. Gerraty  * functions, and the C compiler will optimise them away).
118*0957b409SSimon J. Gerraty  *
119*0957b409SSimon J. Gerraty  *
120*0957b409SSimon J. Gerraty  * # API Usage
121*0957b409SSimon J. Gerraty  *
122*0957b409SSimon J. Gerraty  * BearSSL usage for running a SSL/TLS client or server is described
123*0957b409SSimon J. Gerraty  * on the [BearSSL Web site](https://www.bearssl.org/api1.html). The
124*0957b409SSimon J. Gerraty  * BearSSL source archive also comes with sample code.
125*0957b409SSimon J. Gerraty  */
126*0957b409SSimon J. Gerraty 
127*0957b409SSimon J. Gerraty #include "bearssl_hash.h"
128*0957b409SSimon J. Gerraty #include "bearssl_hmac.h"
129*0957b409SSimon J. Gerraty #include "bearssl_kdf.h"
130*0957b409SSimon J. Gerraty #include "bearssl_rand.h"
131*0957b409SSimon J. Gerraty #include "bearssl_prf.h"
132*0957b409SSimon J. Gerraty #include "bearssl_block.h"
133*0957b409SSimon J. Gerraty #include "bearssl_aead.h"
134*0957b409SSimon J. Gerraty #include "bearssl_rsa.h"
135*0957b409SSimon J. Gerraty #include "bearssl_ec.h"
136*0957b409SSimon J. Gerraty #include "bearssl_ssl.h"
137*0957b409SSimon J. Gerraty #include "bearssl_x509.h"
138*0957b409SSimon J. Gerraty #include "bearssl_pem.h"
139*0957b409SSimon J. Gerraty 
140*0957b409SSimon J. Gerraty /** \brief Type for a configuration option.
141*0957b409SSimon J. Gerraty  *
142*0957b409SSimon J. Gerraty  * A "configuration option" is a value that is selected when the BearSSL
143*0957b409SSimon J. Gerraty  * library itself is compiled. Most options are boolean; their value is
144*0957b409SSimon J. Gerraty  * then either 1 (option is enabled) or 0 (option is disabled). Some
145*0957b409SSimon J. Gerraty  * values have other integer values. Option names correspond to macro
146*0957b409SSimon J. Gerraty  * names. Some of the options can be explicitly set in the internal
147*0957b409SSimon J. Gerraty  * `"config.h"` file.
148*0957b409SSimon J. Gerraty  */
149*0957b409SSimon J. Gerraty typedef struct {
150*0957b409SSimon J. Gerraty 	/** \brief Configurable option name. */
151*0957b409SSimon J. Gerraty 	const char *name;
152*0957b409SSimon J. Gerraty 	/** \brief Configurable option value. */
153*0957b409SSimon J. Gerraty 	long value;
154*0957b409SSimon J. Gerraty } br_config_option;
155*0957b409SSimon J. Gerraty 
156*0957b409SSimon J. Gerraty /** \brief Get configuration report.
157*0957b409SSimon J. Gerraty  *
158*0957b409SSimon J. Gerraty  * This function returns compiled configuration options, each as a
159*0957b409SSimon J. Gerraty  * 'long' value. Names match internal macro names, in particular those
160*0957b409SSimon J. Gerraty  * that can be set in the `"config.h"` inner file. For boolean options,
161*0957b409SSimon J. Gerraty  * the numerical value is 1 if enabled, 0 if disabled. For maximum
162*0957b409SSimon J. Gerraty  * key sizes, values are expressed in bits.
163*0957b409SSimon J. Gerraty  *
164*0957b409SSimon J. Gerraty  * The returned array is terminated by an entry whose `name` is `NULL`.
165*0957b409SSimon J. Gerraty  *
166*0957b409SSimon J. Gerraty  * \return  the configuration report.
167*0957b409SSimon J. Gerraty  */
168*0957b409SSimon J. Gerraty const br_config_option *br_get_config(void);
169*0957b409SSimon J. Gerraty 
170*0957b409SSimon J. Gerraty #endif
171