CHDK_DE Vorschauversion  Trunk Rev. 6014
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
sha1.c-Dateireferenz
#include "eyefi.h"
#include <string.h>
+ Include-Abhängigkeitsdiagramm für sha1.c:

gehe zum Quellcode dieser Datei

Datenstrukturen

struct  SHA1Context
 

Makrodefinitionen

#define SHA1_MAC_LEN   20
 
#define MD5_MAC_LEN   16
 
#define SHA1HANDSOFF
 
#define rol(value, bits)   (((value) << (bits)) | ((value) >> (32 - (bits))))
 
#define blk0(i)
 
#define blk(i)
 
#define R0(v, w, x, y, z, i)
 
#define R1(v, w, x, y, z, i)
 
#define R2(v, w, x, y, z, i)   z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); w = rol(w, 30);
 
#define R3(v, w, x, y, z, i)
 
#define R4(v, w, x, y, z, i)
 

Typdefinitionen

typedef struct SHA1Context SHA1_CTX
 

Funktionen

void sha1_vector (size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
 
void hmac_sha1_vector (const u8 *key, size_t key_len, size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
 
void hmac_sha1 (const u8 *key, size_t key_len, const u8 *data, size_t data_len, u8 *mac)
 
void sha1_prf (const u8 *key, size_t key_len, const char *label, const u8 *data, size_t data_len, u8 *buf, size_t buf_len)
 
void sha1_t_prf (const u8 *key, size_t key_len, const char *label, const u8 *seed, size_t seed_len, u8 *buf, size_t buf_len)
 
int tls_prf (const u8 *secret, size_t secret_len, const char *label, const u8 *seed, size_t seed_len, u8 *out, size_t outlen)
 
static void pbkdf2_sha1_f (const char *passphrase, const char *ssid, size_t ssid_len, int iterations, unsigned int count, u8 *digest)
 
void pbkdf2_sha1 (const char *passphrase, const char *ssid, size_t ssid_len, int iterations, u8 *buf, size_t buflen)
 
static void SHA1Init (struct SHA1Context *context)
 
static void SHA1Update (struct SHA1Context *context, const void *data, u32 len)
 
static void SHA1Final (unsigned char digest[20], struct SHA1Context *context)
 
static void SHA1Transform (u32 state[5], const unsigned char buffer[64])
 
int fips186_2_prf (const u8 *seed, size_t seed_len, u8 *x, size_t xlen)
 

Makro-Dokumentation

#define blk (   i)
Wert:
(block->l[i & 15] = rol(block->l[(i + 13) & 15] ^ \
block->l[(i + 8) & 15] ^ block->l[(i + 2) & 15] ^ block->l[i & 15], 1))

Definiert in Zeile 553 der Datei sha1.c.

#define blk0 (   i)
Wert:
(block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) | \
(rol(block->l[i], 8) & 0x00FF00FF))

Definiert in Zeile 548 der Datei sha1.c.

#define MD5_MAC_LEN   16

Definiert in Zeile 24 der Datei sha1.c.

#define R0 (   v,
  w,
  x,
  y,
  z,
 
)
Wert:
z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \
w = rol(w, 30);

Definiert in Zeile 557 der Datei sha1.c.

#define R1 (   v,
  w,
  x,
  y,
  z,
 
)
Wert:
z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
w = rol(w, 30);

Definiert in Zeile 560 der Datei sha1.c.

#define R2 (   v,
  w,
  x,
  y,
  z,
 
)    z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); w = rol(w, 30);

Definiert in Zeile 563 der Datei sha1.c.

#define R3 (   v,
  w,
  x,
  y,
  z,
 
)
Wert:
z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
w = rol(w, 30);

Definiert in Zeile 565 der Datei sha1.c.

#define R4 (   v,
  w,
  x,
  y,
  z,
 
)
Wert:
z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
w=rol(w, 30);

Definiert in Zeile 568 der Datei sha1.c.

#define rol (   value,
  bits 
)    (((value) << (bits)) | ((value) >> (32 - (bits))))

