]> git.proxmox.com Git - mirror_zfs.git/blame - module/icp/io/skein_mod.c
Linux 4.14 compat: CONFIG_GCC_PLUGIN_RANDSTRUCT
[mirror_zfs.git] / module / icp / io / skein_mod.c
CommitLineData
3c67d83a
TH
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2013 Saso Kiselkov. All rights reserved.
24 */
25
26#include <sys/modctl.h>
27#include <sys/crypto/common.h>
28#include <sys/crypto/spi.h>
29#include <sys/sysmacros.h>
30#include <sys/systm.h>
31#define SKEIN_MODULE_IMPL
32#include <sys/skein.h>
33
34/*
35 * Like the sha2 module, we create the skein module with two modlinkages:
36 * - modlmisc to allow direct calls to Skein_* API functions.
37 * - modlcrypto to integrate well into the Kernel Crypto Framework (KCF).
38 */
39static struct modlmisc modlmisc = {
40 &mod_cryptoops,
41 "Skein Message-Digest Algorithm"
42};
43
44static struct modlcrypto modlcrypto = {
45 &mod_cryptoops,
46 "Skein Kernel SW Provider"
47};
48
49static struct modlinkage modlinkage = {
50 MODREV_1, {&modlmisc, &modlcrypto, NULL}
51};
52
53static crypto_mech_info_t skein_mech_info_tab[] = {
54 {CKM_SKEIN_256, SKEIN_256_MECH_INFO_TYPE,
55 CRYPTO_FG_DIGEST | CRYPTO_FG_DIGEST_ATOMIC,
56 0, 0, CRYPTO_KEYSIZE_UNIT_IN_BITS},
57 {CKM_SKEIN_256_MAC, SKEIN_256_MAC_MECH_INFO_TYPE,
58 CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC, 1, INT_MAX,
59 CRYPTO_KEYSIZE_UNIT_IN_BYTES},
60 {CKM_SKEIN_512, SKEIN_512_MECH_INFO_TYPE,
61 CRYPTO_FG_DIGEST | CRYPTO_FG_DIGEST_ATOMIC,
62 0, 0, CRYPTO_KEYSIZE_UNIT_IN_BITS},
63 {CKM_SKEIN_512_MAC, SKEIN_512_MAC_MECH_INFO_TYPE,
64 CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC, 1, INT_MAX,
65 CRYPTO_KEYSIZE_UNIT_IN_BYTES},
66 {CKM_SKEIN1024, SKEIN1024_MECH_INFO_TYPE,
67 CRYPTO_FG_DIGEST | CRYPTO_FG_DIGEST_ATOMIC,
68 0, 0, CRYPTO_KEYSIZE_UNIT_IN_BITS},
69 {CKM_SKEIN1024_MAC, SKEIN1024_MAC_MECH_INFO_TYPE,
70 CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC, 1, INT_MAX,
71 CRYPTO_KEYSIZE_UNIT_IN_BYTES}
72};
73
74static void skein_provider_status(crypto_provider_handle_t, uint_t *);
75
76static crypto_control_ops_t skein_control_ops = {
77 skein_provider_status
78};
79
80static int skein_digest_init(crypto_ctx_t *, crypto_mechanism_t *,
81 crypto_req_handle_t);
82static int skein_digest(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
83 crypto_req_handle_t);
84static int skein_update(crypto_ctx_t *, crypto_data_t *, crypto_req_handle_t);
85static int skein_final(crypto_ctx_t *, crypto_data_t *, crypto_req_handle_t);
86static int skein_digest_atomic(crypto_provider_handle_t, crypto_session_id_t,
87 crypto_mechanism_t *, crypto_data_t *, crypto_data_t *,
88 crypto_req_handle_t);
89
90static crypto_digest_ops_t skein_digest_ops = {
56d8d8ac
MW
91 .digest_init = skein_digest_init,
92 .digest = skein_digest,
93 .digest_update = skein_update,
94 .digest_key = NULL,
95 .digest_final = skein_final,
96 .digest_atomic = skein_digest_atomic
3c67d83a
TH
97};
98
99static int skein_mac_init(crypto_ctx_t *, crypto_mechanism_t *, crypto_key_t *,
100 crypto_spi_ctx_template_t, crypto_req_handle_t);
101static int skein_mac_atomic(crypto_provider_handle_t, crypto_session_id_t,
102 crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, crypto_data_t *,
103 crypto_spi_ctx_template_t, crypto_req_handle_t);
104
105static crypto_mac_ops_t skein_mac_ops = {
56d8d8ac
MW
106 .mac_init = skein_mac_init,
107 .mac = NULL,
108 .mac_update = skein_update, /* using regular digest update is OK here */
109 .mac_final = skein_final, /* using regular digest final is OK here */
110 .mac_atomic = skein_mac_atomic,
111 .mac_verify_atomic = NULL
3c67d83a
TH
112};
113
114static int skein_create_ctx_template(crypto_provider_handle_t,
115 crypto_mechanism_t *, crypto_key_t *, crypto_spi_ctx_template_t *,
116 size_t *, crypto_req_handle_t);
117static int skein_free_context(crypto_ctx_t *);
118
119static crypto_ctx_ops_t skein_ctx_ops = {
56d8d8ac
MW
120 .create_ctx_template = skein_create_ctx_template,
121 .free_context = skein_free_context
3c67d83a
TH
122};
123
124static crypto_ops_t skein_crypto_ops = {{{{{
125 &skein_control_ops,
126 &skein_digest_ops,
127 NULL,
128 &skein_mac_ops,
129 NULL,
130 NULL,
131 NULL,
132 NULL,
133 NULL,
134 NULL,
135 NULL,
136 NULL,
137 NULL,
138 &skein_ctx_ops,
139}}}}};
140
141static crypto_provider_info_t skein_prov_info = {{{{
142 CRYPTO_SPI_VERSION_1,
143 "Skein Software Provider",
144 CRYPTO_SW_PROVIDER,
145 NULL,
146 &skein_crypto_ops,
147 sizeof (skein_mech_info_tab) / sizeof (crypto_mech_info_t),
148 skein_mech_info_tab
149}}}};
150
151static crypto_kcf_provider_handle_t skein_prov_handle = 0;
152
153typedef struct skein_ctx {
154 skein_mech_type_t sc_mech_type;
155 size_t sc_digest_bitlen;
156 /*LINTED(E_ANONYMOUS_UNION_DECL)*/
157 union {
158 Skein_256_Ctxt_t sc_256;
159 Skein_512_Ctxt_t sc_512;
160 Skein1024_Ctxt_t sc_1024;
161 };
162} skein_ctx_t;
163#define SKEIN_CTX(_ctx_) ((skein_ctx_t *)((_ctx_)->cc_provider_private))
164#define SKEIN_CTX_LVALUE(_ctx_) (_ctx_)->cc_provider_private
165#define SKEIN_OP(_skein_ctx, _op, ...) \
166 do { \
167 skein_ctx_t *sc = (_skein_ctx); \
168 switch (sc->sc_mech_type) { \
169 case SKEIN_256_MECH_INFO_TYPE: \
170 case SKEIN_256_MAC_MECH_INFO_TYPE: \
171 (void) Skein_256_ ## _op(&sc->sc_256, __VA_ARGS__);\
172 break; \
173 case SKEIN_512_MECH_INFO_TYPE: \
174 case SKEIN_512_MAC_MECH_INFO_TYPE: \
175 (void) Skein_512_ ## _op(&sc->sc_512, __VA_ARGS__);\
176 break; \
177 case SKEIN1024_MECH_INFO_TYPE: \
178 case SKEIN1024_MAC_MECH_INFO_TYPE: \
179 (void) Skein1024_ ## _op(&sc->sc_1024, __VA_ARGS__);\
180 break; \
181 } \
182 _NOTE(CONSTCOND) \
183 } while (0)
184
185static int
186skein_get_digest_bitlen(const crypto_mechanism_t *mechanism, size_t *result)
187{
188 if (mechanism->cm_param != NULL) {
189 /*LINTED(E_BAD_PTR_CAST_ALIGN)*/
190 skein_param_t *param = (skein_param_t *)mechanism->cm_param;
191
192 if (mechanism->cm_param_len != sizeof (*param) ||
193 param->sp_digest_bitlen == 0) {
194 return (CRYPTO_MECHANISM_PARAM_INVALID);
195 }
196 *result = param->sp_digest_bitlen;
197 } else {
198 switch (mechanism->cm_type) {
199 case SKEIN_256_MECH_INFO_TYPE:
200 *result = 256;
201 break;
202 case SKEIN_512_MECH_INFO_TYPE:
203 *result = 512;
204 break;
205 case SKEIN1024_MECH_INFO_TYPE:
206 *result = 1024;
207 break;
208 default:
209 return (CRYPTO_MECHANISM_INVALID);
210 }
211 }
212 return (CRYPTO_SUCCESS);
213}
214
215int
216skein_mod_init(void)
217{
218 int error;
219
220 if ((error = mod_install(&modlinkage)) != 0)
221 return (error);
222
223 /*
224 * Try to register with KCF - failure shouldn't unload us, since we
225 * still may want to continue providing misc/skein functionality.
226 */
227 (void) crypto_register_provider(&skein_prov_info, &skein_prov_handle);
228
229 return (0);
230}
231
232int
4ea3f864
GM
233skein_mod_fini(void)
234{
ef78750d
TC
235 int ret;
236
237 if (skein_prov_handle != 0) {
238 if ((ret = crypto_unregister_provider(skein_prov_handle)) !=
239 CRYPTO_SUCCESS) {
240 cmn_err(CE_WARN,
241 "skein _fini: crypto_unregister_provider() "
242 "failed (0x%x)", ret);
243 return (EBUSY);
244 }
245 skein_prov_handle = 0;
246 }
247
3c67d83a
TH
248 return (mod_remove(&modlinkage));
249}
250
251/*
252 * KCF software provider control entry points.
253 */
254/* ARGSUSED */
255static void
256skein_provider_status(crypto_provider_handle_t provider, uint_t *status)
257{
258 *status = CRYPTO_PROVIDER_READY;
259}
260
261/*
262 * General Skein hashing helper functions.
263 */
264
265/*
266 * Performs an Update on a context with uio input data.
267 */
268static int
269skein_digest_update_uio(skein_ctx_t *ctx, const crypto_data_t *data)
270{
271 off_t offset = data->cd_offset;
272 size_t length = data->cd_length;
273 uint_t vec_idx;
274 size_t cur_len;
275 const uio_t *uio = data->cd_uio;
276
277 /* we support only kernel buffer */
278 if (uio->uio_segflg != UIO_SYSSPACE)
279 return (CRYPTO_ARGUMENTS_BAD);
280
281 /*
282 * Jump to the first iovec containing data to be
283 * digested.
284 */
285 for (vec_idx = 0; vec_idx < uio->uio_iovcnt &&
286 offset >= uio->uio_iov[vec_idx].iov_len;
287 offset -= uio->uio_iov[vec_idx++].iov_len)
288 ;
289 if (vec_idx == uio->uio_iovcnt) {
290 /*
291 * The caller specified an offset that is larger than the
292 * total size of the buffers it provided.
293 */
294 return (CRYPTO_DATA_LEN_RANGE);
295 }
296
297 /*
298 * Now do the digesting on the iovecs.
299 */
300 while (vec_idx < uio->uio_iovcnt && length > 0) {
301 cur_len = MIN(uio->uio_iov[vec_idx].iov_len - offset, length);
302 SKEIN_OP(ctx, Update, (uint8_t *)uio->uio_iov[vec_idx].iov_base
303 + offset, cur_len);
304 length -= cur_len;
305 vec_idx++;
306 offset = 0;
307 }
308
309 if (vec_idx == uio->uio_iovcnt && length > 0) {
310 /*
311 * The end of the specified iovec's was reached but
312 * the length requested could not be processed, i.e.
313 * The caller requested to digest more data than it provided.
314 */
315 return (CRYPTO_DATA_LEN_RANGE);
316 }
317
318 return (CRYPTO_SUCCESS);
319}
320
321/*
322 * Performs a Final on a context and writes to a uio digest output.
323 */
324static int
325skein_digest_final_uio(skein_ctx_t *ctx, crypto_data_t *digest,
326 crypto_req_handle_t req)
327{
328 off_t offset = digest->cd_offset;
329 uint_t vec_idx;
330 uio_t *uio = digest->cd_uio;
331
332 /* we support only kernel buffer */
333 if (uio->uio_segflg != UIO_SYSSPACE)
334 return (CRYPTO_ARGUMENTS_BAD);
335
336 /*
337 * Jump to the first iovec containing ptr to the digest to be returned.
338 */
339 for (vec_idx = 0; offset >= uio->uio_iov[vec_idx].iov_len &&
340 vec_idx < uio->uio_iovcnt;
341 offset -= uio->uio_iov[vec_idx++].iov_len)
342 ;
343 if (vec_idx == uio->uio_iovcnt) {
344 /*
345 * The caller specified an offset that is larger than the
346 * total size of the buffers it provided.
347 */
348 return (CRYPTO_DATA_LEN_RANGE);
349 }
350 if (offset + CRYPTO_BITS2BYTES(ctx->sc_digest_bitlen) <=
351 uio->uio_iov[vec_idx].iov_len) {
352 /* The computed digest will fit in the current iovec. */
353 SKEIN_OP(ctx, Final,
354 (uchar_t *)uio->uio_iov[vec_idx].iov_base + offset);
355 } else {
356 uint8_t *digest_tmp;
357 off_t scratch_offset = 0;
358 size_t length = CRYPTO_BITS2BYTES(ctx->sc_digest_bitlen);
359 size_t cur_len;
360
361 digest_tmp = kmem_alloc(CRYPTO_BITS2BYTES(
362 ctx->sc_digest_bitlen), crypto_kmflag(req));
363 if (digest_tmp == NULL)
364 return (CRYPTO_HOST_MEMORY);
365 SKEIN_OP(ctx, Final, digest_tmp);
366 while (vec_idx < uio->uio_iovcnt && length > 0) {
367 cur_len = MIN(uio->uio_iov[vec_idx].iov_len - offset,
368 length);
369 bcopy(digest_tmp + scratch_offset,
370 uio->uio_iov[vec_idx].iov_base + offset, cur_len);
371
372 length -= cur_len;
373 vec_idx++;
374 scratch_offset += cur_len;
375 offset = 0;
376 }
377 kmem_free(digest_tmp, CRYPTO_BITS2BYTES(ctx->sc_digest_bitlen));
378
379 if (vec_idx == uio->uio_iovcnt && length > 0) {
380 /*
381 * The end of the specified iovec's was reached but
382 * the length requested could not be processed, i.e.
383 * The caller requested to digest more data than it
384 * provided.
385 */
386 return (CRYPTO_DATA_LEN_RANGE);
387 }
388 }
389
390 return (CRYPTO_SUCCESS);
391}
392
393/*
394 * KCF software provider digest entry points.
395 */
396
397/*
398 * Initializes a skein digest context to the configuration in `mechanism'.
399 * The mechanism cm_type must be one of SKEIN_*_MECH_INFO_TYPE. The cm_param
400 * field may contain a skein_param_t structure indicating the length of the
401 * digest the algorithm should produce. Otherwise the default output lengths
402 * are applied (32 bytes for Skein-256, 64 bytes for Skein-512 and 128 bytes
403 * for Skein-1024).
404 */
405static int
406skein_digest_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
407 crypto_req_handle_t req)
408{
409 int error = CRYPTO_SUCCESS;
410
411 if (!VALID_SKEIN_DIGEST_MECH(mechanism->cm_type))
412 return (CRYPTO_MECHANISM_INVALID);
413
414 SKEIN_CTX_LVALUE(ctx) = kmem_alloc(sizeof (*SKEIN_CTX(ctx)),
415 crypto_kmflag(req));
416 if (SKEIN_CTX(ctx) == NULL)
417 return (CRYPTO_HOST_MEMORY);
418
419 SKEIN_CTX(ctx)->sc_mech_type = mechanism->cm_type;
420 error = skein_get_digest_bitlen(mechanism,
421 &SKEIN_CTX(ctx)->sc_digest_bitlen);
422 if (error != CRYPTO_SUCCESS)
423 goto errout;
424 SKEIN_OP(SKEIN_CTX(ctx), Init, SKEIN_CTX(ctx)->sc_digest_bitlen);
425
426 return (CRYPTO_SUCCESS);
427errout:
428 bzero(SKEIN_CTX(ctx), sizeof (*SKEIN_CTX(ctx)));
429 kmem_free(SKEIN_CTX(ctx), sizeof (*SKEIN_CTX(ctx)));
430 SKEIN_CTX_LVALUE(ctx) = NULL;
431 return (error);
432}
433
434/*
435 * Executes a skein_update and skein_digest on a pre-initialized crypto
436 * context in a single step. See the documentation to these functions to
437 * see what to pass here.
438 */
439static int
440skein_digest(crypto_ctx_t *ctx, crypto_data_t *data, crypto_data_t *digest,
441 crypto_req_handle_t req)
442{
443 int error = CRYPTO_SUCCESS;
444
445 ASSERT(SKEIN_CTX(ctx) != NULL);
446
447 if (digest->cd_length <
448 CRYPTO_BITS2BYTES(SKEIN_CTX(ctx)->sc_digest_bitlen)) {
449 digest->cd_length =
450 CRYPTO_BITS2BYTES(SKEIN_CTX(ctx)->sc_digest_bitlen);
451 return (CRYPTO_BUFFER_TOO_SMALL);
452 }
453
454 error = skein_update(ctx, data, req);
455 if (error != CRYPTO_SUCCESS) {
456 bzero(SKEIN_CTX(ctx), sizeof (*SKEIN_CTX(ctx)));
457 kmem_free(SKEIN_CTX(ctx), sizeof (*SKEIN_CTX(ctx)));
458 SKEIN_CTX_LVALUE(ctx) = NULL;
459 digest->cd_length = 0;
460 return (error);
461 }
462 error = skein_final(ctx, digest, req);
463
464 return (error);
465}
466
467/*
468 * Performs a skein Update with the input message in `data' (successive calls
469 * can push more data). This is used both for digest and MAC operation.
470 * Supported input data formats are raw, uio and mblk.
471 */
472/*ARGSUSED*/
473static int
474skein_update(crypto_ctx_t *ctx, crypto_data_t *data, crypto_req_handle_t req)
475{
476 int error = CRYPTO_SUCCESS;
477
478 ASSERT(SKEIN_CTX(ctx) != NULL);
479
480 switch (data->cd_format) {
481 case CRYPTO_DATA_RAW:
482 SKEIN_OP(SKEIN_CTX(ctx), Update,
483 (uint8_t *)data->cd_raw.iov_base + data->cd_offset,
484 data->cd_length);
485 break;
486 case CRYPTO_DATA_UIO:
487 error = skein_digest_update_uio(SKEIN_CTX(ctx), data);
488 break;
489 default:
490 error = CRYPTO_ARGUMENTS_BAD;
491 }
492
493 return (error);
494}
495
496/*
497 * Performs a skein Final, writing the output to `digest'. This is used both
498 * for digest and MAC operation.
499 * Supported output digest formats are raw, uio and mblk.
500 */
501/*ARGSUSED*/
502static int
503skein_final(crypto_ctx_t *ctx, crypto_data_t *digest, crypto_req_handle_t req)
504{
505 int error = CRYPTO_SUCCESS;
506
507 ASSERT(SKEIN_CTX(ctx) != NULL);
508
509 if (digest->cd_length <
510 CRYPTO_BITS2BYTES(SKEIN_CTX(ctx)->sc_digest_bitlen)) {
511 digest->cd_length =
512 CRYPTO_BITS2BYTES(SKEIN_CTX(ctx)->sc_digest_bitlen);
513 return (CRYPTO_BUFFER_TOO_SMALL);
514 }
515
516 switch (digest->cd_format) {
517 case CRYPTO_DATA_RAW:
518 SKEIN_OP(SKEIN_CTX(ctx), Final,
519 (uint8_t *)digest->cd_raw.iov_base + digest->cd_offset);
520 break;
521 case CRYPTO_DATA_UIO:
522 error = skein_digest_final_uio(SKEIN_CTX(ctx), digest, req);
523 break;
524 default:
525 error = CRYPTO_ARGUMENTS_BAD;
526 }
527
528 if (error == CRYPTO_SUCCESS)
529 digest->cd_length =
530 CRYPTO_BITS2BYTES(SKEIN_CTX(ctx)->sc_digest_bitlen);
531 else
532 digest->cd_length = 0;
533
534 bzero(SKEIN_CTX(ctx), sizeof (*SKEIN_CTX(ctx)));
535 kmem_free(SKEIN_CTX(ctx), sizeof (*(SKEIN_CTX(ctx))));
536 SKEIN_CTX_LVALUE(ctx) = NULL;
537
538 return (error);
539}
540
541/*
542 * Performs a full skein digest computation in a single call, configuring the
543 * algorithm according to `mechanism', reading the input to be digested from
544 * `data' and writing the output to `digest'.
545 * Supported input/output formats are raw, uio and mblk.
546 */
547/*ARGSUSED*/
548static int
549skein_digest_atomic(crypto_provider_handle_t provider,
550 crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
551 crypto_data_t *data, crypto_data_t *digest, crypto_req_handle_t req)
552{
553 int error;
554 skein_ctx_t skein_ctx;
555 crypto_ctx_t ctx;
556 SKEIN_CTX_LVALUE(&ctx) = &skein_ctx;
557
558 /* Init */
559 if (!VALID_SKEIN_DIGEST_MECH(mechanism->cm_type))
560 return (CRYPTO_MECHANISM_INVALID);
561 skein_ctx.sc_mech_type = mechanism->cm_type;
562 error = skein_get_digest_bitlen(mechanism, &skein_ctx.sc_digest_bitlen);
563 if (error != CRYPTO_SUCCESS)
564 goto out;
565 SKEIN_OP(&skein_ctx, Init, skein_ctx.sc_digest_bitlen);
566
567 if ((error = skein_update(&ctx, data, digest)) != CRYPTO_SUCCESS)
568 goto out;
569 if ((error = skein_final(&ctx, data, digest)) != CRYPTO_SUCCESS)
570 goto out;
571
572out:
573 if (error == CRYPTO_SUCCESS)
574 digest->cd_length =
575 CRYPTO_BITS2BYTES(skein_ctx.sc_digest_bitlen);
576 else
577 digest->cd_length = 0;
578 bzero(&skein_ctx, sizeof (skein_ctx));
579
580 return (error);
581}
582
583/*
584 * Helper function that builds a Skein MAC context from the provided
585 * mechanism and key.
586 */
587static int
588skein_mac_ctx_build(skein_ctx_t *ctx, crypto_mechanism_t *mechanism,
589 crypto_key_t *key)
590{
591 int error;
592
593 if (!VALID_SKEIN_MAC_MECH(mechanism->cm_type))
594 return (CRYPTO_MECHANISM_INVALID);
595 if (key->ck_format != CRYPTO_KEY_RAW)
596 return (CRYPTO_ARGUMENTS_BAD);
597 ctx->sc_mech_type = mechanism->cm_type;
598 error = skein_get_digest_bitlen(mechanism, &ctx->sc_digest_bitlen);
599 if (error != CRYPTO_SUCCESS)
600 return (error);
601 SKEIN_OP(ctx, InitExt, ctx->sc_digest_bitlen, 0, key->ck_data,
602 CRYPTO_BITS2BYTES(key->ck_length));
603
604 return (CRYPTO_SUCCESS);
605}
606
607/*
608 * KCF software provide mac entry points.
609 */
610/*
611 * Initializes a skein MAC context. You may pass a ctx_template, in which
612 * case the template will be reused to make initialization more efficient.
613 * Otherwise a new context will be constructed. The mechanism cm_type must
614 * be one of SKEIN_*_MAC_MECH_INFO_TYPE. Same as in skein_digest_init, you
615 * may pass a skein_param_t in cm_param to configure the length of the
616 * digest. The key must be in raw format.
617 */
618static int
619skein_mac_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
620 crypto_key_t *key, crypto_spi_ctx_template_t ctx_template,
621 crypto_req_handle_t req)
622{
623 int error;
624
625 SKEIN_CTX_LVALUE(ctx) = kmem_alloc(sizeof (*SKEIN_CTX(ctx)),
626 crypto_kmflag(req));
627 if (SKEIN_CTX(ctx) == NULL)
628 return (CRYPTO_HOST_MEMORY);
629
630 if (ctx_template != NULL) {
631 bcopy(ctx_template, SKEIN_CTX(ctx),
632 sizeof (*SKEIN_CTX(ctx)));
633 } else {
634 error = skein_mac_ctx_build(SKEIN_CTX(ctx), mechanism, key);
635 if (error != CRYPTO_SUCCESS)
636 goto errout;
637 }
638
639 return (CRYPTO_SUCCESS);
640errout:
641 bzero(SKEIN_CTX(ctx), sizeof (*SKEIN_CTX(ctx)));
642 kmem_free(SKEIN_CTX(ctx), sizeof (*SKEIN_CTX(ctx)));
643 return (error);
644}
645
646/*
647 * The MAC update and final calls are reused from the regular digest code.
648 */
649
650/*ARGSUSED*/
651/*
652 * Same as skein_digest_atomic, performs an atomic Skein MAC operation in
653 * one step. All the same properties apply to the arguments of this
654 * function as to those of the partial operations above.
655 */
656static int
657skein_mac_atomic(crypto_provider_handle_t provider,
658 crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
659 crypto_key_t *key, crypto_data_t *data, crypto_data_t *mac,
660 crypto_spi_ctx_template_t ctx_template, crypto_req_handle_t req)
661{
662 /* faux crypto context just for skein_digest_{update,final} */
663 int error;
664 crypto_ctx_t ctx;
665 skein_ctx_t skein_ctx;
666 SKEIN_CTX_LVALUE(&ctx) = &skein_ctx;
667
668 if (ctx_template != NULL) {
669 bcopy(ctx_template, &skein_ctx, sizeof (skein_ctx));
670 } else {
671 error = skein_mac_ctx_build(&skein_ctx, mechanism, key);
672 if (error != CRYPTO_SUCCESS)
673 goto errout;
674 }
675
676 if ((error = skein_update(&ctx, data, req)) != CRYPTO_SUCCESS)
677 goto errout;
678 if ((error = skein_final(&ctx, mac, req)) != CRYPTO_SUCCESS)
679 goto errout;
680
681 return (CRYPTO_SUCCESS);
682errout:
683 bzero(&skein_ctx, sizeof (skein_ctx));
684 return (error);
685}
686
687/*
688 * KCF software provider context management entry points.
689 */
690
691/*
692 * Constructs a context template for the Skein MAC algorithm. The same
693 * properties apply to the arguments of this function as to those of
694 * skein_mac_init.
695 */
696/*ARGSUSED*/
697static int
698skein_create_ctx_template(crypto_provider_handle_t provider,
699 crypto_mechanism_t *mechanism, crypto_key_t *key,
700 crypto_spi_ctx_template_t *ctx_template, size_t *ctx_template_size,
701 crypto_req_handle_t req)
702{
703 int error;
704 skein_ctx_t *ctx_tmpl;
705
706 ctx_tmpl = kmem_alloc(sizeof (*ctx_tmpl), crypto_kmflag(req));
707 if (ctx_tmpl == NULL)
708 return (CRYPTO_HOST_MEMORY);
709 error = skein_mac_ctx_build(ctx_tmpl, mechanism, key);
710 if (error != CRYPTO_SUCCESS)
711 goto errout;
712 *ctx_template = ctx_tmpl;
713 *ctx_template_size = sizeof (*ctx_tmpl);
714
715 return (CRYPTO_SUCCESS);
716errout:
717 bzero(ctx_tmpl, sizeof (*ctx_tmpl));
718 kmem_free(ctx_tmpl, sizeof (*ctx_tmpl));
719 return (error);
720}
721
722/*
723 * Frees a skein context in a parent crypto context.
724 */
725static int
726skein_free_context(crypto_ctx_t *ctx)
727{
728 if (SKEIN_CTX(ctx) != NULL) {
729 bzero(SKEIN_CTX(ctx), sizeof (*SKEIN_CTX(ctx)));
730 kmem_free(SKEIN_CTX(ctx), sizeof (*SKEIN_CTX(ctx)));
731 SKEIN_CTX_LVALUE(ctx) = NULL;
732 }
733
734 return (CRYPTO_SUCCESS);
735}