cavp_sha_monte_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_sha_monte_test processes a NIST CAVP SHA-Monte test vector request file
16 // and emits the corresponding response.
17 
18 #include <stdlib.h>
19 
20 #include <openssl/crypto.h>
21 #include <openssl/digest.h>
22 
23 #include "../crypto/test/file_test.h"
24 #include "../crypto/test/test_util.h"
25 #include "cavp_test_util.h"
26 
27 
28 namespace {
29 
30 struct TestCtx {
32 };
33 
34 }
35 
36 static bool TestSHAMonte(FileTest *t, void *arg) {
37  TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);
38 
39  const EVP_MD *md = EVP_get_digestbyname(ctx->hash.c_str());
40  if (md == nullptr) {
41  return false;
42  }
43  const size_t md_len = EVP_MD_size(md);
44 
45  std::string out_len;
46  if (!t->GetInstruction(&out_len, "L") ||
47  md_len != strtoul(out_len.c_str(), nullptr, 0)) {
48  return false;
49  }
50 
51  std::vector<uint8_t> seed;
52  if (!t->GetBytes(&seed, "Seed") ||
53  seed.size() != md_len) {
54  return false;
55  }
56 
57  std::vector<uint8_t> out = seed;
58 
59  printf("%s\r\n", t->CurrentTestToString().c_str());
60 
61  for (int count = 0; count < 100; count++) {
62  std::vector<uint8_t> msg;
63  msg.insert(msg.end(), out.begin(), out.end());
64  msg.insert(msg.end(), out.begin(), out.end());
65  msg.insert(msg.end(), out.begin(), out.end());
66  for (int i = 0; i < 1000; i++) {
67  unsigned digest_len;
68  if (!EVP_Digest(msg.data(), msg.size(), out.data(), &digest_len, md,
69  nullptr) ||
70  digest_len != out.size()) {
71  return false;
72  }
73 
74  msg.erase(msg.begin(), msg.begin() + out.size());
75  msg.insert(msg.end(), out.begin(), out.end());
76  }
77  printf("COUNT = %d\r\n", count);
78  printf("MD = %s\r\n\r\n", EncodeHex(out).c_str());
79  }
80 
81  return true;
82 }
83 
84 static int usage(char *arg) {
85  fprintf(stderr, "usage: %s <hash> <test file>\n", arg);
86  return 1;
87 }
88 
89 int cavp_sha_monte_test_main(int argc, char **argv) {
90  if (argc != 3) {
91  return usage(argv[0]);
92  }
93 
94  TestCtx ctx = {std::string(argv[1])};
95 
97  opts.path = argv[2];
98  opts.callback = TestSHAMonte;
99  opts.arg = &ctx;
100  opts.silent = true;
101  opts.comment_callback = EchoComment;
102  return FileTestMain(opts);
103 }
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
usage
static int usage(char *arg)
Definition: cavp_sha_monte_test.cc:84
ctx
Definition: benchmark-async.c:30
EVP_get_digestbyname
#define EVP_get_digestbyname
Definition: boringssl_prefix_symbols.h:1726
FileTestMain
int FileTestMain(FileTestFunc run_test, void *arg, const char *path)
Definition: file_test.cc:399
env_md_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/internal.h:67
seed
static const uint8_t seed[20]
Definition: dsa_test.cc:79
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
ctx
static struct test_ctx ctx
Definition: test-ipc-send-recv.c:65
TestSHAMonte
static bool TestSHAMonte(FileTest *t, void *arg)
Definition: cavp_sha_monte_test.cc:36
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
hash
uint64_t hash
Definition: ring_hash.cc:284
cavp_sha_monte_test_main
int cavp_sha_monte_test_main(int argc, char **argv)
Definition: cavp_sha_monte_test.cc:89
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
EVP_MD_size
#define EVP_MD_size
Definition: boringssl_prefix_symbols.h:1579
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
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
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
benchmark.md
md
Definition: benchmark.py:86
digest.h
EVP_Digest
#define EVP_Digest
Definition: boringssl_prefix_symbols.h:1506
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
arg
struct arg arg
FileTest::Options
Definition: file_test.h:104
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


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