LCOV - code coverage report
Current view: top level - src/crypto - fips_prf_openssl.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1443382998 Lines: 42 43 97.7 %
Date: 2015-09-27 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  * FIPS 186-2 PRF for libcrypto
       3             :  * Copyright (c) 2004-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             : #include <openssl/sha.h>
      11             : 
      12             : #include "common.h"
      13             : #include "crypto.h"
      14             : 
      15             : 
      16        1256 : static void sha1_transform(u32 *state, const u8 data[64])
      17             : {
      18             :         SHA_CTX context;
      19        1256 :         os_memset(&context, 0, sizeof(context));
      20        1256 :         context.h0 = state[0];
      21        1256 :         context.h1 = state[1];
      22        1256 :         context.h2 = state[2];
      23        1256 :         context.h3 = state[3];
      24        1256 :         context.h4 = state[4];
      25        1256 :         SHA1_Transform(&context, data);
      26        1256 :         state[0] = context.h0;
      27        1256 :         state[1] = context.h1;
      28        1256 :         state[2] = context.h2;
      29        1256 :         state[3] = context.h3;
      30        1256 :         state[4] = context.h4;
      31        1256 : }
      32             : 
      33             : 
      34         157 : int fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x, size_t xlen)
      35             : {
      36             :         u8 xkey[64];
      37             :         u32 t[5], _t[5];
      38             :         int i, j, m, k;
      39         157 :         u8 *xpos = x;
      40             :         u32 carry;
      41             : 
      42         157 :         if (seed_len < sizeof(xkey))
      43         157 :                 os_memset(xkey + seed_len, 0, sizeof(xkey) - seed_len);
      44             :         else
      45           0 :                 seed_len = sizeof(xkey);
      46             : 
      47             :         /* FIPS 186-2 + change notice 1 */
      48             : 
      49         157 :         os_memcpy(xkey, seed, seed_len);
      50         157 :         t[0] = 0x67452301;
      51         157 :         t[1] = 0xEFCDAB89;
      52         157 :         t[2] = 0x98BADCFE;
      53         157 :         t[3] = 0x10325476;
      54         157 :         t[4] = 0xC3D2E1F0;
      55             : 
      56         157 :         m = xlen / 40;
      57         785 :         for (j = 0; j < m; j++) {
      58             :                 /* XSEED_j = 0 */
      59        1884 :                 for (i = 0; i < 2; i++) {
      60             :                         /* XVAL = (XKEY + XSEED_j) mod 2^b */
      61             : 
      62             :                         /* w_i = G(t, XVAL) */
      63        1256 :                         os_memcpy(_t, t, 20);
      64        1256 :                         sha1_transform(_t, xkey);
      65        1256 :                         _t[0] = host_to_be32(_t[0]);
      66        1256 :                         _t[1] = host_to_be32(_t[1]);
      67        1256 :                         _t[2] = host_to_be32(_t[2]);
      68        1256 :                         _t[3] = host_to_be32(_t[3]);
      69        1256 :                         _t[4] = host_to_be32(_t[4]);
      70        1256 :                         os_memcpy(xpos, _t, 20);
      71             : 
      72             :                         /* XKEY = (1 + XKEY + w_i) mod 2^b */
      73        1256 :                         carry = 1;
      74       26376 :                         for (k = 19; k >= 0; k--) {
      75       25120 :                                 carry += xkey[k] + xpos[k];
      76       25120 :                                 xkey[k] = carry & 0xff;
      77       25120 :                                 carry >>= 8;
      78             :                         }
      79             : 
      80        1256 :                         xpos += 20;
      81             :                 }
      82             :                 /* x_j = w_0|w_1 */
      83             :         }
      84             : 
      85         157 :         return 0;
      86             : }

Generated by: LCOV version 1.10