Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  *  HMAC_DRBG implementation (NIST SP 800-90)
  3.  *
  4.  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5.  *  SPDX-License-Identifier: GPL-2.0
  6.  *
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 2 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License along
  18.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  19.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20.  *
  21.  *  This file is part of mbed TLS (https://tls.mbed.org)
  22.  */
  23.  
  24. /*
  25.  *  The NIST SP 800-90A DRBGs are described in the following publication.
  26.  *  http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf
  27.  *  References below are based on rev. 1 (January 2012).
  28.  */
  29.  
  30. #if !defined(MBEDTLS_CONFIG_FILE)
  31. #include "mbedtls/config.h"
  32. #else
  33. #include MBEDTLS_CONFIG_FILE
  34. #endif
  35.  
  36. #if defined(MBEDTLS_HMAC_DRBG_C)
  37.  
  38. #include "mbedtls/hmac_drbg.h"
  39. #include "mbedtls/platform_util.h"
  40.  
  41. #include <string.h>
  42.  
  43. #if defined(MBEDTLS_FS_IO)
  44. #include <stdio.h>
  45. #endif
  46.  
  47. #if defined(MBEDTLS_SELF_TEST)
  48. #if defined(MBEDTLS_PLATFORM_C)
  49. #include "mbedtls/platform.h"
  50. #else
  51. #include <stdio.h>
  52. #define mbedtls_printf printf
  53. #endif /* MBEDTLS_SELF_TEST */
  54. #endif /* MBEDTLS_PLATFORM_C */
  55.  
  56. /*
  57.  * HMAC_DRBG context initialization
  58.  */
  59. void mbedtls_hmac_drbg_init( mbedtls_hmac_drbg_context *ctx )
  60. {
  61.     memset( ctx, 0, sizeof( mbedtls_hmac_drbg_context ) );
  62.  
  63. #if defined(MBEDTLS_THREADING_C)
  64.     mbedtls_mutex_init( &ctx->mutex );
  65. #endif
  66. }
  67.  
  68. /*
  69.  * HMAC_DRBG update, using optional additional data (10.1.2.2)
  70.  */
  71. int mbedtls_hmac_drbg_update_ret( mbedtls_hmac_drbg_context *ctx,
  72.                                   const unsigned char *additional,
  73.                                   size_t add_len )
  74. {
  75.     size_t md_len = mbedtls_md_get_size( ctx->md_ctx.md_info );
  76.     unsigned char rounds = ( additional != NULL && add_len != 0 ) ? 2 : 1;
  77.     unsigned char sep[1];
  78.     unsigned char K[MBEDTLS_MD_MAX_SIZE];
  79.     int ret;
  80.  
  81.     for( sep[0] = 0; sep[0] < rounds; sep[0]++ )
  82.     {
  83.         /* Step 1 or 4 */
  84.         if( ( ret = mbedtls_md_hmac_reset( &ctx->md_ctx ) ) != 0 )
  85.             goto exit;
  86.         if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx,
  87.                                             ctx->V, md_len ) ) != 0 )
  88.             goto exit;
  89.         if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx,
  90.                                             sep, 1 ) ) != 0 )
  91.             goto exit;
  92.         if( rounds == 2 )
  93.         {
  94.             if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx,
  95.                                                 additional, add_len ) ) != 0 )
  96.             goto exit;
  97.         }
  98.         if( ( ret = mbedtls_md_hmac_finish( &ctx->md_ctx, K ) ) != 0 )
  99.             goto exit;
  100.  
  101.         /* Step 2 or 5 */
  102.         if( ( ret = mbedtls_md_hmac_starts( &ctx->md_ctx, K, md_len ) ) != 0 )
  103.             goto exit;
  104.         if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx,
  105.                                             ctx->V, md_len ) ) != 0 )
  106.             goto exit;
  107.         if( ( ret = mbedtls_md_hmac_finish( &ctx->md_ctx, ctx->V ) ) != 0 )
  108.             goto exit;
  109.     }
  110.  
  111. exit:
  112.     mbedtls_platform_zeroize( K, sizeof( K ) );
  113.     return( ret );
  114. }
  115.  
  116. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  117. void mbedtls_hmac_drbg_update( mbedtls_hmac_drbg_context *ctx,
  118.                                const unsigned char *additional,
  119.                                size_t add_len )
  120. {
  121.     (void) mbedtls_hmac_drbg_update_ret( ctx, additional, add_len );
  122. }
  123. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  124.  
  125. /*
  126.  * Simplified HMAC_DRBG initialisation (for use with deterministic ECDSA)
  127.  */
  128. int mbedtls_hmac_drbg_seed_buf( mbedtls_hmac_drbg_context *ctx,
  129.                         const mbedtls_md_info_t * md_info,
  130.                         const unsigned char *data, size_t data_len )
  131. {
  132.     int ret;
  133.  
  134.     if( ( ret = mbedtls_md_setup( &ctx->md_ctx, md_info, 1 ) ) != 0 )
  135.         return( ret );
  136.  
  137.     /*
  138.      * Set initial working state.
  139.      * Use the V memory location, which is currently all 0, to initialize the
  140.      * MD context with an all-zero key. Then set V to its initial value.
  141.      */
  142.     if( ( ret = mbedtls_md_hmac_starts( &ctx->md_ctx, ctx->V,
  143.                                         mbedtls_md_get_size( md_info ) ) ) != 0 )
  144.         return( ret );
  145.     memset( ctx->V, 0x01, mbedtls_md_get_size( md_info ) );
  146.  
  147.     if( ( ret = mbedtls_hmac_drbg_update_ret( ctx, data, data_len ) ) != 0 )
  148.         return( ret );
  149.  
  150.     return( 0 );
  151. }
  152.  
  153. /*
  154.  * Internal function used both for seeding and reseeding the DRBG.
  155.  * Comments starting with arabic numbers refer to section 10.1.2.4
  156.  * of SP800-90A, while roman numbers refer to section 9.2.
  157.  */
  158. static int hmac_drbg_reseed_core( mbedtls_hmac_drbg_context *ctx,
  159.                                   const unsigned char *additional, size_t len,
  160.                                   int use_nonce )
  161. {
  162.     unsigned char seed[MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT];
  163.     size_t seedlen = 0;
  164.     int ret;
  165.  
  166.     {
  167.         size_t total_entropy_len;
  168.  
  169.         if( use_nonce == 0 )
  170.             total_entropy_len = ctx->entropy_len;
  171.         else
  172.             total_entropy_len = ctx->entropy_len * 3 / 2;
  173.  
  174.         /* III. Check input length */
  175.         if( len > MBEDTLS_HMAC_DRBG_MAX_INPUT ||
  176.             total_entropy_len + len > MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT )
  177.         {
  178.             return( MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG );
  179.         }
  180.     }
  181.  
  182.     memset( seed, 0, MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT );
  183.  
  184.     /* IV. Gather entropy_len bytes of entropy for the seed */
  185.     if( ( ret = ctx->f_entropy( ctx->p_entropy,
  186.                                 seed, ctx->entropy_len ) ) != 0 )
  187.     {
  188.         return( MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED );
  189.     }
  190.     seedlen += ctx->entropy_len;
  191.  
  192.     /* For initial seeding, allow adding of nonce generated
  193.      * from the entropy source. See Sect 8.6.7 in SP800-90A. */
  194.     if( use_nonce )
  195.     {
  196.         /* Note: We don't merge the two calls to f_entropy() in order
  197.          *       to avoid requesting too much entropy from f_entropy()
  198.          *       at once. Specifically, if the underlying digest is not
  199.          *       SHA-1, 3 / 2 * entropy_len is at least 36 Bytes, which
  200.          *       is larger than the maximum of 32 Bytes that our own
  201.          *       entropy source implementation can emit in a single
  202.          *       call in configurations disabling SHA-512. */
  203.         if( ( ret = ctx->f_entropy( ctx->p_entropy,
  204.                                     seed + seedlen,
  205.                                     ctx->entropy_len / 2 ) ) != 0 )
  206.         {
  207.             return( MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED );
  208.         }
  209.  
  210.         seedlen += ctx->entropy_len / 2;
  211.     }
  212.  
  213.  
  214.     /* 1. Concatenate entropy and additional data if any */
  215.     if( additional != NULL && len != 0 )
  216.     {
  217.         memcpy( seed + seedlen, additional, len );
  218.         seedlen += len;
  219.     }
  220.  
  221.     /* 2. Update state */
  222.     if( ( ret = mbedtls_hmac_drbg_update_ret( ctx, seed, seedlen ) ) != 0 )
  223.         goto exit;
  224.  
  225.     /* 3. Reset reseed_counter */
  226.     ctx->reseed_counter = 1;
  227.  
  228. exit:
  229.     /* 4. Done */
  230.     mbedtls_platform_zeroize( seed, seedlen );
  231.     return( ret );
  232. }
  233.  
  234. /*
  235.  * HMAC_DRBG reseeding: 10.1.2.4 + 9.2
  236.  */
  237. int mbedtls_hmac_drbg_reseed( mbedtls_hmac_drbg_context *ctx,
  238.                       const unsigned char *additional, size_t len )
  239. {
  240.     return( hmac_drbg_reseed_core( ctx, additional, len, 0 ) );
  241. }
  242.  
  243. /*
  244.  * HMAC_DRBG initialisation (10.1.2.3 + 9.1)
  245.  *
  246.  * The nonce is not passed as a separate parameter but extracted
  247.  * from the entropy source as suggested in 8.6.7.
  248.  */
  249. int mbedtls_hmac_drbg_seed( mbedtls_hmac_drbg_context *ctx,
  250.                     const mbedtls_md_info_t * md_info,
  251.                     int (*f_entropy)(void *, unsigned char *, size_t),
  252.                     void *p_entropy,
  253.                     const unsigned char *custom,
  254.                     size_t len )
  255. {
  256.     int ret;
  257.     size_t md_size;
  258.  
  259.     if( ( ret = mbedtls_md_setup( &ctx->md_ctx, md_info, 1 ) ) != 0 )
  260.         return( ret );
  261.  
  262.     md_size = mbedtls_md_get_size( md_info );
  263.  
  264.     /*
  265.      * Set initial working state.
  266.      * Use the V memory location, which is currently all 0, to initialize the
  267.      * MD context with an all-zero key. Then set V to its initial value.
  268.      */
  269.     if( ( ret = mbedtls_md_hmac_starts( &ctx->md_ctx, ctx->V, md_size ) ) != 0 )
  270.         return( ret );
  271.     memset( ctx->V, 0x01, md_size );
  272.  
  273.     ctx->f_entropy = f_entropy;
  274.     ctx->p_entropy = p_entropy;
  275.  
  276.     ctx->reseed_interval = MBEDTLS_HMAC_DRBG_RESEED_INTERVAL;
  277.  
  278.     if( ctx->entropy_len == 0 )
  279.     {
  280.         /*
  281.          * See SP800-57 5.6.1 (p. 65-66) for the security strength provided by
  282.          * each hash function, then according to SP800-90A rev1 10.1 table 2,
  283.          * min_entropy_len (in bits) is security_strength.
  284.          *
  285.          * (This also matches the sizes used in the NIST test vectors.)
  286.          */
  287.         ctx->entropy_len = md_size <= 20 ? 16 : /* 160-bits hash -> 128 bits */
  288.                            md_size <= 28 ? 24 : /* 224-bits hash -> 192 bits */
  289.                            32;  /* better (256+) -> 256 bits */
  290.     }
  291.  
  292.     if( ( ret = hmac_drbg_reseed_core( ctx, custom, len,
  293.                                        1 /* add nonce */ ) ) != 0 )
  294.     {
  295.         return( ret );
  296.     }
  297.  
  298.     return( 0 );
  299. }
  300.  
  301. /*
  302.  * Set prediction resistance
  303.  */
  304. void mbedtls_hmac_drbg_set_prediction_resistance( mbedtls_hmac_drbg_context *ctx,
  305.                                           int resistance )
  306. {
  307.     ctx->prediction_resistance = resistance;
  308. }
  309.  
  310. /*
  311.  * Set entropy length grabbed for seeding
  312.  */
  313. void mbedtls_hmac_drbg_set_entropy_len( mbedtls_hmac_drbg_context *ctx, size_t len )
  314. {
  315.     ctx->entropy_len = len;
  316. }
  317.  
  318. /*
  319.  * Set reseed interval
  320.  */
  321. void mbedtls_hmac_drbg_set_reseed_interval( mbedtls_hmac_drbg_context *ctx, int interval )
  322. {
  323.     ctx->reseed_interval = interval;
  324. }
  325.  
  326. /*
  327.  * HMAC_DRBG random function with optional additional data:
  328.  * 10.1.2.5 (arabic) + 9.3 (Roman)
  329.  */
  330. int mbedtls_hmac_drbg_random_with_add( void *p_rng,
  331.                                unsigned char *output, size_t out_len,
  332.                                const unsigned char *additional, size_t add_len )
  333. {
  334.     int ret;
  335.     mbedtls_hmac_drbg_context *ctx = (mbedtls_hmac_drbg_context *) p_rng;
  336.     size_t md_len = mbedtls_md_get_size( ctx->md_ctx.md_info );
  337.     size_t left = out_len;
  338.     unsigned char *out = output;
  339.  
  340.     /* II. Check request length */
  341.     if( out_len > MBEDTLS_HMAC_DRBG_MAX_REQUEST )
  342.         return( MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG );
  343.  
  344.     /* III. Check input length */
  345.     if( add_len > MBEDTLS_HMAC_DRBG_MAX_INPUT )
  346.         return( MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG );
  347.  
  348.     /* 1. (aka VII and IX) Check reseed counter and PR */
  349.     if( ctx->f_entropy != NULL && /* For no-reseeding instances */
  350.         ( ctx->prediction_resistance == MBEDTLS_HMAC_DRBG_PR_ON ||
  351.           ctx->reseed_counter > ctx->reseed_interval ) )
  352.     {
  353.         if( ( ret = mbedtls_hmac_drbg_reseed( ctx, additional, add_len ) ) != 0 )
  354.             return( ret );
  355.  
  356.         add_len = 0; /* VII.4 */
  357.     }
  358.  
  359.     /* 2. Use additional data if any */
  360.     if( additional != NULL && add_len != 0 )
  361.     {
  362.         if( ( ret = mbedtls_hmac_drbg_update_ret( ctx,
  363.                                                   additional, add_len ) ) != 0 )
  364.             goto exit;
  365.     }
  366.  
  367.     /* 3, 4, 5. Generate bytes */
  368.     while( left != 0 )
  369.     {
  370.         size_t use_len = left > md_len ? md_len : left;
  371.  
  372.         if( ( ret = mbedtls_md_hmac_reset( &ctx->md_ctx ) ) != 0 )
  373.             goto exit;
  374.         if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx,
  375.                                             ctx->V, md_len ) ) != 0 )
  376.             goto exit;
  377.         if( ( ret = mbedtls_md_hmac_finish( &ctx->md_ctx, ctx->V ) ) != 0 )
  378.             goto exit;
  379.  
  380.         memcpy( out, ctx->V, use_len );
  381.         out += use_len;
  382.         left -= use_len;
  383.     }
  384.  
  385.     /* 6. Update */
  386.     if( ( ret = mbedtls_hmac_drbg_update_ret( ctx,
  387.                                               additional, add_len ) ) != 0 )
  388.         goto exit;
  389.  
  390.     /* 7. Update reseed counter */
  391.     ctx->reseed_counter++;
  392.  
  393. exit:
  394.     /* 8. Done */
  395.     return( ret );
  396. }
  397.  
  398. /*
  399.  * HMAC_DRBG random function
  400.  */
  401. int mbedtls_hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len )
  402. {
  403.     int ret;
  404.     mbedtls_hmac_drbg_context *ctx = (mbedtls_hmac_drbg_context *) p_rng;
  405.  
  406. #if defined(MBEDTLS_THREADING_C)
  407.     if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  408.         return( ret );
  409. #endif
  410.  
  411.     ret = mbedtls_hmac_drbg_random_with_add( ctx, output, out_len, NULL, 0 );
  412.  
  413. #if defined(MBEDTLS_THREADING_C)
  414.     if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  415.         return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  416. #endif
  417.  
  418.     return( ret );
  419. }
  420.  
  421. /*
  422.  * Free an HMAC_DRBG context
  423.  */
  424. void mbedtls_hmac_drbg_free( mbedtls_hmac_drbg_context *ctx )
  425. {
  426.     if( ctx == NULL )
  427.         return;
  428.  
  429. #if defined(MBEDTLS_THREADING_C)
  430.     mbedtls_mutex_free( &ctx->mutex );
  431. #endif
  432.     mbedtls_md_free( &ctx->md_ctx );
  433.     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_hmac_drbg_context ) );
  434. }
  435.  
  436. #if defined(MBEDTLS_FS_IO)
  437. int mbedtls_hmac_drbg_write_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path )
  438. {
  439.     int ret;
  440.     FILE *f;
  441.     unsigned char buf[ MBEDTLS_HMAC_DRBG_MAX_INPUT ];
  442.  
  443.     if( ( f = fopen( path, "wb" ) ) == NULL )
  444.         return( MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR );
  445.  
  446.     if( ( ret = mbedtls_hmac_drbg_random( ctx, buf, sizeof( buf ) ) ) != 0 )
  447.         goto exit;
  448.  
  449.     if( fwrite( buf, 1, sizeof( buf ), f ) != sizeof( buf ) )
  450.     {
  451.         ret = MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR;
  452.         goto exit;
  453.     }
  454.  
  455.     ret = 0;
  456.  
  457. exit:
  458.     fclose( f );
  459.     mbedtls_platform_zeroize( buf, sizeof( buf ) );
  460.  
  461.     return( ret );
  462. }
  463.  
  464. int mbedtls_hmac_drbg_update_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path )
  465. {
  466.     int ret = 0;
  467.     FILE *f = NULL;
  468.     size_t n;
  469.     unsigned char buf[ MBEDTLS_HMAC_DRBG_MAX_INPUT ];
  470.     unsigned char c;
  471.  
  472.     if( ( f = fopen( path, "rb" ) ) == NULL )
  473.         return( MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR );
  474.  
  475.     n = fread( buf, 1, sizeof( buf ), f );
  476.     if( fread( &c, 1, 1, f ) != 0 )
  477.     {
  478.         ret = MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG;
  479.         goto exit;
  480.     }
  481.     if( n == 0 || ferror( f ) )
  482.     {
  483.         ret = MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR;
  484.         goto exit;
  485.     }
  486.     fclose( f );
  487.     f = NULL;
  488.  
  489.     ret = mbedtls_hmac_drbg_update_ret( ctx, buf, n );
  490.  
  491. exit:
  492.     mbedtls_platform_zeroize( buf, sizeof( buf ) );
  493.     if( f != NULL )
  494.         fclose( f );
  495.     if( ret != 0 )
  496.         return( ret );
  497.     return( mbedtls_hmac_drbg_write_seed_file( ctx, path ) );
  498. }
  499. #endif /* MBEDTLS_FS_IO */
  500.  
  501.  
  502. #if defined(MBEDTLS_SELF_TEST)
  503.  
  504. #if !defined(MBEDTLS_SHA1_C)
  505. /* Dummy checkup routine */
  506. int mbedtls_hmac_drbg_self_test( int verbose )
  507. {
  508.     (void) verbose;
  509.     return( 0 );
  510. }
  511. #else
  512.  
  513. #define OUTPUT_LEN  80
  514.  
  515. /* From a NIST PR=true test vector */
  516. static const unsigned char entropy_pr[] = {
  517.     0xa0, 0xc9, 0xab, 0x58, 0xf1, 0xe2, 0xe5, 0xa4, 0xde, 0x3e, 0xbd, 0x4f,
  518.     0xf7, 0x3e, 0x9c, 0x5b, 0x64, 0xef, 0xd8, 0xca, 0x02, 0x8c, 0xf8, 0x11,
  519.     0x48, 0xa5, 0x84, 0xfe, 0x69, 0xab, 0x5a, 0xee, 0x42, 0xaa, 0x4d, 0x42,
  520.     0x17, 0x60, 0x99, 0xd4, 0x5e, 0x13, 0x97, 0xdc, 0x40, 0x4d, 0x86, 0xa3,
  521.     0x7b, 0xf5, 0x59, 0x54, 0x75, 0x69, 0x51, 0xe4 };
  522. static const unsigned char result_pr[OUTPUT_LEN] = {
  523.     0x9a, 0x00, 0xa2, 0xd0, 0x0e, 0xd5, 0x9b, 0xfe, 0x31, 0xec, 0xb1, 0x39,
  524.     0x9b, 0x60, 0x81, 0x48, 0xd1, 0x96, 0x9d, 0x25, 0x0d, 0x3c, 0x1e, 0x94,
  525.     0x10, 0x10, 0x98, 0x12, 0x93, 0x25, 0xca, 0xb8, 0xfc, 0xcc, 0x2d, 0x54,
  526.     0x73, 0x19, 0x70, 0xc0, 0x10, 0x7a, 0xa4, 0x89, 0x25, 0x19, 0x95, 0x5e,
  527.     0x4b, 0xc6, 0x00, 0x1d, 0x7f, 0x4e, 0x6a, 0x2b, 0xf8, 0xa3, 0x01, 0xab,
  528.     0x46, 0x05, 0x5c, 0x09, 0xa6, 0x71, 0x88, 0xf1, 0xa7, 0x40, 0xee, 0xf3,
  529.     0xe1, 0x5c, 0x02, 0x9b, 0x44, 0xaf, 0x03, 0x44 };
  530.  
  531. /* From a NIST PR=false test vector */
  532. static const unsigned char entropy_nopr[] = {
  533.     0x79, 0x34, 0x9b, 0xbf, 0x7c, 0xdd, 0xa5, 0x79, 0x95, 0x57, 0x86, 0x66,
  534.     0x21, 0xc9, 0x13, 0x83, 0x11, 0x46, 0x73, 0x3a, 0xbf, 0x8c, 0x35, 0xc8,
  535.     0xc7, 0x21, 0x5b, 0x5b, 0x96, 0xc4, 0x8e, 0x9b, 0x33, 0x8c, 0x74, 0xe3,
  536.     0xe9, 0x9d, 0xfe, 0xdf };
  537. static const unsigned char result_nopr[OUTPUT_LEN] = {
  538.     0xc6, 0xa1, 0x6a, 0xb8, 0xd4, 0x20, 0x70, 0x6f, 0x0f, 0x34, 0xab, 0x7f,
  539.     0xec, 0x5a, 0xdc, 0xa9, 0xd8, 0xca, 0x3a, 0x13, 0x3e, 0x15, 0x9c, 0xa6,
  540.     0xac, 0x43, 0xc6, 0xf8, 0xa2, 0xbe, 0x22, 0x83, 0x4a, 0x4c, 0x0a, 0x0a,
  541.     0xff, 0xb1, 0x0d, 0x71, 0x94, 0xf1, 0xc1, 0xa5, 0xcf, 0x73, 0x22, 0xec,
  542.     0x1a, 0xe0, 0x96, 0x4e, 0xd4, 0xbf, 0x12, 0x27, 0x46, 0xe0, 0x87, 0xfd,
  543.     0xb5, 0xb3, 0xe9, 0x1b, 0x34, 0x93, 0xd5, 0xbb, 0x98, 0xfa, 0xed, 0x49,
  544.     0xe8, 0x5f, 0x13, 0x0f, 0xc8, 0xa4, 0x59, 0xb7 };
  545.  
  546. /* "Entropy" from buffer */
  547. static size_t test_offset;
  548. static int hmac_drbg_self_test_entropy( void *data,
  549.                                         unsigned char *buf, size_t len )
  550. {
  551.     const unsigned char *p = data;
  552.     memcpy( buf, p + test_offset, len );
  553.     test_offset += len;
  554.     return( 0 );
  555. }
  556.  
  557. #define CHK( c )    if( (c) != 0 )                          \
  558.                     {                                       \
  559.                         if( verbose != 0 )                  \
  560.                             mbedtls_printf( "failed\n" );  \
  561.                         return( 1 );                        \
  562.                     }
  563.  
  564. /*
  565.  * Checkup routine for HMAC_DRBG with SHA-1
  566.  */
  567. int mbedtls_hmac_drbg_self_test( int verbose )
  568. {
  569.     mbedtls_hmac_drbg_context ctx;
  570.     unsigned char buf[OUTPUT_LEN];
  571.     const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
  572.  
  573.     mbedtls_hmac_drbg_init( &ctx );
  574.  
  575.     /*
  576.      * PR = True
  577.      */
  578.     if( verbose != 0 )
  579.         mbedtls_printf( "  HMAC_DRBG (PR = True) : " );
  580.  
  581.     test_offset = 0;
  582.     CHK( mbedtls_hmac_drbg_seed( &ctx, md_info,
  583.                          hmac_drbg_self_test_entropy, (void *) entropy_pr,
  584.                          NULL, 0 ) );
  585.     mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON );
  586.     CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  587.     CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  588.     CHK( memcmp( buf, result_pr, OUTPUT_LEN ) );
  589.     mbedtls_hmac_drbg_free( &ctx );
  590.  
  591.     mbedtls_hmac_drbg_free( &ctx );
  592.  
  593.     if( verbose != 0 )
  594.         mbedtls_printf( "passed\n" );
  595.  
  596.     /*
  597.      * PR = False
  598.      */
  599.     if( verbose != 0 )
  600.         mbedtls_printf( "  HMAC_DRBG (PR = False) : " );
  601.  
  602.     mbedtls_hmac_drbg_init( &ctx );
  603.  
  604.     test_offset = 0;
  605.     CHK( mbedtls_hmac_drbg_seed( &ctx, md_info,
  606.                          hmac_drbg_self_test_entropy, (void *) entropy_nopr,
  607.                          NULL, 0 ) );
  608.     CHK( mbedtls_hmac_drbg_reseed( &ctx, NULL, 0 ) );
  609.     CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  610.     CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  611.     CHK( memcmp( buf, result_nopr, OUTPUT_LEN ) );
  612.     mbedtls_hmac_drbg_free( &ctx );
  613.  
  614.     mbedtls_hmac_drbg_free( &ctx );
  615.  
  616.     if( verbose != 0 )
  617.         mbedtls_printf( "passed\n" );
  618.  
  619.     if( verbose != 0 )
  620.         mbedtls_printf( "\n" );
  621.  
  622.     return( 0 );
  623. }
  624. #endif /* MBEDTLS_SHA1_C */
  625. #endif /* MBEDTLS_SELF_TEST */
  626.  
  627. #endif /* MBEDTLS_HMAC_DRBG_C */
  628.