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