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