1*4a04e8dbSToomas Soome /*
2*4a04e8dbSToomas Soome  * CDDL HEADER START
3*4a04e8dbSToomas Soome  *
4*4a04e8dbSToomas Soome  * The contents of this file are subject to the terms of the
5*4a04e8dbSToomas Soome  * Common Development and Distribution License (the "License").
6*4a04e8dbSToomas Soome  * You may not use this file except in compliance with the License.
7*4a04e8dbSToomas Soome  *
8*4a04e8dbSToomas Soome  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*4a04e8dbSToomas Soome  * or http://opensource.org/licenses/CDDL-1.0.
10*4a04e8dbSToomas Soome  * See the License for the specific language governing permissions
11*4a04e8dbSToomas Soome  * and limitations under the License.
12*4a04e8dbSToomas Soome  *
13*4a04e8dbSToomas Soome  * When distributing Covered Code, include this CDDL HEADER in each
14*4a04e8dbSToomas Soome  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*4a04e8dbSToomas Soome  * If applicable, add the following below this CDDL HEADER, with the
16*4a04e8dbSToomas Soome  * fields enclosed by brackets "[]" replaced with your own identifying
17*4a04e8dbSToomas Soome  * information: Portions Copyright [yyyy] [name of copyright owner]
18*4a04e8dbSToomas Soome  *
19*4a04e8dbSToomas Soome  * CDDL HEADER END
20*4a04e8dbSToomas Soome  */
21*4a04e8dbSToomas Soome /*
22*4a04e8dbSToomas Soome  * Copyright 2013 Saso Kiselkov.  All rights reserved.
23*4a04e8dbSToomas Soome  */
24*4a04e8dbSToomas Soome #include <sys/skein.h>
25*4a04e8dbSToomas Soome 
26*4a04e8dbSToomas Soome /*
27*4a04e8dbSToomas Soome  * Computes a native 256-bit skein MAC checksum. Please note that this
28*4a04e8dbSToomas Soome  * function requires the presence of a ctx_template that should be allocated
29*4a04e8dbSToomas Soome  * using zio_checksum_skein_tmpl_init.
30*4a04e8dbSToomas Soome  */
31*4a04e8dbSToomas Soome /*ARGSUSED*/
32*4a04e8dbSToomas Soome static void
zio_checksum_skein_native(const void * buf,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)33*4a04e8dbSToomas Soome zio_checksum_skein_native(const void *buf, uint64_t size,
34*4a04e8dbSToomas Soome     const void *ctx_template, zio_cksum_t *zcp)
35*4a04e8dbSToomas Soome {
36*4a04e8dbSToomas Soome 	Skein_512_Ctxt_t	ctx;
37*4a04e8dbSToomas Soome 
38*4a04e8dbSToomas Soome 	ASSERT(ctx_template != NULL);
39*4a04e8dbSToomas Soome 	bcopy(ctx_template, &ctx, sizeof (ctx));
40*4a04e8dbSToomas Soome 	(void) Skein_512_Update(&ctx, buf, size);
41*4a04e8dbSToomas Soome 	(void) Skein_512_Final(&ctx, (uint8_t *)zcp);
42*4a04e8dbSToomas Soome 	bzero(&ctx, sizeof (ctx));
43*4a04e8dbSToomas Soome }
44*4a04e8dbSToomas Soome 
45*4a04e8dbSToomas Soome /*
46*4a04e8dbSToomas Soome  * Byteswapped version of zio_checksum_skein_native. This just invokes
47*4a04e8dbSToomas Soome  * the native checksum function and byteswaps the resulting checksum (since
48*4a04e8dbSToomas Soome  * skein is internally endian-insensitive).
49*4a04e8dbSToomas Soome  */
50*4a04e8dbSToomas Soome static void
zio_checksum_skein_byteswap(const void * buf,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)51*4a04e8dbSToomas Soome zio_checksum_skein_byteswap(const void *buf, uint64_t size,
52*4a04e8dbSToomas Soome     const void *ctx_template, zio_cksum_t *zcp)
53*4a04e8dbSToomas Soome {
54*4a04e8dbSToomas Soome 	zio_cksum_t	tmp;
55*4a04e8dbSToomas Soome 
56*4a04e8dbSToomas Soome 	zio_checksum_skein_native(buf, size, ctx_template, &tmp);
57*4a04e8dbSToomas Soome 	zcp->zc_word[0] = BSWAP_64(tmp.zc_word[0]);
58*4a04e8dbSToomas Soome 	zcp->zc_word[1] = BSWAP_64(tmp.zc_word[1]);
59*4a04e8dbSToomas Soome 	zcp->zc_word[2] = BSWAP_64(tmp.zc_word[2]);
60*4a04e8dbSToomas Soome 	zcp->zc_word[3] = BSWAP_64(tmp.zc_word[3]);
61*4a04e8dbSToomas Soome }
62*4a04e8dbSToomas Soome 
63*4a04e8dbSToomas Soome /*
64*4a04e8dbSToomas Soome  * Allocates a skein MAC template suitable for using in skein MAC checksum
65*4a04e8dbSToomas Soome  * computations and returns a pointer to it.
66*4a04e8dbSToomas Soome  */
67*4a04e8dbSToomas Soome static void *
zio_checksum_skein_tmpl_init(const zio_cksum_salt_t * salt)68*4a04e8dbSToomas Soome zio_checksum_skein_tmpl_init(const zio_cksum_salt_t *salt)
69*4a04e8dbSToomas Soome {
70*4a04e8dbSToomas Soome 	Skein_512_Ctxt_t	*ctx;
71*4a04e8dbSToomas Soome 
72*4a04e8dbSToomas Soome 	ctx = malloc(sizeof (*ctx));
73*4a04e8dbSToomas Soome 	bzero(ctx, sizeof (*ctx));
74*4a04e8dbSToomas Soome 	(void) Skein_512_InitExt(ctx, sizeof (zio_cksum_t) * 8, 0,
75*4a04e8dbSToomas Soome 	    salt->zcs_bytes, sizeof (salt->zcs_bytes));
76*4a04e8dbSToomas Soome 	return (ctx);
77*4a04e8dbSToomas Soome }
78*4a04e8dbSToomas Soome 
79*4a04e8dbSToomas Soome /*
80*4a04e8dbSToomas Soome  * Frees a skein context template previously allocated using
81*4a04e8dbSToomas Soome  * zio_checksum_skein_tmpl_init.
82*4a04e8dbSToomas Soome  */
83*4a04e8dbSToomas Soome static void
zio_checksum_skein_tmpl_free(void * ctx_template)84*4a04e8dbSToomas Soome zio_checksum_skein_tmpl_free(void *ctx_template)
85*4a04e8dbSToomas Soome {
86*4a04e8dbSToomas Soome 	Skein_512_Ctxt_t	*ctx = ctx_template;
87*4a04e8dbSToomas Soome 
88*4a04e8dbSToomas Soome 	bzero(ctx, sizeof (*ctx));
89*4a04e8dbSToomas Soome 	free(ctx);
90*4a04e8dbSToomas Soome }
91