unit_test.c revision 302408
1116087Sjmallett/*- 2116087Sjmallett * Copyright (c) 2000-2015 Mark R V Murray 3116087Sjmallett * All rights reserved. 4116087Sjmallett * 5116087Sjmallett * Redistribution and use in source and binary forms, with or without 6116087Sjmallett * modification, are permitted provided that the following conditions 7116087Sjmallett * are met: 8116163Sjmallett * 1. Redistributions of source code must retain the above copyright 9116163Sjmallett * notice, this list of conditions and the following disclaimer 10116087Sjmallett * in this position and unchanged. 11116087Sjmallett * 2. Redistributions in binary form must reproduce the above copyright 12131468Sru * notice, this list of conditions and the following disclaimer in the 13116087Sjmallett * documentation and/or other materials provided with the distribution. 14116087Sjmallett * 15116087Sjmallett * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16116087Sjmallett * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17131609Sru * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18116087Sjmallett * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19116087Sjmallett * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20116087Sjmallett * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21116087Sjmallett * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22116087Sjmallett * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23116087Sjmallett * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24116087Sjmallett * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25116087Sjmallett * 26116087Sjmallett * $FreeBSD: stable/11/sys/dev/random/unit_test.c 292782 2015-12-27 17:33:59Z allanjude $ 27116087Sjmallett */ 28116087Sjmallett 29116087Sjmallett/* 30131609Sru Build this by going: 31131609Sru 32131609Srucc -g -O0 -pthread -DRANDOM_<alg> -I../.. -lstdthreads -Wall \ 33131609Sru unit_test.c \ 34174668Sphk yarrow.c \ 35174668Sphk fortuna.c \ 36174668Sphk hash.c \ 37174668Sphk ../../crypto/rijndael/rijndael-api-fst.c \ 38116087Sjmallett ../../crypto/rijndael/rijndael-alg-fst.c \ 39116087Sjmallett ../../crypto/sha2/sha256c.c \ 40174668Sphk -lz \ 41174668Sphk -o unit_test 42116087Sjmallett./unit_test 43174668Sphk 44174668SphkWhere <alg> is YARROW or FORTUNA. 45116087Sjmallett*/ 46116087Sjmallett 47116087Sjmallett#include <sys/types.h> 48116087Sjmallett#include <inttypes.h> 49131609Sru#include <stdbool.h> 50116087Sjmallett#include <stdio.h> 51131609Sru#include <stdlib.h> 52116087Sjmallett#include <threads.h> 53116087Sjmallett#include <unistd.h> 54116087Sjmallett#include <zlib.h> 55116087Sjmallett 56131609Sru#include "randomdev.h" 57116087Sjmallett#include "unit_test.h" 58174668Sphk 59174668Sphk#define NUM_THREADS 3 60174668Sphk#define DEBUG 61174668Sphk 62116087Sjmallettstatic volatile int stopseeding = 0; 63116087Sjmallett 64116087Sjmallettstatic __inline void 65116087Sjmallettcheck_err(int err, const char *func) 66116087Sjmallett{ 67116087Sjmallett if (err != Z_OK) { 68116165Sjmallett fprintf(stderr, "Compress error in %s: %d\n", func, err); 69116087Sjmallett exit(0); 70116087Sjmallett } 71116087Sjmallett} 72116087Sjmallett 73116087Sjmallettvoid * 74116087Sjmallettmyalloc(void *q, unsigned n, unsigned m) 75116087Sjmallett{ 76116087Sjmallett q = Z_NULL; 77116087Sjmallett return (calloc(n, m)); 78174668Sphk} 79174668Sphk 80174668Sphkvoid myfree(void *q, void *p) 81174668Sphk{ 82174668Sphk q = Z_NULL; 83174668Sphk free(p); 84174668Sphk} 85174668Sphk 86174668Sphksize_t 87116087Sjmallettblock_deflate(uint8_t *uncompr, uint8_t *compr, const size_t len) 88116087Sjmallett{ 89116087Sjmallett z_stream c_stream; 90116087Sjmallett int err; 91116087Sjmallett 92116087Sjmallett if (len == 0) 93116087Sjmallett return (0); 94116087Sjmallett 95116087Sjmallett c_stream.zalloc = myalloc; 96116087Sjmallett c_stream.zfree = myfree; 97116087Sjmallett c_stream.opaque = NULL; 98116087Sjmallett 99267773Sbapt err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); 100 check_err(err, "deflateInit"); 101 102 c_stream.next_in = uncompr; 103 c_stream.next_out = compr; 104 c_stream.avail_in = len; 105 c_stream.avail_out = len*2u +512u; 106 107 while (c_stream.total_in != len && c_stream.total_out < (len*2u + 512u)) { 108 err = deflate(&c_stream, Z_NO_FLUSH); 109#ifdef DEBUG 110 printf("deflate progress: len = %zd total_in = %lu total_out = %lu\n", len, c_stream.total_in, c_stream.total_out); 111#endif 112 check_err(err, "deflate(..., Z_NO_FLUSH)"); 113 } 114 115 for (;;) { 116 err = deflate(&c_stream, Z_FINISH); 117#ifdef DEBUG 118 printf("deflate final: len = %zd total_in = %lu total_out = %lu\n", len, c_stream.total_in, c_stream.total_out); 119#endif 120 if (err == Z_STREAM_END) break; 121 check_err(err, "deflate(..., Z_STREAM_END)"); 122 } 123 124 err = deflateEnd(&c_stream); 125 check_err(err, "deflateEnd"); 126 127 return ((size_t)c_stream.total_out); 128} 129 130void 131randomdev_unblock(void) 132{ 133 134#if 0 135 if (mtx_trylock(&random_reseed_mtx) == thrd_busy) 136 printf("Mutex held. Good.\n"); 137 else { 138 printf("Mutex not held. PANIC!!\n"); 139 thrd_exit(0); 140 } 141#endif 142 printf("random: unblocking device.\n"); 143} 144 145static int 146RunHarvester(void *arg __unused) 147{ 148 int i, r; 149 struct harvest_event e; 150 151 for (i = 0; ; i++) { 152 if (stopseeding) 153 break; 154 if (i % 1000 == 0) 155 printf("Harvest: %d\n", i); 156 r = random()%10; 157 e.he_somecounter = i; 158 *((uint64_t *)e.he_entropy) = random(); 159 e.he_size = 8; 160 e.he_bits = random()%4; 161 e.he_destination = i; 162 e.he_source = (i + 3)%7; 163 e.he_next = NULL; 164 random_alg_context.ra_event_processor(&e); 165 usleep(r); 166 } 167 168 printf("Thread #0 ends\n"); 169 170 thrd_exit(0); 171 172 return (0); 173} 174 175static int 176ReadCSPRNG(void *threadid) 177{ 178 size_t tid, zsize; 179 u_int buffersize; 180 uint8_t *buf, *zbuf; 181 int i; 182#ifdef DEBUG 183 int j; 184#endif 185 186 tid = (size_t)threadid; 187 printf("Thread #%zd starts\n", tid); 188 189 while (!random_alg_context.ra_seeded()) 190 { 191 random_alg_context.ra_pre_read(); 192 usleep(100); 193 } 194 195 for (i = 0; i < 100000; i++) { 196 buffersize = i + RANDOM_BLOCKSIZE; 197 buffersize -= buffersize%RANDOM_BLOCKSIZE; 198 buf = malloc(buffersize); 199 zbuf = malloc(2*i + 1024); 200 if (i % 1000 == 0) 201 printf("Thread read %zd - %d\n", tid, i); 202 if (buf != NULL && zbuf != NULL) { 203 random_alg_context.ra_pre_read(); 204 random_alg_context.ra_read(buf, buffersize); 205 zsize = block_deflate(buf, zbuf, i); 206 if (zsize < i) 207 printf("ERROR!! Compressible RNG output!\n"); 208#ifdef DEBUG 209 printf("RNG output:\n"); 210 for (j = 0; j < i; j++) { 211 printf(" %02X", buf[j]); 212 if (j % 32 == 31 || j == i - 1) 213 printf("\n"); 214 } 215 printf("Compressed output:\n"); 216 for (j = 0; j < zsize; j++) { 217 printf(" %02X", zbuf[j]); 218 if (j % 32 == 31 || j == zsize - 1) 219 printf("\n"); 220 } 221#endif 222 free(zbuf); 223 free(buf); 224 } 225 usleep(100); 226 } 227 228 printf("Thread #%zd ends\n", tid); 229 230 thrd_exit(0); 231 232 return (0); 233} 234 235int 236main(int argc, char *argv[]) 237{ 238 thrd_t threads[NUM_THREADS]; 239 int rc; 240 long t; 241 242 random_alg_context.ra_init_alg(NULL); 243 244 for (t = 0; t < NUM_THREADS; t++) { 245 printf("In main: creating thread %ld\n", t); 246 rc = thrd_create(&threads[t], (t == 0 ? RunHarvester : ReadCSPRNG), NULL); 247 if (rc != thrd_success) { 248 printf("ERROR; return code from thrd_create() is %d\n", rc); 249 exit(-1); 250 } 251 } 252 253 for (t = 2; t < NUM_THREADS; t++) 254 thrd_join(threads[t], &rc); 255 256 stopseeding = 1; 257 258 thrd_join(threads[1], &rc); 259 thrd_join(threads[0], &rc); 260 261 random_alg_context.ra_deinit_alg(NULL); 262 263 /* Last thing that main() should do */ 264 thrd_exit(0); 265 266 return (0); 267} 268