Home | History | Annotate | Line # | Download | only in zfs
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://opensource.org/licenses/CDDL-1.0.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 /*
     22  * Copyright 2013 Saso Kiselkov.  All rights reserved.
     23  */
     24 #include <sys/zfs_context.h>
     25 #include <sys/zio.h>
     26 #ifdef _KERNEL
     27 #include <crypto/skein/skein.h>
     28 #else
     29 #include <skein.h>
     30 #endif
     31 
     32 /*
     33  * Computes a native 256-bit skein MAC checksum. Please note that this
     34  * function requires the presence of a ctx_template that should be allocated
     35  * using zio_checksum_skein_tmpl_init.
     36  */
     37 /*ARGSUSED*/
     38 void
     39 zio_checksum_skein_native(const void *buf, uint64_t size,
     40     const void *ctx_template, zio_cksum_t *zcp)
     41 {
     42 	Skein_512_Ctxt_t	ctx;
     43 
     44 	ASSERT(ctx_template != NULL);
     45 	bcopy(ctx_template, &ctx, sizeof (ctx));
     46 	(void) Skein_512_Update(&ctx, buf, size);
     47 	(void) Skein_512_Final(&ctx, (uint8_t *)zcp);
     48 	bzero(&ctx, sizeof (ctx));
     49 }
     50 
     51 /*
     52  * Byteswapped version of zio_checksum_skein_native. This just invokes
     53  * the native checksum function and byteswaps the resulting checksum (since
     54  * skein is internally endian-insensitive).
     55  */
     56 void
     57 zio_checksum_skein_byteswap(const void *buf, uint64_t size,
     58     const void *ctx_template, zio_cksum_t *zcp)
     59 {
     60 	zio_cksum_t	tmp;
     61 
     62 	zio_checksum_skein_native(buf, size, ctx_template, &tmp);
     63 	zcp->zc_word[0] = BSWAP_64(tmp.zc_word[0]);
     64 	zcp->zc_word[1] = BSWAP_64(tmp.zc_word[1]);
     65 	zcp->zc_word[2] = BSWAP_64(tmp.zc_word[2]);
     66 	zcp->zc_word[3] = BSWAP_64(tmp.zc_word[3]);
     67 }
     68 
     69 /*
     70  * Allocates a skein MAC template suitable for using in skein MAC checksum
     71  * computations and returns a pointer to it.
     72  */
     73 void *
     74 zio_checksum_skein_tmpl_init(const zio_cksum_salt_t *salt)
     75 {
     76 	Skein_512_Ctxt_t	*ctx;
     77 
     78 	ctx = kmem_zalloc(sizeof (*ctx), KM_SLEEP);
     79 	(void) Skein_512_InitExt(ctx, sizeof (zio_cksum_t) * 8, 0,
     80 	    salt->zcs_bytes, sizeof (salt->zcs_bytes));
     81 	return (ctx);
     82 }
     83 
     84 /*
     85  * Frees a skein context template previously allocated using
     86  * zio_checksum_skein_tmpl_init.
     87  */
     88 void
     89 zio_checksum_skein_tmpl_free(void *ctx_template)
     90 {
     91 	Skein_512_Ctxt_t	*ctx = ctx_template;
     92 
     93 	bzero(ctx, sizeof (*ctx));
     94 	kmem_free(ctx, sizeof (*ctx));
     95 }
     96