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