4 * MD5 Message Digest Algorithm (RFC1321).
6 * Adapted for OCTEON by Aaro Koskinen <aaro.koskinen@iki.fi>.
8 * Based on crypto/md5.c, which is:
10 * Derived from cryptoapi implementation, originally based on the
11 * public domain implementation written by Colin Plumb in 1993.
13 * Copyright (c) Cryptoapi developers.
14 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at your option)
22 #include <crypto/md5.h>
23 #include <linux/init.h>
24 #include <linux/types.h>
25 #include <linux/module.h>
26 #include <linux/string.h>
27 #include <asm/byteorder.h>
28 #include <linux/cryptohash.h>
29 #include <asm/octeon/octeon.h>
30 #include <crypto/internal/hash.h>
32 #include "octeon-crypto.h"
35 * We pass everything as 64-bit. OCTEON can handle misaligned data.
38 static void octeon_md5_store_hash(struct md5_state
*ctx
)
40 u64
*hash
= (u64
*)ctx
->hash
;
42 write_octeon_64bit_hash_dword(hash
[0], 0);
43 write_octeon_64bit_hash_dword(hash
[1], 1);
46 static void octeon_md5_read_hash(struct md5_state
*ctx
)
48 u64
*hash
= (u64
*)ctx
->hash
;
50 hash
[0] = read_octeon_64bit_hash_dword(0);
51 hash
[1] = read_octeon_64bit_hash_dword(1);
54 static void octeon_md5_transform(const void *_block
)
56 const u64
*block
= _block
;
58 write_octeon_64bit_block_dword(block
[0], 0);
59 write_octeon_64bit_block_dword(block
[1], 1);
60 write_octeon_64bit_block_dword(block
[2], 2);
61 write_octeon_64bit_block_dword(block
[3], 3);
62 write_octeon_64bit_block_dword(block
[4], 4);
63 write_octeon_64bit_block_dword(block
[5], 5);
64 write_octeon_64bit_block_dword(block
[6], 6);
65 octeon_md5_start(block
[7]);
68 static int octeon_md5_init(struct shash_desc
*desc
)
70 struct md5_state
*mctx
= shash_desc_ctx(desc
);
72 mctx
->hash
[0] = cpu_to_le32(0x67452301);
73 mctx
->hash
[1] = cpu_to_le32(0xefcdab89);
74 mctx
->hash
[2] = cpu_to_le32(0x98badcfe);
75 mctx
->hash
[3] = cpu_to_le32(0x10325476);
81 static int octeon_md5_update(struct shash_desc
*desc
, const u8
*data
,
84 struct md5_state
*mctx
= shash_desc_ctx(desc
);
85 const u32 avail
= sizeof(mctx
->block
) - (mctx
->byte_count
& 0x3f);
86 struct octeon_cop2_state state
;
89 mctx
->byte_count
+= len
;
92 memcpy((char *)mctx
->block
+ (sizeof(mctx
->block
) - avail
),
97 memcpy((char *)mctx
->block
+ (sizeof(mctx
->block
) - avail
), data
,
101 flags
= octeon_crypto_enable(&state
);
102 octeon_md5_store_hash(mctx
);
104 octeon_md5_transform(mctx
->block
);
108 while (len
>= sizeof(mctx
->block
)) {
109 octeon_md5_transform(data
);
110 data
+= sizeof(mctx
->block
);
111 len
-= sizeof(mctx
->block
);
114 octeon_md5_read_hash(mctx
);
115 octeon_crypto_disable(&state
, flags
);
118 memcpy(mctx
->block
, data
, len
);
123 static int octeon_md5_final(struct shash_desc
*desc
, u8
*out
)
125 struct md5_state
*mctx
= shash_desc_ctx(desc
);
126 const unsigned int offset
= mctx
->byte_count
& 0x3f;
127 char *p
= (char *)mctx
->block
+ offset
;
128 int padding
= 56 - (offset
+ 1);
129 struct octeon_cop2_state state
;
135 flags
= octeon_crypto_enable(&state
);
136 octeon_md5_store_hash(mctx
);
139 memset(p
, 0x00, padding
+ sizeof(u64
));
140 octeon_md5_transform(mctx
->block
);
141 p
= (char *)mctx
->block
;
145 memset(p
, 0, padding
);
146 mctx
->block
[14] = cpu_to_le32(mctx
->byte_count
<< 3);
147 mctx
->block
[15] = cpu_to_le32(mctx
->byte_count
>> 29);
148 octeon_md5_transform(mctx
->block
);
150 octeon_md5_read_hash(mctx
);
151 octeon_crypto_disable(&state
, flags
);
154 memcpy(out
, mctx
->hash
, sizeof(mctx
->hash
));
155 memset(mctx
, 0, sizeof(*mctx
));
160 static int octeon_md5_export(struct shash_desc
*desc
, void *out
)
162 struct md5_state
*ctx
= shash_desc_ctx(desc
);
164 memcpy(out
, ctx
, sizeof(*ctx
));
168 static int octeon_md5_import(struct shash_desc
*desc
, const void *in
)
170 struct md5_state
*ctx
= shash_desc_ctx(desc
);
172 memcpy(ctx
, in
, sizeof(*ctx
));
176 static struct shash_alg alg
= {
177 .digestsize
= MD5_DIGEST_SIZE
,
178 .init
= octeon_md5_init
,
179 .update
= octeon_md5_update
,
180 .final
= octeon_md5_final
,
181 .export
= octeon_md5_export
,
182 .import
= octeon_md5_import
,
183 .descsize
= sizeof(struct md5_state
),
184 .statesize
= sizeof(struct md5_state
),
187 .cra_driver_name
= "octeon-md5",
188 .cra_priority
= OCTEON_CR_OPCODE_PRIORITY
,
189 .cra_flags
= CRYPTO_ALG_TYPE_SHASH
,
190 .cra_blocksize
= MD5_HMAC_BLOCK_SIZE
,
191 .cra_module
= THIS_MODULE
,
195 static int __init
md5_mod_init(void)
197 if (!octeon_has_crypto())
199 return crypto_register_shash(&alg
);
202 static void __exit
md5_mod_fini(void)
204 crypto_unregister_shash(&alg
);
207 module_init(md5_mod_init
);
208 module_exit(md5_mod_fini
);
210 MODULE_LICENSE("GPL");
211 MODULE_DESCRIPTION("MD5 Message Digest Algorithm (OCTEON)");
212 MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");