4185 add new cryptographic checksums to ZFS: SHA-512, Skein, Edon-R
[unleashed.git] / usr / src / uts / common / crypto / io / skein_mod.c
blobaca7582dcbace475decf51520d407a9615eb40c9
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2013 Saso Kiselkov. All rights reserved.
26 #include <sys/modctl.h>
27 #include <sys/crypto/common.h>
28 #include <sys/crypto/spi.h>
29 #include <sys/strsun.h>
30 #include <sys/sysmacros.h>
31 #include <sys/systm.h>
32 #define SKEIN_MODULE_IMPL
33 #include <sys/skein.h>
36 * Like the sha2 module, we create the skein module with two modlinkages:
37 * - modlmisc to allow direct calls to Skein_* API functions.
38 * - modlcrypto to integrate well into the Kernel Crypto Framework (KCF).
40 static struct modlmisc modlmisc = {
41 &mod_miscops,
42 "Skein Message-Digest Algorithm"
45 static struct modlcrypto modlcrypto = {
46 &mod_cryptoops,
47 "Skein Kernel SW Provider"
50 static struct modlinkage modlinkage = {
51 MODREV_1, &modlmisc, &modlcrypto, NULL
54 static crypto_mech_info_t skein_mech_info_tab[] = {
55 {CKM_SKEIN_256, SKEIN_256_MECH_INFO_TYPE,
56 CRYPTO_FG_DIGEST | CRYPTO_FG_DIGEST_ATOMIC,
57 0, 0, CRYPTO_KEYSIZE_UNIT_IN_BITS},
58 {CKM_SKEIN_256_MAC, SKEIN_256_MAC_MECH_INFO_TYPE,
59 CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC, 1, INT_MAX,
60 CRYPTO_KEYSIZE_UNIT_IN_BYTES},
61 {CKM_SKEIN_512, SKEIN_512_MECH_INFO_TYPE,
62 CRYPTO_FG_DIGEST | CRYPTO_FG_DIGEST_ATOMIC,
63 0, 0, CRYPTO_KEYSIZE_UNIT_IN_BITS},
64 {CKM_SKEIN_512_MAC, SKEIN_512_MAC_MECH_INFO_TYPE,
65 CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC, 1, INT_MAX,
66 CRYPTO_KEYSIZE_UNIT_IN_BYTES},
67 {CKM_SKEIN1024, SKEIN1024_MECH_INFO_TYPE,
68 CRYPTO_FG_DIGEST | CRYPTO_FG_DIGEST_ATOMIC,
69 0, 0, CRYPTO_KEYSIZE_UNIT_IN_BITS},
70 {CKM_SKEIN1024_MAC, SKEIN1024_MAC_MECH_INFO_TYPE,
71 CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC, 1, INT_MAX,
72 CRYPTO_KEYSIZE_UNIT_IN_BYTES}
75 static void skein_provider_status(crypto_provider_handle_t, uint_t *);
77 static crypto_control_ops_t skein_control_ops = {
78 skein_provider_status
81 static int skein_digest_init(crypto_ctx_t *, crypto_mechanism_t *,
82 crypto_req_handle_t);
83 static int skein_digest(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
84 crypto_req_handle_t);
85 static int skein_update(crypto_ctx_t *, crypto_data_t *, crypto_req_handle_t);
86 static int skein_final(crypto_ctx_t *, crypto_data_t *, crypto_req_handle_t);
87 static int skein_digest_atomic(crypto_provider_handle_t, crypto_session_id_t,
88 crypto_mechanism_t *, crypto_data_t *, crypto_data_t *,
89 crypto_req_handle_t);
91 static crypto_digest_ops_t skein_digest_ops = {
92 skein_digest_init,
93 skein_digest,
94 skein_update,
95 NULL,
96 skein_final,
97 skein_digest_atomic
100 static int skein_mac_init(crypto_ctx_t *, crypto_mechanism_t *, crypto_key_t *,
101 crypto_spi_ctx_template_t, crypto_req_handle_t);
102 static int skein_mac_atomic(crypto_provider_handle_t, crypto_session_id_t,
103 crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, crypto_data_t *,
104 crypto_spi_ctx_template_t, crypto_req_handle_t);
106 static crypto_mac_ops_t skein_mac_ops = {
107 skein_mac_init,
108 NULL,
109 skein_update, /* using regular digest update is OK here */
110 skein_final, /* using regular digest final is OK here */
111 skein_mac_atomic,
112 NULL
115 static int skein_create_ctx_template(crypto_provider_handle_t,
116 crypto_mechanism_t *, crypto_key_t *, crypto_spi_ctx_template_t *,
117 size_t *, crypto_req_handle_t);
118 static int skein_free_context(crypto_ctx_t *);
120 static crypto_ctx_ops_t skein_ctx_ops = {
121 skein_create_ctx_template,
122 skein_free_context
125 static crypto_ops_t skein_crypto_ops = {
126 &skein_control_ops,
127 &skein_digest_ops,
128 NULL,
129 &skein_mac_ops,
130 NULL,
131 NULL,
132 NULL,
133 NULL,
134 NULL,
135 NULL,
136 NULL,
137 NULL,
138 NULL,
139 &skein_ctx_ops,
140 NULL,
141 NULL,
142 NULL
145 static crypto_provider_info_t skein_prov_info = {
146 CRYPTO_SPI_VERSION_4,
147 "Skein Software Provider",
148 CRYPTO_SW_PROVIDER,
149 {&modlinkage},
150 NULL,
151 &skein_crypto_ops,
152 sizeof (skein_mech_info_tab) / sizeof (crypto_mech_info_t),
153 skein_mech_info_tab
156 static crypto_kcf_provider_handle_t skein_prov_handle = NULL;
158 typedef struct skein_ctx {
159 skein_mech_type_t sc_mech_type;
160 size_t sc_digest_bitlen;
161 /*LINTED(E_ANONYMOUS_UNION_DECL)*/
162 union {
163 Skein_256_Ctxt_t sc_256;
164 Skein_512_Ctxt_t sc_512;
165 Skein1024_Ctxt_t sc_1024;
167 } skein_ctx_t;
168 #define SKEIN_CTX(_ctx_) ((skein_ctx_t *)((_ctx_)->cc_provider_private))
169 #define SKEIN_CTX_LVALUE(_ctx_) (_ctx_)->cc_provider_private
170 #define SKEIN_OP(_skein_ctx, _op, ...) \
171 do { \
172 skein_ctx_t *sc = (_skein_ctx); \
173 switch (sc->sc_mech_type) { \
174 case SKEIN_256_MECH_INFO_TYPE: \
175 case SKEIN_256_MAC_MECH_INFO_TYPE: \
176 (void) Skein_256_ ## _op(&sc->sc_256, __VA_ARGS__);\
177 break; \
178 case SKEIN_512_MECH_INFO_TYPE: \
179 case SKEIN_512_MAC_MECH_INFO_TYPE: \
180 (void) Skein_512_ ## _op(&sc->sc_512, __VA_ARGS__);\
181 break; \
182 case SKEIN1024_MECH_INFO_TYPE: \
183 case SKEIN1024_MAC_MECH_INFO_TYPE: \
184 (void) Skein1024_ ## _op(&sc->sc_1024, __VA_ARGS__);\
185 break; \
187 _NOTE(CONSTCOND) \
188 } while (0)
190 static int
191 skein_get_digest_bitlen(const crypto_mechanism_t *mechanism, size_t *result)
193 if (mechanism->cm_param != NULL) {
194 /*LINTED(E_BAD_PTR_CAST_ALIGN)*/
195 skein_param_t *param = (skein_param_t *)mechanism->cm_param;
197 if (mechanism->cm_param_len != sizeof (*param) ||
198 param->sp_digest_bitlen == 0) {
199 return (CRYPTO_MECHANISM_PARAM_INVALID);
201 *result = param->sp_digest_bitlen;
202 } else {
203 switch (mechanism->cm_type) {
204 case SKEIN_256_MECH_INFO_TYPE:
205 *result = 256;
206 break;
207 case SKEIN_512_MECH_INFO_TYPE:
208 *result = 512;
209 break;
210 case SKEIN1024_MECH_INFO_TYPE:
211 *result = 1024;
212 break;
213 default:
214 return (CRYPTO_MECHANISM_INVALID);
217 return (CRYPTO_SUCCESS);
221 _init(void)
223 int error;
225 if ((error = mod_install(&modlinkage)) != 0)
226 return (error);
229 * Try to register with KCF - failure shouldn't unload us, since we
230 * still may want to continue providing misc/skein functionality.
232 (void) crypto_register_provider(&skein_prov_info, &skein_prov_handle);
234 return (0);
238 _info(struct modinfo *modinfop)
240 return (mod_info(&modlinkage, modinfop));
244 * KCF software provider control entry points.
246 /* ARGSUSED */
247 static void
248 skein_provider_status(crypto_provider_handle_t provider, uint_t *status)
250 *status = CRYPTO_PROVIDER_READY;
254 * General Skein hashing helper functions.
258 * Performs an Update on a context with uio input data.
260 static int
261 skein_digest_update_uio(skein_ctx_t *ctx, const crypto_data_t *data)
263 off_t offset = data->cd_offset;
264 size_t length = data->cd_length;
265 uint_t vec_idx;
266 size_t cur_len;
267 const uio_t *uio = data->cd_uio;
269 /* we support only kernel buffer */
270 if (uio->uio_segflg != UIO_SYSSPACE)
271 return (CRYPTO_ARGUMENTS_BAD);
274 * Jump to the first iovec containing data to be
275 * digested.
277 for (vec_idx = 0; vec_idx < uio->uio_iovcnt &&
278 offset >= uio->uio_iov[vec_idx].iov_len;
279 offset -= uio->uio_iov[vec_idx++].iov_len)
281 if (vec_idx == uio->uio_iovcnt) {
283 * The caller specified an offset that is larger than the
284 * total size of the buffers it provided.
286 return (CRYPTO_DATA_LEN_RANGE);
290 * Now do the digesting on the iovecs.
292 while (vec_idx < uio->uio_iovcnt && length > 0) {
293 cur_len = MIN(uio->uio_iov[vec_idx].iov_len - offset, length);
294 SKEIN_OP(ctx, Update, (uint8_t *)uio->uio_iov[vec_idx].iov_base
295 + offset, cur_len);
296 length -= cur_len;
297 vec_idx++;
298 offset = 0;
301 if (vec_idx == uio->uio_iovcnt && length > 0) {
303 * The end of the specified iovec's was reached but
304 * the length requested could not be processed, i.e.
305 * The caller requested to digest more data than it provided.
307 return (CRYPTO_DATA_LEN_RANGE);
310 return (CRYPTO_SUCCESS);
314 * Performs a Final on a context and writes to a uio digest output.
316 static int
317 skein_digest_final_uio(skein_ctx_t *ctx, crypto_data_t *digest,
318 crypto_req_handle_t req)
320 off_t offset = digest->cd_offset;
321 uint_t vec_idx;
322 uio_t *uio = digest->cd_uio;
324 /* we support only kernel buffer */
325 if (uio->uio_segflg != UIO_SYSSPACE)
326 return (CRYPTO_ARGUMENTS_BAD);
329 * Jump to the first iovec containing ptr to the digest to be returned.
331 for (vec_idx = 0; offset >= uio->uio_iov[vec_idx].iov_len &&
332 vec_idx < uio->uio_iovcnt;
333 offset -= uio->uio_iov[vec_idx++].iov_len)
335 if (vec_idx == uio->uio_iovcnt) {
337 * The caller specified an offset that is larger than the
338 * total size of the buffers it provided.
340 return (CRYPTO_DATA_LEN_RANGE);
342 if (offset + CRYPTO_BITS2BYTES(ctx->sc_digest_bitlen) <=
343 uio->uio_iov[vec_idx].iov_len) {
344 /* The computed digest will fit in the current iovec. */
345 SKEIN_OP(ctx, Final,
346 (uchar_t *)uio->uio_iov[vec_idx].iov_base + offset);
347 } else {
348 uint8_t *digest_tmp;
349 off_t scratch_offset = 0;
350 size_t length = CRYPTO_BITS2BYTES(ctx->sc_digest_bitlen);
351 size_t cur_len;
353 digest_tmp = kmem_alloc(CRYPTO_BITS2BYTES(
354 ctx->sc_digest_bitlen), crypto_kmflag(req));
355 if (digest_tmp == NULL)
356 return (CRYPTO_HOST_MEMORY);
357 SKEIN_OP(ctx, Final, digest_tmp);
358 while (vec_idx < uio->uio_iovcnt && length > 0) {
359 cur_len = MIN(uio->uio_iov[vec_idx].iov_len - offset,
360 length);
361 bcopy(digest_tmp + scratch_offset,
362 uio->uio_iov[vec_idx].iov_base + offset, cur_len);
364 length -= cur_len;
365 vec_idx++;
366 scratch_offset += cur_len;
367 offset = 0;
369 kmem_free(digest_tmp, CRYPTO_BITS2BYTES(ctx->sc_digest_bitlen));
371 if (vec_idx == uio->uio_iovcnt && length > 0) {
373 * The end of the specified iovec's was reached but
374 * the length requested could not be processed, i.e.
375 * The caller requested to digest more data than it
376 * provided.
378 return (CRYPTO_DATA_LEN_RANGE);
382 return (CRYPTO_SUCCESS);
386 * Performs an Update on a context with mblk input data.
388 static int
389 skein_digest_update_mblk(skein_ctx_t *ctx, crypto_data_t *data)
391 off_t offset = data->cd_offset;
392 size_t length = data->cd_length;
393 mblk_t *mp;
394 size_t cur_len;
396 /* Jump to the first mblk_t containing data to be digested. */
397 for (mp = data->cd_mp; mp != NULL && offset >= MBLKL(mp);
398 offset -= MBLKL(mp), mp = mp->b_cont)
400 if (mp == NULL) {
402 * The caller specified an offset that is larger than the
403 * total size of the buffers it provided.
405 return (CRYPTO_DATA_LEN_RANGE);
408 /* Now do the digesting on the mblk chain. */
409 while (mp != NULL && length > 0) {
410 cur_len = MIN(MBLKL(mp) - offset, length);
411 SKEIN_OP(ctx, Update, mp->b_rptr + offset, cur_len);
412 length -= cur_len;
413 offset = 0;
414 mp = mp->b_cont;
417 if (mp == NULL && length > 0) {
419 * The end of the mblk was reached but the length requested
420 * could not be processed, i.e. The caller requested
421 * to digest more data than it provided.
423 return (CRYPTO_DATA_LEN_RANGE);
426 return (CRYPTO_SUCCESS);
430 * Performs a Final on a context and writes to an mblk digest output.
432 static int
433 skein_digest_final_mblk(skein_ctx_t *ctx, crypto_data_t *digest,
434 crypto_req_handle_t req)
436 off_t offset = digest->cd_offset;
437 mblk_t *mp;
439 /* Jump to the first mblk_t that will be used to store the digest. */
440 for (mp = digest->cd_mp; mp != NULL && offset >= MBLKL(mp);
441 offset -= MBLKL(mp), mp = mp->b_cont)
443 if (mp == NULL) {
444 /* caller specified offset is too large */
445 return (CRYPTO_DATA_LEN_RANGE);
448 if (offset + CRYPTO_BITS2BYTES(ctx->sc_digest_bitlen) <= MBLKL(mp)) {
449 /* The digest will fit in the current mblk. */
450 SKEIN_OP(ctx, Final, mp->b_rptr + offset);
451 } else {
452 /* Split the digest up between the individual buffers. */
453 uint8_t *digest_tmp;
454 off_t scratch_offset = 0;
455 size_t length = CRYPTO_BITS2BYTES(ctx->sc_digest_bitlen);
456 size_t cur_len;
458 digest_tmp = kmem_alloc(CRYPTO_BITS2BYTES(
459 ctx->sc_digest_bitlen), crypto_kmflag(req));
460 if (digest_tmp == NULL)
461 return (CRYPTO_HOST_MEMORY);
462 SKEIN_OP(ctx, Final, digest_tmp);
463 while (mp != NULL && length > 0) {
464 cur_len = MIN(MBLKL(mp) - offset, length);
465 bcopy(digest_tmp + scratch_offset,
466 mp->b_rptr + offset, cur_len);
467 length -= cur_len;
468 mp = mp->b_cont;
469 scratch_offset += cur_len;
470 offset = 0;
472 kmem_free(digest_tmp, CRYPTO_BITS2BYTES(ctx->sc_digest_bitlen));
473 if (mp == NULL && length > 0) {
474 /* digest too long to fit in the mblk buffers */
475 return (CRYPTO_DATA_LEN_RANGE);
479 return (CRYPTO_SUCCESS);
483 * KCF software provider digest entry points.
487 * Initializes a skein digest context to the configuration in `mechanism'.
488 * The mechanism cm_type must be one of SKEIN_*_MECH_INFO_TYPE. The cm_param
489 * field may contain a skein_param_t structure indicating the length of the
490 * digest the algorithm should produce. Otherwise the default output lengths
491 * are applied (32 bytes for Skein-256, 64 bytes for Skein-512 and 128 bytes
492 * for Skein-1024).
494 static int
495 skein_digest_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
496 crypto_req_handle_t req)
498 int error = CRYPTO_SUCCESS;
500 if (!VALID_SKEIN_DIGEST_MECH(mechanism->cm_type))
501 return (CRYPTO_MECHANISM_INVALID);
503 SKEIN_CTX_LVALUE(ctx) = kmem_alloc(sizeof (*SKEIN_CTX(ctx)),
504 crypto_kmflag(req));
505 if (SKEIN_CTX(ctx) == NULL)
506 return (CRYPTO_HOST_MEMORY);
508 SKEIN_CTX(ctx)->sc_mech_type = mechanism->cm_type;
509 error = skein_get_digest_bitlen(mechanism,
510 &SKEIN_CTX(ctx)->sc_digest_bitlen);
511 if (error != CRYPTO_SUCCESS)
512 goto errout;
513 SKEIN_OP(SKEIN_CTX(ctx), Init, SKEIN_CTX(ctx)->sc_digest_bitlen);
515 return (CRYPTO_SUCCESS);
516 errout:
517 bzero(SKEIN_CTX(ctx), sizeof (*SKEIN_CTX(ctx)));
518 kmem_free(SKEIN_CTX(ctx), sizeof (*SKEIN_CTX(ctx)));
519 SKEIN_CTX_LVALUE(ctx) = NULL;
520 return (error);
524 * Executes a skein_update and skein_digest on a pre-initialized crypto
525 * context in a single step. See the documentation to these functions to
526 * see what to pass here.
528 static int
529 skein_digest(crypto_ctx_t *ctx, crypto_data_t *data, crypto_data_t *digest,
530 crypto_req_handle_t req)
532 int error = CRYPTO_SUCCESS;
534 ASSERT(SKEIN_CTX(ctx) != NULL);
536 if (digest->cd_length <
537 CRYPTO_BITS2BYTES(SKEIN_CTX(ctx)->sc_digest_bitlen)) {
538 digest->cd_length =
539 CRYPTO_BITS2BYTES(SKEIN_CTX(ctx)->sc_digest_bitlen);
540 return (CRYPTO_BUFFER_TOO_SMALL);
543 error = skein_update(ctx, data, req);
544 if (error != CRYPTO_SUCCESS) {
545 bzero(SKEIN_CTX(ctx), sizeof (*SKEIN_CTX(ctx)));
546 kmem_free(SKEIN_CTX(ctx), sizeof (*SKEIN_CTX(ctx)));
547 SKEIN_CTX_LVALUE(ctx) = NULL;
548 digest->cd_length = 0;
549 return (error);
551 error = skein_final(ctx, digest, req);
553 return (error);
557 * Performs a skein Update with the input message in `data' (successive calls
558 * can push more data). This is used both for digest and MAC operation.
559 * Supported input data formats are raw, uio and mblk.
561 /*ARGSUSED*/
562 static int
563 skein_update(crypto_ctx_t *ctx, crypto_data_t *data, crypto_req_handle_t req)
565 int error = CRYPTO_SUCCESS;
567 ASSERT(SKEIN_CTX(ctx) != NULL);
569 switch (data->cd_format) {
570 case CRYPTO_DATA_RAW:
571 SKEIN_OP(SKEIN_CTX(ctx), Update,
572 (uint8_t *)data->cd_raw.iov_base + data->cd_offset,
573 data->cd_length);
574 break;
575 case CRYPTO_DATA_UIO:
576 error = skein_digest_update_uio(SKEIN_CTX(ctx), data);
577 break;
578 case CRYPTO_DATA_MBLK:
579 error = skein_digest_update_mblk(SKEIN_CTX(ctx), data);
580 break;
581 default:
582 error = CRYPTO_ARGUMENTS_BAD;
585 return (error);
589 * Performs a skein Final, writing the output to `digest'. This is used both
590 * for digest and MAC operation.
591 * Supported output digest formats are raw, uio and mblk.
593 /*ARGSUSED*/
594 static int
595 skein_final(crypto_ctx_t *ctx, crypto_data_t *digest, crypto_req_handle_t req)
597 int error = CRYPTO_SUCCESS;
599 ASSERT(SKEIN_CTX(ctx) != NULL);
601 if (digest->cd_length <
602 CRYPTO_BITS2BYTES(SKEIN_CTX(ctx)->sc_digest_bitlen)) {
603 digest->cd_length =
604 CRYPTO_BITS2BYTES(SKEIN_CTX(ctx)->sc_digest_bitlen);
605 return (CRYPTO_BUFFER_TOO_SMALL);
608 switch (digest->cd_format) {
609 case CRYPTO_DATA_RAW:
610 SKEIN_OP(SKEIN_CTX(ctx), Final,
611 (uint8_t *)digest->cd_raw.iov_base + digest->cd_offset);
612 break;
613 case CRYPTO_DATA_UIO:
614 error = skein_digest_final_uio(SKEIN_CTX(ctx), digest, req);
615 break;
616 case CRYPTO_DATA_MBLK:
617 error = skein_digest_final_mblk(SKEIN_CTX(ctx), digest, req);
618 break;
619 default:
620 error = CRYPTO_ARGUMENTS_BAD;
623 if (error == CRYPTO_SUCCESS)
624 digest->cd_length =
625 CRYPTO_BITS2BYTES(SKEIN_CTX(ctx)->sc_digest_bitlen);
626 else
627 digest->cd_length = 0;
629 bzero(SKEIN_CTX(ctx), sizeof (*SKEIN_CTX(ctx)));
630 kmem_free(SKEIN_CTX(ctx), sizeof (*(SKEIN_CTX(ctx))));
631 SKEIN_CTX_LVALUE(ctx) = NULL;
633 return (error);
637 * Performs a full skein digest computation in a single call, configuring the
638 * algorithm according to `mechanism', reading the input to be digested from
639 * `data' and writing the output to `digest'.
640 * Supported input/output formats are raw, uio and mblk.
642 /*ARGSUSED*/
643 static int
644 skein_digest_atomic(crypto_provider_handle_t provider,
645 crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
646 crypto_data_t *data, crypto_data_t *digest, crypto_req_handle_t req)
648 int error;
649 skein_ctx_t skein_ctx;
650 crypto_ctx_t ctx;
651 SKEIN_CTX_LVALUE(&ctx) = &skein_ctx;
653 /* Init */
654 if (!VALID_SKEIN_DIGEST_MECH(mechanism->cm_type))
655 return (CRYPTO_MECHANISM_INVALID);
656 skein_ctx.sc_mech_type = mechanism->cm_type;
657 error = skein_get_digest_bitlen(mechanism, &skein_ctx.sc_digest_bitlen);
658 if (error != CRYPTO_SUCCESS)
659 goto out;
660 SKEIN_OP(&skein_ctx, Init, skein_ctx.sc_digest_bitlen);
662 if ((error = skein_update(&ctx, data, digest)) != CRYPTO_SUCCESS)
663 goto out;
664 if ((error = skein_final(&ctx, data, digest)) != CRYPTO_SUCCESS)
665 goto out;
667 out:
668 if (error == CRYPTO_SUCCESS)
669 digest->cd_length =
670 CRYPTO_BITS2BYTES(skein_ctx.sc_digest_bitlen);
671 else
672 digest->cd_length = 0;
673 bzero(&skein_ctx, sizeof (skein_ctx));
675 return (error);
679 * Helper function that builds a Skein MAC context from the provided
680 * mechanism and key.
682 static int
683 skein_mac_ctx_build(skein_ctx_t *ctx, crypto_mechanism_t *mechanism,
684 crypto_key_t *key)
686 int error;
688 if (!VALID_SKEIN_MAC_MECH(mechanism->cm_type))
689 return (CRYPTO_MECHANISM_INVALID);
690 if (key->ck_format != CRYPTO_KEY_RAW)
691 return (CRYPTO_ARGUMENTS_BAD);
692 ctx->sc_mech_type = mechanism->cm_type;
693 error = skein_get_digest_bitlen(mechanism, &ctx->sc_digest_bitlen);
694 if (error != CRYPTO_SUCCESS)
695 return (error);
696 SKEIN_OP(ctx, InitExt, ctx->sc_digest_bitlen, 0, key->ck_data,
697 CRYPTO_BITS2BYTES(key->ck_length));
699 return (CRYPTO_SUCCESS);
703 * KCF software provide mac entry points.
706 * Initializes a skein MAC context. You may pass a ctx_template, in which
707 * case the template will be reused to make initialization more efficient.
708 * Otherwise a new context will be constructed. The mechanism cm_type must
709 * be one of SKEIN_*_MAC_MECH_INFO_TYPE. Same as in skein_digest_init, you
710 * may pass a skein_param_t in cm_param to configure the length of the
711 * digest. The key must be in raw format.
713 static int
714 skein_mac_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
715 crypto_key_t *key, crypto_spi_ctx_template_t ctx_template,
716 crypto_req_handle_t req)
718 int error;
720 SKEIN_CTX_LVALUE(ctx) = kmem_alloc(sizeof (*SKEIN_CTX(ctx)),
721 crypto_kmflag(req));
722 if (SKEIN_CTX(ctx) == NULL)
723 return (CRYPTO_HOST_MEMORY);
725 if (ctx_template != NULL) {
726 bcopy(ctx_template, SKEIN_CTX(ctx),
727 sizeof (*SKEIN_CTX(ctx)));
728 } else {
729 error = skein_mac_ctx_build(SKEIN_CTX(ctx), mechanism, key);
730 if (error != CRYPTO_SUCCESS)
731 goto errout;
734 return (CRYPTO_SUCCESS);
735 errout:
736 bzero(SKEIN_CTX(ctx), sizeof (*SKEIN_CTX(ctx)));
737 kmem_free(SKEIN_CTX(ctx), sizeof (*SKEIN_CTX(ctx)));
738 return (error);
742 * The MAC update and final calls are reused from the regular digest code.
745 /*ARGSUSED*/
747 * Same as skein_digest_atomic, performs an atomic Skein MAC operation in
748 * one step. All the same properties apply to the arguments of this
749 * function as to those of the partial operations above.
751 static int
752 skein_mac_atomic(crypto_provider_handle_t provider,
753 crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
754 crypto_key_t *key, crypto_data_t *data, crypto_data_t *mac,
755 crypto_spi_ctx_template_t ctx_template, crypto_req_handle_t req)
757 /* faux crypto context just for skein_digest_{update,final} */
758 int error;
759 crypto_ctx_t ctx;
760 skein_ctx_t skein_ctx;
761 SKEIN_CTX_LVALUE(&ctx) = &skein_ctx;
763 if (ctx_template != NULL) {
764 bcopy(ctx_template, &skein_ctx, sizeof (skein_ctx));
765 } else {
766 error = skein_mac_ctx_build(&skein_ctx, mechanism, key);
767 if (error != CRYPTO_SUCCESS)
768 goto errout;
771 if ((error = skein_update(&ctx, data, req)) != CRYPTO_SUCCESS)
772 goto errout;
773 if ((error = skein_final(&ctx, mac, req)) != CRYPTO_SUCCESS)
774 goto errout;
776 return (CRYPTO_SUCCESS);
777 errout:
778 bzero(&skein_ctx, sizeof (skein_ctx));
779 return (error);
783 * KCF software provider context management entry points.
787 * Constructs a context template for the Skein MAC algorithm. The same
788 * properties apply to the arguments of this function as to those of
789 * skein_mac_init.
791 /*ARGSUSED*/
792 static int
793 skein_create_ctx_template(crypto_provider_handle_t provider,
794 crypto_mechanism_t *mechanism, crypto_key_t *key,
795 crypto_spi_ctx_template_t *ctx_template, size_t *ctx_template_size,
796 crypto_req_handle_t req)
798 int error;
799 skein_ctx_t *ctx_tmpl;
801 ctx_tmpl = kmem_alloc(sizeof (*ctx_tmpl), crypto_kmflag(req));
802 if (ctx_tmpl == NULL)
803 return (CRYPTO_HOST_MEMORY);
804 error = skein_mac_ctx_build(ctx_tmpl, mechanism, key);
805 if (error != CRYPTO_SUCCESS)
806 goto errout;
807 *ctx_template = ctx_tmpl;
808 *ctx_template_size = sizeof (*ctx_tmpl);
810 return (CRYPTO_SUCCESS);
811 errout:
812 bzero(ctx_tmpl, sizeof (*ctx_tmpl));
813 kmem_free(ctx_tmpl, sizeof (*ctx_tmpl));
814 return (error);
818 * Frees a skein context in a parent crypto context.
820 static int
821 skein_free_context(crypto_ctx_t *ctx)
823 if (SKEIN_CTX(ctx) != NULL) {
824 bzero(SKEIN_CTX(ctx), sizeof (*SKEIN_CTX(ctx)));
825 kmem_free(SKEIN_CTX(ctx), sizeof (*SKEIN_CTX(ctx)));
826 SKEIN_CTX_LVALUE(ctx) = NULL;
829 return (CRYPTO_SUCCESS);