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"
30 /* Basic operations */
31 #define SHR(x,n) (x >> n) /* SHR(x)^n, right shift n bits , x is w-bit word, 0 <= n <= w */
32 #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 */
33 #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 */
34 #define ROTR32(x,n) ROTR(x,n,32) /* 32 bits word */
35 #define ROTL32(x,n) ROTL(x,n,32) /* 32 bits word */
38 #define Ch(x,y,z) ((x & y) ^ ((~x) & z))
39 #define Maj(x,y,z) ((x & y) ^ (x & z) ^ (y & z))
40 #define Parity(x,y,z) (x ^ y ^ z)
44 #define SHA1_MASK 0x0000000f
45 static const UINT32 SHA1_K[4] = {
46 0x5a827999UL, 0x6ed9eba1UL, 0x8f1bbcdcUL, 0xca62c1d6UL
48 static const UINT32 SHA1_DefaultHashValue[5] = {
49 0x67452301UL, 0xefcdab89UL, 0x98badcfeUL, 0x10325476UL, 0xc3d2e1f0UL
51 #endif /* SHA1_SUPPORT */
55 /* SHA256 functions */
56 #define Zsigma_256_0(x) (ROTR32(x,2) ^ ROTR32(x,13) ^ ROTR32(x,22))
57 #define Zsigma_256_1(x) (ROTR32(x,6) ^ ROTR32(x,11) ^ ROTR32(x,25))
58 #define Sigma_256_0(x) (ROTR32(x,7) ^ ROTR32(x,18) ^ SHR(x,3))
59 #define Sigma_256_1(x) (ROTR32(x,17) ^ ROTR32(x,19) ^ SHR(x,10))
60 /* SHA256 constants */
61 static const UINT32 SHA256_K[64] = {
62 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
63 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
64 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
65 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
66 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
67 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
68 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
69 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
70 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
71 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
72 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
73 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
74 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
75 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
76 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
77 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
79 static const UINT32 SHA256_DefaultHashValue[8] = {
80 0x6a09e667UL, 0xbb67ae85UL, 0x3c6ef372UL, 0xa54ff53aUL,
81 0x510e527fUL, 0x9b05688cUL, 0x1f83d9abUL, 0x5be0cd19UL
83 #endif /* SHA256_SUPPORT */
88 ========================================================================
90 Initial SHA1_CTX_STRUC
93 pSHA_CTX Pointer to SHA1_CTX_STRUC
100 ========================================================================
103 IN SHA1_CTX_STRUC *pSHA_CTX)
105 NdisMoveMemory(pSHA_CTX->HashValue, SHA1_DefaultHashValue,
106 sizeof(SHA1_DefaultHashValue));
107 NdisZeroMemory(pSHA_CTX->Block, SHA1_BLOCK_SIZE);
108 pSHA_CTX->MessageLen = 0;
109 pSHA_CTX->BlockLen = 0;
110 } /* End of SHA1_Init */
114 ========================================================================
116 SHA1 computation for one block (512 bits)
119 pSHA_CTX Pointer to SHA1_CTX_STRUC
126 ========================================================================
129 IN SHA1_CTX_STRUC *pSHA_CTX)
133 UINT32 a,b,c,d,e,T,f_t = 0;
135 /* Prepare the message schedule, {W_i}, 0 < t < 15 */
136 NdisMoveMemory(W, pSHA_CTX->Block, SHA1_BLOCK_SIZE);
137 for (W_i = 0; W_i < 16; W_i++)
138 W[W_i] = cpu2be32(W[W_i]); /* Endian Swap */
141 /* SHA256 hash computation */
142 /* Initialize the working variables */
143 a = pSHA_CTX->HashValue[0];
144 b = pSHA_CTX->HashValue[1];
145 c = pSHA_CTX->HashValue[2];
146 d = pSHA_CTX->HashValue[3];
147 e = pSHA_CTX->HashValue[4];
150 for (t = 0;t < 80;t++) {
152 if (t > 15) { /* Prepare the message schedule, {W_i}, 16 < t < 79 */
153 W[s] = (W[(s+13) & SHA1_MASK]) ^ (W[(s+8) & SHA1_MASK]) ^ (W[(s+2) & SHA1_MASK]) ^ W[s];
154 W[s] = ROTL32(W[s],1);
169 } /* End of switch */
170 T = ROTL32(a,5) + f_t + e + SHA1_K[t / 20] + W[s];
178 /* Compute the i^th intermediate hash value H^(i) */
179 pSHA_CTX->HashValue[0] += a;
180 pSHA_CTX->HashValue[1] += b;
181 pSHA_CTX->HashValue[2] += c;
182 pSHA_CTX->HashValue[3] += d;
183 pSHA_CTX->HashValue[4] += e;
185 NdisZeroMemory(pSHA_CTX->Block, SHA1_BLOCK_SIZE);
186 pSHA_CTX->BlockLen = 0;
187 } /* End of SHA1_Hash */
191 ========================================================================
193 The message is appended to block. If block size > 64 bytes, the SHA1_Hash
197 pSHA_CTX Pointer to SHA1_CTX_STRUC
198 message Message context
199 messageLen The length of message in bytes
206 ========================================================================
209 IN SHA1_CTX_STRUC *pSHA_CTX,
210 IN const UINT8 Message[],
216 while (appendLen != MessageLen) {
217 diffLen = MessageLen - appendLen;
218 if ((pSHA_CTX->BlockLen + diffLen) < SHA1_BLOCK_SIZE) {
219 NdisMoveMemory(pSHA_CTX->Block + pSHA_CTX->BlockLen,
220 Message + appendLen, diffLen);
221 pSHA_CTX->BlockLen += diffLen;
222 appendLen += diffLen;
226 NdisMoveMemory(pSHA_CTX->Block + pSHA_CTX->BlockLen,
227 Message + appendLen, SHA1_BLOCK_SIZE - pSHA_CTX->BlockLen);
228 appendLen += (SHA1_BLOCK_SIZE - pSHA_CTX->BlockLen);
229 pSHA_CTX->BlockLen = SHA1_BLOCK_SIZE;
233 pSHA_CTX->MessageLen += MessageLen;
234 } /* End of SHA1_Append */
238 ========================================================================
240 1. Append bit 1 to end of the message
241 2. Append the length of message in rightmost 64 bits
242 3. Transform the Hash Value to digest message
245 pSHA_CTX Pointer to SHA1_CTX_STRUC
248 digestMessage Digest message
252 ========================================================================
255 IN SHA1_CTX_STRUC *pSHA_CTX,
256 OUT UINT8 DigestMessage[])
259 UINT64 message_length_bits;
261 /* Append bit 1 to end of the message */
262 NdisFillMemory(pSHA_CTX->Block + pSHA_CTX->BlockLen, 1, 0x80);
264 /* 55 = 64 - 8 - 1: append 1 bit(1 byte) and message length (8 bytes) */
265 if (pSHA_CTX->BlockLen > 55)
269 /* Append the length of message in rightmost 64 bits */
270 message_length_bits = pSHA_CTX->MessageLen*8;
271 message_length_bits = cpu2be64(message_length_bits);
272 NdisMoveMemory(&pSHA_CTX->Block[56], &message_length_bits, 8);
275 /* Return message digest, transform the UINT32 hash value to bytes */
276 for (index = 0; index < 5;index++)
277 pSHA_CTX->HashValue[index] = cpu2be32(pSHA_CTX->HashValue[index]);
279 NdisMoveMemory(DigestMessage, pSHA_CTX->HashValue, SHA1_DIGEST_SIZE);
280 } /* End of SHA1_End */
284 ========================================================================
289 message Message context
290 messageLen The length of message in bytes
293 digestMessage Digest message
297 ========================================================================
300 IN const UINT8 Message[],
302 OUT UINT8 DigestMessage[])
305 SHA1_CTX_STRUC sha_ctx;
307 NdisZeroMemory(&sha_ctx, sizeof(SHA1_CTX_STRUC));
309 SHA1_Append(&sha_ctx, Message, MessageLen);
310 SHA1_End(&sha_ctx, DigestMessage);
311 } /* End of RT_SHA1 */
312 #endif /* SHA1_SUPPORT */
315 #ifdef SHA256_SUPPORT
317 ========================================================================
319 Initial SHA256_CTX_STRUC
322 pSHA_CTX Pointer to SHA256_CTX_STRUC
329 ========================================================================
332 IN SHA256_CTX_STRUC *pSHA_CTX)
334 NdisMoveMemory(pSHA_CTX->HashValue, SHA256_DefaultHashValue,
335 sizeof(SHA256_DefaultHashValue));
336 NdisZeroMemory(pSHA_CTX->Block, SHA256_BLOCK_SIZE);
337 pSHA_CTX->MessageLen = 0;
338 pSHA_CTX->BlockLen = 0;
339 } /* End of SHA256_Init */
343 ========================================================================
345 SHA256 computation for one block (512 bits)
348 pSHA_CTX Pointer to SHA256_CTX_STRUC
355 ========================================================================
358 IN SHA256_CTX_STRUC *pSHA_CTX)
362 UINT32 a,b,c,d,e,f,g,h,T1,T2;
364 /* Prepare the message schedule, {W_i}, 0 < t < 15 */
365 NdisMoveMemory(W, pSHA_CTX->Block, SHA256_BLOCK_SIZE);
366 for (W_i = 0; W_i < 16; W_i++)
367 W[W_i] = cpu2be32(W[W_i]); /* Endian Swap */
370 /* SHA256 hash computation */
371 /* Initialize the working variables */
372 a = pSHA_CTX->HashValue[0];
373 b = pSHA_CTX->HashValue[1];
374 c = pSHA_CTX->HashValue[2];
375 d = pSHA_CTX->HashValue[3];
376 e = pSHA_CTX->HashValue[4];
377 f = pSHA_CTX->HashValue[5];
378 g = pSHA_CTX->HashValue[6];
379 h = pSHA_CTX->HashValue[7];
382 for (t = 0;t < 64;t++) {
383 if (t > 15) /* Prepare the message schedule, {W_i}, 16 < t < 63 */
384 W[t] = Sigma_256_1(W[t-2]) + W[t-7] + Sigma_256_0(W[t-15]) + W[t-16];
386 T1 = h + Zsigma_256_1(e) + Ch(e,f,g) + SHA256_K[t] + W[t];
387 T2 = Zsigma_256_0(a) + Maj(a,b,c);
398 /* Compute the i^th intermediate hash value H^(i) */
399 pSHA_CTX->HashValue[0] += a;
400 pSHA_CTX->HashValue[1] += b;
401 pSHA_CTX->HashValue[2] += c;
402 pSHA_CTX->HashValue[3] += d;
403 pSHA_CTX->HashValue[4] += e;
404 pSHA_CTX->HashValue[5] += f;
405 pSHA_CTX->HashValue[6] += g;
406 pSHA_CTX->HashValue[7] += h;
408 NdisZeroMemory(pSHA_CTX->Block, SHA256_BLOCK_SIZE);
409 pSHA_CTX->BlockLen = 0;
410 } /* End of SHA256_Hash */
414 ========================================================================
416 The message is appended to block. If block size > 64 bytes, the SHA256_Hash
420 pSHA_CTX Pointer to SHA256_CTX_STRUC
421 message Message context
422 messageLen The length of message in bytes
429 ========================================================================
432 IN SHA256_CTX_STRUC *pSHA_CTX,
433 IN const UINT8 Message[],
439 while (appendLen != MessageLen) {
440 diffLen = MessageLen - appendLen;
441 if ((pSHA_CTX->BlockLen + diffLen) < SHA256_BLOCK_SIZE) {
442 NdisMoveMemory(pSHA_CTX->Block + pSHA_CTX->BlockLen,
443 Message + appendLen, diffLen);
444 pSHA_CTX->BlockLen += diffLen;
445 appendLen += diffLen;
449 NdisMoveMemory(pSHA_CTX->Block + pSHA_CTX->BlockLen,
450 Message + appendLen, SHA256_BLOCK_SIZE - pSHA_CTX->BlockLen);
451 appendLen += (SHA256_BLOCK_SIZE - pSHA_CTX->BlockLen);
452 pSHA_CTX->BlockLen = SHA256_BLOCK_SIZE;
453 SHA256_Hash(pSHA_CTX);
456 pSHA_CTX->MessageLen += MessageLen;
457 } /* End of SHA256_Append */
461 ========================================================================
463 1. Append bit 1 to end of the message
464 2. Append the length of message in rightmost 64 bits
465 3. Transform the Hash Value to digest message
468 pSHA_CTX Pointer to SHA256_CTX_STRUC
471 digestMessage Digest message
475 ========================================================================
478 IN SHA256_CTX_STRUC *pSHA_CTX,
479 OUT UINT8 DigestMessage[])
482 UINT64 message_length_bits;
484 /* Append bit 1 to end of the message */
485 NdisFillMemory(pSHA_CTX->Block + pSHA_CTX->BlockLen, 1, 0x80);
487 /* 55 = 64 - 8 - 1: append 1 bit(1 byte) and message length (8 bytes) */
488 if (pSHA_CTX->BlockLen > 55)
489 SHA256_Hash(pSHA_CTX);
492 /* Append the length of message in rightmost 64 bits */
493 message_length_bits = pSHA_CTX->MessageLen*8;
494 message_length_bits = cpu2be64(message_length_bits);
495 NdisMoveMemory(&pSHA_CTX->Block[56], &message_length_bits, 8);
496 SHA256_Hash(pSHA_CTX);
498 /* Return message digest, transform the UINT32 hash value to bytes */
499 for (index = 0; index < 8;index++)
500 pSHA_CTX->HashValue[index] = cpu2be32(pSHA_CTX->HashValue[index]);
502 NdisMoveMemory(DigestMessage, pSHA_CTX->HashValue, SHA256_DIGEST_SIZE);
503 } /* End of SHA256_End */
507 ========================================================================
512 message Message context
513 messageLen The length of message in bytes
516 digestMessage Digest message
520 ========================================================================
523 IN const UINT8 Message[],
525 OUT UINT8 DigestMessage[])
527 SHA256_CTX_STRUC sha_ctx;
529 NdisZeroMemory(&sha_ctx, sizeof(SHA256_CTX_STRUC));
530 SHA256_Init(&sha_ctx);
531 SHA256_Append(&sha_ctx, Message, MessageLen);
532 SHA256_End(&sha_ctx, DigestMessage);
533 } /* End of RT_SHA256 */
534 #endif /* SHA256_SUPPORT */
536 /* End of crypt_sha2.c */