deptest: Do not clobber the base build and staging dirs
[openwrt/svn-archive/archive.git] / target / linux / ifxmips / files-2.6.33 / drivers / crypto / ifxmips / ifxmips_md5_hmac.c
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
15 *
16 * Copyright (C) 2010 Ralph Hempel <ralph.hempel@lantiq.com>
17 * Copyright (C) 2009 Mohammad Firdaus
18 */
19
20 /*!
21 \defgroup IFX_DEU IFX_DEU_DRIVERS
22 \ingroup API
23 \brief ifx deu driver module
24 */
25
26 /*!
27 \file ifxmips_md5_hmac.c
28 \ingroup IFX_DEU
29 \brief MD5-HMAC encryption deu driver file
30 */
31
32 /*!
33 \defgroup IFX_MD5_HMAC_FUNCTIONS IFX_MD5_HMAC_FUNCTIONS
34 \ingroup IFX_DEU
35 \brief ifx md5-hmac driver functions
36 */
37
38 /* Project Header files */
39 #include <linux/init.h>
40 #include <linux/module.h>
41 #include <linux/string.h>
42 #include <linux/crypto.h>
43 #include <linux/types.h>
44 #include <asm/byteorder.h>
45 #include "ifxmips_deu.h"
46
47 #define MD5_DIGEST_SIZE 16
48 #define MD5_HMAC_BLOCK_SIZE 64
49 #define MD5_BLOCK_WORDS 16
50 #define MD5_HASH_WORDS 4
51 #define MD5_HMAC_DBN_TEMP_SIZE 1024 // size in dword, needed for dbn workaround
52 #define HASH_START IFX_HASH_CON
53
54 static spinlock_t lock;
55 #define CRTCL_SECT_INIT spin_lock_init(&lock)
56 #define CRTCL_SECT_START spin_lock_irqsave(&lock, flag)
57 #define CRTCL_SECT_END spin_unlock_irqrestore(&lock, flag)
58
59 struct md5_hmac_ctx {
60 u32 hash[MD5_HASH_WORDS];
61 u32 block[MD5_BLOCK_WORDS];
62 u64 byte_count;
63 u32 dbn;
64 u32 temp[MD5_HMAC_DBN_TEMP_SIZE];
65 };
66
67 extern int disable_deudma;
68
69 /*! \fn static u32 endian_swap(u32 input)
70 * \ingroup IFX_MD5_HMAC_FUNCTIONS
71 * \brief perform dword level endian swap
72 * \param input value of dword that requires to be swapped
73 */
74 static u32 endian_swap(u32 input)
75 {
76 u8 *ptr = (u8 *)&input;
77
78 return ((ptr[3] << 24) | (ptr[2] << 16) | (ptr[1] << 8) | ptr[0]);
79 }
80
81 /*! \fn static void md5_hmac_transform(struct crypto_tfm *tfm, u32 const *in)
82 * \ingroup IFX_MD5_HMAC_FUNCTIONS
83 * \brief save input block to context
84 * \param tfm linux crypto algo transform
85 * \param in 64-byte block of input
86 */
87 static void md5_hmac_transform(struct crypto_tfm *tfm, u32 const *in)
88 {
89 struct md5_hmac_ctx *mctx = crypto_tfm_ctx(tfm);
90
91 memcpy(&mctx->temp[mctx->dbn<<4], in, 64); //dbn workaround
92 mctx->dbn += 1;
93
94 if ( (mctx->dbn<<4) > MD5_HMAC_DBN_TEMP_SIZE )
95 {
96 printk("MD5_HMAC_DBN_TEMP_SIZE exceeded\n");
97 }
98
99 }
100
101 /*! \fn int md5_hmac_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
102 * \ingroup IFX_MD5_HMAC_FUNCTIONS
103 * \brief sets md5 hmac key
104 * \param tfm linux crypto algo transform
105 * \param key input key
106 * \param keylen key length greater than 64 bytes IS NOT SUPPORTED
107 */
108 int md5_hmac_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
109 {
110 volatile struct deu_hash_t *hash = (struct deu_hash_t *) HASH_START;
111 int i, j;
112 u32 *in_key = (u32 *)key;
113
114 hash->KIDX = 0x80000000; // reset all 16 words of the key to '0'
115 asm("sync");
116
117 j = 0;
118 for (i = 0; i < keylen; i+=4)
119 {
120 hash->KIDX = j;
121 asm("sync");
122 hash->KEY = *((u32 *) in_key + j);
123 j++;
124 }
125
126 return 0;
127 }
128
129 /*! \fn void md5_hmac_init(struct crypto_tfm *tfm)
130 * \ingroup IFX_MD5_HMAC_FUNCTIONS
131 * \brief initialize md5 hmac context
132 * \param tfm linux crypto algo transform
133 */
134 void md5_hmac_init(struct crypto_tfm *tfm)
135 {
136 struct md5_hmac_ctx *mctx = crypto_tfm_ctx(tfm);
137
138 memset(mctx, 0, sizeof(struct md5_hmac_ctx));
139 mctx->dbn = 0; //dbn workaround
140 }
141
142 /*! \fn void md5_hmac_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
143 * \ingroup IFX_MD5_HMAC_FUNCTIONS
144 * \brief on-the-fly md5 hmac computation
145 * \param tfm linux crypto algo transform
146 * \param data input data
147 * \param len size of input data
148 */
149 void md5_hmac_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
150 {
151 struct md5_hmac_ctx *mctx = crypto_tfm_ctx(tfm);
152 const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
153
154 mctx->byte_count += len;
155
156 if (avail > len) {
157 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
158 data, len);
159 return;
160 }
161
162 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
163 data, avail);
164
165 md5_hmac_transform(tfm, mctx->block);
166 data += avail;
167 len -= avail;
168
169 while (len >= sizeof(mctx->block)) {
170 memcpy(mctx->block, data, sizeof(mctx->block));
171 md5_hmac_transform(tfm, mctx->block);
172 data += sizeof(mctx->block);
173 len -= sizeof(mctx->block);
174 }
175
176 memcpy(mctx->block, data, len);
177
178 }
179
180 /*! \fn void md5_hmac_final(struct crypto_tfm *tfm, u8 *out)
181 * \ingroup IFX_MD5_HMAC_FUNCTIONS
182 * \brief compute final md5 hmac value
183 * \param tfm linux crypto algo transform
184 * \param out final md5 hmac output value
185 */
186 void md5_hmac_final(struct crypto_tfm *tfm, u8 *out)
187 {
188 struct md5_hmac_ctx *mctx = crypto_tfm_ctx(tfm);
189 const unsigned int offset = mctx->byte_count & 0x3f;
190 char *p = (char *)mctx->block + offset;
191 int padding = 56 - (offset + 1);
192 volatile struct deu_hash_t *hashs = (struct deu_hash_t *) HASH_START;
193 u32 flag;
194 int i = 0;
195 int dbn;
196 u32 *in = &mctx->temp[0];
197
198 *p++ = 0x80;
199 if (padding < 0) {
200 memset(p, 0x00, padding + sizeof (u64));
201 md5_hmac_transform(tfm, mctx->block);
202 p = (char *)mctx->block;
203 padding = 56;
204 }
205
206 memset(p, 0, padding);
207 mctx->block[14] = endian_swap((mctx->byte_count + 64) << 3); // need to add 512 bit of the IPAD operation
208 mctx->block[15] = 0x00000000;
209
210 md5_hmac_transform(tfm, mctx->block);
211
212 CRTCL_SECT_START;
213
214 printk("dbn = %d\n", mctx->dbn);
215 hashs->DBN = mctx->dbn;
216
217 *IFX_HASH_CON = 0x0703002D; //khs, go, init, ndc, endi, kyue, hmen, md5
218
219 //wait for processing
220 while (hashs->controlr.BSY) {
221 // this will not take long
222 }
223
224 for (dbn = 0; dbn < mctx->dbn; dbn++)
225 {
226 for (i = 0; i < 16; i++) {
227 hashs->MR = in[i];
228 };
229
230 hashs->controlr.GO = 1;
231 asm("sync");
232
233 //wait for processing
234 while (hashs->controlr.BSY) {
235 // this will not take long
236 }
237
238 in += 16;
239 }
240
241
242 #if 1
243 //wait for digest ready
244 while (! hashs->controlr.DGRY) {
245 // this will not take long
246 }
247 #endif
248
249 *((u32 *) out + 0) = hashs->D1R;
250 *((u32 *) out + 1) = hashs->D2R;
251 *((u32 *) out + 2) = hashs->D3R;
252 *((u32 *) out + 3) = hashs->D4R;
253 *((u32 *) out + 4) = hashs->D5R;
254
255 CRTCL_SECT_END;
256 }
257
258 /*
259 * \brief MD5_HMAC function mappings
260 */
261
262 static struct crypto_alg ifxdeu_md5_hmac_alg = {
263 .cra_name = "hmac(md5)",
264 .cra_driver_name = "ifxdeu-md5_hmac",
265 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
266 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
267 .cra_ctxsize = sizeof(struct md5_hmac_ctx),
268 .cra_module = THIS_MODULE,
269 .cra_list = LIST_HEAD_INIT(ifxdeu_md5_hmac_alg.cra_list),
270 .cra_u = { .digest = {
271 .dia_digestsize = MD5_DIGEST_SIZE,
272 .dia_setkey = md5_hmac_setkey,
273 .dia_init = md5_hmac_init,
274 .dia_update = md5_hmac_update,
275 .dia_final = md5_hmac_final } }
276 };
277
278 /*! \fn int __init ifxdeu_init_md5_hmac (void)
279 * \ingroup IFX_MD5_HMAC_FUNCTIONS
280 * \brief initialize md5 hmac driver
281 */
282 int __init ifxdeu_init_md5_hmac (void)
283 {
284 int ret;
285
286 if ((ret = crypto_register_alg(&ifxdeu_md5_hmac_alg)))
287 goto md5_hmac_err;
288
289 CRTCL_SECT_INIT;
290
291 printk (KERN_NOTICE "IFX DEU MD5_HMAC initialized%s.\n", disable_deudma ? "" : " (DMA)");
292 return ret;
293
294 md5_hmac_err:
295 printk(KERN_ERR "IFX DEU MD5_HMAC initialization failed!\n");
296 return ret;
297 }
298
299 /** \fn void __exit ifxdeu_fini_md5_hmac (void)
300 * \ingroup IFX_MD5_HMAC_FUNCTIONS
301 * \brief unregister md5 hmac driver
302 */
303 void __exit ifxdeu_fini_md5_hmac (void)
304 {
305 crypto_unregister_alg (&ifxdeu_md5_hmac_alg);
306 }
307