1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://opensource.org/licenses/CDDL-1.0. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2013 Saso Kiselkov. All rights reserved. 24 * Copyright (c) 2016 by Delphix. All rights reserved. 25 */ 26 #include <sys/zfs_context.h> 27 #include <sys/zio.h> 28 #include <sys/zio_checksum.h> 29 #include <sys/skein.h> 30 31 #include <sys/abd.h> 32 33 static int 34 skein_incremental(void *buf, size_t size, void *arg) 35 { 36 Skein_512_Ctxt_t *ctx = arg; 37 (void) Skein_512_Update(ctx, buf, size); 38 return (0); 39 } 40 /* 41 * Computes a native 256-bit skein MAC checksum. Please note that this 42 * function requires the presence of a ctx_template that should be allocated 43 * using abd_checksum_skein_tmpl_init. 44 */ 45 void 46 abd_checksum_skein_native(abd_t *abd, uint64_t size, 47 const void *ctx_template, zio_cksum_t *zcp) 48 { 49 Skein_512_Ctxt_t ctx; 50 51 ASSERT(ctx_template != NULL); 52 memcpy(&ctx, ctx_template, sizeof (ctx)); 53 (void) abd_iterate_func(abd, 0, size, skein_incremental, &ctx); 54 (void) Skein_512_Final(&ctx, (uint8_t *)zcp); 55 memset(&ctx, 0, sizeof (ctx)); 56 } 57 58 /* 59 * Byteswapped version of abd_checksum_skein_native. This just invokes 60 * the native checksum function and byteswaps the resulting checksum (since 61 * skein is internally endian-insensitive). 62 */ 63 void 64 abd_checksum_skein_byteswap(abd_t *abd, uint64_t size, 65 const void *ctx_template, zio_cksum_t *zcp) 66 { 67 zio_cksum_t tmp; 68 69 abd_checksum_skein_native(abd, size, ctx_template, &tmp); 70 zcp->zc_word[0] = BSWAP_64(tmp.zc_word[0]); 71 zcp->zc_word[1] = BSWAP_64(tmp.zc_word[1]); 72 zcp->zc_word[2] = BSWAP_64(tmp.zc_word[2]); 73 zcp->zc_word[3] = BSWAP_64(tmp.zc_word[3]); 74 } 75 76 /* 77 * Allocates a skein MAC template suitable for using in skein MAC checksum 78 * computations and returns a pointer to it. 79 */ 80 void * 81 abd_checksum_skein_tmpl_init(const zio_cksum_salt_t *salt) 82 { 83 Skein_512_Ctxt_t *ctx = kmem_zalloc(sizeof (*ctx), KM_SLEEP); 84 85 (void) Skein_512_InitExt(ctx, sizeof (zio_cksum_t) * 8, 0, 86 salt->zcs_bytes, sizeof (salt->zcs_bytes)); 87 return (ctx); 88 } 89 90 /* 91 * Frees a skein context template previously allocated using 92 * zio_checksum_skein_tmpl_init. 93 */ 94 void 95 abd_checksum_skein_tmpl_free(void *ctx_template) 96 { 97 Skein_512_Ctxt_t *ctx = ctx_template; 98 99 memset(ctx, 0, sizeof (*ctx)); 100 kmem_free(ctx, sizeof (*ctx)); 101 } 102