2 *************************************************************************
4 * 5F., No.36, Taiyuan St., Jhubei City,
8 * (c) Copyright 2002-2007, Ralink Technology, Inc.
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 *************************************************************************/
27 #include "../crypt_sha2.h"
29 /* Basic operations */
30 #define SHR(x,n) (x >> n) /* SHR(x)^n, right shift n bits , x is w-bit word, 0 <= n <= w */
31 #define ROTR(x,n,w) ((x >> n) | (x << (w - n))) /* ROTR(x)^n, circular right shift n bits , x is w-bit word, 0 <= n <= w */
32 #define ROTL(x,n,w) ((x << n) | (x >> (w - n))) /* ROTL(x)^n, circular left shift n bits , x is w-bit word, 0 <= n <= w */
33 #define ROTR32(x,n) ROTR(x,n,32) /* 32 bits word */
34 #define ROTL32(x,n) ROTL(x,n,32) /* 32 bits word */
37 #define Ch(x,y,z) ((x & y) ^ ((~x) & z))
38 #define Maj(x,y,z) ((x & y) ^ (x & z) ^ (y & z))
39 #define Parity(x,y,z) (x ^ y ^ z)
43 #define SHA1_MASK 0x0000000f
44 static const u32 SHA1_K[4] = {
45 0x5a827999UL, 0x6ed9eba1UL, 0x8f1bbcdcUL, 0xca62c1d6UL
48 static const u32 SHA1_DefaultHashValue[5] = {
49 0x67452301UL, 0xefcdab89UL, 0x98badcfeUL, 0x10325476UL, 0xc3d2e1f0UL
53 ========================================================================
55 Initial SHA1_CTX_STRUC
58 pSHA_CTX Pointer to SHA1_CTX_STRUC
65 ========================================================================
67 void RT_SHA1_Init(IN SHA1_CTX_STRUC * pSHA_CTX)
69 NdisMoveMemory(pSHA_CTX->HashValue, SHA1_DefaultHashValue,
70 sizeof(SHA1_DefaultHashValue));
71 NdisZeroMemory(pSHA_CTX->Block, SHA1_BLOCK_SIZE);
72 pSHA_CTX->MessageLen = 0;
73 pSHA_CTX->BlockLen = 0;
74 } /* End of RT_SHA1_Init */
77 ========================================================================
79 SHA1 computation for one block (512 bits)
82 pSHA_CTX Pointer to SHA1_CTX_STRUC
89 ========================================================================
91 void SHA1_Hash(IN SHA1_CTX_STRUC * pSHA_CTX)
95 u32 a, b, c, d, e, T, f_t = 0;
97 /* Prepare the message schedule, {W_i}, 0 < t < 15 */
98 NdisMoveMemory(W, pSHA_CTX->Block, SHA1_BLOCK_SIZE);
99 for (W_i = 0; W_i < 16; W_i++)
100 W[W_i] = cpu2be32(W[W_i]); /* Endian Swap */
103 /* SHA256 hash computation */
104 /* Initialize the working variables */
105 a = pSHA_CTX->HashValue[0];
106 b = pSHA_CTX->HashValue[1];
107 c = pSHA_CTX->HashValue[2];
108 d = pSHA_CTX->HashValue[3];
109 e = pSHA_CTX->HashValue[4];
112 for (t = 0; t < 80; t++) {
114 if (t > 15) { /* Prepare the message schedule, {W_i}, 16 < t < 79 */
116 (W[(s + 13) & SHA1_MASK]) ^ (W[(s + 8) & SHA1_MASK])
117 ^ (W[(s + 2) & SHA1_MASK]) ^ W[s];
118 W[s] = ROTL32(W[s], 1);
125 f_t = Parity(b, c, d);
131 f_t = Parity(b, c, d);
133 } /* End of switch */
134 T = ROTL32(a, 5) + f_t + e + SHA1_K[t / 20] + W[s];
142 /* Compute the i^th intermediate hash value H^(i) */
143 pSHA_CTX->HashValue[0] += a;
144 pSHA_CTX->HashValue[1] += b;
145 pSHA_CTX->HashValue[2] += c;
146 pSHA_CTX->HashValue[3] += d;
147 pSHA_CTX->HashValue[4] += e;
149 NdisZeroMemory(pSHA_CTX->Block, SHA1_BLOCK_SIZE);
150 pSHA_CTX->BlockLen = 0;
151 } /* End of SHA1_Hash */
154 ========================================================================
156 The message is appended to block. If block size > 64 bytes, the SHA1_Hash
160 pSHA_CTX Pointer to SHA1_CTX_STRUC
161 message Message context
162 messageLen The length of message in bytes
169 ========================================================================
171 void SHA1_Append(IN SHA1_CTX_STRUC * pSHA_CTX,
172 IN const u8 Message[], u32 MessageLen)
177 while (appendLen != MessageLen) {
178 diffLen = MessageLen - appendLen;
179 if ((pSHA_CTX->BlockLen + diffLen) < SHA1_BLOCK_SIZE) {
180 NdisMoveMemory(pSHA_CTX->Block + pSHA_CTX->BlockLen,
181 Message + appendLen, diffLen);
182 pSHA_CTX->BlockLen += diffLen;
183 appendLen += diffLen;
185 NdisMoveMemory(pSHA_CTX->Block + pSHA_CTX->BlockLen,
187 SHA1_BLOCK_SIZE - pSHA_CTX->BlockLen);
188 appendLen += (SHA1_BLOCK_SIZE - pSHA_CTX->BlockLen);
189 pSHA_CTX->BlockLen = SHA1_BLOCK_SIZE;
193 pSHA_CTX->MessageLen += MessageLen;
194 } /* End of SHA1_Append */
197 ========================================================================
199 1. Append bit 1 to end of the message
200 2. Append the length of message in rightmost 64 bits
201 3. Transform the Hash Value to digest message
204 pSHA_CTX Pointer to SHA1_CTX_STRUC
207 digestMessage Digest message
211 ========================================================================
213 void SHA1_End(IN SHA1_CTX_STRUC * pSHA_CTX, u8 DigestMessage[])
216 u64 message_length_bits;
218 /* Append bit 1 to end of the message */
219 NdisFillMemory(pSHA_CTX->Block + pSHA_CTX->BlockLen, 1, 0x80);
221 /* 55 = 64 - 8 - 1: append 1 bit(1 byte) and message length (8 bytes) */
222 if (pSHA_CTX->BlockLen > 55)
226 /* Append the length of message in rightmost 64 bits */
227 message_length_bits = pSHA_CTX->MessageLen * 8;
228 message_length_bits = cpu2be64(message_length_bits);
229 NdisMoveMemory(&pSHA_CTX->Block[56], &message_length_bits, 8);
232 /* Return message digest, transform the u32 hash value to bytes */
233 for (index = 0; index < 5; index++)
234 pSHA_CTX->HashValue[index] =
235 cpu2be32(pSHA_CTX->HashValue[index]);
237 NdisMoveMemory(DigestMessage, pSHA_CTX->HashValue, SHA1_DIGEST_SIZE);
238 } /* End of SHA1_End */
241 ========================================================================
246 message Message context
247 messageLen The length of message in bytes
250 digestMessage Digest message
254 ========================================================================
256 void RT_SHA1(IN const u8 Message[],
257 u32 MessageLen, u8 DigestMessage[])
260 SHA1_CTX_STRUC sha_ctx;
262 NdisZeroMemory(&sha_ctx, sizeof(SHA1_CTX_STRUC));
263 RT_SHA1_Init(&sha_ctx);
264 SHA1_Append(&sha_ctx, Message, MessageLen);
265 SHA1_End(&sha_ctx, DigestMessage);
266 } /* End of RT_SHA1 */
267 #endif /* SHA1_SUPPORT */
269 /* End of crypt_sha2.c */