crypto: xcbc - Fix incorrect error value when creating instance
[deliverable/linux.git] / drivers / crypto / padlock-sha.c
CommitLineData
6c833275
ML
1/*
2 * Cryptographic API.
3 *
4 * Support for VIA PadLock hardware crypto engine.
5 *
6 * Copyright (c) 2006 Michal Ludvig <michal@logix.cz>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 */
14
7d024608 15#include <crypto/internal/hash.h>
5265eeb2 16#include <crypto/sha.h>
6010439f 17#include <linux/err.h>
6c833275
ML
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/errno.h>
6c833275
ML
21#include <linux/interrupt.h>
22#include <linux/kernel.h>
23#include <linux/scatterlist.h>
e4914012 24#include <asm/i387.h>
6c833275
ML
25#include "padlock.h"
26
bbbee467
HX
27struct padlock_sha_desc {
28 struct shash_desc fallback;
6c833275
ML
29};
30
bbbee467
HX
31struct padlock_sha_ctx {
32 struct crypto_shash *fallback;
33};
6c833275 34
bbbee467 35static int padlock_sha_init(struct shash_desc *desc)
6c833275 36{
bbbee467
HX
37 struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
38 struct padlock_sha_ctx *ctx = crypto_shash_ctx(desc->tfm);
6c833275 39
bbbee467
HX
40 dctx->fallback.tfm = ctx->fallback;
41 dctx->fallback.flags = desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
42 return crypto_shash_init(&dctx->fallback);
6c833275
ML
43}
44
bbbee467
HX
45static int padlock_sha_update(struct shash_desc *desc,
46 const u8 *data, unsigned int length)
6c833275 47{
bbbee467 48 struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
6c833275 49
bbbee467
HX
50 dctx->fallback.flags = desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
51 return crypto_shash_update(&dctx->fallback, data, length);
6c833275
ML
52}
53
54static inline void padlock_output_block(uint32_t *src,
55 uint32_t *dst, size_t count)
56{
57 while (count--)
58 *dst++ = swab32(*src++);
59}
60
bbbee467
HX
61static int padlock_sha1_finup(struct shash_desc *desc, const u8 *in,
62 unsigned int count, u8 *out)
6c833275
ML
63{
64 /* We can't store directly to *out as it may be unaligned. */
65 /* BTW Don't reduce the buffer size below 128 Bytes!
66 * PadLock microcode needs it that big. */
bbbee467
HX
67 char result[128] __attribute__ ((aligned(PADLOCK_ALIGNMENT)));
68 struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
69 struct sha1_state state;
70 unsigned int space;
71 unsigned int leftover;
e4914012 72 int ts_state;
bbbee467
HX
73 int err;
74
75 dctx->fallback.flags = desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
76 err = crypto_shash_export(&dctx->fallback, &state);
77 if (err)
78 goto out;
79
80 if (state.count + count > ULONG_MAX)
81 return crypto_shash_finup(&dctx->fallback, in, count, out);
82
83 leftover = ((state.count - 1) & (SHA1_BLOCK_SIZE - 1)) + 1;
84 space = SHA1_BLOCK_SIZE - leftover;
85 if (space) {
86 if (count > space) {
87 err = crypto_shash_update(&dctx->fallback, in, space) ?:
88 crypto_shash_export(&dctx->fallback, &state);
89 if (err)
90 goto out;
91 count -= space;
92 in += space;
93 } else {
94 memcpy(state.buffer + leftover, in, count);
95 in = state.buffer;
96 count += leftover;
97 }
98 }
99
100 memcpy(result, &state.state, SHA1_DIGEST_SIZE);
6c833275 101
e4914012
SS
102 /* prevent taking the spurious DNA fault with padlock. */
103 ts_state = irq_ts_save();
6c833275 104 asm volatile (".byte 0xf3,0x0f,0xa6,0xc8" /* rep xsha1 */
bbbee467
HX
105 : \
106 : "c"(state.count + count), "a"(state.count), \
107 "S"(in), "D"(result));
e4914012 108 irq_ts_restore(ts_state);
6c833275
ML
109
110 padlock_output_block((uint32_t *)result, (uint32_t *)out, 5);
bbbee467
HX
111
112out:
113 return err;
6c833275
ML
114}
115
bbbee467
HX
116static int padlock_sha1_final(struct shash_desc *desc, u8 *out)
117{
118 u8 buf[4];
119
120 return padlock_sha1_finup(desc, buf, 0, out);
121}
122
123static int padlock_sha256_finup(struct shash_desc *desc, const u8 *in,
124 unsigned int count, u8 *out)
6c833275
ML
125{
126 /* We can't store directly to *out as it may be unaligned. */
127 /* BTW Don't reduce the buffer size below 128 Bytes!
128 * PadLock microcode needs it that big. */
bbbee467
HX
129 char result[128] __attribute__ ((aligned(PADLOCK_ALIGNMENT)));
130 struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
131 struct sha256_state state;
132 unsigned int space;
133 unsigned int leftover;
e4914012 134 int ts_state;
bbbee467 135 int err;
6c833275 136
bbbee467
HX
137 dctx->fallback.flags = desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
138 err = crypto_shash_export(&dctx->fallback, &state);
139 if (err)
140 goto out;
141
142 if (state.count + count > ULONG_MAX)
143 return crypto_shash_finup(&dctx->fallback, in, count, out);
144
145 leftover = ((state.count - 1) & (SHA256_BLOCK_SIZE - 1)) + 1;
146 space = SHA256_BLOCK_SIZE - leftover;
147 if (space) {
148 if (count > space) {
149 err = crypto_shash_update(&dctx->fallback, in, space) ?:
150 crypto_shash_export(&dctx->fallback, &state);
151 if (err)
152 goto out;
153 count -= space;
154 in += space;
155 } else {
156 memcpy(state.buf + leftover, in, count);
157 in = state.buf;
158 count += leftover;
159 }
160 }
161
162 memcpy(result, &state.state, SHA256_DIGEST_SIZE);
6c833275 163
e4914012
SS
164 /* prevent taking the spurious DNA fault with padlock. */
165 ts_state = irq_ts_save();
6c833275 166 asm volatile (".byte 0xf3,0x0f,0xa6,0xd0" /* rep xsha256 */
bbbee467
HX
167 : \
168 : "c"(state.count + count), "a"(state.count), \
169 "S"(in), "D"(result));
e4914012 170 irq_ts_restore(ts_state);
6c833275
ML
171
172 padlock_output_block((uint32_t *)result, (uint32_t *)out, 8);
bbbee467
HX
173
174out:
175 return err;
6c833275
ML
176}
177
bbbee467 178static int padlock_sha256_final(struct shash_desc *desc, u8 *out)
6c833275 179{
bbbee467 180 u8 buf[4];
6c833275 181
bbbee467 182 return padlock_sha256_finup(desc, buf, 0, out);
6c833275
ML
183}
184
6010439f 185static int padlock_cra_init(struct crypto_tfm *tfm)
6c833275 186{
bbbee467 187 struct crypto_shash *hash = __crypto_shash_cast(tfm);
6010439f 188 const char *fallback_driver_name = tfm->__crt_alg->cra_name;
bbbee467 189 struct padlock_sha_ctx *ctx = crypto_tfm_ctx(tfm);
7d024608
HX
190 struct crypto_shash *fallback_tfm;
191 int err = -ENOMEM;
6010439f 192
6c833275 193 /* Allocate a fallback and abort if it failed. */
7d024608
HX
194 fallback_tfm = crypto_alloc_shash(fallback_driver_name, 0,
195 CRYPTO_ALG_NEED_FALLBACK);
6010439f 196 if (IS_ERR(fallback_tfm)) {
6c833275
ML
197 printk(KERN_WARNING PFX "Fallback driver '%s' could not be loaded!\n",
198 fallback_driver_name);
7d024608 199 err = PTR_ERR(fallback_tfm);
bbbee467 200 goto out;
6c833275
ML
201 }
202
bbbee467
HX
203 ctx->fallback = fallback_tfm;
204 hash->descsize += crypto_shash_descsize(fallback_tfm);
6c833275 205 return 0;
7d024608 206
7d024608
HX
207out:
208 return err;
6c833275
ML
209}
210
6c833275
ML
211static void padlock_cra_exit(struct crypto_tfm *tfm)
212{
bbbee467 213 struct padlock_sha_ctx *ctx = crypto_tfm_ctx(tfm);
7d024608 214
bbbee467 215 crypto_free_shash(ctx->fallback);
6c833275
ML
216}
217
bbbee467
HX
218static struct shash_alg sha1_alg = {
219 .digestsize = SHA1_DIGEST_SIZE,
220 .init = padlock_sha_init,
221 .update = padlock_sha_update,
222 .finup = padlock_sha1_finup,
223 .final = padlock_sha1_final,
224 .descsize = sizeof(struct padlock_sha_desc),
225 .base = {
226 .cra_name = "sha1",
227 .cra_driver_name = "sha1-padlock",
228 .cra_priority = PADLOCK_CRA_PRIORITY,
229 .cra_flags = CRYPTO_ALG_TYPE_SHASH |
230 CRYPTO_ALG_NEED_FALLBACK,
231 .cra_blocksize = SHA1_BLOCK_SIZE,
232 .cra_ctxsize = sizeof(struct padlock_sha_ctx),
233 .cra_module = THIS_MODULE,
234 .cra_init = padlock_cra_init,
235 .cra_exit = padlock_cra_exit,
6c833275
ML
236 }
237};
238
bbbee467
HX
239static struct shash_alg sha256_alg = {
240 .digestsize = SHA256_DIGEST_SIZE,
241 .init = padlock_sha_init,
242 .update = padlock_sha_update,
243 .finup = padlock_sha256_finup,
244 .final = padlock_sha256_final,
245 .descsize = sizeof(struct padlock_sha_desc),
246 .base = {
247 .cra_name = "sha256",
248 .cra_driver_name = "sha256-padlock",
249 .cra_priority = PADLOCK_CRA_PRIORITY,
250 .cra_flags = CRYPTO_ALG_TYPE_SHASH |
251 CRYPTO_ALG_NEED_FALLBACK,
252 .cra_blocksize = SHA256_BLOCK_SIZE,
253 .cra_ctxsize = sizeof(struct padlock_sha_ctx),
254 .cra_module = THIS_MODULE,
255 .cra_init = padlock_cra_init,
256 .cra_exit = padlock_cra_exit,
6c833275
ML
257 }
258};
259
6c833275
ML
260static int __init padlock_init(void)
261{
262 int rc = -ENODEV;
263
264 if (!cpu_has_phe) {
b43e726b 265 printk(KERN_NOTICE PFX "VIA PadLock Hash Engine not detected.\n");
6c833275
ML
266 return -ENODEV;
267 }
268
269 if (!cpu_has_phe_enabled) {
b43e726b 270 printk(KERN_NOTICE PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
6c833275
ML
271 return -ENODEV;
272 }
273
bbbee467 274 rc = crypto_register_shash(&sha1_alg);
6c833275
ML
275 if (rc)
276 goto out;
277
bbbee467 278 rc = crypto_register_shash(&sha256_alg);
6c833275
ML
279 if (rc)
280 goto out_unreg1;
281
282 printk(KERN_NOTICE PFX "Using VIA PadLock ACE for SHA1/SHA256 algorithms.\n");
283
284 return 0;
285
286out_unreg1:
bbbee467 287 crypto_unregister_shash(&sha1_alg);
6c833275
ML
288out:
289 printk(KERN_ERR PFX "VIA PadLock SHA1/SHA256 initialization failed.\n");
290 return rc;
291}
292
293static void __exit padlock_fini(void)
294{
bbbee467
HX
295 crypto_unregister_shash(&sha1_alg);
296 crypto_unregister_shash(&sha256_alg);
6c833275
ML
297}
298
299module_init(padlock_init);
300module_exit(padlock_fini);
301
302MODULE_DESCRIPTION("VIA PadLock SHA1/SHA256 algorithms support.");
303MODULE_LICENSE("GPL");
304MODULE_AUTHOR("Michal Ludvig");
305
a760a665
HX
306MODULE_ALIAS("sha1-all");
307MODULE_ALIAS("sha256-all");
6c833275
ML
308MODULE_ALIAS("sha1-padlock");
309MODULE_ALIAS("sha256-padlock");
This page took 0.303009 seconds and 5 git commands to generate.