sparc64: Add SHA1 driver making use of the 'sha1' instruction.
[linux-2.6-block.git] / arch / sparc / crypto / sha1_glue.c
1 /* Glue code for SHA1 hashing optimized for sparc64 crypto opcodes.
2  *
3  * This is based largely upon arch/x86/crypto/sha1_ssse3_glue.c
4  *
5  * Copyright (c) Alan Smithee.
6  * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
7  * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
8  * Copyright (c) Mathias Krause <minipli@googlemail.com>
9  */
10
11 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
12
13 #include <crypto/internal/hash.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/mm.h>
17 #include <linux/cryptohash.h>
18 #include <linux/types.h>
19 #include <crypto/sha.h>
20
21 #include <asm/pstate.h>
22 #include <asm/elf.h>
23
24 asmlinkage void sha1_sparc64_transform(u32 *digest, const char *data,
25                                        unsigned int rounds);
26
27 static int sha1_sparc64_init(struct shash_desc *desc)
28 {
29         struct sha1_state *sctx = shash_desc_ctx(desc);
30
31         *sctx = (struct sha1_state){
32                 .state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
33         };
34
35         return 0;
36 }
37
38 static void __sha1_sparc64_update(struct sha1_state *sctx, const u8 *data,
39                                   unsigned int len, unsigned int partial)
40 {
41         unsigned int done = 0;
42
43         sctx->count += len;
44         if (partial) {
45                 done = SHA1_BLOCK_SIZE - partial;
46                 memcpy(sctx->buffer + partial, data, done);
47                 sha1_sparc64_transform(sctx->state, sctx->buffer, 1);
48         }
49         if (len - done >= SHA1_BLOCK_SIZE) {
50                 const unsigned int rounds = (len - done) / SHA1_BLOCK_SIZE;
51
52                 sha1_sparc64_transform(sctx->state, data + done, rounds);
53                 done += rounds * SHA1_BLOCK_SIZE;
54         }
55
56         memcpy(sctx->buffer, data + done, len - done);
57 }
58
59 static int sha1_sparc64_update(struct shash_desc *desc, const u8 *data,
60                                unsigned int len)
61 {
62         struct sha1_state *sctx = shash_desc_ctx(desc);
63         unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
64
65         /* Handle the fast case right here */
66         if (partial + len < SHA1_BLOCK_SIZE) {
67                 sctx->count += len;
68                 memcpy(sctx->buffer + partial, data, len);
69         } else
70                 __sha1_sparc64_update(sctx, data, len, partial);
71
72         return 0;
73 }
74
75 /* Add padding and return the message digest. */
76 static int sha1_sparc64_final(struct shash_desc *desc, u8 *out)
77 {
78         struct sha1_state *sctx = shash_desc_ctx(desc);
79         unsigned int i, index, padlen;
80         __be32 *dst = (__be32 *)out;
81         __be64 bits;
82         static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, };
83
84         bits = cpu_to_be64(sctx->count << 3);
85
86         /* Pad out to 56 mod 64 and append length */
87         index = sctx->count % SHA1_BLOCK_SIZE;
88         padlen = (index < 56) ? (56 - index) : ((SHA1_BLOCK_SIZE+56) - index);
89
90         /* We need to fill a whole block for __sha1_sparc64_update() */
91         if (padlen <= 56) {
92                 sctx->count += padlen;
93                 memcpy(sctx->buffer + index, padding, padlen);
94         } else {
95                 __sha1_sparc64_update(sctx, padding, padlen, index);
96         }
97         __sha1_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
98
99         /* Store state in digest */
100         for (i = 0; i < 5; i++)
101                 dst[i] = cpu_to_be32(sctx->state[i]);
102
103         /* Wipe context */
104         memset(sctx, 0, sizeof(*sctx));
105
106         return 0;
107 }
108
109 static int sha1_sparc64_export(struct shash_desc *desc, void *out)
110 {
111         struct sha1_state *sctx = shash_desc_ctx(desc);
112
113         memcpy(out, sctx, sizeof(*sctx));
114
115         return 0;
116 }
117
118 static int sha1_sparc64_import(struct shash_desc *desc, const void *in)
119 {
120         struct sha1_state *sctx = shash_desc_ctx(desc);
121
122         memcpy(sctx, in, sizeof(*sctx));
123
124         return 0;
125 }
126
127 static struct shash_alg alg = {
128         .digestsize     =       SHA1_DIGEST_SIZE,
129         .init           =       sha1_sparc64_init,
130         .update         =       sha1_sparc64_update,
131         .final          =       sha1_sparc64_final,
132         .export         =       sha1_sparc64_export,
133         .import         =       sha1_sparc64_import,
134         .descsize       =       sizeof(struct sha1_state),
135         .statesize      =       sizeof(struct sha1_state),
136         .base           =       {
137                 .cra_name       =       "sha1",
138                 .cra_driver_name=       "sha1-sparc64",
139                 .cra_priority   =       150,
140                 .cra_flags      =       CRYPTO_ALG_TYPE_SHASH,
141                 .cra_blocksize  =       SHA1_BLOCK_SIZE,
142                 .cra_module     =       THIS_MODULE,
143         }
144 };
145
146 static bool __init sparc64_has_sha1_opcode(void)
147 {
148         unsigned long cfr;
149
150         if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
151                 return false;
152
153         __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
154         if (!(cfr & CFR_SHA1))
155                 return false;
156
157         return true;
158 }
159
160 static int __init sha1_sparc64_mod_init(void)
161 {
162         if (sparc64_has_sha1_opcode()) {
163                 pr_info("Using sparc64 sha1 opcode optimized SHA-1 implementation\n");
164                 return crypto_register_shash(&alg);
165         }
166         pr_info("sparc64 sha1 opcode not available.\n");
167         return -ENODEV;
168 }
169
170 static void __exit sha1_sparc64_mod_fini(void)
171 {
172         crypto_unregister_shash(&alg);
173 }
174
175 module_init(sha1_sparc64_mod_init);
176 module_exit(sha1_sparc64_mod_fini);
177
178 MODULE_LICENSE("GPL");
179 MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, sparc64 sha1 opcode accelerated");
180
181 MODULE_ALIAS("sha1");