forked from google/boringssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcavp_rsa2_sigver_test.cc
125 lines (104 loc) · 3.35 KB
/
cavp_rsa2_sigver_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* Copyright (c) 2017, Google Inc.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
// cavp_rsa2_sigver_test processes NIST CAVP RSA2 SigVer test vector request
// files and emits the corresponding response.
#include <vector>
#include <openssl/bn.h>
#include <openssl/crypto.h>
#include <openssl/digest.h>
#include <openssl/err.h>
#include <openssl/rsa.h>
#include "../crypto/internal.h"
#include "../crypto/test/file_test.h"
#include "cavp_test_util.h"
namespace {
struct TestCtx {
std::vector<uint8_t> N;
bool is_pss;
};
}
static bool TestRSA2SigVer(FileTest *t, void *arg) {
TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);
std::string mod_str;
if (!t->GetInstruction(&mod_str, "mod")) {
return false;
}
printf("%s", t->CurrentTestToString().c_str());
if (t->HasAttribute("n")) {
printf("\r\n");
return t->GetBytes(&ctx->N, "n");
}
std::string hash;
std::vector<uint8_t> e_bytes, msg, sig;
if (!t->GetAttribute(&hash, "SHAAlg") ||
!t->GetBytes(&e_bytes, "e") ||
!t->GetBytes(&msg, "Msg") ||
!t->GetBytes(&sig, "S")) {
return false;
}
bssl::UniquePtr<RSA> key(RSA_new());
key->n = BN_new();
key->e = BN_new();
if (key == nullptr ||
!BN_bin2bn(ctx->N.data(), ctx->N.size(), key->n) ||
!BN_bin2bn(e_bytes.data(), e_bytes.size(), key->e)) {
return false;
}
const EVP_MD *md = EVP_get_digestbyname(hash.c_str());
uint8_t digest_buf[EVP_MAX_MD_SIZE];
unsigned digest_len;
if (md == NULL ||
!EVP_Digest(msg.data(), msg.size(), digest_buf, &digest_len, md, NULL)) {
return false;
}
int ok;
if (ctx->is_pss) {
ok = RSA_verify_pss_mgf1(key.get(), digest_buf, digest_len, md, md, -1,
sig.data(), sig.size());
} else {
ok = RSA_verify(EVP_MD_type(md), digest_buf, digest_len, sig.data(),
sig.size(), key.get());
}
if (ok) {
printf("Result = P\r\n\r\n");
} else {
char buf[256];
ERR_error_string_n(ERR_get_error(), buf, sizeof(buf));
printf("Result = F (%s)\r\n\r\n", buf);
}
ERR_clear_error();
return true;
}
int cavp_rsa2_sigver_test_main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "usage: %s (pkcs15|pss) <test file>\n",
argv[0]);
return 1;
}
TestCtx ctx;
if (strcmp(argv[1], "pkcs15") == 0) {
ctx = {std::vector<uint8_t>(), false};
} else if (strcmp(argv[1], "pss") == 0) {
ctx = {std::vector<uint8_t>(), true};
} else {
fprintf(stderr, "Unknown test type: %s\n", argv[1]);
return 1;
}
printf("# Generated by");
for (int i = 0; i < argc; i++) {
printf(" %s", argv[i]);
}
printf("\r\n\r\n");
return FileTestMainSilent(TestRSA2SigVer, &ctx, argv[2]);
}