1 /******************************************************************************
3 ** FILE NAME : ifxmips_md5_hmac.c
5 ** MODULES : DEU Module for UEIP
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
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.
20 ** $Date $Author $Comment
21 ** 08,Sept 2009 Mohammad Firdaus Initial UEIP release
22 ** 21,March 2011 Mohammad Firdaus Changes for Kernel 2.6.32 and IPSec integration
23 *******************************************************************************/
25 \defgroup IFX_DEU IFX_DEU_DRIVERS
27 \brief ifx deu driver module
31 \file ifxmips_md5_hmac.c
33 \brief MD5-HMAC encryption deu driver file
37 \defgroup IFX_MD5_HMAC_FUNCTIONS IFX_MD5_HMAC_FUNCTIONS
39 \brief ifx md5-hmac driver functions
42 /* Project Header files */
43 #include <linux/init.h>
44 #include <linux/module.h>
45 #include <linux/string.h>
46 #include <linux/crypto.h>
47 #include <linux/types.h>
48 #include <crypto/internal/hash.h>
49 #include <asm/byteorder.h>
51 #if defined(CONFIG_AR9)
52 #include "ifxmips_deu_ar9.h"
53 #elif defined(CONFIG_VR9) || defined(CONFIG_AR10)
54 #include "ifxmips_deu_vr9.h"
56 #error "Plaform Unknwon!"
59 #define MD5_DIGEST_SIZE 16
60 #define MD5_HMAC_BLOCK_SIZE 64
61 #define MD5_BLOCK_WORDS 16
62 #define MD5_HASH_WORDS 4
63 #define MD5_HMAC_DBN_TEMP_SIZE 1024 // size in dword, needed for dbn workaround
64 #define HASH_START IFX_HASH_CON
66 //#define CRYPTO_DEBUG
68 extern char debug_level
;
69 #define DPRINTF(level, format, args...) if (level < debug_level) printk(KERN_INFO "[%s %s %d]: " format, __FILE__, __func__, __LINE__, ##args);
71 #define DPRINTF(level, format, args...)
74 #define MAX_HASH_KEYLEN 64
77 u8 key
[MAX_HASH_KEYLEN
];
78 u32 hash
[MD5_HASH_WORDS
];
79 u32 block
[MD5_BLOCK_WORDS
];
83 struct shash_desc
*desc
;
84 u32 (*temp
)[MD5_BLOCK_WORDS
];
87 extern int disable_deudma
;
89 /*! \fn static void md5_hmac_transform(struct crypto_tfm *tfm, u32 const *in)
90 * \ingroup IFX_MD5_HMAC_FUNCTIONS
91 * \brief save input block to context
92 * \param tfm linux crypto algo transform
93 * \param in 64-byte block of input
95 static void md5_hmac_transform(struct shash_desc
*desc
, u32
const *in
)
97 struct md5_hmac_ctx
*mctx
= crypto_shash_ctx(desc
->tfm
);
99 memcpy(&mctx
->temp
[mctx
->dbn
], in
, 64); //dbn workaround
102 if ( (mctx
->dbn
<<4) > MD5_HMAC_DBN_TEMP_SIZE
)
104 printk("MD5_HMAC_DBN_TEMP_SIZE exceeded\n");
109 /*! \fn int md5_hmac_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
110 * \ingroup IFX_MD5_HMAC_FUNCTIONS
111 * \brief sets md5 hmac key
112 * \param tfm linux crypto algo transform
113 * \param key input key
114 * \param keylen key length greater than 64 bytes IS NOT SUPPORTED
116 static int md5_hmac_setkey(struct crypto_shash
*tfm
, const u8
*key
, unsigned int keylen
)
118 struct md5_hmac_ctx
*mctx
= crypto_shash_ctx(tfm
);
119 volatile struct deu_hash_t
*hash
= (struct deu_hash_t
*) HASH_START
;
121 //printk("copying keys to context with length %d\n", keylen);
123 if (keylen
> MAX_HASH_KEYLEN
) {
124 char *hash_alg_name
= "md5";
126 mctx
->desc
->tfm
= crypto_alloc_shash(hash_alg_name
, 0, 0);
127 if (IS_ERR(mctx
->desc
->tfm
)) return PTR_ERR(mctx
->desc
->tfm
);
129 memset(mctx
->key
, 0, MAX_HASH_KEYLEN
);
130 err
= crypto_shash_digest(mctx
->desc
, key
, keylen
, mctx
->key
);
133 mctx
->keylen
= MD5_DIGEST_SIZE
;
135 crypto_free_shash(mctx
->desc
->tfm
);
137 memcpy(mctx
->key
, key
, keylen
);
138 mctx
->keylen
= keylen
;
140 memset(mctx
->key
+ mctx
->keylen
, 0, MAX_HASH_KEYLEN
- mctx
->keylen
);
146 /*! \fn int md5_hmac_setkey_hw(const u8 *key, unsigned int keylen)
147 * \ingroup IFX_MD5_HMAC_FUNCTIONS
148 * \brief sets md5 hmac key into the hardware registers
149 * \param key input key
150 * \param keylen key length greater than 64 bytes IS NOT SUPPORTED
153 static int md5_hmac_setkey_hw(const u8
*key
, unsigned int keylen
)
155 volatile struct deu_hash_t
*hash
= (struct deu_hash_t
*) HASH_START
;
157 u32
*in_key
= (u32
*)key
;
159 //printk("\nsetkey keylen: %d\n key: ", keylen);
161 hash
->KIDX
|= 0x80000000; // reset all 16 words of the key to '0'
163 for (i
= 0; i
< keylen
; i
+=4)
167 hash
->KEY
= *((u32
*) in_key
+ j
);
175 /*! \fn void md5_hmac_init(struct crypto_tfm *tfm)
176 * \ingroup IFX_MD5_HMAC_FUNCTIONS
177 * \brief initialize md5 hmac context
178 * \param tfm linux crypto algo transform
180 static int md5_hmac_init(struct shash_desc
*desc
)
183 struct md5_hmac_ctx
*mctx
= crypto_shash_ctx(desc
->tfm
);
186 mctx
->dbn
= 0; //dbn workaround
191 /*! \fn void md5_hmac_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
192 * \ingroup IFX_MD5_HMAC_FUNCTIONS
193 * \brief on-the-fly md5 hmac computation
194 * \param tfm linux crypto algo transform
195 * \param data input data
196 * \param len size of input data
198 static int md5_hmac_update(struct shash_desc
*desc
, const u8
*data
, unsigned int len
)
200 struct md5_hmac_ctx
*mctx
= crypto_shash_ctx(desc
->tfm
);
201 const u32 avail
= sizeof(mctx
->block
) - (mctx
->byte_count
& 0x3f);
203 mctx
->byte_count
+= len
;
206 memcpy((char *)mctx
->block
+ (sizeof(mctx
->block
) - avail
),
211 memcpy((char *)mctx
->block
+ (sizeof(mctx
->block
) - avail
),
214 md5_hmac_transform(desc
, mctx
->block
);
218 while (len
>= sizeof(mctx
->block
)) {
219 memcpy(mctx
->block
, data
, sizeof(mctx
->block
));
220 md5_hmac_transform(desc
, mctx
->block
);
221 data
+= sizeof(mctx
->block
);
222 len
-= sizeof(mctx
->block
);
225 memcpy(mctx
->block
, data
, len
);
229 /*! \fn void md5_hmac_final(struct crypto_tfm *tfm, u8 *out)
230 * \ingroup IFX_MD5_HMAC_FUNCTIONS
231 * \brief compute final md5 hmac value
232 * \param tfm linux crypto algo transform
233 * \param out final md5 hmac output value
235 static int md5_hmac_final(struct shash_desc
*desc
, u8
*out
)
237 struct md5_hmac_ctx
*mctx
= crypto_shash_ctx(desc
->tfm
);
238 const unsigned int offset
= mctx
->byte_count
& 0x3f;
239 char *p
= (char *)mctx
->block
+ offset
;
240 int padding
= 56 - (offset
+ 1);
241 volatile struct deu_hash_t
*hashs
= (struct deu_hash_t
*) HASH_START
;
245 u32
*in
= mctx
->temp
[0];
250 memset(p
, 0x00, padding
+ sizeof (u64
));
251 md5_hmac_transform(desc
, mctx
->block
);
252 p
= (char *)mctx
->block
;
256 memset(p
, 0, padding
);
257 mctx
->block
[14] = le32_to_cpu((mctx
->byte_count
+ 64) << 3); // need to add 512 bit of the IPAD operation
258 mctx
->block
[15] = 0x00000000;
260 md5_hmac_transform(desc
, mctx
->block
);
262 CRTCL_SECT_HASH_START
;
266 md5_hmac_setkey_hw(mctx
->key
, mctx
->keylen
);
268 //printk("\ndbn = %d\n", mctx->dbn);
269 hashs
->DBN
= mctx
->dbn
;
272 *IFX_HASH_CON
= 0x0703002D; //khs, go, init, ndc, endi, kyue, hmen, md5
274 //wait for processing
275 while (hashs
->controlr
.BSY
) {
276 // this will not take long
279 for (dbn
= 0; dbn
< mctx
->dbn
; dbn
++)
281 for (i
= 0; i
< 16; i
++) {
285 hashs
->controlr
.GO
= 1;
288 //wait for processing
289 while (hashs
->controlr
.BSY
) {
290 // this will not take long
298 //wait for digest ready
299 while (! hashs
->controlr
.DGRY
) {
300 // this will not take long
304 *((u32
*) out
+ 0) = hashs
->D1R
;
305 *((u32
*) out
+ 1) = hashs
->D2R
;
306 *((u32
*) out
+ 2) = hashs
->D3R
;
307 *((u32
*) out
+ 3) = hashs
->D4R
;
309 /* reset the context after we finish with the hash */
310 mctx
->byte_count
= 0;
311 memset(&mctx
->hash
[0], 0, sizeof(MD5_HASH_WORDS
));
312 memset(&mctx
->block
[0], 0, sizeof(MD5_BLOCK_WORDS
));
313 memset(&mctx
->temp
[0], 0, MD5_HMAC_DBN_TEMP_SIZE
);
321 /*! \fn void md5_hmac_init_tfm(struct crypto_tfm *tfm)
322 * \ingroup IFX_MD5_HMAC_FUNCTIONS
323 * \brief initialize pointers in md5_hmac_ctx
324 * \param tfm linux crypto algo transform
326 static int md5_hmac_init_tfm(struct crypto_tfm
*tfm
)
328 struct md5_hmac_ctx
*mctx
= crypto_tfm_ctx(tfm
);
329 mctx
->temp
= kzalloc(4 * MD5_HMAC_DBN_TEMP_SIZE
, GFP_KERNEL
);
330 if (IS_ERR(mctx
->temp
)) return PTR_ERR(mctx
->temp
);
331 mctx
->desc
= kzalloc(sizeof(struct shash_desc
), GFP_KERNEL
);
332 if (IS_ERR(mctx
->desc
)) return PTR_ERR(mctx
->desc
);
337 /*! \fn void md5_hmac_exit_tfm(struct crypto_tfm *tfm)
338 * \ingroup IFX_MD5_HMAC_FUNCTIONS
339 * \brief free pointers in md5_hmac_ctx
340 * \param tfm linux crypto algo transform
342 static void md5_hmac_exit_tfm(struct crypto_tfm
*tfm
)
344 struct md5_hmac_ctx
*mctx
= crypto_tfm_ctx(tfm
);
351 * \brief MD5_HMAC function mappings
354 static struct shash_alg ifxdeu_md5_hmac_alg
= {
355 .digestsize
= MD5_DIGEST_SIZE
,
356 .init
= md5_hmac_init
,
357 .update
= md5_hmac_update
,
358 .final
= md5_hmac_final
,
359 .setkey
= md5_hmac_setkey
,
360 .descsize
= sizeof(struct md5_hmac_ctx
),
362 .cra_name
= "hmac(md5)",
363 .cra_driver_name
= "ifxdeu-md5_hmac",
365 .cra_ctxsize
= sizeof(struct md5_hmac_ctx
),
366 .cra_flags
= CRYPTO_ALG_TYPE_HASH
| CRYPTO_ALG_KERN_DRIVER_ONLY
,
367 .cra_blocksize
= MD5_HMAC_BLOCK_SIZE
,
368 .cra_module
= THIS_MODULE
,
369 .cra_init
= md5_hmac_init_tfm
,
370 .cra_exit
= md5_hmac_exit_tfm
,
374 /*! \fn int ifxdeu_init_md5_hmac (void)
375 * \ingroup IFX_MD5_HMAC_FUNCTIONS
376 * \brief initialize md5 hmac driver
378 int ifxdeu_init_md5_hmac (void)
384 if ((ret
= crypto_register_shash(&ifxdeu_md5_hmac_alg
)))
387 printk (KERN_NOTICE
"IFX DEU MD5_HMAC initialized%s.\n", disable_deudma
? "" : " (DMA)");
391 printk(KERN_ERR
"IFX DEU MD5_HMAC initialization failed!\n");
395 /** \fn void ifxdeu_fini_md5_hmac (void)
396 * \ingroup IFX_MD5_HMAC_FUNCTIONS
397 * \brief unregister md5 hmac driver
399 void ifxdeu_fini_md5_hmac (void)
401 crypto_unregister_shash(&ifxdeu_md5_hmac_alg
);