blob: a936ba49b390f84603c022a7895f14c9ccd1ab02 [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 Xubbbee462009-07-11 18:16:16 +080027struct padlock_sha_desc {
28 struct shash_desc fallback;
Michal Ludvig6c833272006-07-12 12:29:38 +100029};
30
Herbert Xubbbee462009-07-11 18:16:16 +080031struct padlock_sha_ctx {
32 struct crypto_shash *fallback;
33};
34
35static int padlock_sha_init(struct shash_desc *desc)
Michal Ludvig6c833272006-07-12 12:29:38 +100036{
Herbert Xubbbee462009-07-11 18:16:16 +080037 struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
38 struct padlock_sha_ctx *ctx = crypto_shash_ctx(desc->tfm);
39
40 dctx->fallback.tfm = ctx->fallback;
41 dctx->fallback.flags = desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
42 return crypto_shash_init(&dctx->fallback);
Michal Ludvig6c833272006-07-12 12:29:38 +100043}
44
Herbert Xubbbee462009-07-11 18:16:16 +080045static int padlock_sha_update(struct shash_desc *desc,
46 const u8 *data, unsigned int length)
Michal Ludvig6c833272006-07-12 12:29:38 +100047{
Herbert Xubbbee462009-07-11 18:16:16 +080048 struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
Herbert Xu7d024602009-07-10 17:26:44 +080049
Herbert Xubbbee462009-07-11 18:16:16 +080050 dctx->fallback.flags = desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
51 return crypto_shash_update(&dctx->fallback, data, length);
Michal Ludvig6c833272006-07-12 12:29:38 +100052}
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
Herbert Xubbbee462009-07-11 18:16:16 +080061static int padlock_sha1_finup(struct shash_desc *desc, const u8 *in,
62 unsigned int count, u8 *out)
Michal Ludvig6c833272006-07-12 12:29:38 +100063{
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. */
Herbert Xubbbee462009-07-11 18:16:16 +080067 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;
Suresh Siddhae4914012008-08-13 22:02:26 +100072 int ts_state;
Herbert Xubbbee462009-07-11 18:16:16 +080073 int err;
Michal Ludvig6c833272006-07-12 12:29:38 +100074
Herbert Xubbbee462009-07-11 18:16:16 +080075 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);
101
Suresh Siddhae4914012008-08-13 22:02:26 +1000102 /* prevent taking the spurious DNA fault with padlock. */
103 ts_state = irq_ts_save();
Michal Ludvig6c833272006-07-12 12:29:38 +1000104 asm volatile (".byte 0xf3,0x0f,0xa6,0xc8" /* rep xsha1 */
Herbert Xubbbee462009-07-11 18:16:16 +0800105 : \
Herbert Xufaae8902009-07-15 18:37:48 +0800106 : "c"((unsigned long)state.count + count), \
107 "a"((unsigned long)state.count), \
Herbert Xubbbee462009-07-11 18:16:16 +0800108 "S"(in), "D"(result));
Suresh Siddhae4914012008-08-13 22:02:26 +1000109 irq_ts_restore(ts_state);
Michal Ludvig6c833272006-07-12 12:29:38 +1000110
111 padlock_output_block((uint32_t *)result, (uint32_t *)out, 5);
Herbert Xubbbee462009-07-11 18:16:16 +0800112
113out:
114 return err;
Michal Ludvig6c833272006-07-12 12:29:38 +1000115}
116
Herbert Xubbbee462009-07-11 18:16:16 +0800117static int padlock_sha1_final(struct shash_desc *desc, u8 *out)
118{
119 u8 buf[4];
120
121 return padlock_sha1_finup(desc, buf, 0, out);
122}
123
124static int padlock_sha256_finup(struct shash_desc *desc, const u8 *in,
125 unsigned int count, u8 *out)
Michal Ludvig6c833272006-07-12 12:29:38 +1000126{
127 /* We can't store directly to *out as it may be unaligned. */
128 /* BTW Don't reduce the buffer size below 128 Bytes!
129 * PadLock microcode needs it that big. */
Herbert Xubbbee462009-07-11 18:16:16 +0800130 char result[128] __attribute__ ((aligned(PADLOCK_ALIGNMENT)));
131 struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
132 struct sha256_state state;
133 unsigned int space;
134 unsigned int leftover;
Suresh Siddhae4914012008-08-13 22:02:26 +1000135 int ts_state;
Herbert Xubbbee462009-07-11 18:16:16 +0800136 int err;
Michal Ludvig6c833272006-07-12 12:29:38 +1000137
Herbert Xubbbee462009-07-11 18:16:16 +0800138 dctx->fallback.flags = desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
139 err = crypto_shash_export(&dctx->fallback, &state);
140 if (err)
141 goto out;
142
143 if (state.count + count > ULONG_MAX)
144 return crypto_shash_finup(&dctx->fallback, in, count, out);
145
146 leftover = ((state.count - 1) & (SHA256_BLOCK_SIZE - 1)) + 1;
147 space = SHA256_BLOCK_SIZE - leftover;
148 if (space) {
149 if (count > space) {
150 err = crypto_shash_update(&dctx->fallback, in, space) ?:
151 crypto_shash_export(&dctx->fallback, &state);
152 if (err)
153 goto out;
154 count -= space;
155 in += space;
156 } else {
157 memcpy(state.buf + leftover, in, count);
158 in = state.buf;
159 count += leftover;
160 }
161 }
162
163 memcpy(result, &state.state, SHA256_DIGEST_SIZE);
Michal Ludvig6c833272006-07-12 12:29:38 +1000164
Suresh Siddhae4914012008-08-13 22:02:26 +1000165 /* prevent taking the spurious DNA fault with padlock. */
166 ts_state = irq_ts_save();
Michal Ludvig6c833272006-07-12 12:29:38 +1000167 asm volatile (".byte 0xf3,0x0f,0xa6,0xd0" /* rep xsha256 */
Herbert Xubbbee462009-07-11 18:16:16 +0800168 : \
Herbert Xufaae8902009-07-15 18:37:48 +0800169 : "c"((unsigned long)state.count + count), \
170 "a"((unsigned long)state.count), \
Herbert Xubbbee462009-07-11 18:16:16 +0800171 "S"(in), "D"(result));
Suresh Siddhae4914012008-08-13 22:02:26 +1000172 irq_ts_restore(ts_state);
Michal Ludvig6c833272006-07-12 12:29:38 +1000173
174 padlock_output_block((uint32_t *)result, (uint32_t *)out, 8);
Herbert Xubbbee462009-07-11 18:16:16 +0800175
176out:
177 return err;
Michal Ludvig6c833272006-07-12 12:29:38 +1000178}
179
Herbert Xubbbee462009-07-11 18:16:16 +0800180static int padlock_sha256_final(struct shash_desc *desc, u8 *out)
Michal Ludvig6c833272006-07-12 12:29:38 +1000181{
Herbert Xubbbee462009-07-11 18:16:16 +0800182 u8 buf[4];
Herbert Xu7d024602009-07-10 17:26:44 +0800183
Herbert Xubbbee462009-07-11 18:16:16 +0800184 return padlock_sha256_finup(desc, buf, 0, out);
Michal Ludvig6c833272006-07-12 12:29:38 +1000185}
186
Herbert Xu60104392006-08-26 18:34:10 +1000187static int padlock_cra_init(struct crypto_tfm *tfm)
Michal Ludvig6c833272006-07-12 12:29:38 +1000188{
Herbert Xubbbee462009-07-11 18:16:16 +0800189 struct crypto_shash *hash = __crypto_shash_cast(tfm);
Herbert Xu60104392006-08-26 18:34:10 +1000190 const char *fallback_driver_name = tfm->__crt_alg->cra_name;
Herbert Xubbbee462009-07-11 18:16:16 +0800191 struct padlock_sha_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu7d024602009-07-10 17:26:44 +0800192 struct crypto_shash *fallback_tfm;
193 int err = -ENOMEM;
Herbert Xu60104392006-08-26 18:34:10 +1000194
Michal Ludvig6c833272006-07-12 12:29:38 +1000195 /* Allocate a fallback and abort if it failed. */
Herbert Xu7d024602009-07-10 17:26:44 +0800196 fallback_tfm = crypto_alloc_shash(fallback_driver_name, 0,
197 CRYPTO_ALG_NEED_FALLBACK);
Herbert Xu60104392006-08-26 18:34:10 +1000198 if (IS_ERR(fallback_tfm)) {
Michal Ludvig6c833272006-07-12 12:29:38 +1000199 printk(KERN_WARNING PFX "Fallback driver '%s' could not be loaded!\n",
200 fallback_driver_name);
Herbert Xu7d024602009-07-10 17:26:44 +0800201 err = PTR_ERR(fallback_tfm);
Herbert Xubbbee462009-07-11 18:16:16 +0800202 goto out;
Michal Ludvig6c833272006-07-12 12:29:38 +1000203 }
204
Herbert Xubbbee462009-07-11 18:16:16 +0800205 ctx->fallback = fallback_tfm;
206 hash->descsize += crypto_shash_descsize(fallback_tfm);
Michal Ludvig6c833272006-07-12 12:29:38 +1000207 return 0;
Herbert Xu7d024602009-07-10 17:26:44 +0800208
Herbert Xu7d024602009-07-10 17:26:44 +0800209out:
210 return err;
Michal Ludvig6c833272006-07-12 12:29:38 +1000211}
212
Michal Ludvig6c833272006-07-12 12:29:38 +1000213static void padlock_cra_exit(struct crypto_tfm *tfm)
214{
Herbert Xubbbee462009-07-11 18:16:16 +0800215 struct padlock_sha_ctx *ctx = crypto_tfm_ctx(tfm);
Michal Ludvig6c833272006-07-12 12:29:38 +1000216
Herbert Xubbbee462009-07-11 18:16:16 +0800217 crypto_free_shash(ctx->fallback);
Michal Ludvig6c833272006-07-12 12:29:38 +1000218}
219
Herbert Xubbbee462009-07-11 18:16:16 +0800220static struct shash_alg sha1_alg = {
221 .digestsize = SHA1_DIGEST_SIZE,
222 .init = padlock_sha_init,
223 .update = padlock_sha_update,
224 .finup = padlock_sha1_finup,
225 .final = padlock_sha1_final,
226 .descsize = sizeof(struct padlock_sha_desc),
227 .base = {
228 .cra_name = "sha1",
229 .cra_driver_name = "sha1-padlock",
230 .cra_priority = PADLOCK_CRA_PRIORITY,
231 .cra_flags = CRYPTO_ALG_TYPE_SHASH |
232 CRYPTO_ALG_NEED_FALLBACK,
233 .cra_blocksize = SHA1_BLOCK_SIZE,
234 .cra_ctxsize = sizeof(struct padlock_sha_ctx),
235 .cra_module = THIS_MODULE,
236 .cra_init = padlock_cra_init,
237 .cra_exit = padlock_cra_exit,
Michal Ludvig6c833272006-07-12 12:29:38 +1000238 }
239};
240
Herbert Xubbbee462009-07-11 18:16:16 +0800241static struct shash_alg sha256_alg = {
242 .digestsize = SHA256_DIGEST_SIZE,
243 .init = padlock_sha_init,
244 .update = padlock_sha_update,
245 .finup = padlock_sha256_finup,
246 .final = padlock_sha256_final,
247 .descsize = sizeof(struct padlock_sha_desc),
248 .base = {
249 .cra_name = "sha256",
250 .cra_driver_name = "sha256-padlock",
251 .cra_priority = PADLOCK_CRA_PRIORITY,
252 .cra_flags = CRYPTO_ALG_TYPE_SHASH |
253 CRYPTO_ALG_NEED_FALLBACK,
254 .cra_blocksize = SHA256_BLOCK_SIZE,
255 .cra_ctxsize = sizeof(struct padlock_sha_ctx),
256 .cra_module = THIS_MODULE,
257 .cra_init = padlock_cra_init,
258 .cra_exit = padlock_cra_exit,
Michal Ludvig6c833272006-07-12 12:29:38 +1000259 }
260};
261
Michal Ludvig6c833272006-07-12 12:29:38 +1000262static int __init padlock_init(void)
263{
264 int rc = -ENODEV;
265
266 if (!cpu_has_phe) {
Jeremy Katzb43e7262008-07-03 19:03:31 +0800267 printk(KERN_NOTICE PFX "VIA PadLock Hash Engine not detected.\n");
Michal Ludvig6c833272006-07-12 12:29:38 +1000268 return -ENODEV;
269 }
270
271 if (!cpu_has_phe_enabled) {
Jeremy Katzb43e7262008-07-03 19:03:31 +0800272 printk(KERN_NOTICE PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
Michal Ludvig6c833272006-07-12 12:29:38 +1000273 return -ENODEV;
274 }
275
Herbert Xubbbee462009-07-11 18:16:16 +0800276 rc = crypto_register_shash(&sha1_alg);
Michal Ludvig6c833272006-07-12 12:29:38 +1000277 if (rc)
278 goto out;
279
Herbert Xubbbee462009-07-11 18:16:16 +0800280 rc = crypto_register_shash(&sha256_alg);
Michal Ludvig6c833272006-07-12 12:29:38 +1000281 if (rc)
282 goto out_unreg1;
283
284 printk(KERN_NOTICE PFX "Using VIA PadLock ACE for SHA1/SHA256 algorithms.\n");
285
286 return 0;
287
288out_unreg1:
Herbert Xubbbee462009-07-11 18:16:16 +0800289 crypto_unregister_shash(&sha1_alg);
Michal Ludvig6c833272006-07-12 12:29:38 +1000290out:
291 printk(KERN_ERR PFX "VIA PadLock SHA1/SHA256 initialization failed.\n");
292 return rc;
293}
294
295static void __exit padlock_fini(void)
296{
Herbert Xubbbee462009-07-11 18:16:16 +0800297 crypto_unregister_shash(&sha1_alg);
298 crypto_unregister_shash(&sha256_alg);
Michal Ludvig6c833272006-07-12 12:29:38 +1000299}
300
301module_init(padlock_init);
302module_exit(padlock_fini);
303
304MODULE_DESCRIPTION("VIA PadLock SHA1/SHA256 algorithms support.");
305MODULE_LICENSE("GPL");
306MODULE_AUTHOR("Michal Ludvig");
307
Herbert Xua760a662009-02-26 14:06:31 +0800308MODULE_ALIAS("sha1-all");
309MODULE_ALIAS("sha256-all");
Michal Ludvig6c833272006-07-12 12:29:38 +1000310MODULE_ALIAS("sha1-padlock");
311MODULE_ALIAS("sha256-padlock");