a139f8d9d4a8562d6d4218ff01c7e89563d4682f
[openwrt/staging/mkresin.git] / package / kernel / lantiq / ltq-deu / src / ifxmips_sha1.c
1 /******************************************************************************
2 **
3 ** FILE NAME : ifxmips_sha1.c
4 ** PROJECT : IFX UEIP
5 ** MODULES : DEU Module for Danube
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_sha1.c
31 \ingroup IFX_DEU
32 \brief SHA1 encryption deu driver file
33 */
34
35 /*!
36 \defgroup IFX_SHA1_FUNCTIONS IFX_SHA1_FUNCTIONS
37 \ingroup IFX_DEU
38 \brief ifx deu sha1 functions
39 */
40
41
42 /* Project header */
43 #include <linux/init.h>
44 #include <linux/module.h>
45 #include <linux/mm.h>
46 #include <linux/crypto.h>
47 #include <crypto/sha.h>
48 #include <crypto/hash.h>
49 #include <crypto/internal/hash.h>
50 #include <linux/types.h>
51 #include <linux/scatterlist.h>
52 #include <asm/byteorder.h>
53
54 #if defined(CONFIG_DANUBE)
55 #include "ifxmips_deu_danube.h"
56 #elif defined(CONFIG_AR9)
57 #include "ifxmips_deu_ar9.h"
58 #elif defined(CONFIG_VR9) || defined(CONFIG_AR10)
59 #include "ifxmips_deu_vr9.h"
60 #else
61 #error "Plaform Unknwon!"
62 #endif
63
64 #define SHA1_DIGEST_SIZE 20
65 #define SHA1_HMAC_BLOCK_SIZE 64
66 #define HASH_START IFX_HASH_CON
67
68 //#define CRYPTO_DEBUG
69 #ifdef CRYPTO_DEBUG
70 extern char debug_level;
71 #define DPRINTF(level, format, args...) if (level < debug_level) printk(KERN_INFO "[%s %s %d]: " format, __FILE__, __func__, __LINE__, ##args);
72 #else
73 #define DPRINTF(level, format, args...)
74 #endif
75
76 /*
77 * \brief SHA1 private structure
78 */
79 struct sha1_ctx {
80 int started;
81 u64 count;
82 u32 hash[5];
83 u32 state[5];
84 u8 buffer[64];
85 };
86
87 extern int disable_deudma;
88
89
90 /*! \fn static void sha1_transform1 (u32 *state, const u32 *in)
91 * \ingroup IFX_SHA1_FUNCTIONS
92 * \brief main interface to sha1 hardware
93 * \param state current state
94 * \param in 64-byte block of input
95 */
96 static void sha1_transform1 (struct sha1_ctx *sctx, u32 *state, const u32 *in)
97 {
98 int i = 0;
99 volatile struct deu_hash_t *hashs = (struct deu_hash_t *) HASH_START;
100 unsigned long flag;
101
102 CRTCL_SECT_HASH_START;
103
104 SHA_HASH_INIT;
105
106 /* For context switching purposes, the previous hash output
107 * is loaded back into the output register
108 */
109 if (sctx->started) {
110 hashs->D1R = *((u32 *) sctx->hash + 0);
111 hashs->D2R = *((u32 *) sctx->hash + 1);
112 hashs->D3R = *((u32 *) sctx->hash + 2);
113 hashs->D4R = *((u32 *) sctx->hash + 3);
114 hashs->D5R = *((u32 *) sctx->hash + 4);
115 }
116
117 for (i = 0; i < 16; i++) {
118 hashs->MR = in[i];
119 };
120
121 //wait for processing
122 while (hashs->controlr.BSY) {
123 // this will not take long
124 }
125
126 /* For context switching purposes, the output is saved into a
127 * context struct which can be used later on
128 */
129 *((u32 *) sctx->hash + 0) = hashs->D1R;
130 *((u32 *) sctx->hash + 1) = hashs->D2R;
131 *((u32 *) sctx->hash + 2) = hashs->D3R;
132 *((u32 *) sctx->hash + 3) = hashs->D4R;
133 *((u32 *) sctx->hash + 4) = hashs->D5R;
134
135 sctx->started = 1;
136
137 CRTCL_SECT_HASH_END;
138 }
139
140 /*! \fn static void sha1_init1(struct crypto_tfm *tfm)
141 * \ingroup IFX_SHA1_FUNCTIONS
142 * \brief initialize sha1 hardware
143 * \param tfm linux crypto algo transform
144 */
145 static int sha1_init1(struct shash_desc *desc)
146 {
147 struct sha1_ctx *sctx = shash_desc_ctx(desc);
148
149 sctx->started = 0;
150 sctx->count = 0;
151 return 0;
152 }
153
154 /*! \fn static void sha1_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
155 * \ingroup IFX_SHA1_FUNCTIONS
156 * \brief on-the-fly sha1 computation
157 * \param tfm linux crypto algo transform
158 * \param data input data
159 * \param len size of input data
160 */
161 static int sha1_update(struct shash_desc * desc, const u8 *data,
162 unsigned int len)
163 {
164 struct sha1_ctx *sctx = shash_desc_ctx(desc);
165 unsigned int i, j;
166
167 j = (sctx->count >> 3) & 0x3f;
168 sctx->count += len << 3;
169
170 if ((j + len) > 63) {
171 memcpy (&sctx->buffer[j], data, (i = 64 - j));
172 sha1_transform1 (sctx, sctx->state, (const u32 *)sctx->buffer);
173 for (; i + 63 < len; i += 64) {
174 sha1_transform1 (sctx, sctx->state, (const u32 *)&data[i]);
175 }
176
177 j = 0;
178 }
179 else
180 i = 0;
181
182 memcpy (&sctx->buffer[j], &data[i], len - i);
183 return 0;
184 }
185
186 /*! \fn static void sha1_final(struct crypto_tfm *tfm, u8 *out)
187 * \ingroup IFX_SHA1_FUNCTIONS
188 * \brief compute final sha1 value
189 * \param tfm linux crypto algo transform
190 * \param out final md5 output value
191 */
192 static int sha1_final(struct shash_desc *desc, u8 *out)
193 {
194 struct sha1_ctx *sctx = shash_desc_ctx(desc);
195 u32 index, padlen;
196 u64 t;
197 u8 bits[8] = { 0, };
198 static const u8 padding[64] = { 0x80, };
199 volatile struct deu_hash_t *hashs = (struct deu_hash_t *) HASH_START;
200 unsigned long flag;
201
202 t = sctx->count;
203 bits[7] = 0xff & t;
204 t >>= 8;
205 bits[6] = 0xff & t;
206 t >>= 8;
207 bits[5] = 0xff & t;
208 t >>= 8;
209 bits[4] = 0xff & t;
210 t >>= 8;
211 bits[3] = 0xff & t;
212 t >>= 8;
213 bits[2] = 0xff & t;
214 t >>= 8;
215 bits[1] = 0xff & t;
216 t >>= 8;
217 bits[0] = 0xff & t;
218
219 /* Pad out to 56 mod 64 */
220 index = (sctx->count >> 3) & 0x3f;
221 padlen = (index < 56) ? (56 - index) : ((64 + 56) - index);
222 sha1_update (desc, padding, padlen);
223
224 /* Append length */
225 sha1_update (desc, bits, sizeof bits);
226
227 memcpy(out, sctx->hash, SHA1_DIGEST_SIZE);
228
229 // Wipe context
230 memset (sctx, 0, sizeof *sctx);
231
232 return 0;
233 }
234
235 /*
236 * \brief SHA1 function mappings
237 */
238 static struct shash_alg ifxdeu_sha1_alg = {
239 .digestsize = SHA1_DIGEST_SIZE,
240 .init = sha1_init1,
241 .update = sha1_update,
242 .final = sha1_final,
243 .descsize = sizeof(struct sha1_ctx),
244 .statesize = sizeof(struct sha1_state),
245 .base = {
246 .cra_name = "sha1",
247 .cra_driver_name= "ifxdeu-sha1",
248 .cra_priority = 300,
249 .cra_flags = CRYPTO_ALG_TYPE_HASH | CRYPTO_ALG_KERN_DRIVER_ONLY,
250 .cra_blocksize = SHA1_HMAC_BLOCK_SIZE,
251 .cra_module = THIS_MODULE,
252 }
253 };
254
255
256 /*! \fn int ifxdeu_init_sha1 (void)
257 * \ingroup IFX_SHA1_FUNCTIONS
258 * \brief initialize sha1 driver
259 */
260 int ifxdeu_init_sha1 (void)
261 {
262 int ret = -ENOSYS;
263
264
265 if ((ret = crypto_register_shash(&ifxdeu_sha1_alg)))
266 goto sha1_err;
267
268 printk (KERN_NOTICE "IFX DEU SHA1 initialized%s.\n", disable_deudma ? "" : " (DMA)");
269 return ret;
270
271 sha1_err:
272 printk(KERN_ERR "IFX DEU SHA1 initialization failed!\n");
273 return ret;
274 }
275
276 /*! \fn void ifxdeu_fini_sha1 (void)
277 * \ingroup IFX_SHA1_FUNCTIONS
278 * \brief unregister sha1 driver
279 */
280 void ifxdeu_fini_sha1 (void)
281 {
282 crypto_unregister_shash(&ifxdeu_sha1_alg);
283
284
285 }
286
287