Modified mbed TLS headers for AES functionality only to reduce build size
Dependents: BLE_Gateway_Linker_fix BLE_Gateway
Fork of mbedtls by
source/ctr_drbg.c@5:f09f5ed830ca, 2017-07-10 (annotated)
- Committer:
- electronichamsters
- Date:
- Mon Jul 10 04:00:25 2017 +0000
- Revision:
- 5:f09f5ed830ca
- Parent:
- 1:24750b9ad5ef
working gateway
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Christopher Haster |
1:24750b9ad5ef | 1 | /* |
Christopher Haster |
1:24750b9ad5ef | 2 | * CTR_DRBG implementation based on AES-256 (NIST SP 800-90) |
Christopher Haster |
1:24750b9ad5ef | 3 | * |
Christopher Haster |
1:24750b9ad5ef | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Christopher Haster |
1:24750b9ad5ef | 5 | * SPDX-License-Identifier: Apache-2.0 |
Christopher Haster |
1:24750b9ad5ef | 6 | * |
Christopher Haster |
1:24750b9ad5ef | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
Christopher Haster |
1:24750b9ad5ef | 8 | * not use this file except in compliance with the License. |
Christopher Haster |
1:24750b9ad5ef | 9 | * You may obtain a copy of the License at |
Christopher Haster |
1:24750b9ad5ef | 10 | * |
Christopher Haster |
1:24750b9ad5ef | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
Christopher Haster |
1:24750b9ad5ef | 12 | * |
Christopher Haster |
1:24750b9ad5ef | 13 | * Unless required by applicable law or agreed to in writing, software |
Christopher Haster |
1:24750b9ad5ef | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
Christopher Haster |
1:24750b9ad5ef | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Christopher Haster |
1:24750b9ad5ef | 16 | * See the License for the specific language governing permissions and |
Christopher Haster |
1:24750b9ad5ef | 17 | * limitations under the License. |
Christopher Haster |
1:24750b9ad5ef | 18 | * |
Christopher Haster |
1:24750b9ad5ef | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Christopher Haster |
1:24750b9ad5ef | 20 | */ |
Christopher Haster |
1:24750b9ad5ef | 21 | /* |
Christopher Haster |
1:24750b9ad5ef | 22 | * The NIST SP 800-90 DRBGs are described in the following publucation. |
Christopher Haster |
1:24750b9ad5ef | 23 | * |
Christopher Haster |
1:24750b9ad5ef | 24 | * http://csrc.nist.gov/publications/nistpubs/800-90/SP800-90revised_March2007.pdf |
Christopher Haster |
1:24750b9ad5ef | 25 | */ |
Christopher Haster |
1:24750b9ad5ef | 26 | |
Christopher Haster |
1:24750b9ad5ef | 27 | #if !defined(MBEDTLS_CONFIG_FILE) |
Christopher Haster |
1:24750b9ad5ef | 28 | #include "mbedtls/config.h" |
Christopher Haster |
1:24750b9ad5ef | 29 | #else |
Christopher Haster |
1:24750b9ad5ef | 30 | #include MBEDTLS_CONFIG_FILE |
Christopher Haster |
1:24750b9ad5ef | 31 | #endif |
Christopher Haster |
1:24750b9ad5ef | 32 | |
Christopher Haster |
1:24750b9ad5ef | 33 | #if defined(MBEDTLS_CTR_DRBG_C) |
Christopher Haster |
1:24750b9ad5ef | 34 | |
Christopher Haster |
1:24750b9ad5ef | 35 | #include "mbedtls/ctr_drbg.h" |
Christopher Haster |
1:24750b9ad5ef | 36 | |
Christopher Haster |
1:24750b9ad5ef | 37 | #include <string.h> |
Christopher Haster |
1:24750b9ad5ef | 38 | |
Christopher Haster |
1:24750b9ad5ef | 39 | #if defined(MBEDTLS_FS_IO) |
Christopher Haster |
1:24750b9ad5ef | 40 | #include <stdio.h> |
Christopher Haster |
1:24750b9ad5ef | 41 | #endif |
Christopher Haster |
1:24750b9ad5ef | 42 | |
Christopher Haster |
1:24750b9ad5ef | 43 | #if defined(MBEDTLS_SELF_TEST) |
Christopher Haster |
1:24750b9ad5ef | 44 | #if defined(MBEDTLS_PLATFORM_C) |
Christopher Haster |
1:24750b9ad5ef | 45 | #include "mbedtls/platform.h" |
Christopher Haster |
1:24750b9ad5ef | 46 | #else |
Christopher Haster |
1:24750b9ad5ef | 47 | #include <stdio.h> |
Christopher Haster |
1:24750b9ad5ef | 48 | #define mbedtls_printf printf |
Christopher Haster |
1:24750b9ad5ef | 49 | #endif /* MBEDTLS_PLATFORM_C */ |
Christopher Haster |
1:24750b9ad5ef | 50 | #endif /* MBEDTLS_SELF_TEST */ |
Christopher Haster |
1:24750b9ad5ef | 51 | |
Christopher Haster |
1:24750b9ad5ef | 52 | /* Implementation that should never be optimized out by the compiler */ |
Christopher Haster |
1:24750b9ad5ef | 53 | static void mbedtls_zeroize( void *v, size_t n ) { |
Christopher Haster |
1:24750b9ad5ef | 54 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
Christopher Haster |
1:24750b9ad5ef | 55 | } |
Christopher Haster |
1:24750b9ad5ef | 56 | |
Christopher Haster |
1:24750b9ad5ef | 57 | /* |
Christopher Haster |
1:24750b9ad5ef | 58 | * CTR_DRBG context initialization |
Christopher Haster |
1:24750b9ad5ef | 59 | */ |
Christopher Haster |
1:24750b9ad5ef | 60 | void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx ) |
Christopher Haster |
1:24750b9ad5ef | 61 | { |
Christopher Haster |
1:24750b9ad5ef | 62 | memset( ctx, 0, sizeof( mbedtls_ctr_drbg_context ) ); |
Christopher Haster |
1:24750b9ad5ef | 63 | |
Christopher Haster |
1:24750b9ad5ef | 64 | #if defined(MBEDTLS_THREADING_C) |
Christopher Haster |
1:24750b9ad5ef | 65 | mbedtls_mutex_init( &ctx->mutex ); |
Christopher Haster |
1:24750b9ad5ef | 66 | #endif |
Christopher Haster |
1:24750b9ad5ef | 67 | } |
Christopher Haster |
1:24750b9ad5ef | 68 | |
Christopher Haster |
1:24750b9ad5ef | 69 | /* |
Christopher Haster |
1:24750b9ad5ef | 70 | * Non-public function wrapped by ctr_crbg_init(). Necessary to allow NIST |
Christopher Haster |
1:24750b9ad5ef | 71 | * tests to succeed (which require known length fixed entropy) |
Christopher Haster |
1:24750b9ad5ef | 72 | */ |
Christopher Haster |
1:24750b9ad5ef | 73 | int mbedtls_ctr_drbg_seed_entropy_len( |
Christopher Haster |
1:24750b9ad5ef | 74 | mbedtls_ctr_drbg_context *ctx, |
Christopher Haster |
1:24750b9ad5ef | 75 | int (*f_entropy)(void *, unsigned char *, size_t), |
Christopher Haster |
1:24750b9ad5ef | 76 | void *p_entropy, |
Christopher Haster |
1:24750b9ad5ef | 77 | const unsigned char *custom, |
Christopher Haster |
1:24750b9ad5ef | 78 | size_t len, |
Christopher Haster |
1:24750b9ad5ef | 79 | size_t entropy_len ) |
Christopher Haster |
1:24750b9ad5ef | 80 | { |
Christopher Haster |
1:24750b9ad5ef | 81 | int ret; |
Christopher Haster |
1:24750b9ad5ef | 82 | unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE]; |
Christopher Haster |
1:24750b9ad5ef | 83 | |
Christopher Haster |
1:24750b9ad5ef | 84 | memset( key, 0, MBEDTLS_CTR_DRBG_KEYSIZE ); |
Christopher Haster |
1:24750b9ad5ef | 85 | |
Christopher Haster |
1:24750b9ad5ef | 86 | mbedtls_aes_init( &ctx->aes_ctx ); |
Christopher Haster |
1:24750b9ad5ef | 87 | |
Christopher Haster |
1:24750b9ad5ef | 88 | ctx->f_entropy = f_entropy; |
Christopher Haster |
1:24750b9ad5ef | 89 | ctx->p_entropy = p_entropy; |
Christopher Haster |
1:24750b9ad5ef | 90 | |
Christopher Haster |
1:24750b9ad5ef | 91 | ctx->entropy_len = entropy_len; |
Christopher Haster |
1:24750b9ad5ef | 92 | ctx->reseed_interval = MBEDTLS_CTR_DRBG_RESEED_INTERVAL; |
Christopher Haster |
1:24750b9ad5ef | 93 | |
Christopher Haster |
1:24750b9ad5ef | 94 | /* |
Christopher Haster |
1:24750b9ad5ef | 95 | * Initialize with an empty key |
Christopher Haster |
1:24750b9ad5ef | 96 | */ |
Christopher Haster |
1:24750b9ad5ef | 97 | mbedtls_aes_setkey_enc( &ctx->aes_ctx, key, MBEDTLS_CTR_DRBG_KEYBITS ); |
Christopher Haster |
1:24750b9ad5ef | 98 | |
Christopher Haster |
1:24750b9ad5ef | 99 | if( ( ret = mbedtls_ctr_drbg_reseed( ctx, custom, len ) ) != 0 ) |
Christopher Haster |
1:24750b9ad5ef | 100 | return( ret ); |
Christopher Haster |
1:24750b9ad5ef | 101 | |
Christopher Haster |
1:24750b9ad5ef | 102 | return( 0 ); |
Christopher Haster |
1:24750b9ad5ef | 103 | } |
Christopher Haster |
1:24750b9ad5ef | 104 | |
Christopher Haster |
1:24750b9ad5ef | 105 | int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx, |
Christopher Haster |
1:24750b9ad5ef | 106 | int (*f_entropy)(void *, unsigned char *, size_t), |
Christopher Haster |
1:24750b9ad5ef | 107 | void *p_entropy, |
Christopher Haster |
1:24750b9ad5ef | 108 | const unsigned char *custom, |
Christopher Haster |
1:24750b9ad5ef | 109 | size_t len ) |
Christopher Haster |
1:24750b9ad5ef | 110 | { |
Christopher Haster |
1:24750b9ad5ef | 111 | return( mbedtls_ctr_drbg_seed_entropy_len( ctx, f_entropy, p_entropy, custom, len, |
Christopher Haster |
1:24750b9ad5ef | 112 | MBEDTLS_CTR_DRBG_ENTROPY_LEN ) ); |
Christopher Haster |
1:24750b9ad5ef | 113 | } |
Christopher Haster |
1:24750b9ad5ef | 114 | |
Christopher Haster |
1:24750b9ad5ef | 115 | void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx ) |
Christopher Haster |
1:24750b9ad5ef | 116 | { |
Christopher Haster |
1:24750b9ad5ef | 117 | if( ctx == NULL ) |
Christopher Haster |
1:24750b9ad5ef | 118 | return; |
Christopher Haster |
1:24750b9ad5ef | 119 | |
Christopher Haster |
1:24750b9ad5ef | 120 | #if defined(MBEDTLS_THREADING_C) |
Christopher Haster |
1:24750b9ad5ef | 121 | mbedtls_mutex_free( &ctx->mutex ); |
Christopher Haster |
1:24750b9ad5ef | 122 | #endif |
Christopher Haster |
1:24750b9ad5ef | 123 | mbedtls_aes_free( &ctx->aes_ctx ); |
Christopher Haster |
1:24750b9ad5ef | 124 | mbedtls_zeroize( ctx, sizeof( mbedtls_ctr_drbg_context ) ); |
Christopher Haster |
1:24750b9ad5ef | 125 | } |
Christopher Haster |
1:24750b9ad5ef | 126 | |
Christopher Haster |
1:24750b9ad5ef | 127 | void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx, int resistance ) |
Christopher Haster |
1:24750b9ad5ef | 128 | { |
Christopher Haster |
1:24750b9ad5ef | 129 | ctx->prediction_resistance = resistance; |
Christopher Haster |
1:24750b9ad5ef | 130 | } |
Christopher Haster |
1:24750b9ad5ef | 131 | |
Christopher Haster |
1:24750b9ad5ef | 132 | void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx, size_t len ) |
Christopher Haster |
1:24750b9ad5ef | 133 | { |
Christopher Haster |
1:24750b9ad5ef | 134 | ctx->entropy_len = len; |
Christopher Haster |
1:24750b9ad5ef | 135 | } |
Christopher Haster |
1:24750b9ad5ef | 136 | |
Christopher Haster |
1:24750b9ad5ef | 137 | void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx, int interval ) |
Christopher Haster |
1:24750b9ad5ef | 138 | { |
Christopher Haster |
1:24750b9ad5ef | 139 | ctx->reseed_interval = interval; |
Christopher Haster |
1:24750b9ad5ef | 140 | } |
Christopher Haster |
1:24750b9ad5ef | 141 | |
Christopher Haster |
1:24750b9ad5ef | 142 | static int block_cipher_df( unsigned char *output, |
Christopher Haster |
1:24750b9ad5ef | 143 | const unsigned char *data, size_t data_len ) |
Christopher Haster |
1:24750b9ad5ef | 144 | { |
Christopher Haster |
1:24750b9ad5ef | 145 | unsigned char buf[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + MBEDTLS_CTR_DRBG_BLOCKSIZE + 16]; |
Christopher Haster |
1:24750b9ad5ef | 146 | unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN]; |
Christopher Haster |
1:24750b9ad5ef | 147 | unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE]; |
Christopher Haster |
1:24750b9ad5ef | 148 | unsigned char chain[MBEDTLS_CTR_DRBG_BLOCKSIZE]; |
Christopher Haster |
1:24750b9ad5ef | 149 | unsigned char *p, *iv; |
Christopher Haster |
1:24750b9ad5ef | 150 | mbedtls_aes_context aes_ctx; |
Christopher Haster |
1:24750b9ad5ef | 151 | |
Christopher Haster |
1:24750b9ad5ef | 152 | int i, j; |
Christopher Haster |
1:24750b9ad5ef | 153 | size_t buf_len, use_len; |
Christopher Haster |
1:24750b9ad5ef | 154 | |
Christopher Haster |
1:24750b9ad5ef | 155 | if( data_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT ) |
Christopher Haster |
1:24750b9ad5ef | 156 | return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG ); |
Christopher Haster |
1:24750b9ad5ef | 157 | |
Christopher Haster |
1:24750b9ad5ef | 158 | memset( buf, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + MBEDTLS_CTR_DRBG_BLOCKSIZE + 16 ); |
Christopher Haster |
1:24750b9ad5ef | 159 | mbedtls_aes_init( &aes_ctx ); |
Christopher Haster |
1:24750b9ad5ef | 160 | |
Christopher Haster |
1:24750b9ad5ef | 161 | /* |
Christopher Haster |
1:24750b9ad5ef | 162 | * Construct IV (16 bytes) and S in buffer |
Christopher Haster |
1:24750b9ad5ef | 163 | * IV = Counter (in 32-bits) padded to 16 with zeroes |
Christopher Haster |
1:24750b9ad5ef | 164 | * S = Length input string (in 32-bits) || Length of output (in 32-bits) || |
Christopher Haster |
1:24750b9ad5ef | 165 | * data || 0x80 |
Christopher Haster |
1:24750b9ad5ef | 166 | * (Total is padded to a multiple of 16-bytes with zeroes) |
Christopher Haster |
1:24750b9ad5ef | 167 | */ |
Christopher Haster |
1:24750b9ad5ef | 168 | p = buf + MBEDTLS_CTR_DRBG_BLOCKSIZE; |
Christopher Haster |
1:24750b9ad5ef | 169 | *p++ = ( data_len >> 24 ) & 0xff; |
Christopher Haster |
1:24750b9ad5ef | 170 | *p++ = ( data_len >> 16 ) & 0xff; |
Christopher Haster |
1:24750b9ad5ef | 171 | *p++ = ( data_len >> 8 ) & 0xff; |
Christopher Haster |
1:24750b9ad5ef | 172 | *p++ = ( data_len ) & 0xff; |
Christopher Haster |
1:24750b9ad5ef | 173 | p += 3; |
Christopher Haster |
1:24750b9ad5ef | 174 | *p++ = MBEDTLS_CTR_DRBG_SEEDLEN; |
Christopher Haster |
1:24750b9ad5ef | 175 | memcpy( p, data, data_len ); |
Christopher Haster |
1:24750b9ad5ef | 176 | p[data_len] = 0x80; |
Christopher Haster |
1:24750b9ad5ef | 177 | |
Christopher Haster |
1:24750b9ad5ef | 178 | buf_len = MBEDTLS_CTR_DRBG_BLOCKSIZE + 8 + data_len + 1; |
Christopher Haster |
1:24750b9ad5ef | 179 | |
Christopher Haster |
1:24750b9ad5ef | 180 | for( i = 0; i < MBEDTLS_CTR_DRBG_KEYSIZE; i++ ) |
Christopher Haster |
1:24750b9ad5ef | 181 | key[i] = i; |
Christopher Haster |
1:24750b9ad5ef | 182 | |
Christopher Haster |
1:24750b9ad5ef | 183 | mbedtls_aes_setkey_enc( &aes_ctx, key, MBEDTLS_CTR_DRBG_KEYBITS ); |
Christopher Haster |
1:24750b9ad5ef | 184 | |
Christopher Haster |
1:24750b9ad5ef | 185 | /* |
Christopher Haster |
1:24750b9ad5ef | 186 | * Reduce data to MBEDTLS_CTR_DRBG_SEEDLEN bytes of data |
Christopher Haster |
1:24750b9ad5ef | 187 | */ |
Christopher Haster |
1:24750b9ad5ef | 188 | for( j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE ) |
Christopher Haster |
1:24750b9ad5ef | 189 | { |
Christopher Haster |
1:24750b9ad5ef | 190 | p = buf; |
Christopher Haster |
1:24750b9ad5ef | 191 | memset( chain, 0, MBEDTLS_CTR_DRBG_BLOCKSIZE ); |
Christopher Haster |
1:24750b9ad5ef | 192 | use_len = buf_len; |
Christopher Haster |
1:24750b9ad5ef | 193 | |
Christopher Haster |
1:24750b9ad5ef | 194 | while( use_len > 0 ) |
Christopher Haster |
1:24750b9ad5ef | 195 | { |
Christopher Haster |
1:24750b9ad5ef | 196 | for( i = 0; i < MBEDTLS_CTR_DRBG_BLOCKSIZE; i++ ) |
Christopher Haster |
1:24750b9ad5ef | 197 | chain[i] ^= p[i]; |
Christopher Haster |
1:24750b9ad5ef | 198 | p += MBEDTLS_CTR_DRBG_BLOCKSIZE; |
Christopher Haster |
1:24750b9ad5ef | 199 | use_len -= ( use_len >= MBEDTLS_CTR_DRBG_BLOCKSIZE ) ? |
Christopher Haster |
1:24750b9ad5ef | 200 | MBEDTLS_CTR_DRBG_BLOCKSIZE : use_len; |
Christopher Haster |
1:24750b9ad5ef | 201 | |
Christopher Haster |
1:24750b9ad5ef | 202 | mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, chain, chain ); |
Christopher Haster |
1:24750b9ad5ef | 203 | } |
Christopher Haster |
1:24750b9ad5ef | 204 | |
Christopher Haster |
1:24750b9ad5ef | 205 | memcpy( tmp + j, chain, MBEDTLS_CTR_DRBG_BLOCKSIZE ); |
Christopher Haster |
1:24750b9ad5ef | 206 | |
Christopher Haster |
1:24750b9ad5ef | 207 | /* |
Christopher Haster |
1:24750b9ad5ef | 208 | * Update IV |
Christopher Haster |
1:24750b9ad5ef | 209 | */ |
Christopher Haster |
1:24750b9ad5ef | 210 | buf[3]++; |
Christopher Haster |
1:24750b9ad5ef | 211 | } |
Christopher Haster |
1:24750b9ad5ef | 212 | |
Christopher Haster |
1:24750b9ad5ef | 213 | /* |
Christopher Haster |
1:24750b9ad5ef | 214 | * Do final encryption with reduced data |
Christopher Haster |
1:24750b9ad5ef | 215 | */ |
Christopher Haster |
1:24750b9ad5ef | 216 | mbedtls_aes_setkey_enc( &aes_ctx, tmp, MBEDTLS_CTR_DRBG_KEYBITS ); |
Christopher Haster |
1:24750b9ad5ef | 217 | iv = tmp + MBEDTLS_CTR_DRBG_KEYSIZE; |
Christopher Haster |
1:24750b9ad5ef | 218 | p = output; |
Christopher Haster |
1:24750b9ad5ef | 219 | |
Christopher Haster |
1:24750b9ad5ef | 220 | for( j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE ) |
Christopher Haster |
1:24750b9ad5ef | 221 | { |
Christopher Haster |
1:24750b9ad5ef | 222 | mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, iv, iv ); |
Christopher Haster |
1:24750b9ad5ef | 223 | memcpy( p, iv, MBEDTLS_CTR_DRBG_BLOCKSIZE ); |
Christopher Haster |
1:24750b9ad5ef | 224 | p += MBEDTLS_CTR_DRBG_BLOCKSIZE; |
Christopher Haster |
1:24750b9ad5ef | 225 | } |
Christopher Haster |
1:24750b9ad5ef | 226 | |
Christopher Haster |
1:24750b9ad5ef | 227 | mbedtls_aes_free( &aes_ctx ); |
Christopher Haster |
1:24750b9ad5ef | 228 | |
Christopher Haster |
1:24750b9ad5ef | 229 | return( 0 ); |
Christopher Haster |
1:24750b9ad5ef | 230 | } |
Christopher Haster |
1:24750b9ad5ef | 231 | |
Christopher Haster |
1:24750b9ad5ef | 232 | static int ctr_drbg_update_internal( mbedtls_ctr_drbg_context *ctx, |
Christopher Haster |
1:24750b9ad5ef | 233 | const unsigned char data[MBEDTLS_CTR_DRBG_SEEDLEN] ) |
Christopher Haster |
1:24750b9ad5ef | 234 | { |
Christopher Haster |
1:24750b9ad5ef | 235 | unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN]; |
Christopher Haster |
1:24750b9ad5ef | 236 | unsigned char *p = tmp; |
Christopher Haster |
1:24750b9ad5ef | 237 | int i, j; |
Christopher Haster |
1:24750b9ad5ef | 238 | |
Christopher Haster |
1:24750b9ad5ef | 239 | memset( tmp, 0, MBEDTLS_CTR_DRBG_SEEDLEN ); |
Christopher Haster |
1:24750b9ad5ef | 240 | |
Christopher Haster |
1:24750b9ad5ef | 241 | for( j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE ) |
Christopher Haster |
1:24750b9ad5ef | 242 | { |
Christopher Haster |
1:24750b9ad5ef | 243 | /* |
Christopher Haster |
1:24750b9ad5ef | 244 | * Increase counter |
Christopher Haster |
1:24750b9ad5ef | 245 | */ |
Christopher Haster |
1:24750b9ad5ef | 246 | for( i = MBEDTLS_CTR_DRBG_BLOCKSIZE; i > 0; i-- ) |
Christopher Haster |
1:24750b9ad5ef | 247 | if( ++ctx->counter[i - 1] != 0 ) |
Christopher Haster |
1:24750b9ad5ef | 248 | break; |
Christopher Haster |
1:24750b9ad5ef | 249 | |
Christopher Haster |
1:24750b9ad5ef | 250 | /* |
Christopher Haster |
1:24750b9ad5ef | 251 | * Crypt counter block |
Christopher Haster |
1:24750b9ad5ef | 252 | */ |
Christopher Haster |
1:24750b9ad5ef | 253 | mbedtls_aes_crypt_ecb( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, ctx->counter, p ); |
Christopher Haster |
1:24750b9ad5ef | 254 | |
Christopher Haster |
1:24750b9ad5ef | 255 | p += MBEDTLS_CTR_DRBG_BLOCKSIZE; |
Christopher Haster |
1:24750b9ad5ef | 256 | } |
Christopher Haster |
1:24750b9ad5ef | 257 | |
Christopher Haster |
1:24750b9ad5ef | 258 | for( i = 0; i < MBEDTLS_CTR_DRBG_SEEDLEN; i++ ) |
Christopher Haster |
1:24750b9ad5ef | 259 | tmp[i] ^= data[i]; |
Christopher Haster |
1:24750b9ad5ef | 260 | |
Christopher Haster |
1:24750b9ad5ef | 261 | /* |
Christopher Haster |
1:24750b9ad5ef | 262 | * Update key and counter |
Christopher Haster |
1:24750b9ad5ef | 263 | */ |
Christopher Haster |
1:24750b9ad5ef | 264 | mbedtls_aes_setkey_enc( &ctx->aes_ctx, tmp, MBEDTLS_CTR_DRBG_KEYBITS ); |
Christopher Haster |
1:24750b9ad5ef | 265 | memcpy( ctx->counter, tmp + MBEDTLS_CTR_DRBG_KEYSIZE, MBEDTLS_CTR_DRBG_BLOCKSIZE ); |
Christopher Haster |
1:24750b9ad5ef | 266 | |
Christopher Haster |
1:24750b9ad5ef | 267 | return( 0 ); |
Christopher Haster |
1:24750b9ad5ef | 268 | } |
Christopher Haster |
1:24750b9ad5ef | 269 | |
Christopher Haster |
1:24750b9ad5ef | 270 | void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx, |
Christopher Haster |
1:24750b9ad5ef | 271 | const unsigned char *additional, size_t add_len ) |
Christopher Haster |
1:24750b9ad5ef | 272 | { |
Christopher Haster |
1:24750b9ad5ef | 273 | unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN]; |
Christopher Haster |
1:24750b9ad5ef | 274 | |
Christopher Haster |
1:24750b9ad5ef | 275 | if( add_len > 0 ) |
Christopher Haster |
1:24750b9ad5ef | 276 | { |
Christopher Haster |
1:24750b9ad5ef | 277 | /* MAX_INPUT would be more logical here, but we have to match |
Christopher Haster |
1:24750b9ad5ef | 278 | * block_cipher_df()'s limits since we can't propagate errors */ |
Christopher Haster |
1:24750b9ad5ef | 279 | if( add_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT ) |
Christopher Haster |
1:24750b9ad5ef | 280 | add_len = MBEDTLS_CTR_DRBG_MAX_SEED_INPUT; |
Christopher Haster |
1:24750b9ad5ef | 281 | |
Christopher Haster |
1:24750b9ad5ef | 282 | block_cipher_df( add_input, additional, add_len ); |
Christopher Haster |
1:24750b9ad5ef | 283 | ctr_drbg_update_internal( ctx, add_input ); |
Christopher Haster |
1:24750b9ad5ef | 284 | } |
Christopher Haster |
1:24750b9ad5ef | 285 | } |
Christopher Haster |
1:24750b9ad5ef | 286 | |
Christopher Haster |
1:24750b9ad5ef | 287 | int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx, |
Christopher Haster |
1:24750b9ad5ef | 288 | const unsigned char *additional, size_t len ) |
Christopher Haster |
1:24750b9ad5ef | 289 | { |
Christopher Haster |
1:24750b9ad5ef | 290 | unsigned char seed[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT]; |
Christopher Haster |
1:24750b9ad5ef | 291 | size_t seedlen = 0; |
Christopher Haster |
1:24750b9ad5ef | 292 | |
Christopher Haster |
1:24750b9ad5ef | 293 | if( ctx->entropy_len + len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT ) |
Christopher Haster |
1:24750b9ad5ef | 294 | return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG ); |
Christopher Haster |
1:24750b9ad5ef | 295 | |
Christopher Haster |
1:24750b9ad5ef | 296 | memset( seed, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT ); |
Christopher Haster |
1:24750b9ad5ef | 297 | |
Christopher Haster |
1:24750b9ad5ef | 298 | /* |
Christopher Haster |
1:24750b9ad5ef | 299 | * Gather entropy_len bytes of entropy to seed state |
Christopher Haster |
1:24750b9ad5ef | 300 | */ |
Christopher Haster |
1:24750b9ad5ef | 301 | if( 0 != ctx->f_entropy( ctx->p_entropy, seed, |
Christopher Haster |
1:24750b9ad5ef | 302 | ctx->entropy_len ) ) |
Christopher Haster |
1:24750b9ad5ef | 303 | { |
Christopher Haster |
1:24750b9ad5ef | 304 | return( MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED ); |
Christopher Haster |
1:24750b9ad5ef | 305 | } |
Christopher Haster |
1:24750b9ad5ef | 306 | |
Christopher Haster |
1:24750b9ad5ef | 307 | seedlen += ctx->entropy_len; |
Christopher Haster |
1:24750b9ad5ef | 308 | |
Christopher Haster |
1:24750b9ad5ef | 309 | /* |
Christopher Haster |
1:24750b9ad5ef | 310 | * Add additional data |
Christopher Haster |
1:24750b9ad5ef | 311 | */ |
Christopher Haster |
1:24750b9ad5ef | 312 | if( additional && len ) |
Christopher Haster |
1:24750b9ad5ef | 313 | { |
Christopher Haster |
1:24750b9ad5ef | 314 | memcpy( seed + seedlen, additional, len ); |
Christopher Haster |
1:24750b9ad5ef | 315 | seedlen += len; |
Christopher Haster |
1:24750b9ad5ef | 316 | } |
Christopher Haster |
1:24750b9ad5ef | 317 | |
Christopher Haster |
1:24750b9ad5ef | 318 | /* |
Christopher Haster |
1:24750b9ad5ef | 319 | * Reduce to 384 bits |
Christopher Haster |
1:24750b9ad5ef | 320 | */ |
Christopher Haster |
1:24750b9ad5ef | 321 | block_cipher_df( seed, seed, seedlen ); |
Christopher Haster |
1:24750b9ad5ef | 322 | |
Christopher Haster |
1:24750b9ad5ef | 323 | /* |
Christopher Haster |
1:24750b9ad5ef | 324 | * Update state |
Christopher Haster |
1:24750b9ad5ef | 325 | */ |
Christopher Haster |
1:24750b9ad5ef | 326 | ctr_drbg_update_internal( ctx, seed ); |
Christopher Haster |
1:24750b9ad5ef | 327 | ctx->reseed_counter = 1; |
Christopher Haster |
1:24750b9ad5ef | 328 | |
Christopher Haster |
1:24750b9ad5ef | 329 | return( 0 ); |
Christopher Haster |
1:24750b9ad5ef | 330 | } |
Christopher Haster |
1:24750b9ad5ef | 331 | |
Christopher Haster |
1:24750b9ad5ef | 332 | int mbedtls_ctr_drbg_random_with_add( void *p_rng, |
Christopher Haster |
1:24750b9ad5ef | 333 | unsigned char *output, size_t output_len, |
Christopher Haster |
1:24750b9ad5ef | 334 | const unsigned char *additional, size_t add_len ) |
Christopher Haster |
1:24750b9ad5ef | 335 | { |
Christopher Haster |
1:24750b9ad5ef | 336 | int ret = 0; |
Christopher Haster |
1:24750b9ad5ef | 337 | mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng; |
Christopher Haster |
1:24750b9ad5ef | 338 | unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN]; |
Christopher Haster |
1:24750b9ad5ef | 339 | unsigned char *p = output; |
Christopher Haster |
1:24750b9ad5ef | 340 | unsigned char tmp[MBEDTLS_CTR_DRBG_BLOCKSIZE]; |
Christopher Haster |
1:24750b9ad5ef | 341 | int i; |
Christopher Haster |
1:24750b9ad5ef | 342 | size_t use_len; |
Christopher Haster |
1:24750b9ad5ef | 343 | |
Christopher Haster |
1:24750b9ad5ef | 344 | if( output_len > MBEDTLS_CTR_DRBG_MAX_REQUEST ) |
Christopher Haster |
1:24750b9ad5ef | 345 | return( MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG ); |
Christopher Haster |
1:24750b9ad5ef | 346 | |
Christopher Haster |
1:24750b9ad5ef | 347 | if( add_len > MBEDTLS_CTR_DRBG_MAX_INPUT ) |
Christopher Haster |
1:24750b9ad5ef | 348 | return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG ); |
Christopher Haster |
1:24750b9ad5ef | 349 | |
Christopher Haster |
1:24750b9ad5ef | 350 | memset( add_input, 0, MBEDTLS_CTR_DRBG_SEEDLEN ); |
Christopher Haster |
1:24750b9ad5ef | 351 | |
Christopher Haster |
1:24750b9ad5ef | 352 | if( ctx->reseed_counter > ctx->reseed_interval || |
Christopher Haster |
1:24750b9ad5ef | 353 | ctx->prediction_resistance ) |
Christopher Haster |
1:24750b9ad5ef | 354 | { |
Christopher Haster |
1:24750b9ad5ef | 355 | if( ( ret = mbedtls_ctr_drbg_reseed( ctx, additional, add_len ) ) != 0 ) |
Christopher Haster |
1:24750b9ad5ef | 356 | return( ret ); |
Christopher Haster |
1:24750b9ad5ef | 357 | |
Christopher Haster |
1:24750b9ad5ef | 358 | add_len = 0; |
Christopher Haster |
1:24750b9ad5ef | 359 | } |
Christopher Haster |
1:24750b9ad5ef | 360 | |
Christopher Haster |
1:24750b9ad5ef | 361 | if( add_len > 0 ) |
Christopher Haster |
1:24750b9ad5ef | 362 | { |
Christopher Haster |
1:24750b9ad5ef | 363 | block_cipher_df( add_input, additional, add_len ); |
Christopher Haster |
1:24750b9ad5ef | 364 | ctr_drbg_update_internal( ctx, add_input ); |
Christopher Haster |
1:24750b9ad5ef | 365 | } |
Christopher Haster |
1:24750b9ad5ef | 366 | |
Christopher Haster |
1:24750b9ad5ef | 367 | while( output_len > 0 ) |
Christopher Haster |
1:24750b9ad5ef | 368 | { |
Christopher Haster |
1:24750b9ad5ef | 369 | /* |
Christopher Haster |
1:24750b9ad5ef | 370 | * Increase counter |
Christopher Haster |
1:24750b9ad5ef | 371 | */ |
Christopher Haster |
1:24750b9ad5ef | 372 | for( i = MBEDTLS_CTR_DRBG_BLOCKSIZE; i > 0; i-- ) |
Christopher Haster |
1:24750b9ad5ef | 373 | if( ++ctx->counter[i - 1] != 0 ) |
Christopher Haster |
1:24750b9ad5ef | 374 | break; |
Christopher Haster |
1:24750b9ad5ef | 375 | |
Christopher Haster |
1:24750b9ad5ef | 376 | /* |
Christopher Haster |
1:24750b9ad5ef | 377 | * Crypt counter block |
Christopher Haster |
1:24750b9ad5ef | 378 | */ |
Christopher Haster |
1:24750b9ad5ef | 379 | mbedtls_aes_crypt_ecb( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, ctx->counter, tmp ); |
Christopher Haster |
1:24750b9ad5ef | 380 | |
Christopher Haster |
1:24750b9ad5ef | 381 | use_len = ( output_len > MBEDTLS_CTR_DRBG_BLOCKSIZE ) ? MBEDTLS_CTR_DRBG_BLOCKSIZE : |
Christopher Haster |
1:24750b9ad5ef | 382 | output_len; |
Christopher Haster |
1:24750b9ad5ef | 383 | /* |
Christopher Haster |
1:24750b9ad5ef | 384 | * Copy random block to destination |
Christopher Haster |
1:24750b9ad5ef | 385 | */ |
Christopher Haster |
1:24750b9ad5ef | 386 | memcpy( p, tmp, use_len ); |
Christopher Haster |
1:24750b9ad5ef | 387 | p += use_len; |
Christopher Haster |
1:24750b9ad5ef | 388 | output_len -= use_len; |
Christopher Haster |
1:24750b9ad5ef | 389 | } |
Christopher Haster |
1:24750b9ad5ef | 390 | |
Christopher Haster |
1:24750b9ad5ef | 391 | ctr_drbg_update_internal( ctx, add_input ); |
Christopher Haster |
1:24750b9ad5ef | 392 | |
Christopher Haster |
1:24750b9ad5ef | 393 | ctx->reseed_counter++; |
Christopher Haster |
1:24750b9ad5ef | 394 | |
Christopher Haster |
1:24750b9ad5ef | 395 | return( 0 ); |
Christopher Haster |
1:24750b9ad5ef | 396 | } |
Christopher Haster |
1:24750b9ad5ef | 397 | |
Christopher Haster |
1:24750b9ad5ef | 398 | int mbedtls_ctr_drbg_random( void *p_rng, unsigned char *output, size_t output_len ) |
Christopher Haster |
1:24750b9ad5ef | 399 | { |
Christopher Haster |
1:24750b9ad5ef | 400 | int ret; |
Christopher Haster |
1:24750b9ad5ef | 401 | mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng; |
Christopher Haster |
1:24750b9ad5ef | 402 | |
Christopher Haster |
1:24750b9ad5ef | 403 | #if defined(MBEDTLS_THREADING_C) |
Christopher Haster |
1:24750b9ad5ef | 404 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
Christopher Haster |
1:24750b9ad5ef | 405 | return( ret ); |
Christopher Haster |
1:24750b9ad5ef | 406 | #endif |
Christopher Haster |
1:24750b9ad5ef | 407 | |
Christopher Haster |
1:24750b9ad5ef | 408 | ret = mbedtls_ctr_drbg_random_with_add( ctx, output, output_len, NULL, 0 ); |
Christopher Haster |
1:24750b9ad5ef | 409 | |
Christopher Haster |
1:24750b9ad5ef | 410 | #if defined(MBEDTLS_THREADING_C) |
Christopher Haster |
1:24750b9ad5ef | 411 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
Christopher Haster |
1:24750b9ad5ef | 412 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
Christopher Haster |
1:24750b9ad5ef | 413 | #endif |
Christopher Haster |
1:24750b9ad5ef | 414 | |
Christopher Haster |
1:24750b9ad5ef | 415 | return( ret ); |
Christopher Haster |
1:24750b9ad5ef | 416 | } |
Christopher Haster |
1:24750b9ad5ef | 417 | |
Christopher Haster |
1:24750b9ad5ef | 418 | #if defined(MBEDTLS_FS_IO) |
Christopher Haster |
1:24750b9ad5ef | 419 | int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path ) |
Christopher Haster |
1:24750b9ad5ef | 420 | { |
Christopher Haster |
1:24750b9ad5ef | 421 | int ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR; |
Christopher Haster |
1:24750b9ad5ef | 422 | FILE *f; |
Christopher Haster |
1:24750b9ad5ef | 423 | unsigned char buf[ MBEDTLS_CTR_DRBG_MAX_INPUT ]; |
Christopher Haster |
1:24750b9ad5ef | 424 | |
Christopher Haster |
1:24750b9ad5ef | 425 | if( ( f = fopen( path, "wb" ) ) == NULL ) |
Christopher Haster |
1:24750b9ad5ef | 426 | return( MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR ); |
Christopher Haster |
1:24750b9ad5ef | 427 | |
Christopher Haster |
1:24750b9ad5ef | 428 | if( ( ret = mbedtls_ctr_drbg_random( ctx, buf, MBEDTLS_CTR_DRBG_MAX_INPUT ) ) != 0 ) |
Christopher Haster |
1:24750b9ad5ef | 429 | goto exit; |
Christopher Haster |
1:24750b9ad5ef | 430 | |
Christopher Haster |
1:24750b9ad5ef | 431 | if( fwrite( buf, 1, MBEDTLS_CTR_DRBG_MAX_INPUT, f ) != MBEDTLS_CTR_DRBG_MAX_INPUT ) |
Christopher Haster |
1:24750b9ad5ef | 432 | { |
Christopher Haster |
1:24750b9ad5ef | 433 | ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR; |
Christopher Haster |
1:24750b9ad5ef | 434 | goto exit; |
Christopher Haster |
1:24750b9ad5ef | 435 | } |
Christopher Haster |
1:24750b9ad5ef | 436 | |
Christopher Haster |
1:24750b9ad5ef | 437 | ret = 0; |
Christopher Haster |
1:24750b9ad5ef | 438 | |
Christopher Haster |
1:24750b9ad5ef | 439 | exit: |
Christopher Haster |
1:24750b9ad5ef | 440 | fclose( f ); |
Christopher Haster |
1:24750b9ad5ef | 441 | return( ret ); |
Christopher Haster |
1:24750b9ad5ef | 442 | } |
Christopher Haster |
1:24750b9ad5ef | 443 | |
Christopher Haster |
1:24750b9ad5ef | 444 | int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path ) |
Christopher Haster |
1:24750b9ad5ef | 445 | { |
Christopher Haster |
1:24750b9ad5ef | 446 | FILE *f; |
Christopher Haster |
1:24750b9ad5ef | 447 | size_t n; |
Christopher Haster |
1:24750b9ad5ef | 448 | unsigned char buf[ MBEDTLS_CTR_DRBG_MAX_INPUT ]; |
Christopher Haster |
1:24750b9ad5ef | 449 | |
Christopher Haster |
1:24750b9ad5ef | 450 | if( ( f = fopen( path, "rb" ) ) == NULL ) |
Christopher Haster |
1:24750b9ad5ef | 451 | return( MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR ); |
Christopher Haster |
1:24750b9ad5ef | 452 | |
Christopher Haster |
1:24750b9ad5ef | 453 | fseek( f, 0, SEEK_END ); |
Christopher Haster |
1:24750b9ad5ef | 454 | n = (size_t) ftell( f ); |
Christopher Haster |
1:24750b9ad5ef | 455 | fseek( f, 0, SEEK_SET ); |
Christopher Haster |
1:24750b9ad5ef | 456 | |
Christopher Haster |
1:24750b9ad5ef | 457 | if( n > MBEDTLS_CTR_DRBG_MAX_INPUT ) |
Christopher Haster |
1:24750b9ad5ef | 458 | { |
Christopher Haster |
1:24750b9ad5ef | 459 | fclose( f ); |
Christopher Haster |
1:24750b9ad5ef | 460 | return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG ); |
Christopher Haster |
1:24750b9ad5ef | 461 | } |
Christopher Haster |
1:24750b9ad5ef | 462 | |
Christopher Haster |
1:24750b9ad5ef | 463 | if( fread( buf, 1, n, f ) != n ) |
Christopher Haster |
1:24750b9ad5ef | 464 | { |
Christopher Haster |
1:24750b9ad5ef | 465 | fclose( f ); |
Christopher Haster |
1:24750b9ad5ef | 466 | return( MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR ); |
Christopher Haster |
1:24750b9ad5ef | 467 | } |
Christopher Haster |
1:24750b9ad5ef | 468 | |
Christopher Haster |
1:24750b9ad5ef | 469 | fclose( f ); |
Christopher Haster |
1:24750b9ad5ef | 470 | |
Christopher Haster |
1:24750b9ad5ef | 471 | mbedtls_ctr_drbg_update( ctx, buf, n ); |
Christopher Haster |
1:24750b9ad5ef | 472 | |
Christopher Haster |
1:24750b9ad5ef | 473 | return( mbedtls_ctr_drbg_write_seed_file( ctx, path ) ); |
Christopher Haster |
1:24750b9ad5ef | 474 | } |
Christopher Haster |
1:24750b9ad5ef | 475 | #endif /* MBEDTLS_FS_IO */ |
Christopher Haster |
1:24750b9ad5ef | 476 | |
Christopher Haster |
1:24750b9ad5ef | 477 | #if defined(MBEDTLS_SELF_TEST) |
Christopher Haster |
1:24750b9ad5ef | 478 | |
Christopher Haster |
1:24750b9ad5ef | 479 | static const unsigned char entropy_source_pr[96] = |
Christopher Haster |
1:24750b9ad5ef | 480 | { 0xc1, 0x80, 0x81, 0xa6, 0x5d, 0x44, 0x02, 0x16, |
Christopher Haster |
1:24750b9ad5ef | 481 | 0x19, 0xb3, 0xf1, 0x80, 0xb1, 0xc9, 0x20, 0x02, |
Christopher Haster |
1:24750b9ad5ef | 482 | 0x6a, 0x54, 0x6f, 0x0c, 0x70, 0x81, 0x49, 0x8b, |
Christopher Haster |
1:24750b9ad5ef | 483 | 0x6e, 0xa6, 0x62, 0x52, 0x6d, 0x51, 0xb1, 0xcb, |
Christopher Haster |
1:24750b9ad5ef | 484 | 0x58, 0x3b, 0xfa, 0xd5, 0x37, 0x5f, 0xfb, 0xc9, |
Christopher Haster |
1:24750b9ad5ef | 485 | 0xff, 0x46, 0xd2, 0x19, 0xc7, 0x22, 0x3e, 0x95, |
Christopher Haster |
1:24750b9ad5ef | 486 | 0x45, 0x9d, 0x82, 0xe1, 0xe7, 0x22, 0x9f, 0x63, |
Christopher Haster |
1:24750b9ad5ef | 487 | 0x31, 0x69, 0xd2, 0x6b, 0x57, 0x47, 0x4f, 0xa3, |
Christopher Haster |
1:24750b9ad5ef | 488 | 0x37, 0xc9, 0x98, 0x1c, 0x0b, 0xfb, 0x91, 0x31, |
Christopher Haster |
1:24750b9ad5ef | 489 | 0x4d, 0x55, 0xb9, 0xe9, 0x1c, 0x5a, 0x5e, 0xe4, |
Christopher Haster |
1:24750b9ad5ef | 490 | 0x93, 0x92, 0xcf, 0xc5, 0x23, 0x12, 0xd5, 0x56, |
Christopher Haster |
1:24750b9ad5ef | 491 | 0x2c, 0x4a, 0x6e, 0xff, 0xdc, 0x10, 0xd0, 0x68 }; |
Christopher Haster |
1:24750b9ad5ef | 492 | |
Christopher Haster |
1:24750b9ad5ef | 493 | static const unsigned char entropy_source_nopr[64] = |
Christopher Haster |
1:24750b9ad5ef | 494 | { 0x5a, 0x19, 0x4d, 0x5e, 0x2b, 0x31, 0x58, 0x14, |
Christopher Haster |
1:24750b9ad5ef | 495 | 0x54, 0xde, 0xf6, 0x75, 0xfb, 0x79, 0x58, 0xfe, |
Christopher Haster |
1:24750b9ad5ef | 496 | 0xc7, 0xdb, 0x87, 0x3e, 0x56, 0x89, 0xfc, 0x9d, |
Christopher Haster |
1:24750b9ad5ef | 497 | 0x03, 0x21, 0x7c, 0x68, 0xd8, 0x03, 0x38, 0x20, |
Christopher Haster |
1:24750b9ad5ef | 498 | 0xf9, 0xe6, 0x5e, 0x04, 0xd8, 0x56, 0xf3, 0xa9, |
Christopher Haster |
1:24750b9ad5ef | 499 | 0xc4, 0x4a, 0x4c, 0xbd, 0xc1, 0xd0, 0x08, 0x46, |
Christopher Haster |
1:24750b9ad5ef | 500 | 0xf5, 0x98, 0x3d, 0x77, 0x1c, 0x1b, 0x13, 0x7e, |
Christopher Haster |
1:24750b9ad5ef | 501 | 0x4e, 0x0f, 0x9d, 0x8e, 0xf4, 0x09, 0xf9, 0x2e }; |
Christopher Haster |
1:24750b9ad5ef | 502 | |
Christopher Haster |
1:24750b9ad5ef | 503 | static const unsigned char nonce_pers_pr[16] = |
Christopher Haster |
1:24750b9ad5ef | 504 | { 0xd2, 0x54, 0xfc, 0xff, 0x02, 0x1e, 0x69, 0xd2, |
Christopher Haster |
1:24750b9ad5ef | 505 | 0x29, 0xc9, 0xcf, 0xad, 0x85, 0xfa, 0x48, 0x6c }; |
Christopher Haster |
1:24750b9ad5ef | 506 | |
Christopher Haster |
1:24750b9ad5ef | 507 | static const unsigned char nonce_pers_nopr[16] = |
Christopher Haster |
1:24750b9ad5ef | 508 | { 0x1b, 0x54, 0xb8, 0xff, 0x06, 0x42, 0xbf, 0xf5, |
Christopher Haster |
1:24750b9ad5ef | 509 | 0x21, 0xf1, 0x5c, 0x1c, 0x0b, 0x66, 0x5f, 0x3f }; |
Christopher Haster |
1:24750b9ad5ef | 510 | |
Christopher Haster |
1:24750b9ad5ef | 511 | static const unsigned char result_pr[16] = |
Christopher Haster |
1:24750b9ad5ef | 512 | { 0x34, 0x01, 0x16, 0x56, 0xb4, 0x29, 0x00, 0x8f, |
Christopher Haster |
1:24750b9ad5ef | 513 | 0x35, 0x63, 0xec, 0xb5, 0xf2, 0x59, 0x07, 0x23 }; |
Christopher Haster |
1:24750b9ad5ef | 514 | |
Christopher Haster |
1:24750b9ad5ef | 515 | static const unsigned char result_nopr[16] = |
Christopher Haster |
1:24750b9ad5ef | 516 | { 0xa0, 0x54, 0x30, 0x3d, 0x8a, 0x7e, 0xa9, 0x88, |
Christopher Haster |
1:24750b9ad5ef | 517 | 0x9d, 0x90, 0x3e, 0x07, 0x7c, 0x6f, 0x21, 0x8f }; |
Christopher Haster |
1:24750b9ad5ef | 518 | |
Christopher Haster |
1:24750b9ad5ef | 519 | static size_t test_offset; |
Christopher Haster |
1:24750b9ad5ef | 520 | static int ctr_drbg_self_test_entropy( void *data, unsigned char *buf, |
Christopher Haster |
1:24750b9ad5ef | 521 | size_t len ) |
Christopher Haster |
1:24750b9ad5ef | 522 | { |
Christopher Haster |
1:24750b9ad5ef | 523 | const unsigned char *p = data; |
Christopher Haster |
1:24750b9ad5ef | 524 | memcpy( buf, p + test_offset, len ); |
Christopher Haster |
1:24750b9ad5ef | 525 | test_offset += len; |
Christopher Haster |
1:24750b9ad5ef | 526 | return( 0 ); |
Christopher Haster |
1:24750b9ad5ef | 527 | } |
Christopher Haster |
1:24750b9ad5ef | 528 | |
Christopher Haster |
1:24750b9ad5ef | 529 | #define CHK( c ) if( (c) != 0 ) \ |
Christopher Haster |
1:24750b9ad5ef | 530 | { \ |
Christopher Haster |
1:24750b9ad5ef | 531 | if( verbose != 0 ) \ |
Christopher Haster |
1:24750b9ad5ef | 532 | mbedtls_printf( "failed\n" ); \ |
Christopher Haster |
1:24750b9ad5ef | 533 | return( 1 ); \ |
Christopher Haster |
1:24750b9ad5ef | 534 | } |
Christopher Haster |
1:24750b9ad5ef | 535 | |
Christopher Haster |
1:24750b9ad5ef | 536 | /* |
Christopher Haster |
1:24750b9ad5ef | 537 | * Checkup routine |
Christopher Haster |
1:24750b9ad5ef | 538 | */ |
Christopher Haster |
1:24750b9ad5ef | 539 | int mbedtls_ctr_drbg_self_test( int verbose ) |
Christopher Haster |
1:24750b9ad5ef | 540 | { |
Christopher Haster |
1:24750b9ad5ef | 541 | mbedtls_ctr_drbg_context ctx; |
Christopher Haster |
1:24750b9ad5ef | 542 | unsigned char buf[16]; |
Christopher Haster |
1:24750b9ad5ef | 543 | |
Christopher Haster |
1:24750b9ad5ef | 544 | mbedtls_ctr_drbg_init( &ctx ); |
Christopher Haster |
1:24750b9ad5ef | 545 | |
Christopher Haster |
1:24750b9ad5ef | 546 | /* |
Christopher Haster |
1:24750b9ad5ef | 547 | * Based on a NIST CTR_DRBG test vector (PR = True) |
Christopher Haster |
1:24750b9ad5ef | 548 | */ |
Christopher Haster |
1:24750b9ad5ef | 549 | if( verbose != 0 ) |
Christopher Haster |
1:24750b9ad5ef | 550 | mbedtls_printf( " CTR_DRBG (PR = TRUE) : " ); |
Christopher Haster |
1:24750b9ad5ef | 551 | |
Christopher Haster |
1:24750b9ad5ef | 552 | test_offset = 0; |
Christopher Haster |
1:24750b9ad5ef | 553 | CHK( mbedtls_ctr_drbg_seed_entropy_len( &ctx, ctr_drbg_self_test_entropy, |
Christopher Haster |
1:24750b9ad5ef | 554 | (void *) entropy_source_pr, nonce_pers_pr, 16, 32 ) ); |
Christopher Haster |
1:24750b9ad5ef | 555 | mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON ); |
Christopher Haster |
1:24750b9ad5ef | 556 | CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) ); |
Christopher Haster |
1:24750b9ad5ef | 557 | CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) ); |
Christopher Haster |
1:24750b9ad5ef | 558 | CHK( memcmp( buf, result_pr, MBEDTLS_CTR_DRBG_BLOCKSIZE ) ); |
Christopher Haster |
1:24750b9ad5ef | 559 | |
Christopher Haster |
1:24750b9ad5ef | 560 | mbedtls_ctr_drbg_free( &ctx ); |
Christopher Haster |
1:24750b9ad5ef | 561 | |
Christopher Haster |
1:24750b9ad5ef | 562 | if( verbose != 0 ) |
Christopher Haster |
1:24750b9ad5ef | 563 | mbedtls_printf( "passed\n" ); |
Christopher Haster |
1:24750b9ad5ef | 564 | |
Christopher Haster |
1:24750b9ad5ef | 565 | /* |
Christopher Haster |
1:24750b9ad5ef | 566 | * Based on a NIST CTR_DRBG test vector (PR = FALSE) |
Christopher Haster |
1:24750b9ad5ef | 567 | */ |
Christopher Haster |
1:24750b9ad5ef | 568 | if( verbose != 0 ) |
Christopher Haster |
1:24750b9ad5ef | 569 | mbedtls_printf( " CTR_DRBG (PR = FALSE): " ); |
Christopher Haster |
1:24750b9ad5ef | 570 | |
Christopher Haster |
1:24750b9ad5ef | 571 | mbedtls_ctr_drbg_init( &ctx ); |
Christopher Haster |
1:24750b9ad5ef | 572 | |
Christopher Haster |
1:24750b9ad5ef | 573 | test_offset = 0; |
Christopher Haster |
1:24750b9ad5ef | 574 | CHK( mbedtls_ctr_drbg_seed_entropy_len( &ctx, ctr_drbg_self_test_entropy, |
Christopher Haster |
1:24750b9ad5ef | 575 | (void *) entropy_source_nopr, nonce_pers_nopr, 16, 32 ) ); |
Christopher Haster |
1:24750b9ad5ef | 576 | CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) ); |
Christopher Haster |
1:24750b9ad5ef | 577 | CHK( mbedtls_ctr_drbg_reseed( &ctx, NULL, 0 ) ); |
Christopher Haster |
1:24750b9ad5ef | 578 | CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) ); |
Christopher Haster |
1:24750b9ad5ef | 579 | CHK( memcmp( buf, result_nopr, 16 ) ); |
Christopher Haster |
1:24750b9ad5ef | 580 | |
Christopher Haster |
1:24750b9ad5ef | 581 | mbedtls_ctr_drbg_free( &ctx ); |
Christopher Haster |
1:24750b9ad5ef | 582 | |
Christopher Haster |
1:24750b9ad5ef | 583 | if( verbose != 0 ) |
Christopher Haster |
1:24750b9ad5ef | 584 | mbedtls_printf( "passed\n" ); |
Christopher Haster |
1:24750b9ad5ef | 585 | |
Christopher Haster |
1:24750b9ad5ef | 586 | if( verbose != 0 ) |
Christopher Haster |
1:24750b9ad5ef | 587 | mbedtls_printf( "\n" ); |
Christopher Haster |
1:24750b9ad5ef | 588 | |
Christopher Haster |
1:24750b9ad5ef | 589 | return( 0 ); |
Christopher Haster |
1:24750b9ad5ef | 590 | } |
Christopher Haster |
1:24750b9ad5ef | 591 | #endif /* MBEDTLS_SELF_TEST */ |
Christopher Haster |
1:24750b9ad5ef | 592 | |
Christopher Haster |
1:24750b9ad5ef | 593 | #endif /* MBEDTLS_CTR_DRBG_C */ |