rippled
openssl.cpp
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 #include <ripple/basics/contract.h>
21 #include <ripple/crypto/impl/openssl.h>
22 #include <openssl/hmac.h>
23 
24 namespace ripple {
25 namespace openssl {
26 
28 {
29  ptr = BN_new();
30  if (ptr == nullptr)
31  Throw<std::runtime_error>("BN_new() failed");
32 }
33 
34 void
35 bignum::assign(uint8_t const* data, size_t size)
36 {
37  // This reuses and assigns ptr
38  BIGNUM* bn = BN_bin2bn(data, size, ptr);
39  if (bn == nullptr)
40  Throw<std::runtime_error>("BN_bin2bn() failed");
41 }
42 
43 void
44 bignum::assign_new(uint8_t const* data, size_t size)
45 {
46  // ptr must not be allocated
47 
48  ptr = BN_bin2bn(data, size, nullptr);
49  if (ptr == nullptr)
50  Throw<std::runtime_error>("BN_bin2bn() failed");
51 }
52 
54 {
55  ptr = BN_CTX_new();
56  if (ptr == nullptr)
57  Throw<std::runtime_error>("BN_CTX_new() failed");
58 }
59 
60 bignum
61 get_order(EC_GROUP const* group, bn_ctx& ctx)
62 {
63  bignum result;
64  if (!EC_GROUP_get_order(group, result.get(), ctx.get()))
65  Throw<std::runtime_error>("EC_GROUP_get_order() failed");
66 
67  return result;
68 }
69 
70 ec_point::ec_point(EC_GROUP const* group)
71 {
72  ptr = EC_POINT_new(group);
73  if (ptr == nullptr)
74  Throw<std::runtime_error>("EC_POINT_new() failed");
75 }
76 
77 void
78 add_to(EC_GROUP const* group, ec_point const& a, ec_point& b, bn_ctx& ctx)
79 {
80  if (!EC_POINT_add(group, b.get(), a.get(), b.get(), ctx.get()))
81  Throw<std::runtime_error>("EC_POINT_add() failed");
82 }
83 
85 multiply(EC_GROUP const* group, bignum const& n, bn_ctx& ctx)
86 {
87  ec_point result(group);
88  if (!EC_POINT_mul(
89  group, result.get(), n.get(), nullptr, nullptr, ctx.get()))
90  Throw<std::runtime_error>("EC_POINT_mul() failed");
91 
92  return result;
93 }
94 
96 bn2point(EC_GROUP const* group, BIGNUM const* number)
97 {
98  EC_POINT* result = EC_POINT_bn2point(group, number, nullptr, nullptr);
99  if (result == nullptr)
100  Throw<std::runtime_error>("EC_POINT_bn2point() failed");
101 
102  return ec_point::acquire(result);
103 }
104 
105 static ec_key
107 {
108  EC_KEY* key = EC_KEY_new_by_curve_name(NID_secp256k1);
109 
110  if (key == nullptr)
111  Throw<std::runtime_error>("EC_KEY_new_by_curve_name() failed");
112 
113  EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
114 
115  return ec_key((ec_key::pointer_t)key);
116 }
117 
118 void
120 {
122  if (EC_KEY_set_public_key((EC_KEY*)key.get(), point.get()) <= 0)
123  Throw<std::runtime_error>("EC_KEY_set_public_key() failed");
124 
125  int const size = i2o_ECPublicKey((EC_KEY*)key.get(), &ptr);
126 
127  assert(size <= 33);
128  (void)size;
129 }
130 
131 } // namespace openssl
132 } // namespace ripple
133 
134 #include <stdio.h>
135 #ifdef _MSC_VER
136 FILE _iob[] = {*stdin, *stdout, *stderr};
137 extern "C" FILE* __cdecl __iob_func(void)
138 {
139  return _iob;
140 }
141 #endif
ripple::openssl::ec_point
Definition: openssl.h:172
ripple::openssl::ec_key::get
pointer_t get() const
Definition: ec_key.h:54
ripple::openssl::bn_ctx
Definition: openssl.h:133
ripple::openssl::ec_point::ptr
pointer_t ptr
Definition: openssl.h:178
ripple::openssl::bn_ctx::get
BN_CTX * get()
Definition: openssl.h:152
ripple::openssl::bignum::ptr
BIGNUM * ptr
Definition: openssl.h:34
ripple::openssl::ec_point::ec_point
ec_point(pointer_t raw)
Definition: openssl.h:180
ripple::openssl::bignum::bignum
bignum()
Definition: openssl.cpp:27
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_key_new_secp256k1_compressed
static ec_key ec_key_new_secp256k1_compressed()
Definition: openssl.cpp:106
ripple::openssl::ec_point::acquire
static ec_point acquire(pointer_t raw)
Definition: openssl.h:186
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::ec_key::pointer_t
struct opaque_EC_KEY * pointer_t
Definition: ec_key.h:32
ripple::openssl::bignum::get
BIGNUM * get()
Definition: openssl.h:82
std::uint8_t
ripple::openssl::bignum
Definition: openssl.h:31
ripple::openssl::get_order
bignum get_order(EC_GROUP const *group, bn_ctx &ctx)
Definition: openssl.cpp:61
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::openssl::ec_key
Definition: ec_key.h:29
ripple::openssl::ec_point::get
EC_POINT * get()
Definition: openssl.h:209
ripple::openssl::bignum::assign
void assign(uint8_t const *data, size_t size)
Definition: openssl.cpp:35
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