1 /******************************************************************************
3 ** FILE NAME : ifxmips_md5.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 *******************************************************************************/
24 \defgroup IFX_DEU IFX_DEU_DRIVERS
26 \brief ifx deu driver module
32 \brief MD5 encryption deu driver file
36 \defgroup IFX_MD5_FUNCTIONS IFX_MD5_FUNCTIONS
38 \brief ifx deu MD5 functions
41 /*Project header files */
42 #include <linux/init.h>
43 #include <linux/module.h>
44 #include <linux/string.h>
45 #include <linux/crypto.h>
46 #include <linux/types.h>
47 #include <crypto/internal/hash.h>
48 #include <asm/byteorder.h>
51 #if defined(CONFIG_DANUBE)
52 #include "ifxmips_deu_danube.h"
53 #elif 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 MD5_DIGEST_SIZE 16
62 #define MD5_HMAC_BLOCK_SIZE 64
63 #define MD5_BLOCK_WORDS 16
64 #define MD5_HASH_WORDS 4
65 #define HASH_START IFX_HASH_CON
67 //#define CRYPTO_DEBUG
69 extern char debug_level
;
70 #define DPRINTF(level, format, args...) if (level < debug_level) printk(KERN_INFO "[%s %s %d]: " format, __FILE__, __func__, __LINE__, ##args);
72 #define DPRINTF(level, format, args...)
77 u32 hash
[MD5_HASH_WORDS
];
78 u32 block
[MD5_BLOCK_WORDS
];
82 extern int disable_deudma
;
84 /*! \fn static void md5_transform(u32 *hash, u32 const *in)
85 * \ingroup IFX_MD5_FUNCTIONS
86 * \brief main interface to md5 hardware
87 * \param hash current hash value
88 * \param in 64-byte block of input
90 static void md5_transform(struct md5_ctx
*mctx
, u32
*hash
, u32
const *in
)
93 volatile struct deu_hash_t
*hashs
= (struct deu_hash_t
*) HASH_START
;
96 CRTCL_SECT_HASH_START
;
101 hashs
->D1R
= *((u32
*) hash
+ 0);
102 hashs
->D2R
= *((u32
*) hash
+ 1);
103 hashs
->D3R
= *((u32
*) hash
+ 2);
104 hashs
->D4R
= *((u32
*) hash
+ 3);
107 for (i
= 0; i
< 16; i
++) {
109 // printk("in[%d]: %08x\n", i, in[i]);
112 //wait for processing
113 while (hashs
->controlr
.BSY
) {
114 // this will not take long
117 *((u32
*) hash
+ 0) = hashs
->D1R
;
118 *((u32
*) hash
+ 1) = hashs
->D2R
;
119 *((u32
*) hash
+ 2) = hashs
->D3R
;
120 *((u32
*) hash
+ 3) = hashs
->D4R
;
127 /*! \fn static inline void md5_transform_helper(struct md5_ctx *ctx)
128 * \ingroup IFX_MD5_FUNCTIONS
129 * \brief interfacing function for md5_transform()
130 * \param ctx crypto context
132 static inline void md5_transform_helper(struct md5_ctx
*ctx
)
134 //le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
135 md5_transform(ctx
, ctx
->hash
, ctx
->block
);
138 /*! \fn static void md5_init(struct crypto_tfm *tfm)
139 * \ingroup IFX_MD5_FUNCTIONS
140 * \brief initialize md5 hardware
141 * \param tfm linux crypto algo transform
143 static int md5_init(struct shash_desc
*desc
)
145 struct md5_ctx
*mctx
= shash_desc_ctx(desc
);
146 volatile struct deu_hash_t
*hash
= (struct deu_hash_t
*) HASH_START
;
148 mctx
->byte_count
= 0;
153 /*! \fn static void md5_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
154 * \ingroup IFX_MD5_FUNCTIONS
155 * \brief on-the-fly md5 computation
156 * \param tfm linux crypto algo transform
157 * \param data input data
158 * \param len size of input data
160 static int md5_update(struct shash_desc
*desc
, const u8
*data
, unsigned int len
)
162 struct md5_ctx
*mctx
= shash_desc_ctx(desc
);
163 const u32 avail
= sizeof(mctx
->block
) - (mctx
->byte_count
& 0x3f);
165 mctx
->byte_count
+= len
;
168 memcpy((char *)mctx
->block
+ (sizeof(mctx
->block
) - avail
),
173 memcpy((char *)mctx
->block
+ (sizeof(mctx
->block
) - avail
),
176 md5_transform_helper(mctx
);
180 while (len
>= sizeof(mctx
->block
)) {
181 memcpy(mctx
->block
, data
, sizeof(mctx
->block
));
182 md5_transform_helper(mctx
);
183 data
+= sizeof(mctx
->block
);
184 len
-= sizeof(mctx
->block
);
187 memcpy(mctx
->block
, data
, len
);
191 /*! \fn static void md5_final(struct crypto_tfm *tfm, u8 *out)
192 * \ingroup IFX_MD5_FUNCTIONS
193 * \brief compute final md5 value
194 * \param tfm linux crypto algo transform
195 * \param out final md5 output value
197 static int md5_final(struct shash_desc
*desc
, u8
*out
)
199 struct md5_ctx
*mctx
= shash_desc_ctx(desc
);
200 const unsigned int offset
= mctx
->byte_count
& 0x3f;
201 char *p
= (char *)mctx
->block
+ offset
;
202 int padding
= 56 - (offset
+ 1);
203 volatile struct deu_hash_t
*hashs
= (struct deu_hash_t
*) HASH_START
;
208 memset(p
, 0x00, padding
+ sizeof (u64
));
209 md5_transform_helper(mctx
);
210 p
= (char *)mctx
->block
;
214 memset(p
, 0, padding
);
215 mctx
->block
[14] = le32_to_cpu(mctx
->byte_count
<< 3);
216 mctx
->block
[15] = le32_to_cpu(mctx
->byte_count
>> 29);
218 md5_transform(mctx
, mctx
->hash
, mctx
->block
);
220 memcpy(out
, mctx
->hash
, MD5_DIGEST_SIZE
);
223 memset(mctx
, 0, sizeof(*mctx
));
229 * \brief MD5 function mappings
231 static struct shash_alg ifxdeu_md5_alg
= {
232 .digestsize
= MD5_DIGEST_SIZE
,
234 .update
= md5_update
,
236 .descsize
= sizeof(struct md5_ctx
),
239 .cra_driver_name
= "ifxdeu-md5",
241 .cra_flags
= CRYPTO_ALG_TYPE_HASH
| CRYPTO_ALG_KERN_DRIVER_ONLY
,
242 .cra_blocksize
= MD5_HMAC_BLOCK_SIZE
,
243 .cra_module
= THIS_MODULE
,
247 /*! \fn int ifxdeu_init_md5 (void)
248 * \ingroup IFX_MD5_FUNCTIONS
249 * \brief initialize md5 driver
251 int ifxdeu_init_md5 (void)
256 if ((ret
= crypto_register_shash(&ifxdeu_md5_alg
)))
259 printk (KERN_NOTICE
"IFX DEU MD5 initialized%s.\n", disable_deudma
? "" : " (DMA)");
263 printk(KERN_ERR
"IFX DEU MD5 initialization failed!\n");
267 /*! \fn void ifxdeu_fini_md5 (void)
268 * \ingroup IFX_MD5_FUNCTIONS
269 * \brief unregister md5 driver
272 void ifxdeu_fini_md5 (void)
274 crypto_unregister_shash(&ifxdeu_md5_alg
);