padlock - get rid of __GNUCLIKE_ASM #ifdefs
[dragonfly.git] / sys / crypto / via / padlock_hash.c
CommitLineData
42ee1e6b
SW
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 * $FreeBSD: src/sys/crypto/via/padlock_hash.c,v 1.4 2009/05/27 09:52:12 vanhu Exp $
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/kernel.h>
32#include <sys/module.h>
33#include <sys/malloc.h>
34#include <sys/libkern.h>
35#include <sys/endian.h>
b2b3ffcd 36#if defined(__x86_64__) || defined(__i386__)
42ee1e6b
SW
37#include <machine/cpufunc.h>
38#include <machine/cputypes.h>
39#include <machine/md_var.h>
40#include <machine/specialreg.h>
41#endif
42
43#include <opencrypto/cryptodev.h>
44#include <opencrypto/cryptosoft.h> /* for hmac_ipad_buffer and hmac_opad_buffer */
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
67struct padlock_sha_ctx {
68 uint8_t *psc_buf;
69 int psc_offset;
70 int psc_size;
71};
72CTASSERT(sizeof(struct padlock_sha_ctx) <= sizeof(union authctx));
73
74static void padlock_sha_init(struct padlock_sha_ctx *ctx);
75static int padlock_sha_update(struct padlock_sha_ctx *ctx, uint8_t *buf,
76 uint16_t bufsize);
77static void padlock_sha1_final(uint8_t *hash, struct padlock_sha_ctx *ctx);
78static void padlock_sha256_final(uint8_t *hash, struct padlock_sha_ctx *ctx);
79
80static struct auth_hash padlock_hmac_sha1 = {
81 CRYPTO_SHA1_HMAC, "HMAC-SHA1",
82 20, SHA1_HASH_LEN, SHA1_HMAC_BLOCK_LEN, sizeof(struct padlock_sha_ctx),
83 (void (*)(void *))padlock_sha_init,
84 (int (*)(void *, uint8_t *, uint16_t))padlock_sha_update,
85 (void (*)(uint8_t *, void *))padlock_sha1_final
86};
87
88static struct auth_hash padlock_hmac_sha256 = {
89 CRYPTO_SHA2_256_HMAC, "HMAC-SHA2-256",
90 32, SHA2_256_HASH_LEN, SHA2_256_HMAC_BLOCK_LEN, sizeof(struct padlock_sha_ctx),
91 (void (*)(void *))padlock_sha_init,
92 (int (*)(void *, uint8_t *, uint16_t))padlock_sha_update,
93 (void (*)(uint8_t *, void *))padlock_sha256_final
94};
95
96MALLOC_DECLARE(M_PADLOCK);
97
98static __inline void
99padlock_output_block(uint32_t *src, uint32_t *dst, size_t count)
100{
101
102 while (count-- > 0)
103 *dst++ = bswap32(*src++);
104}
105
106static void
107padlock_do_sha1(const u_char *in, u_char *out, int count)
108{
109 u_char buf[128+16]; /* PadLock needs at least 128 bytes buffer. */
110 u_char *result = PADLOCK_ALIGN(buf);
111
112 ((uint32_t *)result)[0] = 0x67452301;
113 ((uint32_t *)result)[1] = 0xEFCDAB89;
114 ((uint32_t *)result)[2] = 0x98BADCFE;
115 ((uint32_t *)result)[3] = 0x10325476;
116 ((uint32_t *)result)[4] = 0xC3D2E1F0;
117
42ee1e6b
SW
118 __asm __volatile(
119 ".byte 0xf3, 0x0f, 0xa6, 0xc8" /* rep xsha1 */
120 : "+S"(in), "+D"(result)
121 : "c"(count), "a"(0)
122 );
42ee1e6b
SW
123
124 padlock_output_block((uint32_t *)result, (uint32_t *)out,
125 SHA1_HASH_LEN / sizeof(uint32_t));
126}
127
128static void
129padlock_do_sha256(const char *in, char *out, int count)
130{
131 char buf[128+16]; /* PadLock needs at least 128 bytes buffer. */
132 char *result = PADLOCK_ALIGN(buf);
133
134 ((uint32_t *)result)[0] = 0x6A09E667;
135 ((uint32_t *)result)[1] = 0xBB67AE85;
136 ((uint32_t *)result)[2] = 0x3C6EF372;
137 ((uint32_t *)result)[3] = 0xA54FF53A;
138 ((uint32_t *)result)[4] = 0x510E527F;
139 ((uint32_t *)result)[5] = 0x9B05688C;
140 ((uint32_t *)result)[6] = 0x1F83D9AB;
141 ((uint32_t *)result)[7] = 0x5BE0CD19;
142
42ee1e6b
SW
143 __asm __volatile(
144 ".byte 0xf3, 0x0f, 0xa6, 0xd0" /* rep xsha256 */
145 : "+S"(in), "+D"(result)
146 : "c"(count), "a"(0)
147 );
42ee1e6b
SW
148
149 padlock_output_block((uint32_t *)result, (uint32_t *)out,
150 SHA2_256_HASH_LEN / sizeof(uint32_t));
151}
152
153static void
154padlock_sha_init(struct padlock_sha_ctx *ctx)
155{
156
157 ctx->psc_buf = NULL;
158 ctx->psc_offset = 0;
159 ctx->psc_size = 0;
160}
161
162static int
163padlock_sha_update(struct padlock_sha_ctx *ctx, uint8_t *buf, uint16_t bufsize)
164{
165
166 if (ctx->psc_size - ctx->psc_offset < bufsize) {
167 ctx->psc_size = MAX(ctx->psc_size * 2, ctx->psc_size + bufsize);
54734da1 168 ctx->psc_buf = krealloc(ctx->psc_buf, ctx->psc_size, M_PADLOCK,
42ee1e6b
SW
169 M_NOWAIT);
170 if(ctx->psc_buf == NULL)
171 return (ENOMEM);
172 }
173 bcopy(buf, ctx->psc_buf + ctx->psc_offset, bufsize);
174 ctx->psc_offset += bufsize;
175 return (0);
176}
177
178static void
179padlock_sha_free(struct padlock_sha_ctx *ctx)
180{
181
182 if (ctx->psc_buf != NULL) {
183 //bzero(ctx->psc_buf, ctx->psc_size);
54734da1 184 kfree(ctx->psc_buf, M_PADLOCK);
42ee1e6b
SW
185 ctx->psc_buf = NULL;
186 ctx->psc_offset = 0;
187 ctx->psc_size = 0;
188 }
189}
190
191static void
192padlock_sha1_final(uint8_t *hash, struct padlock_sha_ctx *ctx)
193{
194
195 padlock_do_sha1(ctx->psc_buf, hash, ctx->psc_offset);
196 padlock_sha_free(ctx);
197}
198
199static void
200padlock_sha256_final(uint8_t *hash, struct padlock_sha_ctx *ctx)
201{
202
203 padlock_do_sha256(ctx->psc_buf, hash, ctx->psc_offset);
204 padlock_sha_free(ctx);
205}
206
207static void
208padlock_copy_ctx(struct auth_hash *axf, void *sctx, void *dctx)
209{
210
211 if ((via_feature_xcrypt & VIA_HAS_SHA) != 0 &&
212 (axf->type == CRYPTO_SHA1_HMAC ||
213 axf->type == CRYPTO_SHA2_256_HMAC)) {
214 struct padlock_sha_ctx *spctx = sctx, *dpctx = dctx;
215
216 dpctx->psc_offset = spctx->psc_offset;
217 dpctx->psc_size = spctx->psc_size;
54734da1 218 dpctx->psc_buf = kmalloc(dpctx->psc_size, M_PADLOCK, M_WAITOK);
42ee1e6b
SW
219 bcopy(spctx->psc_buf, dpctx->psc_buf, dpctx->psc_size);
220 } else {
221 bcopy(sctx, dctx, axf->ctxsize);
222 }
223}
224
225static void
226padlock_free_ctx(struct auth_hash *axf, void *ctx)
227{
228
229 if ((via_feature_xcrypt & VIA_HAS_SHA) != 0 &&
230 (axf->type == CRYPTO_SHA1_HMAC ||
231 axf->type == CRYPTO_SHA2_256_HMAC)) {
232 padlock_sha_free(ctx);
233 }
234}
235
236static void
237padlock_hash_key_setup(struct padlock_session *ses, caddr_t key, int klen)
238{
239 struct auth_hash *axf;
240 int i;
241
242 klen /= 8;
243 axf = ses->ses_axf;
244
245 /*
246 * Try to free contexts before using them, because
247 * padlock_hash_key_setup() can be called twice - once from
248 * padlock_newsession() and again from padlock_process().
249 */
250 padlock_free_ctx(axf, ses->ses_ictx);
251 padlock_free_ctx(axf, ses->ses_octx);
252
253 for (i = 0; i < klen; i++)
254 key[i] ^= HMAC_IPAD_VAL;
255
256 axf->Init(ses->ses_ictx);
257 axf->Update(ses->ses_ictx, key, klen);
258 axf->Update(ses->ses_ictx, hmac_ipad_buffer, axf->blocksize - klen);
259
260 for (i = 0; i < klen; i++)
261 key[i] ^= (HMAC_IPAD_VAL ^ HMAC_OPAD_VAL);
262
263 axf->Init(ses->ses_octx);
264 axf->Update(ses->ses_octx, key, klen);
265 axf->Update(ses->ses_octx, hmac_opad_buffer, axf->blocksize - klen);
266
267 for (i = 0; i < klen; i++)
268 key[i] ^= HMAC_OPAD_VAL;
269}
270
271/*
272 * Compute keyed-hash authenticator.
273 */
274static int
275padlock_authcompute(struct padlock_session *ses, struct cryptodesc *crd,
276 caddr_t buf, int flags)
277{
278 u_char hash[HASH_MAX_LEN];
279 struct auth_hash *axf;
280 union authctx ctx;
281 int error;
282
283 axf = ses->ses_axf;
284
285 padlock_copy_ctx(axf, ses->ses_ictx, &ctx);
286 error = crypto_apply(flags, buf, crd->crd_skip, crd->crd_len,
287 (int (*)(void *, void *, unsigned int))axf->Update, (caddr_t)&ctx);
288 if (error != 0) {
289 padlock_free_ctx(axf, &ctx);
290 return (error);
291 }
292 axf->Final(hash, &ctx);
293
294 padlock_copy_ctx(axf, ses->ses_octx, &ctx);
295 axf->Update(&ctx, hash, axf->hashsize);
296 axf->Final(hash, &ctx);
297
298 /* Inject the authentication data */
299 crypto_copyback(flags, buf, crd->crd_inject,
300 ses->ses_mlen == 0 ? axf->hashsize : ses->ses_mlen, hash);
301 return (0);
302}
303
304int
305padlock_hash_setup(struct padlock_session *ses, struct cryptoini *macini)
306{
307
308 ses->ses_mlen = macini->cri_mlen;
309
310 /* Find software structure which describes HMAC algorithm. */
311 switch (macini->cri_alg) {
312 case CRYPTO_NULL_HMAC:
313 ses->ses_axf = &auth_hash_null;
314 break;
315 case CRYPTO_MD5_HMAC:
316 ses->ses_axf = &auth_hash_hmac_md5;
317 break;
318 case CRYPTO_SHA1_HMAC:
319 if ((via_feature_xcrypt & VIA_HAS_SHA) != 0)
320 ses->ses_axf = &padlock_hmac_sha1;
321 else
322 ses->ses_axf = &auth_hash_hmac_sha1;
323 break;
324 case CRYPTO_RIPEMD160_HMAC:
325 ses->ses_axf = &auth_hash_hmac_ripemd_160;
326 break;
327 case CRYPTO_SHA2_256_HMAC:
328 if ((via_feature_xcrypt & VIA_HAS_SHA) != 0)
329 ses->ses_axf = &padlock_hmac_sha256;
330 else
331 ses->ses_axf = &auth_hash_hmac_sha2_256;
332 break;
333 case CRYPTO_SHA2_384_HMAC:
334 ses->ses_axf = &auth_hash_hmac_sha2_384;
335 break;
336 case CRYPTO_SHA2_512_HMAC:
337 ses->ses_axf = &auth_hash_hmac_sha2_512;
338 break;
339 }
340
341 /* Allocate memory for HMAC inner and outer contexts. */
54734da1 342 ses->ses_ictx = kmalloc(ses->ses_axf->ctxsize, M_PADLOCK,
42ee1e6b 343 M_ZERO | M_NOWAIT);
54734da1 344 ses->ses_octx = kmalloc(ses->ses_axf->ctxsize, M_PADLOCK,
42ee1e6b
SW
345 M_ZERO | M_NOWAIT);
346 if (ses->ses_ictx == NULL || ses->ses_octx == NULL)
347 return (ENOMEM);
348
349 /* Setup key if given. */
350 if (macini->cri_key != NULL) {
351 padlock_hash_key_setup(ses, macini->cri_key,
352 macini->cri_klen);
353 }
354 return (0);
355}
356
357int
358padlock_hash_process(struct padlock_session *ses, struct cryptodesc *maccrd,
359 struct cryptop *crp)
360{
361 int error;
362
363 if ((maccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0)
364 padlock_hash_key_setup(ses, maccrd->crd_key, maccrd->crd_klen);
365
366 error = padlock_authcompute(ses, maccrd, crp->crp_buf, crp->crp_flags);
367 return (error);
368}
369
370void
371padlock_hash_free(struct padlock_session *ses)
372{
373
374 if (ses->ses_ictx != NULL) {
375 padlock_free_ctx(ses->ses_axf, ses->ses_ictx);
376 bzero(ses->ses_ictx, ses->ses_axf->ctxsize);
54734da1 377 kfree(ses->ses_ictx, M_PADLOCK);
42ee1e6b
SW
378 ses->ses_ictx = NULL;
379 }
380 if (ses->ses_octx != NULL) {
381 padlock_free_ctx(ses->ses_axf, ses->ses_octx);
382 bzero(ses->ses_octx, ses->ses_axf->ctxsize);
54734da1 383 kfree(ses->ses_octx, M_PADLOCK);
42ee1e6b
SW
384 ses->ses_octx = NULL;
385 }
386}