LCOV - code coverage report
Current view: top level - externals/tinycrypt/lib/source - hmac_prng.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 0 67 0.0 %
Date: 2024-09-16 20:15:30 Functions: 0 4 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 26 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* hmac_prng.c - TinyCrypt implementation of HMAC-PRNG */
       2                 :            : 
       3                 :            : /*
       4                 :            :  *  Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
       5                 :            :  *
       6                 :            :  *  Redistribution and use in source and binary forms, with or without
       7                 :            :  *  modification, are permitted provided that the following conditions are met:
       8                 :            :  *
       9                 :            :  *    - Redistributions of source code must retain the above copyright notice,
      10                 :            :  *     this list of conditions and the following disclaimer.
      11                 :            :  *
      12                 :            :  *    - Redistributions in binary form must reproduce the above copyright
      13                 :            :  *    notice, this list of conditions and the following disclaimer in the
      14                 :            :  *    documentation and/or other materials provided with the distribution.
      15                 :            :  *
      16                 :            :  *    - Neither the name of Intel Corporation nor the names of its contributors
      17                 :            :  *    may be used to endorse or promote products derived from this software
      18                 :            :  *    without specific prior written permission.
      19                 :            :  *
      20                 :            :  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
      21                 :            :  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      22                 :            :  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      23                 :            :  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
      24                 :            :  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
      25                 :            :  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
      26                 :            :  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
      27                 :            :  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
      28                 :            :  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
      29                 :            :  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
      30                 :            :  *  POSSIBILITY OF SUCH DAMAGE.
      31                 :            :  */
      32                 :            : 
      33                 :            : #include <tinycrypt/hmac_prng.h>
      34                 :            : #include <tinycrypt/hmac.h>
      35                 :            : #include <tinycrypt/constants.h>
      36                 :            : #include <tinycrypt/utils.h>
      37                 :            : 
      38                 :            : /*
      39                 :            :  * min bytes in the seed string.
      40                 :            :  * MIN_SLEN*8 must be at least the expected security level.
      41                 :            :  */
      42                 :            : static const unsigned int MIN_SLEN = 32;
      43                 :            : 
      44                 :            : /*
      45                 :            :  * max bytes in the seed string;
      46                 :            :  * SP800-90A specifies a maximum of 2^35 bits (i.e., 2^32 bytes).
      47                 :            :  */
      48                 :            : static const unsigned int MAX_SLEN = UINT32_MAX;
      49                 :            : 
      50                 :            : /*
      51                 :            :  * max bytes in the personalization string;
      52                 :            :  * SP800-90A specifies a maximum of 2^35 bits (i.e., 2^32 bytes).
      53                 :            :  */
      54                 :            : static const unsigned int MAX_PLEN = UINT32_MAX;
      55                 :            : 
      56                 :            : /*
      57                 :            :  * max bytes in the additional_info string;
      58                 :            :  * SP800-90A specifies a maximum of 2^35 bits (i.e., 2^32 bytes).
      59                 :            :  */
      60                 :            : static const unsigned int MAX_ALEN = UINT32_MAX;
      61                 :            : 
      62                 :            : /*
      63                 :            :  * max number of generates between re-seeds;
      64                 :            :  * TinyCrypt accepts up to (2^32 - 1) which is the maximal value of
      65                 :            :  * a 32-bit unsigned int variable, while SP800-90A specifies a maximum of 2^48.
      66                 :            :  */
      67                 :            : static const unsigned int MAX_GENS = UINT32_MAX;
      68                 :            : 
      69                 :            : /*
      70                 :            :  * maximum bytes per generate call;
      71                 :            :  * SP800-90A specifies a maximum up to 2^19.
      72                 :            :  */
      73                 :            : static const unsigned int  MAX_OUT = (1 << 19);
      74                 :            : 
      75                 :            : /*
      76                 :            :  * Assumes: prng != NULL
      77                 :            :  */
      78                 :          0 : static void update(TCHmacPrng_t prng, const uint8_t *data, unsigned int datalen, const uint8_t *additional_data, unsigned int additional_datalen)
      79                 :            : {
      80                 :          0 :         const uint8_t separator0 = 0x00;
      81                 :          0 :         const uint8_t separator1 = 0x01;
      82                 :            : 
      83                 :            :         /* configure the new prng key into the prng's instance of hmac */
      84                 :          0 :         tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key));
      85                 :            : 
      86                 :            :         /* use current state, e and separator 0 to compute a new prng key: */
      87                 :          0 :         (void)tc_hmac_init(&prng->h);
      88                 :          0 :         (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
      89                 :          0 :         (void)tc_hmac_update(&prng->h, &separator0, sizeof(separator0));
      90                 :            : 
      91         [ #  # ]:          0 :         if (data && datalen)
      92                 :          0 :                 (void)tc_hmac_update(&prng->h, data, datalen);
      93         [ #  # ]:          0 :         if (additional_data && additional_datalen)
      94                 :          0 :                 (void)tc_hmac_update(&prng->h, additional_data, additional_datalen);
      95                 :            : 
      96                 :          0 :         (void)tc_hmac_final(prng->key, sizeof(prng->key), &prng->h);
      97                 :            : 
      98                 :            :         /* configure the new prng key into the prng's instance of hmac */
      99                 :          0 :         (void)tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key));
     100                 :            : 
     101                 :            :         /* use the new key to compute a new state variable v */
     102                 :          0 :         (void)tc_hmac_init(&prng->h);
     103                 :          0 :         (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
     104                 :          0 :         (void)tc_hmac_final(prng->v, sizeof(prng->v), &prng->h);
     105                 :            : 
     106         [ #  # ]:          0 :         if (data == 0 || datalen == 0)
     107                 :          0 :                 return;
     108                 :            : 
     109                 :            :         /* configure the new prng key into the prng's instance of hmac */
     110                 :          0 :         tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key));
     111                 :            : 
     112                 :            :         /* use current state, e and separator 1 to compute a new prng key: */
     113                 :          0 :         (void)tc_hmac_init(&prng->h);
     114                 :          0 :         (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
     115                 :          0 :         (void)tc_hmac_update(&prng->h, &separator1, sizeof(separator1));
     116                 :          0 :         (void)tc_hmac_update(&prng->h, data, datalen);
     117         [ #  # ]:          0 :         if (additional_data && additional_datalen)
     118                 :          0 :                 (void)tc_hmac_update(&prng->h, additional_data, additional_datalen);
     119                 :          0 :         (void)tc_hmac_final(prng->key, sizeof(prng->key), &prng->h);
     120                 :            : 
     121                 :            :         /* configure the new prng key into the prng's instance of hmac */
     122                 :          0 :         (void)tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key));
     123                 :            : 
     124                 :            :         /* use the new key to compute a new state variable v */
     125                 :          0 :         (void)tc_hmac_init(&prng->h);
     126                 :          0 :         (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
     127                 :          0 :         (void)tc_hmac_final(prng->v, sizeof(prng->v), &prng->h);
     128                 :            : }
     129                 :            : 
     130                 :          0 : int tc_hmac_prng_init(TCHmacPrng_t prng,
     131                 :            :                       const uint8_t *personalization,
     132                 :            :                       unsigned int plen)
     133                 :            : {
     134                 :            : 
     135                 :            :         /* input sanity check: */
     136                 :          0 :         if (prng == (TCHmacPrng_t) 0 ||
     137         [ #  # ]:          0 :             personalization == (uint8_t *) 0 ||
     138                 :            :             plen > MAX_PLEN) {
     139                 :            :                 return TC_CRYPTO_FAIL;
     140                 :            :         }
     141                 :            : 
     142                 :            :         /* put the generator into a known state: */
     143                 :          0 :         _set(prng->key, 0x00, sizeof(prng->key));
     144                 :          0 :         _set(prng->v, 0x01, sizeof(prng->v));
     145                 :            : 
     146                 :          0 :         update(prng, personalization, plen, 0, 0);
     147                 :            : 
     148                 :            :         /* force a reseed before allowing tc_hmac_prng_generate to succeed: */
     149                 :          0 :         prng->countdown = 0;
     150                 :            : 
     151                 :          0 :         return TC_CRYPTO_SUCCESS;
     152                 :            : }
     153                 :            : 
     154                 :          0 : int tc_hmac_prng_reseed(TCHmacPrng_t prng,
     155                 :            :                         const uint8_t *seed,
     156                 :            :                         unsigned int seedlen,
     157                 :            :                         const uint8_t *additional_input,
     158                 :            :                         unsigned int additionallen)
     159                 :            : {
     160                 :            : 
     161                 :            :         /* input sanity check: */
     162                 :          0 :         if (prng == (TCHmacPrng_t) 0 ||
     163         [ #  # ]:          0 :             seed == (const uint8_t *) 0 ||
     164         [ #  # ]:          0 :             seedlen < MIN_SLEN ||
     165                 :            :             seedlen > MAX_SLEN) {
     166                 :            :                 return TC_CRYPTO_FAIL;
     167                 :            :         }
     168                 :            : 
     169         [ #  # ]:          0 :         if (additional_input != (const uint8_t *) 0) {
     170                 :            :                 /*
     171                 :            :                  * Abort if additional_input is provided but has inappropriate
     172                 :            :                  * length
     173                 :            :                  */
     174         [ #  # ]:          0 :                 if (additionallen == 0 ||
     175                 :            :                     additionallen > MAX_ALEN) {
     176                 :            :                         return TC_CRYPTO_FAIL;
     177                 :            :                 } else {
     178                 :            :                         /* call update for the seed and additional_input */
     179                 :          0 :                         update(prng, seed, seedlen, additional_input, additionallen);
     180                 :            :                 }
     181                 :            :         } else {
     182                 :            :                 /* call update only for the seed */
     183                 :          0 :                 update(prng, seed, seedlen, 0, 0);
     184                 :            :         }
     185                 :            : 
     186                 :            :         /* ... and enable hmac_prng_generate */
     187                 :          0 :         prng->countdown = MAX_GENS;
     188                 :            : 
     189                 :          0 :         return TC_CRYPTO_SUCCESS;
     190                 :            : }
     191                 :            : 
     192                 :          0 : int tc_hmac_prng_generate(uint8_t *out, unsigned int outlen, TCHmacPrng_t prng)
     193                 :            : {
     194                 :          0 :         unsigned int bufferlen;
     195                 :            : 
     196                 :            :         /* input sanity check: */
     197                 :          0 :         if (out == (uint8_t *) 0 ||
     198         [ #  # ]:          0 :             prng == (TCHmacPrng_t) 0 ||
     199         [ #  # ]:          0 :             outlen == 0 ||
     200                 :            :             outlen > MAX_OUT) {
     201                 :            :                 return TC_CRYPTO_FAIL;
     202         [ #  # ]:          0 :         } else if (prng->countdown == 0) {
     203                 :            :                 return TC_HMAC_PRNG_RESEED_REQ;
     204                 :            :         }
     205                 :            : 
     206                 :          0 :         prng->countdown--;
     207                 :            : 
     208         [ #  # ]:          0 :         while (outlen != 0) {
     209                 :            :                 /* configure the new prng key into the prng's instance of hmac */
     210                 :          0 :                 tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key));
     211                 :            : 
     212                 :            :                 /* operate HMAC in OFB mode to create "random" outputs */
     213                 :          0 :                 (void)tc_hmac_init(&prng->h);
     214                 :          0 :                 (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
     215                 :          0 :                 (void)tc_hmac_final(prng->v, sizeof(prng->v), &prng->h);
     216                 :            : 
     217                 :          0 :                 bufferlen = (TC_SHA256_DIGEST_SIZE > outlen) ?
     218                 :            :                         outlen : TC_SHA256_DIGEST_SIZE;
     219                 :          0 :                 (void)_copy(out, bufferlen, prng->v, bufferlen);
     220                 :            : 
     221                 :          0 :                 out += bufferlen;
     222                 :          0 :                 outlen = (outlen > TC_SHA256_DIGEST_SIZE) ?
     223                 :          0 :                         (outlen - TC_SHA256_DIGEST_SIZE) : 0;
     224                 :            :         }
     225                 :            : 
     226                 :            :         /* block future PRNG compromises from revealing past state */
     227                 :          0 :         update(prng, 0, 0, 0, 0);
     228                 :            : 
     229                 :          0 :         return TC_CRYPTO_SUCCESS;
     230                 :            : }

Generated by: LCOV version 1.14