aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid S. Miller <davem@davemloft.net>2012-08-20 00:51:26 -0400
committerDavid S. Miller <davem@davemloft.net>2012-08-20 18:08:50 -0400
commitfa4dfedcc23a589f953750de54eebf986336fa70 (patch)
treeccc5e857beeed7624bd8929bb9e51a1c37b54853
parent775e0c69987a76d08900edbca857425f952c60fb (diff)
sparc64: Add MD5 driver making use of the 'md5' instruction.
Signed-off-by: David S. Miller <davem@davemloft.net> Acked-by: Herbert Xu <herbert@gondor.apana.org.au>
-rw-r--r--arch/sparc/crypto/Makefile2
-rw-r--r--arch/sparc/crypto/md5_asm.S70
-rw-r--r--arch/sparc/crypto/md5_glue.c186
-rw-r--r--crypto/Kconfig9
4 files changed, 267 insertions, 0 deletions
diff --git a/arch/sparc/crypto/Makefile b/arch/sparc/crypto/Makefile
index cfae0e874d26..535669828d47 100644
--- a/arch/sparc/crypto/Makefile
+++ b/arch/sparc/crypto/Makefile
@@ -5,7 +5,9 @@
5obj-$(CONFIG_CRYPTO_SHA1_SPARC64) += sha1-sparc64.o 5obj-$(CONFIG_CRYPTO_SHA1_SPARC64) += sha1-sparc64.o
6obj-$(CONFIG_CRYPTO_SHA256_SPARC64) += sha256-sparc64.o 6obj-$(CONFIG_CRYPTO_SHA256_SPARC64) += sha256-sparc64.o
7obj-$(CONFIG_CRYPTO_SHA512_SPARC64) += sha512-sparc64.o 7obj-$(CONFIG_CRYPTO_SHA512_SPARC64) += sha512-sparc64.o
8obj-$(CONFIG_CRYPTO_MD5_SPARC64) += md5-sparc64.o
8 9
9sha1-sparc64-y := sha1_asm.o sha1_glue.o 10sha1-sparc64-y := sha1_asm.o sha1_glue.o
10sha256-sparc64-y := sha256_asm.o sha256_glue.o 11sha256-sparc64-y := sha256_asm.o sha256_glue.o
11sha512-sparc64-y := sha512_asm.o sha512_glue.o 12sha512-sparc64-y := sha512_asm.o sha512_glue.o
13md5-sparc64-y := md5_asm.o md5_glue.o
diff --git a/arch/sparc/crypto/md5_asm.S b/arch/sparc/crypto/md5_asm.S
new file mode 100644
index 000000000000..220b73baa551
--- /dev/null
+++ b/arch/sparc/crypto/md5_asm.S
@@ -0,0 +1,70 @@
1#include <linux/linkage.h>
2#include <asm/visasm.h>
3
4ENTRY(md5_sparc64_transform)
5 /* %o0 = digest, %o1 = data, %o2 = rounds */
6 VISEntryHalf
7 ld [%o0 + 0x00], %f0
8 ld [%o0 + 0x04], %f1
9 andcc %o1, 0x7, %g0
10 ld [%o0 + 0x08], %f2
11 bne,pn %xcc, 10f
12 ld [%o0 + 0x0c], %f3
13
141:
15 ldd [%o1 + 0x00], %f8
16 ldd [%o1 + 0x08], %f10
17 ldd [%o1 + 0x10], %f12
18 ldd [%o1 + 0x18], %f14
19 ldd [%o1 + 0x20], %f16
20 ldd [%o1 + 0x28], %f18
21 ldd [%o1 + 0x30], %f20
22 ldd [%o1 + 0x38], %f22
23
24 /* md5 */
25 .word 0x81b02800
26
27 subcc %o2, 1, %o2
28 bne,pt %xcc, 1b
29 add %o1, 0x40, %o1
30
315:
32 st %f0, [%o0 + 0x00]
33 st %f1, [%o0 + 0x04]
34 st %f2, [%o0 + 0x08]
35 st %f3, [%o0 + 0x0c]
36 retl
37 VISExitHalf
3810:
39 alignaddr %o1, %g0, %o1
40
41 ldd [%o1 + 0x00], %f10
421:
43 ldd [%o1 + 0x08], %f12
44 ldd [%o1 + 0x10], %f14
45 ldd [%o1 + 0x18], %f16
46 ldd [%o1 + 0x20], %f18
47 ldd [%o1 + 0x28], %f20
48 ldd [%o1 + 0x30], %f22
49 ldd [%o1 + 0x38], %f24
50 ldd [%o1 + 0x40], %f26
51
52 faligndata %f10, %f12, %f8
53 faligndata %f12, %f14, %f10
54 faligndata %f14, %f16, %f12
55 faligndata %f16, %f18, %f14
56 faligndata %f18, %f20, %f16
57 faligndata %f20, %f22, %f18
58 faligndata %f22, %f24, %f20
59 faligndata %f24, %f26, %f22
60
61 /* md5 */
62 .word 0x81b02800
63
64 subcc %o2, 1, %o2
65 fsrc1 %f26, %f10
66 bne,pt %xcc, 1b
67 add %o1, 0x40, %o1
68
69 ba,a,pt %xcc, 5b
70ENDPROC(md5_sparc64_transform)
diff --git a/arch/sparc/crypto/md5_glue.c b/arch/sparc/crypto/md5_glue.c
new file mode 100644
index 000000000000..bdfcfefbd4fd
--- /dev/null
+++ b/arch/sparc/crypto/md5_glue.c
@@ -0,0 +1,186 @@
1/* Glue code for MD5 hashing optimized for sparc64 crypto opcodes.
2 *
3 * This is based largely upon arch/x86/crypto/sha1_ssse3_glue.c
4 * and crypto/md5.c which are:
5 *
6 * Copyright (c) Alan Smithee.
7 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
8 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
9 * Copyright (c) Mathias Krause <minipli@googlemail.com>
10 * Copyright (c) Cryptoapi developers.
11 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
12 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <crypto/internal/hash.h>
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/mm.h>
20#include <linux/cryptohash.h>
21#include <linux/types.h>
22#include <crypto/md5.h>
23
24#include <asm/pstate.h>
25#include <asm/elf.h>
26
27asmlinkage void md5_sparc64_transform(u32 *digest, const char *data,
28 unsigned int rounds);
29
30static int md5_sparc64_init(struct shash_desc *desc)
31{
32 struct md5_state *mctx = shash_desc_ctx(desc);
33
34 mctx->hash[0] = cpu_to_le32(0x67452301);
35 mctx->hash[1] = cpu_to_le32(0xefcdab89);
36 mctx->hash[2] = cpu_to_le32(0x98badcfe);
37 mctx->hash[3] = cpu_to_le32(0x10325476);
38 mctx->byte_count = 0;
39
40 return 0;
41}
42
43static void __md5_sparc64_update(struct md5_state *sctx, const u8 *data,
44 unsigned int len, unsigned int partial)
45{
46 unsigned int done = 0;
47
48 sctx->byte_count += len;
49 if (partial) {
50 done = MD5_HMAC_BLOCK_SIZE - partial;
51 memcpy((u8 *)sctx->block + partial, data, done);
52 md5_sparc64_transform(sctx->hash, (u8 *)sctx->block, 1);
53 }
54 if (len - done >= MD5_HMAC_BLOCK_SIZE) {
55 const unsigned int rounds = (len - done) / MD5_HMAC_BLOCK_SIZE;
56
57 md5_sparc64_transform(sctx->hash, data + done, rounds);
58 done += rounds * MD5_HMAC_BLOCK_SIZE;
59 }
60
61 memcpy(sctx->block, data + done, len - done);
62}
63
64static int md5_sparc64_update(struct shash_desc *desc, const u8 *data,
65 unsigned int len)
66{
67 struct md5_state *sctx = shash_desc_ctx(desc);
68 unsigned int partial = sctx->byte_count % MD5_HMAC_BLOCK_SIZE;
69
70 /* Handle the fast case right here */
71 if (partial + len < MD5_HMAC_BLOCK_SIZE) {
72 sctx->byte_count += len;
73 memcpy((u8 *)sctx->block + partial, data, len);
74 } else
75 __md5_sparc64_update(sctx, data, len, partial);
76
77 return 0;
78}
79
80/* Add padding and return the message digest. */
81static int md5_sparc64_final(struct shash_desc *desc, u8 *out)
82{
83 struct md5_state *sctx = shash_desc_ctx(desc);
84 unsigned int i, index, padlen;
85 u32 *dst = (u32 *)out;
86 __le64 bits;
87 static const u8 padding[MD5_HMAC_BLOCK_SIZE] = { 0x80, };
88
89 bits = cpu_to_le64(sctx->byte_count << 3);
90
91 /* Pad out to 56 mod 64 and append length */
92 index = sctx->byte_count % MD5_HMAC_BLOCK_SIZE;
93 padlen = (index < 56) ? (56 - index) : ((MD5_HMAC_BLOCK_SIZE+56) - index);
94
95 /* We need to fill a whole block for __md5_sparc64_update() */
96 if (padlen <= 56) {
97 sctx->byte_count += padlen;
98 memcpy((u8 *)sctx->block + index, padding, padlen);
99 } else {
100 __md5_sparc64_update(sctx, padding, padlen, index);
101 }
102 __md5_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
103
104 /* Store state in digest */
105 for (i = 0; i < MD5_HASH_WORDS; i++)
106 dst[i] = sctx->hash[i];
107
108 /* Wipe context */
109 memset(sctx, 0, sizeof(*sctx));
110
111 return 0;
112}
113
114static int md5_sparc64_export(struct shash_desc *desc, void *out)
115{
116 struct md5_state *sctx = shash_desc_ctx(desc);
117
118 memcpy(out, sctx, sizeof(*sctx));
119
120 return 0;
121}
122
123static int md5_sparc64_import(struct shash_desc *desc, const void *in)
124{
125 struct md5_state *sctx = shash_desc_ctx(desc);
126
127 memcpy(sctx, in, sizeof(*sctx));
128
129 return 0;
130}
131
132static struct shash_alg alg = {
133 .digestsize = MD5_DIGEST_SIZE,
134 .init = md5_sparc64_init,
135 .update = md5_sparc64_update,
136 .final = md5_sparc64_final,
137 .export = md5_sparc64_export,
138 .import = md5_sparc64_import,
139 .descsize = sizeof(struct md5_state),
140 .statesize = sizeof(struct md5_state),
141 .base = {
142 .cra_name = "md5",
143 .cra_driver_name= "md5-sparc64",
144 .cra_priority = 150,
145 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
146 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
147 .cra_module = THIS_MODULE,
148 }
149};
150
151static bool __init sparc64_has_md5_opcode(void)
152{
153 unsigned long cfr;
154
155 if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
156 return false;
157
158 __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
159 if (!(cfr & CFR_MD5))
160 return false;
161
162 return true;
163}
164
165static int __init md5_sparc64_mod_init(void)
166{
167 if (sparc64_has_md5_opcode()) {
168 pr_info("Using sparc64 md5 opcode optimized MD5 implementation\n");
169 return crypto_register_shash(&alg);
170 }
171 pr_info("sparc64 md5 opcode not available.\n");
172 return -ENODEV;
173}
174
175static void __exit md5_sparc64_mod_fini(void)
176{
177 crypto_unregister_shash(&alg);
178}
179
180module_init(md5_sparc64_mod_init);
181module_exit(md5_sparc64_mod_fini);
182
183MODULE_LICENSE("GPL");
184MODULE_DESCRIPTION("MD5 Secure Hash Algorithm, sparc64 md5 opcode accelerated");
185
186MODULE_ALIAS("md5");
diff --git a/crypto/Kconfig b/crypto/Kconfig
index e7ed12abc875..4cb1ab04168f 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -354,6 +354,15 @@ config CRYPTO_MD5
354 help 354 help
355 MD5 message digest algorithm (RFC1321). 355 MD5 message digest algorithm (RFC1321).
356 356
357config CRYPTO_MD5_SPARC64
358 tristate "MD5 digest algorithm (SPARC64)"
359 depends on SPARC64
360 select CRYPTO_MD5
361 select CRYPTO_HASH
362 help
363 MD5 message digest algorithm (RFC1321) implemented
364 using sparc64 crypto instructions, when available.
365
357config CRYPTO_MICHAEL_MIC 366config CRYPTO_MICHAEL_MIC
358 tristate "Michael MIC keyed digest algorithm" 367 tristate "Michael MIC keyed digest algorithm"
359 select CRYPTO_HASH 368 select CRYPTO_HASH