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 u32 endian_swap(u32 input)
85 * \ingroup IFX_MD5_FUNCTIONS
86 * \brief perform dword level endian swap
87 * \param input value of dword that requires to be swapped
89 static u32
endian_swap(u32 input
)
91 u8
*ptr
= (u8
*)&input
;
93 return ((ptr
[3] << 24) | (ptr
[2] << 16) | (ptr
[1] << 8) | ptr
[0]);
96 /*! \fn static void md5_transform(u32 *hash, u32 const *in)
97 * \ingroup IFX_MD5_FUNCTIONS
98 * \brief main interface to md5 hardware
99 * \param hash current hash value
100 * \param in 64-byte block of input
102 static void md5_transform(struct md5_ctx
*mctx
, u32
*hash
, u32
const *in
)
105 volatile struct deu_hash_t
*hashs
= (struct deu_hash_t
*) HASH_START
;
108 CRTCL_SECT_HASH_START
;
111 hashs
->D1R
= endian_swap(*((u32
*) hash
+ 0));
112 hashs
->D2R
= endian_swap(*((u32
*) hash
+ 1));
113 hashs
->D3R
= endian_swap(*((u32
*) hash
+ 2));
114 hashs
->D4R
= endian_swap(*((u32
*) hash
+ 3));
117 for (i
= 0; i
< 16; i
++) {
118 hashs
->MR
= endian_swap(in
[i
]);
119 // printk("in[%d]: %08x\n", i, endian_swap(in[i]));
122 //wait for processing
123 while (hashs
->controlr
.BSY
) {
124 // this will not take long
127 *((u32
*) hash
+ 0) = endian_swap (hashs
->D1R
);
128 *((u32
*) hash
+ 1) = endian_swap (hashs
->D2R
);
129 *((u32
*) hash
+ 2) = endian_swap (hashs
->D3R
);
130 *((u32
*) hash
+ 3) = endian_swap (hashs
->D4R
);
137 /*! \fn static inline void md5_transform_helper(struct md5_ctx *ctx)
138 * \ingroup IFX_MD5_FUNCTIONS
139 * \brief interfacing function for md5_transform()
140 * \param ctx crypto context
142 static inline void md5_transform_helper(struct md5_ctx
*ctx
)
144 //le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
145 md5_transform(ctx
, ctx
->hash
, ctx
->block
);
148 /*! \fn static void md5_init(struct crypto_tfm *tfm)
149 * \ingroup IFX_MD5_FUNCTIONS
150 * \brief initialize md5 hardware
151 * \param tfm linux crypto algo transform
153 static int md5_init(struct shash_desc
*desc
)
155 struct md5_ctx
*mctx
= shash_desc_ctx(desc
);
156 volatile struct deu_hash_t
*hash
= (struct deu_hash_t
*) HASH_START
;
158 hash
->controlr
.ENDI
= 0;
159 hash
->controlr
.SM
= 1;
160 hash
->controlr
.ALGO
= 1; // 1 = md5 0 = sha1
161 hash
->controlr
.INIT
= 1; // Initialize the hash operation by writing a '1' to the INIT bit.
163 mctx
->byte_count
= 0;
168 /*! \fn static void md5_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
169 * \ingroup IFX_MD5_FUNCTIONS
170 * \brief on-the-fly md5 computation
171 * \param tfm linux crypto algo transform
172 * \param data input data
173 * \param len size of input data
175 static int md5_update(struct shash_desc
*desc
, const u8
*data
, unsigned int len
)
177 struct md5_ctx
*mctx
= shash_desc_ctx(desc
);
178 const u32 avail
= sizeof(mctx
->block
) - (mctx
->byte_count
& 0x3f);
180 mctx
->byte_count
+= len
;
183 memcpy((char *)mctx
->block
+ (sizeof(mctx
->block
) - avail
),
188 memcpy((char *)mctx
->block
+ (sizeof(mctx
->block
) - avail
),
191 md5_transform_helper(mctx
);
195 while (len
>= sizeof(mctx
->block
)) {
196 memcpy(mctx
->block
, data
, sizeof(mctx
->block
));
197 md5_transform_helper(mctx
);
198 data
+= sizeof(mctx
->block
);
199 len
-= sizeof(mctx
->block
);
202 memcpy(mctx
->block
, data
, len
);
206 /*! \fn static void md5_final(struct crypto_tfm *tfm, u8 *out)
207 * \ingroup IFX_MD5_FUNCTIONS
208 * \brief compute final md5 value
209 * \param tfm linux crypto algo transform
210 * \param out final md5 output value
212 static int md5_final(struct shash_desc
*desc
, u8
*out
)
214 struct md5_ctx
*mctx
= shash_desc_ctx(desc
);
215 const unsigned int offset
= mctx
->byte_count
& 0x3f;
216 char *p
= (char *)mctx
->block
+ offset
;
217 int padding
= 56 - (offset
+ 1);
218 volatile struct deu_hash_t
*hashs
= (struct deu_hash_t
*) HASH_START
;
223 memset(p
, 0x00, padding
+ sizeof (u64
));
224 md5_transform_helper(mctx
);
225 p
= (char *)mctx
->block
;
229 memset(p
, 0, padding
);
230 mctx
->block
[14] = endian_swap(mctx
->byte_count
<< 3);
231 mctx
->block
[15] = endian_swap(mctx
->byte_count
>> 29);
234 le32_to_cpu_array(mctx
->block
, (sizeof(mctx
->block
) -
235 sizeof(u64
)) / sizeof(u32
));
238 md5_transform(mctx
, mctx
->hash
, mctx
->block
);
240 CRTCL_SECT_HASH_START
;
242 *((u32
*) out
+ 0) = endian_swap (hashs
->D1R
);
243 *((u32
*) out
+ 1) = endian_swap (hashs
->D2R
);
244 *((u32
*) out
+ 2) = endian_swap (hashs
->D3R
);
245 *((u32
*) out
+ 3) = endian_swap (hashs
->D4R
);
250 memset(mctx
, 0, sizeof(*mctx
));
256 * \brief MD5 function mappings
258 static struct shash_alg ifxdeu_md5_alg
= {
259 .digestsize
= MD5_DIGEST_SIZE
,
261 .update
= md5_update
,
263 .descsize
= sizeof(struct md5_ctx
),
266 .cra_driver_name
= "ifxdeu-md5",
268 .cra_flags
= CRYPTO_ALG_TYPE_HASH
| CRYPTO_ALG_KERN_DRIVER_ONLY
,
269 .cra_blocksize
= MD5_HMAC_BLOCK_SIZE
,
270 .cra_module
= THIS_MODULE
,
274 /*! \fn int ifxdeu_init_md5 (void)
275 * \ingroup IFX_MD5_FUNCTIONS
276 * \brief initialize md5 driver
278 int ifxdeu_init_md5 (void)
283 if ((ret
= crypto_register_shash(&ifxdeu_md5_alg
)))
286 printk (KERN_NOTICE
"IFX DEU MD5 initialized%s.\n", disable_deudma
? "" : " (DMA)");
290 printk(KERN_ERR
"IFX DEU MD5 initialization failed!\n");
294 /*! \fn void ifxdeu_fini_md5 (void)
295 * \ingroup IFX_MD5_FUNCTIONS
296 * \brief unregister md5 driver
299 void ifxdeu_fini_md5 (void)
301 crypto_unregister_shash(&ifxdeu_md5_alg
);