1 /******************************************************************************
3 ** FILE NAME : ifxmips_sha1_hmac.c
5 ** MODULES : DEU Module for UEIP
6 ** DATE : September 8, 2009
7 ** AUTHOR : Mohammad Firdaus
8 ** DESCRIPTION : Data Encryption Unit Driver
9 ** COPYRIGHT : Copyright (c) 2009
10 ** Infineon Technologies AG
11 ** Am Campeon 1-12, 85579 Neubiberg, Germany
13 ** This program is free software; you can redistribute it and/or modify
14 ** it under the terms of the GNU General Public License as published by
15 ** the Free Software Foundation; either version 2 of the License, or
16 ** (at your option) any later version.
19 ** $Date $Author $Comment
20 ** 08,Sept 2009 Mohammad Firdaus Initial UEIP release
21 ** 21,March 2011 Mohammad Firdaus Changes for Kernel 2.6.32 and IPSec integration
22 *******************************************************************************/
24 \defgroup IFX_DEU IFX_DEU_DRIVERS
26 \brief ifx deu driver module
30 \file ifxmips_sha1_hmac.c
32 \brief SHA1-HMAC deu driver file
36 \defgroup IFX_SHA1_HMAC_FUNCTIONS IFX_SHA1_HMAC_FUNCTIONS
38 \brief ifx sha1 hmac functions
42 #include <linux/init.h>
43 #include <linux/module.h>
45 #include <linux/crypto.h>
46 #include <crypto/internal/hash.h>
47 #include <crypto/sha.h>
48 #include <linux/types.h>
49 #include <linux/scatterlist.h>
50 #include <asm/byteorder.h>
51 #include <linux/delay.h>
53 #if defined(CONFIG_AR9)
54 #include "ifxmips_deu_ar9.h"
55 #elif defined(CONFIG_VR9) || defined(CONFIG_AR10)
56 #include "ifxmips_deu_vr9.h"
58 #error "Plaform Unknwon!"
61 #define SHA1_DIGEST_SIZE 20
62 #define SHA1_BLOCK_WORDS 16
63 #define SHA1_HASH_WORDS 5
64 #define SHA1_HMAC_BLOCK_SIZE 64
65 #define SHA1_HMAC_DBN_TEMP_SIZE 1024 // size in dword, needed for dbn workaround
66 #define HASH_START IFX_HASH_CON
68 #define SHA1_HMAC_MAX_KEYLEN 64
71 extern char debug_level
;
72 #define DPRINTF(level, format, args...) if (level < debug_level) printk(KERN_INFO "[%s %s %d]: " format, __FILE__, __func__, __LINE__, ##args);
74 #define DPRINTF(level, format, args...)
77 struct sha1_hmac_ctx
{
80 u8 buffer
[SHA1_HMAC_BLOCK_SIZE
];
81 u8 key
[SHA1_HMAC_MAX_KEYLEN
];
82 u32 hash
[SHA1_HASH_WORDS
];
87 struct shash_desc
*desc
;
88 u32 (*temp
)[SHA1_BLOCK_WORDS
];
91 extern int disable_deudma
;
93 static int sha1_hmac_final_impl(struct shash_desc
*desc
, u8
*out
, bool hash_final
);
95 /*! \fn static void sha1_hmac_transform(struct crypto_tfm *tfm, u32 const *in)
96 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
97 * \brief save input block to context
98 * \param tfm linux crypto algo transform
99 * \param in 64-byte block of input
101 static int sha1_hmac_transform(struct shash_desc
*desc
, u32
const *in
)
103 struct sha1_hmac_ctx
*sctx
= crypto_shash_ctx(desc
->tfm
);
105 if ( ((sctx
->dbn
<<4)+1) > SHA1_HMAC_DBN_TEMP_SIZE
)
107 //printk("SHA1_HMAC_DBN_TEMP_SIZE exceeded\n");
108 sha1_hmac_final_impl(desc
, (u8
*)sctx
->hash
, false);
111 memcpy(&sctx
->temp
[sctx
->dbn
], in
, 64); //dbn workaround
117 /*! \fn int sha1_hmac_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
118 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
119 * \brief sets sha1 hmac key
120 * \param tfm linux crypto algo transform
121 * \param key input key
122 * \param keylen key length greater than 64 bytes IS NOT SUPPORTED
124 static int sha1_hmac_setkey(struct crypto_shash
*tfm
, const u8
*key
, unsigned int keylen
)
126 struct sha1_hmac_ctx
*sctx
= crypto_shash_ctx(tfm
);
129 if (keylen
> SHA1_HMAC_MAX_KEYLEN
) {
130 char *hash_alg_name
= "sha1";
132 sctx
->desc
->tfm
= crypto_alloc_shash(hash_alg_name
, 0, 0);
133 if (IS_ERR(sctx
->desc
->tfm
)) return PTR_ERR(sctx
->desc
->tfm
);
135 memset(sctx
->key
, 0, SHA1_HMAC_MAX_KEYLEN
);
136 err
= crypto_shash_digest(sctx
->desc
, key
, keylen
, sctx
->key
);
139 sctx
->keylen
= SHA1_DIGEST_SIZE
;
141 crypto_free_shash(sctx
->desc
->tfm
);
143 memcpy(sctx
->key
, key
, keylen
);
144 sctx
->keylen
= keylen
;
146 memset(sctx
->key
+ sctx
->keylen
, 0, SHA1_HMAC_MAX_KEYLEN
- sctx
->keylen
);
148 //printk("Setting keys of len: %d\n", keylen);
153 /*! \fn int sha1_hmac_setkey_hw(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
154 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
155 * \brief sets sha1 hmac key into hw registers
156 * \param tfm linux crypto algo transform
157 * \param key input key
158 * \param keylen key length greater than 64 bytes IS NOT SUPPORTED
160 static int sha1_hmac_setkey_hw(const u8
*key
, unsigned int keylen
)
162 volatile struct deu_hash_t
*hash
= (struct deu_hash_t
*) HASH_START
;
164 u32
*in_key
= (u32
*)key
;
168 hash
->KIDX
|= 0x80000000; //reset keys back to 0
169 for (i
= 0; i
< keylen
; i
+=4)
173 hash
->KEY
= *((u32
*) in_key
+ j
);
180 /*! \fn void sha1_hmac_init(struct crypto_tfm *tfm)
181 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
182 * \brief initialize sha1 hmac context
183 * \param tfm linux crypto algo transform
185 static int sha1_hmac_init(struct shash_desc
*desc
)
187 struct sha1_hmac_ctx
*sctx
= crypto_shash_ctx(desc
->tfm
);
189 //printk("debug ln: %d, fn: %s\n", __LINE__, __func__);
190 sctx
->dbn
= 0; //dbn workaround
196 /*! \fn static void sha1_hmac_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
197 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
198 * \brief on-the-fly sha1 hmac computation
199 * \param tfm linux crypto algo transform
200 * \param data input data
201 * \param len size of input data
203 static int sha1_hmac_update(struct shash_desc
*desc
, const u8
*data
,
206 struct sha1_hmac_ctx
*sctx
= crypto_shash_ctx(desc
->tfm
);
209 j
= (sctx
->count
>> 3) & 0x3f;
210 sctx
->count
+= len
<< 3;
211 // printk("sctx->count = %d\n", sctx->count);
213 if ((j
+ len
) > 63) {
214 memcpy (&sctx
->buffer
[j
], data
, (i
= 64 - j
));
215 sha1_hmac_transform (desc
, (const u32
*)sctx
->buffer
);
216 for (; i
+ 63 < len
; i
+= 64) {
217 sha1_hmac_transform (desc
, (const u32
*)&data
[i
]);
225 memcpy (&sctx
->buffer
[j
], &data
[i
], len
- i
);
229 /*! \fn static int sha1_hmac_final(struct crypto_tfm *tfm, u8 *out)
230 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
231 * \brief call sha1_hmac_final_impl with hash_final true
232 * \param tfm linux crypto algo transform
233 * \param out final sha1 hmac output value
235 static int sha1_hmac_final(struct shash_desc
*desc
, u8
*out
)
237 return sha1_hmac_final_impl(desc
, out
, true);
240 /*! \fn static int sha1_hmac_final_impl(struct crypto_tfm *tfm, u8 *out, bool hash_final)
241 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
242 * \brief ompute final or intermediate sha1 hmac value
243 * \param tfm linux crypto algo transform
244 * \param out final sha1 hmac output value
245 * \param in finalize or intermediate processing
247 static int sha1_hmac_final_impl(struct shash_desc
*desc
, u8
*out
, bool hash_final
)
249 struct sha1_hmac_ctx
*sctx
= crypto_shash_ctx(desc
->tfm
);
253 static const u8 padding
[64] = { 0x80, };
254 volatile struct deu_hash_t
*hashs
= (struct deu_hash_t
*) HASH_START
;
258 u32
*in
= sctx
->temp
[0];
261 t
= sctx
->count
+ 512; // need to add 512 bit of the IPAD operation
278 /* Pad out to 56 mod 64 */
279 index
= (sctx
->count
>> 3) & 0x3f;
280 padlen
= (index
< 56) ? (56 - index
) : ((64 + 56) - index
);
281 sha1_hmac_update (desc
, padding
, padlen
);
284 sha1_hmac_update (desc
, bits
, sizeof bits
);
287 CRTCL_SECT_HASH_START
;
291 sha1_hmac_setkey_hw(sctx
->key
, sctx
->keylen
);
294 hashs
->DBN
= sctx
->dbn
;
296 hashs
->DBN
= sctx
->dbn
+ 5;
300 //for vr9 change, ENDI = 1
301 *IFX_HASH_CON
= HASH_CON_VALUE
;
303 //wait for processing
304 while (hashs
->controlr
.BSY
) {
305 // this will not take long
309 hashs
->D1R
= *((u32
*) sctx
->hash
+ 0);
310 hashs
->D2R
= *((u32
*) sctx
->hash
+ 1);
311 hashs
->D3R
= *((u32
*) sctx
->hash
+ 2);
312 hashs
->D4R
= *((u32
*) sctx
->hash
+ 3);
313 hashs
->D5R
= *((u32
*) sctx
->hash
+ 4);
318 for (dbn
= 0; dbn
< sctx
->dbn
; dbn
++)
320 for (i
= 0; i
< 16; i
++) {
324 hashs
->controlr
.GO
= 1;
327 //wait for processing
328 while (hashs
->controlr
.BSY
) {
329 // this will not take long
338 //wait for digest ready
339 while (! hashs
->controlr
.DGRY
) {
340 // this will not take long
345 *((u32
*) out
+ 0) = hashs
->D1R
;
346 *((u32
*) out
+ 1) = hashs
->D2R
;
347 *((u32
*) out
+ 2) = hashs
->D3R
;
348 *((u32
*) out
+ 3) = hashs
->D4R
;
349 *((u32
*) out
+ 4) = hashs
->D5R
;
354 memset(&sctx
->buffer
[0], 0, SHA1_HMAC_BLOCK_SIZE
);
359 //printk("debug ln: %d, fn: %s\n", __LINE__, __func__);
365 /*! \fn void sha1_hmac_init_tfm(struct crypto_tfm *tfm)
366 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
367 * \brief initialize pointers in sha1_hmac_ctx
368 * \param tfm linux crypto algo transform
370 static int sha1_hmac_init_tfm(struct crypto_tfm
*tfm
)
372 struct sha1_hmac_ctx
*sctx
= crypto_tfm_ctx(tfm
);
373 sctx
->temp
= kzalloc(4 * SHA1_HMAC_DBN_TEMP_SIZE
, GFP_KERNEL
);
374 if (IS_ERR(sctx
->temp
)) return PTR_ERR(sctx
->temp
);
375 sctx
->desc
= kzalloc(sizeof(struct shash_desc
), GFP_KERNEL
);
376 if (IS_ERR(sctx
->desc
)) return PTR_ERR(sctx
->desc
);
381 /*! \fn void sha1_hmac_exit_tfm(struct crypto_tfm *tfm)
382 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
383 * \brief free pointers in sha1_hmac_ctx
384 * \param tfm linux crypto algo transform
386 static void sha1_hmac_exit_tfm(struct crypto_tfm
*tfm
)
388 struct sha1_hmac_ctx
*sctx
= crypto_tfm_ctx(tfm
);
394 * \brief SHA1_HMAC function mappings
397 static struct shash_alg ifxdeu_sha1_hmac_alg
= {
398 .digestsize
= SHA1_DIGEST_SIZE
,
399 .init
= sha1_hmac_init
,
400 .update
= sha1_hmac_update
,
401 .final
= sha1_hmac_final
,
402 .setkey
= sha1_hmac_setkey
,
403 .descsize
= sizeof(struct sha1_hmac_ctx
),
405 .cra_name
= "hmac(sha1)",
406 .cra_driver_name
= "ifxdeu-sha1_hmac",
408 .cra_ctxsize
= sizeof(struct sha1_hmac_ctx
),
409 .cra_flags
= CRYPTO_ALG_TYPE_HASH
| CRYPTO_ALG_KERN_DRIVER_ONLY
,
410 .cra_blocksize
= SHA1_HMAC_BLOCK_SIZE
,
411 .cra_module
= THIS_MODULE
,
412 .cra_init
= sha1_hmac_init_tfm
,
413 .cra_exit
= sha1_hmac_exit_tfm
,
417 /*! \fn int ifxdeu_init_sha1_hmac (void)
418 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
419 * \brief initialize sha1 hmac driver
421 int ifxdeu_init_sha1_hmac (void)
427 if ((ret
= crypto_register_shash(&ifxdeu_sha1_hmac_alg
)))
430 printk (KERN_NOTICE
"IFX DEU SHA1_HMAC initialized%s.\n", disable_deudma
? "" : " (DMA)");
434 printk(KERN_ERR
"IFX DEU SHA1_HMAC initialization failed!\n");
438 /*! \fn void ifxdeu_fini_sha1_hmac (void)
439 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
440 * \brief unregister sha1 hmac driver
442 void ifxdeu_fini_sha1_hmac (void)
445 crypto_unregister_shash(&ifxdeu_sha1_hmac_alg
);