| 1 |
// Copyright (c) 2022, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_KDF_H |
| 5 |
#define OPENSSL_HEADER_KDF_H |
| 6 |
|
| 7 |
#include <openssl/base.h> |
| 8 |
|
| 9 |
#if defined(__cplusplus) |
| 10 |
extern "C" { |
| 11 |
#endif |
| 12 |
|
| 13 |
|
| 14 |
// Key derivation functions. |
| 15 |
|
| 16 |
|
| 17 |
// CRYPTO_tls1_prf calculates |out_len| bytes of the TLS PRF, using |digest|, |
| 18 |
// and writes them to |out|. It returns one on success and zero on error. |
| 19 |
// TLS 1.2: https://datatracker.ietf.org/doc/html/rfc5246#section-5 |
| 20 |
// TLS 1.{0,1}: https://datatracker.ietf.org/doc/html/rfc4346#section-5 |
| 21 |
OPENSSL_EXPORT int CRYPTO_tls1_prf(const EVP_MD *digest, |
| 22 |
uint8_t *out, size_t out_len, |
| 23 |
const uint8_t *secret, size_t secret_len, |
| 24 |
const char *label, size_t label_len, |
| 25 |
const uint8_t *seed1, size_t seed1_len, |
| 26 |
const uint8_t *seed2, size_t seed2_len); |
| 27 |
|
| 28 |
// SSKDF_digest computes the One-step key derivation using the |
| 29 |
// provided digest algorithm as the backing PRF. This algorithm |
| 30 |
// may be referred to as "Single-Step KDF" or "NIST Concatenation KDF" by other |
| 31 |
// implementors. |info_len| may be zero length. |
| 32 |
// |
| 33 |
// Returns a 1 on success, otherwise returns 0. |
| 34 |
// |
| 35 |
// This implementation adheres to the algorithm specified in Section 4 of the |
| 36 |
// NIST Special Publication 800-56C Revision 2 published on August 2020. The |
| 37 |
// parameters relevant to the specification are as follows: |
| 38 |
// * Auxillary Function H is Option 1 |
| 39 |
// * |out_len|, |secret_len|, and |info_len| are specified in bytes |
| 40 |
// * |out_len|, |secret_len|, |info_len| each must be <= 2^30 |
| 41 |
// * |out_len| and |secret_len| > 0 |
| 42 |
// * |out_len|, |secret_len| are analogous to |L| and |Z| respectively in the |
| 43 |
// specification. |
| 44 |
// * |info| and |info_len| refer to |FixedInfo| in the specification. |
| 45 |
// |
| 46 |
// Specification is available at https://doi.org/10.6028/NIST.SP.800-56Cr2 |
| 47 |
OPENSSL_EXPORT int SSKDF_digest(uint8_t *out_key, size_t out_len, |
| 48 |
const EVP_MD *digest, |
| 49 |
const uint8_t *secret, size_t secret_len, |
| 50 |
const uint8_t *info, size_t info_len); |
| 51 |
|
| 52 |
// SSKDF_hmac computes the One-step key derivation using the |
| 53 |
// provided digest algorithm with HMAC as the backing PRF. This algorithm |
| 54 |
// may be referred to as "Single-Step KDF" or "NIST Concatenation KDF" by other |
| 55 |
// implementors. |salt| is optional and may be |NULL| or zero-length. In |
| 56 |
// addition |info_len| may be zero length. |
| 57 |
// |
| 58 |
// Returns a 1 on success, otherwise returns 0. |
| 59 |
// |
| 60 |
// This implementation adheres to the algorithm specified in Section 4 of the |
| 61 |
// NIST Special Publication 800-56C Revision 2 published on August 2020. The |
| 62 |
// parameters relevant to the specification are as follows: |
| 63 |
// * Auxillary Function H is Option 2 |
| 64 |
// * |out_len|, |secret_len|, |info_len|, and |salt_len| are specified in bytes |
| 65 |
// * |out_len|, |secret_len|, |info_len| each must be <= 2^30 |
| 66 |
// * |out_len| and |secret_len| > 0 |
| 67 |
// * |out_len|, |secret_len| are analogous to |L| and |Z| respectively in the |
| 68 |
// specification. |
| 69 |
// * |info| and |info_len| refer to |FixedInfo| in the specification. |
| 70 |
// * |salt| and |salt_len| refer to |salt| in the specification. |
| 71 |
// * |salt| or |salt_len| being |NULL| or |0| respectively will result in a |
| 72 |
// default salt being used which will be an all-zero byte string whose length |
| 73 |
// is equal to the length of the specified |digest| input block length in |
| 74 |
// bytes. |
| 75 |
OPENSSL_EXPORT int SSKDF_hmac(uint8_t *out_key, size_t out_len, |
| 76 |
const EVP_MD *digest, |
| 77 |
const uint8_t *secret, size_t secret_len, |
| 78 |
const uint8_t *info, size_t info_len, |
| 79 |
const uint8_t *salt, size_t salt_len); |
| 80 |
|
| 81 |
// KBKDF_ctr_hmac derives keying material using the KDF counter mode algorithm, |
| 82 |
// using the provided key derivation key |secret| and fixed info |info|. |
| 83 |
// |info| or |info_len| may be zero-length. This algorithm |
| 84 |
// may be referred to as a "Key-Based Key Derivation Function in Counter Mode". |
| 85 |
// |
| 86 |
// This implementation adheres to the algorithm specified in Section 4.1 of the |
| 87 |
// NIST Special Publication 800-108 Revision 1 Update 1 published on August |
| 88 |
// 2022. The parameters relevant to the specification are as follows: |
| 89 |
// * |out_len|, |secret_len|, and |info_len| are specified in bytes |
| 90 |
// * |out_len| is analogous to |L| in the specification. |
| 91 |
// * |r| is the length of the binary representation of the counter |i| |
| 92 |
// referred to by the specification. |r| is 32 bits in this implementation. |
| 93 |
// * The 32-bit counter is big-endian in this implementation. |
| 94 |
// * The 32-bit counter location is placed before |info|. |
| 95 |
// * |K_IN| is analogous to |secret| and |secret_len|. |
| 96 |
// * |PRF| refers to HMAC in this implementation. |
| 97 |
// |
| 98 |
// Specification is available at https://doi.org/10.6028/NIST.SP.800-108r1-upd1 |
| 99 |
OPENSSL_EXPORT int KBKDF_ctr_hmac(uint8_t *out_key, size_t out_len, |
| 100 |
const EVP_MD *digest, const uint8_t *secret, |
| 101 |
size_t secret_len, const uint8_t *info, |
| 102 |
size_t info_len); |
| 103 |
|
| 104 |
// KDF support for EVP. |
| 105 |
|
| 106 |
|
| 107 |
// HKDF-specific functions. |
| 108 |
// |
| 109 |
// The following functions are provided for OpenSSL compatibility. Prefer the |
| 110 |
// HKDF functions in <openssl/hkdf.h>. In each, |ctx| must be created with |
| 111 |
// |EVP_PKEY_CTX_new_id| with |EVP_PKEY_HKDF| and then initialized with |
| 112 |
// |EVP_PKEY_derive_init|. |
| 113 |
|
| 114 |
// EVP_PKEY_HKDEF_MODE_* define "modes" for use with |EVP_PKEY_CTX_hkdf_mode|. |
| 115 |
// The mispelling of "HKDF" as "HKDEF" is intentional for OpenSSL compatibility. |
| 116 |
#define EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND 0 |
| 117 |
#define EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY 1 |
| 118 |
#define EVP_PKEY_HKDEF_MODE_EXPAND_ONLY 2 |
| 119 |
|
| 120 |
// EVP_PKEY_CTX_hkdf_mode configures which HKDF operation to run. It returns one |
| 121 |
// on success and zero on error. |mode| must be one of |EVP_PKEY_HKDEF_MODE_*|. |
| 122 |
// By default, the mode is |EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND|. |
| 123 |
// |
| 124 |
// If |mode| is |EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND| or |
| 125 |
// |EVP_PKEY_HKDEF_MODE_EXPAND_ONLY|, the output is variable-length. |
| 126 |
// |EVP_PKEY_derive| uses the size of the output buffer as the output length for |
| 127 |
// HKDF-Expand. |
| 128 |
// |
| 129 |
// WARNING: Although this API calls it a "mode", HKDF-Extract and HKDF-Expand |
| 130 |
// are distinct operations with distinct inputs and distinct kinds of keys. |
| 131 |
// Callers should not pass input secrets for one operation into the other. |
| 132 |
OPENSSL_EXPORT int EVP_PKEY_CTX_hkdf_mode(EVP_PKEY_CTX *ctx, int mode); |
| 133 |
|
| 134 |
// EVP_PKEY_CTX_set_hkdf_md sets |md| as the digest to use with HKDF. It returns |
| 135 |
// one on success and zero on error. |
| 136 |
OPENSSL_EXPORT int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, |
| 137 |
const EVP_MD *md); |
| 138 |
|
| 139 |
// EVP_PKEY_CTX_set1_hkdf_key configures HKDF to use |key_len| bytes from |key| |
| 140 |
// as the "key", described below. It returns one on success and zero on error. |
| 141 |
// |
| 142 |
// Which input is the key depends on the "mode" (see |EVP_PKEY_CTX_hkdf_mode|). |
| 143 |
// If |EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND| or |
| 144 |
// |EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY|, this function specifies the input keying |
| 145 |
// material (IKM) for HKDF-Extract. If |EVP_PKEY_HKDEF_MODE_EXPAND_ONLY|, it |
| 146 |
// instead specifies the pseudorandom key (PRK) for HKDF-Expand. |
| 147 |
OPENSSL_EXPORT int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx, |
| 148 |
const uint8_t *key, |
| 149 |
size_t key_len); |
| 150 |
|
| 151 |
// EVP_PKEY_CTX_set1_hkdf_salt configures HKDF to use |salt_len| bytes from |
| 152 |
// |salt| as the salt parameter to HKDF-Extract. It returns one on success and |
| 153 |
// zero on error. If performing HKDF-Expand only, this parameter is ignored. |
| 154 |
OPENSSL_EXPORT int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx, |
| 155 |
const uint8_t *salt, |
| 156 |
size_t salt_len); |
| 157 |
|
| 158 |
// EVP_PKEY_CTX_add1_hkdf_info appends |info_len| bytes from |info| to the info |
| 159 |
// parameter used with HKDF-Expand. It returns one on success and zero on error. |
| 160 |
// If performing HKDF-Extract only, this parameter is ignored. |
| 161 |
OPENSSL_EXPORT int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx, |
| 162 |
const uint8_t *info, |
| 163 |
size_t info_len); |
| 164 |
|
| 165 |
|
| 166 |
#if defined(__cplusplus) |
| 167 |
} // extern C |
| 168 |
#endif |
| 169 |
|
| 170 |
#endif // OPENSSL_HEADER_KDF_H |