Definiert in Zeile 543 der Datei sha1.c.

#define SHA1_MAC_LEN   20

Definiert in Zeile 21 der Datei sha1.c.

#define SHA1HANDSOFF

Definiert in Zeile 541 der Datei sha1.c.

Dokumentation der benutzerdefinierten Typen

typedef struct SHA1Context SHA1_CTX

Definiert in Zeile 378 der Datei sha1.c.

Dokumentation der Funktionen

int fips186_2_prf ( const u8 seed,
size_t  seed_len,
u8 x,
size_t  xlen 
)

Definiert in Zeile 407 der Datei sha1.c.

408 {
409  u8 xkey[64];
410  u32 t[5], _t[5];
411  int i, j, m, k;
412  u8 *xpos = x;
413  u32 carry;
414 
415  if (seed_len > sizeof(xkey))
416  seed_len = sizeof(xkey);
417 
418  /* FIPS 186-2 + change notice 1 */
419 
420  memcpy(xkey, seed, seed_len);
421  memset(xkey + seed_len, 0, 64 - seed_len);
422  t[0] = 0x67452301;
423  t[1] = 0xEFCDAB89;
424  t[2] = 0x98BADCFE;
425  t[3] = 0x10325476;
426  t[4] = 0xC3D2E1F0;
427 
428  m = xlen / 40;
429  for (j = 0; j < m; j++) {
430  /* XSEED_j = 0 */
431  for (i = 0; i < 2; i++) {
432  /* XVAL = (XKEY + XSEED_j) mod 2^b */
433 
434  /* w_i = G(t, XVAL) */
435  memcpy(_t, t, 20);
436  SHA1Transform(_t, xkey);
437  _t[0] = host_to_be32(_t[0]);
438  _t[1] = host_to_be32(_t[1]);
439  _t[2] = host_to_be32(_t[2]);
440  _t[3] = host_to_be32(_t[3]);
441  _t[4] = host_to_be32(_t[4]);
442  memcpy(xpos, _t, 20);
443 
444  /* XKEY = (1 + XKEY + w_i) mod 2^b */
445  carry = 1;
446  for (k = 19; k >= 0; k--) {
447  carry += xkey[k] + xpos[k];
448  xkey[k] = carry & 0xff;
449  carry >>= 8;
450  }
451 
452  xpos += SHA1_MAC_LEN;
453  }
454  /* x_j = w_0|w_1 */
455  }
456 
457  return 0;
458 }
void hmac_sha1 ( const u8 key,
size_t  key_len,
const u8 data,
size_t  data_len,
u8 mac 
)

hmac_sha1 - HMAC-SHA1 over data buffer (RFC 2104) : Key for HMAC operations : Length of the key in bytes : Pointers to the data area : Length of the data area : Buffer for the hash (20 bytes)

Definiert in Zeile 106 der Datei sha1.c.

108 {
109  hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
110 }
void hmac_sha1_vector ( const u8 key,
size_t  key_len,
size_t  num_elem,
const u8 addr[],
const size_t len,
u8 mac 
)

hmac_sha1_vector - HMAC-SHA1 over data vector (RFC 2104) : Key for HMAC operations : Length of the key in bytes : Number of elements in the data vector : Pointers to the data areas : Lengths of the data blocks : Buffer for the hash (20 bytes)

Definiert in Zeile 35 der Datei sha1.c.

