rippled
PreimageSha256_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2016 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/basics/Buffer.h>
21 #include <ripple/basics/Slice.h>
22 #include <ripple/basics/strHex.h>
23 #include <ripple/beast/unit_test.h>
24 #include <ripple/conditions/Condition.h>
25 #include <ripple/conditions/Fulfillment.h>
26 #include <ripple/conditions/impl/PreimageSha256.h>
27 #include <algorithm>
28 #include <string>
29 #include <utility>
30 #include <vector>
31 
32 namespace ripple {
33 namespace cryptoconditions {
34 
35 class PreimageSha256_test : public beast::unit_test::suite
36 {
37  inline Buffer
38  hexblob(std::string const& s)
39  {
41  x.reserve(s.size() / 2);
42 
43  auto iter = s.cbegin();
44 
45  while (iter != s.cend())
46  {
47  int cHigh = charUnHex(*iter++);
48 
49  if (cHigh < 0)
50  return {};
51 
52  int cLow = charUnHex(*iter++);
53 
54  if (cLow < 0)
55  return {};
56 
57  x.push_back(
58  static_cast<std::uint8_t>(cHigh << 4) |
59  static_cast<std::uint8_t>(cLow));
60  }
61 
62  return {x.data(), x.size()};
63  }
64 
65  void
67  {
68  testcase("Known Vectors");
69 
70  std::pair<std::string, std::string> const known[] = {
71  {"A0028000",
72  "A0258020E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7"
73  "852B855810100"},
74  {"A0058003616161",
75  "A02580209834876DCFB05CB167A5C24953EBA58C4AC89B1ADF57F28F2F9D09AF1"
76  "07EE8F0810103"},
77  };
78 
79  std::error_code ec;
80 
81  auto f1 = Fulfillment::deserialize(hexblob(known[0].first), ec);
82  BEAST_EXPECT(f1);
83  BEAST_EXPECT(!ec);
84 
85  auto c1 = Condition::deserialize(hexblob(known[0].second), ec);
86  BEAST_EXPECT(c1);
87  BEAST_EXPECT(!ec);
88 
89  auto f2 = Fulfillment::deserialize(hexblob(known[1].first), ec);
90  BEAST_EXPECT(f2);
91  BEAST_EXPECT(!ec);
92 
93  auto c2 = Condition::deserialize(hexblob(known[1].second), ec);
94  BEAST_EXPECT(c2);
95  BEAST_EXPECT(!ec);
96 
97  // Check equality and inequality
98  BEAST_EXPECT(f1->condition() == *c1);
99  BEAST_EXPECT(f1->condition() != *c2);
100  BEAST_EXPECT(f2->condition() == *c2);
101  BEAST_EXPECT(f2->condition() != *c1);
102  BEAST_EXPECT(*c1 != *c2);
103  BEAST_EXPECT(*c1 == *c1);
104  BEAST_EXPECT(f1->condition() == f1->condition());
105 
106  // Should validate with the empty string
107  BEAST_EXPECT(validate(*f1, *c1));
108  BEAST_EXPECT(validate(*f2, *c2));
109 
110  // And with any string - the message doesn't matter for PrefixSha256
111  BEAST_EXPECT(validate(*f1, *c1, makeSlice(known[0].first)));
112  BEAST_EXPECT(validate(*f1, *c1, makeSlice(known[0].second)));
113  BEAST_EXPECT(validate(*f2, *c2, makeSlice(known[0].first)));
114  BEAST_EXPECT(validate(*f2, *c2, makeSlice(known[0].second)));
115 
116  // Shouldn't validate if the fulfillment & condition don't match
117  // regardless of the message.
118  BEAST_EXPECT(!validate(*f2, *c1));
119  BEAST_EXPECT(!validate(*f2, *c1, makeSlice(known[0].first)));
120  BEAST_EXPECT(!validate(*f2, *c1, makeSlice(known[0].second)));
121  BEAST_EXPECT(!validate(*f1, *c2));
122  BEAST_EXPECT(!validate(*f1, *c2, makeSlice(known[0].first)));
123  BEAST_EXPECT(!validate(*f1, *c2, makeSlice(known[0].second)));
124  }
125 
126  void
128  {
129  testcase("Other Types");
130 
131  std::pair<std::string, std::string> const others[] = {
132  // PREFIX + PREIMAGE:
133  {"A10B8000810100A204A0028000",
134 
135  "A12A8020BB1AC5260C0141B7E54B26EC2330637C5597BF811951AC09E744AD20F"
136  "F77E287810204"
137  "0082020780"},
138 
139  // THRESHOLD:
140  {"A208A004A0028000A100",
141 
142  "A22A8020B4B84136DF48A71D73F4985C04C6767A778ECB65BA7023B4506823BEE"
143  "E7631B9810204"
144  "0082020780"},
145 
146  // RSA:
147  {"A382020880820100E1EF8B24D6F76B09C81ED7752AA262F044F04A874D43809D3"
148  "1CEA612F99B0C97"
149  "A8B4374153E3EEF3D66616843E0E41C293264B71B6173DB1CF0D6CD558C586577"
150  "06FCF097F704C48"
151  "3E59CBFDFD5B3EE7BC80D740C5E0F047F3E85FC0D75815776A6F3F23C5DC5E797"
152  "139A6882E38336A"
153  "4A5FB36137620FF3663DBAE328472801862F72F2F87B202B9C89ADD7CD5B0A076"
154  "F7C53E35039F67E"
155  "D17EC815E5B4305CC63197068D5E6E579BA6DE5F4E3E57DF5E4E072FF2CE4C66E"
156  "B45233973875275"
157  "9639F0257BF57DBD5C443FB5158CCE0A3D36ADC7BA01F33A0BB6DBB2BF989D607"
158  "112F2344D993E77"
159  "E563C1D361DEDF57DA96EF2CFC685F002B638246A5B309B981820100BD42D6569"
160  "F6599AED455F96B"
161  "C0ED08ED1480BF36CD9E1467F9C6F74461C9E3A749334B2F6404AA5F9F6BAFE76"
162  "C347D069250B35D"
163  "1C970C793059EE733A8193F30FA78FEC7CAE459E3DDFD7633805D476940D0CB53"
164  "D7FB389DCDAEAF6"
165  "E8CF48C4B5635430E4F2BCDFE505C2C0FC17B40D93C7EDB7C261EBF43895A705E"
166  "024AA0549A660F7"
167  "0A32150647522DBE6B63520497CFF8F8D5D74768A27C5B86E580BE3FCDC96F197"
168  "6293CBA0D58DFC6"
169  "0B518B632A6DC1E950C43E231FE1A379AA6DDCC52C70EDF851C6C0123A964261C"
170  "FDB3857CD6CD5AD"
171  "C37D8DA2CC924EDAE1D84CF6124587F274C1FA3697DA2901F0269F03B243C03B6"
172  "14E0385E1961FAC"
173  "5000F9BB",
174 
175  "A3258020484950515253545548495051525354554849505152535455484950515"
176  "2535455810101"},
177 
178  // ED25519:
179  {"A4648020D75A980182B10AB7D54BFED3C964073A0EE172F3DAA62325AF021A68F"
180  "707511A8140E556"
181  "4300C360AC729086E2CC806E828A84877F1EB8E5D974D873E065224901555FB88"
182  "21590A33BACC61E"
183  "39701CF9B46BD25BF5F0595BBE24655141438E7A100B",
184 
185  "A4278020799239ABA8FC4FF7EABFBC4C44E69E8BDFED993324E12ED64792ABE28"
186  "9CF1D5F810302"
187  "0000"}};
188 
189  for (auto x : others)
190  {
191  std::error_code ec;
192 
193  BEAST_EXPECT(!Fulfillment::deserialize(hexblob(x.first), ec));
194  BEAST_EXPECT(!Condition::deserialize(hexblob(x.second), ec));
195  }
196  }
197 
198  void
199  run() override
200  {
202  testOtherTypes();
203  }
204 };
205 
206 BEAST_DEFINE_TESTSUITE(PreimageSha256, conditions, ripple);
207 
208 } // namespace cryptoconditions
209 
210 } // namespace ripple
ripple::makeSlice
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
Definition: Slice.h:194
std::string
STL class.
ripple::cryptoconditions::Fulfillment::deserialize
static std::unique_ptr< Fulfillment > deserialize(Slice s, std::error_code &ec)
Load a fulfillment from its binary form.
Definition: Fulfillment.cpp:59
utility
ripple::charUnHex
int charUnHex(unsigned char c)
Converts a hex digit to the corresponding integer.
Definition: strHex.cpp:27
std::pair
std::vector::reserve
T reserve(T... args)
vector
std::string::size
T size(T... args)
ripple::cryptoconditions::PreimageSha256_test::testOtherTypes
void testOtherTypes()
Definition: PreimageSha256_test.cpp:127
ripple::cryptoconditions::PreimageSha256_test::run
void run() override
Definition: PreimageSha256_test.cpp:199
ripple::Buffer
Like std::vector<char> but better.
Definition: Buffer.h:35
algorithm
std::error_code
STL class.
ripple::cryptoconditions::PreimageSha256_test::testKnownVectors
void testKnownVectors()
Definition: PreimageSha256_test.cpp:66
std::vector::push_back
T push_back(T... args)
ripple::cryptoconditions::PreimageSha256_test
Definition: PreimageSha256_test.cpp:35
std::uint8_t
ripple::cryptoconditions::validate
bool validate(Fulfillment const &f, Condition const &c, Slice m)
Verify if the given message satisfies the fulfillment.
Definition: Fulfillment.cpp:47
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::string::cbegin
T cbegin(T... args)
std::string::cend
T cend(T... args)
ripple::cryptoconditions::PreimageSha256_test::hexblob
Buffer hexblob(std::string const &s)
Definition: PreimageSha256_test.cpp:38
ripple::cryptoconditions::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(PreimageSha256, conditions, ripple)
std::vector::data
T data(T... args)
ripple::cryptoconditions::Condition::deserialize
static std::unique_ptr< Condition > deserialize(Slice s, std::error_code &ec)
Load a condition from its binary form.
Definition: Condition.cpp:154
string