LCOV - code coverage report
Current view: top level - src/crypto - sha1-tprf.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1393793999 Lines: 27 28 96.4 %
Date: 2014-03-02 Functions: 1 1 100.0 %
Branches: 5 6 83.3 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * SHA1 T-PRF for EAP-FAST
       3                 :            :  * Copyright (c) 2003-2005, 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 "sha1.h"
      13                 :            : #include "crypto.h"
      14                 :            : 
      15                 :            : /**
      16                 :            :  * sha1_t_prf - EAP-FAST Pseudo-Random Function (T-PRF)
      17                 :            :  * @key: Key for PRF
      18                 :            :  * @key_len: Length of the key in bytes
      19                 :            :  * @label: A unique label for each purpose of the PRF
      20                 :            :  * @seed: Seed value to bind into the key
      21                 :            :  * @seed_len: Length of the seed
      22                 :            :  * @buf: Buffer for the generated pseudo-random key
      23                 :            :  * @buf_len: Number of bytes of key to generate
      24                 :            :  * Returns: 0 on success, -1 of failure
      25                 :            :  *
      26                 :            :  * This function is used to derive new, cryptographically separate keys from a
      27                 :            :  * given key for EAP-FAST. T-PRF is defined in RFC 4851, Section 5.5.
      28                 :            :  */
      29                 :         38 : int sha1_t_prf(const u8 *key, size_t key_len, const char *label,
      30                 :            :                const u8 *seed, size_t seed_len, u8 *buf, size_t buf_len)
      31                 :            : {
      32                 :         38 :         unsigned char counter = 0;
      33                 :            :         size_t pos, plen;
      34                 :            :         u8 hash[SHA1_MAC_LEN];
      35                 :         38 :         size_t label_len = os_strlen(label);
      36                 :            :         u8 output_len[2];
      37                 :            :         const unsigned char *addr[5];
      38                 :            :         size_t len[5];
      39                 :            : 
      40                 :         38 :         addr[0] = hash;
      41                 :         38 :         len[0] = 0;
      42                 :         38 :         addr[1] = (unsigned char *) label;
      43                 :         38 :         len[1] = label_len + 1;
      44                 :         38 :         addr[2] = seed;
      45                 :         38 :         len[2] = seed_len;
      46                 :         38 :         addr[3] = output_len;
      47                 :         38 :         len[3] = 2;
      48                 :         38 :         addr[4] = &counter;
      49                 :         38 :         len[4] = 1;
      50                 :            : 
      51                 :         38 :         output_len[0] = (buf_len >> 8) & 0xff;
      52                 :         38 :         output_len[1] = buf_len & 0xff;
      53                 :         38 :         pos = 0;
      54         [ +  + ]:        146 :         while (pos < buf_len) {
      55                 :        132 :                 counter++;
      56                 :        132 :                 plen = buf_len - pos;
      57         [ -  + ]:        132 :                 if (hmac_sha1_vector(key, key_len, 5, addr, len, hash))
      58                 :          0 :                         return -1;
      59         [ +  + ]:        132 :                 if (plen >= SHA1_MAC_LEN) {
      60                 :        108 :                         os_memcpy(&buf[pos], hash, SHA1_MAC_LEN);
      61                 :        108 :                         pos += SHA1_MAC_LEN;
      62                 :            :                 } else {
      63                 :         24 :                         os_memcpy(&buf[pos], hash, plen);
      64                 :         24 :                         break;
      65                 :            :                 }
      66                 :        108 :                 len[0] = SHA1_MAC_LEN;
      67                 :            :         }
      68                 :            : 
      69                 :         38 :         return 0;
      70                 :            : }

Generated by: LCOV version 1.9