blob: 0af80577dc7bbfd9d80f276765e5afb7d218c067 [file] [log] [blame]
Michal Ludvig6c833272006-07-12 12:29:38 +10001/*
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
Herbert Xu7d024602009-07-10 17:26:44 +080015#include <crypto/internal/hash.h>
Jan Glauber5265eeb2007-10-09 22:43:13 +080016#include <crypto/sha.h>
Herbert Xu60104392006-08-26 18:34:10 +100017#include <linux/err.h>
Michal Ludvig6c833272006-07-12 12:29:38 +100018#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/errno.h>
Michal Ludvig6c833272006-07-12 12:29:38 +100021#include <linux/interrupt.h>
22#include <linux/kernel.h>
23#include <linux/scatterlist.h>
Suresh Siddhae4914012008-08-13 22:02:26 +100024#include <asm/i387.h>
Michal Ludvig6c833272006-07-12 12:29:38 +100025#include "padlock.h"
26
Herbert Xu4c6ab3e2009-09-21 23:21:53 -070027#ifdef CONFIG_64BIT
28#define STACK_ALIGN 16
29#else
30#define STACK_ALIGN 4
31#endif
32
Herbert Xubbbee462009-07-11 18:16:16 +080033struct padlock_sha_desc {
34 struct shash_desc fallback;
Michal Ludvig6c833272006-07-12 12:29:38 +100035};
36
Herbert Xubbbee462009-07-11 18:16:16 +080037struct padlock_sha_ctx {
38 struct crypto_shash *fallback;
39};
40
41static int padlock_sha_init(struct shash_desc *desc)
Michal Ludvig6c833272006-07-12 12:29:38 +100042{
Herbert Xubbbee462009-07-11 18:16:16 +080043 struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
44 struct padlock_sha_ctx *ctx = crypto_shash_ctx(desc->tfm);
45
46 dctx->fallback.tfm = ctx->fallback;
47 dctx->fallback.flags = desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
48 return crypto_shash_init(&dctx->fallback);
Michal Ludvig6c833272006-07-12 12:29:38 +100049}
50
Herbert Xubbbee462009-07-11 18:16:16 +080051static int padlock_sha_update(struct shash_desc *desc,
52 const u8 *data, unsigned int length)
Michal Ludvig6c833272006-07-12 12:29:38 +100053{
Herbert Xubbbee462009-07-11 18:16:16 +080054 struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
Herbert Xu7d024602009-07-10 17:26:44 +080055
Herbert Xubbbee462009-07-11 18:16:16 +080056 dctx->fallback.flags = desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
57 return crypto_shash_update(&dctx->fallback, data, length);
Michal Ludvig6c833272006-07-12 12:29:38 +100058}
59
60static inline void padlock_output_block(uint32_t *src,
61 uint32_t *dst, size_t count)
62{
63 while (count--)
64 *dst++ = swab32(*src++);
65}
66
Herbert Xubbbee462009-07-11 18:16:16 +080067static int padlock_sha1_finup(struct shash_desc *desc, const u8 *in,
68 unsigned int count, u8 *out)
Michal Ludvig6c833272006-07-12 12:29:38 +100069{
70 /* We can't store directly to *out as it may be unaligned. */
71 /* BTW Don't reduce the buffer size below 128 Bytes!
72 * PadLock microcode needs it that big. */
Herbert Xu4c6ab3e2009-09-21 23:21:53 -070073 char buf[128 + PADLOCK_ALIGNMENT - STACK_ALIGN] __attribute__
74 ((aligned(STACK_ALIGN)));
75 char *result = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
Herbert Xubbbee462009-07-11 18:16:16 +080076 struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
77 struct sha1_state state;
78 unsigned int space;
79 unsigned int leftover;
Suresh Siddhae4914012008-08-13 22:02:26 +100080 int ts_state;
Herbert Xubbbee462009-07-11 18:16:16 +080081 int err;
Michal Ludvig6c833272006-07-12 12:29:38 +100082
Herbert Xubbbee462009-07-11 18:16:16 +080083 dctx->fallback.flags = desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
84 err = crypto_shash_export(&dctx->fallback, &state);
85 if (err)
86 goto out;
87
88 if (state.count + count > ULONG_MAX)
89 return crypto_shash_finup(&dctx->fallback, in, count, out);
90
91 leftover = ((state.count - 1) & (SHA1_BLOCK_SIZE - 1)) + 1;
92 space = SHA1_BLOCK_SIZE - leftover;
93 if (space) {
94 if (count > space) {
95 err = crypto_shash_update(&dctx->fallback, in, space) ?:
96 crypto_shash_export(&dctx->fallback, &state);
97 if (err)
98 goto out;
99 count -= space;
100 in += space;
101 } else {
102 memcpy(state.buffer + leftover, in, count);
103 in = state.buffer;
104 count += leftover;
Herbert Xue9b25f12009-07-16 10:33:27 +0800105 state.count &= ~(SHA1_BLOCK_SIZE - 1);
Herbert Xubbbee462009-07-11 18:16:16 +0800106 }
107 }
108
109 memcpy(result, &state.state, SHA1_DIGEST_SIZE);
110
Suresh Siddhae4914012008-08-13 22:02:26 +1000111 /* prevent taking the spurious DNA fault with padlock. */
112 ts_state = irq_ts_save();
Michal Ludvig6c833272006-07-12 12:29:38 +1000113 asm volatile (".byte 0xf3,0x0f,0xa6,0xc8" /* rep xsha1 */
Herbert Xubbbee462009-07-11 18:16:16 +0800114 : \
Herbert Xufaae8902009-07-15 18:37:48 +0800115 : "c"((unsigned long)state.count + count), \
116 "a"((unsigned long)state.count), \
Herbert Xubbbee462009-07-11 18:16:16 +0800117 "S"(in), "D"(result));
Suresh Siddhae4914012008-08-13 22:02:26 +1000118 irq_ts_restore(ts_state);
Michal Ludvig6c833272006-07-12 12:29:38 +1000119
120 padlock_output_block((uint32_t *)result, (uint32_t *)out, 5);
Herbert Xubbbee462009-07-11 18:16:16 +0800121
122out:
123 return err;
Michal Ludvig6c833272006-07-12 12:29:38 +1000124}
125
Herbert Xubbbee462009-07-11 18:16:16 +0800126static int padlock_sha1_final(struct shash_desc *desc, u8 *out)
127{
128 u8 buf[4];
129
130 return padlock_sha1_finup(desc, buf, 0, out);
131}
132
133static int padlock_sha256_finup(struct shash_desc *desc, const u8 *in,
134 unsigned int count, u8 *out)
Michal Ludvig6c833272006-07-12 12:29:38 +1000135{
136 /* We can't store directly to *out as it may be unaligned. */
137 /* BTW Don't reduce the buffer size below 128 Bytes!
138 * PadLock microcode needs it that big. */
Herbert Xu4c6ab3e2009-09-21 23:21:53 -0700139 char buf[128 + PADLOCK_ALIGNMENT - STACK_ALIGN] __attribute__
140 ((aligned(STACK_ALIGN)));
141 char *result = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
Herbert Xubbbee462009-07-11 18:16:16 +0800142 struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
143 struct sha256_state state;
144 unsigned int space;
145 unsigned int leftover;
Suresh Siddhae4914012008-08-13 22:02:26 +1000146 int ts_state;
Herbert Xubbbee462009-07-11 18:16:16 +0800147 int err;
Michal Ludvig6c833272006-07-12 12:29:38 +1000148
Herbert Xubbbee462009-07-11 18:16:16 +0800149 dctx->fallback.flags = desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
150 err = crypto_shash_export(&dctx->fallback, &state);
151 if (err)
152 goto out;
153
154 if (state.count + count > ULONG_MAX)
155 return crypto_shash_finup(&dctx->fallback, in, count, out);
156
157 leftover = ((state.count - 1) & (SHA256_BLOCK_SIZE - 1)) + 1;
158 space = SHA256_BLOCK_SIZE - leftover;
159 if (space) {
160 if (count > space) {
161 err = crypto_shash_update(&dctx->fallback, in, space) ?:
162 crypto_shash_export(&dctx->fallback, &state);
163 if (err)
164 goto out;
165 count -= space;
166 in += space;
167 } else {
168 memcpy(state.buf + leftover, in, count);
169 in = state.buf;
170 count += leftover;
Herbert Xue9b25f12009-07-16 10:33:27 +0800171 state.count &= ~(SHA1_BLOCK_SIZE - 1);
Herbert Xubbbee462009-07-11 18:16:16 +0800172 }
173 }
174
175 memcpy(result, &state.state, SHA256_DIGEST_SIZE);
Michal Ludvig6c833272006-07-12 12:29:38 +1000176
Suresh Siddhae4914012008-08-13 22:02:26 +1000177 /* prevent taking the spurious DNA fault with padlock. */
178 ts_state = irq_ts_save();
Michal Ludvig6c833272006-07-12 12:29:38 +1000179 asm volatile (".byte 0xf3,0x0f,0xa6,0xd0" /* rep xsha256 */
Herbert Xubbbee462009-07-11 18:16:16 +0800180 : \
Herbert Xufaae8902009-07-15 18:37:48 +0800181 : "c"((unsigned long)state.count + count), \
182 "a"((unsigned long)state.count), \
Herbert Xubbbee462009-07-11 18:16:16 +0800183 "S"(in), "D"(result));
Suresh Siddhae4914012008-08-13 22:02:26 +1000184 irq_ts_restore(ts_state);
Michal Ludvig6c833272006-07-12 12:29:38 +1000185
186 padlock_output_block((uint32_t *)result, (uint32_t *)out, 8);
Herbert Xubbbee462009-07-11 18:16:16 +0800187
188out:
189 return err;
Michal Ludvig6c833272006-07-12 12:29:38 +1000190}
191
Herbert Xubbbee462009-07-11 18:16:16 +0800192static int padlock_sha256_final(struct shash_desc *desc, u8 *out)
Michal Ludvig6c833272006-07-12 12:29:38 +1000193{
Herbert Xubbbee462009-07-11 18:16:16 +0800194 u8 buf[4];
Herbert Xu7d024602009-07-10 17:26:44 +0800195
Herbert Xubbbee462009-07-11 18:16:16 +0800196 return padlock_sha256_finup(desc, buf, 0, out);
Michal Ludvig6c833272006-07-12 12:29:38 +1000197}
198
Herbert Xu60104392006-08-26 18:34:10 +1000199static int padlock_cra_init(struct crypto_tfm *tfm)
Michal Ludvig6c833272006-07-12 12:29:38 +1000200{
Herbert Xubbbee462009-07-11 18:16:16 +0800201 struct crypto_shash *hash = __crypto_shash_cast(tfm);
Herbert Xu60104392006-08-26 18:34:10 +1000202 const char *fallback_driver_name = tfm->__crt_alg->cra_name;
Herbert Xubbbee462009-07-11 18:16:16 +0800203 struct padlock_sha_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu7d024602009-07-10 17:26:44 +0800204 struct crypto_shash *fallback_tfm;
205 int err = -ENOMEM;
Herbert Xu60104392006-08-26 18:34:10 +1000206
Michal Ludvig6c833272006-07-12 12:29:38 +1000207 /* Allocate a fallback and abort if it failed. */
Herbert Xu7d024602009-07-10 17:26:44 +0800208 fallback_tfm = crypto_alloc_shash(fallback_driver_name, 0,
209 CRYPTO_ALG_NEED_FALLBACK);
Herbert Xu60104392006-08-26 18:34:10 +1000210 if (IS_ERR(fallback_tfm)) {
Michal Ludvig6c833272006-07-12 12:29:38 +1000211 printk(KERN_WARNING PFX "Fallback driver '%s' could not be loaded!\n",
212 fallback_driver_name);
Herbert Xu7d024602009-07-10 17:26:44 +0800213 err = PTR_ERR(fallback_tfm);
Herbert Xubbbee462009-07-11 18:16:16 +0800214 goto out;
Michal Ludvig6c833272006-07-12 12:29:38 +1000215 }
216
Herbert Xubbbee462009-07-11 18:16:16 +0800217 ctx->fallback = fallback_tfm;
218 hash->descsize += crypto_shash_descsize(fallback_tfm);
Michal Ludvig6c833272006-07-12 12:29:38 +1000219 return 0;
Herbert Xu7d024602009-07-10 17:26:44 +0800220
Herbert Xu7d024602009-07-10 17:26:44 +0800221out:
222 return err;
Michal Ludvig6c833272006-07-12 12:29:38 +1000223}
224
Michal Ludvig6c833272006-07-12 12:29:38 +1000225static void padlock_cra_exit(struct crypto_tfm *tfm)
226{
Herbert Xubbbee462009-07-11 18:16:16 +0800227 struct padlock_sha_ctx *ctx = crypto_tfm_ctx(tfm);
Michal Ludvig6c833272006-07-12 12:29:38 +1000228
Herbert Xubbbee462009-07-11 18:16:16 +0800229 crypto_free_shash(ctx->fallback);
Michal Ludvig6c833272006-07-12 12:29:38 +1000230}
231
Herbert Xubbbee462009-07-11 18:16:16 +0800232static struct shash_alg sha1_alg = {
233 .digestsize = SHA1_DIGEST_SIZE,
234 .init = padlock_sha_init,
235 .update = padlock_sha_update,
236 .finup = padlock_sha1_finup,
237 .final = padlock_sha1_final,
238 .descsize = sizeof(struct padlock_sha_desc),
239 .base = {
240 .cra_name = "sha1",
241 .cra_driver_name = "sha1-padlock",
242 .cra_priority = PADLOCK_CRA_PRIORITY,
243 .cra_flags = CRYPTO_ALG_TYPE_SHASH |
244 CRYPTO_ALG_NEED_FALLBACK,
245 .cra_blocksize = SHA1_BLOCK_SIZE,
246 .cra_ctxsize = sizeof(struct padlock_sha_ctx),
247 .cra_module = THIS_MODULE,
248 .cra_init = padlock_cra_init,
249 .cra_exit = padlock_cra_exit,
Michal Ludvig6c833272006-07-12 12:29:38 +1000250 }
251};
252
Herbert Xubbbee462009-07-11 18:16:16 +0800253static struct shash_alg sha256_alg = {
254 .digestsize = SHA256_DIGEST_SIZE,
255 .init = padlock_sha_init,
256 .update = padlock_sha_update,
257 .finup = padlock_sha256_finup,
258 .final = padlock_sha256_final,
259 .descsize = sizeof(struct padlock_sha_desc),
260 .base = {
261 .cra_name = "sha256",
262 .cra_driver_name = "sha256-padlock",
263 .cra_priority = PADLOCK_CRA_PRIORITY,
264 .cra_flags = CRYPTO_ALG_TYPE_SHASH |
265 CRYPTO_ALG_NEED_FALLBACK,
266 .cra_blocksize = SHA256_BLOCK_SIZE,
267 .cra_ctxsize = sizeof(struct padlock_sha_ctx),
268 .cra_module = THIS_MODULE,
269 .cra_init = padlock_cra_init,
270 .cra_exit = padlock_cra_exit,
Michal Ludvig6c833272006-07-12 12:29:38 +1000271 }
272};
273
Michal Ludvig6c833272006-07-12 12:29:38 +1000274static int __init padlock_init(void)
275{
276 int rc = -ENODEV;
277
278 if (!cpu_has_phe) {
Jeremy Katzb43e7262008-07-03 19:03:31 +0800279 printk(KERN_NOTICE PFX "VIA PadLock Hash Engine not detected.\n");
Michal Ludvig6c833272006-07-12 12:29:38 +1000280 return -ENODEV;
281 }
282
283 if (!cpu_has_phe_enabled) {
Jeremy Katzb43e7262008-07-03 19:03:31 +0800284 printk(KERN_NOTICE PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
Michal Ludvig6c833272006-07-12 12:29:38 +1000285 return -ENODEV;
286 }
287
Herbert Xubbbee462009-07-11 18:16:16 +0800288 rc = crypto_register_shash(&sha1_alg);
Michal Ludvig6c833272006-07-12 12:29:38 +1000289 if (rc)
290 goto out;
291
Herbert Xubbbee462009-07-11 18:16:16 +0800292 rc = crypto_register_shash(&sha256_alg);
Michal Ludvig6c833272006-07-12 12:29:38 +1000293 if (rc)
294 goto out_unreg1;
295
296 printk(KERN_NOTICE PFX "Using VIA PadLock ACE for SHA1/SHA256 algorithms.\n");
297
298 return 0;
299
300out_unreg1:
Herbert Xubbbee462009-07-11 18:16:16 +0800301 crypto_unregister_shash(&sha1_alg);
Michal Ludvig6c833272006-07-12 12:29:38 +1000302out:
303 printk(KERN_ERR PFX "VIA PadLock SHA1/SHA256 initialization failed.\n");
304 return rc;
305}
306
307static void __exit padlock_fini(void)
308{
Herbert Xubbbee462009-07-11 18:16:16 +0800309 crypto_unregister_shash(&sha1_alg);
310 crypto_unregister_shash(&sha256_alg);
Michal Ludvig6c833272006-07-12 12:29:38 +1000311}
312
313module_init(padlock_init);
314module_exit(padlock_fini);
315
316MODULE_DESCRIPTION("VIA PadLock SHA1/SHA256 algorithms support.");
317MODULE_LICENSE("GPL");
318MODULE_AUTHOR("Michal Ludvig");
319
Herbert Xua760a662009-02-26 14:06:31 +0800320MODULE_ALIAS("sha1-all");
321MODULE_ALIAS("sha256-all");
Michal Ludvig6c833272006-07-12 12:29:38 +1000322MODULE_ALIAS("sha1-padlock");
323MODULE_ALIAS("sha256-padlock");