1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * 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.LICENSE
9 * or http://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2022 Tino Reichardt <milky-zfs@mcmilk.de>
24 */
25
26 #include <sys/zfs_context.h>
27 #include <sys/zio_checksum.h>
28 #include <sys/blake3.h>
29 #include <sys/abd.h>
30
31 static int
32 blake3_incremental(void *buf, size_t size, void *arg)
33 {
34 BLAKE3_CTX *ctx = arg;
35
36 Blake3_Update(ctx, buf, size);
37
38 return (0);
39 }
40
41 /*
42 * Computes a native 256-bit BLAKE3 MAC checksum. Please note that this
43 * function requires the presence of a ctx_template that should be allocated
44 * using abd_checksum_blake3_tmpl_init.
45 */
46 void
47 abd_checksum_blake3_native(abd_t *abd, uint64_t size, const void *ctx_template,
48 zio_cksum_t *zcp)
49 {
50 ASSERT(ctx_template != NULL);
51
52 #if defined(_KERNEL)
53 BLAKE3_CTX *ctx = blake3_per_cpu_ctx[CPU_SEQID_UNSTABLE];
54 #else
55 BLAKE3_CTX *ctx = kmem_alloc(sizeof (*ctx), KM_SLEEP);
56 #endif
57
58 memcpy(ctx, ctx_template, sizeof (*ctx));
59 (void) abd_iterate_func(abd, 0, size, blake3_incremental, ctx);
60 Blake3_Final(ctx, (uint8_t *)zcp);
61
62 #if !defined(_KERNEL)
63 memset(ctx, 0, sizeof (*ctx));
64 kmem_free(ctx, sizeof (*ctx));
65 #endif
66 }
67
68 /*
69 * Byteswapped version of abd_checksum_blake3_native. This just invokes
70 * the native checksum function and byteswaps the resulting checksum (since
71 * BLAKE3 is internally endian-insensitive).
72 */
73 void
74 abd_checksum_blake3_byteswap(abd_t *abd, uint64_t size,
75 const void *ctx_template, zio_cksum_t *zcp)
76 {
77 zio_cksum_t tmp;
78
79 ASSERT(ctx_template != NULL);
80
81 abd_checksum_blake3_native(abd, size, ctx_template, &tmp);
82 zcp->zc_word[0] = BSWAP_64(tmp.zc_word[0]);
83 zcp->zc_word[1] = BSWAP_64(tmp.zc_word[1]);
84 zcp->zc_word[2] = BSWAP_64(tmp.zc_word[2]);
85 zcp->zc_word[3] = BSWAP_64(tmp.zc_word[3]);
86 }
87
88 /*
89 * Allocates a BLAKE3 MAC template suitable for using in BLAKE3 MAC checksum
90 * computations and returns a pointer to it.
91 */
92 void *
93 abd_checksum_blake3_tmpl_init(const zio_cksum_salt_t *salt)
94 {
95 BLAKE3_CTX *ctx;
96
97 ASSERT(sizeof (salt->zcs_bytes) == 32);
98
99 /* init reference object */
100 ctx = kmem_zalloc(sizeof (*ctx), KM_SLEEP);
101 Blake3_InitKeyed(ctx, salt->zcs_bytes);
102
103 return (ctx);
104 }
105
106 /*
107 * Frees a BLAKE3 context template previously allocated using
108 * zio_checksum_blake3_tmpl_init.
109 */
110 void
111 abd_checksum_blake3_tmpl_free(void *ctx_template)
112 {
113 BLAKE3_CTX *ctx = ctx_template;
114
115 memset(ctx, 0, sizeof (*ctx));
116 kmem_free(ctx, sizeof (*ctx));
117 }
Cache object: a6659d123acdb9c63b6d4d3b14dbaf59
|