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 * Use is subject to license terms.
25 */
26 /*
27 * Copyright (c) 2016 by Delphix. All rights reserved.
28 */
29 #include <sys/zfs_context.h>
30 #include <sys/zio.h>
31 #include <sys/zio_checksum.h>
32 #include <sys/edonr.h>
33 #include <sys/abd.h>
34
35 #define EDONR_MODE 512
36 #define EDONR_BLOCK_SIZE EdonR512_BLOCK_SIZE
37
38 static int
edonr_incremental(void * buf,size_t size,void * arg)39 edonr_incremental(void *buf, size_t size, void *arg)
40 {
41 EdonRState *ctx = arg;
42 EdonRUpdate(ctx, buf, size * 8);
43 return (0);
44 }
45
46 /*
47 * Native zio_checksum interface for the Edon-R hash function.
48 */
49 void
abd_checksum_edonr_native(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)50 abd_checksum_edonr_native(abd_t *abd, uint64_t size,
51 const void *ctx_template, zio_cksum_t *zcp)
52 {
53 uint8_t digest[EDONR_MODE / 8];
54 EdonRState ctx;
55
56 ASSERT(ctx_template != NULL);
57 memcpy(&ctx, ctx_template, sizeof (ctx));
58 (void) abd_iterate_func(abd, 0, size, edonr_incremental, &ctx);
59 EdonRFinal(&ctx, digest);
60 memcpy(zcp->zc_word, digest, sizeof (zcp->zc_word));
61 }
62
63 /*
64 * Byteswapped zio_checksum interface for the Edon-R hash function.
65 */
66 void
abd_checksum_edonr_byteswap(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)67 abd_checksum_edonr_byteswap(abd_t *abd, uint64_t size,
68 const void *ctx_template, zio_cksum_t *zcp)
69 {
70 zio_cksum_t tmp;
71
72 abd_checksum_edonr_native(abd, size, ctx_template, &tmp);
73 zcp->zc_word[0] = BSWAP_64(zcp->zc_word[0]);
74 zcp->zc_word[1] = BSWAP_64(zcp->zc_word[1]);
75 zcp->zc_word[2] = BSWAP_64(zcp->zc_word[2]);
76 zcp->zc_word[3] = BSWAP_64(zcp->zc_word[3]);
77 }
78
79 void *
abd_checksum_edonr_tmpl_init(const zio_cksum_salt_t * salt)80 abd_checksum_edonr_tmpl_init(const zio_cksum_salt_t *salt)
81 {
82 EdonRState *ctx;
83 uint8_t salt_block[EDONR_BLOCK_SIZE];
84
85 /*
86 * Edon-R needs all but the last hash invocation to be on full-size
87 * blocks, but the salt is too small. Rather than simply padding it
88 * with zeros, we expand the salt into a new salt block of proper
89 * size by double-hashing it (the new salt block will be composed of
90 * H(salt) || H(H(salt))).
91 */
92 _Static_assert(EDONR_BLOCK_SIZE == 2 * (EDONR_MODE / 8),
93 "Edon-R block size mismatch");
94 EdonRHash(salt->zcs_bytes, sizeof (salt->zcs_bytes) * 8, salt_block);
95 EdonRHash(salt_block, EDONR_MODE, salt_block + EDONR_MODE / 8);
96
97 /*
98 * Feed the new salt block into the hash function - this will serve
99 * as our MAC key.
100 */
101 ctx = kmem_zalloc(sizeof (*ctx), KM_SLEEP);
102 EdonRInit(ctx);
103 EdonRUpdate(ctx, salt_block, sizeof (salt_block) * 8);
104 return (ctx);
105 }
106
107 void
abd_checksum_edonr_tmpl_free(void * ctx_template)108 abd_checksum_edonr_tmpl_free(void *ctx_template)
109 {
110 EdonRState *ctx = ctx_template;
111
112 memset(ctx, 0, sizeof (*ctx));
113 kmem_free(ctx, sizeof (*ctx));
114 }
115