rippled
Loading...
Searching...
No Matches
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 <xrpld/conditions/Condition.h>
24#include <xrpld/conditions/Fulfillment.h>
25#include <xrpld/conditions/detail/error.h>
26#include <xrpl/basics/Buffer.h>
27#include <xrpl/basics/Slice.h>
28#include <xrpl/protocol/digest.h>
29#include <memory>
30
31namespace ripple {
32namespace cryptoconditions {
33
34class PreimageSha256 final : public Fulfillment
35{
36public:
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
101private:
103
104public:
105 PreimageSha256(Buffer&& b) noexcept : payload_(std::move(b))
106 {
107 }
108
110 {
111 }
112
113 Type
114 type() const override
115 {
117 }
118
119 Buffer
120 fingerprint() const override
121 {
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
135 condition() const override
136 {
137 return {type(), cost(), fingerprint()};
138 }
139
140 bool
141 validate(Slice) const override
142 {
143 // Perhaps counterintuitively, the message isn't
144 // relevant.
145 return true;
146 }
147};
148
149} // namespace cryptoconditions
150} // namespace ripple
151
152#endif
Like std::vector<char> but better.
Definition: Buffer.h:36
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition: Buffer.h:127
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Buffer.h:151
An immutable linear range of bytes.
Definition: Slice.h:45
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition: Slice.h:80
std::size_t length() const noexcept
Definition: Slice.h:86
bool validate(Slice) const override
Validates a fulfillment.
Buffer fingerprint() const override
Returns the fulfillment's fingerprint:
static constexpr std::size_t maxPreimageLength
The maximum allowed length of a preimage.
Condition condition() const override
Returns the condition associated with the given fulfillment.
Type type() const override
Returns the type of this condition.
static std::unique_ptr< Fulfillment > deserialize(Slice s, std::error_code &ec)
Parse the payload for a PreimageSha256 condition.
std::uint32_t cost() const override
Calculates the cost associated with this fulfillment.
T data(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
SHA-256 digest.
Definition: digest.h:93