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 * Use is subject to license terms.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include <sys/zfs_context.h>
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include <sys/zio.h>
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include <sys/edonr.h>
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#define EDONR_MODE 512
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#define EDONR_BLOCK_SIZE EdonR512_BLOCK_SIZE
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Native zio_checksum interface for the Edon-R hash function.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/*ARGSUSED*/
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensvoid
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrenszio_checksum_edonr_native(const void *buf, uint64_t size,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens const void *ctx_template, zio_cksum_t *zcp)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint8_t digest[EDONR_MODE / 8];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens EdonRState ctx;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ASSERT(ctx_template != NULL);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(ctx_template, &ctx, sizeof (ctx));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens EdonRUpdate(&ctx, buf, size * 8);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens EdonRFinal(&ctx, digest);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bcopy(digest, zcp->zc_word, sizeof (zcp->zc_word));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Byteswapped zio_checksum interface for the Edon-R hash function.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensvoid
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrenszio_checksum_edonr_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_edonr_native(buf, size, ctx_template, &tmp);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens zcp->zc_word[0] = BSWAP_64(zcp->zc_word[0]);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens zcp->zc_word[1] = BSWAP_64(zcp->zc_word[1]);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens zcp->zc_word[2] = BSWAP_64(zcp->zc_word[2]);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens zcp->zc_word[3] = BSWAP_64(zcp->zc_word[3]);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensvoid *
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrenszio_checksum_edonr_tmpl_init(const zio_cksum_salt_t *salt)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens EdonRState *ctx;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens uint8_t salt_block[EDONR_BLOCK_SIZE];
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Edon-R needs all but the last hash invocation to be on full-size
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * blocks, but the salt is too small. Rather than simply padding it
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * with zeros, we expand the salt into a new salt block of proper
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * size by double-hashing it (the new salt block will be composed of
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * H(salt) || H(H(salt))).
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens CTASSERT(EDONR_BLOCK_SIZE == 2 * (EDONR_MODE / 8));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens EdonRHash(EDONR_MODE, salt->zcs_bytes, sizeof (salt->zcs_bytes) * 8,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens salt_block);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens EdonRHash(EDONR_MODE, salt_block, EDONR_MODE, salt_block +
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens EDONR_MODE / 8);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens /*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Feed the new salt block into the hash function - this will serve
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * as our MAC key.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ctx = kmem_zalloc(sizeof (*ctx), KM_SLEEP);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens EdonRInit(ctx, EDONR_MODE);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens EdonRUpdate(ctx, salt_block, sizeof (salt_block) * 8);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens return (ctx);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensvoid
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrenszio_checksum_edonr_tmpl_free(void *ctx_template)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens EdonRState *ctx = ctx_template;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens bzero(ctx, sizeof (*ctx));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens kmem_free(ctx, sizeof (*ctx));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}