Nick Harper | 4685e87 | 2014-12-09 15:51:28 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2014, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #ifndef OPENSSL_HEADER_HKDF_H |
| 16 | #define OPENSSL_HEADER_HKDF_H |
| 17 | |
| 18 | #include <openssl/base.h> |
| 19 | |
David Benjamin | 325664e | 2016-04-21 16:07:49 -0400 | [diff] [blame] | 20 | #if defined(__cplusplus) |
Nick Harper | 4685e87 | 2014-12-09 15:51:28 -0800 | [diff] [blame] | 21 | extern "C" { |
| 22 | #endif |
| 23 | |
| 24 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 25 | // HKDF. |
David Benjamin | 325664e | 2016-04-21 16:07:49 -0400 | [diff] [blame] | 26 | |
| 27 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 28 | // HKDF computes HKDF (as specified by RFC 5869) of initial keying material |
| 29 | // |secret| with |salt| and |info| using |digest|, and outputs |out_len| bytes |
| 30 | // to |out_key|. It returns one on success and zero on error. |
| 31 | // |
| 32 | // HKDF is an Extract-and-Expand algorithm. It does not do any key stretching, |
| 33 | // and as such, is not suited to be used alone to generate a key from a |
| 34 | // password. |
Nick Harper | 4685e87 | 2014-12-09 15:51:28 -0800 | [diff] [blame] | 35 | OPENSSL_EXPORT int HKDF(uint8_t *out_key, size_t out_len, const EVP_MD *digest, |
| 36 | const uint8_t *secret, size_t secret_len, |
| 37 | const uint8_t *salt, size_t salt_len, |
| 38 | const uint8_t *info, size_t info_len); |
| 39 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 40 | // HKDF_extract computes a HKDF PRK (as specified by RFC 5869) from initial |
| 41 | // keying material |secret| and salt |salt| using |digest|, and outputs |
| 42 | // |out_len| bytes to |out_key|. The maximum output size is |EVP_MAX_MD_SIZE|. |
| 43 | // It returns one on success and zero on error. |
Steven Valdez | 3686584 | 2016-05-19 12:26:42 -0400 | [diff] [blame] | 44 | OPENSSL_EXPORT int HKDF_extract(uint8_t *out_key, size_t *out_len, |
| 45 | const EVP_MD *digest, const uint8_t *secret, |
| 46 | size_t secret_len, const uint8_t *salt, |
| 47 | size_t salt_len); |
| 48 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 49 | // HKDF_expand computes a HKDF OKM (as specified by RFC 5869) of length |
| 50 | // |out_len| from the PRK |prk| and info |info| using |digest|, and outputs |
| 51 | // the result to |out_key|. It returns one on success and zero on error. |
Steven Valdez | 3686584 | 2016-05-19 12:26:42 -0400 | [diff] [blame] | 52 | OPENSSL_EXPORT int HKDF_expand(uint8_t *out_key, size_t out_len, |
David Benjamin | edd65fb | 2016-07-16 05:17:31 +0200 | [diff] [blame] | 53 | const EVP_MD *digest, const uint8_t *prk, |
Steven Valdez | 3686584 | 2016-05-19 12:26:42 -0400 | [diff] [blame] | 54 | size_t prk_len, const uint8_t *info, |
| 55 | size_t info_len); |
| 56 | |
Nick Harper | 4685e87 | 2014-12-09 15:51:28 -0800 | [diff] [blame] | 57 | |
| 58 | #if defined(__cplusplus) |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 59 | } // extern C |
Nick Harper | 4685e87 | 2014-12-09 15:51:28 -0800 | [diff] [blame] | 60 | #endif |
| 61 | |
Nick Harper | 4685e87 | 2014-12-09 15:51:28 -0800 | [diff] [blame] | 62 | #define HKDF_R_OUTPUT_TOO_LARGE 100 |
| 63 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 64 | #endif // OPENSSL_HEADER_HKDF_H |