squid: update to 6.13
[openadk.git] / package / cfgfs / src / sys_bsd.c
blobb37fd56375fd6a61313335bac88bc519d167a39a
1 /* $MirOS: contrib/hosted/fwcf/sys_bsd.c,v 1.4 2007/03/13 18:14:30 tg Exp $ */
3 /*-
4 * Copyright (c) 2006, 2007
5 * Thorsten Glaser <tg@mirbsd.de>
7 * Provided that these terms and disclaimer and all copyright notices
8 * are retained or reproduced in an accompanying document, permission
9 * is granted to deal in this work without restriction, including un-
10 * limited rights to use, publicly perform, distribute, sell, modify,
11 * merge, give away, or sublicence.
13 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
14 * the utmost extent permitted by applicable law, neither express nor
15 * implied; without malicious intent or gross negligence. In no event
16 * may a licensor, author or contributor be held liable for indirect,
17 * direct, other damage, loss, or other issues arising in any way out
18 * of dealing in the work, even if advised of the possibility of such
19 * damage or existence of a defect, except proven that it results out
20 * of said person's immediate fault when using the work as intended.
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
27 #include "defs.h"
28 #include "sysdeps.h"
30 __RCSID("$MirOS: contrib/hosted/fwcf/sys_bsd.c,v 1.4 2007/03/13 18:14:30 tg Exp $");
32 void
33 pull_rndata(uint8_t *buf, size_t n)
35 #ifdef RND_DISABLE
36 while (n--)
37 *buf++ = 0xF6;
38 *--buf = 0xFF;
39 #else
40 #ifdef RND_DEBUG
41 fprintf(stderr, "writing %ld bytes of entropy\n", n);
42 while (n > 4) {
43 #else
44 while (n >= 4) {
45 #endif
46 *(uint32_t *)buf = arc4random();
47 #ifdef RND_DEBUG
48 *buf = 0xF6;
49 #endif
50 buf += 4;
51 n -= 4;
53 while (n) {
54 #ifdef RND_DEBUG
55 *buf++ = 0xF6;
56 #else
57 *buf++ = arc4random() & 0xFF;
58 #endif
59 n--;
61 #ifdef RND_DEBUG
62 *--buf = 0xFF;
63 #endif
64 #endif
67 void
68 push_rndata(uint8_t *buf, size_t n)
70 #ifdef RND_DEBUG
71 size_t i;
72 #endif
73 #ifdef __MirBSD__
74 arc4random_pushb(buf, n);
75 #else
76 int fd;
77 uint32_t x;
79 arc4random_addrandom(buf, n);
80 x = arc4random();
81 if ((fd = open("/dev/arandom", O_WRONLY)) >= 0) {
82 write(fd, &x, 4);
83 close(fd);
84 } else
85 warn("cannot write to /dev/arandom");
86 #endif
87 #ifdef RND_DEBUG
88 printf("reading %ld bytes of entropy\n", n);
89 for (i = 0; i < n; ++i) {
90 printf(" %02X", buf[i]);
91 if ((i & 0xF) == 0xF)
92 putchar('\n');
94 putchar('\n');
95 #endif