summaryrefslogtreecommitdiff
path: root/src/util/fipstools/cavp/cavp_rsa2_siggen_test.cc
blob: 063b89d863eea2ee941167924315a8adbf9144d2 (plain)
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
126
127
128
129
/* 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_siggen_test processes NIST CAVP RSA2 SigGen 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/rsa.h>

#include "../crypto/internal.h"
#include "../crypto/test/file_test.h"
#include "cavp_test_util.h"

namespace {

struct TestCtx {
  bssl::UniquePtr<RSA> key;
  bool is_pss;
};

}

static bool TestRSA2SigGen(FileTest *t, void *arg) {
  TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);

  std::string mod_str, hash;
  std::vector<uint8_t> msg;
  if (!t->GetInstruction(&mod_str, "mod") ||
      !t->GetAttribute(&hash, "SHAAlg") ||
      !t->GetBytes(&msg, "Msg")) {
    return false;
  }

  std::string test = t->CurrentTestToString();
  if (t->IsAtNewInstructionBlock()) {
    int mod_bits = strtoul(mod_str.c_str(), nullptr, 0);
    ctx->key = bssl::UniquePtr<RSA>(RSA_new());
    if (ctx->key == nullptr ||
        mod_bits == 0 ||
        !RSA_generate_key_fips(ctx->key.get(), mod_bits, nullptr)) {
      return false;
    }

    const BIGNUM *n, *e;
    RSA_get0_key(ctx->key.get(), &n, &e, nullptr);

    std::vector<uint8_t> n_bytes(BN_num_bytes(n));
    std::vector<uint8_t> e_bytes(BN_num_bytes(e));
    if (!BN_bn2bin_padded(n_bytes.data(), n_bytes.size(), n) ||
        !BN_bn2bin_padded(e_bytes.data(), e_bytes.size(), e)) {
      return false;
    }

    printf("[mod = %s]\r\n\r\nn = %s\r\n\r\ne = %s",
           mod_str.c_str(),
           EncodeHex(n_bytes.data(), n_bytes.size()).c_str(),
           EncodeHex(e_bytes.data(), e_bytes.size()).c_str());
    test = test.substr(test.find("]") + 3);
  }

  const EVP_MD *md = EVP_get_digestbyname(hash.c_str());
  uint8_t digest_buf[EVP_MAX_MD_SIZE];
  std::vector<uint8_t> sig(RSA_size(ctx->key.get()));
  unsigned digest_len;
  size_t sig_len;
  if (md == NULL ||
      !EVP_Digest(msg.data(), msg.size(), digest_buf, &digest_len, md, NULL)) {
    return false;
  }

  if (ctx->is_pss) {
    if (!RSA_sign_pss_mgf1(ctx->key.get(), &sig_len, sig.data(), sig.size(),
                           digest_buf, digest_len, md, md, -1)) {
      return false;
    }
  } else {
    unsigned sig_len_u;
    if (!RSA_sign(EVP_MD_type(md), digest_buf, digest_len, sig.data(),
                  &sig_len_u, ctx->key.get())) {
      return false;
    }
    sig_len = sig_len_u;
  }

  printf("%sS = %s\r\n\r\n", test.c_str(),
         EncodeHex(sig.data(), sig_len).c_str());
  return true;
}

int cavp_rsa2_siggen_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 = {nullptr, false};
  } else if (strcmp(argv[1], "pss") == 0) {
    ctx = {nullptr, true};
  } else {
    fprintf(stderr, "Unknown test type: %s\n", argv[1]);
    return 1;
  }

  FileTest::Options opts;
  opts.path = argv[2];
  opts.callback = TestRSA2SigGen;
  opts.arg = &ctx;
  opts.silent = true;
  opts.comment_callback = EchoComment;
  return FileTestMain(opts);
}