Linux-libre 2.6.32.6-gnu1
[librecmc/linux-libre.git] / drivers / staging / rt3090 / common / crypt_sha2.c
1 /*
2  *************************************************************************
3  * Ralink Tech Inc.
4  * 5F., No.36, Taiyuan St., Jhubei City,
5  * Hsinchu County 302,
6  * Taiwan, R.O.C.
7  *
8  * (c) Copyright 2002-2007, Ralink Technology, Inc.
9  *
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.                                   *
14  *                                                                       *
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.                          *
19  *                                                                       *
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.             *
24  *                                                                       *
25  *************************************************************************/
26
27 #include "../crypt_sha2.h"
28
29
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 */
36
37 /* Basic functions */
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)
41
42 #ifdef SHA1_SUPPORT
43 /* SHA1 constants */
44 #define SHA1_MASK 0x0000000f
45 static const UINT32 SHA1_K[4] = {
46     0x5a827999UL, 0x6ed9eba1UL, 0x8f1bbcdcUL, 0xca62c1d6UL
47 };
48 static const UINT32 SHA1_DefaultHashValue[5] = {
49     0x67452301UL, 0xefcdab89UL, 0x98badcfeUL, 0x10325476UL, 0xc3d2e1f0UL
50 };
51 #endif /* SHA1_SUPPORT */
52
53
54 #ifdef SHA256_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
78 };
79 static const UINT32 SHA256_DefaultHashValue[8] = {
80     0x6a09e667UL, 0xbb67ae85UL, 0x3c6ef372UL, 0xa54ff53aUL,
81     0x510e527fUL, 0x9b05688cUL, 0x1f83d9abUL, 0x5be0cd19UL
82 };
83 #endif /* SHA256_SUPPORT */
84
85
86 #ifdef SHA1_SUPPORT
87 /*
88 ========================================================================
89 Routine Description:
90     Initial SHA1_CTX_STRUC
91
92 Arguments:
93     pSHA_CTX        Pointer to SHA1_CTX_STRUC
94
95 Return Value:
96     None
97
98 Note:
99     None
100 ========================================================================
101 */
102 VOID SHA1_Init (
103     IN  SHA1_CTX_STRUC *pSHA_CTX)
104 {
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 */
111
112
113 /*
114 ========================================================================
115 Routine Description:
116     SHA1 computation for one block (512 bits)
117
118 Arguments:
119     pSHA_CTX        Pointer to SHA1_CTX_STRUC
120
121 Return Value:
122     None
123
124 Note:
125     None
126 ========================================================================
127 */
128 VOID SHA1_Hash (
129     IN  SHA1_CTX_STRUC *pSHA_CTX)
130 {
131     UINT32 W_i,t,s;
132     UINT32 W[16];
133     UINT32 a,b,c,d,e,T,f_t = 0;
134
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 */
139         /* End of for */
140
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];
148
149     /* 80 rounds */
150     for (t = 0;t < 80;t++) {
151         s = t & SHA1_MASK;
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);
155         } /* End of if */
156         switch (t / 20) {
157             case 0:
158                  f_t = Ch(b,c,d);
159                  break;
160             case 1:
161                  f_t = Parity(b,c,d);
162                  break;
163             case 2:
164                  f_t = Maj(b,c,d);
165                  break;
166             case 3:
167                  f_t = Parity(b,c,d);
168                  break;
169         } /* End of switch */
170         T = ROTL32(a,5) + f_t + e + SHA1_K[t / 20] + W[s];
171         e = d;
172         d = c;
173         c = ROTL32(b,30);
174         b = a;
175         a = T;
176      } /* End of for */
177
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;
184
185     NdisZeroMemory(pSHA_CTX->Block, SHA1_BLOCK_SIZE);
186     pSHA_CTX->BlockLen = 0;
187 } /* End of SHA1_Hash */
188
189
190 /*
191 ========================================================================
192 Routine Description:
193     The message is appended to block. If block size > 64 bytes, the SHA1_Hash
194 will be called.
195
196 Arguments:
197     pSHA_CTX        Pointer to SHA1_CTX_STRUC
198     message         Message context
199     messageLen      The length of message in bytes
200
201 Return Value:
202     None
203
204 Note:
205     None
206 ========================================================================
207 */
208 VOID SHA1_Append (
209     IN  SHA1_CTX_STRUC *pSHA_CTX,
210     IN  const UINT8 Message[],
211     IN  UINT MessageLen)
212 {
213     UINT appendLen = 0;
214     UINT diffLen   = 0;
215
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;
223         }
224         else
225         {
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;
230             SHA1_Hash(pSHA_CTX);
231         } /* End of if */
232     } /* End of while */
233     pSHA_CTX->MessageLen += MessageLen;
234 } /* End of SHA1_Append */
235
236
237 /*
238 ========================================================================
239 Routine Description:
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
243
244 Arguments:
245     pSHA_CTX        Pointer to SHA1_CTX_STRUC
246
247 Return Value:
248     digestMessage   Digest message
249
250 Note:
251     None
252 ========================================================================
253 */
254 VOID SHA1_End (
255     IN  SHA1_CTX_STRUC *pSHA_CTX,
256     OUT UINT8 DigestMessage[])
257 {
258     UINT index;
259     UINT64 message_length_bits;
260
261     /* Append bit 1 to end of the message */
262     NdisFillMemory(pSHA_CTX->Block + pSHA_CTX->BlockLen, 1, 0x80);
263
264     /* 55 = 64 - 8 - 1: append 1 bit(1 byte) and message length (8 bytes) */
265     if (pSHA_CTX->BlockLen > 55)
266         SHA1_Hash(pSHA_CTX);
267         /* End of if */
268
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);
273     SHA1_Hash(pSHA_CTX);
274
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]);
278         /* End of for */
279     NdisMoveMemory(DigestMessage, pSHA_CTX->HashValue, SHA1_DIGEST_SIZE);
280 } /* End of SHA1_End */
281
282
283 /*
284 ========================================================================
285 Routine Description:
286     SHA1 algorithm
287
288 Arguments:
289     message         Message context
290     messageLen      The length of message in bytes
291
292 Return Value:
293     digestMessage   Digest message
294
295 Note:
296     None
297 ========================================================================
298 */
299 VOID RT_SHA1 (
300     IN  const UINT8 Message[],
301     IN  UINT MessageLen,
302     OUT UINT8 DigestMessage[])
303 {
304
305     SHA1_CTX_STRUC sha_ctx;
306
307     NdisZeroMemory(&sha_ctx, sizeof(SHA1_CTX_STRUC));
308     SHA1_Init(&sha_ctx);
309     SHA1_Append(&sha_ctx, Message, MessageLen);
310     SHA1_End(&sha_ctx, DigestMessage);
311 } /* End of RT_SHA1 */
312 #endif /* SHA1_SUPPORT */
313
314
315 #ifdef SHA256_SUPPORT
316 /*
317 ========================================================================
318 Routine Description:
319     Initial SHA256_CTX_STRUC
320
321 Arguments:
322     pSHA_CTX    Pointer to SHA256_CTX_STRUC
323
324 Return Value:
325     None
326
327 Note:
328     None
329 ========================================================================
330 */
331 VOID SHA256_Init (
332     IN  SHA256_CTX_STRUC *pSHA_CTX)
333 {
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 */
340
341
342 /*
343 ========================================================================
344 Routine Description:
345     SHA256 computation for one block (512 bits)
346
347 Arguments:
348     pSHA_CTX    Pointer to SHA256_CTX_STRUC
349
350 Return Value:
351     None
352
353 Note:
354     None
355 ========================================================================
356 */
357 VOID SHA256_Hash (
358     IN  SHA256_CTX_STRUC *pSHA_CTX)
359 {
360     UINT32 W_i,t;
361     UINT32 W[64];
362     UINT32 a,b,c,d,e,f,g,h,T1,T2;
363
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 */
368         /* End of for */
369
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];
380
381     /* 64 rounds */
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];
385             /* End of if */
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);
388         h = g;
389         g = f;
390         f = e;
391         e = d + T1;
392         d = c;
393         c = b;
394         b = a;
395         a = T1 + T2;
396      } /* End of for */
397
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;
407
408     NdisZeroMemory(pSHA_CTX->Block, SHA256_BLOCK_SIZE);
409     pSHA_CTX->BlockLen = 0;
410 } /* End of SHA256_Hash */
411
412
413 /*
414 ========================================================================
415 Routine Description:
416     The message is appended to block. If block size > 64 bytes, the SHA256_Hash
417 will be called.
418
419 Arguments:
420     pSHA_CTX    Pointer to SHA256_CTX_STRUC
421     message     Message context
422     messageLen  The length of message in bytes
423
424 Return Value:
425     None
426
427 Note:
428     None
429 ========================================================================
430 */
431 VOID SHA256_Append (
432     IN  SHA256_CTX_STRUC *pSHA_CTX,
433     IN  const UINT8 Message[],
434     IN  UINT MessageLen)
435 {
436     UINT appendLen = 0;
437     UINT diffLen   = 0;
438
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;
446         }
447         else
448         {
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);
454         } /* End of if */
455     } /* End of while */
456     pSHA_CTX->MessageLen += MessageLen;
457 } /* End of SHA256_Append */
458
459
460 /*
461 ========================================================================
462 Routine Description:
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
466
467 Arguments:
468     pSHA_CTX        Pointer to SHA256_CTX_STRUC
469
470 Return Value:
471     digestMessage   Digest message
472
473 Note:
474     None
475 ========================================================================
476 */
477 VOID SHA256_End (
478     IN  SHA256_CTX_STRUC *pSHA_CTX,
479     OUT UINT8 DigestMessage[])
480 {
481     UINT index;
482     UINT64 message_length_bits;
483
484     /* Append bit 1 to end of the message */
485     NdisFillMemory(pSHA_CTX->Block + pSHA_CTX->BlockLen, 1, 0x80);
486
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);
490         /* End of if */
491
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);
497
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]);
501         /* End of for */
502     NdisMoveMemory(DigestMessage, pSHA_CTX->HashValue, SHA256_DIGEST_SIZE);
503 } /* End of SHA256_End */
504
505
506 /*
507 ========================================================================
508 Routine Description:
509     SHA256 algorithm
510
511 Arguments:
512     message         Message context
513     messageLen      The length of message in bytes
514
515 Return Value:
516     digestMessage   Digest message
517
518 Note:
519     None
520 ========================================================================
521 */
522 VOID RT_SHA256 (
523     IN  const UINT8 Message[],
524     IN  UINT MessageLen,
525     OUT UINT8 DigestMessage[])
526 {
527     SHA256_CTX_STRUC sha_ctx;
528
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 */
535
536 /* End of crypt_sha2.c */