packages: clean up the package folder
[openwrt/staging/yousong.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 static spinlock_t lock;
67 #define CRTCL_SECT_INIT spin_lock_init(&lock)
68 #define CRTCL_SECT_START spin_lock_irqsave(&lock, flag)
69 #define CRTCL_SECT_END spin_unlock_irqrestore(&lock, flag)
70
71 //#define CRYPTO_DEBUG
72 #ifdef CRYPTO_DEBUG
73 extern char debug_level;
74 #define DPRINTF(level, format, args...) if (level < debug_level) printk(KERN_INFO "[%s %s %d]: " format, __FILE__, __func__, __LINE__, ##args);
75 #else
76 #define DPRINTF(level, format, args...)
77 #endif
78
79 #define MAX_HASH_KEYLEN 64
80
81 struct md5_hmac_ctx {
82 u8 key[MAX_HASH_KEYLEN];
83 u32 hash[MD5_HASH_WORDS];
84 u32 block[MD5_BLOCK_WORDS];
85 u64 byte_count;
86 u32 dbn;
87 unsigned int keylen;
88 };
89
90 static u32 temp[MD5_HMAC_DBN_TEMP_SIZE];
91
92 extern int disable_deudma;
93
94 /*! \fn static u32 endian_swap(u32 input)
95 * \ingroup IFX_MD5_HMAC_FUNCTIONS
96 * \brief perform dword level endian swap
97 * \param input value of dword that requires to be swapped
98 */
99 static u32 endian_swap(u32 input)
100 {
101 u8 *ptr = (u8 *)&input;
102
103 return ((ptr[3] << 24) | (ptr[2] << 16) | (ptr[1] << 8) | ptr[0]);
104 }
105
106 /*! \fn static void md5_hmac_transform(struct crypto_tfm *tfm, u32 const *in)
107 * \ingroup IFX_MD5_HMAC_FUNCTIONS
108 * \brief save input block to context
109 * \param tfm linux crypto algo transform
110 * \param in 64-byte block of input
111 */
112 static void md5_hmac_transform(struct shash_desc *desc, u32 const *in)
113 {
114 struct md5_hmac_ctx *mctx = crypto_shash_ctx(desc->tfm);
115
116 memcpy(&temp[mctx->dbn<<4], in, 64); //dbn workaround
117 mctx->dbn += 1;
118
119 if ( (mctx->dbn<<4) > MD5_HMAC_DBN_TEMP_SIZE )
120 {
121 printk("MD5_HMAC_DBN_TEMP_SIZE exceeded\n");
122 }
123
124 }
125
126 /*! \fn int md5_hmac_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
127 * \ingroup IFX_MD5_HMAC_FUNCTIONS
128 * \brief sets md5 hmac key
129 * \param tfm linux crypto algo transform
130 * \param key input key
131 * \param keylen key length greater than 64 bytes IS NOT SUPPORTED
132 */
133 static int md5_hmac_setkey(struct crypto_shash *tfm, const u8 *key, unsigned int keylen)
134 {
135 struct md5_hmac_ctx *mctx = crypto_shash_ctx(tfm);
136 volatile struct deu_hash_t *hash = (struct deu_hash_t *) HASH_START;
137 //printk("copying keys to context with length %d\n", keylen);
138
139 if (keylen > MAX_HASH_KEYLEN) {
140 printk("Key length more than what DEU hash can handle\n");
141 return -EINVAL;
142 }
143
144
145 hash->KIDX |= 0x80000000; // reset all 16 words of the key to '0'
146 memcpy(&mctx->key, key, keylen);
147 mctx->keylen = keylen;
148
149 return 0;
150
151 }
152
153
154 /*! \fn int md5_hmac_setkey_hw(const u8 *key, unsigned int keylen)
155 * \ingroup IFX_MD5_HMAC_FUNCTIONS
156 * \brief sets md5 hmac key into the hardware registers
157 * \param key input key
158 * \param keylen key length greater than 64 bytes IS NOT SUPPORTED
159 */
160
161 static int md5_hmac_setkey_hw(const u8 *key, unsigned int keylen)
162 {
163 volatile struct deu_hash_t *hash = (struct deu_hash_t *) HASH_START;
164 unsigned long flag;
165 int i, j;
166 u32 *in_key = (u32 *)key;
167
168 //printk("\nsetkey keylen: %d\n key: ", keylen);
169
170 CRTCL_SECT_START;
171 j = 0;
172 for (i = 0; i < keylen; i+=4)
173 {
174 hash->KIDX = j;
175 asm("sync");
176 hash->KEY = *((u32 *) in_key + j);
177 asm("sync");
178 j++;
179 }
180 CRTCL_SECT_END;
181
182 return 0;
183 }
184
185 /*! \fn void md5_hmac_init(struct crypto_tfm *tfm)
186 * \ingroup IFX_MD5_HMAC_FUNCTIONS
187 * \brief initialize md5 hmac context
188 * \param tfm linux crypto algo transform
189 */
190 static int md5_hmac_init(struct shash_desc *desc)
191 {
192
193 struct md5_hmac_ctx *mctx = crypto_shash_ctx(desc->tfm);
194
195
196 mctx->dbn = 0; //dbn workaround
197 md5_hmac_setkey_hw(mctx->key, mctx->keylen);
198
199 return 0;
200 }
201 EXPORT_SYMBOL(md5_hmac_init);
202
203 /*! \fn void md5_hmac_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
204 * \ingroup IFX_MD5_HMAC_FUNCTIONS
205 * \brief on-the-fly md5 hmac computation
206 * \param tfm linux crypto algo transform
207 * \param data input data
208 * \param len size of input data
209 */
210 static int md5_hmac_update(struct shash_desc *desc, const u8 *data, unsigned int len)
211 {
212 struct md5_hmac_ctx *mctx = crypto_shash_ctx(desc->tfm);
213 const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
214
215 mctx->byte_count += len;
216
217 if (avail > len) {
218 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
219 data, len);
220 return 0;
221 }
222
223 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
224 data, avail);
225
226 md5_hmac_transform(desc, mctx->block);
227 data += avail;
228 len -= avail;
229
230 while (len >= sizeof(mctx->block)) {
231 memcpy(mctx->block, data, sizeof(mctx->block));
232 md5_hmac_transform(desc, mctx->block);
233 data += sizeof(mctx->block);
234 len -= sizeof(mctx->block);
235 }
236
237 memcpy(mctx->block, data, len);
238 return 0;
239 }
240 EXPORT_SYMBOL(md5_hmac_update);
241
242 /*! \fn void md5_hmac_final(struct crypto_tfm *tfm, u8 *out)
243 * \ingroup IFX_MD5_HMAC_FUNCTIONS
244 * \brief compute final md5 hmac value
245 * \param tfm linux crypto algo transform
246 * \param out final md5 hmac output value
247 */
248 static int md5_hmac_final(struct shash_desc *desc, u8 *out)
249 {
250 struct md5_hmac_ctx *mctx = crypto_shash_ctx(desc->tfm);
251 const unsigned int offset = mctx->byte_count & 0x3f;
252 char *p = (char *)mctx->block + offset;
253 int padding = 56 - (offset + 1);
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 = &temp[0];
259
260
261 *p++ = 0x80;
262 if (padding < 0) {
263 memset(p, 0x00, padding + sizeof (u64));
264 md5_hmac_transform(desc, mctx->block);
265 p = (char *)mctx->block;
266 padding = 56;
267 }
268
269 memset(p, 0, padding);
270 mctx->block[14] = endian_swap((mctx->byte_count + 64) << 3); // need to add 512 bit of the IPAD operation
271 mctx->block[15] = 0x00000000;
272
273 md5_hmac_transform(desc, mctx->block);
274
275 CRTCL_SECT_START;
276
277 //printk("\ndbn = %d\n", mctx->dbn);
278 hashs->DBN = mctx->dbn;
279 asm("sync");
280
281 *IFX_HASH_CON = 0x0703002D; //khs, go, init, ndc, endi, kyue, hmen, md5
282
283 //wait for processing
284 while (hashs->controlr.BSY) {
285 // this will not take long
286 }
287
288 for (dbn = 0; dbn < mctx->dbn; dbn++)
289 {
290 for (i = 0; i < 16; i++) {
291 hashs->MR = in[i];
292 };
293
294 hashs->controlr.GO = 1;
295 asm("sync");
296
297 //wait for processing
298 while (hashs->controlr.BSY) {
299 // this will not take long
300 }
301
302 in += 16;
303 }
304
305
306 #if 1
307 //wait for digest ready
308 while (! hashs->controlr.DGRY) {
309 // this will not take long
310 }
311 #endif
312
313 *((u32 *) out + 0) = hashs->D1R;
314 *((u32 *) out + 1) = hashs->D2R;
315 *((u32 *) out + 2) = hashs->D3R;
316 *((u32 *) out + 3) = hashs->D4R;
317 *((u32 *) out + 4) = hashs->D5R;
318
319 /* reset the context after we finish with the hash */
320 mctx->byte_count = 0;
321 memset(&mctx->hash[0], 0, sizeof(MD5_HASH_WORDS));
322 memset(&mctx->block[0], 0, sizeof(MD5_BLOCK_WORDS));
323 memset(&temp[0], 0, MD5_HMAC_DBN_TEMP_SIZE);
324
325 CRTCL_SECT_END;
326
327
328 return 0;
329 }
330
331 EXPORT_SYMBOL(md5_hmac_final);
332
333 /*
334 * \brief MD5_HMAC function mappings
335 */
336
337 static struct shash_alg ifxdeu_md5_hmac_alg = {
338 .digestsize = MD5_DIGEST_SIZE,
339 .init = md5_hmac_init,
340 .update = md5_hmac_update,
341 .final = md5_hmac_final,
342 .setkey = md5_hmac_setkey,
343 .descsize = sizeof(struct md5_hmac_ctx),
344 .base = {
345 .cra_name = "hmac(md5)",
346 .cra_driver_name= "ifxdeu-md5_hmac",
347 .cra_ctxsize = sizeof(struct md5_hmac_ctx),
348 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
349 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
350 .cra_module = THIS_MODULE,
351 }
352 };
353
354 /*! \fn int __init ifxdeu_init_md5_hmac (void)
355 * \ingroup IFX_MD5_HMAC_FUNCTIONS
356 * \brief initialize md5 hmac driver
357 */
358 int __init ifxdeu_init_md5_hmac (void)
359 {
360
361 int ret = -ENOSYS;
362
363
364 if ((ret = crypto_register_shash(&ifxdeu_md5_hmac_alg)))
365 goto md5_hmac_err;
366
367 CRTCL_SECT_INIT;
368
369 printk (KERN_NOTICE "IFX DEU MD5_HMAC initialized%s.\n", disable_deudma ? "" : " (DMA)");
370 return ret;
371
372 md5_hmac_err:
373 printk(KERN_ERR "IFX DEU MD5_HMAC initialization failed!\n");
374 return ret;
375 }
376
377 /** \fn void __exit ifxdeu_fini_md5_hmac (void)
378 * \ingroup IFX_MD5_HMAC_FUNCTIONS
379 * \brief unregister md5 hmac driver
380 */
381 void __exit ifxdeu_fini_md5_hmac (void)
382 {
383 crypto_unregister_shash(&ifxdeu_md5_hmac_alg);
384 }
385
386