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