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