37 {
38  unsigned char k_pad[64]; /* padding - key XORd with ipad/opad */
39  unsigned char tk[20];
40  const u8 *_addr[6];
41  size_t _len[6], i;
42 
43  if (num_elem > 5) {
44  /*
45  * Fixed limit on the number of fragments to avoid having to
46  * allocate memory (which could fail).
47  */
48  return;
49  }
50 
51  /* if key is longer than 64 bytes reset it to key = SHA1(key) */
52  if (key_len > 64) {
53  sha1_vector(1, &key, &key_len, tk);
54  key = tk;
55  key_len = 20;
56  }
57 
58  /* the HMAC_SHA1 transform looks like:
59  *
60  * SHA1(K XOR opad, SHA1(K XOR ipad, text))
61  *
62  * where K is an n byte key
63  * ipad is the byte 0x36 repeated 64 times
64  * opad is the byte 0x5c repeated 64 times
65  * and text is the data being protected */
66 
67  /* start out by storing key in ipad */
68  memset(k_pad, 0, sizeof(k_pad));
69  memcpy(k_pad, key, key_len);
70  /* XOR key with ipad values */
71  for (i = 0; i < 64; i++)
72  k_pad[i] ^= 0x36;
73 
74  /* perform inner SHA1 */
75  _addr[0] = k_pad;
76  _len[0] = 64;
77  for (i = 0; i < num_elem; i++) {
78  _addr[i + 1] = addr[i];
79  _len[i + 1] = len[i];
80  }
81  sha1_vector(1 + num_elem, _addr, _len, mac);
82 
83  memset(k_pad, 0, sizeof(k_pad));
84  memcpy(k_pad, key, key_len);
85  /* XOR key with opad values */
86  for (i = 0; i < 64; i++)
87  k_pad[i] ^= 0x5c;
88 
89  /* perform outer SHA1 */
90  _addr[0] = k_pad;
91  _len[0] = 64;
92  _addr[1] = mac;
93  _len[1] = SHA1_MAC_LEN;
94  sha1_vector(2, _addr, _len, mac);
95 }
void pbkdf2_sha1 ( const char *  passphrase,
const char *  ssid,
size_t  ssid_len,
int  iterations,
u8 buf,
size_t  buflen 
)

pbkdf2_sha1 - SHA1-based key derivation function (PBKDF2) for IEEE 802.11i : ASCII passphrase : SSID : SSID length in bytes : Number of iterations to run : Buffer for the generated key : Length of the buffer in bytes

This function is used to derive PSK for WPA-PSK. For this protocol, iterations is set to 4096 and buflen to 32. This function is described in IEEE Std 802.11-2004, Clause H.4. The main construction is from PKCS#5 v2.0.

Definiert in Zeile 352 der Datei sha1.c.

354 {
355  unsigned int count = 0;
356  unsigned char *pos = buf;
357  size_t left = buflen, plen;
358  unsigned char digest[SHA1_MAC_LEN];
359 
360  while (left > 0) {
361  count++;
362  pbkdf2_sha1_f(passphrase, ssid, ssid_len, iterations, count,
363  digest);
364  plen = left > SHA1_MAC_LEN ? SHA1_MAC_LEN : left;
365  memcpy(pos, digest, plen);
366  pos += plen;
367  left -= plen;
368  }
369 }
static void pbkdf2_sha1_f ( const char *  passphrase,
const char *  ssid,
size_t  ssid_len,
int  iterations,
unsigned int  count,
u8 digest 
)
static

Definiert in Zeile 300 der Datei sha1.c.

303 {
304  unsigned char tmp[SHA1_MAC_LEN], tmp2[SHA1_MAC_LEN];
305  int i, j;
306  unsigned char count_buf[4];
307  const u8 *addr[2];
308  size_t len[2];
309  size_t passphrase_len = strlen(passphrase);
310 
311  addr[0] = (u8 *) ssid;
312  len[0] = ssid_len;
313  addr[1] = count_buf;
314  len[1] = 4;
315 
316  /* F(P, S, c, i) = U1 xor U2 xor ... Uc
317  * U1 = PRF(P, S || i)
318  * U2 = PRF(P, U1)
319  * Uc = PRF(P, Uc-1)
320  */
321 
322  count_buf[0] = (count >> 24) & 0xff;
323  count_buf[1] = (count >> 16) & 0xff;
324  count_buf[2] = (count >> 8) & 0xff;
325  count_buf[3] = count & 0xff;
326  hmac_sha1_vector((u8 *) passphrase, passphrase_len, 2, addr, len, tmp);
327  memcpy(digest, tmp, SHA1_MAC_LEN);
328 
329  for (i = 1; i < iterations; i++) {
330  hmac_sha1((u8 *) passphrase, passphrase_len, tmp, SHA1_MAC_LEN,
331  tmp2);
332  memcpy(tmp, tmp2, SHA1_MAC_LEN);
333  for (j = 0; j < SHA1_MAC_LEN; j++)
334  digest[j] ^= tmp2[j];
335  }
336 }
void sha1_prf ( const u8 key,
size_t  key_len,
const char *  label,
const u8 data,
size_t  data_len,
u8 buf,
size_t  buf_len 
)

