Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * HKDF implementation -- RFC 5869 |
| 3 | * |
| 4 | * Copyright (C) 2016-2018, 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 | int mbedtls_hkdf( const mbedtls_md_info_t *md, const unsigned char *salt, |
| 34 | size_t salt_len, const unsigned char *ikm, size_t ikm_len, |
| 35 | const unsigned char *info, size_t info_len, |
| 36 | unsigned char *okm, size_t okm_len ) |
| 37 | { |
| 38 | int ret; |
| 39 | unsigned char prk[MBEDTLS_MD_MAX_SIZE]; |
| 40 | |
| 41 | ret = mbedtls_hkdf_extract( md, salt, salt_len, ikm, ikm_len, prk ); |
| 42 | |
| 43 | if( ret == 0 ) |
| 44 | { |
| 45 | ret = mbedtls_hkdf_expand( md, prk, mbedtls_md_get_size( md ), |
| 46 | info, info_len, okm, okm_len ); |
| 47 | } |
| 48 | |
| 49 | mbedtls_platform_zeroize( prk, sizeof( prk ) ); |
| 50 | |
| 51 | return( ret ); |
| 52 | } |
| 53 | |
| 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 | { |
| 63 | size_t hash_len; |
| 64 | |
| 65 | hash_len = mbedtls_md_get_size( md ); |
| 66 | |
| 67 | if( hash_len == 0 ) |
| 68 | { |
| 69 | return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA; |
| 70 | } |
| 71 | |
| 72 | salt = null_salt; |
| 73 | salt_len = hash_len; |
| 74 | } |
| 75 | |
| 76 | return( mbedtls_md_hmac( md, salt, salt_len, ikm, ikm_len, prk ) ); |
| 77 | } |
| 78 | |
| 79 | int mbedtls_hkdf_expand( const mbedtls_md_info_t *md, const unsigned char *prk, |
| 80 | size_t prk_len, const unsigned char *info, |
| 81 | size_t info_len, unsigned char *okm, size_t okm_len ) |
| 82 | { |
| 83 | size_t hash_len; |
| 84 | size_t where = 0; |
| 85 | size_t n; |
| 86 | size_t t_len = 0; |
| 87 | size_t i; |
| 88 | int ret = 0; |
| 89 | mbedtls_md_context_t ctx; |
| 90 | unsigned char t[MBEDTLS_MD_MAX_SIZE]; |
| 91 | |
| 92 | if( okm == NULL ) |
| 93 | { |
| 94 | return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA ); |
| 95 | } |
| 96 | |
| 97 | hash_len = mbedtls_md_get_size( md ); |
| 98 | |
| 99 | if( prk_len < hash_len || hash_len == 0 ) |
| 100 | { |
| 101 | return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA ); |
| 102 | } |
| 103 | |
| 104 | if( info == NULL ) |
| 105 | { |
| 106 | info = (const unsigned char *) ""; |
| 107 | info_len = 0; |
| 108 | } |
| 109 | |
| 110 | n = okm_len / hash_len; |
| 111 | |
| 112 | if( (okm_len % hash_len) != 0 ) |
| 113 | { |
| 114 | n++; |
| 115 | } |
| 116 | |
| 117 | if( n > 255 ) |
| 118 | { |
| 119 | return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA ); |
| 120 | } |
| 121 | |
| 122 | mbedtls_md_init( &ctx ); |
| 123 | |
| 124 | if( (ret = mbedtls_md_setup( &ctx, md, 1) ) != 0 ) |
| 125 | { |
| 126 | goto exit; |
| 127 | } |
| 128 | |
| 129 | /* RFC 5869 Section 2.3. */ |
| 130 | for( i = 1; i <= n; i++ ) |
| 131 | { |
| 132 | size_t num_to_copy; |
| 133 | unsigned char c = i & 0xff; |
| 134 | |
| 135 | ret = mbedtls_md_hmac_starts( &ctx, prk, prk_len ); |
| 136 | if( ret != 0 ) |
| 137 | { |
| 138 | goto exit; |
| 139 | } |
| 140 | |
| 141 | ret = mbedtls_md_hmac_update( &ctx, t, t_len ); |
| 142 | if( ret != 0 ) |
| 143 | { |
| 144 | goto exit; |
| 145 | } |
| 146 | |
| 147 | ret = mbedtls_md_hmac_update( &ctx, info, info_len ); |
| 148 | if( ret != 0 ) |
| 149 | { |
| 150 | goto exit; |
| 151 | } |
| 152 | |
| 153 | /* The constant concatenated to the end of each t(n) is a single octet. |
| 154 | * */ |
| 155 | ret = mbedtls_md_hmac_update( &ctx, &c, 1 ); |
| 156 | if( ret != 0 ) |
| 157 | { |
| 158 | goto exit; |
| 159 | } |
| 160 | |
| 161 | ret = mbedtls_md_hmac_finish( &ctx, t ); |
| 162 | if( ret != 0 ) |
| 163 | { |
| 164 | goto exit; |
| 165 | } |
| 166 | |
| 167 | num_to_copy = i != n ? hash_len : okm_len - where; |
| 168 | memcpy( okm + where, t, num_to_copy ); |
| 169 | where += hash_len; |
| 170 | t_len = hash_len; |
| 171 | } |
| 172 | |
| 173 | exit: |
| 174 | mbedtls_md_free( &ctx ); |
| 175 | mbedtls_platform_zeroize( t, sizeof( t ) ); |
| 176 | |
| 177 | return( ret ); |
| 178 | } |
| 179 | |
| 180 | #endif /* MBEDTLS_HKDF_C */ |