cavp_ctr_drbg_test.cc
Go to the documentation of this file.
1 /* 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 
15 // cavp_ctr_drbg_test processes a NIST CAVP DRBG800-90A test vector request
16 // file and emits the corresponding response.
17 
18 #include <openssl/crypto.h>
19 
20 #include <stdlib.h>
21 
22 #include "cavp_test_util.h"
23 #include "../crypto/fipsmodule/rand/internal.h"
24 #include "../crypto/test/file_test.h"
25 #include "../crypto/test/test_util.h"
26 
27 
28 static bool TestCTRDRBG(FileTest *t, void *arg) {
29  std::string test_type, prediction_resistance, entropy_input_len, nonce_len,
30  personalization_str_len, additional_input_len, returned_bits_len;
31  if (!t->GetInstruction(&test_type, "AES-256 no df") ||
32  !t->GetInstruction(&prediction_resistance, "PredictionResistance") ||
33  !t->GetInstruction(&entropy_input_len, "EntropyInputLen") ||
34  !t->GetInstruction(&nonce_len, "NonceLen") ||
35  !t->GetInstruction(&personalization_str_len,
36  "PersonalizationStringLen") ||
37  !t->GetInstruction(&additional_input_len, "AdditionalInputLen") ||
38  !t->GetInstruction(&returned_bits_len, "ReturnedBitsLen") ||
39  !test_type.empty() ||
40  prediction_resistance != "False" ||
41  strtoul(entropy_input_len.c_str(), nullptr, 0) !=
43  nonce_len != "0") {
44  return false;
45  }
46 
48  std::vector<uint8_t> entropy, nonce, personalization_str, ai1, ai2;
49  if (!t->GetAttribute(&count, "COUNT") ||
50  !t->GetBytes(&entropy, "EntropyInput") ||
51  !t->GetBytes(&nonce, "Nonce") ||
52  !t->GetBytes(&personalization_str, "PersonalizationString") ||
53  !t->GetBytes(&ai1, "AdditionalInput") ||
54  !t->GetBytes(&ai2, "AdditionalInput/2") ||
55  entropy.size() * 8 != strtoul(entropy_input_len.c_str(), nullptr, 0) ||
56  nonce.size() != 0 ||
57  personalization_str.size() * 8 !=
58  strtoul(personalization_str_len.c_str(), nullptr, 0) ||
59  ai1.size() != ai2.size() ||
60  ai1.size() * 8 != strtoul(additional_input_len.c_str(), nullptr, 0)) {
61  return false;
62  }
63 
64  CTR_DRBG_STATE drbg;
65  CTR_DRBG_init(&drbg, entropy.data(),
66  personalization_str.size() > 0 ? personalization_str.data()
67  : nullptr,
68  personalization_str.size());
69 
70  uint64_t out_len = strtoul(returned_bits_len.c_str(), nullptr, 0);
71  if (out_len == 0 || (out_len & 7) != 0) {
72  return false;
73  }
74  out_len /= 8;
75 
76  std::vector<uint8_t> out;
77  out.resize(out_len);
78 
79  CTR_DRBG_generate(&drbg, out.data(), out.size(),
80  ai1.size() > 0 ? ai1.data() : nullptr, ai1.size());
81  CTR_DRBG_generate(&drbg, out.data(), out.size(),
82  ai2.size() > 0 ? ai2.data() : nullptr, ai2.size());
83 
84  printf("%s", t->CurrentTestToString().c_str());
85  printf("ReturnedBits = %s\r\n\r\n", EncodeHex(out).c_str());
86 
87  return true;
88 }
89 
90 static int usage(char *arg) {
91  fprintf(stderr, "usage: %s <test file>\n", arg);
92  return 1;
93 }
94 
95 int cavp_ctr_drbg_test_main(int argc, char **argv) {
96  if (argc != 2) {
97  return usage(argv[0]);
98  }
99 
101  opts.path = argv[1];
102  opts.callback = TestCTRDRBG;
103  opts.silent = true;
104  opts.comment_callback = EchoComment;
105  return FileTestMain(opts);
106 }
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
FileTestMain
int FileTestMain(FileTestFunc run_test, void *arg, const char *path)
Definition: file_test.cc:399
CTR_DRBG_init
#define CTR_DRBG_init
Definition: boringssl_prefix_symbols.h:1206
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
cstest_report.opts
opts
Definition: cstest_report.py:81
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
cavp_ctr_drbg_test_main
int cavp_ctr_drbg_test_main(int argc, char **argv)
Definition: cavp_ctr_drbg_test.cc:95
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
TestCTRDRBG
static bool TestCTRDRBG(FileTest *t, void *arg)
Definition: cavp_ctr_drbg_test.cc:28
cavp_test_util.h
EncodeHex
std::string EncodeHex(bssl::Span< const uint8_t > in)
Definition: boringssl-with-bazel/src/crypto/test/test_util.cc:75
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
FileTest
Definition: file_test.h:90
crypto.h
arg
Definition: cmdline.cc:40
EchoComment
void EchoComment(const std::string &comment)
Definition: cavp_test_util.cc:218
usage
static int usage(char *arg)
Definition: cavp_ctr_drbg_test.cc:90
CTR_DRBG_ENTROPY_LEN
#define CTR_DRBG_ENTROPY_LEN
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/internal.h:111
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
FileTest::Options
Definition: file_test.h:104
CTR_DRBG_STATE
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/internal.h:99
CTR_DRBG_generate
#define CTR_DRBG_generate
Definition: boringssl_prefix_symbols.h:1205


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:52