1289310Smav/*
2289310Smav * CDDL HEADER START
3289310Smav *
4289310Smav * The contents of this file are subject to the terms of the
5289310Smav * Common Development and Distribution License (the "License").
6289310Smav * You may not use this file except in compliance with the License.
7289310Smav *
8289310Smav * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9289310Smav * or http://opensource.org/licenses/CDDL-1.0.
10289310Smav * See the License for the specific language governing permissions
11289310Smav * and limitations under the License.
12289310Smav *
13289310Smav * When distributing Covered Code, include this CDDL HEADER in each
14289310Smav * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15289310Smav * If applicable, add the following below this CDDL HEADER, with the
16289310Smav * fields enclosed by brackets "[]" replaced with your own identifying
17289310Smav * information: Portions Copyright [yyyy] [name of copyright owner]
18289310Smav *
19289310Smav * CDDL HEADER END
20289310Smav */
21289310Smav/*
22289310Smav * Copyright 2013 Saso Kiselkov.  All rights reserved.
23289310Smav * Use is subject to license terms.
24289310Smav */
25289310Smav#include <sys/zfs_context.h>
26289310Smav#include <sys/zio.h>
27289310Smav#include <sys/edonr.h>
28289310Smav
29289310Smav#define	EDONR_MODE		512
30289310Smav#define	EDONR_BLOCK_SIZE	EdonR512_BLOCK_SIZE
31289310Smav
32289310Smav/*
33289310Smav * Native zio_checksum interface for the Edon-R hash function.
34289310Smav */
35289310Smav/*ARGSUSED*/
36289310Smavvoid
37289310Smavzio_checksum_edonr_native(const void *buf, uint64_t size,
38289310Smav    const void *ctx_template, zio_cksum_t *zcp)
39289310Smav{
40289310Smav	uint8_t		digest[EDONR_MODE / 8];
41289310Smav	EdonRState	ctx;
42289310Smav
43289310Smav	ASSERT(ctx_template != NULL);
44289310Smav	bcopy(ctx_template, &ctx, sizeof (ctx));
45289310Smav	EdonRUpdate(&ctx, buf, size * 8);
46289310Smav	EdonRFinal(&ctx, digest);
47289310Smav	bcopy(digest, zcp->zc_word, sizeof (zcp->zc_word));
48289310Smav}
49289310Smav
50289310Smav/*
51289310Smav * Byteswapped zio_checksum interface for the Edon-R hash function.
52289310Smav */
53289310Smavvoid
54289310Smavzio_checksum_edonr_byteswap(const void *buf, uint64_t size,
55289310Smav    const void *ctx_template, zio_cksum_t *zcp)
56289310Smav{
57289310Smav	zio_cksum_t	tmp;
58289310Smav
59289310Smav	zio_checksum_edonr_native(buf, size, ctx_template, &tmp);
60289310Smav	zcp->zc_word[0] = BSWAP_64(zcp->zc_word[0]);
61289310Smav	zcp->zc_word[1] = BSWAP_64(zcp->zc_word[1]);
62289310Smav	zcp->zc_word[2] = BSWAP_64(zcp->zc_word[2]);
63289310Smav	zcp->zc_word[3] = BSWAP_64(zcp->zc_word[3]);
64289310Smav}
65289310Smav
66289310Smavvoid *
67289310Smavzio_checksum_edonr_tmpl_init(const zio_cksum_salt_t *salt)
68289310Smav{
69289310Smav	EdonRState	*ctx;
70289310Smav	uint8_t		salt_block[EDONR_BLOCK_SIZE];
71289310Smav
72289310Smav	/*
73289310Smav	 * Edon-R needs all but the last hash invocation to be on full-size
74289310Smav	 * blocks, but the salt is too small. Rather than simply padding it
75289310Smav	 * with zeros, we expand the salt into a new salt block of proper
76289310Smav	 * size by double-hashing it (the new salt block will be composed of
77289310Smav	 * H(salt) || H(H(salt))).
78289310Smav	 */
79289310Smav	CTASSERT(EDONR_BLOCK_SIZE == 2 * (EDONR_MODE / 8));
80289310Smav	EdonRHash(EDONR_MODE, salt->zcs_bytes, sizeof (salt->zcs_bytes) * 8,
81289310Smav	    salt_block);
82289310Smav	EdonRHash(EDONR_MODE, salt_block, EDONR_MODE, salt_block +
83289310Smav	    EDONR_MODE / 8);
84289310Smav
85289310Smav	/*
86289310Smav	 * Feed the new salt block into the hash function - this will serve
87289310Smav	 * as our MAC key.
88289310Smav	 */
89289310Smav	ctx = kmem_zalloc(sizeof (*ctx), KM_SLEEP);
90289310Smav	EdonRInit(ctx, EDONR_MODE);
91289310Smav	EdonRUpdate(ctx, salt_block, sizeof (salt_block) * 8);
92289310Smav	return (ctx);
93289310Smav}
94289310Smav
95289310Smavvoid
96289310Smavzio_checksum_edonr_tmpl_free(void *ctx_template)
97289310Smav{
98289310Smav	EdonRState	*ctx = ctx_template;
99289310Smav
100289310Smav	bzero(ctx, sizeof (*ctx));
101289310Smav	kmem_free(ctx, sizeof (*ctx));
102289310Smav}
103