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