Home | History | Annotate | Line # | Download | only in libkern
      1 /*	$NetBSD: entpool.h,v 1.1 2020/04/30 03:28:19 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2019 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef	ENTPOOL_H
     33 #define	ENTPOOL_H
     34 
     35 #include <sys/types.h>
     36 #include <sys/endian.h>
     37 
     38 #if defined(_KERNEL) || defined(_STANDALONE)
     39 #include <sys/stdbool.h>
     40 #else
     41 #include <stdbool.h>
     42 #endif
     43 
     44 #if ENTPOOL_SMALL
     45 #define	ENTPOOL_HEADER		<gimli.h>
     46 #define	ENTPOOL_PERMUTE		gimli
     47 #define	ENTPOOL_SIZE		48
     48 #define	ENTPOOL_WORD		uint32_t
     49 #define	ENTPOOL_WTOH		le32toh
     50 #define	ENTPOOL_HTOW		htole32
     51 #define	ENTPOOL_SECURITY	16
     52 #else
     53 #define	ENTPOOL_HEADER		<keccak.h>
     54 #define	ENTPOOL_PERMUTE		keccakf1600
     55 #define	ENTPOOL_SIZE		200
     56 #define	ENTPOOL_WORD		uint64_t
     57 #define	ENTPOOL_WTOH		le64toh
     58 #define	ENTPOOL_HTOW		htole64
     59 #define	ENTPOOL_SECURITY	16
     60 #endif
     61 
     62 #define	ENTPOOL_CAPACITY	(2*ENTPOOL_SECURITY)
     63 #define	ENTPOOL_RATE		(ENTPOOL_SIZE - ENTPOOL_CAPACITY)
     64 
     65 struct entpool {
     66 	union {
     67 		uint8_t		u8[ENTPOOL_SIZE];
     68 		ENTPOOL_WORD	w[ENTPOOL_SIZE/sizeof(ENTPOOL_WORD)];
     69 	}		s;
     70 	unsigned	i;
     71 };
     72 
     73 int	entpool_selftest(void);
     74 void	entpool_enter(struct entpool *, const void *, size_t);
     75 bool	entpool_enter_nostir(struct entpool *, const void *, size_t);
     76 void	entpool_stir(struct entpool *);
     77 void	entpool_extract(struct entpool *, void *, size_t);
     78 
     79 #endif	/* ENTPOOL_H */
     80