ltq-deu: update initialisations for hmac algorithms
[openwrt/staging/stintel.git] / package / kernel / lantiq / ltq-deu / src / ifxmips_md5_hmac.c
1 /******************************************************************************
2 **
3 ** FILE NAME : ifxmips_md5_hmac.c
4 ** PROJECT : IFX UEIP
5 ** MODULES : DEU Module for UEIP
6 **
7 ** DATE : September 8, 2009
8 ** AUTHOR : Mohammad Firdaus
9 ** DESCRIPTION : Data Encryption Unit Driver
10 ** COPYRIGHT : Copyright (c) 2009
11 ** Infineon Technologies AG
12 ** Am Campeon 1-12, 85579 Neubiberg, Germany
13 **
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License as published by
16 ** the Free Software Foundation; either version 2 of the License, or
17 ** (at your option) any later version.
18 **
19 ** HISTORY
20 ** $Date $Author $Comment
21 ** 08,Sept 2009 Mohammad Firdaus Initial UEIP release
22 ** 21,March 2011 Mohammad Firdaus Changes for Kernel 2.6.32 and IPSec integration
23 *******************************************************************************/
24 /*!
25 \defgroup IFX_DEU IFX_DEU_DRIVERS
26 \ingroup API
27 \brief ifx deu driver module
28 */
29
30 /*!
31 \file ifxmips_md5_hmac.c
32 \ingroup IFX_DEU
33 \brief MD5-HMAC encryption deu driver file
34 */
35
36 /*!
37 \defgroup IFX_MD5_HMAC_FUNCTIONS IFX_MD5_HMAC_FUNCTIONS
38 \ingroup IFX_DEU
39 \brief ifx md5-hmac driver functions
40 */
41
42 /* Project Header files */
43 #include <linux/init.h>
44 #include <linux/module.h>
45 #include <linux/string.h>
46 #include <linux/crypto.h>
47 #include <linux/types.h>
48 #include <crypto/internal/hash.h>
49 #include <asm/byteorder.h>
50
51 #if defined(CONFIG_AR9)
52 #include "ifxmips_deu_ar9.h"
53 #elif defined(CONFIG_VR9) || defined(CONFIG_AR10)
54 #include "ifxmips_deu_vr9.h"
55 #else
56 #error "Plaform Unknwon!"
57 #endif
58
59 #define MD5_DIGEST_SIZE 16
60 #define MD5_HMAC_BLOCK_SIZE 64
61 #define MD5_BLOCK_WORDS 16
62 #define MD5_HASH_WORDS 4
63 #define MD5_HMAC_DBN_TEMP_SIZE 1024 // size in dword, needed for dbn workaround
64 #define HASH_START IFX_HASH_CON
65
66 //#define CRYPTO_DEBUG
67 #ifdef CRYPTO_DEBUG
68 extern char debug_level;
69 #define DPRINTF(level, format, args...) if (level < debug_level) printk(KERN_INFO "[%s %s %d]: " format, __FILE__, __func__, __LINE__, ##args);
70 #else
71 #define DPRINTF(level, format, args...)
72 #endif
73
74 #define MAX_HASH_KEYLEN 64
75
76 struct md5_hmac_ctx {
77 u8 key[MAX_HASH_KEYLEN];
78 u32 hash[MD5_HASH_WORDS];
79 u32 block[MD5_BLOCK_WORDS];
80 u64 byte_count;
81 u32 dbn;
82 int started;
83 unsigned int keylen;
84 struct shash_desc *desc;
85 u32 (*temp)[MD5_BLOCK_WORDS];
86 };
87
88 extern int disable_deudma;
89
90 static int md5_hmac_final_impl(struct shash_desc *desc, u8 *out, bool hash_final);
91
92 /*! \fn static void md5_hmac_transform(struct crypto_tfm *tfm, u32 const *in)
93 * \ingroup IFX_MD5_HMAC_FUNCTIONS
94 * \brief save input block to context
95 * \param tfm linux crypto algo transform
96 * \param in 64-byte block of input
97 */
98 static void md5_hmac_transform(struct shash_desc *desc, u32 const *in)
99 {
100 struct md5_hmac_ctx *mctx = crypto_shash_ctx(desc->tfm);
101
102 if ( ((mctx->dbn<<4)+1) > MD5_HMAC_DBN_TEMP_SIZE )
103 {
104 //printk("MD5_HMAC_DBN_TEMP_SIZE exceeded\n");
105 md5_hmac_final_impl(desc, (u8 *)mctx->hash, false);
106 }
107
108 memcpy(&mctx->temp[mctx->dbn], in, 64); //dbn workaround
109 mctx->dbn += 1;
110 }
111
112 /*! \fn int md5_hmac_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
113 * \ingroup IFX_MD5_HMAC_FUNCTIONS
114 * \brief sets md5 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 md5_hmac_setkey(struct crypto_shash *tfm, const u8 *key, unsigned int keylen)
120 {
121 struct md5_hmac_ctx *mctx = crypto_shash_ctx(tfm);
122 int err;
123 //printk("copying keys to context with length %d\n", keylen);
124
125 if (keylen > MAX_HASH_KEYLEN) {
126 char *hash_alg_name = "md5";
127
128 mctx->desc->tfm = crypto_alloc_shash(hash_alg_name, 0, 0);
129 if (IS_ERR(mctx->desc->tfm)) return PTR_ERR(mctx->desc->tfm);
130
131 memset(mctx->key, 0, MAX_HASH_KEYLEN);
132 err = crypto_shash_digest(mctx->desc, key, keylen, mctx->key);
133 if (err) return err;
134
135 mctx->keylen = MD5_DIGEST_SIZE;
136
137 crypto_free_shash(mctx->desc->tfm);
138 } else {
139 memcpy(mctx->key, key, keylen);
140 mctx->keylen = keylen;
141 }
142 memset(mctx->key + mctx->keylen, 0, MAX_HASH_KEYLEN - mctx->keylen);
143
144 return 0;
145 }
146
147 /*! \fn int md5_hmac_setkey_hw(const u8 *key, unsigned int keylen)
148 * \ingroup IFX_MD5_HMAC_FUNCTIONS
149 * \brief sets md5 hmac key into the hardware registers
150 * \param key input key
151 * \param keylen key length greater than 64 bytes IS NOT SUPPORTED
152 */
153 static int md5_hmac_setkey_hw(const u8 *key, unsigned int keylen)
154 {
155 volatile struct deu_hash_t *hash = (struct deu_hash_t *) HASH_START;
156 int i, j;
157 u32 *in_key = (u32 *)key;
158
159 //printk("\nsetkey keylen: %d\n key: ", keylen);
160
161 hash->KIDX |= 0x80000000; // reset all 16 words of the key to '0'
162 j = 0;
163 for (i = 0; i < keylen; i+=4)
164 {
165 hash->KIDX = j;
166 asm("sync");
167 hash->KEY = *((u32 *) in_key + j);
168 asm("sync");
169 j++;
170 }
171
172 return 0;
173 }
174
175 /*! \fn void md5_hmac_init(struct crypto_tfm *tfm)
176 * \ingroup IFX_MD5_HMAC_FUNCTIONS
177 * \brief initialize md5 hmac context
178 * \param tfm linux crypto algo transform
179 */
180 static int md5_hmac_init(struct shash_desc *desc)
181 {
182
183 struct md5_hmac_ctx *mctx = crypto_shash_ctx(desc->tfm);
184
185
186 mctx->dbn = 0; //dbn workaround
187 mctx->started = 0;
188 mctx->byte_count = 0;
189
190 return 0;
191 }
192
193 /*! \fn void md5_hmac_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
194 * \ingroup IFX_MD5_HMAC_FUNCTIONS
195 * \brief on-the-fly md5 hmac computation
196 * \param tfm linux crypto algo transform
197 * \param data input data
198 * \param len size of input data
199 */
200 static int md5_hmac_update(struct shash_desc *desc, const u8 *data, unsigned int len)
201 {
202 struct md5_hmac_ctx *mctx = crypto_shash_ctx(desc->tfm);
203 const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
204
205 mctx->byte_count += len;
206
207 if (avail > len) {
208 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
209 data, len);
210 return 0;
211 }
212
213 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
214 data, avail);
215
216 md5_hmac_transform(desc, mctx->block);
217 data += avail;
218 len -= avail;
219
220 while (len >= sizeof(mctx->block)) {
221 memcpy(mctx->block, data, sizeof(mctx->block));
222 md5_hmac_transform(desc, mctx->block);
223 data += sizeof(mctx->block);
224 len -= sizeof(mctx->block);
225 }
226
227 memcpy(mctx->block, data, len);
228 return 0;
229 }
230
231 /*! \fn static int md5_hmac_final(struct crypto_tfm *tfm, u8 *out)
232 * \ingroup IFX_MD5_HMAC_FUNCTIONS
233 * \brief call md5_hmac_final_impl with hash_final true
234 * \param tfm linux crypto algo transform
235 * \param out final md5 hmac output value
236 */
237 static int md5_hmac_final(struct shash_desc *desc, u8 *out)
238 {
239 return md5_hmac_final_impl(desc, out, true);
240 }
241
242 /*! \fn static int md5_hmac_final_impl(struct crypto_tfm *tfm, u8 *out, bool hash_final)
243 * \ingroup IFX_MD5_HMAC_FUNCTIONS
244 * \brief compute final or intermediate md5 hmac value
245 * \param tfm linux crypto algo transform
246 * \param out final md5 hmac output value
247 * \param in finalize or intermediate processing
248 */
249 static int md5_hmac_final_impl(struct shash_desc *desc, u8 *out, bool hash_final)
250 {
251 struct md5_hmac_ctx *mctx = crypto_shash_ctx(desc->tfm);
252 const unsigned int offset = mctx->byte_count & 0x3f;
253 char *p = (char *)mctx->block + offset;
254 int padding = 56 - (offset + 1);
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 = mctx->temp[0];
260
261 if (hash_final) {
262 *p++ = 0x80;
263 if (padding < 0) {
264 memset(p, 0x00, padding + sizeof (u64));
265 md5_hmac_transform(desc, mctx->block);
266 p = (char *)mctx->block;
267 padding = 56;
268 }
269
270 memset(p, 0, padding);
271 mctx->block[14] = le32_to_cpu((mctx->byte_count + 64) << 3); // need to add 512 bit of the IPAD operation
272 mctx->block[15] = 0x00000000;
273
274 md5_hmac_transform(desc, mctx->block);
275 }
276
277 CRTCL_SECT_HASH_START;
278
279 MD5_HASH_INIT;
280
281 md5_hmac_setkey_hw(mctx->key, mctx->keylen);
282
283 //printk("\ndbn = %d\n", mctx->dbn);
284 if (hash_final) {
285 hashs->DBN = mctx->dbn;
286 } else {
287 hashs->DBN = mctx->dbn + 5;
288 }
289 asm("sync");
290
291 *IFX_HASH_CON = 0x0703002D; //khs, go, init, ndc, endi, kyue, hmen, md5
292
293 //wait for processing
294 while (hashs->controlr.BSY) {
295 // this will not take long
296 }
297
298 if (mctx->started) {
299 hashs->D1R = *((u32 *) mctx->hash + 0);
300 hashs->D2R = *((u32 *) mctx->hash + 1);
301 hashs->D3R = *((u32 *) mctx->hash + 2);
302 hashs->D4R = *((u32 *) mctx->hash + 3);
303 } else {
304 mctx->started = 1;
305 }
306
307 for (dbn = 0; dbn < mctx->dbn; dbn++)
308 {
309 for (i = 0; i < 16; i++) {
310 hashs->MR = in[i];
311 };
312
313 hashs->controlr.GO = 1;
314 asm("sync");
315
316 //wait for processing
317 while (hashs->controlr.BSY) {
318 // this will not take long
319 }
320
321 in += 16;
322 }
323
324 #if 1
325 if (hash_final) {
326 //wait for digest ready
327 while (! hashs->controlr.DGRY) {
328 // this will not take long
329 }
330 }
331 #endif
332
333 *((u32 *) out + 0) = hashs->D1R;
334 *((u32 *) out + 1) = hashs->D2R;
335 *((u32 *) out + 2) = hashs->D3R;
336 *((u32 *) out + 3) = hashs->D4R;
337
338 CRTCL_SECT_HASH_END;
339
340 if (hash_final) {
341 /* reset the context after we finish with the hash */
342 md5_hmac_init(desc);
343 } else {
344 mctx->dbn = 0;
345 }
346 return 0;
347 }
348
349 /*! \fn void md5_hmac_init_tfm(struct crypto_tfm *tfm)
350 * \ingroup IFX_MD5_HMAC_FUNCTIONS
351 * \brief initialize pointers in md5_hmac_ctx
352 * \param tfm linux crypto algo transform
353 */
354 static int md5_hmac_init_tfm(struct crypto_tfm *tfm)
355 {
356 struct md5_hmac_ctx *mctx = crypto_tfm_ctx(tfm);
357 mctx->temp = kzalloc(4 * MD5_HMAC_DBN_TEMP_SIZE, GFP_KERNEL);
358 if (IS_ERR(mctx->temp)) return PTR_ERR(mctx->temp);
359 mctx->desc = kzalloc(sizeof(struct shash_desc), GFP_KERNEL);
360 if (IS_ERR(mctx->desc)) return PTR_ERR(mctx->desc);
361
362 return 0;
363 }
364
365 /*! \fn void md5_hmac_exit_tfm(struct crypto_tfm *tfm)
366 * \ingroup IFX_MD5_HMAC_FUNCTIONS
367 * \brief free pointers in md5_hmac_ctx
368 * \param tfm linux crypto algo transform
369 */
370 static void md5_hmac_exit_tfm(struct crypto_tfm *tfm)
371 {
372 struct md5_hmac_ctx *mctx = crypto_tfm_ctx(tfm);
373 kfree(mctx->temp);
374 kfree(mctx->desc);
375 }
376
377 /*
378 * \brief MD5_HMAC function mappings
379 */
380 static struct shash_alg ifxdeu_md5_hmac_alg = {
381 .digestsize = MD5_DIGEST_SIZE,
382 .init = md5_hmac_init,
383 .update = md5_hmac_update,
384 .final = md5_hmac_final,
385 .setkey = md5_hmac_setkey,
386 .descsize = sizeof(struct md5_hmac_ctx),
387 .base = {
388 .cra_name = "hmac(md5)",
389 .cra_driver_name= "ifxdeu-md5_hmac",
390 .cra_priority = 400,
391 .cra_ctxsize = sizeof(struct md5_hmac_ctx),
392 .cra_flags = CRYPTO_ALG_TYPE_HASH | CRYPTO_ALG_KERN_DRIVER_ONLY,
393 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
394 .cra_module = THIS_MODULE,
395 .cra_init = md5_hmac_init_tfm,
396 .cra_exit = md5_hmac_exit_tfm,
397 }
398 };
399
400 /*! \fn int ifxdeu_init_md5_hmac (void)
401 * \ingroup IFX_MD5_HMAC_FUNCTIONS
402 * \brief initialize md5 hmac driver
403 */
404 int ifxdeu_init_md5_hmac (void)
405 {
406
407 int ret = -ENOSYS;
408
409
410 if ((ret = crypto_register_shash(&ifxdeu_md5_hmac_alg)))
411 goto md5_hmac_err;
412
413 printk (KERN_NOTICE "IFX DEU MD5_HMAC initialized%s.\n", disable_deudma ? "" : " (DMA)");
414 return ret;
415
416 md5_hmac_err:
417 printk(KERN_ERR "IFX DEU MD5_HMAC initialization failed!\n");
418 return ret;
419 }
420
421 /** \fn void ifxdeu_fini_md5_hmac (void)
422 * \ingroup IFX_MD5_HMAC_FUNCTIONS
423 * \brief unregister md5 hmac driver
424 */
425 void ifxdeu_fini_md5_hmac (void)
426 {
427 crypto_unregister_shash(&ifxdeu_md5_hmac_alg);
428 }