LCOV - code coverage report
Current view: top level - src/crypto - sha256-kdf.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1443382998 Lines: 25 32 78.1 %
Date: 2015-09-27 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * HMAC-SHA256 KDF (RFC 5295)
       3             :  * Copyright (c) 2014, Jouni Malinen <j@w1.fi>
       4             :  *
       5             :  * This software may be distributed under the terms of the BSD license.
       6             :  * See README for more details.
       7             :  */
       8             : 
       9             : #include "includes.h"
      10             : 
      11             : #include "common.h"
      12             : #include "sha256.h"
      13             : 
      14             : 
      15             : /**
      16             :  * hmac_sha256_kdf - HMAC-SHA256 based KDF (RFC 5295)
      17             :  * @secret: Key for KDF
      18             :  * @secret_len: Length of the key in bytes
      19             :  * @label: A unique label for each purpose of the KDF
      20             :  * @seed: Seed value to bind into the key
      21             :  * @seed_len: Length of the seed
      22             :  * @out: Buffer for the generated pseudo-random key
      23             :  * @outlen: Number of bytes of key to generate
      24             :  * Returns: 0 on success, -1 on failure.
      25             :  *
      26             :  * This function is used to derive new, cryptographically separate keys from a
      27             :  * given key in ERP. This KDF is defined in RFC 5295, Chapter 3.1.2.
      28             :  */
      29         154 : int hmac_sha256_kdf(const u8 *secret, size_t secret_len,
      30             :                     const char *label, const u8 *seed, size_t seed_len,
      31             :                     u8 *out, size_t outlen)
      32             : {
      33             :         u8 T[SHA256_MAC_LEN];
      34         154 :         u8 iter = 1;
      35             :         const unsigned char *addr[4];
      36             :         size_t len[4];
      37             :         size_t pos, clen;
      38             : 
      39         154 :         addr[0] = T;
      40         154 :         len[0] = SHA256_MAC_LEN;
      41         154 :         addr[1] = (const unsigned char *) label;
      42         154 :         len[1] = os_strlen(label) + 1;
      43         154 :         addr[2] = seed;
      44         154 :         len[2] = seed_len;
      45         154 :         addr[3] = &iter;
      46         154 :         len[3] = 1;
      47             : 
      48         154 :         if (hmac_sha256_vector(secret, secret_len, 3, &addr[1], &len[1], T) < 0)
      49           0 :                 return -1;
      50             : 
      51         154 :         pos = 0;
      52             :         for (;;) {
      53         270 :                 clen = outlen - pos;
      54         270 :                 if (clen > SHA256_MAC_LEN)
      55         116 :                         clen = SHA256_MAC_LEN;
      56         270 :                 os_memcpy(out + pos, T, clen);
      57         270 :                 pos += clen;
      58             : 
      59         270 :                 if (pos == outlen)
      60         154 :                         break;
      61             : 
      62         116 :                 if (iter == 255) {
      63           0 :                         os_memset(out, 0, outlen);
      64           0 :                         os_memset(T, 0, SHA256_MAC_LEN);
      65           0 :                         return -1;
      66             :                 }
      67         116 :                 iter++;
      68             : 
      69         116 :                 if (hmac_sha256_vector(secret, secret_len, 4, addr, len, T) < 0)
      70             :                 {
      71           0 :                         os_memset(out, 0, outlen);
      72           0 :                         os_memset(T, 0, SHA256_MAC_LEN);
      73           0 :                         return -1;
      74             :                 }
      75         116 :         }
      76             : 
      77         154 :         os_memset(T, 0, SHA256_MAC_LEN);
      78         154 :         return 0;
      79             : }

Generated by: LCOV version 1.10