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