Rich Salz | 440e5d8 | 2016-05-17 14:20:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3 | * |
Rich Salz | 440e5d8 | 2016-05-17 14:20:24 -0400 | [diff] [blame] | 4 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 5 | * this file except in compliance with the License. You can obtain a copy |
| 6 | * in the file LICENSE in the source distribution or at |
| 7 | * https://www.openssl.org/source/license.html |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
Bodo Möller | ec57782 | 1999-04-23 22:13:45 +0000 | [diff] [blame] | 12 | #include <openssl/rand.h> |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 13 | |
Richard Levitte | 55f78ba | 2002-11-28 18:54:30 +0000 | [diff] [blame] | 14 | #include "../e_os.h" |
| 15 | |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 16 | /* some FIPS 140-1 random number test */ |
| 17 | /* some simple tests */ |
| 18 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 19 | int main(int argc, char **argv) |
| 20 | { |
| 21 | unsigned char buf[2500]; |
| 22 | int i, j, k, s, sign, nsign, err = 0; |
| 23 | unsigned long n1; |
| 24 | unsigned long n2[16]; |
| 25 | unsigned long runs[2][34]; |
| 26 | /* |
| 27 | * double d; |
| 28 | */ |
| 29 | long d; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 30 | |
Matt Caswell | 266483d | 2015-02-26 11:57:37 +0000 | [diff] [blame] | 31 | i = RAND_bytes(buf, 2500); |
| 32 | if (i <= 0) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 33 | printf("init failed, the rand method is not properly installed\n"); |
| 34 | err++; |
| 35 | goto err; |
| 36 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 37 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 38 | n1 = 0; |
| 39 | for (i = 0; i < 16; i++) |
| 40 | n2[i] = 0; |
| 41 | for (i = 0; i < 34; i++) |
| 42 | runs[0][i] = runs[1][i] = 0; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 43 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 44 | /* test 1 and 2 */ |
| 45 | sign = 0; |
| 46 | nsign = 0; |
| 47 | for (i = 0; i < 2500; i++) { |
| 48 | j = buf[i]; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 49 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 50 | n2[j & 0x0f]++; |
| 51 | n2[(j >> 4) & 0x0f]++; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 52 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 53 | for (k = 0; k < 8; k++) { |
| 54 | s = (j & 0x01); |
| 55 | if (s == sign) |
| 56 | nsign++; |
| 57 | else { |
| 58 | if (nsign > 34) |
| 59 | nsign = 34; |
| 60 | if (nsign != 0) { |
| 61 | runs[sign][nsign - 1]++; |
| 62 | if (nsign > 6) |
| 63 | runs[sign][5]++; |
| 64 | } |
| 65 | sign = s; |
| 66 | nsign = 1; |
| 67 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 68 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 69 | if (s) |
| 70 | n1++; |
| 71 | j >>= 1; |
| 72 | } |
| 73 | } |
| 74 | if (nsign > 34) |
| 75 | nsign = 34; |
| 76 | if (nsign != 0) |
| 77 | runs[sign][nsign - 1]++; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 78 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 79 | /* test 1 */ |
| 80 | if (!((9654 < n1) && (n1 < 10346))) { |
| 81 | printf("test 1 failed, X=%lu\n", n1); |
| 82 | err++; |
| 83 | } |
| 84 | printf("test 1 done\n"); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 85 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 86 | /* test 2 */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 87 | d = 0; |
| 88 | for (i = 0; i < 16; i++) |
| 89 | d += n2[i] * n2[i]; |
| 90 | d = (d * 8) / 25 - 500000; |
| 91 | if (!((103 < d) && (d < 5740))) { |
| 92 | printf("test 2 failed, X=%ld.%02ld\n", d / 100L, d % 100L); |
| 93 | err++; |
| 94 | } |
| 95 | printf("test 2 done\n"); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 96 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 97 | /* test 3 */ |
| 98 | for (i = 0; i < 2; i++) { |
| 99 | if (!((2267 < runs[i][0]) && (runs[i][0] < 2733))) { |
| 100 | printf("test 3 failed, bit=%d run=%d num=%lu\n", |
| 101 | i, 1, runs[i][0]); |
| 102 | err++; |
| 103 | } |
| 104 | if (!((1079 < runs[i][1]) && (runs[i][1] < 1421))) { |
| 105 | printf("test 3 failed, bit=%d run=%d num=%lu\n", |
| 106 | i, 2, runs[i][1]); |
| 107 | err++; |
| 108 | } |
| 109 | if (!((502 < runs[i][2]) && (runs[i][2] < 748))) { |
| 110 | printf("test 3 failed, bit=%d run=%d num=%lu\n", |
| 111 | i, 3, runs[i][2]); |
| 112 | err++; |
| 113 | } |
| 114 | if (!((223 < runs[i][3]) && (runs[i][3] < 402))) { |
| 115 | printf("test 3 failed, bit=%d run=%d num=%lu\n", |
| 116 | i, 4, runs[i][3]); |
| 117 | err++; |
| 118 | } |
| 119 | if (!((90 < runs[i][4]) && (runs[i][4] < 223))) { |
| 120 | printf("test 3 failed, bit=%d run=%d num=%lu\n", |
| 121 | i, 5, runs[i][4]); |
| 122 | err++; |
| 123 | } |
| 124 | if (!((90 < runs[i][5]) && (runs[i][5] < 223))) { |
| 125 | printf("test 3 failed, bit=%d run=%d num=%lu\n", |
| 126 | i, 6, runs[i][5]); |
| 127 | err++; |
| 128 | } |
| 129 | } |
| 130 | printf("test 3 done\n"); |
| 131 | |
| 132 | /* test 4 */ |
| 133 | if (runs[0][33] != 0) { |
| 134 | printf("test 4 failed, bit=%d run=%d num=%lu\n", 0, 34, runs[0][33]); |
| 135 | err++; |
| 136 | } |
| 137 | if (runs[1][33] != 0) { |
| 138 | printf("test 4 failed, bit=%d run=%d num=%lu\n", 1, 34, runs[1][33]); |
| 139 | err++; |
| 140 | } |
| 141 | printf("test 4 done\n"); |
Richard Levitte | 5ef6704 | 2000-10-10 15:08:30 +0000 | [diff] [blame] | 142 | err: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 143 | err = ((err) ? 1 : 0); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 144 | EXIT(err); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 145 | } |