/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://opensource.org/licenses/CDDL-1.0.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/2021/*22* Copyright 2022 Tino Reichardt <[email protected]>23*/2425#include <sys/blake3.h>2627/*28* Computes a native 256-bit BLAKE3 MAC checksum. Please note that this29* function requires the presence of a ctx_template that should be allocated30* using zio_checksum_blake3_tmpl_init.31*/32static void33zio_checksum_blake3_native(const void *buf, uint64_t size,34const void *ctx_template, zio_cksum_t *zcp)35{36BLAKE3_CTX ctx;3738ASSERT(ctx_template != 0);3940memcpy(&ctx, ctx_template, sizeof(ctx));41Blake3_Update(&ctx, buf, size);42Blake3_Final(&ctx, (uint8_t *)zcp);4344memset(&ctx, 0, sizeof (ctx));45}4647/*48* Byteswapped version of zio_checksum_blake3_native. This just invokes49* the native checksum function and byteswaps the resulting checksum (since50* BLAKE3 is internally endian-insensitive).51*/52static void53zio_checksum_blake3_byteswap(const void *buf, uint64_t size,54const void *ctx_template, zio_cksum_t *zcp)55{56zio_cksum_t tmp;5758ASSERT(ctx_template != 0);5960zio_checksum_blake3_native(buf, size, ctx_template, &tmp);61zcp->zc_word[0] = BSWAP_64(tmp.zc_word[0]);62zcp->zc_word[1] = BSWAP_64(tmp.zc_word[1]);63zcp->zc_word[2] = BSWAP_64(tmp.zc_word[2]);64zcp->zc_word[3] = BSWAP_64(tmp.zc_word[3]);65}6667/*68* Allocates a BLAKE3 MAC template suitable for using in BLAKE3 MAC checksum69* computations and returns a pointer to it.70*/71static void *72zio_checksum_blake3_tmpl_init(const zio_cksum_salt_t *salt)73{74BLAKE3_CTX *ctx;7576ASSERT(sizeof (salt->zcs_bytes) == 32);7778/* init reference object */79ctx = calloc(1, sizeof(*ctx));80Blake3_InitKeyed(ctx, salt->zcs_bytes);8182return (ctx);83}8485/*86* Frees a BLAKE3 context template previously allocated using87* zio_checksum_blake3_tmpl_init.88*/89static void90zio_checksum_blake3_tmpl_free(void *ctx_template)91{92BLAKE3_CTX *ctx = ctx_template;9394memset(ctx, 0, sizeof(*ctx));95free(ctx);96}979899