9adf4d47101752343b9111c762fc7348b1579d72
[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 /* 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
193 return 0;
194 }
195
196 /*! \fn static void sha1_hmac_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
197 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
198 * \brief on-the-fly sha1 hmac computation
199 * \param tfm linux crypto algo transform
200 * \param data input data
201 * \param len size of input data
202 */
203 static int sha1_hmac_update(struct shash_desc *desc, const u8 *data,
204 unsigned int len)
205 {
206 struct sha1_hmac_ctx *sctx = crypto_shash_ctx(desc->tfm);
207 unsigned int i, j;
208
209 j = (sctx->count >> 3) & 0x3f;
210 sctx->count += len << 3;
211 // printk("sctx->count = %d\n", sctx->count);
212
213 if ((j + len) > 63) {
214 memcpy (&sctx->buffer[j], data, (i = 64 - j));
215 sha1_hmac_transform (desc, (const u32 *)sctx->buffer);
216 for (; i + 63 < len; i += 64) {
217 sha1_hmac_transform (desc, (const u32 *)&data[i]);
218 }
219
220 j = 0;
221 }
222 else
223 i = 0;
224
225 memcpy (&sctx->buffer[j], &data[i], len - i);
226 return 0;
227 }
228
229 /*! \fn static int sha1_hmac_final(struct crypto_tfm *tfm, u8 *out)
230 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
231 * \brief call sha1_hmac_final_impl with hash_final true
232 * \param tfm linux crypto algo transform
233 * \param out final sha1 hmac output value
234 */
235 static int sha1_hmac_final(struct shash_desc *desc, u8 *out)
236 {
237 return sha1_hmac_final_impl(desc, out, true);
238 }
239
240 /*! \fn static int sha1_hmac_final_impl(struct crypto_tfm *tfm, u8 *out, bool hash_final)
241 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
242 * \brief ompute final or intermediate sha1 hmac value
243 * \param tfm linux crypto algo transform
244 * \param out final sha1 hmac output value
245 * \param in finalize or intermediate processing
246 */
247 static int sha1_hmac_final_impl(struct shash_desc *desc, u8 *out, bool hash_final)
248 {
249 struct sha1_hmac_ctx *sctx = crypto_shash_ctx(desc->tfm);
250 u32 index, padlen;
251 u64 t;
252 u8 bits[8] = { 0, };
253 static const u8 padding[64] = { 0x80, };
254 volatile struct deu_hash_t *hashs = (struct deu_hash_t *) HASH_START;
255 unsigned long flag;
256 int i = 0;
257 int dbn;
258 u32 *in = sctx->temp[0];
259
260 if (hash_final) {
261 t = sctx->count + 512; // need to add 512 bit of the IPAD operation
262 bits[7] = 0xff & t;
263 t >>= 8;
264 bits[6] = 0xff & t;
265 t >>= 8;
266 bits[5] = 0xff & t;
267 t >>= 8;
268 bits[4] = 0xff & t;
269 t >>= 8;
270 bits[3] = 0xff & t;
271 t >>= 8;
272 bits[2] = 0xff & t;
273 t >>= 8;
274 bits[1] = 0xff & t;
275 t >>= 8;
276 bits[0] = 0xff & t;
277
278 /* Pad out to 56 mod 64 */
279 index = (sctx->count >> 3) & 0x3f;
280 padlen = (index < 56) ? (56 - index) : ((64 + 56) - index);
281 sha1_hmac_update (desc, padding, padlen);
282
283 /* Append length */
284 sha1_hmac_update (desc, bits, sizeof bits);
285 }
286
287 CRTCL_SECT_HASH_START;
288
289 SHA_HASH_INIT;
290
291 sha1_hmac_setkey_hw(sctx->key, sctx->keylen);
292
293 if (hash_final) {
294 hashs->DBN = sctx->dbn;
295 } else {
296 hashs->DBN = sctx->dbn + 5;
297 }
298 asm("sync");
299
300 //for vr9 change, ENDI = 1
301 *IFX_HASH_CON = HASH_CON_VALUE;
302
303 //wait for processing
304 while (hashs->controlr.BSY) {
305 // this will not take long
306 }
307
308 if (sctx->started) {
309 hashs->D1R = *((u32 *) sctx->hash + 0);
310 hashs->D2R = *((u32 *) sctx->hash + 1);
311 hashs->D3R = *((u32 *) sctx->hash + 2);
312 hashs->D4R = *((u32 *) sctx->hash + 3);
313 hashs->D5R = *((u32 *) sctx->hash + 4);
314 } else {
315 sctx->started = 1;
316 }
317
318 for (dbn = 0; dbn < sctx->dbn; dbn++)
319 {
320 for (i = 0; i < 16; i++) {
321 hashs->MR = in[i];
322 };
323
324 hashs->controlr.GO = 1;
325 asm("sync");
326
327 //wait for processing
328 while (hashs->controlr.BSY) {
329 // this will not take long
330 }
331
332 in += 16;
333 }
334
335
336 #if 1
337 if (hash_final) {
338 //wait for digest ready
339 while (! hashs->controlr.DGRY) {
340 // this will not take long
341 }
342 }
343 #endif
344
345 *((u32 *) out + 0) = hashs->D1R;
346 *((u32 *) out + 1) = hashs->D2R;
347 *((u32 *) out + 2) = hashs->D3R;
348 *((u32 *) out + 3) = hashs->D4R;
349 *((u32 *) out + 4) = hashs->D5R;
350
351 CRTCL_SECT_HASH_END;
352
353 if (hash_final) {
354 memset(&sctx->buffer[0], 0, SHA1_HMAC_BLOCK_SIZE);
355 sctx->count = 0;
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 }