ltq-deu: update initialisations for hmac algorithms
[openwrt/staging/wigyori.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 /* Project header */
42 #include <linux/init.h>
43 #include <linux/module.h>
44 #include <linux/mm.h>
45 #include <linux/crypto.h>
46 #include <crypto/internal/hash.h>
47 #include <crypto/sha.h>
48 #include <linux/types.h>
49 #include <linux/scatterlist.h>
50 #include <asm/byteorder.h>
51 #include <linux/delay.h>
52
53 #if defined(CONFIG_AR9)
54 #include "ifxmips_deu_ar9.h"
55 #elif defined(CONFIG_VR9) || defined(CONFIG_AR10)
56 #include "ifxmips_deu_vr9.h"
57 #else
58 #error "Plaform Unknwon!"
59 #endif
60
61 #define SHA1_DIGEST_SIZE 20
62 #define SHA1_BLOCK_WORDS 16
63 #define SHA1_HASH_WORDS 5
64 #define SHA1_HMAC_BLOCK_SIZE 64
65 #define SHA1_HMAC_DBN_TEMP_SIZE 1024 // size in dword, needed for dbn workaround
66 #define HASH_START IFX_HASH_CON
67
68 #define SHA1_HMAC_MAX_KEYLEN 64
69
70 #ifdef CRYPTO_DEBUG
71 extern char debug_level;
72 #define DPRINTF(level, format, args...) if (level < debug_level) printk(KERN_INFO "[%s %s %d]: " format, __FILE__, __func__, __LINE__, ##args);
73 #else
74 #define DPRINTF(level, format, args...)
75 #endif
76
77 struct sha1_hmac_ctx {
78 int keylen;
79
80 u8 buffer[SHA1_HMAC_BLOCK_SIZE];
81 u8 key[SHA1_HMAC_MAX_KEYLEN];
82 u32 hash[SHA1_HASH_WORDS];
83 u32 dbn;
84 int started;
85 u64 count;
86
87 struct shash_desc *desc;
88 u32 (*temp)[SHA1_BLOCK_WORDS];
89 };
90
91 extern int disable_deudma;
92
93 static int sha1_hmac_final_impl(struct shash_desc *desc, u8 *out, bool hash_final);
94
95 /*! \fn static void sha1_hmac_transform(struct crypto_tfm *tfm, u32 const *in)
96 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
97 * \brief save input block to context
98 * \param tfm linux crypto algo transform
99 * \param in 64-byte block of input
100 */
101 static int sha1_hmac_transform(struct shash_desc *desc, u32 const *in)
102 {
103 struct sha1_hmac_ctx *sctx = crypto_shash_ctx(desc->tfm);
104
105 if ( ((sctx->dbn<<4)+1) > SHA1_HMAC_DBN_TEMP_SIZE )
106 {
107 //printk("SHA1_HMAC_DBN_TEMP_SIZE exceeded\n");
108 sha1_hmac_final_impl(desc, (u8 *)sctx->hash, false);
109 }
110
111 memcpy(&sctx->temp[sctx->dbn], in, 64); //dbn workaround
112 sctx->dbn += 1;
113
114 return 0;
115 }
116
117 /*! \fn int sha1_hmac_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
118 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
119 * \brief sets sha1 hmac key
120 * \param tfm linux crypto algo transform
121 * \param key input key
122 * \param keylen key length greater than 64 bytes IS NOT SUPPORTED
123 */
124 static int sha1_hmac_setkey(struct crypto_shash *tfm, const u8 *key, unsigned int keylen)
125 {
126 struct sha1_hmac_ctx *sctx = crypto_shash_ctx(tfm);
127 int err;
128
129 if (keylen > SHA1_HMAC_MAX_KEYLEN) {
130 char *hash_alg_name = "sha1";
131
132 sctx->desc->tfm = crypto_alloc_shash(hash_alg_name, 0, 0);
133 if (IS_ERR(sctx->desc->tfm)) return PTR_ERR(sctx->desc->tfm);
134
135 memset(sctx->key, 0, SHA1_HMAC_MAX_KEYLEN);
136 err = crypto_shash_digest(sctx->desc, key, keylen, sctx->key);
137 if (err) return err;
138
139 sctx->keylen = SHA1_DIGEST_SIZE;
140
141 crypto_free_shash(sctx->desc->tfm);
142 } else {
143 memcpy(sctx->key, key, keylen);
144 sctx->keylen = keylen;
145 }
146 memset(sctx->key + sctx->keylen, 0, SHA1_HMAC_MAX_KEYLEN - sctx->keylen);
147
148 //printk("Setting keys of len: %d\n", keylen);
149
150 return 0;
151 }
152
153 /*! \fn int sha1_hmac_setkey_hw(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
154 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
155 * \brief sets sha1 hmac key into hw registers
156 * \param tfm linux crypto algo transform
157 * \param key input key
158 * \param keylen key length greater than 64 bytes IS NOT SUPPORTED
159 */
160 static int sha1_hmac_setkey_hw(const u8 *key, unsigned int keylen)
161 {
162 volatile struct deu_hash_t *hash = (struct deu_hash_t *) HASH_START;
163 int i, j;
164 u32 *in_key = (u32 *)key;
165
166 j = 0;
167
168 hash->KIDX |= 0x80000000; //reset keys back to 0
169 for (i = 0; i < keylen; i+=4)
170 {
171 hash->KIDX = j;
172 asm("sync");
173 hash->KEY = *((u32 *) in_key + j);
174 j++;
175 }
176
177 return 0;
178 }
179
180 /*! \fn void sha1_hmac_init(struct crypto_tfm *tfm)
181 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
182 * \brief initialize sha1 hmac context
183 * \param tfm linux crypto algo transform
184 */
185 static int sha1_hmac_init(struct shash_desc *desc)
186 {
187 struct sha1_hmac_ctx *sctx = crypto_shash_ctx(desc->tfm);
188
189 //printk("debug ln: %d, fn: %s\n", __LINE__, __func__);
190 sctx->dbn = 0; //dbn workaround
191 sctx->started = 0;
192 sctx->count = 0;
193
194 return 0;
195 }
196
197 /*! \fn static void sha1_hmac_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
198 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
199 * \brief on-the-fly sha1 hmac computation
200 * \param tfm linux crypto algo transform
201 * \param data input data
202 * \param len size of input data
203 */
204 static int sha1_hmac_update(struct shash_desc *desc, const u8 *data,
205 unsigned int len)
206 {
207 struct sha1_hmac_ctx *sctx = crypto_shash_ctx(desc->tfm);
208 unsigned int i, j;
209
210 j = (sctx->count >> 3) & 0x3f;
211 sctx->count += len << 3;
212 // printk("sctx->count = %d\n", sctx->count);
213
214 if ((j + len) > 63) {
215 memcpy (&sctx->buffer[j], data, (i = 64 - j));
216 sha1_hmac_transform (desc, (const u32 *)sctx->buffer);
217 for (; i + 63 < len; i += 64) {
218 sha1_hmac_transform (desc, (const u32 *)&data[i]);
219 }
220
221 j = 0;
222 }
223 else
224 i = 0;
225
226 memcpy (&sctx->buffer[j], &data[i], len - i);
227 return 0;
228 }
229
230 /*! \fn static int sha1_hmac_final(struct crypto_tfm *tfm, u8 *out)
231 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
232 * \brief call sha1_hmac_final_impl with hash_final true
233 * \param tfm linux crypto algo transform
234 * \param out final sha1 hmac output value
235 */
236 static int sha1_hmac_final(struct shash_desc *desc, u8 *out)
237 {
238 return sha1_hmac_final_impl(desc, out, true);
239 }
240
241 /*! \fn static int sha1_hmac_final_impl(struct crypto_tfm *tfm, u8 *out, bool hash_final)
242 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
243 * \brief ompute final or intermediate sha1 hmac value
244 * \param tfm linux crypto algo transform
245 * \param out final sha1 hmac output value
246 * \param in finalize or intermediate processing
247 */
248 static int sha1_hmac_final_impl(struct shash_desc *desc, u8 *out, bool hash_final)
249 {
250 struct sha1_hmac_ctx *sctx = crypto_shash_ctx(desc->tfm);
251 u32 index, padlen;
252 u64 t;
253 u8 bits[8] = { 0, };
254 static const u8 padding[64] = { 0x80, };
255 volatile struct deu_hash_t *hashs = (struct deu_hash_t *) HASH_START;
256 unsigned long flag;
257 int i = 0;
258 int dbn;
259 u32 *in = sctx->temp[0];
260
261 if (hash_final) {
262 t = sctx->count + 512; // need to add 512 bit of the IPAD operation
263 bits[7] = 0xff & t;
264 t >>= 8;
265 bits[6] = 0xff & t;
266 t >>= 8;
267 bits[5] = 0xff & t;
268 t >>= 8;
269 bits[4] = 0xff & t;
270 t >>= 8;
271 bits[3] = 0xff & t;
272 t >>= 8;
273 bits[2] = 0xff & t;
274 t >>= 8;
275 bits[1] = 0xff & t;
276 t >>= 8;
277 bits[0] = 0xff & t;
278
279 /* Pad out to 56 mod 64 */
280 index = (sctx->count >> 3) & 0x3f;
281 padlen = (index < 56) ? (56 - index) : ((64 + 56) - index);
282 sha1_hmac_update (desc, padding, padlen);
283
284 /* Append length */
285 sha1_hmac_update (desc, bits, sizeof bits);
286 }
287
288 CRTCL_SECT_HASH_START;
289
290 SHA_HASH_INIT;
291
292 sha1_hmac_setkey_hw(sctx->key, sctx->keylen);
293
294 if (hash_final) {
295 hashs->DBN = sctx->dbn;
296 } else {
297 hashs->DBN = sctx->dbn + 5;
298 }
299 asm("sync");
300
301 //for vr9 change, ENDI = 1
302 *IFX_HASH_CON = HASH_CON_VALUE;
303
304 //wait for processing
305 while (hashs->controlr.BSY) {
306 // this will not take long
307 }
308
309 if (sctx->started) {
310 hashs->D1R = *((u32 *) sctx->hash + 0);
311 hashs->D2R = *((u32 *) sctx->hash + 1);
312 hashs->D3R = *((u32 *) sctx->hash + 2);
313 hashs->D4R = *((u32 *) sctx->hash + 3);
314 hashs->D5R = *((u32 *) sctx->hash + 4);
315 } else {
316 sctx->started = 1;
317 }
318
319 for (dbn = 0; dbn < sctx->dbn; dbn++)
320 {
321 for (i = 0; i < 16; i++) {
322 hashs->MR = in[i];
323 };
324
325 hashs->controlr.GO = 1;
326 asm("sync");
327
328 //wait for processing
329 while (hashs->controlr.BSY) {
330 // this will not take long
331 }
332
333 in += 16;
334 }
335
336
337 #if 1
338 if (hash_final) {
339 //wait for digest ready
340 while (! hashs->controlr.DGRY) {
341 // this will not take long
342 }
343 }
344 #endif
345
346 *((u32 *) out + 0) = hashs->D1R;
347 *((u32 *) out + 1) = hashs->D2R;
348 *((u32 *) out + 2) = hashs->D3R;
349 *((u32 *) out + 3) = hashs->D4R;
350 *((u32 *) out + 4) = hashs->D5R;
351
352 CRTCL_SECT_HASH_END;
353
354 if (hash_final) {
355 sha1_hmac_init(desc);
356 } else {
357 sctx->dbn = 0;
358 }
359 //printk("debug ln: %d, fn: %s\n", __LINE__, __func__);
360
361 return 0;
362
363 }
364
365 /*! \fn void sha1_hmac_init_tfm(struct crypto_tfm *tfm)
366 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
367 * \brief initialize pointers in sha1_hmac_ctx
368 * \param tfm linux crypto algo transform
369 */
370 static int sha1_hmac_init_tfm(struct crypto_tfm *tfm)
371 {
372 struct sha1_hmac_ctx *sctx = crypto_tfm_ctx(tfm);
373 sctx->temp = kzalloc(4 * SHA1_HMAC_DBN_TEMP_SIZE, GFP_KERNEL);
374 if (IS_ERR(sctx->temp)) return PTR_ERR(sctx->temp);
375 sctx->desc = kzalloc(sizeof(struct shash_desc), GFP_KERNEL);
376 if (IS_ERR(sctx->desc)) return PTR_ERR(sctx->desc);
377
378 return 0;
379 }
380
381 /*! \fn void sha1_hmac_exit_tfm(struct crypto_tfm *tfm)
382 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
383 * \brief free pointers in sha1_hmac_ctx
384 * \param tfm linux crypto algo transform
385 */
386 static void sha1_hmac_exit_tfm(struct crypto_tfm *tfm)
387 {
388 struct sha1_hmac_ctx *sctx = crypto_tfm_ctx(tfm);
389 kfree(sctx->temp);
390 kfree(sctx->desc);
391 }
392
393 /*
394 * \brief SHA1_HMAC function mappings
395 */
396
397 static struct shash_alg ifxdeu_sha1_hmac_alg = {
398 .digestsize = SHA1_DIGEST_SIZE,
399 .init = sha1_hmac_init,
400 .update = sha1_hmac_update,
401 .final = sha1_hmac_final,
402 .setkey = sha1_hmac_setkey,
403 .descsize = sizeof(struct sha1_hmac_ctx),
404 .base = {
405 .cra_name = "hmac(sha1)",
406 .cra_driver_name= "ifxdeu-sha1_hmac",
407 .cra_priority = 400,
408 .cra_ctxsize = sizeof(struct sha1_hmac_ctx),
409 .cra_flags = CRYPTO_ALG_TYPE_HASH | CRYPTO_ALG_KERN_DRIVER_ONLY,
410 .cra_blocksize = SHA1_HMAC_BLOCK_SIZE,
411 .cra_module = THIS_MODULE,
412 .cra_init = sha1_hmac_init_tfm,
413 .cra_exit = sha1_hmac_exit_tfm,
414 }
415 };
416
417 /*! \fn int ifxdeu_init_sha1_hmac (void)
418 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
419 * \brief initialize sha1 hmac driver
420 */
421 int ifxdeu_init_sha1_hmac (void)
422 {
423 int ret = -ENOSYS;
424
425
426
427 if ((ret = crypto_register_shash(&ifxdeu_sha1_hmac_alg)))
428 goto sha1_err;
429
430 printk (KERN_NOTICE "IFX DEU SHA1_HMAC initialized%s.\n", disable_deudma ? "" : " (DMA)");
431 return ret;
432
433 sha1_err:
434 printk(KERN_ERR "IFX DEU SHA1_HMAC initialization failed!\n");
435 return ret;
436 }
437
438 /*! \fn void ifxdeu_fini_sha1_hmac (void)
439 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
440 * \brief unregister sha1 hmac driver
441 */
442 void ifxdeu_fini_sha1_hmac (void)
443 {
444
445 crypto_unregister_shash(&ifxdeu_sha1_hmac_alg);
446
447
448 }