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
43 #include <linux/init.h>
44 #include <linux/module.h>
46 #include <linux/crypto.h>
47 #include <crypto/internal/hash.h>
48 #include <crypto/sha.h>
49 #include <linux/types.h>
50 #include <linux/scatterlist.h>
51 #include <asm/byteorder.h>
52 #include <linux/delay.h>
54 #if defined(CONFIG_AR9)
55 #include "ifxmips_deu_ar9.h"
56 #elif defined(CONFIG_VR9) || defined(CONFIG_AR10)
57 #include "ifxmips_deu_vr9.h"
59 #error "Plaform Unknwon!"
62 #define SHA1_DIGEST_SIZE 20
63 #define SHA1_HMAC_BLOCK_SIZE 64
64 #define SHA1_HMAC_DBN_TEMP_SIZE 1024 // size in dword, needed for dbn workaround
65 #define HASH_START IFX_HASH_CON
67 #define SHA1_HMAC_MAX_KEYLEN 64
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);
73 #define DPRINTF(level, format, args...)
76 struct sha1_hmac_ctx
{
79 u8 buffer
[SHA1_HMAC_BLOCK_SIZE
];
80 u8 key
[SHA1_HMAC_MAX_KEYLEN
];
87 static u32 temp
[SHA1_HMAC_DBN_TEMP_SIZE
];
89 extern int disable_deudma
;
91 /*! \fn static void sha1_hmac_transform(struct crypto_tfm *tfm, u32 const *in)
92 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
93 * \brief save input block to context
94 * \param tfm linux crypto algo transform
95 * \param in 64-byte block of input
97 static int sha1_hmac_transform(struct shash_desc
*desc
, u32
const *in
)
99 struct sha1_hmac_ctx
*sctx
= crypto_shash_ctx(desc
->tfm
);
101 memcpy(&temp
[sctx
->dbn
<<4], in
, 64); //dbn workaround
104 if ( (sctx
->dbn
<<4) > SHA1_HMAC_DBN_TEMP_SIZE
)
106 printk("SHA1_HMAC_DBN_TEMP_SIZE exceeded\n");
112 /*! \fn int sha1_hmac_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
113 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
114 * \brief sets sha1 hmac key
115 * \param tfm linux crypto algo transform
116 * \param key input key
117 * \param keylen key length greater than 64 bytes IS NOT SUPPORTED
119 static int sha1_hmac_setkey(struct crypto_shash
*tfm
, const u8
*key
, unsigned int keylen
)
121 struct sha1_hmac_ctx
*sctx
= crypto_shash_ctx(tfm
);
123 if (keylen
> SHA1_HMAC_MAX_KEYLEN
) {
124 printk("Key length exceeds maximum key length\n");
128 //printk("Setting keys of len: %d\n", keylen);
130 memcpy(&sctx
->key
, key
, keylen
);
131 sctx
->keylen
= keylen
;
138 /*! \fn int sha1_hmac_setkey_hw(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
139 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
140 * \brief sets sha1 hmac key into hw registers
141 * \param tfm linux crypto algo transform
142 * \param key input key
143 * \param keylen key length greater than 64 bytes IS NOT SUPPORTED
145 static int sha1_hmac_setkey_hw(const u8
*key
, unsigned int keylen
)
147 volatile struct deu_hash_t
*hash
= (struct deu_hash_t
*) HASH_START
;
149 u32
*in_key
= (u32
*)key
;
153 hash
->KIDX
|= 0x80000000; //reset keys back to 0
154 for (i
= 0; i
< keylen
; i
+=4)
158 hash
->KEY
= *((u32
*) in_key
+ j
);
165 /*! \fn void sha1_hmac_init(struct crypto_tfm *tfm)
166 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
167 * \brief initialize sha1 hmac context
168 * \param tfm linux crypto algo transform
170 static int sha1_hmac_init(struct shash_desc
*desc
)
172 struct sha1_hmac_ctx
*sctx
= crypto_shash_ctx(desc
->tfm
);
174 //printk("debug ln: %d, fn: %s\n", __LINE__, __func__);
175 sctx
->dbn
= 0; //dbn workaround
180 /*! \fn static void sha1_hmac_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
181 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
182 * \brief on-the-fly sha1 hmac computation
183 * \param tfm linux crypto algo transform
184 * \param data input data
185 * \param len size of input data
187 static int sha1_hmac_update(struct shash_desc
*desc
, const u8
*data
,
190 struct sha1_hmac_ctx
*sctx
= crypto_shash_ctx(desc
->tfm
);
193 j
= (sctx
->count
>> 3) & 0x3f;
194 sctx
->count
+= len
<< 3;
195 // printk("sctx->count = %d\n", sctx->count);
197 if ((j
+ len
) > 63) {
198 memcpy (&sctx
->buffer
[j
], data
, (i
= 64 - j
));
199 sha1_hmac_transform (desc
, (const u32
*)sctx
->buffer
);
200 for (; i
+ 63 < len
; i
+= 64) {
201 sha1_hmac_transform (desc
, (const u32
*)&data
[i
]);
209 memcpy (&sctx
->buffer
[j
], &data
[i
], len
- i
);
213 /*! \fn static void sha1_hmac_final(struct crypto_tfm *tfm, u8 *out)
214 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
215 * \brief ompute final sha1 hmac value
216 * \param tfm linux crypto algo transform
217 * \param out final sha1 hmac output value
219 static int sha1_hmac_final(struct shash_desc
*desc
, u8
*out
)
221 //struct sha1_hmac_ctx *sctx = shash_desc_ctx(desc);
222 struct sha1_hmac_ctx
*sctx
= crypto_shash_ctx(desc
->tfm
);
226 static const u8 padding
[64] = { 0x80, };
227 volatile struct deu_hash_t
*hashs
= (struct deu_hash_t
*) HASH_START
;
233 t
= sctx
->count
+ 512; // need to add 512 bit of the IPAD operation
250 /* Pad out to 56 mod 64 */
251 index
= (sctx
->count
>> 3) & 0x3f;
252 padlen
= (index
< 56) ? (56 - index
) : ((64 + 56) - index
);
253 sha1_hmac_update (desc
, padding
, padlen
);
256 sha1_hmac_update (desc
, bits
, sizeof bits
);
258 CRTCL_SECT_HASH_START
;
262 sha1_hmac_setkey_hw(sctx
->key
, sctx
->keylen
);
264 hashs
->DBN
= sctx
->dbn
;
266 //for vr9 change, ENDI = 1
267 *IFX_HASH_CON
= HASH_CON_VALUE
;
269 //wait for processing
270 while (hashs
->controlr
.BSY
) {
271 // this will not take long
274 for (dbn
= 0; dbn
< sctx
->dbn
; dbn
++)
276 for (i
= 0; i
< 16; i
++) {
280 hashs
->controlr
.GO
= 1;
283 //wait for processing
284 while (hashs
->controlr
.BSY
) {
285 // this will not take long
293 //wait for digest ready
294 while (! hashs
->controlr
.DGRY
) {
295 // this will not take long
299 *((u32
*) out
+ 0) = hashs
->D1R
;
300 *((u32
*) out
+ 1) = hashs
->D2R
;
301 *((u32
*) out
+ 2) = hashs
->D3R
;
302 *((u32
*) out
+ 3) = hashs
->D4R
;
303 *((u32
*) out
+ 4) = hashs
->D5R
;
305 memset(&sctx
->buffer
[0], 0, SHA1_HMAC_BLOCK_SIZE
);
308 //printk("debug ln: %d, fn: %s\n", __LINE__, __func__);
317 * \brief SHA1-HMAC function mappings
319 static struct shash_alg ifxdeu_sha1_hmac_alg
= {
320 .digestsize
= SHA1_DIGEST_SIZE
,
321 .init
= sha1_hmac_init
,
322 .update
= sha1_hmac_update
,
323 .final
= sha1_hmac_final
,
324 .setkey
= sha1_hmac_setkey
,
325 .descsize
= sizeof(struct sha1_hmac_ctx
),
327 .cra_name
= "hmac(sha1)",
328 .cra_driver_name
= "ifxdeu-sha1_hmac",
330 .cra_ctxsize
= sizeof(struct sha1_hmac_ctx
),
331 .cra_flags
= CRYPTO_ALG_TYPE_HASH
| CRYPTO_ALG_KERN_DRIVER_ONLY
,
332 .cra_blocksize
= SHA1_HMAC_BLOCK_SIZE
,
333 .cra_module
= THIS_MODULE
,
339 /*! \fn int ifxdeu_init_sha1_hmac (void)
340 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
341 * \brief initialize sha1 hmac driver
343 int ifxdeu_init_sha1_hmac (void)
349 if ((ret
= crypto_register_shash(&ifxdeu_sha1_hmac_alg
)))
352 printk (KERN_NOTICE
"IFX DEU SHA1_HMAC initialized%s.\n", disable_deudma
? "" : " (DMA)");
356 printk(KERN_ERR
"IFX DEU SHA1_HMAC initialization failed!\n");
360 /*! \fn void ifxdeu_fini_sha1_hmac (void)
361 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
362 * \brief unregister sha1 hmac driver
364 void ifxdeu_fini_sha1_hmac (void)
367 crypto_unregister_shash(&ifxdeu_sha1_hmac_alg
);