ltq-deu: make deu hash lock global and remove md5_hmac_ exports
[openwrt/staging/mkresin.git] / package / kernel / lantiq / ltq-deu / src / ifxmips_md5.c
1 /******************************************************************************
2 **
3 ** FILE NAME : ifxmips_md5.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 *******************************************************************************/
23 /*!
24 \defgroup IFX_DEU IFX_DEU_DRIVERS
25 \ingroup API
26 \brief ifx deu driver module
27 */
28
29 /*!
30 \file ifxmips_md5.c
31 \ingroup IFX_DEU
32 \brief MD5 encryption deu driver file
33 */
34
35 /*!
36 \defgroup IFX_MD5_FUNCTIONS IFX_MD5_FUNCTIONS
37 \ingroup IFX_DEU
38 \brief ifx deu MD5 functions
39 */
40
41 /*Project header files */
42 #include <linux/init.h>
43 #include <linux/module.h>
44 #include <linux/string.h>
45 #include <linux/crypto.h>
46 #include <linux/types.h>
47 #include <crypto/internal/hash.h>
48 #include <asm/byteorder.h>
49
50 /* Project header */
51 #if defined(CONFIG_DANUBE)
52 #include "ifxmips_deu_danube.h"
53 #elif defined(CONFIG_AR9)
54 #include "ifxmips_deu_ar9.h"
55 #elif defined(CONFIG_VR9) || defined(CONFIG_AR10)
56 #include "ifxmips_deu_vr9.h"
57 #else
58 #error "Plaform Unknwon!"
59 #endif
60
61 #define MD5_DIGEST_SIZE 16
62 #define MD5_HMAC_BLOCK_SIZE 64
63 #define MD5_BLOCK_WORDS 16
64 #define MD5_HASH_WORDS 4
65 #define HASH_START IFX_HASH_CON
66
67 //#define CRYPTO_DEBUG
68 #ifdef CRYPTO_DEBUG
69 extern char debug_level;
70 #define DPRINTF(level, format, args...) if (level < debug_level) printk(KERN_INFO "[%s %s %d]: " format, __FILE__, __func__, __LINE__, ##args);
71 #else
72 #define DPRINTF(level, format, args...)
73 #endif
74
75 struct md5_ctx {
76 int started;
77 u32 hash[MD5_HASH_WORDS];
78 u32 block[MD5_BLOCK_WORDS];
79 u64 byte_count;
80 };
81
82 extern int disable_deudma;
83
84 /*! \fn static u32 endian_swap(u32 input)
85 * \ingroup IFX_MD5_FUNCTIONS
86 * \brief perform dword level endian swap
87 * \param input value of dword that requires to be swapped
88 */
89 static u32 endian_swap(u32 input)
90 {
91 u8 *ptr = (u8 *)&input;
92
93 return ((ptr[3] << 24) | (ptr[2] << 16) | (ptr[1] << 8) | ptr[0]);
94 }
95
96 /*! \fn static void md5_transform(u32 *hash, u32 const *in)
97 * \ingroup IFX_MD5_FUNCTIONS
98 * \brief main interface to md5 hardware
99 * \param hash current hash value
100 * \param in 64-byte block of input
101 */
102 static void md5_transform(struct md5_ctx *mctx, u32 *hash, u32 const *in)
103 {
104 int i;
105 volatile struct deu_hash_t *hashs = (struct deu_hash_t *) HASH_START;
106 unsigned long flag;
107
108 CRTCL_SECT_HASH_START;
109
110 if (mctx->started) {
111 hashs->D1R = endian_swap(*((u32 *) hash + 0));
112 hashs->D2R = endian_swap(*((u32 *) hash + 1));
113 hashs->D3R = endian_swap(*((u32 *) hash + 2));
114 hashs->D4R = endian_swap(*((u32 *) hash + 3));
115 }
116
117 for (i = 0; i < 16; i++) {
118 hashs->MR = endian_swap(in[i]);
119 // printk("in[%d]: %08x\n", i, endian_swap(in[i]));
120 };
121
122 //wait for processing
123 while (hashs->controlr.BSY) {
124 // this will not take long
125 }
126
127 *((u32 *) hash + 0) = endian_swap (hashs->D1R);
128 *((u32 *) hash + 1) = endian_swap (hashs->D2R);
129 *((u32 *) hash + 2) = endian_swap (hashs->D3R);
130 *((u32 *) hash + 3) = endian_swap (hashs->D4R);
131
132 mctx->started = 1;
133
134 CRTCL_SECT_HASH_END;
135 }
136
137 /*! \fn static inline void md5_transform_helper(struct md5_ctx *ctx)
138 * \ingroup IFX_MD5_FUNCTIONS
139 * \brief interfacing function for md5_transform()
140 * \param ctx crypto context
141 */
142 static inline void md5_transform_helper(struct md5_ctx *ctx)
143 {
144 //le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
145 md5_transform(ctx, ctx->hash, ctx->block);
146 }
147
148 /*! \fn static void md5_init(struct crypto_tfm *tfm)
149 * \ingroup IFX_MD5_FUNCTIONS
150 * \brief initialize md5 hardware
151 * \param tfm linux crypto algo transform
152 */
153 static int md5_init(struct shash_desc *desc)
154 {
155 struct md5_ctx *mctx = shash_desc_ctx(desc);
156 volatile struct deu_hash_t *hash = (struct deu_hash_t *) HASH_START;
157
158 hash->controlr.ENDI = 0;
159 hash->controlr.SM = 1;
160 hash->controlr.ALGO = 1; // 1 = md5 0 = sha1
161 hash->controlr.INIT = 1; // Initialize the hash operation by writing a '1' to the INIT bit.
162
163 mctx->byte_count = 0;
164 mctx->started = 0;
165 return 0;
166 }
167
168 /*! \fn static void md5_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
169 * \ingroup IFX_MD5_FUNCTIONS
170 * \brief on-the-fly md5 computation
171 * \param tfm linux crypto algo transform
172 * \param data input data
173 * \param len size of input data
174 */
175 static int md5_update(struct shash_desc *desc, const u8 *data, unsigned int len)
176 {
177 struct md5_ctx *mctx = shash_desc_ctx(desc);
178 const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
179
180 mctx->byte_count += len;
181
182 if (avail > len) {
183 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
184 data, len);
185 return 0;
186 }
187
188 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
189 data, avail);
190
191 md5_transform_helper(mctx);
192 data += avail;
193 len -= avail;
194
195 while (len >= sizeof(mctx->block)) {
196 memcpy(mctx->block, data, sizeof(mctx->block));
197 md5_transform_helper(mctx);
198 data += sizeof(mctx->block);
199 len -= sizeof(mctx->block);
200 }
201
202 memcpy(mctx->block, data, len);
203 return 0;
204 }
205
206 /*! \fn static void md5_final(struct crypto_tfm *tfm, u8 *out)
207 * \ingroup IFX_MD5_FUNCTIONS
208 * \brief compute final md5 value
209 * \param tfm linux crypto algo transform
210 * \param out final md5 output value
211 */
212 static int md5_final(struct shash_desc *desc, u8 *out)
213 {
214 struct md5_ctx *mctx = shash_desc_ctx(desc);
215 const unsigned int offset = mctx->byte_count & 0x3f;
216 char *p = (char *)mctx->block + offset;
217 int padding = 56 - (offset + 1);
218 volatile struct deu_hash_t *hashs = (struct deu_hash_t *) HASH_START;
219 unsigned long flag;
220
221 *p++ = 0x80;
222 if (padding < 0) {
223 memset(p, 0x00, padding + sizeof (u64));
224 md5_transform_helper(mctx);
225 p = (char *)mctx->block;
226 padding = 56;
227 }
228
229 memset(p, 0, padding);
230 mctx->block[14] = endian_swap(mctx->byte_count << 3);
231 mctx->block[15] = endian_swap(mctx->byte_count >> 29);
232
233 #if 0
234 le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
235 sizeof(u64)) / sizeof(u32));
236 #endif
237
238 md5_transform(mctx, mctx->hash, mctx->block);
239
240 CRTCL_SECT_HASH_START;
241
242 *((u32 *) out + 0) = endian_swap (hashs->D1R);
243 *((u32 *) out + 1) = endian_swap (hashs->D2R);
244 *((u32 *) out + 2) = endian_swap (hashs->D3R);
245 *((u32 *) out + 3) = endian_swap (hashs->D4R);
246
247 CRTCL_SECT_HASH_END;
248
249 // Wipe context
250 memset(mctx, 0, sizeof(*mctx));
251
252 return 0;
253 }
254
255 /*
256 * \brief MD5 function mappings
257 */
258 static struct shash_alg ifxdeu_md5_alg = {
259 .digestsize = MD5_DIGEST_SIZE,
260 .init = md5_init,
261 .update = md5_update,
262 .final = md5_final,
263 .descsize = sizeof(struct md5_ctx),
264 .base = {
265 .cra_name = "md5",
266 .cra_driver_name= "ifxdeu-md5",
267 .cra_priority = 300,
268 .cra_flags = CRYPTO_ALG_TYPE_HASH | CRYPTO_ALG_KERN_DRIVER_ONLY,
269 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
270 .cra_module = THIS_MODULE,
271 }
272 };
273
274 /*! \fn int ifxdeu_init_md5 (void)
275 * \ingroup IFX_MD5_FUNCTIONS
276 * \brief initialize md5 driver
277 */
278 int ifxdeu_init_md5 (void)
279 {
280 int ret = -ENOSYS;
281
282
283 if ((ret = crypto_register_shash(&ifxdeu_md5_alg)))
284 goto md5_err;
285
286 printk (KERN_NOTICE "IFX DEU MD5 initialized%s.\n", disable_deudma ? "" : " (DMA)");
287 return ret;
288
289 md5_err:
290 printk(KERN_ERR "IFX DEU MD5 initialization failed!\n");
291 return ret;
292 }
293
294 /*! \fn void ifxdeu_fini_md5 (void)
295 * \ingroup IFX_MD5_FUNCTIONS
296 * \brief unregister md5 driver
297 */
298
299 void ifxdeu_fini_md5 (void)
300 {
301 crypto_unregister_shash(&ifxdeu_md5_alg);
302
303 }
304