• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/drivers/staging/rt2860/common/
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/* 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 */
35
36/* Basic functions */
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)
40
41#ifdef SHA1_SUPPORT
42/* SHA1 constants */
43#define SHA1_MASK 0x0000000f
44static const u32 SHA1_K[4] = {
45	0x5a827999UL, 0x6ed9eba1UL, 0x8f1bbcdcUL, 0xca62c1d6UL
46};
47
48static const u32 SHA1_DefaultHashValue[5] = {
49	0x67452301UL, 0xefcdab89UL, 0x98badcfeUL, 0x10325476UL, 0xc3d2e1f0UL
50};
51
52/*
53========================================================================
54Routine Description:
55    Initial struct rt_sha1_ctx
56
57Arguments:
58    pSHA_CTX        Pointer to struct rt_sha1_ctx
59
60Return Value:
61    None
62
63Note:
64    None
65========================================================================
66*/
67void RT_SHA1_Init(struct rt_sha1_ctx *pSHA_CTX)
68{
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 */
75
76/*
77========================================================================
78Routine Description:
79    SHA1 computation for one block (512 bits)
80
81Arguments:
82    pSHA_CTX        Pointer to struct rt_sha1_ctx
83
84Return Value:
85    None
86
87Note:
88    None
89========================================================================
90*/
91void SHA1_Hash(struct rt_sha1_ctx *pSHA_CTX)
92{
93	u32 W_i, t, s;
94	u32 W[16];
95	u32 a, b, c, d, e, T, f_t = 0;
96
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 */
101	/* End of for */
102
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];
110
111	/* 80 rounds */
112	for (t = 0; t < 80; t++) {
113		s = t & SHA1_MASK;
114		if (t > 15) {	/* Prepare the message schedule, {W_i}, 16 < t < 79 */
115			W[s] =
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);
119		}		/* End of if */
120		switch (t / 20) {
121		case 0:
122			f_t = Ch(b, c, d);
123			break;
124		case 1:
125			f_t = Parity(b, c, d);
126			break;
127		case 2:
128			f_t = Maj(b, c, d);
129			break;
130		case 3:
131			f_t = Parity(b, c, d);
132			break;
133		}		/* End of switch */
134		T = ROTL32(a, 5) + f_t + e + SHA1_K[t / 20] + W[s];
135		e = d;
136		d = c;
137		c = ROTL32(b, 30);
138		b = a;
139		a = T;
140	}			/* End of for */
141
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;
148
149	NdisZeroMemory(pSHA_CTX->Block, SHA1_BLOCK_SIZE);
150	pSHA_CTX->BlockLen = 0;
151}				/* End of SHA1_Hash */
152
153/*
154========================================================================
155Routine Description:
156    The message is appended to block. If block size > 64 bytes, the SHA1_Hash
157will be called.
158
159Arguments:
160    pSHA_CTX        Pointer to struct rt_sha1_ctx
161    message         Message context
162    messageLen      The length of message in bytes
163
164Return Value:
165    None
166
167Note:
168    None
169========================================================================
170*/
171void SHA1_Append(struct rt_sha1_ctx *pSHA_CTX,
172		 IN const u8 Message[], u32 MessageLen)
173{
174	u32 appendLen = 0;
175	u32 diffLen = 0;
176
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;
184		} else {
185			NdisMoveMemory(pSHA_CTX->Block + pSHA_CTX->BlockLen,
186				       Message + appendLen,
187				       SHA1_BLOCK_SIZE - pSHA_CTX->BlockLen);
188			appendLen += (SHA1_BLOCK_SIZE - pSHA_CTX->BlockLen);
189			pSHA_CTX->BlockLen = SHA1_BLOCK_SIZE;
190			SHA1_Hash(pSHA_CTX);
191		}		/* End of if */
192	}			/* End of while */
193	pSHA_CTX->MessageLen += MessageLen;
194}				/* End of SHA1_Append */
195
196/*
197========================================================================
198Routine Description:
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
202
203Arguments:
204    pSHA_CTX        Pointer to struct rt_sha1_ctx
205
206Return Value:
207    digestMessage   Digest message
208
209Note:
210    None
211========================================================================
212*/
213void SHA1_End(struct rt_sha1_ctx *pSHA_CTX, u8 DigestMessage[])
214{
215	u32 index;
216	u64 message_length_bits;
217
218	/* Append bit 1 to end of the message */
219	NdisFillMemory(pSHA_CTX->Block + pSHA_CTX->BlockLen, 1, 0x80);
220
221	/* 55 = 64 - 8 - 1: append 1 bit(1 byte) and message length (8 bytes) */
222	if (pSHA_CTX->BlockLen > 55)
223		SHA1_Hash(pSHA_CTX);
224	/* End of if */
225
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);
230	SHA1_Hash(pSHA_CTX);
231
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]);
236	/* End of for */
237	NdisMoveMemory(DigestMessage, pSHA_CTX->HashValue, SHA1_DIGEST_SIZE);
238}				/* End of SHA1_End */
239
240/*
241========================================================================
242Routine Description:
243    SHA1 algorithm
244
245Arguments:
246    message         Message context
247    messageLen      The length of message in bytes
248
249Return Value:
250    digestMessage   Digest message
251
252Note:
253    None
254========================================================================
255*/
256void RT_SHA1(IN const u8 Message[],
257	     u32 MessageLen, u8 DigestMessage[])
258{
259
260	struct rt_sha1_ctx sha_ctx;
261
262	NdisZeroMemory(&sha_ctx, sizeof(struct rt_sha1_ctx));
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 */
268
269/* End of crypt_sha2.c */
270