📄 sha1.c
字号:
/* * SHA1 hash implementation and interface functions * Copyright (c) 2003-2005, Jouni Malinen <jkmaline@cc.hut.fi> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * Alternatively, this software may be distributed under the terms of BSD * license. * * See README and COPYING for more details. */#include <stdlib.h>#include <stdio.h>#include <string.h>#include "common.h"#include "sha1.h"#include "md5.h"void sha1_mac(const u8 *key, size_t key_len, const u8 *data, size_t data_len, u8 *mac){ SHA1_CTX context; SHA1Init(&context); SHA1Update(&context, key, key_len); SHA1Update(&context, data, data_len); SHA1Update(&context, key, key_len); SHA1Final(mac, &context);}/* HMAC code is based on RFC 2104 */void hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac){ SHA1_CTX context; unsigned char k_ipad[65]; /* inner padding - key XORd with ipad */ unsigned char k_opad[65]; /* outer padding - key XORd with opad */ unsigned char tk[20]; int i; /* if key is longer than 64 bytes reset it to key = SHA1(key) */ if (key_len > 64) { SHA1Init(&context); SHA1Update(&context, key, key_len); SHA1Final(tk, &context); key = tk; key_len = 20; } /* the HMAC_SHA1 transform looks like: * * SHA1(K XOR opad, SHA1(K XOR ipad, text)) * * where K is an n byte key * ipad is the byte 0x36 repeated 64 times * opad is the byte 0x5c repeated 64 times * and text is the data being protected */ /* start out by storing key in pads */ memset(k_ipad, 0, sizeof(k_ipad)); memset(k_opad, 0, sizeof(k_opad)); memcpy(k_ipad, key, key_len); memcpy(k_opad, key, key_len); /* XOR key with ipad and opad values */ for (i = 0; i < 64; i++) { k_ipad[i] ^= 0x36; k_opad[i] ^= 0x5c; } /* perform inner SHA1 */ SHA1Init(&context); /* init context for 1st pass */ SHA1Update(&context, k_ipad, 64); /* start with inner pad */ /* then text of datagram; all fragments */ for (i = 0; i < num_elem; i++) { SHA1Update(&context, addr[i], len[i]); } SHA1Final(mac, &context); /* finish up 1st pass */ /* perform outer SHA1 */ SHA1Init(&context); /* init context for 2nd pass */ SHA1Update(&context, k_opad, 64); /* start with outer pad */ SHA1Update(&context, mac, 20); /* then results of 1st hash */ SHA1Final(mac, &context); /* finish up 2nd pass */}void hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len, u8 *mac){ hmac_sha1_vector(key, key_len, 1, &data, &data_len, 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){ u8 zero = 0, counter = 0; size_t pos, plen; u8 hash[SHA1_MAC_LEN]; size_t label_len = strlen(label); const unsigned char *addr[4]; size_t len[4]; addr[0] = (u8 *) label; len[0] = label_len; addr[1] = &zero; len[1] = 1; addr[2] = data; len[2] = data_len; addr[3] = &counter; len[3] = 1; pos = 0; while (pos < buf_len) { plen = buf_len - pos; if (plen >= SHA1_MAC_LEN) { hmac_sha1_vector(key, key_len, 4, addr, len, &buf[pos]); pos += SHA1_MAC_LEN; } else { hmac_sha1_vector(key, key_len, 4, addr, len, hash); memcpy(&buf[pos], hash, plen); break; } counter++; }}/* draft-cam-winget-eap-fast-00.txt */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){ unsigned char counter = 0; size_t pos, plen; u8 hash[SHA1_MAC_LEN]; size_t label_len = strlen(label); u8 output_len[2]; const unsigned char *addr[5]; size_t len[5]; addr[0] = hash; len[0] = 0; addr[1] = (unsigned char *) label; len[1] = label_len + 1; addr[2] = seed; len[2] = seed_len; addr[3] = output_len; len[3] = 2; addr[4] = &counter; len[4] = 1; output_len[0] = (buf_len >> 8) & 0xff; output_len[1] = buf_len & 0xff; pos = 0; while (pos < buf_len) { counter++; plen = buf_len - pos; hmac_sha1_vector(key, key_len, 5, addr, len, hash); if (plen >= SHA1_MAC_LEN) { memcpy(&buf[pos], hash, SHA1_MAC_LEN); pos += SHA1_MAC_LEN; } else { memcpy(&buf[pos], hash, plen); break; } len[0] = SHA1_MAC_LEN; }}/* RFC 2246 */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){ size_t L_S1, L_S2; const u8 *S1, *S2; u8 A_MD5[MD5_MAC_LEN], A_SHA1[SHA1_MAC_LEN]; u8 P_MD5[MD5_MAC_LEN], P_SHA1[SHA1_MAC_LEN]; int i, MD5_pos, SHA1_pos; const u8 *MD5_addr[3]; size_t MD5_len[3]; const unsigned char *SHA1_addr[3]; size_t SHA1_len[3]; if (secret_len & 1) return -1; MD5_addr[0] = A_MD5; MD5_len[0] = MD5_MAC_LEN; MD5_addr[1] = (unsigned char *) label; MD5_len[1] = strlen(label); MD5_addr[2] = seed; MD5_len[2] = seed_len; SHA1_addr[0] = A_SHA1; SHA1_len[0] = SHA1_MAC_LEN; SHA1_addr[1] = (unsigned char *) label; SHA1_len[1] = strlen(label); SHA1_addr[2] = seed; SHA1_len[2] = seed_len; /* RFC 2246, Chapter 5 * A(0) = seed, A(i) = HMAC(secret, A(i-1)) * P_hash = HMAC(secret, A(1) + seed) + HMAC(secret, A(2) + seed) + .. * PRF = P_MD5(S1, label + seed) XOR P_SHA-1(S2, label + seed) */ L_S1 = L_S2 = (secret_len + 1) / 2; S1 = secret; S2 = secret + L_S1; hmac_md5_vector(S1, L_S1, 2, &MD5_addr[1], &MD5_len[1], A_MD5); hmac_sha1_vector(S2, L_S2, 2, &SHA1_addr[1], &SHA1_len[1], A_SHA1); MD5_pos = MD5_MAC_LEN; SHA1_pos = SHA1_MAC_LEN; for (i = 0; i < outlen; i++) { if (MD5_pos == MD5_MAC_LEN) { hmac_md5_vector(S1, L_S1, 3, MD5_addr, MD5_len, P_MD5); MD5_pos = 0; hmac_md5(S1, L_S1, A_MD5, MD5_MAC_LEN, A_MD5); } if (SHA1_pos == SHA1_MAC_LEN) { hmac_sha1_vector(S2, L_S2, 3, SHA1_addr, SHA1_len, P_SHA1); SHA1_pos = 0; hmac_sha1(S2, L_S2, A_SHA1, SHA1_MAC_LEN, A_SHA1); } out[i] = P_MD5[MD5_pos] ^ P_SHA1[SHA1_pos]; MD5_pos++; SHA1_pos++; } return 0;}static void pbkdf2_sha1_f(const char *passphrase, const char *ssid, size_t ssid_len, int iterations, int count, u8 *digest){ unsigned char tmp[SHA1_MAC_LEN], tmp2[SHA1_MAC_LEN]; int i, j; unsigned char count_buf[4]; const u8 *addr[2]; size_t len[2]; size_t passphrase_len = strlen(passphrase); addr[0] = (u8 *) ssid; len[0] = ssid_len; addr[1] = count_buf; len[1] = 4; /* F(P, S, c, i) = U1 xor U2 xor ... Uc * U1 = PRF(P, S || i) * U2 = PRF(P, U1) * Uc = PRF(P, Uc-1) */ count_buf[0] = (count >> 24) & 0xff; count_buf[1] = (count >> 16) & 0xff; count_buf[2] = (count >> 8) & 0xff; count_buf[3] = count & 0xff; hmac_sha1_vector((u8 *) passphrase, passphrase_len, 2, addr, len, tmp); memcpy(digest, tmp, SHA1_MAC_LEN); for (i = 1; i < iterations; i++) { hmac_sha1((u8 *) passphrase, passphrase_len, tmp, SHA1_MAC_LEN, tmp2); memcpy(tmp, tmp2, SHA1_MAC_LEN); for (j = 0; j < SHA1_MAC_LEN; j++) digest[j] ^= tmp2[j]; }}void pbkdf2_sha1(const char *passphrase, const char *ssid, size_t ssid_len, int iterations, u8 *buf, size_t buflen){ int count = 0; unsigned char *pos = buf; size_t left = buflen, plen; unsigned char digest[SHA1_MAC_LEN]; while (left > 0) { count++; pbkdf2_sha1_f(passphrase, ssid, ssid_len, iterations, count, digest); plen = left > SHA1_MAC_LEN ? SHA1_MAC_LEN : left; memcpy(pos, digest, plen); pos += plen; left -= plen; }}void sha1_transform(u8 *state, u8 data[64]){#ifdef EAP_TLS_FUNCS SHA_CTX context; memset(&context, 0, sizeof(context)); memcpy(&context.h0, state, 5 * 4); SHA1_Transform(&context, data); memcpy(state, &context.h0, 5 * 4);#else /* EAP_TLS_FUNCS */ SHA1Transform((u32 *) state, data);#endif /* EAP_TLS_FUNCS */}void sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac){ SHA1_CTX ctx; int i; SHA1Init(&ctx); for (i = 0; i < num_elem; i++) SHA1Update(&ctx, addr[i], len[i]); SHA1Final(mac, &ctx);}#ifndef EAP_TLS_FUNCS/* ===== start - public domain SHA1 implementation ===== *//*SHA-1 in CBy Steve Reid <sreid@sea-to-sky.net>100% Public Domain-----------------Modified 7/98 By James H. Brown <jbrown@burgoyne.com>Still 100% Public DomainCorrected a problem which generated improper hash values on 16 bit machinesRoutine SHA1Update changed from void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned intlen)to void SHA1Update(SHA1_CTX* context, unsigned char* data, unsignedlong len)The 'len' parameter was declared an int which works fine on 32 bit machines.However, on 16 bit machines an int is too small for the shifts being doneagainstit. This caused the hash function to generate incorrect values if len wasgreater than 8191 (8K - 1) due to the 'len << 3' on line 3 of SHA1Update().Since the file IO in main() reads 16K at a time, any file 8K or larger wouldbe guaranteed to generate the wrong hash (e.g. Test Vector #3, a million"a"s).I also changed the declaration of variables i & j in SHA1Update to unsigned long from unsigned int for the same reason.These changes should make no difference to any 32 bit implementations sinceanint and a long are the same size in those environments.--I also corrected a few compiler warnings generated by Borland C.1. Added #include <process.h> for exit() prototype2. Removed unused variable 'j' in SHA1Final3. Changed exit(0) to return(0) at end of main.ALL changes I made can be located by searching for comments containing 'JHB'-----------------Modified 8/98By Steve Reid <sreid@sea-to-sky.net>Still 100% public domain1- Removed #include <process.h> and used return() instead of exit()2- Fixed overwriting of finalcount in SHA1Final() (discovered by Chris Hall)3- Changed email address from steve@edmweb.com to sreid@sea-to-sky.net-----------------Modified 4/01By Saul Kravitz <Saul.Kravitz@celera.com>Still 100% PDModified to run on Compaq Alpha hardware. -----------------Modified 4/01By Jouni Malinen <jkmaline@cc.hut.fi>Minor changes to match the coding style used in Dynamics.Modified September 24, 2004By Jouni Malinen <jkmaline@cc.hut.fi>Fixed alignment issue in SHA1Transform when SHA1HANDSOFF is defined.*//*Test Vectors (from FIPS PUB 180-1)"abc" A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1A million repetitions of "a" 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F*/#define SHA1HANDSOFF#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))/* blk0() and blk() perform the initial expand. *//* I got the idea of expanding during the round function from SSLeay */#ifndef WORDS_BIGENDIAN#define blk0(i) (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) | \ (rol(block->l[i], 8) & 0x00FF00FF))#else#define blk0(i) block->l[i]#endif#define blk(i) (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))/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */#define R0(v,w,x,y,z,i) \ z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \ w = rol(w, 30);#define R1(v,w,x,y,z,i) \ z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \ w = rol(w, 30);#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) \ z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \ w = rol(w, 30);#define R4(v,w,x,y,z,i) \ z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \ w=rol(w, 30);#ifdef VERBOSE /* SAK */void SHAPrintContext(SHA1_CTX *context, char *msg){ printf("%s (%d,%d) %x %x %x %x %x\n", msg, context->count[0], context->count[1], context->state[0], context->state[1],
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -