rippled
openssl.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2014 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_OPENSSL_H
21 #define RIPPLE_OPENSSL_H
22 
23 #include <ripple/basics/base_uint.h>
24 #include <ripple/crypto/impl/ec_key.h>
25 #include <openssl/bn.h>
26 #include <openssl/ec.h>
27 
28 namespace ripple {
29 namespace openssl {
30 
31 class bignum
32 {
33 private:
34  BIGNUM* ptr;
35 
36  // non-copyable
37  bignum(bignum const&) = delete;
38  bignum&
39  operator=(bignum const&) = delete;
40 
41  void
42  assign_new(uint8_t const* data, size_t size);
43 
44 public:
45  bignum();
46 
48  {
49  if (ptr != nullptr)
50  {
51  BN_free(ptr);
52  }
53  }
54 
55  bignum(uint8_t const* data, size_t size)
56  {
57  assign_new(data, size);
58  }
59 
60  template <class T>
61  explicit bignum(T const& thing)
62  {
63  assign_new(thing.data(), thing.size());
64  }
65 
66  bignum(bignum&& that) noexcept : ptr(that.ptr)
67  {
68  that.ptr = nullptr;
69  }
70 
71  bignum&
72  operator=(bignum&& that) noexcept
73  {
74  using std::swap;
75 
76  swap(ptr, that.ptr);
77 
78  return *this;
79  }
80 
81  BIGNUM*
82  get()
83  {
84  return ptr;
85  }
86  BIGNUM const*
87  get() const
88  {
89  return ptr;
90  }
91 
92  bool
93  is_zero() const
94  {
95  return BN_is_zero(ptr);
96  }
97 
98  void
100  {
101  BN_clear(ptr);
102  }
103 
104  void
105  assign(uint8_t const* data, size_t size);
106 };
107 
108 inline bool
109 operator<(bignum const& a, bignum const& b)
110 {
111  return BN_cmp(a.get(), b.get()) < 0;
112 }
113 
114 inline bool
115 operator>=(bignum const& a, bignum const& b)
116 {
117  return !(a < b);
118 }
119 
120 inline uint256
122 {
123  uint256 result;
124  result.zero();
125 
126  BN_bn2bin(number.get(), result.end() - BN_num_bytes(number.get()));
127 
128  number.clear();
129 
130  return result;
131 }
132 
133 class bn_ctx
134 {
135 private:
136  BN_CTX* ptr;
137 
138  // non-copyable
139  bn_ctx(bn_ctx const&);
140  bn_ctx&
141  operator=(bn_ctx const&);
142 
143 public:
144  bn_ctx();
145 
147  {
148  BN_CTX_free(ptr);
149  }
150 
151  BN_CTX*
152  get()
153  {
154  return ptr;
155  }
156  BN_CTX const*
157  get() const
158  {
159  return ptr;
160  }
161 };
162 
163 bignum
164 get_order(EC_GROUP const* group, bn_ctx& ctx);
165 
166 inline void
167 add_to(bignum const& a, bignum& b, bignum const& modulus, bn_ctx& ctx)
168 {
169  BN_mod_add(b.get(), a.get(), b.get(), modulus.get(), ctx.get());
170 }
171 
172 class ec_point
173 {
174 public:
175  using pointer_t = EC_POINT*;
176 
177 private:
179 
180  ec_point(pointer_t raw) : ptr(raw)
181  {
182  }
183 
184 public:
185  static ec_point
187  {
188  return ec_point(raw);
189  }
190 
191  ec_point(EC_GROUP const* group);
192 
194  {
195  EC_POINT_free(ptr);
196  }
197 
198  ec_point(ec_point const&) = delete;
199  ec_point&
200  operator=(ec_point const&) = delete;
201 
202  ec_point(ec_point&& that) noexcept
203  {
204  ptr = that.ptr;
205  that.ptr = nullptr;
206  }
207 
208  EC_POINT*
209  get()
210  {
211  return ptr;
212  }
213  EC_POINT const*
214  get() const
215  {
216  return ptr;
217  }
218 };
219 
220 void
221 add_to(EC_GROUP const* group, ec_point const& a, ec_point& b, bn_ctx& ctx);
222 
223 ec_point
224 multiply(EC_GROUP const* group, bignum const& n, bn_ctx& ctx);
225 
226 ec_point
227 bn2point(EC_GROUP const* group, BIGNUM const* number);
228 
229 // output buffer must hold 33 bytes
230 void
231 serialize_ec_point(ec_point const& point, std::uint8_t* ptr);
232 
233 } // namespace openssl
234 } // namespace ripple
235 
236 #endif
ripple::openssl::ec_point
Definition: openssl.h:172
ripple::openssl::bignum::~bignum
~bignum()
Definition: openssl.h:47
ripple::openssl::bn_ctx::get
BN_CTX const * get() const
Definition: openssl.h:157
ripple::openssl::operator>=
bool operator>=(bignum const &a, bignum const &b)
Definition: openssl.h:115
ripple::openssl::bn_ctx
Definition: openssl.h:133
ripple::openssl::ec_point::ptr
pointer_t ptr
Definition: openssl.h:178
ripple::openssl::ec_point::operator=
ec_point & operator=(ec_point const &)=delete
ripple::openssl::ec_point::pointer_t
EC_POINT * pointer_t
Definition: openssl.h:175
ripple::openssl::bn_ctx::get
BN_CTX * get()
Definition: openssl.h:152
ripple::openssl::bn_ctx::operator=
bn_ctx & operator=(bn_ctx const &)
ripple::base_uint::end
iterator end()
Definition: base_uint.h:129
ripple::openssl::bignum::ptr
BIGNUM * ptr
Definition: openssl.h:34
ripple::openssl::uint256_from_bignum_clear
uint256 uint256_from_bignum_clear(bignum &number)
Definition: openssl.h:121
ripple::openssl::ec_point::ec_point
ec_point(pointer_t raw)
Definition: openssl.h:180
ripple::openssl::bignum::is_zero
bool is_zero() const
Definition: openssl.h:93
ripple::openssl::bignum::bignum
bignum()
Definition: openssl.cpp:27
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:73
ripple::openssl::serialize_ec_point
void serialize_ec_point(ec_point const &point, std::uint8_t *ptr)
Definition: openssl.cpp:119
ripple::openssl::add_to
void add_to(EC_GROUP const *group, ec_point const &a, ec_point &b, bn_ctx &ctx)
Definition: openssl.cpp:78
ripple::openssl::ec_point::acquire
static ec_point acquire(pointer_t raw)
Definition: openssl.h:186
ripple::openssl::bn_ctx::~bn_ctx
~bn_ctx()
Definition: openssl.h:146
ripple::openssl::ec_point::ec_point
ec_point(ec_point &&that) noexcept
Definition: openssl.h:202
ripple::openssl::bignum::bignum
bignum(T const &thing)
Definition: openssl.h:61
ripple::openssl::bignum::assign_new
void assign_new(uint8_t const *data, size_t size)
Definition: openssl.cpp:44
ripple::openssl::bn_ctx::ptr
BN_CTX * ptr
Definition: openssl.h:136
ripple::openssl::bignum::get
BIGNUM * get()
Definition: openssl.h:82
std::uint8_t
ripple::openssl::bignum::bignum
bignum(bignum &&that) noexcept
Definition: openssl.h:66
ripple::openssl::bignum::clear
void clear()
Definition: openssl.h:99
ripple::openssl::bignum::get
BIGNUM const * get() const
Definition: openssl.h:87
ripple::openssl::bignum
Definition: openssl.h:31
ripple::openssl::bignum::operator=
bignum & operator=(bignum &&that) noexcept
Definition: openssl.h:72
std::swap
T swap(T... args)
ripple::openssl::ec_point::~ec_point
~ec_point()
Definition: openssl.h:193
ripple::openssl::get_order
bignum get_order(EC_GROUP const *group, bn_ctx &ctx)
Definition: openssl.cpp:61
ripple::openssl::operator<
bool operator<(bignum const &a, bignum const &b)
Definition: openssl.h:109
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::openssl::bn_ctx::bn_ctx
bn_ctx()
Definition: openssl.cpp:53
ripple::base_uint::zero
void zero()
Definition: base_uint.h:449
ripple::openssl::ec_point::get
EC_POINT * get()
Definition: openssl.h:209
ripple::openssl::ec_point::get
EC_POINT const * get() const
Definition: openssl.h:214
ripple::openssl::bignum::assign
void assign(uint8_t const *data, size_t size)
Definition: openssl.cpp:35
ripple::openssl::bignum::operator=
bignum & operator=(bignum const &)=delete
ripple::openssl::bn2point
ec_point bn2point(EC_GROUP const *group, BIGNUM const *number)
Definition: openssl.cpp:96
ripple::openssl::multiply
ec_point multiply(EC_GROUP const *group, bignum const &n, bn_ctx &ctx)
Definition: openssl.cpp:85
ripple::openssl::bignum::bignum
bignum(uint8_t const *data, size_t size)
Definition: openssl.h:55