ltq-deu: make deu hash lock global and remove md5_hmac_ exports
[openwrt/staging/dedeckeh.git] / package / kernel / lantiq / ltq-deu / src / ifxmips_sha1_hmac.c
1 /******************************************************************************
2 **
3 ** FILE NAME : ifxmips_sha1_hmac.c
4 ** PROJECT : IFX UEIP
5 ** MODULES : DEU Module for UEIP
6 ** DATE : September 8, 2009
7 ** AUTHOR : Mohammad Firdaus
8 ** DESCRIPTION : Data Encryption Unit Driver
9 ** COPYRIGHT : Copyright (c) 2009
10 ** Infineon Technologies AG
11 ** Am Campeon 1-12, 85579 Neubiberg, Germany
12 **
13 ** This program is free software; you can redistribute it and/or modify
14 ** it under the terms of the GNU General Public License as published by
15 ** the Free Software Foundation; either version 2 of the License, or
16 ** (at your option) any later version.
17 **
18 ** HISTORY
19 ** $Date $Author $Comment
20 ** 08,Sept 2009 Mohammad Firdaus Initial UEIP release
21 ** 21,March 2011 Mohammad Firdaus Changes for Kernel 2.6.32 and IPSec integration
22 *******************************************************************************/
23 /*!
24 \defgroup IFX_DEU IFX_DEU_DRIVERS
25 \ingroup API
26 \brief ifx deu driver module
27 */
28
29 /*!
30 \file ifxmips_sha1_hmac.c
31 \ingroup IFX_DEU
32 \brief SHA1-HMAC deu driver file
33 */
34
35 /*!
36 \defgroup IFX_SHA1_HMAC_FUNCTIONS IFX_SHA1_HMAC_FUNCTIONS
37 \ingroup IFX_DEU
38 \brief ifx sha1 hmac functions
39 */
40
41
42 /* Project header */
43 #include <linux/init.h>
44 #include <linux/module.h>
45 #include <linux/mm.h>
46 #include <linux/crypto.h>
47 #include <crypto/internal/hash.h>
48 #include <crypto/sha.h>
49 #include <linux/types.h>
50 #include <linux/scatterlist.h>
51 #include <asm/byteorder.h>
52 #include <linux/delay.h>
53
54 #if defined(CONFIG_AR9)
55 #include "ifxmips_deu_ar9.h"
56 #elif defined(CONFIG_VR9) || defined(CONFIG_AR10)
57 #include "ifxmips_deu_vr9.h"
58 #else
59 #error "Plaform Unknwon!"
60 #endif
61
62 #define SHA1_DIGEST_SIZE 20
63 #define SHA1_HMAC_BLOCK_SIZE 64
64 #define SHA1_HMAC_DBN_TEMP_SIZE 1024 // size in dword, needed for dbn workaround
65 #define HASH_START IFX_HASH_CON
66
67 #define SHA1_HMAC_MAX_KEYLEN 64
68
69 #ifdef CRYPTO_DEBUG
70 extern char debug_level;
71 #define DPRINTF(level, format, args...) if (level < debug_level) printk(KERN_INFO "[%s %s %d]: " format, __FILE__, __func__, __LINE__, ##args);
72 #else
73 #define DPRINTF(level, format, args...)
74 #endif
75
76 struct sha1_hmac_ctx {
77 int keylen;
78
79 u8 buffer[SHA1_HMAC_BLOCK_SIZE];
80 u8 key[SHA1_HMAC_MAX_KEYLEN];
81 u32 state[5];
82 u32 dbn;
83 u64 count;
84
85 };
86
87 static u32 temp[SHA1_HMAC_DBN_TEMP_SIZE];
88
89 extern int disable_deudma;
90
91 /*! \fn static void sha1_hmac_transform(struct crypto_tfm *tfm, u32 const *in)
92 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
93 * \brief save input block to context
94 * \param tfm linux crypto algo transform
95 * \param in 64-byte block of input
96 */
97 static int sha1_hmac_transform(struct shash_desc *desc, u32 const *in)
98 {
99 struct sha1_hmac_ctx *sctx = crypto_shash_ctx(desc->tfm);
100
101 memcpy(&temp[sctx->dbn<<4], in, 64); //dbn workaround
102 sctx->dbn += 1;
103
104 if ( (sctx->dbn<<4) > SHA1_HMAC_DBN_TEMP_SIZE )
105 {
106 printk("SHA1_HMAC_DBN_TEMP_SIZE exceeded\n");
107 }
108
109 return 0;
110 }
111
112 /*! \fn int sha1_hmac_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
113 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
114 * \brief sets sha1 hmac key
115 * \param tfm linux crypto algo transform
116 * \param key input key
117 * \param keylen key length greater than 64 bytes IS NOT SUPPORTED
118 */
119 static int sha1_hmac_setkey(struct crypto_shash *tfm, const u8 *key, unsigned int keylen)
120 {
121 struct sha1_hmac_ctx *sctx = crypto_shash_ctx(tfm);
122 volatile struct deu_hash_t *hashs = (struct deu_hash_t *) HASH_START;
123
124 if (keylen > SHA1_HMAC_MAX_KEYLEN) {
125 printk("Key length exceeds maximum key length\n");
126 return -EINVAL;
127 }
128
129 //printk("Setting keys of len: %d\n", keylen);
130
131 hashs->KIDX |= 0x80000000; //reset keys back to 0
132 memcpy(&sctx->key, key, keylen);
133 sctx->keylen = keylen;
134
135 return 0;
136
137 }
138
139
140 /*! \fn int sha1_hmac_setkey_hw(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
141 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
142 * \brief sets sha1 hmac key into hw registers
143 * \param tfm linux crypto algo transform
144 * \param key input key
145 * \param keylen key length greater than 64 bytes IS NOT SUPPORTED
146 */
147 static int sha1_hmac_setkey_hw(const u8 *key, unsigned int keylen)
148 {
149 volatile struct deu_hash_t *hash = (struct deu_hash_t *) HASH_START;
150 int i, j;
151 unsigned long flag;
152 u32 *in_key = (u32 *)key;
153
154 j = 0;
155
156 CRTCL_SECT_HASH_START;
157 for (i = 0; i < keylen; i+=4)
158 {
159 hash->KIDX = j;
160 asm("sync");
161 hash->KEY = *((u32 *) in_key + j);
162 j++;
163 }
164
165 CRTCL_SECT_HASH_END;
166 return 0;
167 }
168
169 /*! \fn void sha1_hmac_init(struct crypto_tfm *tfm)
170 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
171 * \brief initialize sha1 hmac context
172 * \param tfm linux crypto algo transform
173 */
174 static int sha1_hmac_init(struct shash_desc *desc)
175 {
176 struct sha1_hmac_ctx *sctx = crypto_shash_ctx(desc->tfm);
177
178 //printk("debug ln: %d, fn: %s\n", __LINE__, __func__);
179 sctx->dbn = 0; //dbn workaround
180 sha1_hmac_setkey_hw(sctx->key, sctx->keylen);
181
182 return 0;
183 }
184
185 /*! \fn static void sha1_hmac_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
186 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
187 * \brief on-the-fly sha1 hmac computation
188 * \param tfm linux crypto algo transform
189 * \param data input data
190 * \param len size of input data
191 */
192 static int sha1_hmac_update(struct shash_desc *desc, const u8 *data,
193 unsigned int len)
194 {
195 struct sha1_hmac_ctx *sctx = crypto_shash_ctx(desc->tfm);
196 unsigned int i, j;
197
198 j = (sctx->count >> 3) & 0x3f;
199 sctx->count += len << 3;
200 // printk("sctx->count = %d\n", sctx->count);
201
202 if ((j + len) > 63) {
203 memcpy (&sctx->buffer[j], data, (i = 64 - j));
204 sha1_hmac_transform (desc, (const u32 *)sctx->buffer);
205 for (; i + 63 < len; i += 64) {
206 sha1_hmac_transform (desc, (const u32 *)&data[i]);
207 }
208
209 j = 0;
210 }
211 else
212 i = 0;
213
214 memcpy (&sctx->buffer[j], &data[i], len - i);
215 return 0;
216 }
217
218 /*! \fn static void sha1_hmac_final(struct crypto_tfm *tfm, u8 *out)
219 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
220 * \brief ompute final sha1 hmac value
221 * \param tfm linux crypto algo transform
222 * \param out final sha1 hmac output value
223 */
224 static int sha1_hmac_final(struct shash_desc *desc, u8 *out)
225 {
226 //struct sha1_hmac_ctx *sctx = shash_desc_ctx(desc);
227 struct sha1_hmac_ctx *sctx = crypto_shash_ctx(desc->tfm);
228 u32 index, padlen;
229 u64 t;
230 u8 bits[8] = { 0, };
231 static const u8 padding[64] = { 0x80, };
232 volatile struct deu_hash_t *hashs = (struct deu_hash_t *) HASH_START;
233 unsigned long flag;
234 int i = 0;
235 int dbn;
236 u32 *in = &temp[0];
237
238 t = sctx->count + 512; // need to add 512 bit of the IPAD operation
239 bits[7] = 0xff & t;
240 t >>= 8;
241 bits[6] = 0xff & t;
242 t >>= 8;
243 bits[5] = 0xff & t;
244 t >>= 8;
245 bits[4] = 0xff & t;
246 t >>= 8;
247 bits[3] = 0xff & t;
248 t >>= 8;
249 bits[2] = 0xff & t;
250 t >>= 8;
251 bits[1] = 0xff & t;
252 t >>= 8;
253 bits[0] = 0xff & t;
254
255 /* Pad out to 56 mod 64 */
256 index = (sctx->count >> 3) & 0x3f;
257 padlen = (index < 56) ? (56 - index) : ((64 + 56) - index);
258 sha1_hmac_update (desc, padding, padlen);
259
260 /* Append length */
261 sha1_hmac_update (desc, bits, sizeof bits);
262
263 CRTCL_SECT_HASH_START;
264
265 hashs->DBN = sctx->dbn;
266
267 //for vr9 change, ENDI = 1
268 *IFX_HASH_CON = HASH_CON_VALUE;
269
270 //wait for processing
271 while (hashs->controlr.BSY) {
272 // this will not take long
273 }
274
275 for (dbn = 0; dbn < sctx->dbn; dbn++)
276 {
277 for (i = 0; i < 16; i++) {
278 hashs->MR = in[i];
279 };
280
281 hashs->controlr.GO = 1;
282 asm("sync");
283
284 //wait for processing
285 while (hashs->controlr.BSY) {
286 // this will not take long
287 }
288
289 in += 16;
290 }
291
292
293 #if 1
294 //wait for digest ready
295 while (! hashs->controlr.DGRY) {
296 // this will not take long
297 }
298 #endif
299
300 *((u32 *) out + 0) = hashs->D1R;
301 *((u32 *) out + 1) = hashs->D2R;
302 *((u32 *) out + 2) = hashs->D3R;
303 *((u32 *) out + 3) = hashs->D4R;
304 *((u32 *) out + 4) = hashs->D5R;
305
306 memset(&sctx->buffer[0], 0, SHA1_HMAC_BLOCK_SIZE);
307 sctx->count = 0;
308
309 //printk("debug ln: %d, fn: %s\n", __LINE__, __func__);
310 CRTCL_SECT_HASH_END;
311
312
313 return 0;
314
315 }
316
317 /*
318 * \brief SHA1-HMAC function mappings
319 */
320 static struct shash_alg ifxdeu_sha1_hmac_alg = {
321 .digestsize = SHA1_DIGEST_SIZE,
322 .init = sha1_hmac_init,
323 .update = sha1_hmac_update,
324 .final = sha1_hmac_final,
325 .setkey = sha1_hmac_setkey,
326 .descsize = sizeof(struct sha1_hmac_ctx),
327 .base = {
328 .cra_name = "hmac(sha1)",
329 .cra_driver_name= "ifxdeu-sha1_hmac",
330 .cra_priority = 400,
331 .cra_ctxsize = sizeof(struct sha1_hmac_ctx),
332 .cra_flags = CRYPTO_ALG_TYPE_HASH | CRYPTO_ALG_KERN_DRIVER_ONLY,
333 .cra_blocksize = SHA1_HMAC_BLOCK_SIZE,
334 .cra_module = THIS_MODULE,
335 }
336
337 };
338
339
340 /*! \fn int ifxdeu_init_sha1_hmac (void)
341 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
342 * \brief initialize sha1 hmac driver
343 */
344 int ifxdeu_init_sha1_hmac (void)
345 {
346 int ret = -ENOSYS;
347
348
349
350 if ((ret = crypto_register_shash(&ifxdeu_sha1_hmac_alg)))
351 goto sha1_err;
352
353 printk (KERN_NOTICE "IFX DEU SHA1_HMAC initialized%s.\n", disable_deudma ? "" : " (DMA)");
354 return ret;
355
356 sha1_err:
357 printk(KERN_ERR "IFX DEU SHA1_HMAC initialization failed!\n");
358 return ret;
359 }
360
361 /*! \fn void ifxdeu_fini_sha1_hmac (void)
362 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
363 * \brief unregister sha1 hmac driver
364 */
365 void ifxdeu_fini_sha1_hmac (void)
366 {
367
368 crypto_unregister_shash(&ifxdeu_sha1_hmac_alg);
369
370
371 }
372