Path: blob/main/sys/contrib/openzfs/module/zfs/edonr_zfs.c
48383 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2013 Saso Kiselkov. All rights reserved.23* Use is subject to license terms.24*/25/*26* Copyright (c) 2016 by Delphix. All rights reserved.27*/28#include <sys/zfs_context.h>29#include <sys/zio.h>30#include <sys/zio_checksum.h>31#include <sys/edonr.h>32#include <sys/abd.h>3334#define EDONR_MODE 51235#define EDONR_BLOCK_SIZE EdonR512_BLOCK_SIZE3637static int38edonr_incremental(void *buf, size_t size, void *arg)39{40EdonRState *ctx = arg;41EdonRUpdate(ctx, buf, size * 8);42return (0);43}4445/*46* Native zio_checksum interface for the Edon-R hash function.47*/48void49abd_checksum_edonr_native(abd_t *abd, uint64_t size,50const void *ctx_template, zio_cksum_t *zcp)51{52uint8_t digest[EDONR_MODE / 8];53EdonRState ctx;5455ASSERT(ctx_template != NULL);56memcpy(&ctx, ctx_template, sizeof (ctx));57(void) abd_iterate_func(abd, 0, size, edonr_incremental, &ctx);58EdonRFinal(&ctx, digest);59memcpy(zcp->zc_word, digest, sizeof (zcp->zc_word));60}6162/*63* Byteswapped zio_checksum interface for the Edon-R hash function.64*/65void66abd_checksum_edonr_byteswap(abd_t *abd, uint64_t size,67const void *ctx_template, zio_cksum_t *zcp)68{69zio_cksum_t tmp;7071abd_checksum_edonr_native(abd, size, ctx_template, &tmp);72zcp->zc_word[0] = BSWAP_64(zcp->zc_word[0]);73zcp->zc_word[1] = BSWAP_64(zcp->zc_word[1]);74zcp->zc_word[2] = BSWAP_64(zcp->zc_word[2]);75zcp->zc_word[3] = BSWAP_64(zcp->zc_word[3]);76}7778void *79abd_checksum_edonr_tmpl_init(const zio_cksum_salt_t *salt)80{81EdonRState *ctx;82uint8_t salt_block[EDONR_BLOCK_SIZE];8384/*85* Edon-R needs all but the last hash invocation to be on full-size86* blocks, but the salt is too small. Rather than simply padding it87* with zeros, we expand the salt into a new salt block of proper88* size by double-hashing it (the new salt block will be composed of89* H(salt) || H(H(salt))).90*/91_Static_assert(EDONR_BLOCK_SIZE == 2 * (EDONR_MODE / 8),92"Edon-R block size mismatch");93EdonRHash(salt->zcs_bytes, sizeof (salt->zcs_bytes) * 8, salt_block);94EdonRHash(salt_block, EDONR_MODE, salt_block + EDONR_MODE / 8);9596/*97* Feed the new salt block into the hash function - this will serve98* as our MAC key.99*/100ctx = kmem_zalloc(sizeof (*ctx), KM_SLEEP);101EdonRInit(ctx);102EdonRUpdate(ctx, salt_block, sizeof (salt_block) * 8);103return (ctx);104}105106void107abd_checksum_edonr_tmpl_free(void *ctx_template)108{109EdonRState *ctx = ctx_template;110111memset(ctx, 0, sizeof (*ctx));112kmem_free(ctx, sizeof (*ctx));113}114115116