xref: /freebsd/sys/cddl/boot/zfs/skein_zfs.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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  * $FreeBSD$
22  */
23 /*
24  * Copyright 2013 Saso Kiselkov.  All rights reserved.
25  */
26 #include <skein.h>
27 
28 /*
29  * Computes a native 256-bit skein MAC checksum. Please note that this
30  * function requires the presence of a ctx_template that should be allocated
31  * using zio_checksum_skein_tmpl_init.
32  */
33 /*ARGSUSED*/
34 static void
35 zio_checksum_skein_native(const void *buf, uint64_t size,
36     const void *ctx_template, zio_cksum_t *zcp)
37 {
38 	Skein_512_Ctxt_t	ctx;
39 
40 	ASSERT(ctx_template != NULL);
41 	bcopy(ctx_template, &ctx, sizeof (ctx));
42 	(void) Skein_512_Update(&ctx, buf, size);
43 	(void) Skein_512_Final(&ctx, (uint8_t *)zcp);
44 	bzero(&ctx, sizeof (ctx));
45 }
46 
47 /*
48  * Byteswapped version of zio_checksum_skein_native. This just invokes
49  * the native checksum function and byteswaps the resulting checksum (since
50  * skein is internally endian-insensitive).
51  */
52 static void
53 zio_checksum_skein_byteswap(const void *buf, uint64_t size,
54     const void *ctx_template, zio_cksum_t *zcp)
55 {
56 	zio_cksum_t	tmp;
57 
58 	zio_checksum_skein_native(buf, size, ctx_template, &tmp);
59 	zcp->zc_word[0] = BSWAP_64(tmp.zc_word[0]);
60 	zcp->zc_word[1] = BSWAP_64(tmp.zc_word[1]);
61 	zcp->zc_word[2] = BSWAP_64(tmp.zc_word[2]);
62 	zcp->zc_word[3] = BSWAP_64(tmp.zc_word[3]);
63 }
64 
65 /*
66  * Allocates a skein MAC template suitable for using in skein MAC checksum
67  * computations and returns a pointer to it.
68  */
69 static void *
70 zio_checksum_skein_tmpl_init(const zio_cksum_salt_t *salt)
71 {
72 	Skein_512_Ctxt_t	*ctx;
73 
74 	ctx = malloc(sizeof (*ctx));
75 	bzero(ctx, sizeof (*ctx));
76 	(void) Skein_512_InitExt(ctx, sizeof (zio_cksum_t) * 8, 0,
77 	    salt->zcs_bytes, sizeof (salt->zcs_bytes));
78 	return (ctx);
79 }
80 
81 /*
82  * Frees a skein context template previously allocated using
83  * zio_checksum_skein_tmpl_init.
84  */
85 static void
86 zio_checksum_skein_tmpl_free(void *ctx_template)
87 {
88 	Skein_512_Ctxt_t	*ctx = ctx_template;
89 
90 	bzero(ctx, sizeof (*ctx));
91 	free(ctx);
92 }
93