45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * CDDL HEADER START
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens *
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * The contents of this file are subject to the terms of the
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Common Development and Distribution License (the "License").
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * You may not use this file except in compliance with the License.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens *
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * or http://opensource.org/licenses/CDDL-1.0.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * See the License for the specific language governing permissions
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * and limitations under the License.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens *
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * When distributing Covered Code, include this CDDL HEADER in each
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * If applicable, add the following below this CDDL HEADER, with the
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * fields enclosed by brackets "[]" replaced with your own identifying
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens *
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * CDDL HEADER END
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Copyright 2013 Saso Kiselkov. All rights reserved.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include <sys/zfs_context.h>
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include <sys/zio.h>
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include <sys/skein.h>
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Computes a native 256-bit skein MAC checksum. Please note that this
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * function requires the presence of a ctx_template that should be allocated
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * using zio_checksum_skein_tmpl_init.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/*ARGSUSED*/
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensvoid
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrenszio_checksum_skein_native(const void *buf, uint64_t size,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens const void *ctx_template, zio_cksum_t *zcp)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_512_Ctxt_t ctx;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ASSERT(ctx_template != NULL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(ctx_template, &ctx, sizeof (ctx));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens (void) Skein_512_Update(&ctx, buf, size);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens (void) Skein_512_Final(&ctx, (uint8_t *)zcp);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(&ctx, sizeof (ctx));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Byteswapped version of zio_checksum_skein_native. This just invokes
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * the native checksum function and byteswaps the resulting checksum (since
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * skein is internally endian-insensitive).
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensvoid
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrenszio_checksum_skein_byteswap(const void *buf, uint64_t size,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens const void *ctx_template, zio_cksum_t *zcp)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens zio_cksum_t tmp;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens zio_checksum_skein_native(buf, size, ctx_template, &tmp);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens zcp->zc_word[0] = BSWAP_64(tmp.zc_word[0]);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens zcp->zc_word[1] = BSWAP_64(tmp.zc_word[1]);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens zcp->zc_word[2] = BSWAP_64(tmp.zc_word[2]);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens zcp->zc_word[3] = BSWAP_64(tmp.zc_word[3]);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Allocates a skein MAC template suitable for using in skein MAC checksum
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * computations and returns a pointer to it.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensvoid *
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrenszio_checksum_skein_tmpl_init(const zio_cksum_salt_t *salt)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_512_Ctxt_t *ctx;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx = kmem_zalloc(sizeof (*ctx), KM_SLEEP);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens (void) Skein_512_InitExt(ctx, sizeof (zio_cksum_t) * 8, 0,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens salt->zcs_bytes, sizeof (salt->zcs_bytes));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (ctx);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Frees a skein context template previously allocated using
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * zio_checksum_skein_tmpl_init.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensvoid
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrenszio_checksum_skein_tmpl_free(void *ctx_template)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens Skein_512_Ctxt_t *ctx = ctx_template;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(ctx, sizeof (*ctx));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens kmem_free(ctx, sizeof (*ctx));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}