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