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]
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
= {
42 "Skein Message-Digest Algorithm"
45 static struct modlcrypto modlcrypto
= {
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
= {
81 static int skein_digest_init(crypto_ctx_t
*, crypto_mechanism_t
*,
83 static int skein_digest(crypto_ctx_t
*, crypto_data_t
*, crypto_data_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
*,
91 static crypto_digest_ops_t skein_digest_ops
= {
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
= {
109 skein_update
, /* using regular digest update is OK here */
110 skein_final
, /* using regular digest final is OK here */
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
,
125 static crypto_ops_t skein_crypto_ops
= {
145 static crypto_provider_info_t skein_prov_info
= {
146 CRYPTO_SPI_VERSION_4
,
147 "Skein Software Provider",
152 sizeof (skein_mech_info_tab
) / sizeof (crypto_mech_info_t
),
156 static crypto_kcf_provider_handle_t skein_prov_handle
= 0;
158 typedef struct skein_ctx
{
159 skein_mech_type_t sc_mech_type
;
160 size_t sc_digest_bitlen
;
162 Skein_256_Ctxt_t sc_256
;
163 Skein_512_Ctxt_t sc_512
;
164 Skein1024_Ctxt_t sc_1024
;
167 #define SKEIN_CTX(_ctx_) ((skein_ctx_t *)((_ctx_)->cc_provider_private))
168 #define SKEIN_CTX_LVALUE(_ctx_) (_ctx_)->cc_provider_private
169 #define SKEIN_OP(_skein_ctx, _op, ...) \
171 skein_ctx_t *sc = (_skein_ctx); \
172 switch (sc->sc_mech_type) { \
173 case SKEIN_256_MECH_INFO_TYPE: \
174 case SKEIN_256_MAC_MECH_INFO_TYPE: \
175 (void) Skein_256_ ## _op(&sc->sc_u.sc_256, \
178 case SKEIN_512_MECH_INFO_TYPE: \
179 case SKEIN_512_MAC_MECH_INFO_TYPE: \
180 (void) Skein_512_ ## _op(&sc->sc_u.sc_512, \
183 case SKEIN1024_MECH_INFO_TYPE: \
184 case SKEIN1024_MAC_MECH_INFO_TYPE: \
185 (void) Skein1024_ ## _op(&sc->sc_u.sc_1024, \
193 skein_get_digest_bitlen(const crypto_mechanism_t
*mechanism
, size_t *result
)
195 if (mechanism
->cm_param
!= NULL
) {
196 /*LINTED(E_BAD_PTR_CAST_ALIGN)*/
197 skein_param_t
*param
= (skein_param_t
*)mechanism
->cm_param
;
199 if (mechanism
->cm_param_len
!= sizeof (*param
) ||
200 param
->sp_digest_bitlen
== 0) {
201 return (CRYPTO_MECHANISM_PARAM_INVALID
);
203 *result
= param
->sp_digest_bitlen
;
205 switch (mechanism
->cm_type
) {
206 case SKEIN_256_MECH_INFO_TYPE
:
209 case SKEIN_512_MECH_INFO_TYPE
:
212 case SKEIN1024_MECH_INFO_TYPE
:
216 return (CRYPTO_MECHANISM_INVALID
);
219 return (CRYPTO_SUCCESS
);
227 if ((error
= mod_install(&modlinkage
)) != 0)
231 * Try to register with KCF - failure shouldn't unload us, since we
232 * still may want to continue providing misc/skein functionality.
234 (void) crypto_register_provider(&skein_prov_info
, &skein_prov_handle
);
240 _info(struct modinfo
*modinfop
)
242 return (mod_info(&modlinkage
, modinfop
));
246 * KCF software provider control entry points.
250 skein_provider_status(crypto_provider_handle_t provider
, uint_t
*status
)
252 *status
= CRYPTO_PROVIDER_READY
;
256 * General Skein hashing helper functions.
260 * Performs an Update on a context with uio input data.
263 skein_digest_update_uio(skein_ctx_t
*ctx
, const crypto_data_t
*data
)
265 off_t offset
= data
->cd_offset
;
266 size_t length
= data
->cd_length
;
269 const uio_t
*uio
= data
->cd_uio
;
271 /* we support only kernel buffer */
272 if (uio
->uio_segflg
!= UIO_SYSSPACE
)
273 return (CRYPTO_ARGUMENTS_BAD
);
276 * Jump to the first iovec containing data to be
279 for (vec_idx
= 0; vec_idx
< uio
->uio_iovcnt
&&
280 offset
>= uio
->uio_iov
[vec_idx
].iov_len
;
281 offset
-= uio
->uio_iov
[vec_idx
++].iov_len
)
283 if (vec_idx
== uio
->uio_iovcnt
) {
285 * The caller specified an offset that is larger than the
286 * total size of the buffers it provided.
288 return (CRYPTO_DATA_LEN_RANGE
);
292 * Now do the digesting on the iovecs.
294 while (vec_idx
< uio
->uio_iovcnt
&& length
> 0) {
295 cur_len
= MIN(uio
->uio_iov
[vec_idx
].iov_len
- offset
, length
);
296 SKEIN_OP(ctx
, Update
, (uint8_t *)uio
->uio_iov
[vec_idx
].iov_base
303 if (vec_idx
== uio
->uio_iovcnt
&& length
> 0) {
305 * The end of the specified iovec's was reached but
306 * the length requested could not be processed, i.e.
307 * The caller requested to digest more data than it provided.
309 return (CRYPTO_DATA_LEN_RANGE
);
312 return (CRYPTO_SUCCESS
);
316 * Performs a Final on a context and writes to a uio digest output.
319 skein_digest_final_uio(skein_ctx_t
*ctx
, crypto_data_t
*digest
,
320 crypto_req_handle_t req
)
322 off_t offset
= digest
->cd_offset
;
324 uio_t
*uio
= digest
->cd_uio
;
326 /* we support only kernel buffer */
327 if (uio
->uio_segflg
!= UIO_SYSSPACE
)
328 return (CRYPTO_ARGUMENTS_BAD
);
331 * Jump to the first iovec containing ptr to the digest to be returned.
333 for (vec_idx
= 0; offset
>= uio
->uio_iov
[vec_idx
].iov_len
&&
334 vec_idx
< uio
->uio_iovcnt
;
335 offset
-= uio
->uio_iov
[vec_idx
++].iov_len
)
337 if (vec_idx
== uio
->uio_iovcnt
) {
339 * The caller specified an offset that is larger than the
340 * total size of the buffers it provided.
342 return (CRYPTO_DATA_LEN_RANGE
);
344 if (offset
+ CRYPTO_BITS2BYTES(ctx
->sc_digest_bitlen
) <=
345 uio
->uio_iov
[vec_idx
].iov_len
) {
346 /* The computed digest will fit in the current iovec. */
348 (uchar_t
*)uio
->uio_iov
[vec_idx
].iov_base
+ offset
);
351 off_t scratch_offset
= 0;
352 size_t length
= CRYPTO_BITS2BYTES(ctx
->sc_digest_bitlen
);
355 digest_tmp
= kmem_alloc(CRYPTO_BITS2BYTES(
356 ctx
->sc_digest_bitlen
), crypto_kmflag(req
));
357 if (digest_tmp
== NULL
)
358 return (CRYPTO_HOST_MEMORY
);
359 SKEIN_OP(ctx
, Final
, digest_tmp
);
360 while (vec_idx
< uio
->uio_iovcnt
&& length
> 0) {
361 cur_len
= MIN(uio
->uio_iov
[vec_idx
].iov_len
- offset
,
363 bcopy(digest_tmp
+ scratch_offset
,
364 uio
->uio_iov
[vec_idx
].iov_base
+ offset
, cur_len
);
368 scratch_offset
+= cur_len
;
371 kmem_free(digest_tmp
, CRYPTO_BITS2BYTES(ctx
->sc_digest_bitlen
));
373 if (vec_idx
== uio
->uio_iovcnt
&& length
> 0) {
375 * The end of the specified iovec's was reached but
376 * the length requested could not be processed, i.e.
377 * The caller requested to digest more data than it
380 return (CRYPTO_DATA_LEN_RANGE
);
384 return (CRYPTO_SUCCESS
);
388 * Performs an Update on a context with mblk input data.
391 skein_digest_update_mblk(skein_ctx_t
*ctx
, crypto_data_t
*data
)
393 off_t offset
= data
->cd_offset
;
394 size_t length
= data
->cd_length
;
398 /* Jump to the first mblk_t containing data to be digested. */
399 for (mp
= data
->cd_mp
; mp
!= NULL
&& offset
>= MBLKL(mp
);
400 offset
-= MBLKL(mp
), mp
= mp
->b_cont
)
404 * The caller specified an offset that is larger than the
405 * total size of the buffers it provided.
407 return (CRYPTO_DATA_LEN_RANGE
);
410 /* Now do the digesting on the mblk chain. */
411 while (mp
!= NULL
&& length
> 0) {
412 cur_len
= MIN(MBLKL(mp
) - offset
, length
);
413 SKEIN_OP(ctx
, Update
, mp
->b_rptr
+ offset
, cur_len
);
419 if (mp
== NULL
&& length
> 0) {
421 * The end of the mblk was reached but the length requested
422 * could not be processed, i.e. The caller requested
423 * to digest more data than it provided.
425 return (CRYPTO_DATA_LEN_RANGE
);
428 return (CRYPTO_SUCCESS
);
432 * Performs a Final on a context and writes to an mblk digest output.
435 skein_digest_final_mblk(skein_ctx_t
*ctx
, crypto_data_t
*digest
,
436 crypto_req_handle_t req
)
438 off_t offset
= digest
->cd_offset
;
441 /* Jump to the first mblk_t that will be used to store the digest. */
442 for (mp
= digest
->cd_mp
; mp
!= NULL
&& offset
>= MBLKL(mp
);
443 offset
-= MBLKL(mp
), mp
= mp
->b_cont
)
446 /* caller specified offset is too large */
447 return (CRYPTO_DATA_LEN_RANGE
);
450 if (offset
+ CRYPTO_BITS2BYTES(ctx
->sc_digest_bitlen
) <= MBLKL(mp
)) {
451 /* The digest will fit in the current mblk. */
452 SKEIN_OP(ctx
, Final
, mp
->b_rptr
+ offset
);
454 /* Split the digest up between the individual buffers. */
456 off_t scratch_offset
= 0;
457 size_t length
= CRYPTO_BITS2BYTES(ctx
->sc_digest_bitlen
);
460 digest_tmp
= kmem_alloc(CRYPTO_BITS2BYTES(
461 ctx
->sc_digest_bitlen
), crypto_kmflag(req
));
462 if (digest_tmp
== NULL
)
463 return (CRYPTO_HOST_MEMORY
);
464 SKEIN_OP(ctx
, Final
, digest_tmp
);
465 while (mp
!= NULL
&& length
> 0) {
466 cur_len
= MIN(MBLKL(mp
) - offset
, length
);
467 bcopy(digest_tmp
+ scratch_offset
,
468 mp
->b_rptr
+ offset
, cur_len
);
471 scratch_offset
+= cur_len
;
474 kmem_free(digest_tmp
, CRYPTO_BITS2BYTES(ctx
->sc_digest_bitlen
));
475 if (mp
== NULL
&& length
> 0) {
476 /* digest too long to fit in the mblk buffers */
477 return (CRYPTO_DATA_LEN_RANGE
);
481 return (CRYPTO_SUCCESS
);
485 * KCF software provider digest entry points.
489 * Initializes a skein digest context to the configuration in `mechanism'.
490 * The mechanism cm_type must be one of SKEIN_*_MECH_INFO_TYPE. The cm_param
491 * field may contain a skein_param_t structure indicating the length of the
492 * digest the algorithm should produce. Otherwise the default output lengths
493 * are applied (32 bytes for Skein-256, 64 bytes for Skein-512 and 128 bytes
497 skein_digest_init(crypto_ctx_t
*ctx
, crypto_mechanism_t
*mechanism
,
498 crypto_req_handle_t req
)
500 int error
= CRYPTO_SUCCESS
;
502 if (!VALID_SKEIN_DIGEST_MECH(mechanism
->cm_type
))
503 return (CRYPTO_MECHANISM_INVALID
);
505 SKEIN_CTX_LVALUE(ctx
) = kmem_alloc(sizeof (*SKEIN_CTX(ctx
)),
507 if (SKEIN_CTX(ctx
) == NULL
)
508 return (CRYPTO_HOST_MEMORY
);
510 SKEIN_CTX(ctx
)->sc_mech_type
= mechanism
->cm_type
;
511 error
= skein_get_digest_bitlen(mechanism
,
512 &SKEIN_CTX(ctx
)->sc_digest_bitlen
);
513 if (error
!= CRYPTO_SUCCESS
)
515 SKEIN_OP(SKEIN_CTX(ctx
), Init
, SKEIN_CTX(ctx
)->sc_digest_bitlen
);
517 return (CRYPTO_SUCCESS
);
519 bzero(SKEIN_CTX(ctx
), sizeof (*SKEIN_CTX(ctx
)));
520 kmem_free(SKEIN_CTX(ctx
), sizeof (*SKEIN_CTX(ctx
)));
521 SKEIN_CTX_LVALUE(ctx
) = NULL
;
526 * Executes a skein_update and skein_digest on a pre-initialized crypto
527 * context in a single step. See the documentation to these functions to
528 * see what to pass here.
531 skein_digest(crypto_ctx_t
*ctx
, crypto_data_t
*data
, crypto_data_t
*digest
,
532 crypto_req_handle_t req
)
534 int error
= CRYPTO_SUCCESS
;
536 ASSERT(SKEIN_CTX(ctx
) != NULL
);
538 if (digest
->cd_length
<
539 CRYPTO_BITS2BYTES(SKEIN_CTX(ctx
)->sc_digest_bitlen
)) {
541 CRYPTO_BITS2BYTES(SKEIN_CTX(ctx
)->sc_digest_bitlen
);
542 return (CRYPTO_BUFFER_TOO_SMALL
);
545 error
= skein_update(ctx
, data
, req
);
546 if (error
!= CRYPTO_SUCCESS
) {
547 bzero(SKEIN_CTX(ctx
), sizeof (*SKEIN_CTX(ctx
)));
548 kmem_free(SKEIN_CTX(ctx
), sizeof (*SKEIN_CTX(ctx
)));
549 SKEIN_CTX_LVALUE(ctx
) = NULL
;
550 digest
->cd_length
= 0;
553 error
= skein_final(ctx
, digest
, req
);
559 * Performs a skein Update with the input message in `data' (successive calls
560 * can push more data). This is used both for digest and MAC operation.
561 * Supported input data formats are raw, uio and mblk.
565 skein_update(crypto_ctx_t
*ctx
, crypto_data_t
*data
, crypto_req_handle_t req
)
567 int error
= CRYPTO_SUCCESS
;
569 ASSERT(SKEIN_CTX(ctx
) != NULL
);
571 switch (data
->cd_format
) {
572 case CRYPTO_DATA_RAW
:
573 SKEIN_OP(SKEIN_CTX(ctx
), Update
,
574 (uint8_t *)data
->cd_raw
.iov_base
+ data
->cd_offset
,
577 case CRYPTO_DATA_UIO
:
578 error
= skein_digest_update_uio(SKEIN_CTX(ctx
), data
);
580 case CRYPTO_DATA_MBLK
:
581 error
= skein_digest_update_mblk(SKEIN_CTX(ctx
), data
);
584 error
= CRYPTO_ARGUMENTS_BAD
;
591 * Performs a skein Final, writing the output to `digest'. This is used both
592 * for digest and MAC operation.
593 * Supported output digest formats are raw, uio and mblk.
597 skein_final(crypto_ctx_t
*ctx
, crypto_data_t
*digest
, crypto_req_handle_t req
)
599 int error
= CRYPTO_SUCCESS
;
601 ASSERT(SKEIN_CTX(ctx
) != NULL
);
603 if (digest
->cd_length
<
604 CRYPTO_BITS2BYTES(SKEIN_CTX(ctx
)->sc_digest_bitlen
)) {
606 CRYPTO_BITS2BYTES(SKEIN_CTX(ctx
)->sc_digest_bitlen
);
607 return (CRYPTO_BUFFER_TOO_SMALL
);
610 switch (digest
->cd_format
) {
611 case CRYPTO_DATA_RAW
:
612 SKEIN_OP(SKEIN_CTX(ctx
), Final
,
613 (uint8_t *)digest
->cd_raw
.iov_base
+ digest
->cd_offset
);
615 case CRYPTO_DATA_UIO
:
616 error
= skein_digest_final_uio(SKEIN_CTX(ctx
), digest
, req
);
618 case CRYPTO_DATA_MBLK
:
619 error
= skein_digest_final_mblk(SKEIN_CTX(ctx
), digest
, req
);
622 error
= CRYPTO_ARGUMENTS_BAD
;
625 if (error
== CRYPTO_SUCCESS
)
627 CRYPTO_BITS2BYTES(SKEIN_CTX(ctx
)->sc_digest_bitlen
);
629 digest
->cd_length
= 0;
631 bzero(SKEIN_CTX(ctx
), sizeof (*SKEIN_CTX(ctx
)));
632 kmem_free(SKEIN_CTX(ctx
), sizeof (*(SKEIN_CTX(ctx
))));
633 SKEIN_CTX_LVALUE(ctx
) = NULL
;
639 * Performs a full skein digest computation in a single call, configuring the
640 * algorithm according to `mechanism', reading the input to be digested from
641 * `data' and writing the output to `digest'.
642 * Supported input/output formats are raw, uio and mblk.
646 skein_digest_atomic(crypto_provider_handle_t provider
,
647 crypto_session_id_t session_id
, crypto_mechanism_t
*mechanism
,
648 crypto_data_t
*data
, crypto_data_t
*digest
, crypto_req_handle_t req
)
651 skein_ctx_t skein_ctx
;
653 SKEIN_CTX_LVALUE(&ctx
) = &skein_ctx
;
656 if (!VALID_SKEIN_DIGEST_MECH(mechanism
->cm_type
))
657 return (CRYPTO_MECHANISM_INVALID
);
658 skein_ctx
.sc_mech_type
= mechanism
->cm_type
;
659 error
= skein_get_digest_bitlen(mechanism
, &skein_ctx
.sc_digest_bitlen
);
660 if (error
!= CRYPTO_SUCCESS
)
662 SKEIN_OP(&skein_ctx
, Init
, skein_ctx
.sc_digest_bitlen
);
664 if ((error
= skein_update(&ctx
, data
, digest
)) != CRYPTO_SUCCESS
)
666 if ((error
= skein_final(&ctx
, data
, digest
)) != CRYPTO_SUCCESS
)
670 if (error
== CRYPTO_SUCCESS
)
672 CRYPTO_BITS2BYTES(skein_ctx
.sc_digest_bitlen
);
674 digest
->cd_length
= 0;
675 bzero(&skein_ctx
, sizeof (skein_ctx
));
681 * Helper function that builds a Skein MAC context from the provided
685 skein_mac_ctx_build(skein_ctx_t
*ctx
, crypto_mechanism_t
*mechanism
,
690 if (!VALID_SKEIN_MAC_MECH(mechanism
->cm_type
))
691 return (CRYPTO_MECHANISM_INVALID
);
692 if (key
->ck_format
!= CRYPTO_KEY_RAW
)
693 return (CRYPTO_ARGUMENTS_BAD
);
694 ctx
->sc_mech_type
= mechanism
->cm_type
;
695 error
= skein_get_digest_bitlen(mechanism
, &ctx
->sc_digest_bitlen
);
696 if (error
!= CRYPTO_SUCCESS
)
698 SKEIN_OP(ctx
, InitExt
, ctx
->sc_digest_bitlen
, 0, key
->ck_data
,
699 CRYPTO_BITS2BYTES(key
->ck_length
));
701 return (CRYPTO_SUCCESS
);
705 * KCF software provide mac entry points.
708 * Initializes a skein MAC context. You may pass a ctx_template, in which
709 * case the template will be reused to make initialization more efficient.
710 * Otherwise a new context will be constructed. The mechanism cm_type must
711 * be one of SKEIN_*_MAC_MECH_INFO_TYPE. Same as in skein_digest_init, you
712 * may pass a skein_param_t in cm_param to configure the length of the
713 * digest. The key must be in raw format.
716 skein_mac_init(crypto_ctx_t
*ctx
, crypto_mechanism_t
*mechanism
,
717 crypto_key_t
*key
, crypto_spi_ctx_template_t ctx_template
,
718 crypto_req_handle_t req
)
722 SKEIN_CTX_LVALUE(ctx
) = kmem_alloc(sizeof (*SKEIN_CTX(ctx
)),
724 if (SKEIN_CTX(ctx
) == NULL
)
725 return (CRYPTO_HOST_MEMORY
);
727 if (ctx_template
!= NULL
) {
728 bcopy(ctx_template
, SKEIN_CTX(ctx
),
729 sizeof (*SKEIN_CTX(ctx
)));
731 error
= skein_mac_ctx_build(SKEIN_CTX(ctx
), mechanism
, key
);
732 if (error
!= CRYPTO_SUCCESS
)
736 return (CRYPTO_SUCCESS
);
738 bzero(SKEIN_CTX(ctx
), sizeof (*SKEIN_CTX(ctx
)));
739 kmem_free(SKEIN_CTX(ctx
), sizeof (*SKEIN_CTX(ctx
)));
744 * The MAC update and final calls are reused from the regular digest code.
749 * Same as skein_digest_atomic, performs an atomic Skein MAC operation in
750 * one step. All the same properties apply to the arguments of this
751 * function as to those of the partial operations above.
754 skein_mac_atomic(crypto_provider_handle_t provider
,
755 crypto_session_id_t session_id
, crypto_mechanism_t
*mechanism
,
756 crypto_key_t
*key
, crypto_data_t
*data
, crypto_data_t
*mac
,
757 crypto_spi_ctx_template_t ctx_template
, crypto_req_handle_t req
)
759 /* faux crypto context just for skein_digest_{update,final} */
762 skein_ctx_t skein_ctx
;
763 SKEIN_CTX_LVALUE(&ctx
) = &skein_ctx
;
765 if (ctx_template
!= NULL
) {
766 bcopy(ctx_template
, &skein_ctx
, sizeof (skein_ctx
));
768 error
= skein_mac_ctx_build(&skein_ctx
, mechanism
, key
);
769 if (error
!= CRYPTO_SUCCESS
)
773 if ((error
= skein_update(&ctx
, data
, req
)) != CRYPTO_SUCCESS
)
775 if ((error
= skein_final(&ctx
, mac
, req
)) != CRYPTO_SUCCESS
)
778 return (CRYPTO_SUCCESS
);
780 bzero(&skein_ctx
, sizeof (skein_ctx
));
785 * KCF software provider context management entry points.
789 * Constructs a context template for the Skein MAC algorithm. The same
790 * properties apply to the arguments of this function as to those of
795 skein_create_ctx_template(crypto_provider_handle_t provider
,
796 crypto_mechanism_t
*mechanism
, crypto_key_t
*key
,
797 crypto_spi_ctx_template_t
*ctx_template
, size_t *ctx_template_size
,
798 crypto_req_handle_t req
)
801 skein_ctx_t
*ctx_tmpl
;
803 ctx_tmpl
= kmem_alloc(sizeof (*ctx_tmpl
), crypto_kmflag(req
));
804 if (ctx_tmpl
== NULL
)
805 return (CRYPTO_HOST_MEMORY
);
806 error
= skein_mac_ctx_build(ctx_tmpl
, mechanism
, key
);
807 if (error
!= CRYPTO_SUCCESS
)
809 *ctx_template
= ctx_tmpl
;
810 *ctx_template_size
= sizeof (*ctx_tmpl
);
812 return (CRYPTO_SUCCESS
);
814 bzero(ctx_tmpl
, sizeof (*ctx_tmpl
));
815 kmem_free(ctx_tmpl
, sizeof (*ctx_tmpl
));
820 * Frees a skein context in a parent crypto context.
823 skein_free_context(crypto_ctx_t
*ctx
)
825 if (SKEIN_CTX(ctx
) != NULL
) {
826 bzero(SKEIN_CTX(ctx
), sizeof (*SKEIN_CTX(ctx
)));
827 kmem_free(SKEIN_CTX(ctx
), sizeof (*SKEIN_CTX(ctx
)));
828 SKEIN_CTX_LVALUE(ctx
) = NULL
;
831 return (CRYPTO_SUCCESS
);