1289310Smav/*
2289310Smav * CDDL HEADER START
3289310Smav *
4289310Smav * The contents of this file are subject to the terms of the
5289310Smav * Common Development and Distribution License (the "License").
6289310Smav * You may not use this file except in compliance with the License.
7289310Smav *
8289310Smav * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9289310Smav * or http://opensource.org/licenses/CDDL-1.0.
10289310Smav * See the License for the specific language governing permissions
11289310Smav * and limitations under the License.
12289310Smav *
13289310Smav * When distributing Covered Code, include this CDDL HEADER in each
14289310Smav * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15289310Smav * If applicable, add the following below this CDDL HEADER, with the
16289310Smav * fields enclosed by brackets "[]" replaced with your own identifying
17289310Smav * information: Portions Copyright [yyyy] [name of copyright owner]
18289310Smav *
19289310Smav * CDDL HEADER END
20289310Smav */
21289310Smav/*
22289310Smav * Copyright 2013 Saso Kiselkov.  All rights reserved.
23289310Smav */
24289310Smav#include <sys/zfs_context.h>
25289310Smav#include <sys/zio.h>
26289310Smav#include <sys/skein.h>
27289310Smav
28289310Smav/*
29289310Smav * Computes a native 256-bit skein MAC checksum. Please note that this
30289310Smav * function requires the presence of a ctx_template that should be allocated
31289310Smav * using zio_checksum_skein_tmpl_init.
32289310Smav */
33289310Smav/*ARGSUSED*/
34289310Smavvoid
35289310Smavzio_checksum_skein_native(const void *buf, uint64_t size,
36289310Smav    const void *ctx_template, zio_cksum_t *zcp)
37289310Smav{
38289310Smav	Skein_512_Ctxt_t	ctx;
39289310Smav
40289310Smav	ASSERT(ctx_template != NULL);
41289310Smav	bcopy(ctx_template, &ctx, sizeof (ctx));
42289310Smav	(void) Skein_512_Update(&ctx, buf, size);
43289310Smav	(void) Skein_512_Final(&ctx, (uint8_t *)zcp);
44289310Smav	bzero(&ctx, sizeof (ctx));
45289310Smav}
46289310Smav
47289310Smav/*
48289310Smav * Byteswapped version of zio_checksum_skein_native. This just invokes
49289310Smav * the native checksum function and byteswaps the resulting checksum (since
50289310Smav * skein is internally endian-insensitive).
51289310Smav */
52289310Smavvoid
53289310Smavzio_checksum_skein_byteswap(const void *buf, uint64_t size,
54289310Smav    const void *ctx_template, zio_cksum_t *zcp)
55289310Smav{
56289310Smav	zio_cksum_t	tmp;
57289310Smav
58289310Smav	zio_checksum_skein_native(buf, size, ctx_template, &tmp);
59289310Smav	zcp->zc_word[0] = BSWAP_64(tmp.zc_word[0]);
60289310Smav	zcp->zc_word[1] = BSWAP_64(tmp.zc_word[1]);
61289310Smav	zcp->zc_word[2] = BSWAP_64(tmp.zc_word[2]);
62289310Smav	zcp->zc_word[3] = BSWAP_64(tmp.zc_word[3]);
63289310Smav}
64289310Smav
65289310Smav/*
66289310Smav * Allocates a skein MAC template suitable for using in skein MAC checksum
67289310Smav * computations and returns a pointer to it.
68289310Smav */
69289310Smavvoid *
70289310Smavzio_checksum_skein_tmpl_init(const zio_cksum_salt_t *salt)
71289310Smav{
72289310Smav	Skein_512_Ctxt_t	*ctx;
73289310Smav
74289310Smav	ctx = kmem_zalloc(sizeof (*ctx), KM_SLEEP);
75289310Smav	(void) Skein_512_InitExt(ctx, sizeof (zio_cksum_t) * 8, 0,
76289310Smav	    salt->zcs_bytes, sizeof (salt->zcs_bytes));
77289310Smav	return (ctx);
78289310Smav}
79289310Smav
80289310Smav/*
81289310Smav * Frees a skein context template previously allocated using
82289310Smav * zio_checksum_skein_tmpl_init.
83289310Smav */
84289310Smavvoid
85289310Smavzio_checksum_skein_tmpl_free(void *ctx_template)
86289310Smav{
87289310Smav	Skein_512_Ctxt_t	*ctx = ctx_template;
88289310Smav
89289310Smav	bzero(ctx, sizeof (*ctx));
90289310Smav	kmem_free(ctx, sizeof (*ctx));
91289310Smav}
92