ctr_drbg.c 18 KB

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