aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_rsa.cpp
blob: f1ec8e6038246c5f8618404815cf3444ae65fa20 (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
#include "tests.h"
#include "test_pubkey.h"


#include <botan/pubkey.h>
#include <botan/rsa.h>
#include <botan/hex.h>
#include <iostream>
#include <fstream>

using namespace Botan;

namespace {

size_t rsaes_kat(const std::string& e,
                 const std::string& p,
                 const std::string& q,
                 const std::string& msg,
                 std::string padding,
                 const std::string& nonce,
                 const std::string& output)
   {
   auto& rng = test_rng();

   RSA_PrivateKey privkey(rng, BigInt(p), BigInt(q), BigInt(e));

   RSA_PublicKey pubkey = privkey;

   if(padding == "")
      padding = "Raw";

   PK_Encryptor_EME enc(pubkey, padding);
   PK_Decryptor_EME dec(privkey, padding);

   return validate_encryption(enc, dec, "RSAES/" + padding, msg, nonce, output);
   }

size_t rsa_sig_kat(const std::string& e,
                 const std::string& p,
                 const std::string& q,
                 const std::string& msg,
                 std::string padding,
                 const std::string& nonce,
                 const std::string& output)
   {
   auto& rng = test_rng();

   RSA_PrivateKey privkey(rng, BigInt(p), BigInt(q), BigInt(e));

   RSA_PublicKey pubkey = privkey;

   if(padding == "")
      padding = "Raw";

   PK_Verifier verify(pubkey, padding);
   PK_Signer sign(privkey, padding);

   return validate_signature(verify, sign, "RSA/" + padding, msg, rng, nonce, output);
   }

size_t rsa_sig_verify(const std::string& e,
                      const std::string& n,
                      const std::string& msg,
                      std::string padding,
                      const std::string& signature)
   {
   BigInt e_bn(e);
   BigInt n_bn(n);

   RSA_PublicKey key(n_bn, e_bn);

   if(padding == "")
      padding = "Raw";

   PK_Verifier verify(key, padding);

   if(!verify.verify_message(hex_decode(msg), hex_decode(signature)))
      return 1;
   return 0;
   }

}

size_t test_rsa()
   {
   std::ifstream rsa_enc(PK_TEST_DATA_DIR "/rsaes.vec");
   std::ifstream rsa_sig(PK_TEST_DATA_DIR "/rsa_sig.vec");
   std::ifstream rsa_verify(PK_TEST_DATA_DIR "/rsa_verify.vec");

   size_t fails = 0;

   fails += run_tests_bb(rsa_enc, "RSA Encryption", "Ciphertext", true,
             [](std::map<std::string, std::string> m) -> size_t
             {
             return rsaes_kat(m["E"], m["P"], m["Q"], m["Msg"],
                              m["Padding"], m["Nonce"], m["Ciphertext"]);
             });

   fails += run_tests_bb(rsa_sig, "RSA Signature", "Signature", true,
             [](std::map<std::string, std::string> m) -> size_t
             {
             return rsa_sig_kat(m["E"], m["P"], m["Q"], m["Msg"],
                                m["Padding"], m["Nonce"], m["Signature"]);
             });

   fails += run_tests_bb(rsa_verify, "RSA Verify", "Signature", true,
             [](std::map<std::string, std::string> m) -> size_t
             {
             return rsa_sig_verify(m["E"], m["N"], m["Msg"],
                                   m["Padding"], m["Signature"]);
             });

   return fails;
   }