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 <linux/version.h>
48 #if LINUX_VERSION_CODE < KERNEL_VERSION(5,11,0)
49 #include <crypto/sha.h>
51 #include <crypto/sha1.h>
53 #include <linux/types.h>
54 #include <linux/scatterlist.h>
55 #include <asm/byteorder.h>
56 #include <linux/delay.h>
58 #if defined(CONFIG_AR9)
59 #include "ifxmips_deu_ar9.h"
60 #elif defined(CONFIG_VR9) || defined(CONFIG_AR10)
61 #include "ifxmips_deu_vr9.h"
63 #error "Plaform Unknwon!"
66 #define SHA1_DIGEST_SIZE 20
67 #define SHA1_BLOCK_WORDS 16
68 #define SHA1_HASH_WORDS 5
69 #define SHA1_HMAC_BLOCK_SIZE 64
70 #define SHA1_HMAC_DBN_TEMP_SIZE 1024 // size in dword, needed for dbn workaround
71 #define HASH_START IFX_HASH_CON
73 #define SHA1_HMAC_MAX_KEYLEN 64
76 extern char debug_level
;
77 #define DPRINTF(level, format, args...) if (level < debug_level) printk(KERN_INFO "[%s %s %d]: " format, __FILE__, __func__, __LINE__, ##args);
79 #define DPRINTF(level, format, args...)
82 struct sha1_hmac_ctx
{
85 u8 buffer
[SHA1_HMAC_BLOCK_SIZE
];
86 u8 key
[SHA1_HMAC_MAX_KEYLEN
];
87 u32 hash
[SHA1_HASH_WORDS
];
92 struct shash_desc
*desc
;
93 u32 (*temp
)[SHA1_BLOCK_WORDS
];
96 extern int disable_deudma
;
98 static int sha1_hmac_final_impl(struct shash_desc
*desc
, u8
*out
, bool hash_final
);
100 /*! \fn static void sha1_hmac_transform(struct crypto_tfm *tfm, u32 const *in)
101 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
102 * \brief save input block to context
103 * \param tfm linux crypto algo transform
104 * \param in 64-byte block of input
106 static int sha1_hmac_transform(struct shash_desc
*desc
, u32
const *in
)
108 struct sha1_hmac_ctx
*sctx
= crypto_shash_ctx(desc
->tfm
);
110 if ( ((sctx
->dbn
<<4)+1) > SHA1_HMAC_DBN_TEMP_SIZE
)
112 //printk("SHA1_HMAC_DBN_TEMP_SIZE exceeded\n");
113 sha1_hmac_final_impl(desc
, (u8
*)sctx
->hash
, false);
116 memcpy(&sctx
->temp
[sctx
->dbn
], in
, 64); //dbn workaround
122 /*! \fn int sha1_hmac_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
123 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
124 * \brief sets sha1 hmac key
125 * \param tfm linux crypto algo transform
126 * \param key input key
127 * \param keylen key length greater than 64 bytes IS NOT SUPPORTED
129 static int sha1_hmac_setkey(struct crypto_shash
*tfm
, const u8
*key
, unsigned int keylen
)
131 struct sha1_hmac_ctx
*sctx
= crypto_shash_ctx(tfm
);
134 if (keylen
> SHA1_HMAC_MAX_KEYLEN
) {
135 char *hash_alg_name
= "sha1";
137 sctx
->desc
->tfm
= crypto_alloc_shash(hash_alg_name
, 0, 0);
138 if (IS_ERR(sctx
->desc
->tfm
)) return PTR_ERR(sctx
->desc
->tfm
);
140 memset(sctx
->key
, 0, SHA1_HMAC_MAX_KEYLEN
);
141 err
= crypto_shash_digest(sctx
->desc
, key
, keylen
, sctx
->key
);
144 sctx
->keylen
= SHA1_DIGEST_SIZE
;
146 crypto_free_shash(sctx
->desc
->tfm
);
148 memcpy(sctx
->key
, key
, keylen
);
149 sctx
->keylen
= keylen
;
151 memset(sctx
->key
+ sctx
->keylen
, 0, SHA1_HMAC_MAX_KEYLEN
- sctx
->keylen
);
153 //printk("Setting keys of len: %d\n", keylen);
158 /*! \fn int sha1_hmac_setkey_hw(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
159 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
160 * \brief sets sha1 hmac key into hw registers
161 * \param tfm linux crypto algo transform
162 * \param key input key
163 * \param keylen key length greater than 64 bytes IS NOT SUPPORTED
165 static int sha1_hmac_setkey_hw(const u8
*key
, unsigned int keylen
)
167 volatile struct deu_hash_t
*hash
= (struct deu_hash_t
*) HASH_START
;
169 u32
*in_key
= (u32
*)key
;
173 hash
->KIDX
|= 0x80000000; //reset keys back to 0
174 for (i
= 0; i
< keylen
; i
+=4)
178 hash
->KEY
= *((u32
*) in_key
+ j
);
185 /*! \fn void sha1_hmac_init(struct crypto_tfm *tfm)
186 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
187 * \brief initialize sha1 hmac context
188 * \param tfm linux crypto algo transform
190 static int sha1_hmac_init(struct shash_desc
*desc
)
192 struct sha1_hmac_ctx
*sctx
= crypto_shash_ctx(desc
->tfm
);
194 //printk("debug ln: %d, fn: %s\n", __LINE__, __func__);
195 sctx
->dbn
= 0; //dbn workaround
202 /*! \fn static void sha1_hmac_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
203 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
204 * \brief on-the-fly sha1 hmac computation
205 * \param tfm linux crypto algo transform
206 * \param data input data
207 * \param len size of input data
209 static int sha1_hmac_update(struct shash_desc
*desc
, const u8
*data
,
212 struct sha1_hmac_ctx
*sctx
= crypto_shash_ctx(desc
->tfm
);
215 j
= (sctx
->count
>> 3) & 0x3f;
216 sctx
->count
+= len
<< 3;
217 // printk("sctx->count = %d\n", sctx->count);
219 if ((j
+ len
) > 63) {
220 memcpy (&sctx
->buffer
[j
], data
, (i
= 64 - j
));
221 sha1_hmac_transform (desc
, (const u32
*)sctx
->buffer
);
222 for (; i
+ 63 < len
; i
+= 64) {
223 sha1_hmac_transform (desc
, (const u32
*)&data
[i
]);
231 memcpy (&sctx
->buffer
[j
], &data
[i
], len
- i
);
235 /*! \fn static int sha1_hmac_final(struct crypto_tfm *tfm, u8 *out)
236 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
237 * \brief call sha1_hmac_final_impl with hash_final true
238 * \param tfm linux crypto algo transform
239 * \param out final sha1 hmac output value
241 static int sha1_hmac_final(struct shash_desc
*desc
, u8
*out
)
243 return sha1_hmac_final_impl(desc
, out
, true);
246 /*! \fn static int sha1_hmac_final_impl(struct crypto_tfm *tfm, u8 *out, bool hash_final)
247 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
248 * \brief ompute final or intermediate sha1 hmac value
249 * \param tfm linux crypto algo transform
250 * \param out final sha1 hmac output value
251 * \param in finalize or intermediate processing
253 static int sha1_hmac_final_impl(struct shash_desc
*desc
, u8
*out
, bool hash_final
)
255 struct sha1_hmac_ctx
*sctx
= crypto_shash_ctx(desc
->tfm
);
259 static const u8 padding
[64] = { 0x80, };
260 volatile struct deu_hash_t
*hashs
= (struct deu_hash_t
*) HASH_START
;
264 u32
*in
= sctx
->temp
[0];
267 t
= sctx
->count
+ 512; // need to add 512 bit of the IPAD operation
284 /* Pad out to 56 mod 64 */
285 index
= (sctx
->count
>> 3) & 0x3f;
286 padlen
= (index
< 56) ? (56 - index
) : ((64 + 56) - index
);
287 sha1_hmac_update (desc
, padding
, padlen
);
290 sha1_hmac_update (desc
, bits
, sizeof bits
);
293 CRTCL_SECT_HASH_START
;
297 sha1_hmac_setkey_hw(sctx
->key
, sctx
->keylen
);
300 hashs
->DBN
= sctx
->dbn
;
302 hashs
->DBN
= sctx
->dbn
+ 5;
306 //for vr9 change, ENDI = 1
307 *IFX_HASH_CON
= HASH_CON_VALUE
;
309 //wait for processing
310 while (hashs
->controlr
.BSY
) {
311 // this will not take long
315 hashs
->D1R
= *((u32
*) sctx
->hash
+ 0);
316 hashs
->D2R
= *((u32
*) sctx
->hash
+ 1);
317 hashs
->D3R
= *((u32
*) sctx
->hash
+ 2);
318 hashs
->D4R
= *((u32
*) sctx
->hash
+ 3);
319 hashs
->D5R
= *((u32
*) sctx
->hash
+ 4);
324 for (dbn
= 0; dbn
< sctx
->dbn
; dbn
++)
326 for (i
= 0; i
< 16; i
++) {
330 hashs
->controlr
.GO
= 1;
333 //wait for processing
334 while (hashs
->controlr
.BSY
) {
335 // this will not take long
344 //wait for digest ready
345 while (! hashs
->controlr
.DGRY
) {
346 // this will not take long
351 *((u32
*) out
+ 0) = hashs
->D1R
;
352 *((u32
*) out
+ 1) = hashs
->D2R
;
353 *((u32
*) out
+ 2) = hashs
->D3R
;
354 *((u32
*) out
+ 3) = hashs
->D4R
;
355 *((u32
*) out
+ 4) = hashs
->D5R
;
360 sha1_hmac_init(desc
);
364 //printk("debug ln: %d, fn: %s\n", __LINE__, __func__);
370 /*! \fn void sha1_hmac_init_tfm(struct crypto_tfm *tfm)
371 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
372 * \brief initialize pointers in sha1_hmac_ctx
373 * \param tfm linux crypto algo transform
375 static int sha1_hmac_init_tfm(struct crypto_tfm
*tfm
)
377 struct sha1_hmac_ctx
*sctx
= crypto_tfm_ctx(tfm
);
378 sctx
->temp
= kzalloc(4 * SHA1_HMAC_DBN_TEMP_SIZE
, GFP_KERNEL
);
379 if (IS_ERR(sctx
->temp
)) return PTR_ERR(sctx
->temp
);
380 sctx
->desc
= kzalloc(sizeof(struct shash_desc
), GFP_KERNEL
);
381 if (IS_ERR(sctx
->desc
)) return PTR_ERR(sctx
->desc
);
386 /*! \fn void sha1_hmac_exit_tfm(struct crypto_tfm *tfm)
387 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
388 * \brief free pointers in sha1_hmac_ctx
389 * \param tfm linux crypto algo transform
391 static void sha1_hmac_exit_tfm(struct crypto_tfm
*tfm
)
393 struct sha1_hmac_ctx
*sctx
= crypto_tfm_ctx(tfm
);
399 * \brief SHA1_HMAC function mappings
402 static struct shash_alg ifxdeu_sha1_hmac_alg
= {
403 .digestsize
= SHA1_DIGEST_SIZE
,
404 .init
= sha1_hmac_init
,
405 .update
= sha1_hmac_update
,
406 .final
= sha1_hmac_final
,
407 .setkey
= sha1_hmac_setkey
,
408 .descsize
= sizeof(struct sha1_hmac_ctx
),
410 .cra_name
= "hmac(sha1)",
411 .cra_driver_name
= "ifxdeu-sha1_hmac",
413 .cra_ctxsize
= sizeof(struct sha1_hmac_ctx
),
414 .cra_flags
= CRYPTO_ALG_TYPE_HASH
| CRYPTO_ALG_KERN_DRIVER_ONLY
,
415 .cra_blocksize
= SHA1_HMAC_BLOCK_SIZE
,
416 .cra_module
= THIS_MODULE
,
417 .cra_init
= sha1_hmac_init_tfm
,
418 .cra_exit
= sha1_hmac_exit_tfm
,
422 /*! \fn int ifxdeu_init_sha1_hmac (void)
423 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
424 * \brief initialize sha1 hmac driver
426 int ifxdeu_init_sha1_hmac (void)
432 if ((ret
= crypto_register_shash(&ifxdeu_sha1_hmac_alg
)))
435 printk (KERN_NOTICE
"IFX DEU SHA1_HMAC initialized%s.\n", disable_deudma
? "" : " (DMA)");
439 printk(KERN_ERR
"IFX DEU SHA1_HMAC initialization failed!\n");
443 /*! \fn void ifxdeu_fini_sha1_hmac (void)
444 * \ingroup IFX_SHA1_HMAC_FUNCTIONS
445 * \brief unregister sha1 hmac driver
447 void ifxdeu_fini_sha1_hmac (void)
450 crypto_unregister_shash(&ifxdeu_sha1_hmac_alg
);