sha1_prf - SHA1-based Pseudo-Random Function (PRF) (IEEE 802.11i, 8.5.1.1) : Key for PRF : Length of the key in bytes : A unique label for each purpose of the PRF : Extra data to bind into the key : Length of the data : Buffer for the generated pseudo-random key : Number of bytes of key to generate

This function is used to derive new, cryptographically separate keys from a given key (e.g., PMK in IEEE 802.11i).

Definiert in Zeile 126 der Datei sha1.c.

128 {
129  u8 zero = 0, counter = 0;
130  size_t pos, plen;
132  size_t label_len = strlen(label);
133  const unsigned char *addr[4];
134  size_t len[4];
135 
136  addr[0] = (u8 *) label;
137  len[0] = label_len;
138  addr[1] = &zero;
139  len[1] = 1;
140  addr[2] = data;
141  len[2] = data_len;
142  addr[3] = &counter;
143  len[3] = 1;
144 
145  pos = 0;
146  while (pos < buf_len) {
147  plen = buf_len - pos;
148  if (plen >= SHA1_MAC_LEN) {
149  hmac_sha1_vector(key, key_len, 4, addr, len,
150  &buf[pos]);
151  pos += SHA1_MAC_LEN;
152  } else {
153  hmac_sha1_vector(key, key_len, 4, addr, len,
154  hash);
155  memcpy(&buf[pos], hash, plen);
156  break;
157  }
158  counter++;
159  }
160 }
void sha1_t_prf ( const u8 key,
size_t  key_len,
const char *  label,
const u8 seed,
size_t  seed_len,
u8 buf,
size_t  buf_len 
)

sha1_t_prf - EAP-FAST Pseudo-Random Function (T-PRF) : Key for PRF : Length of the key in bytes : A unique label for each purpose of the PRF : Seed value to bind into the key : Length of the seed : Buffer for the generated pseudo-random key : Number of bytes of key to generate

This function is used to derive new, cryptographically separate keys from a given key for EAP-FAST. T-PRF is defined in draft-cam-winget-eap-fast-02.txt, Appendix B.

Definiert in Zeile 177 der Datei sha1.c.

179 {
180  unsigned char counter = 0;
181  size_t pos, plen;
183  size_t label_len = strlen(label);
184  u8 output_len[2];
185  const unsigned char *addr[5];
186  size_t len[5];
187 
188  addr[0] = hash;
189  len[0] = 0;
190  addr[1] = (unsigned char *) label;
191  len[1] = label_len + 1;
192  addr[2] = seed;
193  len[2] = seed_len;
194  addr[3] = output_len;
195  len[3] = 2;
196  addr[4] = &counter;
197  len[4] = 1;
198 
199  output_len[0] = (buf_len >> 8) & 0xff;
200  output_len[1] = buf_len & 0xff;
201  pos = 0;
202  while (pos < buf_len) {
203  counter++;
204  plen = buf_len - pos;
205  hmac_sha1_vector(key, key_len, 5, addr, len, hash);
206  if (plen >= SHA1_MAC_LEN) {
207  memcpy(&buf[pos], hash, SHA1_MAC_LEN);
208  pos += SHA1_MAC_LEN;
209  } else {
210  memcpy(&buf[pos], hash, plen);
211  break;
212  }
213  len[0] = SHA1_MAC_LEN;
214  }
215 }
void sha1_vector ( size_t  num_elem,
const u8 addr[],
const size_t len,
u8 mac 
)

sha1_vector - SHA-1 hash for data vector : Number of elements in the data vector : Pointers to the data areas : Lengths of the data blocks : Buffer for the hash

