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