blob: d6f2fb09566fcb470c04f819ef8df35ed6b34f9a [file] [log] [blame]
Robert Sloan2424d842017-05-01 07:46:28 -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
15// cavp_sha_monte_test processes a NIST CAVP SHA-Monte test vector request file
Robert Sloan8ff03552017-06-14 12:40:58 -070016// and emits the corresponding response.
Robert Sloan2424d842017-05-01 07:46:28 -070017
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 "cavp_test_util.h"
25
26
Robert Sloan8ff03552017-06-14 12:40:58 -070027namespace {
28
Robert Sloan2424d842017-05-01 07:46:28 -070029struct TestCtx {
30 std::string hash;
31};
32
Robert Sloan8ff03552017-06-14 12:40:58 -070033}
34
Robert Sloan2424d842017-05-01 07:46:28 -070035static bool TestSHAMonte(FileTest *t, void *arg) {
36 TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);
37
38 const EVP_MD *md = EVP_get_digestbyname(ctx->hash.c_str());
39 if (md == nullptr) {
40 return false;
41 }
42 const size_t md_len = EVP_MD_size(md);
43
44 std::string out_len;
45 if (!t->GetInstruction(&out_len, "L") ||
46 md_len != strtoul(out_len.c_str(), nullptr, 0)) {
47 return false;
48 }
49
50 std::vector<uint8_t> seed;
51 if (!t->GetBytes(&seed, "Seed") ||
52 seed.size() != md_len) {
53 return false;
54 }
55
56 std::vector<uint8_t> out = seed;
57
58 printf("%s\r\n", t->CurrentTestToString().c_str());
59
60 for (int count = 0; count < 100; count++) {
61 std::vector<uint8_t> msg;
62 msg.insert(msg.end(), out.begin(), out.end());
63 msg.insert(msg.end(), out.begin(), out.end());
64 msg.insert(msg.end(), out.begin(), out.end());
65 for (int i = 0; i < 1000; i++) {
66 unsigned digest_len;
67 if (!EVP_Digest(msg.data(), msg.size(), out.data(), &digest_len, md,
68 nullptr) ||
69 digest_len != out.size()) {
70 return false;
71 }
72
73 msg.erase(msg.begin(), msg.begin() + out.size());
74 msg.insert(msg.end(), out.begin(), out.end());
75 }
76 printf("COUNT = %d\r\n", count);
77 printf("MD = %s\r\n\r\n", EncodeHex(out.data(), out.size()).c_str());
78 }
79
80 return true;
81}
82
83static int usage(char *arg) {
84 fprintf(stderr, "usage: %s <hash> <test file>\n", arg);
85 return 1;
86}
87
Robert Sloan8ff03552017-06-14 12:40:58 -070088int cavp_sha_monte_test_main(int argc, char **argv) {
Robert Sloan2424d842017-05-01 07:46:28 -070089 if (argc != 3) {
90 return usage(argv[0]);
91 }
92
93 TestCtx ctx = {std::string(argv[1])};
94
Robert Sloan8ff03552017-06-14 12:40:58 -070095 FileTest::Options opts;
96 opts.path = argv[2];
97 opts.callback = TestSHAMonte;
98 opts.arg = &ctx;
99 opts.silent = true;
100 opts.comment_callback = EchoComment;
101 return FileTestMain(opts);
Robert Sloan2424d842017-05-01 07:46:28 -0700102}