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