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& operator=(bignum const&) = delete;
39 
40  void assign_new (uint8_t const* data, size_t size);
41 
42 public:
43  bignum();
44 
46  {
47  if ( ptr != nullptr)
48  {
49  BN_free (ptr);
50  }
51  }
52 
53  bignum (uint8_t const* data, size_t size)
54  {
55  assign_new (data, size);
56  }
57 
58  template <class T>
59  explicit bignum (T const& thing)
60  {
61  assign_new (thing.data(), thing.size());
62  }
63 
64  bignum(bignum&& that) noexcept : ptr( that.ptr )
65  {
66  that.ptr = nullptr;
67  }
68 
69  bignum& operator= (bignum&& that) noexcept
70  {
71  using std::swap;
72 
73  swap( ptr, that.ptr );
74 
75  return *this;
76  }
77 
78  BIGNUM * get() { return ptr; }
79  BIGNUM const* get() const { return ptr; }
80 
81  bool is_zero() const { return BN_is_zero (ptr); }
82 
83  void clear() { BN_clear (ptr); }
84 
85  void assign (uint8_t const* data, size_t size);
86 };
87 
88 inline bool operator< (bignum const& a, bignum const& b)
89 {
90  return BN_cmp (a.get(), b.get()) < 0;
91 }
92 
93 inline bool operator>= (bignum const& a, bignum const& b)
94 {
95  return !(a < b);
96 }
97 
99 {
100  uint256 result;
101  result.zero();
102 
103  BN_bn2bin (number.get(), result.end() - BN_num_bytes (number.get()));
104 
105  number.clear();
106 
107  return result;
108 }
109 
110 class bn_ctx
111 {
112 private:
113  BN_CTX* ptr;
114 
115  // non-copyable
116  bn_ctx (bn_ctx const&);
117  bn_ctx& operator=(bn_ctx const&);
118 
119 public:
120  bn_ctx();
121 
123  {
124  BN_CTX_free (ptr);
125  }
126 
127  BN_CTX * get() { return ptr; }
128  BN_CTX const* get() const { return ptr; }
129 };
130 
131 bignum get_order (EC_GROUP const* group, bn_ctx& ctx);
132 
133 inline void add_to (bignum const& a,
134  bignum& b,
135  bignum const& modulus,
136  bn_ctx& ctx)
137 {
138  BN_mod_add (b.get(), a.get(), b.get(), modulus.get(), ctx.get());
139 }
140 
141 class ec_point
142 {
143 public:
144  using pointer_t = EC_POINT*;
145 
146 private:
148 
149  ec_point (pointer_t raw) : ptr(raw)
150  {
151  }
152 
153 public:
155  {
156  return ec_point (raw);
157  }
158 
159  ec_point (EC_GROUP const* group);
160 
161  ~ec_point() { EC_POINT_free (ptr); }
162 
163  ec_point (ec_point const&) = delete;
164  ec_point& operator=(ec_point const&) = delete;
165 
166  ec_point(ec_point&& that) noexcept
167  {
168  ptr = that.ptr;
169  that.ptr = nullptr;
170  }
171 
172  EC_POINT * get() { return ptr; }
173  EC_POINT const* get() const { return ptr; }
174 };
175 
176 void add_to (EC_GROUP const* group,
177  ec_point const& a,
178  ec_point& b,
179  bn_ctx& ctx);
180 
181 ec_point multiply (EC_GROUP const* group,
182  bignum const& n,
183  bn_ctx& ctx);
184 
185 ec_point bn2point (EC_GROUP const* group, BIGNUM const* number);
186 
187 // output buffer must hold 33 bytes
188 void serialize_ec_point (ec_point const& point, std::uint8_t* ptr);
189 
190 } // openssl
191 } // ripple
192 
193 #endif
ripple::openssl::ec_point
Definition: openssl.h:141
ripple::openssl::bignum::~bignum
~bignum()
Definition: openssl.h:45
ripple::openssl::bn_ctx::get
BN_CTX const * get() const
Definition: openssl.h:128
ripple::openssl::operator>=
bool operator>=(bignum const &a, bignum const &b)
Definition: openssl.h:93
ripple::openssl::bn_ctx
Definition: openssl.h:110
ripple::openssl::ec_point::ptr
pointer_t ptr
Definition: openssl.h:147
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:144
ripple::openssl::bn_ctx::get
BN_CTX * get()
Definition: openssl.h:127
ripple::openssl::bn_ctx::operator=
bn_ctx & operator=(bn_ctx const &)
ripple::base_uint::end
iterator end()
Definition: base_uint.h:107
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:98
ripple::openssl::ec_point::ec_point
ec_point(pointer_t raw)
Definition: openssl.h:149
ripple::openssl::bignum::is_zero
bool is_zero() const
Definition: openssl.h:81
ripple::openssl::bignum::bignum
bignum()
Definition: openssl.cpp:27
ripple::base_uint< 256 >
ripple::openssl::serialize_ec_point
void serialize_ec_point(ec_point const &point, std::uint8_t *ptr)
Definition: openssl.cpp:114
ripple::openssl::add_to
void add_to(EC_GROUP const *group, ec_point const &a, ec_point &b, bn_ctx &ctx)
Definition: openssl.cpp:74
ripple::openssl::ec_point::acquire
static ec_point acquire(pointer_t raw)
Definition: openssl.h:154
ripple::openssl::bn_ctx::~bn_ctx
~bn_ctx()
Definition: openssl.h:122
ripple::openssl::ec_point::ec_point
ec_point(ec_point &&that) noexcept
Definition: openssl.h:166
ripple::openssl::bignum::bignum
bignum(T const &thing)
Definition: openssl.h:59
ripple::openssl::bignum::assign_new
void assign_new(uint8_t const *data, size_t size)
Definition: openssl.cpp:42
ripple::openssl::bn_ctx::ptr
BN_CTX * ptr
Definition: openssl.h:113
ripple::openssl::bignum::get
BIGNUM * get()
Definition: openssl.h:78
std::uint8_t
ripple::openssl::bignum::bignum
bignum(bignum &&that) noexcept
Definition: openssl.h:64
ripple::openssl::bignum::clear
void clear()
Definition: openssl.h:83
ripple::openssl::bignum::get
BIGNUM const * get() const
Definition: openssl.h:79
ripple::openssl::bignum
Definition: openssl.h:31
std::swap
T swap(T... args)
ripple::openssl::ec_point::~ec_point
~ec_point()
Definition: openssl.h:161
ripple::openssl::get_order
bignum get_order(EC_GROUP const *group, bn_ctx &ctx)
Definition: openssl.cpp:58
ripple::openssl::operator<
bool operator<(bignum const &a, bignum const &b)
Definition: openssl.h:88
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:51
ripple::base_uint::zero
void zero()
Definition: base_uint.h:431
ripple::openssl::ec_point::get
EC_POINT * get()
Definition: openssl.h:172
ripple::openssl::ec_point::get
EC_POINT const * get() const
Definition: openssl.h:173
ripple::openssl::bignum::assign
void assign(uint8_t const *data, size_t size)
Definition: openssl.cpp:34
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:94
ripple::openssl::multiply
ec_point multiply(EC_GROUP const *group, bignum const &n, bn_ctx &ctx)
Definition: openssl.cpp:83
ripple::openssl::bignum::bignum
bignum(uint8_t const *data, size_t size)
Definition: openssl.h:53