2 * arch/ubicom32/crypto/md5_ubicom32.c
3 * Ubicom32 implementation of the MD5 Secure Hash Algorithm
5 * (C) Copyright 2009, Ubicom, Inc.
7 * This file is part of the Ubicom32 Linux Kernel Port.
9 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
10 * it and/or modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation, either version 2 of the
12 * License, or (at your option) any later version.
14 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
15 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
16 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with the Ubicom32 Linux Kernel Port. If not,
21 * see <http://www.gnu.org/licenses/>.
23 * Ubicom32 implementation derived from (with many thanks):
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/crypto.h>
32 #include "crypto_ubicom32.h"
34 #define MD5_DIGEST_SIZE 16
35 #define MD5_BLOCK_SIZE 64
36 #define MD5_HASH_WORDS 4
38 extern void _md5_ip5k_init_digest(u32_t
*digest
);
39 extern void _md5_ip5k_transform(u32_t
*data_input
);
40 extern void _md5_ip5k_get_digest(u32_t
*digest
);
42 struct ubicom32_md5_ctx
{
43 u64 count
; /* message length */
44 u32 state
[MD5_HASH_WORDS
];
45 u8 buf
[2 * MD5_BLOCK_SIZE
];
48 static void md5_init(struct crypto_tfm
*tfm
)
50 struct ubicom32_md5_ctx
*mctx
= crypto_tfm_ctx(tfm
);
51 mctx
->state
[0] = 0x01234567;
52 mctx
->state
[1] = 0x89abcdef;
53 mctx
->state
[2] = 0xfedcba98;
54 mctx
->state
[3] = 0x76543210;
59 static inline void _md5_process(u32
*digest
, const u8
*data
)
61 _md5_ip5k_transform((u32
*)data
);
64 static void md5_update(struct crypto_tfm
*tfm
, const u8
*data
,
67 struct ubicom32_md5_ctx
*mctx
= crypto_tfm_ctx(tfm
);
70 /* how much is already in the buffer? */
71 index
= mctx
->count
& 0x3f;
75 if (index
+ len
< MD5_BLOCK_SIZE
) {
82 /* init digest set ctrl register too */
83 _md5_ip5k_init_digest(mctx
->state
);
85 if (unlikely(index
== 0 && SEC_ALIGNED(data
))) {
87 while (len
>= MD5_BLOCK_SIZE
) {
88 _md5_process(mctx
->state
, data
);
89 data
+= MD5_BLOCK_SIZE
;
90 len
-= MD5_BLOCK_SIZE
;
95 /* process one stored block */
97 clen
= MD5_BLOCK_SIZE
- index
;
98 memcpy(mctx
->buf
+ index
, data
, clen
);
99 _md5_process(mctx
->state
, mctx
->buf
);
105 if (likely(SEC_ALIGNED(data
))) {
109 /* process as many blocks as possible */
110 while (len
>= MD5_BLOCK_SIZE
) {
111 memcpy(mctx
->buf
, data
, MD5_BLOCK_SIZE
);
112 _md5_process(mctx
->state
, mctx
->buf
);
113 data
+= MD5_BLOCK_SIZE
;
114 len
-= MD5_BLOCK_SIZE
;
118 _md5_ip5k_get_digest(mctx
->state
);
124 memcpy(mctx
->buf
+ index
, data
, len
);
127 /* Add padding and return the message digest. */
128 static void md5_final(struct crypto_tfm
*tfm
, u8
*out
)
130 struct ubicom32_md5_ctx
*mctx
= crypto_tfm_ctx(tfm
);
132 unsigned int index
, end
;
134 /* must perform manual padding */
135 index
= mctx
->count
& 0x3f;
136 end
= (index
< 56) ? MD5_BLOCK_SIZE
: (2 * MD5_BLOCK_SIZE
);
138 /* start pad with 1 */
139 mctx
->buf
[index
] = 0x80;
143 memset(mctx
->buf
+ index
, 0x00, end
- index
- 8);
145 /* append message length */
146 bits
[0] = mctx
->count
<< 3;
147 bits
[1] = mctx
->count
>> 29;
148 __cpu_to_le32s(bits
);
149 __cpu_to_le32s(bits
+ 1);
151 memcpy(mctx
->buf
+ end
- 8, &bits
, sizeof(bits
));
153 /* force to use the mctx->buf and ignore the partial buf */
154 mctx
->count
= mctx
->count
& ~0x3f;
155 md5_update(tfm
, mctx
->buf
, end
);
157 /* copy digest to out */
158 memcpy(out
, mctx
->state
, MD5_DIGEST_SIZE
);
161 memset(mctx
, 0, sizeof *mctx
);
164 static struct crypto_alg alg
= {
166 .cra_driver_name
= "md5-ubicom32",
167 .cra_priority
= CRYPTO_UBICOM32_PRIORITY
,
168 .cra_flags
= CRYPTO_ALG_TYPE_DIGEST
,
169 .cra_blocksize
= MD5_BLOCK_SIZE
,
170 .cra_ctxsize
= sizeof(struct ubicom32_md5_ctx
),
171 .cra_module
= THIS_MODULE
,
172 .cra_list
= LIST_HEAD_INIT(alg
.cra_list
),
175 .dia_digestsize
= MD5_DIGEST_SIZE
,
176 .dia_init
= md5_init
,
177 .dia_update
= md5_update
,
178 .dia_final
= md5_final
,
183 static int __init
init(void)
186 return crypto_register_alg(&alg
);
189 static void __exit
fini(void)
191 crypto_unregister_alg(&alg
);
199 MODULE_LICENSE("GPL");
200 MODULE_DESCRIPTION("MD5 Secure Hash Algorithm");