~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to src/tests/test_ecdsa.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* (C) 2014,2015 Jack Lloyd
 
3
* (C) 2017 René Korthaus, Rohde & Schwarz Cybersecurity
 
4
*
 
5
* Botan is released under the Simplified BSD License (see license.txt)
 
6
*/
 
7
 
 
8
#include "tests.h"
 
9
 
 
10
#include "test_rng.h"
 
11
 
 
12
#if defined(BOTAN_HAS_ECDSA)
 
13
   #include "test_pubkey.h"
 
14
   #include <botan/ecdsa.h>
 
15
   #include <botan/oids.h>
 
16
#endif
 
17
 
 
18
namespace Botan_Tests {
 
19
 
 
20
namespace {
 
21
 
 
22
#if defined(BOTAN_HAS_ECDSA)
 
23
 
 
24
class ECDSA_Signature_KAT_Tests final : public PK_Signature_Generation_Test
 
25
   {
 
26
   public:
 
27
      ECDSA_Signature_KAT_Tests() : PK_Signature_Generation_Test(
 
28
            "ECDSA",
 
29
#if defined(BOTAN_HAS_RFC6979_GENERATOR)
 
30
            "pubkey/ecdsa_rfc6979.vec",
 
31
            "Group,X,Hash,Msg,Signature") {}
 
32
#else
 
33
            "pubkey/ecdsa_prob.vec",
 
34
            "Group,X,Hash,Msg,Nonce,Signature") {}
 
35
#endif
 
36
 
 
37
      bool clear_between_callbacks() const override
 
38
         {
 
39
         return false;
 
40
         }
 
41
 
 
42
      std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override
 
43
         {
 
44
         const std::string group_id = get_req_str(vars, "Group");
 
45
         const BigInt x = get_req_bn(vars, "X");
 
46
         Botan::EC_Group group(Botan::OIDS::lookup(group_id));
 
47
 
 
48
         std::unique_ptr<Botan::Private_Key> key(new Botan::ECDSA_PrivateKey(Test::rng(), group, x));
 
49
         return key;
 
50
         }
 
51
 
 
52
      std::string default_padding(const VarMap& vars) const override
 
53
         {
 
54
         const std::string hash = get_req_str(vars, "Hash");
 
55
         if(hash.substr(0,3) == "Raw")
 
56
            return hash;
 
57
         return "EMSA1(" + hash + ")";
 
58
         }
 
59
 
 
60
#if !defined(BOTAN_HAS_RFC6979)
 
61
      Botan::RandomNumberGenerator* test_rng(const std::vector<uint8_t>& nonce) const override
 
62
         {
 
63
         // probabilistic ecdsa signature generation extracts more random than just the nonce,
 
64
         // but the nonce is extracted first
 
65
         return new Fixed_Output_Position_RNG(nonce, 1);
 
66
         }
 
67
#endif
 
68
   };
 
69
 
 
70
class ECDSA_Keygen_Tests final : public PK_Key_Generation_Test
 
71
   {
 
72
   public:
 
73
      std::vector<std::string> keygen_params() const override
 
74
         {
 
75
         return { "secp256r1", "secp384r1", "secp521r1", "frp256v1" };
 
76
         }
 
77
      std::string algo_name() const override
 
78
         {
 
79
         return "ECDSA";
 
80
         }
 
81
   };
 
82
 
 
83
class ECDSA_Invalid_Key_Tests final : public Text_Based_Test
 
84
   {
 
85
   public:
 
86
      ECDSA_Invalid_Key_Tests() :
 
87
         Text_Based_Test("pubkey/ecdsa_invalid.vec", "Group,InvalidKeyX,InvalidKeyY") {}
 
88
 
 
89
      bool clear_between_callbacks() const override
 
90
         {
 
91
         return false;
 
92
         }
 
93
 
 
94
      Test::Result run_one_test(const std::string&, const VarMap& vars) override
 
95
         {
 
96
         Test::Result result("ECDSA invalid keys");
 
97
 
 
98
         const std::string group_id = get_req_str(vars, "Group");
 
99
         Botan::EC_Group group(Botan::OIDS::lookup(group_id));
 
100
         const Botan::BigInt x = get_req_bn(vars, "InvalidKeyX");
 
101
         const Botan::BigInt y = get_req_bn(vars, "InvalidKeyY");
 
102
 
 
103
         std::unique_ptr<Botan::PointGFp> public_point;
 
104
 
 
105
         try
 
106
            {
 
107
            public_point.reset(new Botan::PointGFp(group.get_curve(), x, y));
 
108
            }
 
109
         catch(Botan::Invalid_Argument&)
 
110
            {
 
111
            // PointGFp() performs a range check on x, y in [0, p−1],
 
112
            // which is also part of the EC public key checks, e.g.,
 
113
            // in NIST SP800-56A rev2, sec. 5.6.2.3.2
 
114
            result.test_success("public key fails check");
 
115
            return result;
 
116
            }
 
117
 
 
118
         std::unique_ptr<Botan::Public_Key> key(new Botan::ECDSA_PublicKey(group, *public_point));
 
119
         result.test_eq("public key fails check", key->check_key(Test::rng(), false), false);
 
120
         return result;
 
121
         }
 
122
   };
 
123
 
 
124
BOTAN_REGISTER_TEST("ecdsa_sign", ECDSA_Signature_KAT_Tests);
 
125
BOTAN_REGISTER_TEST("ecdsa_keygen", ECDSA_Keygen_Tests);
 
126
BOTAN_REGISTER_TEST("ecdsa_invalid", ECDSA_Invalid_Key_Tests);
 
127
 
 
128
#endif
 
129
 
 
130
}
 
131
 
 
132
}