xref: /illumos-gate/usr/src/uts/common/fs/zfs/edonr_zfs.c (revision 770499e1)
145818ee1SMatthew Ahrens /*
245818ee1SMatthew Ahrens  * CDDL HEADER START
345818ee1SMatthew Ahrens  *
445818ee1SMatthew Ahrens  * The contents of this file are subject to the terms of the
545818ee1SMatthew Ahrens  * Common Development and Distribution License (the "License").
645818ee1SMatthew Ahrens  * You may not use this file except in compliance with the License.
745818ee1SMatthew Ahrens  *
845818ee1SMatthew Ahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
945818ee1SMatthew Ahrens  * or http://opensource.org/licenses/CDDL-1.0.
1045818ee1SMatthew Ahrens  * See the License for the specific language governing permissions
1145818ee1SMatthew Ahrens  * and limitations under the License.
1245818ee1SMatthew Ahrens  *
1345818ee1SMatthew Ahrens  * When distributing Covered Code, include this CDDL HEADER in each
1445818ee1SMatthew Ahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1545818ee1SMatthew Ahrens  * If applicable, add the following below this CDDL HEADER, with the
1645818ee1SMatthew Ahrens  * fields enclosed by brackets "[]" replaced with your own identifying
1745818ee1SMatthew Ahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
1845818ee1SMatthew Ahrens  *
1945818ee1SMatthew Ahrens  * CDDL HEADER END
2045818ee1SMatthew Ahrens  */
2145818ee1SMatthew Ahrens /*
2245818ee1SMatthew Ahrens  * Copyright 2013 Saso Kiselkov.  All rights reserved.
2345818ee1SMatthew Ahrens  * Use is subject to license terms.
2445818ee1SMatthew Ahrens  */
25*770499e1SDan Kimmel /*
26*770499e1SDan Kimmel  * Copyright (c) 2016 by Delphix. All rights reserved.
27*770499e1SDan Kimmel  */
2845818ee1SMatthew Ahrens #include <sys/zfs_context.h>
2945818ee1SMatthew Ahrens #include <sys/zio.h>
3045818ee1SMatthew Ahrens #include <sys/edonr.h>
31*770499e1SDan Kimmel #include <sys/abd.h>
3245818ee1SMatthew Ahrens 
3345818ee1SMatthew Ahrens #define	EDONR_MODE		512
3445818ee1SMatthew Ahrens #define	EDONR_BLOCK_SIZE	EdonR512_BLOCK_SIZE
3545818ee1SMatthew Ahrens 
36*770499e1SDan Kimmel static int
edonr_incremental(void * buf,size_t size,void * arg)37*770499e1SDan Kimmel edonr_incremental(void *buf, size_t size, void *arg)
38*770499e1SDan Kimmel {
39*770499e1SDan Kimmel 	EdonRState *ctx = arg;
40*770499e1SDan Kimmel 	EdonRUpdate(ctx, buf, size * 8);
41*770499e1SDan Kimmel 	return (0);
42*770499e1SDan Kimmel }
43*770499e1SDan Kimmel 
4445818ee1SMatthew Ahrens /*
4545818ee1SMatthew Ahrens  * Native zio_checksum interface for the Edon-R hash function.
4645818ee1SMatthew Ahrens  */
4745818ee1SMatthew Ahrens /*ARGSUSED*/
4845818ee1SMatthew Ahrens void
abd_checksum_edonr_native(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)49*770499e1SDan Kimmel abd_checksum_edonr_native(abd_t *abd, uint64_t size,
5045818ee1SMatthew Ahrens     const void *ctx_template, zio_cksum_t *zcp)
5145818ee1SMatthew Ahrens {
5245818ee1SMatthew Ahrens 	uint8_t		digest[EDONR_MODE / 8];
5345818ee1SMatthew Ahrens 	EdonRState	ctx;
5445818ee1SMatthew Ahrens 
5545818ee1SMatthew Ahrens 	ASSERT(ctx_template != NULL);
5645818ee1SMatthew Ahrens 	bcopy(ctx_template, &ctx, sizeof (ctx));
57*770499e1SDan Kimmel 	(void) abd_iterate_func(abd, 0, size, edonr_incremental, &ctx);
5845818ee1SMatthew Ahrens 	EdonRFinal(&ctx, digest);
5945818ee1SMatthew Ahrens 	bcopy(digest, zcp->zc_word, sizeof (zcp->zc_word));
6045818ee1SMatthew Ahrens }
6145818ee1SMatthew Ahrens 
6245818ee1SMatthew Ahrens /*
6345818ee1SMatthew Ahrens  * Byteswapped zio_checksum interface for the Edon-R hash function.
6445818ee1SMatthew Ahrens  */
6545818ee1SMatthew Ahrens void
abd_checksum_edonr_byteswap(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)66*770499e1SDan Kimmel abd_checksum_edonr_byteswap(abd_t *abd, uint64_t size,
6745818ee1SMatthew Ahrens     const void *ctx_template, zio_cksum_t *zcp)
6845818ee1SMatthew Ahrens {
6945818ee1SMatthew Ahrens 	zio_cksum_t	tmp;
7045818ee1SMatthew Ahrens 
71*770499e1SDan Kimmel 	abd_checksum_edonr_native(abd, size, ctx_template, &tmp);
7245818ee1SMatthew Ahrens 	zcp->zc_word[0] = BSWAP_64(zcp->zc_word[0]);
7345818ee1SMatthew Ahrens 	zcp->zc_word[1] = BSWAP_64(zcp->zc_word[1]);
7445818ee1SMatthew Ahrens 	zcp->zc_word[2] = BSWAP_64(zcp->zc_word[2]);
7545818ee1SMatthew Ahrens 	zcp->zc_word[3] = BSWAP_64(zcp->zc_word[3]);
7645818ee1SMatthew Ahrens }
7745818ee1SMatthew Ahrens 
7845818ee1SMatthew Ahrens void *
abd_checksum_edonr_tmpl_init(const zio_cksum_salt_t * salt)79*770499e1SDan Kimmel abd_checksum_edonr_tmpl_init(const zio_cksum_salt_t *salt)
8045818ee1SMatthew Ahrens {
8145818ee1SMatthew Ahrens 	EdonRState	*ctx;
8245818ee1SMatthew Ahrens 	uint8_t		salt_block[EDONR_BLOCK_SIZE];
8345818ee1SMatthew Ahrens 
8445818ee1SMatthew Ahrens 	/*
8545818ee1SMatthew Ahrens 	 * Edon-R needs all but the last hash invocation to be on full-size
8645818ee1SMatthew Ahrens 	 * blocks, but the salt is too small. Rather than simply padding it
8745818ee1SMatthew Ahrens 	 * with zeros, we expand the salt into a new salt block of proper
8845818ee1SMatthew Ahrens 	 * size by double-hashing it (the new salt block will be composed of
8945818ee1SMatthew Ahrens 	 * H(salt) || H(H(salt))).
9045818ee1SMatthew Ahrens 	 */
9145818ee1SMatthew Ahrens 	CTASSERT(EDONR_BLOCK_SIZE == 2 * (EDONR_MODE / 8));
9245818ee1SMatthew Ahrens 	EdonRHash(EDONR_MODE, salt->zcs_bytes, sizeof (salt->zcs_bytes) * 8,
9345818ee1SMatthew Ahrens 	    salt_block);
9445818ee1SMatthew Ahrens 	EdonRHash(EDONR_MODE, salt_block, EDONR_MODE, salt_block +
9545818ee1SMatthew Ahrens 	    EDONR_MODE / 8);
9645818ee1SMatthew Ahrens 
9745818ee1SMatthew Ahrens 	/*
9845818ee1SMatthew Ahrens 	 * Feed the new salt block into the hash function - this will serve
9945818ee1SMatthew Ahrens 	 * as our MAC key.
10045818ee1SMatthew Ahrens 	 */
10145818ee1SMatthew Ahrens 	ctx = kmem_zalloc(sizeof (*ctx), KM_SLEEP);
10245818ee1SMatthew Ahrens 	EdonRInit(ctx, EDONR_MODE);
10345818ee1SMatthew Ahrens 	EdonRUpdate(ctx, salt_block, sizeof (salt_block) * 8);
10445818ee1SMatthew Ahrens 	return (ctx);
10545818ee1SMatthew Ahrens }
10645818ee1SMatthew Ahrens 
10745818ee1SMatthew Ahrens void
abd_checksum_edonr_tmpl_free(void * ctx_template)108*770499e1SDan Kimmel abd_checksum_edonr_tmpl_free(void *ctx_template)
10945818ee1SMatthew Ahrens {
11045818ee1SMatthew Ahrens 	EdonRState	*ctx = ctx_template;
11145818ee1SMatthew Ahrens 
11245818ee1SMatthew Ahrens 	bzero(ctx, sizeof (*ctx));
11345818ee1SMatthew Ahrens 	kmem_free(ctx, sizeof (*ctx));
11445818ee1SMatthew Ahrens }
115