cf-ruby-libecp 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/libecp.rb +97 -0
- data/lib/libecp.so +0 -0
- metadata +49 -0
data/lib/libecp.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
=begin
|
4
|
+
This library is a ruby wrapper for the C++ libecp library. It supports signing and generating public/private key pairs specific to coinfloor.
|
5
|
+
|
6
|
+
Copyright 2014 Coinfloor LTD.
|
7
|
+
|
8
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
9
|
+
you may not use this file except in compliance with the License.
|
10
|
+
You may obtain a copy of the License at
|
11
|
+
|
12
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
13
|
+
|
14
|
+
Unless required by applicable law or agreed to in writing, software
|
15
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
16
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
17
|
+
See the License for the specific language governing permissions and
|
18
|
+
limitations under the License.
|
19
|
+
=end
|
20
|
+
|
21
|
+
require 'base64'
|
22
|
+
require 'securerandom'
|
23
|
+
require 'openssl'
|
24
|
+
require 'ffi'
|
25
|
+
|
26
|
+
module LibEcp
|
27
|
+
extend FFI::Library
|
28
|
+
# load the C++ shared library libecp.so
|
29
|
+
ffi_lib Gem.find_files("libecp.so")[0]
|
30
|
+
|
31
|
+
# attached methods from the libecp C++ library
|
32
|
+
attach_function :ecp_pubkey_u8, [:pointer, :buffer_in, :buffer_in, :buffer_in, :buffer_in, :size_t], :void
|
33
|
+
attach_function :ecp_sign_u8, [:pointer, :pointer, :buffer_in, :buffer_in, :buffer_in, :buffer_in, :buffer_in, :buffer_in, :size_t], :void
|
34
|
+
attach_function :ecp_verify_u8, [:buffer_in, :buffer_in, :buffer_in, :buffer_in, :buffer_in, :buffer_in, :buffer_in, :buffer_in, :size_t], :bool
|
35
|
+
|
36
|
+
@@cookie_secret="\xF9+\xB46h\x1Cn\x9E\x9Cu\xF1\x1F\xED{Yi".force_encoding("ASCII-8BIT")
|
37
|
+
|
38
|
+
# get the byte string of a, G, p and n
|
39
|
+
|
40
|
+
p="\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xE5m".force_encoding("ASCII-8BIT")
|
41
|
+
a="\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00".force_encoding("ASCII-8BIT")
|
42
|
+
g="\x00\xA1E[3M\xF0\x99\xDF0\xFC(\xA1i\xA4g\xE9\xE4pu\xA9\x0F~e\x0E\xB6\xB7\xA4\\\x00~\b\x9F\xED\x7F\xBA4B\x82\xCA\xFB\xD6\xF7\xE3\x19\xF7\xC0\xB0\xBDY\xE2\xCAK\xDBUma\xA5\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01".force_encoding("ASCII-8BIT")
|
43
|
+
n="\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xDC\xE8\xD2\xECa\x84\xCA\xF0\xA9qv\x9F\xB1\xF7".force_encoding("ASCII-8BIT")
|
44
|
+
|
45
|
+
#create the buffers
|
46
|
+
@@pb = FFI::MemoryPointer.new(:char, 29)
|
47
|
+
@@pb.put_bytes(0,p)
|
48
|
+
@@ab = FFI::MemoryPointer.new(:char, 29)
|
49
|
+
@@ab.put_bytes(0,a)
|
50
|
+
@@gb = FFI::MemoryPointer.new(:char, 29*3)
|
51
|
+
@@gb.put_bytes(0,g)
|
52
|
+
@@nb = FFI::MemoryPointer.new(:char, 29)
|
53
|
+
@@nb.put_bytes(0,n)
|
54
|
+
def self.gen_cookie(uid)#uid should be a bytestring
|
55
|
+
packed_cs=(@@cookie_secret+uid)
|
56
|
+
return Base64.encode64(OpenSSL::Digest.digest("SHA1",packed_cs)).rstrip
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.gen_nonce
|
60
|
+
return SecureRandom.random_bytes.rstrip
|
61
|
+
end
|
62
|
+
|
63
|
+
# Generates user id as a bytestring
|
64
|
+
def self.gen_uid(user_id)
|
65
|
+
(user_id >> 56 & 0xFF).chr + (user_id >> 48 & 0xFF).chr + (user_id >> 40 & 0xFF).chr + (user_id >> 32 & 0xFF).chr + (user_id >> 24 & 0xFF).chr + (user_id >> 16 & 0xFF).chr + (user_id >> 8 & 0xFF).chr + (user_id & 0xFF).chr
|
66
|
+
end
|
67
|
+
|
68
|
+
# Generates users private key, Arguments: user id bytestring (from gen_uid), password String
|
69
|
+
def self.private_key(uid,pass)
|
70
|
+
return OpenSSL::Digest.digest("SHA224", uid + pass)
|
71
|
+
end
|
72
|
+
|
73
|
+
# Generates public key from private key
|
74
|
+
def self.gen_pub(priv_key)
|
75
|
+
#create pointer
|
76
|
+
qbuf = FFI::MemoryPointer.new(:char, 29*3)
|
77
|
+
zbuf = FFI::MemoryPointer.new(:char, 29)
|
78
|
+
zbuf.put_bytes(1,priv_key)
|
79
|
+
LibEcp::ecp_pubkey_u8 qbuf, @@pb, @@ab, @@gb, zbuf, 29
|
80
|
+
return [Base64.encode64(qbuf.get_bytes(1, 28)).rstrip,Base64.encode64(qbuf.get_bytes(30, 28)).rstrip]
|
81
|
+
end
|
82
|
+
|
83
|
+
# Arguments: user id, server nonce, client nonce, users private key ; returns an array with the two coordinates (i.e. the signature)
|
84
|
+
def self.sign(user_id,snonce,cnonce,priv_key)
|
85
|
+
rbuf = FFI::MemoryPointer.new(:char, 29)
|
86
|
+
sbuf = FFI::MemoryPointer.new(:char, 29)
|
87
|
+
dbuf = FFI::MemoryPointer.new(:char, 29)
|
88
|
+
dbuf.put_bytes(1,priv_key)
|
89
|
+
zbuf = FFI::MemoryPointer.new(:char, 29)
|
90
|
+
zbuf.put_bytes(1, OpenSSL::Digest.digest("SHA224", user_id + snonce + cnonce))
|
91
|
+
LibEcp::ecp_sign_u8 rbuf, sbuf, @@pb, @@ab, @@gb, @@nb, dbuf, zbuf, 29
|
92
|
+
# these are the coordinates that is the signature:
|
93
|
+
return [ rbuf.get_bytes(1, 28), sbuf.get_bytes(1, 28) ]
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
#=end
|
data/lib/libecp.so
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cf-ruby-libecp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Coinfloor
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-01-02 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Libecp is a C++ elliptic curve cryptography library for signing,verifying
|
15
|
+
signatures and generating keys.
|
16
|
+
email: development@coinfloor.co.uk
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/libecp.rb
|
22
|
+
- lib/libecp.so
|
23
|
+
homepage: https://github.com/coinfloor/ruby-libecp
|
24
|
+
licenses:
|
25
|
+
- APACHE 2.0
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ! '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 1.9.3
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 1.8.25
|
45
|
+
signing_key:
|
46
|
+
specification_version: 3
|
47
|
+
summary: A ruby wrapper for libecp
|
48
|
+
test_files: []
|
49
|
+
has_rdoc:
|