Definiert in Zeile 394 der Datei sha1.c.

396 {
397  SHA1_CTX ctx;
398  size_t i;
399 
400  SHA1Init(&ctx);
401  for (i = 0; i < num_elem; i++)
402  SHA1Update(&ctx, addr[i], len[i]);
403  SHA1Final(mac, &ctx);
404 }
void SHA1Final ( unsigned char  digest[20],
struct SHA1Context context 
)
static

Definiert in Zeile 691 der Datei sha1.c.

692 {
693  u32 i;
694  unsigned char finalcount[8];
695 
696  for (i = 0; i < 8; i++) {
697  finalcount[i] = (unsigned char)
698  ((context->count[(i >= 4 ? 0 : 1)] >>
699  ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
700  }
701  SHA1Update(context, (unsigned char *) "\200", 1);
702  while ((context->count[0] & 504) != 448) {
703  SHA1Update(context, (unsigned char *) "\0", 1);
704  }
705  SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform()
706  */
707  for (i = 0; i < 20; i++) {
708  digest[i] = (unsigned char)
709  ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) &
710  255);
711  }
712  /* Wipe variables */
713  i = 0;
714  memset(context->buffer, 0, 64);
715  memset(context->state, 0, 20);
716  memset(context->count, 0, 8);
717  memset(finalcount, 0, 8);
718 }
void SHA1Init ( struct SHA1Context context)
static

Definiert in Zeile 647 der Datei sha1.c.

648 {
649  /* SHA1 initialization constants */
650  context->state[0] = 0x67452301;
651  context->state[1] = 0xEFCDAB89;
652  context->state[2] = 0x98BADCFE;
653  context->state[3] = 0x10325476;
654  context->state[4] = 0xC3D2E1F0;
655  context->count[0] = context->count[1] = 0;
656 }
static void SHA1Transform ( u32  state[5],
const unsigned char  buffer[64] 
)
static

Definiert in Zeile 589 der Datei sha1.c.

590 {
591  u32 a, b, c, d, e;
592  typedef union {
593  unsigned char c[64];
594  u32 l[16];
595  } CHAR64LONG16;
596  CHAR64LONG16* block;
597 #ifdef SHA1HANDSOFF
598  u32 workspace[16];
599  block = (CHAR64LONG16 *) workspace;
600  memcpy(block, buffer, 64);
601 #else
602  block = (CHAR64LONG16 *) buffer;
603 #endif
604  /* Copy context->state[] to working vars */
605  a = state[0];
606  b = state[1];
607  c = state[2];
608  d = state[3];
609  e = state[4];
610  /* 4 rounds of 20 operations each. Loop unrolled. */
611  R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
612  R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
613  R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
614  R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
615  R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
616  R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
617  R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
618  R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
619  R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
620  R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
621  R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
622  R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
623  R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
624  R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
625  R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
626  R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
627  R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
628  R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
629  R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
630  R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
631  /* Add the working vars back into context.state[] */
632  state[0] += a;
633  state[1] += b;
634  state[2] += c;
635  state[3] += d;
636  state[4] += e;
637  /* Wipe variables */
638  a = b = c = d = e = 0;
639 #ifdef SHA1HANDSOFF
640  memset(block, 0, 64);
641 #endif
642 }
void SHA1Update ( struct SHA1Context context,
const void *  data,
u32  len 
)
static

Definiert in Zeile 661 der Datei sha1.c.

662 {
663  u32 i, j;
664  const unsigned char *data = _data;
665 
666 #ifdef VERBOSE
667  SHAPrintContext(context, "before");
668 #endif
669  j = (context->count[0] >> 3) & 63;
670  if ((context->count[0] += len << 3) < (len << 3))
671  context->count[1]++;
672  context->count[1] += (len >> 29);
673  if ((j + len) > 63) {
674  memcpy(&context->buffer[j], data, (i = 64-j));
675  SHA1Transform(context->state, context->buffer);
676  for ( ; i + 63 < len; i += 64) {
677  SHA1Transform(context->state, &data[i]);
678  }
679  j = 0;
680  }
681  else i = 0;
682  memcpy(&context->buffer[j], &data[i], len - i);
683 #ifdef VERBOSE
684  SHAPrintContext(context, "after ");
685 #endif
686 }
int tls_prf ( const u8 secret,
size_t  secret_len,
const char *  label,
const u8 seed,
size_t  seed_len,
u8 out,
size_t  outlen 
)

