blob: 73743be9294b1be2535ba9e5cf2979881c5c6a84 [file] [log] [blame]
Adam Langleyb387e222017-05-01 10:54:03 -07001/* Copyright (c) 2017, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
Adam Langley1ac76f72017-05-01 13:40:03 -070015// cavp_ctr_drbg_test processes a NIST CAVP DRBG800-90A test vector request
16// file and emits the corresponding response. An optional sample vector file
17// can be passed to verify the result.
Adam Langleyb387e222017-05-01 10:54:03 -070018
19#include <openssl/crypto.h>
20
21#include <stdlib.h>
22
23#include "cavp_test_util.h"
24#include "../crypto/fipsmodule/rand/internal.h"
25#include "../crypto/test/file_test.h"
26
27
28struct TestCtx {
29 std::unique_ptr<FileTest> response_sample;
30};
31
32static bool TestCTRDRBG(FileTest *t, void *arg) {
33 TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);
34
35 std::string test_type, prediction_resistance, entropy_input_len, nonce_len,
36 personalization_str_len, additional_input_len, returned_bits_len;
37 if (!t->GetInstruction(&test_type, "AES-256 no df") ||
38 !t->GetInstruction(&prediction_resistance, "PredictionResistance") ||
39 !t->GetInstruction(&entropy_input_len, "EntropyInputLen") ||
40 !t->GetInstruction(&nonce_len, "NonceLen") ||
41 !t->GetInstruction(&personalization_str_len,
42 "PersonalizationStringLen") ||
43 !t->GetInstruction(&additional_input_len, "AdditionalInputLen") ||
44 !t->GetInstruction(&returned_bits_len, "ReturnedBitsLen") ||
45 !test_type.empty() ||
46 prediction_resistance != "False" ||
47 strtoul(entropy_input_len.c_str(), nullptr, 0) !=
48 CTR_DRBG_ENTROPY_LEN * 8 ||
49 nonce_len != "0") {
50 return false;
51 }
52
53 std::string count;
54 std::vector<uint8_t> entropy, nonce, personalization_str, ai1, ai2;
55 if (!t->GetAttribute(&count, "COUNT") ||
56 !t->GetBytes(&entropy, "EntropyInput") ||
57 !t->GetBytes(&nonce, "Nonce") ||
58 !t->GetBytes(&personalization_str, "PersonalizationString") ||
59 !t->GetBytes(&ai1, "AdditionalInput") ||
60 !t->GetBytes(&ai2, "AdditionalInput/2") ||
61 entropy.size() * 8 != strtoul(entropy_input_len.c_str(), nullptr, 0) ||
62 nonce.size() != 0 ||
63 personalization_str.size() * 8 !=
64 strtoul(personalization_str_len.c_str(), nullptr, 0) ||
65 ai1.size() != ai2.size() ||
66 ai1.size() * 8 != strtoul(additional_input_len.c_str(), nullptr, 0)) {
67 return false;
68 }
69
70 CTR_DRBG_STATE drbg;
71 CTR_DRBG_init(&drbg, entropy.data(),
72 personalization_str.size() > 0 ? personalization_str.data()
73 : nullptr,
74 personalization_str.size());
75
76 uint64_t out_len = strtoul(returned_bits_len.c_str(), nullptr, 0);
77 if (out_len == 0 || (out_len & 7) != 0) {
78 return false;
79 }
80 out_len /= 8;
81
82 std::vector<uint8_t> out;
83 out.resize(out_len);
84
85 CTR_DRBG_generate(&drbg, out.data(), out.size(),
86 ai1.size() > 0 ? ai1.data() : nullptr, ai1.size());
87 CTR_DRBG_generate(&drbg, out.data(), out.size(),
88 ai2.size() > 0 ? ai2.data() : nullptr, ai2.size());
89
90 printf("%s", t->CurrentTestToString().c_str());
91 printf("ReturnedBits = %s\r\n\r\n",
92 EncodeHex(out.data(), out.size()).c_str());
93
94 // Check if sample response file matches.
95 if (ctx->response_sample) {
96 ctx->response_sample->ReadNext();
97 std::string expected_count;
98 std::vector<uint8_t> expected_bits;
99 if (!ctx->response_sample->GetAttribute(&expected_count, "COUNT") ||
100 count != expected_count ||
101 !ctx->response_sample->GetBytes(&expected_bits, "ReturnedBits") ||
102 !t->ExpectBytesEqual(expected_bits.data(), expected_bits.size(),
103 out.data(), out.size())) {
104 t->PrintLine("result doesn't match");
105 return false;
106 }
107 }
108
109 return true;
110}
111
112static int usage(char *arg) {
113 fprintf(stderr, "usage: %s <test file> [<sample response file>]\n", arg);
114 return 1;
115}
116
117int main(int argc, char **argv) {
118 CRYPTO_library_init();
119
120 if (argc != 2 && argc != 3) {
121 return usage(argv[0]);
122 }
123
124 TestCtx ctx = {nullptr};
125
126 if (argc == 3) {
127 ctx.response_sample.reset(new FileTest(argv[2]));
128 if (!ctx.response_sample->is_open()) {
129 return 1;
130 }
131 ctx.response_sample->SetIgnoreUnusedAttributes(true);
132 }
133
134 printf("# Generated by");
135 for (int i = 0; i < argc; i++) {
136 printf(" %s", argv[i]);
137 }
138 printf("\r\n\r\n");
139
140 return FileTestMainSilent(TestCTRDRBG, &ctx, argv[1]);
141}