rippled
PreimageSha256.h
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 #ifndef RIPPLE_CONDITIONS_PREIMAGE_SHA256_H
21 #define RIPPLE_CONDITIONS_PREIMAGE_SHA256_H
22 
23 #include <ripple/basics/Buffer.h>
24 #include <ripple/basics/Slice.h>
25 #include <ripple/conditions/Condition.h>
26 #include <ripple/conditions/Fulfillment.h>
27 #include <ripple/conditions/impl/error.h>
28 #include <ripple/protocol/digest.h>
29 #include <memory>
30 
31 namespace ripple {
32 namespace cryptoconditions {
33 
34 class PreimageSha256 final : public Fulfillment
35 {
36 public:
46  static constexpr std::size_t maxPreimageLength = 128;
47 
56  {
57  // Per the RFC, a preimage fulfulliment is defined as
58  // follows:
59  //
60  // PreimageFulfillment ::= SEQUENCE {
61  // preimage OCTET STRING
62  // }
63 
64  using namespace der;
65 
66  auto p = parsePreamble(s, ec);
67  if (ec)
68  return nullptr;
69 
70  if (!isPrimitive(p) || !isContextSpecific(p))
71  {
73  return {};
74  }
75 
76  if (p.tag != 0)
77  {
79  return {};
80  }
81 
82  if (s.size() != p.length)
83  {
85  return {};
86  }
87 
88  if (s.size() > maxPreimageLength)
89  {
91  return {};
92  }
93 
94  auto b = parseOctetString(s, p.length, ec);
95  if (ec)
96  return {};
97 
98  return std::make_unique<PreimageSha256>(std::move(b));
99  }
100 
101 private:
103 
104 public:
105  PreimageSha256(Buffer&& b) noexcept : payload_(std::move(b))
106  {
107  }
108 
109  PreimageSha256(Slice s) noexcept : payload_(s)
110  {
111  }
112 
113  Type
114  type() const override
115  {
116  return Type::preimageSha256;
117  }
118 
119  Buffer
120  fingerprint() const override
121  {
122  sha256_hasher h;
123  h(payload_.data(), payload_.size());
124  auto const d = static_cast<sha256_hasher::result_type>(h);
125  return {d.data(), d.size()};
126  }
127 
129  cost() const override
130  {
131  return static_cast<std::uint32_t>(payload_.size());
132  }
133 
134  Condition
135  condition() const override
136  {
137  return {type(), cost(), fingerprint()};
138  }
139 
140  bool validate(Slice) const override
141  {
142  // Perhaps counterintuitively, the message isn't
143  // relevant.
144  return true;
145  }
146 };
147 
148 } // namespace cryptoconditions
149 } // namespace ripple
150 
151 #endif
ripple::Slice::size
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition: Slice.h:80
ripple::cryptoconditions::Type::preimageSha256
@ preimageSha256
ripple::openssl_sha256_hasher
SHA-256 digest.
Definition: digest.h:90
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
ripple::cryptoconditions::PreimageSha256::validate
bool validate(Slice) const override
Validates a fulfillment.
Definition: PreimageSha256.h:140
ripple::cryptoconditions::PreimageSha256
Definition: PreimageSha256.h:34
ripple::Slice::length
std::size_t length() const noexcept
Definition: Slice.h:86
ripple::Buffer
Like std::vector<char> but better.
Definition: Buffer.h:35
ripple::cryptoconditions::Condition
Definition: Condition.h:44
ripple::cryptoconditions::error::incorrect_encoding
@ incorrect_encoding
ripple::cryptoconditions::PreimageSha256::fingerprint
Buffer fingerprint() const override
Returns the fulfillment's fingerprint:
Definition: PreimageSha256.h:120
ripple::cryptoconditions::PreimageSha256::deserialize
static std::unique_ptr< Fulfillment > deserialize(Slice s, std::error_code &ec)
Parse the payload for a PreimageSha256 condition.
Definition: PreimageSha256.h:55
std::error_code
STL class.
ripple::cryptoconditions::PreimageSha256::cost
std::uint32_t cost() const override
Calculates the cost associated with this fulfillment.
Definition: PreimageSha256.h:129
ripple::cryptoconditions::PreimageSha256::payload_
Buffer payload_
Definition: PreimageSha256.h:102
std::array< std::uint8_t, 32 >
ripple::cryptoconditions::error::unexpected_tag
@ unexpected_tag
ripple::Buffer::size
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition: Buffer.h:126
ripple::Buffer::data
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Buffer.h:150
std::uint32_t
memory
ripple::cryptoconditions::PreimageSha256::condition
Condition condition() const override
Returns the condition associated with the given fulfillment.
Definition: PreimageSha256.h:135
ripple::cryptoconditions::Type
Type
Definition: Condition.h:36
ripple::cryptoconditions::error::preimage_too_long
@ preimage_too_long
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::cryptoconditions::PreimageSha256::PreimageSha256
PreimageSha256(Buffer &&b) noexcept
Definition: PreimageSha256.h:105
ripple::cryptoconditions::PreimageSha256::type
Type type() const override
Returns the type of this condition.
Definition: PreimageSha256.h:114
std::size_t
ripple::cryptoconditions::Fulfillment
Definition: Fulfillment.h:31
ripple::cryptoconditions::PreimageSha256::PreimageSha256
PreimageSha256(Slice s) noexcept
Definition: PreimageSha256.h:109
std::unique_ptr
STL class.
ripple::cryptoconditions::error::trailing_garbage
@ trailing_garbage
std::array::data
T data(T... args)
ripple::cryptoconditions::PreimageSha256::maxPreimageLength
static constexpr std::size_t maxPreimageLength
The maximum allowed length of a preimage.
Definition: PreimageSha256.h:46