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 UINT32 SHA1_K[4] = {
45 0x5a827999UL, 0x6ed9eba1UL, 0x8f1bbcdcUL, 0xca62c1d6UL
47 static const UINT32 SHA1_DefaultHashValue[5] = {
48 0x67452301UL, 0xefcdab89UL, 0x98badcfeUL, 0x10325476UL, 0xc3d2e1f0UL
52 ========================================================================
54 Initial SHA1_CTX_STRUC
57 pSHA_CTX Pointer to SHA1_CTX_STRUC
64 ========================================================================
67 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 */
78 ========================================================================
80 SHA1 computation for one block (512 bits)
83 pSHA_CTX Pointer to SHA1_CTX_STRUC
90 ========================================================================
93 IN SHA1_CTX_STRUC *pSHA_CTX)
97 UINT32 a,b,c,d,e,T,f_t = 0;
99 /* Prepare the message schedule, {W_i}, 0 < t < 15 */
100 NdisMoveMemory(W, pSHA_CTX->Block, SHA1_BLOCK_SIZE);
101 for (W_i = 0; W_i < 16; W_i++)
102 W[W_i] = cpu2be32(W[W_i]); /* Endian Swap */
105 /* SHA256 hash computation */
106 /* Initialize the working variables */
107 a = pSHA_CTX->HashValue[0];
108 b = pSHA_CTX->HashValue[1];
109 c = pSHA_CTX->HashValue[2];
110 d = pSHA_CTX->HashValue[3];
111 e = pSHA_CTX->HashValue[4];
114 for (t = 0;t < 80;t++) {
116 if (t > 15) { /* Prepare the message schedule, {W_i}, 16 < t < 79 */
117 W[s] = (W[(s+13) & SHA1_MASK]) ^ (W[(s+8) & SHA1_MASK]) ^ (W[(s+2) & SHA1_MASK]) ^ W[s];
118 W[s] = ROTL32(W[s],1);
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 */
155 ========================================================================
157 The message is appended to block. If block size > 64 bytes, the SHA1_Hash
161 pSHA_CTX Pointer to SHA1_CTX_STRUC
162 message Message context
163 messageLen The length of message in bytes
170 ========================================================================
173 IN SHA1_CTX_STRUC *pSHA_CTX,
174 IN const UINT8 Message[],
180 while (appendLen != MessageLen) {
181 diffLen = MessageLen - appendLen;
182 if ((pSHA_CTX->BlockLen + diffLen) < SHA1_BLOCK_SIZE) {
183 NdisMoveMemory(pSHA_CTX->Block + pSHA_CTX->BlockLen,
184 Message + appendLen, diffLen);
185 pSHA_CTX->BlockLen += diffLen;
186 appendLen += diffLen;
190 NdisMoveMemory(pSHA_CTX->Block + pSHA_CTX->BlockLen,
191 Message + appendLen, SHA1_BLOCK_SIZE - pSHA_CTX->BlockLen);
192 appendLen += (SHA1_BLOCK_SIZE - pSHA_CTX->BlockLen);
193 pSHA_CTX->BlockLen = SHA1_BLOCK_SIZE;
197 pSHA_CTX->MessageLen += MessageLen;
198 } /* End of SHA1_Append */
202 ========================================================================
204 1. Append bit 1 to end of the message
205 2. Append the length of message in rightmost 64 bits
206 3. Transform the Hash Value to digest message
209 pSHA_CTX Pointer to SHA1_CTX_STRUC
212 digestMessage Digest message
216 ========================================================================
219 IN SHA1_CTX_STRUC *pSHA_CTX,
220 OUT UINT8 DigestMessage[])
223 UINT64 message_length_bits;
225 /* Append bit 1 to end of the message */
226 NdisFillMemory(pSHA_CTX->Block + pSHA_CTX->BlockLen, 1, 0x80);
228 /* 55 = 64 - 8 - 1: append 1 bit(1 byte) and message length (8 bytes) */
229 if (pSHA_CTX->BlockLen > 55)
233 /* Append the length of message in rightmost 64 bits */
234 message_length_bits = pSHA_CTX->MessageLen*8;
235 message_length_bits = cpu2be64(message_length_bits);
236 NdisMoveMemory(&pSHA_CTX->Block[56], &message_length_bits, 8);
239 /* Return message digest, transform the UINT32 hash value to bytes */
240 for (index = 0; index < 5;index++)
241 pSHA_CTX->HashValue[index] = cpu2be32(pSHA_CTX->HashValue[index]);
243 NdisMoveMemory(DigestMessage, pSHA_CTX->HashValue, SHA1_DIGEST_SIZE);
244 } /* End of SHA1_End */
248 ========================================================================
253 message Message context
254 messageLen The length of message in bytes
257 digestMessage Digest message
261 ========================================================================
264 IN const UINT8 Message[],
266 OUT UINT8 DigestMessage[])
269 SHA1_CTX_STRUC sha_ctx;
271 NdisZeroMemory(&sha_ctx, sizeof(SHA1_CTX_STRUC));
272 RT_SHA1_Init(&sha_ctx);
273 SHA1_Append(&sha_ctx, Message, MessageLen);
274 SHA1_End(&sha_ctx, DigestMessage);
275 } /* End of RT_SHA1 */
276 #endif /* SHA1_SUPPORT */
278 /* End of crypt_sha2.c */