2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16 * Copyright (C) 2010 Ralph Hempel <ralph.hempel@lantiq.com>
17 * Copyright (C) 2009 Mohammad Firdaus
21 \defgroup IFX_DEU IFX_DEU_DRIVERS
23 \brief ifx deu driver module
27 \file ifxmips_md5_hmac.c
29 \brief MD5-HMAC encryption deu driver file
33 \defgroup IFX_MD5_HMAC_FUNCTIONS IFX_MD5_HMAC_FUNCTIONS
35 \brief ifx md5-hmac driver functions
38 /* Project Header files */
39 #include <linux/init.h>
40 #include <linux/module.h>
41 #include <linux/string.h>
42 #include <linux/crypto.h>
43 #include <linux/types.h>
44 #include <asm/byteorder.h>
45 #include "ifxmips_deu.h"
47 #define MD5_DIGEST_SIZE 16
48 #define MD5_HMAC_BLOCK_SIZE 64
49 #define MD5_BLOCK_WORDS 16
50 #define MD5_HASH_WORDS 4
51 #define MD5_HMAC_DBN_TEMP_SIZE 1024 // size in dword, needed for dbn workaround
52 #define HASH_START IFX_HASH_CON
54 static spinlock_t lock
;
55 #define CRTCL_SECT_INIT spin_lock_init(&lock)
56 #define CRTCL_SECT_START spin_lock_irqsave(&lock, flag)
57 #define CRTCL_SECT_END spin_unlock_irqrestore(&lock, flag)
60 u32 hash
[MD5_HASH_WORDS
];
61 u32 block
[MD5_BLOCK_WORDS
];
64 u32 temp
[MD5_HMAC_DBN_TEMP_SIZE
];
67 extern int disable_deudma
;
69 /*! \fn static u32 endian_swap(u32 input)
70 * \ingroup IFX_MD5_HMAC_FUNCTIONS
71 * \brief perform dword level endian swap
72 * \param input value of dword that requires to be swapped
74 static u32
endian_swap(u32 input
)
76 u8
*ptr
= (u8
*)&input
;
78 return ((ptr
[3] << 24) | (ptr
[2] << 16) | (ptr
[1] << 8) | ptr
[0]);
81 /*! \fn static void md5_hmac_transform(struct crypto_tfm *tfm, u32 const *in)
82 * \ingroup IFX_MD5_HMAC_FUNCTIONS
83 * \brief save input block to context
84 * \param tfm linux crypto algo transform
85 * \param in 64-byte block of input
87 static void md5_hmac_transform(struct crypto_tfm
*tfm
, u32
const *in
)
89 struct md5_hmac_ctx
*mctx
= crypto_tfm_ctx(tfm
);
91 memcpy(&mctx
->temp
[mctx
->dbn
<<4], in
, 64); //dbn workaround
94 if ( (mctx
->dbn
<<4) > MD5_HMAC_DBN_TEMP_SIZE
)
96 printk("MD5_HMAC_DBN_TEMP_SIZE exceeded\n");
101 /*! \fn int md5_hmac_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
102 * \ingroup IFX_MD5_HMAC_FUNCTIONS
103 * \brief sets md5 hmac key
104 * \param tfm linux crypto algo transform
105 * \param key input key
106 * \param keylen key length greater than 64 bytes IS NOT SUPPORTED
108 int md5_hmac_setkey(struct crypto_tfm
*tfm
, const u8
*key
, unsigned int keylen
)
110 volatile struct deu_hash_t
*hash
= (struct deu_hash_t
*) HASH_START
;
112 u32
*in_key
= (u32
*)key
;
114 hash
->KIDX
= 0x80000000; // reset all 16 words of the key to '0'
118 for (i
= 0; i
< keylen
; i
+=4)
122 hash
->KEY
= *((u32
*) in_key
+ j
);
129 /*! \fn void md5_hmac_init(struct crypto_tfm *tfm)
130 * \ingroup IFX_MD5_HMAC_FUNCTIONS
131 * \brief initialize md5 hmac context
132 * \param tfm linux crypto algo transform
134 void md5_hmac_init(struct crypto_tfm
*tfm
)
136 struct md5_hmac_ctx
*mctx
= crypto_tfm_ctx(tfm
);
138 memset(mctx
, 0, sizeof(struct md5_hmac_ctx
));
139 mctx
->dbn
= 0; //dbn workaround
142 /*! \fn void md5_hmac_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
143 * \ingroup IFX_MD5_HMAC_FUNCTIONS
144 * \brief on-the-fly md5 hmac computation
145 * \param tfm linux crypto algo transform
146 * \param data input data
147 * \param len size of input data
149 void md5_hmac_update(struct crypto_tfm
*tfm
, const u8
*data
, unsigned int len
)
151 struct md5_hmac_ctx
*mctx
= crypto_tfm_ctx(tfm
);
152 const u32 avail
= sizeof(mctx
->block
) - (mctx
->byte_count
& 0x3f);
154 mctx
->byte_count
+= len
;
157 memcpy((char *)mctx
->block
+ (sizeof(mctx
->block
) - avail
),
162 memcpy((char *)mctx
->block
+ (sizeof(mctx
->block
) - avail
),
165 md5_hmac_transform(tfm
, mctx
->block
);
169 while (len
>= sizeof(mctx
->block
)) {
170 memcpy(mctx
->block
, data
, sizeof(mctx
->block
));
171 md5_hmac_transform(tfm
, mctx
->block
);
172 data
+= sizeof(mctx
->block
);
173 len
-= sizeof(mctx
->block
);
176 memcpy(mctx
->block
, data
, len
);
180 /*! \fn void md5_hmac_final(struct crypto_tfm *tfm, u8 *out)
181 * \ingroup IFX_MD5_HMAC_FUNCTIONS
182 * \brief compute final md5 hmac value
183 * \param tfm linux crypto algo transform
184 * \param out final md5 hmac output value
186 void md5_hmac_final(struct crypto_tfm
*tfm
, u8
*out
)
188 struct md5_hmac_ctx
*mctx
= crypto_tfm_ctx(tfm
);
189 const unsigned int offset
= mctx
->byte_count
& 0x3f;
190 char *p
= (char *)mctx
->block
+ offset
;
191 int padding
= 56 - (offset
+ 1);
192 volatile struct deu_hash_t
*hashs
= (struct deu_hash_t
*) HASH_START
;
196 u32
*in
= &mctx
->temp
[0];
200 memset(p
, 0x00, padding
+ sizeof (u64
));
201 md5_hmac_transform(tfm
, mctx
->block
);
202 p
= (char *)mctx
->block
;
206 memset(p
, 0, padding
);
207 mctx
->block
[14] = endian_swap((mctx
->byte_count
+ 64) << 3); // need to add 512 bit of the IPAD operation
208 mctx
->block
[15] = 0x00000000;
210 md5_hmac_transform(tfm
, mctx
->block
);
214 printk("dbn = %d\n", mctx
->dbn
);
215 hashs
->DBN
= mctx
->dbn
;
217 *IFX_HASH_CON
= 0x0703002D; //khs, go, init, ndc, endi, kyue, hmen, md5
219 //wait for processing
220 while (hashs
->controlr
.BSY
) {
221 // this will not take long
224 for (dbn
= 0; dbn
< mctx
->dbn
; dbn
++)
226 for (i
= 0; i
< 16; i
++) {
230 hashs
->controlr
.GO
= 1;
233 //wait for processing
234 while (hashs
->controlr
.BSY
) {
235 // this will not take long
243 //wait for digest ready
244 while (! hashs
->controlr
.DGRY
) {
245 // this will not take long
249 *((u32
*) out
+ 0) = hashs
->D1R
;
250 *((u32
*) out
+ 1) = hashs
->D2R
;
251 *((u32
*) out
+ 2) = hashs
->D3R
;
252 *((u32
*) out
+ 3) = hashs
->D4R
;
253 *((u32
*) out
+ 4) = hashs
->D5R
;
259 * \brief MD5_HMAC function mappings
262 static struct crypto_alg ifxdeu_md5_hmac_alg
= {
263 .cra_name
= "hmac(md5)",
264 .cra_driver_name
= "ifxdeu-md5_hmac",
265 .cra_flags
= CRYPTO_ALG_TYPE_DIGEST
,
266 .cra_blocksize
= MD5_HMAC_BLOCK_SIZE
,
267 .cra_ctxsize
= sizeof(struct md5_hmac_ctx
),
268 .cra_module
= THIS_MODULE
,
269 .cra_list
= LIST_HEAD_INIT(ifxdeu_md5_hmac_alg
.cra_list
),
270 .cra_u
= { .digest
= {
271 .dia_digestsize
= MD5_DIGEST_SIZE
,
272 .dia_setkey
= md5_hmac_setkey
,
273 .dia_init
= md5_hmac_init
,
274 .dia_update
= md5_hmac_update
,
275 .dia_final
= md5_hmac_final
} }
278 /*! \fn int __init ifxdeu_init_md5_hmac (void)
279 * \ingroup IFX_MD5_HMAC_FUNCTIONS
280 * \brief initialize md5 hmac driver
282 int __init
ifxdeu_init_md5_hmac (void)
286 if ((ret
= crypto_register_alg(&ifxdeu_md5_hmac_alg
)))
291 printk (KERN_NOTICE
"IFX DEU MD5_HMAC initialized%s.\n", disable_deudma
? "" : " (DMA)");
295 printk(KERN_ERR
"IFX DEU MD5_HMAC initialization failed!\n");
299 /** \fn void __exit ifxdeu_fini_md5_hmac (void)
300 * \ingroup IFX_MD5_HMAC_FUNCTIONS
301 * \brief unregister md5 hmac driver
303 void __exit
ifxdeu_fini_md5_hmac (void)
305 crypto_unregister_alg (&ifxdeu_md5_hmac_alg
);