xref: /illumos-gate/usr/src/uts/common/fs/zfs/skein_zfs.c (revision 770499e1)
145818ee1SMatthew Ahrens /*
245818ee1SMatthew Ahrens  * CDDL HEADER START
345818ee1SMatthew Ahrens  *
445818ee1SMatthew Ahrens  * The contents of this file are subject to the terms of the
545818ee1SMatthew Ahrens  * Common Development and Distribution License (the "License").
645818ee1SMatthew Ahrens  * You may not use this file except in compliance with the License.
745818ee1SMatthew Ahrens  *
845818ee1SMatthew Ahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
945818ee1SMatthew Ahrens  * or http://opensource.org/licenses/CDDL-1.0.
1045818ee1SMatthew Ahrens  * See the License for the specific language governing permissions
1145818ee1SMatthew Ahrens  * and limitations under the License.
1245818ee1SMatthew Ahrens  *
1345818ee1SMatthew Ahrens  * When distributing Covered Code, include this CDDL HEADER in each
1445818ee1SMatthew Ahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1545818ee1SMatthew Ahrens  * If applicable, add the following below this CDDL HEADER, with the
1645818ee1SMatthew Ahrens  * fields enclosed by brackets "[]" replaced with your own identifying
1745818ee1SMatthew Ahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
1845818ee1SMatthew Ahrens  *
1945818ee1SMatthew Ahrens  * CDDL HEADER END
2045818ee1SMatthew Ahrens  */
2145818ee1SMatthew Ahrens /*
2245818ee1SMatthew Ahrens  * Copyright 2013 Saso Kiselkov.  All rights reserved.
23*770499e1SDan Kimmel  * Copyright (c) 2016 by Delphix. All rights reserved.
2445818ee1SMatthew Ahrens  */
2545818ee1SMatthew Ahrens #include <sys/zfs_context.h>
2645818ee1SMatthew Ahrens #include <sys/zio.h>
2745818ee1SMatthew Ahrens #include <sys/skein.h>
28*770499e1SDan Kimmel #include <sys/abd.h>
29*770499e1SDan Kimmel 
30*770499e1SDan Kimmel static int
skein_incremental(void * buf,size_t size,void * arg)31*770499e1SDan Kimmel skein_incremental(void *buf, size_t size, void *arg)
32*770499e1SDan Kimmel {
33*770499e1SDan Kimmel 	Skein_512_Ctxt_t *ctx = arg;
34*770499e1SDan Kimmel 	(void) Skein_512_Update(ctx, buf, size);
35*770499e1SDan Kimmel 	return (0);
36*770499e1SDan Kimmel }
3745818ee1SMatthew Ahrens 
3845818ee1SMatthew Ahrens /*
3945818ee1SMatthew Ahrens  * Computes a native 256-bit skein MAC checksum. Please note that this
4045818ee1SMatthew Ahrens  * function requires the presence of a ctx_template that should be allocated
41*770499e1SDan Kimmel  * using abd_checksum_skein_tmpl_init.
4245818ee1SMatthew Ahrens  */
4345818ee1SMatthew Ahrens /*ARGSUSED*/
4445818ee1SMatthew Ahrens void
abd_checksum_skein_native(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)45*770499e1SDan Kimmel abd_checksum_skein_native(abd_t *abd, uint64_t size,
4645818ee1SMatthew Ahrens     const void *ctx_template, zio_cksum_t *zcp)
4745818ee1SMatthew Ahrens {
4845818ee1SMatthew Ahrens 	Skein_512_Ctxt_t	ctx;
4945818ee1SMatthew Ahrens 
5045818ee1SMatthew Ahrens 	ASSERT(ctx_template != NULL);
5145818ee1SMatthew Ahrens 	bcopy(ctx_template, &ctx, sizeof (ctx));
52*770499e1SDan Kimmel 	(void) abd_iterate_func(abd, 0, size, skein_incremental, &ctx);
5345818ee1SMatthew Ahrens 	(void) Skein_512_Final(&ctx, (uint8_t *)zcp);
5445818ee1SMatthew Ahrens 	bzero(&ctx, sizeof (ctx));
5545818ee1SMatthew Ahrens }
5645818ee1SMatthew Ahrens 
5745818ee1SMatthew Ahrens /*
58*770499e1SDan Kimmel  * Byteswapped version of abd_checksum_skein_native. This just invokes
5945818ee1SMatthew Ahrens  * the native checksum function and byteswaps the resulting checksum (since
6045818ee1SMatthew Ahrens  * skein is internally endian-insensitive).
6145818ee1SMatthew Ahrens  */
6245818ee1SMatthew Ahrens void
abd_checksum_skein_byteswap(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)63*770499e1SDan Kimmel abd_checksum_skein_byteswap(abd_t *abd, uint64_t size,
6445818ee1SMatthew Ahrens     const void *ctx_template, zio_cksum_t *zcp)
6545818ee1SMatthew Ahrens {
6645818ee1SMatthew Ahrens 	zio_cksum_t	tmp;
6745818ee1SMatthew Ahrens 
68*770499e1SDan Kimmel 	abd_checksum_skein_native(abd, size, ctx_template, &tmp);
6945818ee1SMatthew Ahrens 	zcp->zc_word[0] = BSWAP_64(tmp.zc_word[0]);
7045818ee1SMatthew Ahrens 	zcp->zc_word[1] = BSWAP_64(tmp.zc_word[1]);
7145818ee1SMatthew Ahrens 	zcp->zc_word[2] = BSWAP_64(tmp.zc_word[2]);
7245818ee1SMatthew Ahrens 	zcp->zc_word[3] = BSWAP_64(tmp.zc_word[3]);
7345818ee1SMatthew Ahrens }
7445818ee1SMatthew Ahrens 
7545818ee1SMatthew Ahrens /*
7645818ee1SMatthew Ahrens  * Allocates a skein MAC template suitable for using in skein MAC checksum
7745818ee1SMatthew Ahrens  * computations and returns a pointer to it.
7845818ee1SMatthew Ahrens  */
7945818ee1SMatthew Ahrens void *
abd_checksum_skein_tmpl_init(const zio_cksum_salt_t * salt)80*770499e1SDan Kimmel abd_checksum_skein_tmpl_init(const zio_cksum_salt_t *salt)
8145818ee1SMatthew Ahrens {
8245818ee1SMatthew Ahrens 	Skein_512_Ctxt_t	*ctx;
8345818ee1SMatthew Ahrens 
8445818ee1SMatthew Ahrens 	ctx = kmem_zalloc(sizeof (*ctx), KM_SLEEP);
8545818ee1SMatthew Ahrens 	(void) Skein_512_InitExt(ctx, sizeof (zio_cksum_t) * 8, 0,
8645818ee1SMatthew Ahrens 	    salt->zcs_bytes, sizeof (salt->zcs_bytes));
8745818ee1SMatthew Ahrens 	return (ctx);
8845818ee1SMatthew Ahrens }
8945818ee1SMatthew Ahrens 
9045818ee1SMatthew Ahrens /*
9145818ee1SMatthew Ahrens  * Frees a skein context template previously allocated using
92*770499e1SDan Kimmel  * abd_checksum_skein_tmpl_init.
9345818ee1SMatthew Ahrens  */
9445818ee1SMatthew Ahrens void
abd_checksum_skein_tmpl_free(void * ctx_template)95*770499e1SDan Kimmel abd_checksum_skein_tmpl_free(void *ctx_template)
9645818ee1SMatthew Ahrens {
9745818ee1SMatthew Ahrens 	Skein_512_Ctxt_t	*ctx = ctx_template;
9845818ee1SMatthew Ahrens 
9945818ee1SMatthew Ahrens 	bzero(ctx, sizeof (*ctx));
10045818ee1SMatthew Ahrens 	kmem_free(ctx, sizeof (*ctx));
10145818ee1SMatthew Ahrens }
102