1*0957b409SSimon J. Gerraty /* 2*0957b409SSimon J. Gerraty * Copyright (c) 2018 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_KDF_H__ 26*0957b409SSimon J. Gerraty #define BR_BEARSSL_KDF_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 #include "bearssl_hash.h" 32*0957b409SSimon J. Gerraty #include "bearssl_hmac.h" 33*0957b409SSimon J. Gerraty 34*0957b409SSimon J. Gerraty #ifdef __cplusplus 35*0957b409SSimon J. Gerraty extern "C" { 36*0957b409SSimon J. Gerraty #endif 37*0957b409SSimon J. Gerraty 38*0957b409SSimon J. Gerraty /** \file bearssl_kdf.h 39*0957b409SSimon J. Gerraty * 40*0957b409SSimon J. Gerraty * # Key Derivation Functions 41*0957b409SSimon J. Gerraty * 42*0957b409SSimon J. Gerraty * KDF are functions that takes a variable length input, and provide a 43*0957b409SSimon J. Gerraty * variable length output, meant to be used to derive subkeys from a 44*0957b409SSimon J. Gerraty * master key. 45*0957b409SSimon J. Gerraty * 46*0957b409SSimon J. Gerraty * ## HKDF 47*0957b409SSimon J. Gerraty * 48*0957b409SSimon J. Gerraty * HKDF is a KDF defined by [RFC 5869](https://tools.ietf.org/html/rfc5869). 49*0957b409SSimon J. Gerraty * It is based on HMAC, itself using an underlying hash function. Any 50*0957b409SSimon J. Gerraty * hash function can be used, as long as it is compatible with the rules 51*0957b409SSimon J. Gerraty * for the HMAC implementation (i.e. output size is 64 bytes or less, hash 52*0957b409SSimon J. Gerraty * internal state size is 64 bytes or less, and the internal block length is 53*0957b409SSimon J. Gerraty * a power of 2 between 16 and 256 bytes). HKDF has two phases: 54*0957b409SSimon J. Gerraty * 55*0957b409SSimon J. Gerraty * - HKDF-Extract: the input data in ingested, along with a "salt" value. 56*0957b409SSimon J. Gerraty * 57*0957b409SSimon J. Gerraty * - HKDF-Expand: the output is produced, from the result of processing 58*0957b409SSimon J. Gerraty * the input and salt, and using an extra non-secret parameter called 59*0957b409SSimon J. Gerraty * "info". 60*0957b409SSimon J. Gerraty * 61*0957b409SSimon J. Gerraty * The "salt" and "info" strings are non-secret and can be empty. Their role 62*0957b409SSimon J. Gerraty * is normally to bind the input and output, respectively, to conventional 63*0957b409SSimon J. Gerraty * identifiers that qualifu them within the used protocol or application. 64*0957b409SSimon J. Gerraty * 65*0957b409SSimon J. Gerraty * The implementation defined in this file uses the following functions: 66*0957b409SSimon J. Gerraty * 67*0957b409SSimon J. Gerraty * - `br_hkdf_init()`: initialize an HKDF context, with a hash function, 68*0957b409SSimon J. Gerraty * and the salt. This starts the HKDF-Extract process. 69*0957b409SSimon J. Gerraty * 70*0957b409SSimon J. Gerraty * - `br_hkdf_inject()`: inject more input bytes. This function may be 71*0957b409SSimon J. Gerraty * called repeatedly if the input data is provided by chunks. 72*0957b409SSimon J. Gerraty * 73*0957b409SSimon J. Gerraty * - `br_hkdf_flip()`: end the HKDF-Extract process, and start the 74*0957b409SSimon J. Gerraty * HKDF-Expand process. 75*0957b409SSimon J. Gerraty * 76*0957b409SSimon J. Gerraty * - `br_hkdf_produce()`: get the next bytes of output. This function 77*0957b409SSimon J. Gerraty * may be called several times to obtain the full output by chunks. 78*0957b409SSimon J. Gerraty * For correct HKDF processing, the same "info" string must be 79*0957b409SSimon J. Gerraty * provided for each call. 80*0957b409SSimon J. Gerraty * 81*0957b409SSimon J. Gerraty * Note that the HKDF total output size (the number of bytes that 82*0957b409SSimon J. Gerraty * HKDF-Expand is willing to produce) is limited: if the hash output size 83*0957b409SSimon J. Gerraty * is _n_ bytes, then the maximum output size is _255*n_. 84*0957b409SSimon J. Gerraty * 85*0957b409SSimon J. Gerraty * ## SHAKE 86*0957b409SSimon J. Gerraty * 87*0957b409SSimon J. Gerraty * SHAKE is defined in 88*0957b409SSimon J. Gerraty * [FIPS 202](https://csrc.nist.gov/publications/detail/fips/202/final) 89*0957b409SSimon J. Gerraty * under two versions: SHAKE128 and SHAKE256, offering an alleged 90*0957b409SSimon J. Gerraty * "security level" of 128 and 256 bits, respectively (SHAKE128 is 91*0957b409SSimon J. Gerraty * about 20 to 25% faster than SHAKE256). SHAKE internally relies on 92*0957b409SSimon J. Gerraty * the Keccak family of sponge functions, not on any externally provided 93*0957b409SSimon J. Gerraty * hash function. Contrary to HKDF, SHAKE does not have a concept of 94*0957b409SSimon J. Gerraty * either a "salt" or an "info" string. The API consists in four 95*0957b409SSimon J. Gerraty * functions: 96*0957b409SSimon J. Gerraty * 97*0957b409SSimon J. Gerraty * - `br_shake_init()`: initialize a SHAKE context for a given 98*0957b409SSimon J. Gerraty * security level. 99*0957b409SSimon J. Gerraty * 100*0957b409SSimon J. Gerraty * - `br_shake_inject()`: inject more input bytes. This function may be 101*0957b409SSimon J. Gerraty * called repeatedly if the input data is provided by chunks. 102*0957b409SSimon J. Gerraty * 103*0957b409SSimon J. Gerraty * - `br_shake_flip()`: end the data injection process, and start the 104*0957b409SSimon J. Gerraty * data production process. 105*0957b409SSimon J. Gerraty * 106*0957b409SSimon J. Gerraty * - `br_shake_produce()`: get the next bytes of output. This function 107*0957b409SSimon J. Gerraty * may be called several times to obtain the full output by chunks. 108*0957b409SSimon J. Gerraty */ 109*0957b409SSimon J. Gerraty 110*0957b409SSimon J. Gerraty /** 111*0957b409SSimon J. Gerraty * \brief HKDF context. 112*0957b409SSimon J. Gerraty * 113*0957b409SSimon J. Gerraty * The HKDF context is initialized with a hash function implementation 114*0957b409SSimon J. Gerraty * and a salt value. Contents are opaque (callers should not access them 115*0957b409SSimon J. Gerraty * directly). The caller is responsible for allocating the context where 116*0957b409SSimon J. Gerraty * appropriate. Context initialisation and usage incurs no dynamic 117*0957b409SSimon J. Gerraty * allocation, so there is no release function. 118*0957b409SSimon J. Gerraty */ 119*0957b409SSimon J. Gerraty typedef struct { 120*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 121*0957b409SSimon J. Gerraty union { 122*0957b409SSimon J. Gerraty br_hmac_context hmac_ctx; 123*0957b409SSimon J. Gerraty br_hmac_key_context prk_ctx; 124*0957b409SSimon J. Gerraty } u; 125*0957b409SSimon J. Gerraty unsigned char buf[64]; 126*0957b409SSimon J. Gerraty size_t ptr; 127*0957b409SSimon J. Gerraty size_t dig_len; 128*0957b409SSimon J. Gerraty unsigned chunk_num; 129*0957b409SSimon J. Gerraty #endif 130*0957b409SSimon J. Gerraty } br_hkdf_context; 131*0957b409SSimon J. Gerraty 132*0957b409SSimon J. Gerraty /** 133*0957b409SSimon J. Gerraty * \brief HKDF context initialization. 134*0957b409SSimon J. Gerraty * 135*0957b409SSimon J. Gerraty * The underlying hash function and salt value are provided. Arbitrary 136*0957b409SSimon J. Gerraty * salt lengths can be used. 137*0957b409SSimon J. Gerraty * 138*0957b409SSimon J. Gerraty * HKDF makes a difference between a salt of length zero, and an 139*0957b409SSimon J. Gerraty * absent salt (the latter being equivalent to a salt consisting of 140*0957b409SSimon J. Gerraty * bytes of value zero, of the same length as the hash function output). 141*0957b409SSimon J. Gerraty * If `salt_len` is zero, then this function assumes that the salt is 142*0957b409SSimon J. Gerraty * present but of length zero. To specify an _absent_ salt, use 143*0957b409SSimon J. Gerraty * `BR_HKDF_NO_SALT` as `salt` parameter (`salt_len` is then ignored). 144*0957b409SSimon J. Gerraty * 145*0957b409SSimon J. Gerraty * \param hc HKDF context to initialise. 146*0957b409SSimon J. Gerraty * \param digest_vtable pointer to the hash function implementation vtable. 147*0957b409SSimon J. Gerraty * \param salt HKDF-Extract salt. 148*0957b409SSimon J. Gerraty * \param salt_len HKDF-Extract salt length (in bytes). 149*0957b409SSimon J. Gerraty */ 150*0957b409SSimon J. Gerraty void br_hkdf_init(br_hkdf_context *hc, const br_hash_class *digest_vtable, 151*0957b409SSimon J. Gerraty const void *salt, size_t salt_len); 152*0957b409SSimon J. Gerraty 153*0957b409SSimon J. Gerraty /** 154*0957b409SSimon J. Gerraty * \brief The special "absent salt" value for HKDF. 155*0957b409SSimon J. Gerraty */ 156*0957b409SSimon J. Gerraty #define BR_HKDF_NO_SALT (&br_hkdf_no_salt) 157*0957b409SSimon J. Gerraty 158*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 159*0957b409SSimon J. Gerraty extern const unsigned char br_hkdf_no_salt; 160*0957b409SSimon J. Gerraty #endif 161*0957b409SSimon J. Gerraty 162*0957b409SSimon J. Gerraty /** 163*0957b409SSimon J. Gerraty * \brief HKDF input injection (HKDF-Extract). 164*0957b409SSimon J. Gerraty * 165*0957b409SSimon J. Gerraty * This function injects some more input bytes ("key material") into 166*0957b409SSimon J. Gerraty * HKDF. This function may be called several times, after `br_hkdf_init()` 167*0957b409SSimon J. Gerraty * but before `br_hkdf_flip()`. 168*0957b409SSimon J. Gerraty * 169*0957b409SSimon J. Gerraty * \param hc HKDF context. 170*0957b409SSimon J. Gerraty * \param ikm extra input bytes. 171*0957b409SSimon J. Gerraty * \param ikm_len number of extra input bytes. 172*0957b409SSimon J. Gerraty */ 173*0957b409SSimon J. Gerraty void br_hkdf_inject(br_hkdf_context *hc, const void *ikm, size_t ikm_len); 174*0957b409SSimon J. Gerraty 175*0957b409SSimon J. Gerraty /** 176*0957b409SSimon J. Gerraty * \brief HKDF switch to the HKDF-Expand phase. 177*0957b409SSimon J. Gerraty * 178*0957b409SSimon J. Gerraty * This call terminates the HKDF-Extract process (input injection), and 179*0957b409SSimon J. Gerraty * starts the HKDF-Expand process (output production). 180*0957b409SSimon J. Gerraty * 181*0957b409SSimon J. Gerraty * \param hc HKDF context. 182*0957b409SSimon J. Gerraty */ 183*0957b409SSimon J. Gerraty void br_hkdf_flip(br_hkdf_context *hc); 184*0957b409SSimon J. Gerraty 185*0957b409SSimon J. Gerraty /** 186*0957b409SSimon J. Gerraty * \brief HKDF output production (HKDF-Expand). 187*0957b409SSimon J. Gerraty * 188*0957b409SSimon J. Gerraty * Produce more output bytes from the current state. This function may be 189*0957b409SSimon J. Gerraty * called several times, but only after `br_hkdf_flip()`. 190*0957b409SSimon J. Gerraty * 191*0957b409SSimon J. Gerraty * Returned value is the number of actually produced bytes. The total 192*0957b409SSimon J. Gerraty * output length is limited to 255 times the output length of the 193*0957b409SSimon J. Gerraty * underlying hash function. 194*0957b409SSimon J. Gerraty * 195*0957b409SSimon J. Gerraty * \param hc HKDF context. 196*0957b409SSimon J. Gerraty * \param info application specific information string. 197*0957b409SSimon J. Gerraty * \param info_len application specific information string length (in bytes). 198*0957b409SSimon J. Gerraty * \param out destination buffer for the HKDF output. 199*0957b409SSimon J. Gerraty * \param out_len the length of the requested output (in bytes). 200*0957b409SSimon J. Gerraty * \return the produced output length (in bytes). 201*0957b409SSimon J. Gerraty */ 202*0957b409SSimon J. Gerraty size_t br_hkdf_produce(br_hkdf_context *hc, 203*0957b409SSimon J. Gerraty const void *info, size_t info_len, void *out, size_t out_len); 204*0957b409SSimon J. Gerraty 205*0957b409SSimon J. Gerraty /** 206*0957b409SSimon J. Gerraty * \brief SHAKE context. 207*0957b409SSimon J. Gerraty * 208*0957b409SSimon J. Gerraty * The HKDF context is initialized with a "security level". The internal 209*0957b409SSimon J. Gerraty * notion is called "capacity"; the capacity is twice the security level 210*0957b409SSimon J. Gerraty * (for instance, SHAKE128 has capacity 256). 211*0957b409SSimon J. Gerraty * 212*0957b409SSimon J. Gerraty * The caller is responsible for allocating the context where 213*0957b409SSimon J. Gerraty * appropriate. Context initialisation and usage incurs no dynamic 214*0957b409SSimon J. Gerraty * allocation, so there is no release function. 215*0957b409SSimon J. Gerraty */ 216*0957b409SSimon J. Gerraty typedef struct { 217*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE 218*0957b409SSimon J. Gerraty unsigned char dbuf[200]; 219*0957b409SSimon J. Gerraty size_t dptr; 220*0957b409SSimon J. Gerraty size_t rate; 221*0957b409SSimon J. Gerraty uint64_t A[25]; 222*0957b409SSimon J. Gerraty #endif 223*0957b409SSimon J. Gerraty } br_shake_context; 224*0957b409SSimon J. Gerraty 225*0957b409SSimon J. Gerraty /** 226*0957b409SSimon J. Gerraty * \brief SHAKE context initialization. 227*0957b409SSimon J. Gerraty * 228*0957b409SSimon J. Gerraty * The context is initialized for the provided "security level". 229*0957b409SSimon J. Gerraty * Internally, this sets the "capacity" to twice the security level; 230*0957b409SSimon J. Gerraty * thus, for SHAKE128, the `security_level` parameter should be 128, 231*0957b409SSimon J. Gerraty * which corresponds to a 256-bit capacity. 232*0957b409SSimon J. Gerraty * 233*0957b409SSimon J. Gerraty * Allowed security levels are all multiples of 32, from 32 to 768, 234*0957b409SSimon J. Gerraty * inclusive. Larger security levels imply lower performance; levels 235*0957b409SSimon J. Gerraty * beyond 256 bits don't make much sense. Standard levels are 128 236*0957b409SSimon J. Gerraty * and 256 bits (for SHAKE128 and SHAKE256, respectively). 237*0957b409SSimon J. Gerraty * 238*0957b409SSimon J. Gerraty * \param sc SHAKE context to initialise. 239*0957b409SSimon J. Gerraty * \param security_level security level (in bits). 240*0957b409SSimon J. Gerraty */ 241*0957b409SSimon J. Gerraty void br_shake_init(br_shake_context *sc, int security_level); 242*0957b409SSimon J. Gerraty 243*0957b409SSimon J. Gerraty /** 244*0957b409SSimon J. Gerraty * \brief SHAKE input injection. 245*0957b409SSimon J. Gerraty * 246*0957b409SSimon J. Gerraty * This function injects some more input bytes ("key material") into 247*0957b409SSimon J. Gerraty * SHAKE. This function may be called several times, after `br_shake_init()` 248*0957b409SSimon J. Gerraty * but before `br_shake_flip()`. 249*0957b409SSimon J. Gerraty * 250*0957b409SSimon J. Gerraty * \param sc SHAKE context. 251*0957b409SSimon J. Gerraty * \param data extra input bytes. 252*0957b409SSimon J. Gerraty * \param len number of extra input bytes. 253*0957b409SSimon J. Gerraty */ 254*0957b409SSimon J. Gerraty void br_shake_inject(br_shake_context *sc, const void *data, size_t len); 255*0957b409SSimon J. Gerraty 256*0957b409SSimon J. Gerraty /** 257*0957b409SSimon J. Gerraty * \brief SHAKE switch to production phase. 258*0957b409SSimon J. Gerraty * 259*0957b409SSimon J. Gerraty * This call terminates the input injection process, and starts the 260*0957b409SSimon J. Gerraty * output production process. 261*0957b409SSimon J. Gerraty * 262*0957b409SSimon J. Gerraty * \param sc SHAKE context. 263*0957b409SSimon J. Gerraty */ 264*0957b409SSimon J. Gerraty void br_shake_flip(br_shake_context *hc); 265*0957b409SSimon J. Gerraty 266*0957b409SSimon J. Gerraty /** 267*0957b409SSimon J. Gerraty * \brief SHAKE output production. 268*0957b409SSimon J. Gerraty * 269*0957b409SSimon J. Gerraty * Produce more output bytes from the current state. This function may be 270*0957b409SSimon J. Gerraty * called several times, but only after `br_shake_flip()`. 271*0957b409SSimon J. Gerraty * 272*0957b409SSimon J. Gerraty * There is no practical limit to the number of bytes that may be produced. 273*0957b409SSimon J. Gerraty * 274*0957b409SSimon J. Gerraty * \param sc SHAKE context. 275*0957b409SSimon J. Gerraty * \param out destination buffer for the SHAKE output. 276*0957b409SSimon J. Gerraty * \param len the length of the requested output (in bytes). 277*0957b409SSimon J. Gerraty */ 278*0957b409SSimon J. Gerraty void br_shake_produce(br_shake_context *sc, void *out, size_t len); 279*0957b409SSimon J. Gerraty 280*0957b409SSimon J. Gerraty #ifdef __cplusplus 281*0957b409SSimon J. Gerraty } 282*0957b409SSimon J. Gerraty #endif 283*0957b409SSimon J. Gerraty 284*0957b409SSimon J. Gerraty #endif 285