Thomas Fossati | eb01024 | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * HKDF implementation -- RFC 5869 |
| 3 | * |
| 4 | * Copyright (C) 2016-2017, ARM Limited, All Rights Reserved |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * |
| 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 20 | */ |
| 21 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 22 | #include "mbedtls/config.h" |
| 23 | #else |
| 24 | #include MBEDTLS_CONFIG_FILE |
| 25 | #endif |
| 26 | |
| 27 | #if defined(MBEDTLS_HKDF_C) |
| 28 | |
| 29 | #include <string.h> |
| 30 | #include "mbedtls/hkdf.h" |
| 31 | #include "mbedtls/platform_util.h" |
| 32 | |
| 33 | /* HKDF-Extract + HKDF-Expand */ |
| 34 | int mbedtls_hkdf( const mbedtls_md_info_t *md, const unsigned char *salt, |
| 35 | size_t salt_len, const unsigned char *ikm, size_t ikm_len, |
| 36 | const unsigned char *info, size_t info_len, |
| 37 | unsigned char *okm, size_t okm_len ) |
| 38 | { |
| 39 | int ret; |
| 40 | unsigned char prk[MBEDTLS_MD_MAX_SIZE]; |
| 41 | |
| 42 | ret = mbedtls_hkdf_extract( md, salt, salt_len, ikm, ikm_len, prk ); |
| 43 | |
| 44 | if ( ret == 0 ) { |
| 45 | ret = mbedtls_hkdf_expand( md, prk, mbedtls_md_get_size(md), info, info_len, okm, okm_len ); |
| 46 | } |
| 47 | |
| 48 | mbedtls_platform_zeroize( prk, sizeof( prk ) ); |
| 49 | |
| 50 | return( ret ); |
| 51 | } |
| 52 | |
| 53 | /* HKDF-Extract(salt, IKM) -> PRK */ |
| 54 | int mbedtls_hkdf_extract( const mbedtls_md_info_t *md, |
| 55 | const unsigned char *salt, size_t salt_len, |
| 56 | const unsigned char *ikm, size_t ikm_len, |
| 57 | unsigned char *prk ) |
| 58 | { |
| 59 | unsigned char null_salt[MBEDTLS_MD_MAX_SIZE] = { '\0' }; |
| 60 | |
| 61 | if ( salt == NULL ) { |
| 62 | size_t hash_len; |
| 63 | |
| 64 | hash_len = mbedtls_md_get_size( md ); |
| 65 | |
| 66 | if ( hash_len == 0 ) { |
| 67 | return MBEDTLS_ERR_HKDF_BAD_PARAM; |
| 68 | } |
| 69 | |
| 70 | salt = null_salt; |
| 71 | salt_len = hash_len; |
| 72 | } |
| 73 | |
| 74 | return( mbedtls_md_hmac( md, salt, salt_len, ikm, ikm_len, prk ) ); |
| 75 | } |
| 76 | |
| 77 | /* HKDF-Expand(PRK, info, L) -> OKM */ |
| 78 | int mbedtls_hkdf_expand( const mbedtls_md_info_t *md, const unsigned char *prk, |
| 79 | size_t prk_len, const unsigned char *info, |
| 80 | size_t info_len, unsigned char *okm, size_t okm_len ) |
| 81 | { |
| 82 | size_t hash_len; |
| 83 | size_t where = 0; |
| 84 | size_t N; |
| 85 | size_t T_len = 0; |
| 86 | size_t i; |
| 87 | int ret = 0; |
| 88 | mbedtls_md_context_t ctx; |
| 89 | unsigned char T[MBEDTLS_MD_MAX_SIZE]; |
| 90 | |
| 91 | if ( okm == NULL ) { |
| 92 | return( MBEDTLS_ERR_HKDF_BAD_PARAM ); |
| 93 | } |
| 94 | |
| 95 | hash_len = mbedtls_md_get_size(md); |
| 96 | |
| 97 | if ( (prk_len < hash_len) || (hash_len == 0) ) { |
| 98 | return( MBEDTLS_ERR_HKDF_BAD_PARAM ); |
| 99 | } |
| 100 | |
| 101 | if ( info == NULL ) { |
| 102 | info = (const unsigned char *) ""; |
| 103 | info_len = 0; |
| 104 | } |
| 105 | |
| 106 | N = okm_len / hash_len; |
| 107 | |
| 108 | if ( (okm_len % hash_len) != 0 ) { |
| 109 | N++; |
| 110 | } |
| 111 | |
| 112 | if ( N > 255 ) { |
| 113 | return( MBEDTLS_ERR_HKDF_BAD_PARAM ); |
| 114 | } |
| 115 | |
| 116 | mbedtls_md_init( &ctx ); |
| 117 | |
| 118 | if ( (ret = mbedtls_md_setup( &ctx, md, 1) ) != 0 ) { |
| 119 | goto exit; |
| 120 | } |
| 121 | |
| 122 | /* Section 2.3. */ |
| 123 | for ( i = 1; i <= N; i++ ) { |
| 124 | unsigned char c = i & 0xff; |
| 125 | |
| 126 | ret = mbedtls_md_hmac_starts( &ctx, prk, prk_len ); |
| 127 | if ( ret != 0 ) { |
| 128 | goto exit; |
| 129 | } |
| 130 | |
| 131 | ret = mbedtls_md_hmac_update( &ctx, T, T_len ); |
| 132 | if ( ret != 0 ) { |
| 133 | goto exit; |
| 134 | } |
| 135 | |
| 136 | ret = mbedtls_md_hmac_update( &ctx, info, info_len ); |
| 137 | if ( ret != 0 ) { |
| 138 | goto exit; |
| 139 | } |
| 140 | |
| 141 | /* The constant concatenated to the end of each T(n) is a single octet. |
| 142 | * */ |
| 143 | ret = mbedtls_md_hmac_update( &ctx, &c, 1 ); |
| 144 | if ( ret != 0 ) { |
| 145 | goto exit; |
| 146 | } |
| 147 | |
| 148 | ret = mbedtls_md_hmac_finish( &ctx, T ); |
| 149 | if ( ret != 0 ) { |
| 150 | goto exit; |
| 151 | } |
| 152 | |
| 153 | memcpy( okm + where, T, (i != N) ? hash_len : (okm_len - where) ); |
| 154 | where += hash_len; |
| 155 | T_len = hash_len; |
| 156 | } |
| 157 | |
| 158 | exit: |
| 159 | mbedtls_md_free( &ctx ); |
| 160 | |
| 161 | return( ret ); |
| 162 | } |
| 163 | |
| 164 | #endif /* MBEDTLS_HKDF_C */ |