tls_prf - Pseudo-Random Function for TLS (TLS-PRF, RFC 2246) : Key for PRF : Length of the key in bytes : A unique label for each purpose of the PRF : Seed value to bind into the key : Length of the seed : Buffer for the generated pseudo-random key : Number of bytes of key to generate Returns: 0 on success, -1 on failure.

This function is used to derive new, cryptographically separate keys from a given key in TLS. This PRF is defined in RFC 2246, Chapter 5.

Definiert in Zeile 232 der Datei sha1.c.

234 {
235  size_t L_S1, L_S2, i;
236  const u8 *S1, *S2;
237  u8 A_MD5[MD5_MAC_LEN], A_SHA1[SHA1_MAC_LEN];
238  u8 P_MD5[MD5_MAC_LEN], P_SHA1[SHA1_MAC_LEN];
239  int MD5_pos, SHA1_pos;
240  const u8 *MD5_addr[3];
241  size_t MD5_len[3];
242  const unsigned char *SHA1_addr[3];
243  size_t SHA1_len[3];
244 
245  if (secret_len & 1)
246  return -1;
247 
248  MD5_addr[0] = A_MD5;
249  MD5_len[0] = MD5_MAC_LEN;
250  MD5_addr[1] = (unsigned char *) label;
251  MD5_len[1] = strlen(label);
252  MD5_addr[2] = seed;
253  MD5_len[2] = seed_len;
254 
255  SHA1_addr[0] = A_SHA1;
256  SHA1_len[0] = SHA1_MAC_LEN;
257  SHA1_addr[1] = (unsigned char *) label;
258  SHA1_len[1] = strlen(label);
259  SHA1_addr[2] = seed;
260  SHA1_len[2] = seed_len;
261 
262  /* RFC 2246, Chapter 5
263  * A(0) = seed, A(i) = HMAC(secret, A(i-1))
264  * P_hash = HMAC(secret, A(1) + seed) + HMAC(secret, A(2) + seed) + ..
265  * PRF = P_MD5(S1, label + seed) XOR P_SHA-1(S2, label + seed)
266  */
267 
268  L_S1 = L_S2 = (secret_len + 1) / 2;
269  S1 = secret;
270  S2 = secret + L_S1;
271 
272  hmac_md5_vector(S1, L_S1, 2, &MD5_addr[1], &MD5_len[1], A_MD5);
273  hmac_sha1_vector(S2, L_S2, 2, &SHA1_addr[1], &SHA1_len[1], A_SHA1);
274 
275  MD5_pos = MD5_MAC_LEN;
276  SHA1_pos = SHA1_MAC_LEN;
277  for (i = 0; i < outlen; i++) {
278  if (MD5_pos == MD5_MAC_LEN) {
279  hmac_md5_vector(S1, L_S1, 3, MD5_addr, MD5_len, P_MD5);
280  MD5_pos = 0;
281  hmac_md5(S1, L_S1, A_MD5, MD5_MAC_LEN, A_MD5);
282  }
283  if (SHA1_pos == SHA1_MAC_LEN) {
284  hmac_sha1_vector(S2, L_S2, 3, SHA1_addr, SHA1_len,
285  P_SHA1);
286  SHA1_pos = 0;
287  hmac_sha1(S2, L_S2, A_SHA1, SHA1_MAC_LEN, A_SHA1);
288  }
289 
290  out[i] = P_MD5[MD5_pos] ^ P_SHA1[SHA1_pos];
291 
292  MD5_pos++;
293  SHA1_pos++;
294  }
295 
296  return 0;
297 }