1eda14cbcSMatt Macy /* 2eda14cbcSMatt Macy * CDDL HEADER START 3eda14cbcSMatt Macy * 4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5eda14cbcSMatt Macy * Common Development and Distribution License (the "License"). 6eda14cbcSMatt Macy * You may not use this file except in compliance with the License. 7eda14cbcSMatt Macy * 8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9eda14cbcSMatt Macy * or http://opensource.org/licenses/CDDL-1.0. 10eda14cbcSMatt Macy * See the License for the specific language governing permissions 11eda14cbcSMatt Macy * and limitations under the License. 12eda14cbcSMatt Macy * 13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 18eda14cbcSMatt Macy * 19eda14cbcSMatt Macy * CDDL HEADER END 20eda14cbcSMatt Macy */ 21eda14cbcSMatt Macy /* 22eda14cbcSMatt Macy * Copyright 2013 Saso Kiselkov. All rights reserved. 23eda14cbcSMatt Macy * Use is subject to license terms. 24eda14cbcSMatt Macy */ 25eda14cbcSMatt Macy /* 26eda14cbcSMatt Macy * Copyright (c) 2016 by Delphix. All rights reserved. 27eda14cbcSMatt Macy */ 28eda14cbcSMatt Macy #include <sys/zfs_context.h> 29eda14cbcSMatt Macy #include <sys/zio.h> 30eda14cbcSMatt Macy #include <sys/zio_checksum.h> 31eda14cbcSMatt Macy #include <sys/edonr.h> 32eda14cbcSMatt Macy #include <sys/abd.h> 33eda14cbcSMatt Macy 34eda14cbcSMatt Macy #define EDONR_MODE 512 35eda14cbcSMatt Macy #define EDONR_BLOCK_SIZE EdonR512_BLOCK_SIZE 36eda14cbcSMatt Macy 37eda14cbcSMatt Macy static int 38eda14cbcSMatt Macy edonr_incremental(void *buf, size_t size, void *arg) 39eda14cbcSMatt Macy { 40eda14cbcSMatt Macy EdonRState *ctx = arg; 41eda14cbcSMatt Macy EdonRUpdate(ctx, buf, size * 8); 42eda14cbcSMatt Macy return (0); 43eda14cbcSMatt Macy } 44eda14cbcSMatt Macy 45eda14cbcSMatt Macy /* 46eda14cbcSMatt Macy * Native zio_checksum interface for the Edon-R hash function. 47eda14cbcSMatt Macy */ 48eda14cbcSMatt Macy void 49eda14cbcSMatt Macy abd_checksum_edonr_native(abd_t *abd, uint64_t size, 50eda14cbcSMatt Macy const void *ctx_template, zio_cksum_t *zcp) 51eda14cbcSMatt Macy { 52eda14cbcSMatt Macy uint8_t digest[EDONR_MODE / 8]; 53eda14cbcSMatt Macy EdonRState ctx; 54eda14cbcSMatt Macy 55eda14cbcSMatt Macy ASSERT(ctx_template != NULL); 56eda14cbcSMatt Macy bcopy(ctx_template, &ctx, sizeof (ctx)); 57eda14cbcSMatt Macy (void) abd_iterate_func(abd, 0, size, edonr_incremental, &ctx); 58eda14cbcSMatt Macy EdonRFinal(&ctx, digest); 59eda14cbcSMatt Macy bcopy(digest, zcp->zc_word, sizeof (zcp->zc_word)); 60eda14cbcSMatt Macy } 61eda14cbcSMatt Macy 62eda14cbcSMatt Macy /* 63eda14cbcSMatt Macy * Byteswapped zio_checksum interface for the Edon-R hash function. 64eda14cbcSMatt Macy */ 65eda14cbcSMatt Macy void 66eda14cbcSMatt Macy abd_checksum_edonr_byteswap(abd_t *abd, uint64_t size, 67eda14cbcSMatt Macy const void *ctx_template, zio_cksum_t *zcp) 68eda14cbcSMatt Macy { 69eda14cbcSMatt Macy zio_cksum_t tmp; 70eda14cbcSMatt Macy 71eda14cbcSMatt Macy abd_checksum_edonr_native(abd, size, ctx_template, &tmp); 72eda14cbcSMatt Macy zcp->zc_word[0] = BSWAP_64(zcp->zc_word[0]); 73eda14cbcSMatt Macy zcp->zc_word[1] = BSWAP_64(zcp->zc_word[1]); 74eda14cbcSMatt Macy zcp->zc_word[2] = BSWAP_64(zcp->zc_word[2]); 75eda14cbcSMatt Macy zcp->zc_word[3] = BSWAP_64(zcp->zc_word[3]); 76eda14cbcSMatt Macy } 77eda14cbcSMatt Macy 78eda14cbcSMatt Macy void * 79eda14cbcSMatt Macy abd_checksum_edonr_tmpl_init(const zio_cksum_salt_t *salt) 80eda14cbcSMatt Macy { 81eda14cbcSMatt Macy EdonRState *ctx; 82eda14cbcSMatt Macy uint8_t salt_block[EDONR_BLOCK_SIZE]; 83eda14cbcSMatt Macy 84eda14cbcSMatt Macy /* 85eda14cbcSMatt Macy * Edon-R needs all but the last hash invocation to be on full-size 86eda14cbcSMatt Macy * blocks, but the salt is too small. Rather than simply padding it 87eda14cbcSMatt Macy * with zeros, we expand the salt into a new salt block of proper 88eda14cbcSMatt Macy * size by double-hashing it (the new salt block will be composed of 89eda14cbcSMatt Macy * H(salt) || H(H(salt))). 90eda14cbcSMatt Macy */ 91*c03c5b1cSMartin Matuska _Static_assert(EDONR_BLOCK_SIZE == 2 * (EDONR_MODE / 8), 92*c03c5b1cSMartin Matuska "Edon-R block size mismatch"); 93eda14cbcSMatt Macy EdonRHash(EDONR_MODE, salt->zcs_bytes, sizeof (salt->zcs_bytes) * 8, 94eda14cbcSMatt Macy salt_block); 95eda14cbcSMatt Macy EdonRHash(EDONR_MODE, salt_block, EDONR_MODE, salt_block + 96eda14cbcSMatt Macy EDONR_MODE / 8); 97eda14cbcSMatt Macy 98eda14cbcSMatt Macy /* 99eda14cbcSMatt Macy * Feed the new salt block into the hash function - this will serve 100eda14cbcSMatt Macy * as our MAC key. 101eda14cbcSMatt Macy */ 102eda14cbcSMatt Macy ctx = kmem_zalloc(sizeof (*ctx), KM_SLEEP); 103eda14cbcSMatt Macy EdonRInit(ctx, EDONR_MODE); 104eda14cbcSMatt Macy EdonRUpdate(ctx, salt_block, sizeof (salt_block) * 8); 105eda14cbcSMatt Macy return (ctx); 106eda14cbcSMatt Macy } 107eda14cbcSMatt Macy 108eda14cbcSMatt Macy void 109eda14cbcSMatt Macy abd_checksum_edonr_tmpl_free(void *ctx_template) 110eda14cbcSMatt Macy { 111eda14cbcSMatt Macy EdonRState *ctx = ctx_template; 112eda14cbcSMatt Macy 113eda14cbcSMatt Macy bzero(ctx, sizeof (*ctx)); 114eda14cbcSMatt Macy kmem_free(ctx, sizeof (*ctx)); 115eda14cbcSMatt Macy } 116