• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
TestCTRDRBG(FileTest * t,void * arg)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) !=
42           CTR_DRBG_ENTROPY_LEN * 8 ||
43       nonce_len != "0") {
44     return false;
45   }
46 
47   std::string count;
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 
usage(char * arg)90 static int usage(char *arg) {
91   fprintf(stderr, "usage: %s <test file>\n", arg);
92   return 1;
93 }
94 
cavp_ctr_drbg_test_main(int argc,char ** argv)95 int cavp_ctr_drbg_test_main(int argc, char **argv) {
96   if (argc != 2) {
97     return usage(argv[0]);
98   }
99 
100   FileTest::Options opts;
101   opts.path = argv[1];
102   opts.callback = TestCTRDRBG;
103   opts.silent = true;
104   opts.comment_callback = EchoComment;
105   return FileTestMain(opts);
106 }
107