1 // Copyright 2015 The BoringSSL Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <string>
16 #include <vector>
17
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #include <openssl/rand.h>
23
24 #include "internal.h"
25
26
27 static const struct argument kArguments[] = {
28 {
29 "-hex", kBooleanArgument,
30 "Hex encoded output."
31 },
32 {
33 "", kOptionalArgument, "",
34 },
35 };
36
Rand(const std::vector<std::string> & args)37 bool Rand(const std::vector<std::string> &args) {
38 bool forever = true, hex = false;
39 size_t len = 0;
40
41 if (!args.empty()) {
42 std::vector<std::string> args_copy(args);
43 const std::string &last_arg = args.back();
44
45 if (!last_arg.empty() && last_arg[0] != '-') {
46 char *endptr;
47 unsigned long long num = strtoull(last_arg.c_str(), &endptr, 10);
48 if (*endptr == 0) {
49 len = num;
50 forever = false;
51 args_copy.pop_back();
52 }
53 }
54
55 std::map<std::string, std::string> args_map;
56 if (!ParseKeyValueArguments(&args_map, args_copy, kArguments)) {
57 PrintUsage(kArguments);
58 return false;
59 }
60
61 hex = args_map.count("-hex") > 0;
62 }
63
64 uint8_t buf[4096];
65 uint8_t hex_buf[8192];
66
67 size_t done = 0;
68 while (forever || done < len) {
69 size_t todo = sizeof(buf);
70 if (!forever && todo > len - done) {
71 todo = len - done;
72 }
73 RAND_bytes(buf, todo);
74 if (hex) {
75 static const char hextable[16 + 1] = "0123456789abcdef";
76 for (unsigned i = 0; i < todo; i++) {
77 hex_buf[i*2] = hextable[buf[i] >> 4];
78 hex_buf[i*2 + 1] = hextable[buf[i] & 0xf];
79 }
80 if (fwrite(hex_buf, todo*2, 1, stdout) != 1) {
81 return false;
82 }
83 } else {
84 if (fwrite(buf, todo, 1, stdout) != 1) {
85 return false;
86 }
87 }
88 done += todo;
89 }
90
91 if (hex && fwrite("\n", 1, 1, stdout) != 1) {
92 return false;
93 }
94
95 return true;
96 }
97