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

« back to all changes in this revision

Viewing changes to src/tests/test_sm2.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) 2017 Ribose Inc
 
3
*
 
4
* Botan is released under the Simplified BSD License (see license.txt)
 
5
*/
 
6
 
 
7
#include "tests.h"
 
8
#include "test_rng.h"
 
9
 
 
10
#if defined(BOTAN_HAS_SM2)
 
11
   #include <botan/sm2.h>
 
12
   #include <botan/sm2_enc.h>
 
13
   #include "test_pubkey.h"
 
14
#endif
 
15
 
 
16
namespace Botan_Tests {
 
17
 
 
18
#if defined(BOTAN_HAS_SM2)
 
19
 
 
20
namespace {
 
21
 
 
22
class SM2_Signature_KAT_Tests final : public PK_Signature_Generation_Test
 
23
   {
 
24
   public:
 
25
      SM2_Signature_KAT_Tests()
 
26
         : PK_Signature_Generation_Test(
 
27
            "SM2",
 
28
            "pubkey/sm2_sig.vec",
 
29
            "P,A,B,xG,yG,Order,Cofactor,Ident,Msg,x,Nonce,Signature",
 
30
            "Hash") {}
 
31
 
 
32
      std::string default_padding(const VarMap& vars) const override
 
33
         {
 
34
         return get_req_str(vars, "Ident") + "," + get_opt_str(vars, "Hash", "SM3");
 
35
         }
 
36
 
 
37
      Botan::RandomNumberGenerator* test_rng(const std::vector<uint8_t>& nonce) const override
 
38
         {
 
39
         return new Fixed_Output_Position_RNG(nonce, 1);
 
40
         }
 
41
 
 
42
      std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override
 
43
         {
 
44
         // group params
 
45
         const BigInt p = get_req_bn(vars, "P");
 
46
         const BigInt a = get_req_bn(vars, "A");
 
47
         const BigInt b = get_req_bn(vars, "B");
 
48
         const BigInt xG = get_req_bn(vars, "xG");
 
49
         const BigInt yG = get_req_bn(vars, "yG");
 
50
         const BigInt order = get_req_bn(vars, "Order");
 
51
         const BigInt cofactor = get_req_bn(vars, "Cofactor");
 
52
         const BigInt x = get_req_bn(vars, "x");
 
53
 
 
54
         Botan::CurveGFp curve(p, a, b);
 
55
         Botan::PointGFp base_point(curve, xG, yG);
 
56
         Botan::EC_Group domain(curve, base_point, order, cofactor);
 
57
 
 
58
         Botan::Null_RNG null_rng;
 
59
         std::unique_ptr<Botan::Private_Key> key(new Botan::SM2_Signature_PrivateKey(null_rng, domain, x));
 
60
         return key;
 
61
         }
 
62
   };
 
63
 
 
64
BOTAN_REGISTER_TEST("sm2_sig", SM2_Signature_KAT_Tests);
 
65
 
 
66
class SM2_Encryption_KAT_Tests final : public PK_Encryption_Decryption_Test
 
67
   {
 
68
   public:
 
69
      SM2_Encryption_KAT_Tests()
 
70
         : PK_Encryption_Decryption_Test(
 
71
            "SM2",
 
72
            "pubkey/sm2_enc.vec",
 
73
            "P,A,B,xG,yG,Order,Cofactor,Msg,x,Nonce,Ciphertext",
 
74
            "Hash") {}
 
75
 
 
76
      std::string default_padding(const VarMap& vars) const override
 
77
         {
 
78
         return get_opt_str(vars, "Hash", "SM3");
 
79
         }
 
80
 
 
81
      bool clear_between_callbacks() const override { return false; }
 
82
 
 
83
      Botan::RandomNumberGenerator* test_rng(const std::vector<uint8_t>& nonce) const override
 
84
         {
 
85
         return new Fixed_Output_Position_RNG(nonce, 1);
 
86
         }
 
87
 
 
88
      std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override
 
89
         {
 
90
         // group params
 
91
         const BigInt p = get_req_bn(vars, "P");
 
92
         const BigInt a = get_req_bn(vars, "A");
 
93
         const BigInt b = get_req_bn(vars, "B");
 
94
         const BigInt xG = get_req_bn(vars, "xG");
 
95
         const BigInt yG = get_req_bn(vars, "yG");
 
96
         const BigInt order = get_req_bn(vars, "Order");
 
97
         const BigInt cofactor = get_req_bn(vars, "Cofactor");
 
98
         const BigInt x = get_req_bn(vars, "x");
 
99
 
 
100
         Botan::CurveGFp curve(p, a, b);
 
101
         Botan::PointGFp base_point(curve, xG, yG);
 
102
         Botan::EC_Group domain(curve, base_point, order, cofactor);
 
103
 
 
104
         Botan::Null_RNG null_rng;
 
105
         std::unique_ptr<Botan::Private_Key> key(new Botan::SM2_Encryption_PrivateKey(null_rng, domain, x));
 
106
         return key;
 
107
         }
 
108
   };
 
109
 
 
110
}
 
111
 
 
112
BOTAN_REGISTER_TEST("sm2_enc", SM2_Encryption_KAT_Tests);
 
113
 
 
114
 
 
115
#endif
 
116
 
 
117
}