Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * CTR_DRBG implementation based on AES-256 (NIST SP 800-90) |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 6 | */ |
| 7 | /* |
Paul Sokolovsky | 8d6d8c8 | 2018-02-10 11:11:41 +0200 | [diff] [blame] | 8 | * The NIST SP 800-90 DRBGs are described in the following publication. |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 9 | * |
Tom Cosgrove | ce37c5e | 2023-08-04 13:53:36 +0100 | [diff] [blame] | 10 | * https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-90r.pdf |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 13 | #include "common.h" |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 14 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 15 | #if defined(MBEDTLS_CTR_DRBG_C) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 16 | |
Dave Rodgman | 591ff05 | 2024-01-13 16:42:38 +0000 | [diff] [blame^] | 17 | #include "ctr.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 18 | #include "mbedtls/ctr_drbg.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 19 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 20 | #include "mbedtls/error.h" |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 21 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 22 | #include <string.h> |
| 23 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 24 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #endif |
| 27 | |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 28 | /* Using error translation functions from PSA to MbedTLS */ |
| 29 | #if !defined(MBEDTLS_AES_C) |
| 30 | #include "psa_util_internal.h" |
| 31 | #endif |
| 32 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 33 | #include "mbedtls/platform.h" |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 34 | |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 35 | #if !defined(MBEDTLS_AES_C) |
| 36 | static psa_status_t ctr_drbg_setup_psa_context(mbedtls_ctr_drbg_psa_context *psa_ctx, |
| 37 | unsigned char *key, size_t key_len) |
| 38 | { |
| 39 | psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT; |
| 40 | psa_status_t status; |
| 41 | |
| 42 | psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_ENCRYPT); |
| 43 | psa_set_key_algorithm(&key_attr, PSA_ALG_ECB_NO_PADDING); |
| 44 | psa_set_key_type(&key_attr, PSA_KEY_TYPE_AES); |
| 45 | status = psa_import_key(&key_attr, key, key_len, &psa_ctx->key_id); |
| 46 | if (status != PSA_SUCCESS) { |
| 47 | goto exit; |
| 48 | } |
| 49 | |
| 50 | status = psa_cipher_encrypt_setup(&psa_ctx->operation, psa_ctx->key_id, PSA_ALG_ECB_NO_PADDING); |
| 51 | if (status != PSA_SUCCESS) { |
| 52 | goto exit; |
| 53 | } |
| 54 | |
| 55 | exit: |
| 56 | psa_reset_key_attributes(&key_attr); |
| 57 | return status; |
| 58 | } |
| 59 | |
| 60 | static void ctr_drbg_destroy_psa_contex(mbedtls_ctr_drbg_psa_context *psa_ctx) |
| 61 | { |
| 62 | psa_cipher_abort(&psa_ctx->operation); |
| 63 | psa_destroy_key(psa_ctx->key_id); |
| 64 | |
| 65 | psa_ctx->operation = psa_cipher_operation_init(); |
| 66 | psa_ctx->key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 67 | } |
| 68 | #endif |
| 69 | |
Paul Bakker | 18d3291 | 2011-12-10 21:42:49 +0000 | [diff] [blame] | 70 | /* |
Manuel Pégourié-Gonnard | 8d128ef | 2015-04-28 22:38:08 +0200 | [diff] [blame] | 71 | * CTR_DRBG context initialization |
| 72 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 73 | void mbedtls_ctr_drbg_init(mbedtls_ctr_drbg_context *ctx) |
Manuel Pégourié-Gonnard | 8d128ef | 2015-04-28 22:38:08 +0200 | [diff] [blame] | 74 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | memset(ctx, 0, sizeof(mbedtls_ctr_drbg_context)); |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 76 | #if defined(MBEDTLS_AES_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 77 | mbedtls_aes_init(&ctx->aes_ctx); |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 78 | #else |
| 79 | ctx->psa_ctx.key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 80 | ctx->psa_ctx.operation = psa_cipher_operation_init(); |
| 81 | #endif |
Gilles Peskine | e9a3454 | 2019-10-22 20:43:24 +0200 | [diff] [blame] | 82 | /* Indicate that the entropy nonce length is not set explicitly. |
| 83 | * See mbedtls_ctr_drbg_set_nonce_len(). */ |
| 84 | ctx->reseed_counter = -1; |
Manuel Pégourié-Gonnard | 0a4fb09 | 2015-05-07 12:50:31 +0100 | [diff] [blame] | 85 | |
Gavin Acquroff | 6aceb51 | 2020-03-01 17:06:11 -0800 | [diff] [blame] | 86 | ctx->reseed_interval = MBEDTLS_CTR_DRBG_RESEED_INTERVAL; |
Manuel Pégourié-Gonnard | 8d128ef | 2015-04-28 22:38:08 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Gavin Acquroff | 6aceb51 | 2020-03-01 17:06:11 -0800 | [diff] [blame] | 89 | /* |
| 90 | * This function resets CTR_DRBG context to the state immediately |
| 91 | * after initial call of mbedtls_ctr_drbg_init(). |
| 92 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 93 | void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context *ctx) |
Paul Bakker | fff0366 | 2014-06-18 16:21:25 +0200 | [diff] [blame] | 94 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 95 | if (ctx == NULL) { |
Paul Bakker | fff0366 | 2014-06-18 16:21:25 +0200 | [diff] [blame] | 96 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | } |
Paul Bakker | fff0366 | 2014-06-18 16:21:25 +0200 | [diff] [blame] | 98 | |
Manuel Pégourié-Gonnard | 0a4fb09 | 2015-05-07 12:50:31 +0100 | [diff] [blame] | 99 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | da290f9 | 2021-02-09 18:44:02 +0100 | [diff] [blame] | 100 | /* The mutex is initialized iff f_entropy is set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 101 | if (ctx->f_entropy != NULL) { |
| 102 | mbedtls_mutex_free(&ctx->mutex); |
| 103 | } |
Manuel Pégourié-Gonnard | 0a4fb09 | 2015-05-07 12:50:31 +0100 | [diff] [blame] | 104 | #endif |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 105 | #if defined(MBEDTLS_AES_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 106 | mbedtls_aes_free(&ctx->aes_ctx); |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 107 | #else |
| 108 | ctr_drbg_destroy_psa_contex(&ctx->psa_ctx); |
| 109 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 110 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ctr_drbg_context)); |
Gavin Acquroff | 6aceb51 | 2020-03-01 17:06:11 -0800 | [diff] [blame] | 111 | ctx->reseed_interval = MBEDTLS_CTR_DRBG_RESEED_INTERVAL; |
| 112 | ctx->reseed_counter = -1; |
Paul Bakker | fff0366 | 2014-06-18 16:21:25 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | void mbedtls_ctr_drbg_set_prediction_resistance(mbedtls_ctr_drbg_context *ctx, |
| 116 | int resistance) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 117 | { |
| 118 | ctx->prediction_resistance = resistance; |
| 119 | } |
| 120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | void mbedtls_ctr_drbg_set_entropy_len(mbedtls_ctr_drbg_context *ctx, |
| 122 | size_t len) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 123 | { |
| 124 | ctx->entropy_len = len; |
| 125 | } |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 127 | int mbedtls_ctr_drbg_set_nonce_len(mbedtls_ctr_drbg_context *ctx, |
| 128 | size_t len) |
Gilles Peskine | 9be5098 | 2019-10-22 18:42:27 +0200 | [diff] [blame] | 129 | { |
| 130 | /* If mbedtls_ctr_drbg_seed() has already been called, it's |
| 131 | * too late. Return the error code that's closest to making sense. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 132 | if (ctx->f_entropy != NULL) { |
| 133 | return MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; |
| 134 | } |
Gilles Peskine | 9be5098 | 2019-10-22 18:42:27 +0200 | [diff] [blame] | 135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 136 | if (len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT) { |
| 137 | return MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG; |
| 138 | } |
Dave Rodgman | 4a5c9ee | 2023-02-10 16:03:44 +0000 | [diff] [blame] | 139 | |
Gilles Peskine | 9be5098 | 2019-10-22 18:42:27 +0200 | [diff] [blame] | 140 | /* This shouldn't be an issue because |
| 141 | * MBEDTLS_CTR_DRBG_MAX_SEED_INPUT < INT_MAX in any sensible |
| 142 | * configuration, but make sure anyway. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 143 | if (len > INT_MAX) { |
| 144 | return MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG; |
| 145 | } |
Gilles Peskine | 9be5098 | 2019-10-22 18:42:27 +0200 | [diff] [blame] | 146 | |
| 147 | /* For backward compatibility with Mbed TLS <= 2.19, store the |
| 148 | * entropy nonce length in a field that already exists, but isn't |
| 149 | * used until after the initial seeding. */ |
| 150 | /* Due to the capping of len above, the value fits in an int. */ |
| 151 | ctx->reseed_counter = (int) len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | return 0; |
Gilles Peskine | 9be5098 | 2019-10-22 18:42:27 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | void mbedtls_ctr_drbg_set_reseed_interval(mbedtls_ctr_drbg_context *ctx, |
| 156 | int interval) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 157 | { |
| 158 | ctx->reseed_interval = interval; |
| 159 | } |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 161 | static int block_cipher_df(unsigned char *output, |
| 162 | const unsigned char *data, size_t data_len) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 163 | { |
Hanno Becker | a08651f | 2018-10-05 09:38:59 +0100 | [diff] [blame] | 164 | unsigned char buf[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + |
| 165 | MBEDTLS_CTR_DRBG_BLOCKSIZE + 16]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN]; |
| 167 | unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE]; |
| 168 | unsigned char chain[MBEDTLS_CTR_DRBG_BLOCKSIZE]; |
Manuel Pégourié-Gonnard | 7c59363 | 2014-01-20 10:27:13 +0100 | [diff] [blame] | 169 | unsigned char *p, *iv; |
Dvir Markovich | 1b36499 | 2017-06-26 13:43:34 +0300 | [diff] [blame] | 170 | int ret = 0; |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 171 | #if defined(MBEDTLS_AES_C) |
| 172 | mbedtls_aes_context aes_ctx; |
| 173 | #else |
| 174 | psa_status_t status; |
| 175 | size_t tmp_len; |
| 176 | mbedtls_ctr_drbg_psa_context psa_ctx; |
| 177 | |
| 178 | psa_ctx.key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 179 | psa_ctx.operation = psa_cipher_operation_init(); |
| 180 | #endif |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 181 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 182 | int i, j; |
| 183 | size_t buf_len, use_len; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 185 | if (data_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT) { |
| 186 | return MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG; |
| 187 | } |
Manuel Pégourié-Gonnard | 5cb4b31 | 2014-11-25 17:41:50 +0100 | [diff] [blame] | 188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 189 | memset(buf, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + |
| 190 | MBEDTLS_CTR_DRBG_BLOCKSIZE + 16); |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 191 | |
| 192 | /* |
| 193 | * Construct IV (16 bytes) and S in buffer |
| 194 | * IV = Counter (in 32-bits) padded to 16 with zeroes |
| 195 | * S = Length input string (in 32-bits) || Length of output (in 32-bits) || |
| 196 | * data || 0x80 |
| 197 | * (Total is padded to a multiple of 16-bytes with zeroes) |
| 198 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | p = buf + MBEDTLS_CTR_DRBG_BLOCKSIZE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 200 | MBEDTLS_PUT_UINT32_BE(data_len, p, 0); |
Joe Subbiani | a5cb0d2 | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 201 | p += 4 + 3; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 202 | *p++ = MBEDTLS_CTR_DRBG_SEEDLEN; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | memcpy(p, data, data_len); |
Paul Bakker | 2bc7cf1 | 2011-11-29 10:50:51 +0000 | [diff] [blame] | 204 | p[data_len] = 0x80; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 205 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | buf_len = MBEDTLS_CTR_DRBG_BLOCKSIZE + 8 + data_len + 1; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 208 | for (i = 0; i < MBEDTLS_CTR_DRBG_KEYSIZE; i++) { |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 209 | key[i] = i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 210 | } |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 211 | |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 212 | #if defined(MBEDTLS_AES_C) |
| 213 | mbedtls_aes_init(&aes_ctx); |
| 214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | if ((ret = mbedtls_aes_setkey_enc(&aes_ctx, key, |
| 216 | MBEDTLS_CTR_DRBG_KEYBITS)) != 0) { |
Dvir Markovich | 1b36499 | 2017-06-26 13:43:34 +0300 | [diff] [blame] | 217 | goto exit; |
| 218 | } |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 219 | #else |
| 220 | status = ctr_drbg_setup_psa_context(&psa_ctx, key, sizeof(key)); |
| 221 | if (status != PSA_SUCCESS) { |
| 222 | ret = psa_generic_status_to_mbedtls(status); |
| 223 | goto exit; |
| 224 | } |
| 225 | #endif |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 226 | |
| 227 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 228 | * Reduce data to MBEDTLS_CTR_DRBG_SEEDLEN bytes of data |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 229 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 230 | for (j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE) { |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 231 | p = buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 232 | memset(chain, 0, MBEDTLS_CTR_DRBG_BLOCKSIZE); |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 233 | use_len = buf_len; |
| 234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 235 | while (use_len > 0) { |
| 236 | mbedtls_xor(chain, chain, p, MBEDTLS_CTR_DRBG_BLOCKSIZE); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | p += MBEDTLS_CTR_DRBG_BLOCKSIZE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 238 | use_len -= (use_len >= MBEDTLS_CTR_DRBG_BLOCKSIZE) ? |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 239 | MBEDTLS_CTR_DRBG_BLOCKSIZE : use_len; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 240 | |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 241 | #if defined(MBEDTLS_AES_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 242 | if ((ret = mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, |
| 243 | chain, chain)) != 0) { |
Dvir Markovich | 1b36499 | 2017-06-26 13:43:34 +0300 | [diff] [blame] | 244 | goto exit; |
| 245 | } |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 246 | #else |
| 247 | status = psa_cipher_update(&psa_ctx.operation, chain, MBEDTLS_CTR_DRBG_BLOCKSIZE, |
| 248 | chain, MBEDTLS_CTR_DRBG_BLOCKSIZE, &tmp_len); |
| 249 | if (status != PSA_SUCCESS) { |
| 250 | ret = psa_generic_status_to_mbedtls(status); |
| 251 | goto exit; |
| 252 | } |
| 253 | #endif |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 254 | } |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 256 | memcpy(tmp + j, chain, MBEDTLS_CTR_DRBG_BLOCKSIZE); |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 257 | |
| 258 | /* |
| 259 | * Update IV |
| 260 | */ |
| 261 | buf[3]++; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Do final encryption with reduced data |
| 266 | */ |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 267 | #if defined(MBEDTLS_AES_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 268 | if ((ret = mbedtls_aes_setkey_enc(&aes_ctx, tmp, |
| 269 | MBEDTLS_CTR_DRBG_KEYBITS)) != 0) { |
Dvir Markovich | 1b36499 | 2017-06-26 13:43:34 +0300 | [diff] [blame] | 270 | goto exit; |
| 271 | } |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 272 | #else |
| 273 | ctr_drbg_destroy_psa_contex(&psa_ctx); |
| 274 | |
| 275 | status = ctr_drbg_setup_psa_context(&psa_ctx, tmp, MBEDTLS_CTR_DRBG_KEYSIZE); |
| 276 | if (status != PSA_SUCCESS) { |
| 277 | ret = psa_generic_status_to_mbedtls(status); |
| 278 | goto exit; |
| 279 | } |
| 280 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 281 | iv = tmp + MBEDTLS_CTR_DRBG_KEYSIZE; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 282 | p = output; |
| 283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | for (j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE) { |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 285 | #if defined(MBEDTLS_AES_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 286 | if ((ret = mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, |
| 287 | iv, iv)) != 0) { |
Dvir Markovich | 1b36499 | 2017-06-26 13:43:34 +0300 | [diff] [blame] | 288 | goto exit; |
| 289 | } |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 290 | #else |
| 291 | status = psa_cipher_update(&psa_ctx.operation, iv, MBEDTLS_CTR_DRBG_BLOCKSIZE, |
| 292 | iv, MBEDTLS_CTR_DRBG_BLOCKSIZE, &tmp_len); |
| 293 | if (status != PSA_SUCCESS) { |
| 294 | ret = psa_generic_status_to_mbedtls(status); |
| 295 | goto exit; |
| 296 | } |
| 297 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 298 | memcpy(p, iv, MBEDTLS_CTR_DRBG_BLOCKSIZE); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 299 | p += MBEDTLS_CTR_DRBG_BLOCKSIZE; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 300 | } |
Dvir Markovich | 1b36499 | 2017-06-26 13:43:34 +0300 | [diff] [blame] | 301 | exit: |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 302 | #if defined(MBEDTLS_AES_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | mbedtls_aes_free(&aes_ctx); |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 304 | #else |
| 305 | ctr_drbg_destroy_psa_contex(&psa_ctx); |
| 306 | #endif |
Dvir Markovich | 1b36499 | 2017-06-26 13:43:34 +0300 | [diff] [blame] | 307 | /* |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 308 | * tidy up the stack |
| 309 | */ |
| 310 | mbedtls_platform_zeroize(buf, sizeof(buf)); |
| 311 | mbedtls_platform_zeroize(tmp, sizeof(tmp)); |
| 312 | mbedtls_platform_zeroize(key, sizeof(key)); |
| 313 | mbedtls_platform_zeroize(chain, sizeof(chain)); |
| 314 | if (0 != ret) { |
Dvir Markovich | 1b36499 | 2017-06-26 13:43:34 +0300 | [diff] [blame] | 315 | /* |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 316 | * wipe partial seed from memory |
| 317 | */ |
| 318 | mbedtls_platform_zeroize(output, MBEDTLS_CTR_DRBG_SEEDLEN); |
Dvir Markovich | 1b36499 | 2017-06-26 13:43:34 +0300 | [diff] [blame] | 319 | } |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 321 | return ret; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Gilles Peskine | ed7da59 | 2018-08-03 20:16:52 +0200 | [diff] [blame] | 324 | /* CTR_DRBG_Update (SP 800-90A §10.2.1.2) |
| 325 | * ctr_drbg_update_internal(ctx, provided_data) |
| 326 | * implements |
| 327 | * CTR_DRBG_Update(provided_data, Key, V) |
| 328 | * with inputs and outputs |
| 329 | * ctx->aes_ctx = Key |
| 330 | * ctx->counter = V |
| 331 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 332 | static int ctr_drbg_update_internal(mbedtls_ctr_drbg_context *ctx, |
| 333 | const unsigned char data[MBEDTLS_CTR_DRBG_SEEDLEN]) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 334 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 335 | unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN]; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 336 | unsigned char *p = tmp; |
Dave Rodgman | 591ff05 | 2024-01-13 16:42:38 +0000 | [diff] [blame^] | 337 | int j; |
Dvir Markovich | 1b36499 | 2017-06-26 13:43:34 +0300 | [diff] [blame] | 338 | int ret = 0; |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 339 | #if !defined(MBEDTLS_AES_C) |
| 340 | psa_status_t status; |
| 341 | size_t tmp_len; |
| 342 | #endif |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 344 | memset(tmp, 0, MBEDTLS_CTR_DRBG_SEEDLEN); |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 346 | for (j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE) { |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 347 | /* |
| 348 | * Increase counter |
| 349 | */ |
Dave Rodgman | 591ff05 | 2024-01-13 16:42:38 +0000 | [diff] [blame^] | 350 | mbedtls_ctr_increment_counter(ctx->counter); |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 351 | |
| 352 | /* |
| 353 | * Crypt counter block |
| 354 | */ |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 355 | #if defined(MBEDTLS_AES_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 356 | if ((ret = mbedtls_aes_crypt_ecb(&ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, |
| 357 | ctx->counter, p)) != 0) { |
Gilles Peskine | d9aa84d | 2018-09-11 15:34:17 +0200 | [diff] [blame] | 358 | goto exit; |
Dvir Markovich | 1b36499 | 2017-06-26 13:43:34 +0300 | [diff] [blame] | 359 | } |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 360 | #else |
| 361 | status = psa_cipher_update(&ctx->psa_ctx.operation, ctx->counter, sizeof(ctx->counter), |
| 362 | p, MBEDTLS_CTR_DRBG_BLOCKSIZE, &tmp_len); |
| 363 | if (status != PSA_SUCCESS) { |
| 364 | ret = psa_generic_status_to_mbedtls(status); |
| 365 | goto exit; |
| 366 | } |
| 367 | #endif |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 368 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 369 | p += MBEDTLS_CTR_DRBG_BLOCKSIZE; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | for (i = 0; i < MBEDTLS_CTR_DRBG_SEEDLEN; i++) { |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 373 | tmp[i] ^= data[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 374 | } |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 375 | |
| 376 | /* |
| 377 | * Update key and counter |
| 378 | */ |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 379 | #if defined(MBEDTLS_AES_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 380 | if ((ret = mbedtls_aes_setkey_enc(&ctx->aes_ctx, tmp, |
| 381 | MBEDTLS_CTR_DRBG_KEYBITS)) != 0) { |
Gilles Peskine | d9aa84d | 2018-09-11 15:34:17 +0200 | [diff] [blame] | 382 | goto exit; |
Dvir Markovich | 1b36499 | 2017-06-26 13:43:34 +0300 | [diff] [blame] | 383 | } |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 384 | #else |
| 385 | ctr_drbg_destroy_psa_contex(&ctx->psa_ctx); |
| 386 | |
| 387 | status = ctr_drbg_setup_psa_context(&ctx->psa_ctx, tmp, MBEDTLS_CTR_DRBG_KEYSIZE); |
| 388 | if (status != PSA_SUCCESS) { |
| 389 | ret = psa_generic_status_to_mbedtls(status); |
| 390 | goto exit; |
| 391 | } |
| 392 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 393 | memcpy(ctx->counter, tmp + MBEDTLS_CTR_DRBG_KEYSIZE, |
| 394 | MBEDTLS_CTR_DRBG_BLOCKSIZE); |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 395 | |
Gilles Peskine | d9aa84d | 2018-09-11 15:34:17 +0200 | [diff] [blame] | 396 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 397 | mbedtls_platform_zeroize(tmp, sizeof(tmp)); |
| 398 | return ret; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Gilles Peskine | ed7da59 | 2018-08-03 20:16:52 +0200 | [diff] [blame] | 401 | /* CTR_DRBG_Instantiate with derivation function (SP 800-90A §10.2.1.3.2) |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 402 | * mbedtls_ctr_drbg_update(ctx, additional, add_len) |
Gilles Peskine | ed7da59 | 2018-08-03 20:16:52 +0200 | [diff] [blame] | 403 | * implements |
| 404 | * CTR_DRBG_Instantiate(entropy_input, nonce, personalization_string, |
| 405 | * security_strength) -> initial_working_state |
| 406 | * with inputs |
| 407 | * ctx->counter = all-bits-0 |
| 408 | * ctx->aes_ctx = context from all-bits-0 key |
| 409 | * additional[:add_len] = entropy_input || nonce || personalization_string |
| 410 | * and with outputs |
| 411 | * ctx = initial_working_state |
| 412 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 413 | int mbedtls_ctr_drbg_update(mbedtls_ctr_drbg_context *ctx, |
| 414 | const unsigned char *additional, |
| 415 | size_t add_len) |
Paul Bakker | 1bc9efc | 2011-12-03 11:29:32 +0000 | [diff] [blame] | 416 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 417 | unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN]; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 418 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1bc9efc | 2011-12-03 11:29:32 +0000 | [diff] [blame] | 419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 420 | if (add_len == 0) { |
| 421 | return 0; |
| 422 | } |
Manuel Pégourié-Gonnard | 5cb4b31 | 2014-11-25 17:41:50 +0100 | [diff] [blame] | 423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 424 | if ((ret = block_cipher_df(add_input, additional, add_len)) != 0) { |
Gilles Peskine | d919993 | 2018-09-11 16:41:54 +0200 | [diff] [blame] | 425 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 426 | } |
| 427 | if ((ret = ctr_drbg_update_internal(ctx, add_input)) != 0) { |
Gilles Peskine | d919993 | 2018-09-11 16:41:54 +0200 | [diff] [blame] | 428 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 429 | } |
Gilles Peskine | d919993 | 2018-09-11 16:41:54 +0200 | [diff] [blame] | 430 | |
| 431 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 432 | mbedtls_platform_zeroize(add_input, sizeof(add_input)); |
| 433 | return ret; |
Paul Bakker | 1bc9efc | 2011-12-03 11:29:32 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Gilles Peskine | ed7da59 | 2018-08-03 20:16:52 +0200 | [diff] [blame] | 436 | /* CTR_DRBG_Reseed with derivation function (SP 800-90A §10.2.1.4.2) |
Gilles Peskine | 9be5098 | 2019-10-22 18:42:27 +0200 | [diff] [blame] | 437 | * mbedtls_ctr_drbg_reseed(ctx, additional, len, nonce_len) |
Gilles Peskine | ed7da59 | 2018-08-03 20:16:52 +0200 | [diff] [blame] | 438 | * implements |
| 439 | * CTR_DRBG_Reseed(working_state, entropy_input, additional_input) |
| 440 | * -> new_working_state |
| 441 | * with inputs |
| 442 | * ctx contains working_state |
| 443 | * additional[:len] = additional_input |
| 444 | * and entropy_input comes from calling ctx->f_entropy |
Gilles Peskine | 9be5098 | 2019-10-22 18:42:27 +0200 | [diff] [blame] | 445 | * for (ctx->entropy_len + nonce_len) bytes |
Gilles Peskine | ed7da59 | 2018-08-03 20:16:52 +0200 | [diff] [blame] | 446 | * and with output |
| 447 | * ctx contains new_working_state |
| 448 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 449 | static int mbedtls_ctr_drbg_reseed_internal(mbedtls_ctr_drbg_context *ctx, |
| 450 | const unsigned char *additional, |
| 451 | size_t len, |
| 452 | size_t nonce_len) |
Paul Bakker | 1bc9efc | 2011-12-03 11:29:32 +0000 | [diff] [blame] | 453 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 454 | unsigned char seed[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT]; |
Paul Bakker | 1bc9efc | 2011-12-03 11:29:32 +0000 | [diff] [blame] | 455 | size_t seedlen = 0; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 456 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1bc9efc | 2011-12-03 11:29:32 +0000 | [diff] [blame] | 457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 458 | if (ctx->entropy_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT) { |
| 459 | return MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG; |
| 460 | } |
| 461 | if (nonce_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - ctx->entropy_len) { |
| 462 | return MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG; |
| 463 | } |
| 464 | if (len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - ctx->entropy_len - nonce_len) { |
| 465 | return MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG; |
| 466 | } |
Paul Bakker | 1bc9efc | 2011-12-03 11:29:32 +0000 | [diff] [blame] | 467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | memset(seed, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT); |
Paul Bakker | 1bc9efc | 2011-12-03 11:29:32 +0000 | [diff] [blame] | 469 | |
Gilles Peskine | dbd3f7c | 2019-10-22 17:25:30 +0200 | [diff] [blame] | 470 | /* Gather entropy_len bytes of entropy to seed state. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 471 | if (0 != ctx->f_entropy(ctx->p_entropy, seed, ctx->entropy_len)) { |
| 472 | return MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; |
Paul Bakker | 1bc9efc | 2011-12-03 11:29:32 +0000 | [diff] [blame] | 473 | } |
Paul Bakker | 1bc9efc | 2011-12-03 11:29:32 +0000 | [diff] [blame] | 474 | seedlen += ctx->entropy_len; |
| 475 | |
Gilles Peskine | 9be5098 | 2019-10-22 18:42:27 +0200 | [diff] [blame] | 476 | /* Gather entropy for a nonce if requested. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 477 | if (nonce_len != 0) { |
| 478 | if (0 != ctx->f_entropy(ctx->p_entropy, seed + seedlen, nonce_len)) { |
| 479 | return MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; |
Gilles Peskine | 9be5098 | 2019-10-22 18:42:27 +0200 | [diff] [blame] | 480 | } |
| 481 | seedlen += nonce_len; |
| 482 | } |
| 483 | |
Gilles Peskine | dbd3f7c | 2019-10-22 17:25:30 +0200 | [diff] [blame] | 484 | /* Add additional data if provided. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 485 | if (additional != NULL && len != 0) { |
| 486 | memcpy(seed + seedlen, additional, len); |
Paul Bakker | 1bc9efc | 2011-12-03 11:29:32 +0000 | [diff] [blame] | 487 | seedlen += len; |
| 488 | } |
| 489 | |
Gilles Peskine | dbd3f7c | 2019-10-22 17:25:30 +0200 | [diff] [blame] | 490 | /* Reduce to 384 bits. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 491 | if ((ret = block_cipher_df(seed, seed, seedlen)) != 0) { |
Gilles Peskine | d9aa84d | 2018-09-11 15:34:17 +0200 | [diff] [blame] | 492 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 493 | } |
Paul Bakker | 1bc9efc | 2011-12-03 11:29:32 +0000 | [diff] [blame] | 494 | |
Gilles Peskine | dbd3f7c | 2019-10-22 17:25:30 +0200 | [diff] [blame] | 495 | /* Update state. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 496 | if ((ret = ctr_drbg_update_internal(ctx, seed)) != 0) { |
Gilles Peskine | d9aa84d | 2018-09-11 15:34:17 +0200 | [diff] [blame] | 497 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 498 | } |
Paul Bakker | 1bc9efc | 2011-12-03 11:29:32 +0000 | [diff] [blame] | 499 | ctx->reseed_counter = 1; |
| 500 | |
Gilles Peskine | d9aa84d | 2018-09-11 15:34:17 +0200 | [diff] [blame] | 501 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 502 | mbedtls_platform_zeroize(seed, sizeof(seed)); |
| 503 | return ret; |
Paul Bakker | 1bc9efc | 2011-12-03 11:29:32 +0000 | [diff] [blame] | 504 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 506 | int mbedtls_ctr_drbg_reseed(mbedtls_ctr_drbg_context *ctx, |
| 507 | const unsigned char *additional, size_t len) |
Gilles Peskine | 9be5098 | 2019-10-22 18:42:27 +0200 | [diff] [blame] | 508 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 509 | return mbedtls_ctr_drbg_reseed_internal(ctx, additional, len, 0); |
Gilles Peskine | 9be5098 | 2019-10-22 18:42:27 +0200 | [diff] [blame] | 510 | } |
| 511 | |
Gilles Peskine | e9a3454 | 2019-10-22 20:43:24 +0200 | [diff] [blame] | 512 | /* Return a "good" nonce length for CTR_DRBG. The chosen nonce length |
| 513 | * is sufficient to achieve the maximum security strength given the key |
| 514 | * size and entropy length. If there is enough entropy in the initial |
| 515 | * call to the entropy function to serve as both the entropy input and |
| 516 | * the nonce, don't make a second call to get a nonce. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 517 | static size_t good_nonce_len(size_t entropy_len) |
Gilles Peskine | e9a3454 | 2019-10-22 20:43:24 +0200 | [diff] [blame] | 518 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 519 | if (entropy_len >= MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2) { |
| 520 | return 0; |
| 521 | } else { |
| 522 | return (entropy_len + 1) / 2; |
| 523 | } |
Gilles Peskine | e9a3454 | 2019-10-22 20:43:24 +0200 | [diff] [blame] | 524 | } |
| 525 | |
Gilles Peskine | 8bf5613 | 2019-10-02 20:31:54 +0200 | [diff] [blame] | 526 | /* CTR_DRBG_Instantiate with derivation function (SP 800-90A §10.2.1.3.2) |
Gilles Peskine | 379561f | 2019-10-18 16:57:48 +0200 | [diff] [blame] | 527 | * mbedtls_ctr_drbg_seed(ctx, f_entropy, p_entropy, custom, len) |
Gilles Peskine | 8bf5613 | 2019-10-02 20:31:54 +0200 | [diff] [blame] | 528 | * implements |
| 529 | * CTR_DRBG_Instantiate(entropy_input, nonce, personalization_string, |
| 530 | * security_strength) -> initial_working_state |
| 531 | * with inputs |
| 532 | * custom[:len] = nonce || personalization_string |
Gilles Peskine | 379561f | 2019-10-18 16:57:48 +0200 | [diff] [blame] | 533 | * where entropy_input comes from f_entropy for ctx->entropy_len bytes |
Gilles Peskine | 8bf5613 | 2019-10-02 20:31:54 +0200 | [diff] [blame] | 534 | * and with outputs |
| 535 | * ctx = initial_working_state |
| 536 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 537 | int mbedtls_ctr_drbg_seed(mbedtls_ctr_drbg_context *ctx, |
| 538 | int (*f_entropy)(void *, unsigned char *, size_t), |
| 539 | void *p_entropy, |
| 540 | const unsigned char *custom, |
| 541 | size_t len) |
Gilles Peskine | 8bf5613 | 2019-10-02 20:31:54 +0200 | [diff] [blame] | 542 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 543 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 8bf5613 | 2019-10-02 20:31:54 +0200 | [diff] [blame] | 544 | unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE]; |
Gilles Peskine | e9a3454 | 2019-10-22 20:43:24 +0200 | [diff] [blame] | 545 | size_t nonce_len; |
Gilles Peskine | 8bf5613 | 2019-10-02 20:31:54 +0200 | [diff] [blame] | 546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 547 | memset(key, 0, MBEDTLS_CTR_DRBG_KEYSIZE); |
Gilles Peskine | 8bf5613 | 2019-10-02 20:31:54 +0200 | [diff] [blame] | 548 | |
Gilles Peskine | da290f9 | 2021-02-09 18:44:02 +0100 | [diff] [blame] | 549 | /* The mutex is initialized iff f_entropy is set. */ |
Gilles Peskine | f4b3429 | 2021-01-30 13:05:32 +0100 | [diff] [blame] | 550 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 551 | mbedtls_mutex_init(&ctx->mutex); |
Gilles Peskine | f4b3429 | 2021-01-30 13:05:32 +0100 | [diff] [blame] | 552 | #endif |
| 553 | |
Gilles Peskine | 8bf5613 | 2019-10-02 20:31:54 +0200 | [diff] [blame] | 554 | ctx->f_entropy = f_entropy; |
| 555 | ctx->p_entropy = p_entropy; |
| 556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 557 | if (ctx->entropy_len == 0) { |
Gilles Peskine | 50ed86b | 2019-10-04 12:15:55 +0200 | [diff] [blame] | 558 | ctx->entropy_len = MBEDTLS_CTR_DRBG_ENTROPY_LEN; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 559 | } |
Gilles Peskine | e9a3454 | 2019-10-22 20:43:24 +0200 | [diff] [blame] | 560 | /* ctx->reseed_counter contains the desired amount of entropy to |
| 561 | * grab for a nonce (see mbedtls_ctr_drbg_set_nonce_len()). |
| 562 | * If it's -1, indicating that the entropy nonce length was not set |
| 563 | * explicitly, use a sufficiently large nonce for security. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 564 | nonce_len = (ctx->reseed_counter >= 0 ? |
| 565 | (size_t) ctx->reseed_counter : |
| 566 | good_nonce_len(ctx->entropy_len)); |
Gilles Peskine | e9a3454 | 2019-10-22 20:43:24 +0200 | [diff] [blame] | 567 | |
Gilles Peskine | 9be5098 | 2019-10-22 18:42:27 +0200 | [diff] [blame] | 568 | /* Initialize with an empty key. */ |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 569 | #if defined(MBEDTLS_AES_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 570 | if ((ret = mbedtls_aes_setkey_enc(&ctx->aes_ctx, key, |
| 571 | MBEDTLS_CTR_DRBG_KEYBITS)) != 0) { |
| 572 | return ret; |
Gilles Peskine | 8bf5613 | 2019-10-02 20:31:54 +0200 | [diff] [blame] | 573 | } |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 574 | #else |
| 575 | psa_status_t status; |
| 576 | |
| 577 | status = ctr_drbg_setup_psa_context(&ctx->psa_ctx, key, MBEDTLS_CTR_DRBG_KEYSIZE); |
| 578 | if (status != PSA_SUCCESS) { |
| 579 | ret = psa_generic_status_to_mbedtls(status); |
| 580 | return status; |
| 581 | } |
| 582 | #endif |
Gilles Peskine | 8bf5613 | 2019-10-02 20:31:54 +0200 | [diff] [blame] | 583 | |
Gilles Peskine | e9a3454 | 2019-10-22 20:43:24 +0200 | [diff] [blame] | 584 | /* Do the initial seeding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 585 | if ((ret = mbedtls_ctr_drbg_reseed_internal(ctx, custom, len, |
| 586 | nonce_len)) != 0) { |
| 587 | return ret; |
Gilles Peskine | 8bf5613 | 2019-10-02 20:31:54 +0200 | [diff] [blame] | 588 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 589 | return 0; |
Gilles Peskine | 8bf5613 | 2019-10-02 20:31:54 +0200 | [diff] [blame] | 590 | } |
| 591 | |
Gilles Peskine | ed7da59 | 2018-08-03 20:16:52 +0200 | [diff] [blame] | 592 | /* CTR_DRBG_Generate with derivation function (SP 800-90A §10.2.1.5.2) |
| 593 | * mbedtls_ctr_drbg_random_with_add(ctx, output, output_len, additional, add_len) |
| 594 | * implements |
| 595 | * CTR_DRBG_Reseed(working_state, entropy_input, additional[:add_len]) |
| 596 | * -> working_state_after_reseed |
| 597 | * if required, then |
| 598 | * CTR_DRBG_Generate(working_state_after_reseed, |
| 599 | * requested_number_of_bits, additional_input) |
| 600 | * -> status, returned_bits, new_working_state |
| 601 | * with inputs |
| 602 | * ctx contains working_state |
| 603 | * requested_number_of_bits = 8 * output_len |
| 604 | * additional[:add_len] = additional_input |
| 605 | * and entropy_input comes from calling ctx->f_entropy |
| 606 | * and with outputs |
| 607 | * status = SUCCESS (this function does the reseed internally) |
| 608 | * returned_bits = output[:output_len] |
| 609 | * ctx contains new_working_state |
| 610 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 611 | int mbedtls_ctr_drbg_random_with_add(void *p_rng, |
| 612 | unsigned char *output, size_t output_len, |
| 613 | const unsigned char *additional, size_t add_len) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 614 | { |
| 615 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 616 | mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng; |
| 617 | unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN]; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 618 | unsigned char *p = output; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 619 | unsigned char tmp[MBEDTLS_CTR_DRBG_BLOCKSIZE]; |
Paul Bakker | 369e14b | 2012-04-18 14:16:09 +0000 | [diff] [blame] | 620 | int i; |
Paul Bakker | 23fd5ea | 2011-11-29 15:56:12 +0000 | [diff] [blame] | 621 | size_t use_len; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 623 | if (output_len > MBEDTLS_CTR_DRBG_MAX_REQUEST) { |
| 624 | return MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG; |
| 625 | } |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 627 | if (add_len > MBEDTLS_CTR_DRBG_MAX_INPUT) { |
| 628 | return MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG; |
| 629 | } |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 631 | memset(add_input, 0, MBEDTLS_CTR_DRBG_SEEDLEN); |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 633 | if (ctx->reseed_counter > ctx->reseed_interval || |
| 634 | ctx->prediction_resistance) { |
| 635 | if ((ret = mbedtls_ctr_drbg_reseed(ctx, additional, add_len)) != 0) { |
| 636 | return ret; |
Dvir Markovich | 1b36499 | 2017-06-26 13:43:34 +0300 | [diff] [blame] | 637 | } |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 638 | add_len = 0; |
| 639 | } |
| 640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 641 | if (add_len > 0) { |
| 642 | if ((ret = block_cipher_df(add_input, additional, add_len)) != 0) { |
Gilles Peskine | d9aa84d | 2018-09-11 15:34:17 +0200 | [diff] [blame] | 643 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 644 | } |
| 645 | if ((ret = ctr_drbg_update_internal(ctx, add_input)) != 0) { |
Gilles Peskine | d9aa84d | 2018-09-11 15:34:17 +0200 | [diff] [blame] | 646 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 647 | } |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 650 | while (output_len > 0) { |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 651 | /* |
Dave Rodgman | 591ff05 | 2024-01-13 16:42:38 +0000 | [diff] [blame^] | 652 | * Increase counter (treat it as a 128-bit big-endian integer). |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 653 | */ |
Dave Rodgman | 591ff05 | 2024-01-13 16:42:38 +0000 | [diff] [blame^] | 654 | mbedtls_ctr_increment_counter(ctx->counter); |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 655 | |
| 656 | /* |
| 657 | * Crypt counter block |
| 658 | */ |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 659 | #if defined(MBEDTLS_AES_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 660 | if ((ret = mbedtls_aes_crypt_ecb(&ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, |
| 661 | ctx->counter, tmp)) != 0) { |
Gilles Peskine | d9aa84d | 2018-09-11 15:34:17 +0200 | [diff] [blame] | 662 | goto exit; |
Dvir Markovich | 1b36499 | 2017-06-26 13:43:34 +0300 | [diff] [blame] | 663 | } |
Valerio Setti | 5f4b28d | 2023-11-13 10:18:16 +0100 | [diff] [blame] | 664 | #else |
| 665 | psa_status_t status; |
| 666 | size_t tmp_len; |
| 667 | |
| 668 | status = psa_cipher_update(&ctx->psa_ctx.operation, ctx->counter, sizeof(ctx->counter), |
| 669 | tmp, MBEDTLS_CTR_DRBG_BLOCKSIZE, &tmp_len); |
| 670 | if (status != PSA_SUCCESS) { |
| 671 | ret = psa_generic_status_to_mbedtls(status); |
| 672 | goto exit; |
| 673 | } |
| 674 | #endif |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 676 | use_len = (output_len > MBEDTLS_CTR_DRBG_BLOCKSIZE) |
Hanno Becker | a08651f | 2018-10-05 09:38:59 +0100 | [diff] [blame] | 677 | ? MBEDTLS_CTR_DRBG_BLOCKSIZE : output_len; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 678 | /* |
| 679 | * Copy random block to destination |
| 680 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 681 | memcpy(p, tmp, use_len); |
Paul Bakker | 23fd5ea | 2011-11-29 15:56:12 +0000 | [diff] [blame] | 682 | p += use_len; |
| 683 | output_len -= use_len; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 686 | if ((ret = ctr_drbg_update_internal(ctx, add_input)) != 0) { |
Gilles Peskine | d9aa84d | 2018-09-11 15:34:17 +0200 | [diff] [blame] | 687 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 688 | } |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 689 | |
| 690 | ctx->reseed_counter++; |
| 691 | |
Gilles Peskine | d9aa84d | 2018-09-11 15:34:17 +0200 | [diff] [blame] | 692 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 693 | mbedtls_platform_zeroize(add_input, sizeof(add_input)); |
| 694 | mbedtls_platform_zeroize(tmp, sizeof(tmp)); |
| 695 | return ret; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 698 | int mbedtls_ctr_drbg_random(void *p_rng, unsigned char *output, |
| 699 | size_t output_len) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 700 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 701 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 0a4fb09 | 2015-05-07 12:50:31 +0100 | [diff] [blame] | 702 | mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng; |
| 703 | |
| 704 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 705 | if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) { |
| 706 | return ret; |
| 707 | } |
Manuel Pégourié-Gonnard | 0a4fb09 | 2015-05-07 12:50:31 +0100 | [diff] [blame] | 708 | #endif |
| 709 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 710 | ret = mbedtls_ctr_drbg_random_with_add(ctx, output, output_len, NULL, 0); |
Manuel Pégourié-Gonnard | 0a4fb09 | 2015-05-07 12:50:31 +0100 | [diff] [blame] | 711 | |
| 712 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 713 | if (mbedtls_mutex_unlock(&ctx->mutex) != 0) { |
| 714 | return MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
| 715 | } |
Manuel Pégourié-Gonnard | 0a4fb09 | 2015-05-07 12:50:31 +0100 | [diff] [blame] | 716 | #endif |
| 717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 718 | return ret; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 721 | #if defined(MBEDTLS_FS_IO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 722 | int mbedtls_ctr_drbg_write_seed_file(mbedtls_ctr_drbg_context *ctx, |
| 723 | const char *path) |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 724 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 725 | int ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR; |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 726 | FILE *f; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 727 | unsigned char buf[MBEDTLS_CTR_DRBG_MAX_INPUT]; |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 728 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 729 | if ((f = fopen(path, "wb")) == NULL) { |
| 730 | return MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR; |
| 731 | } |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 732 | |
Gilles Peskine | da0913b | 2022-06-30 17:03:40 +0200 | [diff] [blame] | 733 | /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 734 | mbedtls_setbuf(f, NULL); |
Gilles Peskine | da0913b | 2022-06-30 17:03:40 +0200 | [diff] [blame] | 735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 736 | if ((ret = mbedtls_ctr_drbg_random(ctx, buf, |
| 737 | MBEDTLS_CTR_DRBG_MAX_INPUT)) != 0) { |
Paul Bakker | c72d3f7 | 2013-05-14 13:22:41 +0200 | [diff] [blame] | 738 | goto exit; |
Hanno Becker | a08651f | 2018-10-05 09:38:59 +0100 | [diff] [blame] | 739 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 740 | |
| 741 | if (fwrite(buf, 1, MBEDTLS_CTR_DRBG_MAX_INPUT, f) != |
| 742 | MBEDTLS_CTR_DRBG_MAX_INPUT) { |
| 743 | ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR; |
| 744 | } else { |
Andres Amaya Garcia | 13f41e1 | 2017-06-26 10:56:58 +0100 | [diff] [blame] | 745 | ret = 0; |
Hanno Becker | a08651f | 2018-10-05 09:38:59 +0100 | [diff] [blame] | 746 | } |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 747 | |
Andres Amaya Garcia | 4e2c07c | 2017-06-27 16:57:26 +0100 | [diff] [blame] | 748 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 749 | mbedtls_platform_zeroize(buf, sizeof(buf)); |
Paul Bakker | c72d3f7 | 2013-05-14 13:22:41 +0200 | [diff] [blame] | 750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 751 | fclose(f); |
| 752 | return ret; |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 755 | int mbedtls_ctr_drbg_update_seed_file(mbedtls_ctr_drbg_context *ctx, |
| 756 | const char *path) |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 757 | { |
Andres Amaya Garcia | 13f41e1 | 2017-06-26 10:56:58 +0100 | [diff] [blame] | 758 | int ret = 0; |
Gilles Peskine | 8220466 | 2018-09-11 18:43:09 +0200 | [diff] [blame] | 759 | FILE *f = NULL; |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 760 | size_t n; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 761 | unsigned char buf[MBEDTLS_CTR_DRBG_MAX_INPUT]; |
Gilles Peskine | 8220466 | 2018-09-11 18:43:09 +0200 | [diff] [blame] | 762 | unsigned char c; |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 764 | if ((f = fopen(path, "rb")) == NULL) { |
| 765 | return MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR; |
| 766 | } |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 767 | |
Gilles Peskine | da0913b | 2022-06-30 17:03:40 +0200 | [diff] [blame] | 768 | /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 769 | mbedtls_setbuf(f, NULL); |
Gilles Peskine | da0913b | 2022-06-30 17:03:40 +0200 | [diff] [blame] | 770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 771 | n = fread(buf, 1, sizeof(buf), f); |
| 772 | if (fread(&c, 1, 1, f) != 0) { |
Gilles Peskine | 8220466 | 2018-09-11 18:43:09 +0200 | [diff] [blame] | 773 | ret = MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG; |
| 774 | goto exit; |
Andres Amaya Garcia | 4e2c07c | 2017-06-27 16:57:26 +0100 | [diff] [blame] | 775 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 776 | if (n == 0 || ferror(f)) { |
Andres Amaya Garcia | 13f41e1 | 2017-06-26 10:56:58 +0100 | [diff] [blame] | 777 | ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR; |
Gilles Peskine | 8220466 | 2018-09-11 18:43:09 +0200 | [diff] [blame] | 778 | goto exit; |
| 779 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 780 | fclose(f); |
Gilles Peskine | 8220466 | 2018-09-11 18:43:09 +0200 | [diff] [blame] | 781 | f = NULL; |
Paul Bakker | c72d3f7 | 2013-05-14 13:22:41 +0200 | [diff] [blame] | 782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 783 | ret = mbedtls_ctr_drbg_update(ctx, buf, n); |
Gilles Peskine | 8220466 | 2018-09-11 18:43:09 +0200 | [diff] [blame] | 784 | |
| 785 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 786 | mbedtls_platform_zeroize(buf, sizeof(buf)); |
| 787 | if (f != NULL) { |
| 788 | fclose(f); |
| 789 | } |
| 790 | if (ret != 0) { |
| 791 | return ret; |
| 792 | } |
| 793 | return mbedtls_ctr_drbg_write_seed_file(ctx, path); |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 794 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 795 | #endif /* MBEDTLS_FS_IO */ |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 796 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 797 | #if defined(MBEDTLS_SELF_TEST) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 798 | |
ENT\stroej1 | 70f63d0 | 2020-12-28 08:50:23 -0600 | [diff] [blame] | 799 | /* The CTR_DRBG NIST test vectors used here are available at |
| 800 | * https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/drbg/drbgtestvectors.zip |
| 801 | * |
| 802 | * The parameters used to derive the test data are: |
| 803 | * |
| 804 | * [AES-128 use df] |
| 805 | * [PredictionResistance = True/False] |
| 806 | * [EntropyInputLen = 128] |
| 807 | * [NonceLen = 64] |
| 808 | * [PersonalizationStringLen = 128] |
| 809 | * [AdditionalInputLen = 0] |
| 810 | * [ReturnedBitsLen = 512] |
| 811 | * |
| 812 | * [AES-256 use df] |
| 813 | * [PredictionResistance = True/False] |
| 814 | * [EntropyInputLen = 256] |
| 815 | * [NonceLen = 128] |
| 816 | * [PersonalizationStringLen = 256] |
| 817 | * [AdditionalInputLen = 0] |
| 818 | * [ReturnedBitsLen = 512] |
| 819 | * |
| 820 | */ |
| 821 | |
Gilles Peskine | 02e79a4 | 2019-10-07 17:06:06 +0200 | [diff] [blame] | 822 | #if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY) |
ENT\stroej1 | df30700 | 2020-12-26 12:41:04 -0600 | [diff] [blame] | 823 | static const unsigned char entropy_source_pr[] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 824 | { 0x04, 0xd9, 0x49, 0xa6, 0xdc, 0xe8, 0x6e, 0xbb, |
| 825 | 0xf1, 0x08, 0x77, 0x2b, 0x9e, 0x08, 0xca, 0x92, |
| 826 | 0x65, 0x16, 0xda, 0x99, 0xa2, 0x59, 0xf3, 0xe8, |
| 827 | 0x38, 0x7e, 0x3f, 0x6b, 0x51, 0x70, 0x7b, 0x20, |
| 828 | 0xec, 0x53, 0xd0, 0x66, 0xc3, 0x0f, 0xe3, 0xb0, |
| 829 | 0xe0, 0x86, 0xa6, 0xaa, 0x5f, 0x72, 0x2f, 0xad, |
| 830 | 0xf7, 0xef, 0x06, 0xb8, 0xd6, 0x9c, 0x9d, 0xe8 }; |
Gilles Peskine | 02e79a4 | 2019-10-07 17:06:06 +0200 | [diff] [blame] | 831 | |
ENT\stroej1 | df30700 | 2020-12-26 12:41:04 -0600 | [diff] [blame] | 832 | static const unsigned char entropy_source_nopr[] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 833 | { 0x07, 0x0d, 0x59, 0x63, 0x98, 0x73, 0xa5, 0x45, |
| 834 | 0x27, 0x38, 0x22, 0x7b, 0x76, 0x85, 0xd1, 0xa9, |
| 835 | 0x74, 0x18, 0x1f, 0x3c, 0x22, 0xf6, 0x49, 0x20, |
| 836 | 0x4a, 0x47, 0xc2, 0xf3, 0x85, 0x16, 0xb4, 0x6f, |
| 837 | 0x00, 0x2e, 0x71, 0xda, 0xed, 0x16, 0x9b, 0x5c }; |
ENT\stroej1 | df30700 | 2020-12-26 12:41:04 -0600 | [diff] [blame] | 838 | |
stroebeljc | d4de1b5 | 2021-01-04 18:14:32 -0600 | [diff] [blame] | 839 | static const unsigned char pers_pr[] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 840 | { 0xbf, 0xa4, 0x9a, 0x8f, 0x7b, 0xd8, 0xb1, 0x7a, |
| 841 | 0x9d, 0xfa, 0x45, 0xed, 0x21, 0x52, 0xb3, 0xad }; |
ENT\stroej1 | df30700 | 2020-12-26 12:41:04 -0600 | [diff] [blame] | 842 | |
stroebeljc | d4de1b5 | 2021-01-04 18:14:32 -0600 | [diff] [blame] | 843 | static const unsigned char pers_nopr[] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 844 | { 0x4e, 0x61, 0x79, 0xd4, 0xc2, 0x72, 0xa1, 0x4c, |
| 845 | 0xf1, 0x3d, 0xf6, 0x5e, 0xa3, 0xa6, 0xe5, 0x0f }; |
ENT\stroej1 | df30700 | 2020-12-26 12:41:04 -0600 | [diff] [blame] | 846 | |
| 847 | static const unsigned char result_pr[] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 848 | { 0xc9, 0x0a, 0xaf, 0x85, 0x89, 0x71, 0x44, 0x66, |
| 849 | 0x4f, 0x25, 0x0b, 0x2b, 0xde, 0xd8, 0xfa, 0xff, |
| 850 | 0x52, 0x5a, 0x1b, 0x32, 0x5e, 0x41, 0x7a, 0x10, |
| 851 | 0x1f, 0xef, 0x1e, 0x62, 0x23, 0xe9, 0x20, 0x30, |
| 852 | 0xc9, 0x0d, 0xad, 0x69, 0xb4, 0x9c, 0x5b, 0xf4, |
| 853 | 0x87, 0x42, 0xd5, 0xae, 0x5e, 0x5e, 0x43, 0xcc, |
| 854 | 0xd9, 0xfd, 0x0b, 0x93, 0x4a, 0xe3, 0xd4, 0x06, |
| 855 | 0x37, 0x36, 0x0f, 0x3f, 0x72, 0x82, 0x0c, 0xcf }; |
ENT\stroej1 | df30700 | 2020-12-26 12:41:04 -0600 | [diff] [blame] | 856 | |
| 857 | static const unsigned char result_nopr[] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 858 | { 0x31, 0xc9, 0x91, 0x09, 0xf8, 0xc5, 0x10, 0x13, |
| 859 | 0x3c, 0xd3, 0x96, 0xf9, 0xbc, 0x2c, 0x12, 0xc0, |
| 860 | 0x7c, 0xc1, 0x61, 0x5f, 0xa3, 0x09, 0x99, 0xaf, |
| 861 | 0xd7, 0xf2, 0x36, 0xfd, 0x40, 0x1a, 0x8b, 0xf2, |
| 862 | 0x33, 0x38, 0xee, 0x1d, 0x03, 0x5f, 0x83, 0xb7, |
| 863 | 0xa2, 0x53, 0xdc, 0xee, 0x18, 0xfc, 0xa7, 0xf2, |
| 864 | 0xee, 0x96, 0xc6, 0xc2, 0xcd, 0x0c, 0xff, 0x02, |
| 865 | 0x76, 0x70, 0x69, 0xaa, 0x69, 0xd1, 0x3b, 0xe8 }; |
Gilles Peskine | 02e79a4 | 2019-10-07 17:06:06 +0200 | [diff] [blame] | 866 | #else /* MBEDTLS_CTR_DRBG_USE_128_BIT_KEY */ |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 867 | |
ENT\stroej1 | df30700 | 2020-12-26 12:41:04 -0600 | [diff] [blame] | 868 | static const unsigned char entropy_source_pr[] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 869 | { 0xca, 0x58, 0xfd, 0xf2, 0xb9, 0x77, 0xcb, 0x49, |
| 870 | 0xd4, 0xe0, 0x5b, 0xe2, 0x39, 0x50, 0xd9, 0x8a, |
| 871 | 0x6a, 0xb3, 0xc5, 0x2f, 0xdf, 0x74, 0xd5, 0x85, |
| 872 | 0x8f, 0xd1, 0xba, 0x64, 0x54, 0x7b, 0xdb, 0x1e, |
| 873 | 0xc5, 0xea, 0x24, 0xc0, 0xfa, 0x0c, 0x90, 0x15, |
| 874 | 0x09, 0x20, 0x92, 0x42, 0x32, 0x36, 0x45, 0x45, |
| 875 | 0x7d, 0x20, 0x76, 0x6b, 0xcf, 0xa2, 0x15, 0xc8, |
| 876 | 0x2f, 0x9f, 0xbc, 0x88, 0x3f, 0x80, 0xd1, 0x2c, |
| 877 | 0xb7, 0x16, 0xd1, 0x80, 0x9e, 0xe1, 0xc9, 0xb3, |
| 878 | 0x88, 0x1b, 0x21, 0x45, 0xef, 0xa1, 0x7f, 0xce, |
| 879 | 0xc8, 0x92, 0x35, 0x55, 0x2a, 0xd9, 0x1d, 0x8e, |
| 880 | 0x12, 0x38, 0xac, 0x01, 0x4e, 0x38, 0x18, 0x76, |
| 881 | 0x9c, 0xf2, 0xb6, 0xd4, 0x13, 0xb6, 0x2c, 0x77, |
| 882 | 0xc0, 0xe7, 0xe6, 0x0c, 0x47, 0x44, 0x95, 0xbe }; |
ENT\stroej1 | df30700 | 2020-12-26 12:41:04 -0600 | [diff] [blame] | 883 | |
| 884 | static const unsigned char entropy_source_nopr[] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 885 | { 0x4c, 0xfb, 0x21, 0x86, 0x73, 0x34, 0x6d, 0x9d, |
| 886 | 0x50, 0xc9, 0x22, 0xe4, 0x9b, 0x0d, 0xfc, 0xd0, |
| 887 | 0x90, 0xad, 0xf0, 0x4f, 0x5c, 0x3b, 0xa4, 0x73, |
| 888 | 0x27, 0xdf, 0xcd, 0x6f, 0xa6, 0x3a, 0x78, 0x5c, |
| 889 | 0x01, 0x69, 0x62, 0xa7, 0xfd, 0x27, 0x87, 0xa2, |
| 890 | 0x4b, 0xf6, 0xbe, 0x47, 0xef, 0x37, 0x83, 0xf1, |
| 891 | 0xb7, 0xec, 0x46, 0x07, 0x23, 0x63, 0x83, 0x4a, |
| 892 | 0x1b, 0x01, 0x33, 0xf2, 0xc2, 0x38, 0x91, 0xdb, |
| 893 | 0x4f, 0x11, 0xa6, 0x86, 0x51, 0xf2, 0x3e, 0x3a, |
| 894 | 0x8b, 0x1f, 0xdc, 0x03, 0xb1, 0x92, 0xc7, 0xe7 }; |
ENT\stroej1 | df30700 | 2020-12-26 12:41:04 -0600 | [diff] [blame] | 895 | |
stroebeljc | d4de1b5 | 2021-01-04 18:14:32 -0600 | [diff] [blame] | 896 | static const unsigned char pers_pr[] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 897 | { 0x5a, 0x70, 0x95, 0xe9, 0x81, 0x40, 0x52, 0x33, |
| 898 | 0x91, 0x53, 0x7e, 0x75, 0xd6, 0x19, 0x9d, 0x1e, |
| 899 | 0xad, 0x0d, 0xc6, 0xa7, 0xde, 0x6c, 0x1f, 0xe0, |
| 900 | 0xea, 0x18, 0x33, 0xa8, 0x7e, 0x06, 0x20, 0xe9 }; |
ENT\stroej1 | df30700 | 2020-12-26 12:41:04 -0600 | [diff] [blame] | 901 | |
stroebeljc | d4de1b5 | 2021-01-04 18:14:32 -0600 | [diff] [blame] | 902 | static const unsigned char pers_nopr[] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 903 | { 0x88, 0xee, 0xb8, 0xe0, 0xe8, 0x3b, 0xf3, 0x29, |
| 904 | 0x4b, 0xda, 0xcd, 0x60, 0x99, 0xeb, 0xe4, 0xbf, |
| 905 | 0x55, 0xec, 0xd9, 0x11, 0x3f, 0x71, 0xe5, 0xeb, |
| 906 | 0xcb, 0x45, 0x75, 0xf3, 0xd6, 0xa6, 0x8a, 0x6b }; |
ENT\stroej1 | df30700 | 2020-12-26 12:41:04 -0600 | [diff] [blame] | 907 | |
| 908 | static const unsigned char result_pr[] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 909 | { 0xce, 0x2f, 0xdb, 0xb6, 0xd9, 0xb7, 0x39, 0x85, |
| 910 | 0x04, 0xc5, 0xc0, 0x42, 0xc2, 0x31, 0xc6, 0x1d, |
| 911 | 0x9b, 0x5a, 0x59, 0xf8, 0x7e, 0x0d, 0xcc, 0x62, |
| 912 | 0x7b, 0x65, 0x11, 0x55, 0x10, 0xeb, 0x9e, 0x3d, |
| 913 | 0xa4, 0xfb, 0x1c, 0x6a, 0x18, 0xc0, 0x74, 0xdb, |
| 914 | 0xdd, 0xe7, 0x02, 0x23, 0x63, 0x21, 0xd0, 0x39, |
| 915 | 0xf9, 0xa7, 0xc4, 0x52, 0x84, 0x3b, 0x49, 0x40, |
| 916 | 0x72, 0x2b, 0xb0, 0x6c, 0x9c, 0xdb, 0xc3, 0x43 }; |
ENT\stroej1 | df30700 | 2020-12-26 12:41:04 -0600 | [diff] [blame] | 917 | |
| 918 | static const unsigned char result_nopr[] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 919 | { 0xa5, 0x51, 0x80, 0xa1, 0x90, 0xbe, 0xf3, 0xad, |
| 920 | 0xaf, 0x28, 0xf6, 0xb7, 0x95, 0xe9, 0xf1, 0xf3, |
| 921 | 0xd6, 0xdf, 0xa1, 0xb2, 0x7d, 0xd0, 0x46, 0x7b, |
| 922 | 0x0c, 0x75, 0xf5, 0xfa, 0x93, 0x1e, 0x97, 0x14, |
| 923 | 0x75, 0xb2, 0x7c, 0xae, 0x03, 0xa2, 0x96, 0x54, |
| 924 | 0xe2, 0xf4, 0x09, 0x66, 0xea, 0x33, 0x64, 0x30, |
| 925 | 0x40, 0xd1, 0x40, 0x0f, 0xe6, 0x77, 0x87, 0x3a, |
| 926 | 0xf8, 0x09, 0x7c, 0x1f, 0xe9, 0xf0, 0x02, 0x98 }; |
Gilles Peskine | 02e79a4 | 2019-10-07 17:06:06 +0200 | [diff] [blame] | 927 | #endif /* MBEDTLS_CTR_DRBG_USE_128_BIT_KEY */ |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 928 | |
Manuel Pégourié-Gonnard | 9592485 | 2014-03-21 10:54:55 +0100 | [diff] [blame] | 929 | static size_t test_offset; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 930 | static int ctr_drbg_self_test_entropy(void *data, unsigned char *buf, |
| 931 | size_t len) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 932 | { |
Manuel Pégourié-Gonnard | b3b205e | 2014-01-31 12:04:06 +0100 | [diff] [blame] | 933 | const unsigned char *p = data; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 934 | memcpy(buf, p + test_offset, len); |
Manuel Pégourié-Gonnard | b3b205e | 2014-01-31 12:04:06 +0100 | [diff] [blame] | 935 | test_offset += len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 936 | return 0; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 939 | #define CHK(c) if ((c) != 0) \ |
| 940 | { \ |
| 941 | if (verbose != 0) \ |
| 942 | mbedtls_printf("failed\n"); \ |
| 943 | return 1; \ |
| 944 | } |
Manuel Pégourié-Gonnard | b3b205e | 2014-01-31 12:04:06 +0100 | [diff] [blame] | 945 | |
bootstrap-prime | 6dbbf44 | 2022-05-17 19:30:44 -0400 | [diff] [blame] | 946 | #define SELF_TEST_OUTPUT_DISCARD_LENGTH 64 |
stroebeljc | d4de1b5 | 2021-01-04 18:14:32 -0600 | [diff] [blame] | 947 | |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 948 | /* |
| 949 | * Checkup routine |
| 950 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 951 | int mbedtls_ctr_drbg_self_test(int verbose) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 952 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 953 | mbedtls_ctr_drbg_context ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 954 | unsigned char buf[sizeof(result_pr)]; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 956 | mbedtls_ctr_drbg_init(&ctx); |
Manuel Pégourié-Gonnard | 8d128ef | 2015-04-28 22:38:08 +0200 | [diff] [blame] | 957 | |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 958 | /* |
| 959 | * Based on a NIST CTR_DRBG test vector (PR = True) |
| 960 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 961 | if (verbose != 0) { |
| 962 | mbedtls_printf(" CTR_DRBG (PR = TRUE) : "); |
| 963 | } |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 964 | |
| 965 | test_offset = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 966 | mbedtls_ctr_drbg_set_entropy_len(&ctx, MBEDTLS_CTR_DRBG_KEYSIZE); |
| 967 | mbedtls_ctr_drbg_set_nonce_len(&ctx, MBEDTLS_CTR_DRBG_KEYSIZE / 2); |
| 968 | CHK(mbedtls_ctr_drbg_seed(&ctx, |
| 969 | ctr_drbg_self_test_entropy, |
| 970 | (void *) entropy_source_pr, |
| 971 | pers_pr, MBEDTLS_CTR_DRBG_KEYSIZE)); |
| 972 | mbedtls_ctr_drbg_set_prediction_resistance(&ctx, MBEDTLS_CTR_DRBG_PR_ON); |
| 973 | CHK(mbedtls_ctr_drbg_random(&ctx, buf, SELF_TEST_OUTPUT_DISCARD_LENGTH)); |
| 974 | CHK(mbedtls_ctr_drbg_random(&ctx, buf, sizeof(result_pr))); |
| 975 | CHK(memcmp(buf, result_pr, sizeof(result_pr))); |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 976 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 977 | mbedtls_ctr_drbg_free(&ctx); |
Manuel Pégourié-Gonnard | 0a4fb09 | 2015-05-07 12:50:31 +0100 | [diff] [blame] | 978 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 979 | if (verbose != 0) { |
| 980 | mbedtls_printf("passed\n"); |
| 981 | } |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 982 | |
| 983 | /* |
| 984 | * Based on a NIST CTR_DRBG test vector (PR = FALSE) |
| 985 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 986 | if (verbose != 0) { |
| 987 | mbedtls_printf(" CTR_DRBG (PR = FALSE): "); |
| 988 | } |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 989 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 990 | mbedtls_ctr_drbg_init(&ctx); |
Manuel Pégourié-Gonnard | 0a4fb09 | 2015-05-07 12:50:31 +0100 | [diff] [blame] | 991 | |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 992 | test_offset = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 993 | mbedtls_ctr_drbg_set_entropy_len(&ctx, MBEDTLS_CTR_DRBG_KEYSIZE); |
| 994 | mbedtls_ctr_drbg_set_nonce_len(&ctx, MBEDTLS_CTR_DRBG_KEYSIZE / 2); |
| 995 | CHK(mbedtls_ctr_drbg_seed(&ctx, |
| 996 | ctr_drbg_self_test_entropy, |
| 997 | (void *) entropy_source_nopr, |
| 998 | pers_nopr, MBEDTLS_CTR_DRBG_KEYSIZE)); |
| 999 | CHK(mbedtls_ctr_drbg_reseed(&ctx, NULL, 0)); |
| 1000 | CHK(mbedtls_ctr_drbg_random(&ctx, buf, SELF_TEST_OUTPUT_DISCARD_LENGTH)); |
| 1001 | CHK(mbedtls_ctr_drbg_random(&ctx, buf, sizeof(result_nopr))); |
| 1002 | CHK(memcmp(buf, result_nopr, sizeof(result_nopr))); |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 1003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1004 | mbedtls_ctr_drbg_free(&ctx); |
Manuel Pégourié-Gonnard | 0a4fb09 | 2015-05-07 12:50:31 +0100 | [diff] [blame] | 1005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1006 | if (verbose != 0) { |
| 1007 | mbedtls_printf("passed\n"); |
| 1008 | } |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 1009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1010 | if (verbose != 0) { |
| 1011 | mbedtls_printf("\n"); |
| 1012 | } |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 1013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1014 | return 0; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 1015 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1016 | #endif /* MBEDTLS_SELF_TEST */ |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 1017 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1018 | #endif /* MBEDTLS_CTR_DRBG_C */ |