sha1-tlsprf.c

Go to the documentation of this file.
00001 
00016 #include "includes.h"
00017 
00018 #include "common.h"
00019 #include "sha1.h"
00020 #include "md5.h"
00021 #include "crypto.h"
00022 
00023 
00039 int tls_prf(const u8 *secret, size_t secret_len, const char *label,
00040             const u8 *seed, size_t seed_len, u8 *out, size_t outlen)
00041 {
00042         size_t L_S1, L_S2, i;
00043         const u8 *S1, *S2;
00044         u8 A_MD5[MD5_MAC_LEN], A_SHA1[SHA1_MAC_LEN];
00045         u8 P_MD5[MD5_MAC_LEN], P_SHA1[SHA1_MAC_LEN];
00046         int MD5_pos, SHA1_pos;
00047         const u8 *MD5_addr[3];
00048         size_t MD5_len[3];
00049         const unsigned char *SHA1_addr[3];
00050         size_t SHA1_len[3];
00051 
00052         if (secret_len & 1)
00053                 return -1;
00054 
00055         MD5_addr[0] = A_MD5;
00056         MD5_len[0] = MD5_MAC_LEN;
00057         MD5_addr[1] = (unsigned char *) label;
00058         MD5_len[1] = os_strlen(label);
00059         MD5_addr[2] = seed;
00060         MD5_len[2] = seed_len;
00061 
00062         SHA1_addr[0] = A_SHA1;
00063         SHA1_len[0] = SHA1_MAC_LEN;
00064         SHA1_addr[1] = (unsigned char *) label;
00065         SHA1_len[1] = os_strlen(label);
00066         SHA1_addr[2] = seed;
00067         SHA1_len[2] = seed_len;
00068 
00069         /* RFC 2246, Chapter 5
00070          * A(0) = seed, A(i) = HMAC(secret, A(i-1))
00071          * P_hash = HMAC(secret, A(1) + seed) + HMAC(secret, A(2) + seed) + ..
00072          * PRF = P_MD5(S1, label + seed) XOR P_SHA-1(S2, label + seed)
00073          */
00074 
00075         L_S1 = L_S2 = (secret_len + 1) / 2;
00076         S1 = secret;
00077         S2 = secret + L_S1;
00078         if (secret_len & 1) {
00079                 /* The last byte of S1 will be shared with S2 */
00080                 S2--;
00081         }
00082 
00083         hmac_md5_vector_non_fips_allow(S1, L_S1, 2, &MD5_addr[1], &MD5_len[1],
00084                                        A_MD5);
00085         hmac_sha1_vector(S2, L_S2, 2, &SHA1_addr[1], &SHA1_len[1], A_SHA1);
00086 
00087         MD5_pos = MD5_MAC_LEN;
00088         SHA1_pos = SHA1_MAC_LEN;
00089         for (i = 0; i < outlen; i++) {
00090                 if (MD5_pos == MD5_MAC_LEN) {
00091                         hmac_md5_vector_non_fips_allow(S1, L_S1, 3, MD5_addr,
00092                                                        MD5_len, P_MD5);
00093                         MD5_pos = 0;
00094                         hmac_md5_non_fips_allow(S1, L_S1, A_MD5, MD5_MAC_LEN,
00095                                                 A_MD5);
00096                 }
00097                 if (SHA1_pos == SHA1_MAC_LEN) {
00098                         hmac_sha1_vector(S2, L_S2, 3, SHA1_addr, SHA1_len,
00099                                          P_SHA1);
00100                         SHA1_pos = 0;
00101                         hmac_sha1(S2, L_S2, A_SHA1, SHA1_MAC_LEN, A_SHA1);
00102                 }
00103 
00104                 out[i] = P_MD5[MD5_pos] ^ P_SHA1[SHA1_pos];
00105 
00106                 MD5_pos++;
00107                 SHA1_pos++;
00108         }
00109 
00110         return 0;
00111 }
00112 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on Sat Nov 21 23:16:50 2009 for hostapd by  doxygen 1.6.1