[87337dc5] | 1 | /*
|
---|
| 2 | * Copyright (c) 2018 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libc
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * @brief Random number generator.
|
---|
| 35 | *
|
---|
| 36 | * Generate random (as opposed to pseudorandom) numbers. This should be
|
---|
| 37 | * used sparingly (e.g. to seed a pseudorandom number generator).
|
---|
| 38 | */
|
---|
| 39 |
|
---|
| 40 | #include <errno.h>
|
---|
| 41 | #include <rndgen.h>
|
---|
| 42 | #include <stdint.h>
|
---|
| 43 | #include <stdlib.h>
|
---|
| 44 | #include <time.h>
|
---|
| 45 |
|
---|
| 46 | /** Create random number generator.
|
---|
| 47 | *
|
---|
| 48 | * @param rrndgen Place to store new random number generator
|
---|
| 49 | * @return EOK on success or error code
|
---|
| 50 | */
|
---|
| 51 | errno_t rndgen_create(rndgen_t **rrndgen)
|
---|
| 52 | {
|
---|
| 53 | rndgen_t *rndgen;
|
---|
| 54 | struct timeval tv;
|
---|
| 55 |
|
---|
| 56 | rndgen = calloc(1, sizeof(rndgen_t));
|
---|
| 57 | if (rndgen == NULL)
|
---|
| 58 | return ENOMEM;
|
---|
| 59 |
|
---|
| 60 | /* XXX This is a rather poor way of generating random numbers */
|
---|
| 61 | gettimeofday(&tv, NULL);
|
---|
| 62 | rndgen->seed = tv.tv_sec ^ tv.tv_usec;
|
---|
| 63 |
|
---|
| 64 | *rrndgen = rndgen;
|
---|
| 65 | return EOK;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | /** Destroy random number generator.
|
---|
| 69 | *
|
---|
| 70 | * @param rndgen Random number generator or @c NULL
|
---|
| 71 | */
|
---|
| 72 | void rndgen_destroy(rndgen_t *rndgen)
|
---|
| 73 | {
|
---|
| 74 | if (rndgen == NULL)
|
---|
| 75 | return;
|
---|
| 76 |
|
---|
| 77 | free(rndgen);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /** Generate random 8-bit integer.
|
---|
| 81 | *
|
---|
| 82 | * @param rndgen Random number generator
|
---|
| 83 | * @param rb Place to store random 8-bit integer
|
---|
| 84 | * @return EOK on success or error code
|
---|
| 85 | */
|
---|
| 86 | errno_t rndgen_uint8(rndgen_t *rndgen, uint8_t *rb)
|
---|
| 87 | {
|
---|
| 88 | rndgen->seed = (1366 * rndgen->seed + 150889) % 714025;
|
---|
| 89 |
|
---|
| 90 | *rb = rndgen->seed & 0xff;
|
---|
| 91 | return EOK;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | /** Generate random 32-bit integer.
|
---|
| 95 | *
|
---|
| 96 | * @param rndgen Random number generator
|
---|
| 97 | * @param rw Place to store random 32-bit integer
|
---|
| 98 | * @return EOK on success or error code
|
---|
| 99 | */
|
---|
| 100 | errno_t rndgen_uint32(rndgen_t *rndgen, uint32_t *rw)
|
---|
| 101 | {
|
---|
| 102 | int i;
|
---|
| 103 | uint8_t b;
|
---|
| 104 | uint32_t w;
|
---|
| 105 | errno_t rc;
|
---|
| 106 |
|
---|
| 107 | w = 0;
|
---|
| 108 | for (i = 0; i < 4; i++) {
|
---|
| 109 | rc = rndgen_uint8(rndgen, &b);
|
---|
| 110 | if (rc != EOK)
|
---|
| 111 | return rc;
|
---|
| 112 |
|
---|
| 113 | w = (w << 8) | b;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | *rw = w;
|
---|
| 117 | return EOK;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /** @}
|
---|
| 121 | */
|
---|