1 /*-
2 * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
31 #include <sys/module.h>
32 #include <sys/malloc.h>
33 #include <sys/libkern.h>
34 #include <sys/endian.h>
35 #include <sys/pcpu.h>
36 #if defined(__amd64__) || defined(__i386__)
37 #include <machine/cpufunc.h>
38 #include <machine/cputypes.h>
39 #include <machine/md_var.h>
40 #include <machine/specialreg.h>
41 #endif
42 #include <machine/pcb.h>
43
44 #include <opencrypto/cryptodev.h>
45 #include <opencrypto/xform.h>
46
47 #include <crypto/via/padlock.h>
48
49 /*
50 * Implementation notes.
51 *
52 * Some VIA CPUs provides SHA1 and SHA256 acceleration.
53 * We implement all HMAC algorithms provided by crypto(9) framework, but we do
54 * the crypto work in software unless this is HMAC/SHA1 or HMAC/SHA256 and
55 * our CPU can accelerate it.
56 *
57 * Additional CPU instructions, which preform SHA1 and SHA256 are one-shot
58 * functions - we have only one chance to give the data, CPU itself will add
59 * the padding and calculate hash automatically.
60 * This means, it is not possible to implement common init(), update(), final()
61 * methods.
62 * The way I've choosen is to keep adding data to the buffer on update()
63 * (reallocating the buffer if necessary) and call XSHA{1,256} instruction on
64 * final().
65 */
66
67 struct padlock_sha_ctx {
68 uint8_t *psc_buf;
69 int psc_offset;
70 int psc_size;
71 };
72 CTASSERT(sizeof(struct padlock_sha_ctx) <= sizeof(union authctx));
73
74 static void padlock_sha_init(void *vctx);
75 static int padlock_sha_update(void *vctx, const void *buf, u_int bufsize);
76 static void padlock_sha1_final(uint8_t *hash, void *vctx);
77 static void padlock_sha256_final(uint8_t *hash, void *vctx);
78
79 static struct auth_hash padlock_hmac_sha1 = {
80 .type = CRYPTO_SHA1_HMAC,
81 .name = "HMAC-SHA1",
82 .keysize = SHA1_BLOCK_LEN,
83 .hashsize = SHA1_HASH_LEN,
84 .ctxsize = sizeof(struct padlock_sha_ctx),
85 .blocksize = SHA1_BLOCK_LEN,
86 .Init = padlock_sha_init,
87 .Update = padlock_sha_update,
88 .Final = padlock_sha1_final,
89 };
90
91 static struct auth_hash padlock_hmac_sha256 = {
92 .type = CRYPTO_SHA2_256_HMAC,
93 .name = "HMAC-SHA2-256",
94 .keysize = SHA2_256_BLOCK_LEN,
95 .hashsize = SHA2_256_HASH_LEN,
96 .ctxsize = sizeof(struct padlock_sha_ctx),
97 .blocksize = SHA2_256_BLOCK_LEN,
98 .Init = padlock_sha_init,
99 .Update = padlock_sha_update,
100 .Final = padlock_sha256_final,
101 };
102
103 MALLOC_DECLARE(M_PADLOCK);
104
105 static __inline void
padlock_output_block(uint32_t * src,uint32_t * dst,size_t count)106 padlock_output_block(uint32_t *src, uint32_t *dst, size_t count)
107 {
108
109 while (count-- > 0)
110 *dst++ = bswap32(*src++);
111 }
112
113 static void
padlock_do_sha1(const u_char * in,u_char * out,int count)114 padlock_do_sha1(const u_char *in, u_char *out, int count)
115 {
116 u_char buf[128+16]; /* PadLock needs at least 128 bytes buffer. */
117 u_char *result = PADLOCK_ALIGN(buf);
118
119 ((uint32_t *)result)[0] = 0x67452301;
120 ((uint32_t *)result)[1] = 0xEFCDAB89;
121 ((uint32_t *)result)[2] = 0x98BADCFE;
122 ((uint32_t *)result)[3] = 0x10325476;
123 ((uint32_t *)result)[4] = 0xC3D2E1F0;
124
125 #ifdef __GNUCLIKE_ASM
126 __asm __volatile(
127 ".byte 0xf3, 0x0f, 0xa6, 0xc8" /* rep xsha1 */
128 : "+S"(in), "+D"(result)
129 : "c"(count), "a"(0)
130 );
131 #endif
132
133 padlock_output_block((uint32_t *)result, (uint32_t *)out,
134 SHA1_HASH_LEN / sizeof(uint32_t));
135 }
136
137 static void
padlock_do_sha256(const char * in,char * out,int count)138 padlock_do_sha256(const char *in, char *out, int count)
139 {
140 char buf[128+16]; /* PadLock needs at least 128 bytes buffer. */
141 char *result = PADLOCK_ALIGN(buf);
142
143 ((uint32_t *)result)[0] = 0x6A09E667;
144 ((uint32_t *)result)[1] = 0xBB67AE85;
145 ((uint32_t *)result)[2] = 0x3C6EF372;
146 ((uint32_t *)result)[3] = 0xA54FF53A;
147 ((uint32_t *)result)[4] = 0x510E527F;
148 ((uint32_t *)result)[5] = 0x9B05688C;
149 ((uint32_t *)result)[6] = 0x1F83D9AB;
150 ((uint32_t *)result)[7] = 0x5BE0CD19;
151
152 #ifdef __GNUCLIKE_ASM
153 __asm __volatile(
154 ".byte 0xf3, 0x0f, 0xa6, 0xd0" /* rep xsha256 */
155 : "+S"(in), "+D"(result)
156 : "c"(count), "a"(0)
157 );
158 #endif
159
160 padlock_output_block((uint32_t *)result, (uint32_t *)out,
161 SHA2_256_HASH_LEN / sizeof(uint32_t));
162 }
163
164 static void
padlock_sha_init(void * vctx)165 padlock_sha_init(void *vctx)
166 {
167 struct padlock_sha_ctx *ctx;
168
169 ctx = vctx;
170 ctx->psc_buf = NULL;
171 ctx->psc_offset = 0;
172 ctx->psc_size = 0;
173 }
174
175 static int
padlock_sha_update(void * vctx,const void * buf,u_int bufsize)176 padlock_sha_update(void *vctx, const void *buf, u_int bufsize)
177 {
178 struct padlock_sha_ctx *ctx;
179
180 ctx = vctx;
181 if (ctx->psc_size - ctx->psc_offset < bufsize) {
182 ctx->psc_size = MAX(ctx->psc_size * 2, ctx->psc_size + bufsize);
183 ctx->psc_buf = realloc(ctx->psc_buf, ctx->psc_size, M_PADLOCK,
184 M_NOWAIT);
185 if(ctx->psc_buf == NULL)
186 return (ENOMEM);
187 }
188 bcopy(buf, ctx->psc_buf + ctx->psc_offset, bufsize);
189 ctx->psc_offset += bufsize;
190 return (0);
191 }
192
193 static void
padlock_sha_free(void * vctx)194 padlock_sha_free(void *vctx)
195 {
196 struct padlock_sha_ctx *ctx;
197
198 ctx = vctx;
199 if (ctx->psc_buf != NULL) {
200 zfree(ctx->psc_buf, M_PADLOCK);
201 ctx->psc_buf = NULL;
202 ctx->psc_offset = 0;
203 ctx->psc_size = 0;
204 }
205 }
206
207 static void
padlock_sha1_final(uint8_t * hash,void * vctx)208 padlock_sha1_final(uint8_t *hash, void *vctx)
209 {
210 struct padlock_sha_ctx *ctx;
211
212 ctx = vctx;
213 padlock_do_sha1(ctx->psc_buf, hash, ctx->psc_offset);
214 padlock_sha_free(ctx);
215 }
216
217 static void
padlock_sha256_final(uint8_t * hash,void * vctx)218 padlock_sha256_final(uint8_t *hash, void *vctx)
219 {
220 struct padlock_sha_ctx *ctx;
221
222 ctx = vctx;
223 padlock_do_sha256(ctx->psc_buf, hash, ctx->psc_offset);
224 padlock_sha_free(ctx);
225 }
226
227 static void
padlock_copy_ctx(struct auth_hash * axf,void * sctx,void * dctx)228 padlock_copy_ctx(struct auth_hash *axf, void *sctx, void *dctx)
229 {
230
231 if ((via_feature_xcrypt & VIA_HAS_SHA) != 0 &&
232 (axf->type == CRYPTO_SHA1_HMAC ||
233 axf->type == CRYPTO_SHA2_256_HMAC)) {
234 struct padlock_sha_ctx *spctx = sctx, *dpctx = dctx;
235
236 dpctx->psc_offset = spctx->psc_offset;
237 dpctx->psc_size = spctx->psc_size;
238 dpctx->psc_buf = malloc(dpctx->psc_size, M_PADLOCK, M_WAITOK);
239 bcopy(spctx->psc_buf, dpctx->psc_buf, dpctx->psc_size);
240 } else {
241 bcopy(sctx, dctx, axf->ctxsize);
242 }
243 }
244
245 static void
padlock_free_ctx(struct auth_hash * axf,void * ctx)246 padlock_free_ctx(struct auth_hash *axf, void *ctx)
247 {
248
249 if ((via_feature_xcrypt & VIA_HAS_SHA) != 0 &&
250 (axf->type == CRYPTO_SHA1_HMAC ||
251 axf->type == CRYPTO_SHA2_256_HMAC)) {
252 padlock_sha_free(ctx);
253 }
254 }
255
256 static void
padlock_hash_key_setup(struct padlock_session * ses,const uint8_t * key,int klen)257 padlock_hash_key_setup(struct padlock_session *ses, const uint8_t *key,
258 int klen)
259 {
260 struct auth_hash *axf;
261
262 axf = ses->ses_axf;
263
264 /*
265 * Try to free contexts before using them, because
266 * padlock_hash_key_setup() can be called twice - once from
267 * padlock_newsession() and again from padlock_process().
268 */
269 padlock_free_ctx(axf, ses->ses_ictx);
270 padlock_free_ctx(axf, ses->ses_octx);
271
272 hmac_init_ipad(axf, key, klen, ses->ses_ictx);
273 hmac_init_opad(axf, key, klen, ses->ses_octx);
274 }
275
276 /*
277 * Compute keyed-hash authenticator.
278 */
279 static int
padlock_authcompute(struct padlock_session * ses,struct cryptop * crp)280 padlock_authcompute(struct padlock_session *ses, struct cryptop *crp)
281 {
282 u_char hash[HASH_MAX_LEN], hash2[HASH_MAX_LEN];
283 struct auth_hash *axf;
284 union authctx ctx;
285 int error;
286
287 axf = ses->ses_axf;
288
289 padlock_copy_ctx(axf, ses->ses_ictx, &ctx);
290 error = crypto_apply(crp, crp->crp_aad_start, crp->crp_aad_length,
291 axf->Update, &ctx);
292 if (error != 0) {
293 padlock_free_ctx(axf, &ctx);
294 return (error);
295 }
296 error = crypto_apply(crp, crp->crp_payload_start,
297 crp->crp_payload_length, axf->Update, &ctx);
298 if (error != 0) {
299 padlock_free_ctx(axf, &ctx);
300 return (error);
301 }
302 axf->Final(hash, &ctx);
303
304 padlock_copy_ctx(axf, ses->ses_octx, &ctx);
305 axf->Update(&ctx, hash, axf->hashsize);
306 axf->Final(hash, &ctx);
307
308 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
309 crypto_copydata(crp, crp->crp_digest_start, ses->ses_mlen,
310 hash2);
311 if (timingsafe_bcmp(hash, hash2, ses->ses_mlen) != 0)
312 return (EBADMSG);
313 } else
314 crypto_copyback(crp, crp->crp_digest_start, ses->ses_mlen,
315 hash);
316 return (0);
317 }
318
319 /* Find software structure which describes HMAC algorithm. */
320 static struct auth_hash *
padlock_hash_lookup(int alg)321 padlock_hash_lookup(int alg)
322 {
323 struct auth_hash *axf;
324
325 switch (alg) {
326 case CRYPTO_NULL_HMAC:
327 axf = &auth_hash_null;
328 break;
329 case CRYPTO_SHA1_HMAC:
330 if ((via_feature_xcrypt & VIA_HAS_SHA) != 0)
331 axf = &padlock_hmac_sha1;
332 else
333 axf = &auth_hash_hmac_sha1;
334 break;
335 case CRYPTO_RIPEMD160_HMAC:
336 axf = &auth_hash_hmac_ripemd_160;
337 break;
338 case CRYPTO_SHA2_256_HMAC:
339 if ((via_feature_xcrypt & VIA_HAS_SHA) != 0)
340 axf = &padlock_hmac_sha256;
341 else
342 axf = &auth_hash_hmac_sha2_256;
343 break;
344 case CRYPTO_SHA2_384_HMAC:
345 axf = &auth_hash_hmac_sha2_384;
346 break;
347 case CRYPTO_SHA2_512_HMAC:
348 axf = &auth_hash_hmac_sha2_512;
349 break;
350 default:
351 axf = NULL;
352 break;
353 }
354 return (axf);
355 }
356
357 bool
padlock_hash_check(const struct crypto_session_params * csp)358 padlock_hash_check(const struct crypto_session_params *csp)
359 {
360
361 return (padlock_hash_lookup(csp->csp_auth_alg) != NULL);
362 }
363
364 int
padlock_hash_setup(struct padlock_session * ses,const struct crypto_session_params * csp)365 padlock_hash_setup(struct padlock_session *ses,
366 const struct crypto_session_params *csp)
367 {
368
369 ses->ses_axf = padlock_hash_lookup(csp->csp_auth_alg);
370 if (csp->csp_auth_mlen == 0)
371 ses->ses_mlen = ses->ses_axf->hashsize;
372 else
373 ses->ses_mlen = csp->csp_auth_mlen;
374
375 /* Allocate memory for HMAC inner and outer contexts. */
376 ses->ses_ictx = malloc(ses->ses_axf->ctxsize, M_PADLOCK,
377 M_ZERO | M_NOWAIT);
378 ses->ses_octx = malloc(ses->ses_axf->ctxsize, M_PADLOCK,
379 M_ZERO | M_NOWAIT);
380 if (ses->ses_ictx == NULL || ses->ses_octx == NULL)
381 return (ENOMEM);
382
383 /* Setup key if given. */
384 if (csp->csp_auth_key != NULL) {
385 padlock_hash_key_setup(ses, csp->csp_auth_key,
386 csp->csp_auth_klen);
387 }
388 return (0);
389 }
390
391 int
padlock_hash_process(struct padlock_session * ses,struct cryptop * crp,const struct crypto_session_params * csp)392 padlock_hash_process(struct padlock_session *ses, struct cryptop *crp,
393 const struct crypto_session_params *csp)
394 {
395 struct thread *td;
396 int error;
397
398 td = curthread;
399 fpu_kern_enter(td, ses->ses_fpu_ctx, FPU_KERN_NORMAL | FPU_KERN_KTHR);
400 if (crp->crp_auth_key != NULL)
401 padlock_hash_key_setup(ses, crp->crp_auth_key,
402 csp->csp_auth_klen);
403
404 error = padlock_authcompute(ses, crp);
405 fpu_kern_leave(td, ses->ses_fpu_ctx);
406 return (error);
407 }
408
409 void
padlock_hash_free(struct padlock_session * ses)410 padlock_hash_free(struct padlock_session *ses)
411 {
412
413 if (ses->ses_ictx != NULL) {
414 padlock_free_ctx(ses->ses_axf, ses->ses_ictx);
415 zfree(ses->ses_ictx, M_PADLOCK);
416 ses->ses_ictx = NULL;
417 }
418 if (ses->ses_octx != NULL) {
419 padlock_free_ctx(ses->ses_axf, ses->ses_octx);
420 zfree(ses->ses_octx, M_PADLOCK);
421 ses->ses_octx = NULL;
422 }
423 }
424