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