ffi-libsodium 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +191 -0
- data/README.md +21 -0
- data/lib/crypto/aead/chacha20_poly1305.rb +75 -0
- data/lib/crypto/auth.rb +54 -0
- data/lib/crypto/box.rb +150 -0
- data/lib/crypto/generic_hash.rb +123 -0
- data/lib/crypto/one_time_auth.rb +87 -0
- data/lib/crypto/pw_hash/scrypt_salsa208_sha256.rb +101 -0
- data/lib/crypto/scalar_mult.rb +55 -0
- data/lib/crypto/secret_box.rb +101 -0
- data/lib/crypto/short_hash.rb +41 -0
- data/lib/crypto/sign.rb +102 -0
- data/lib/libsodium.rb +21 -0
- data/lib/random_bytes.rb +21 -0
- data/lib/sodium.rb +69 -0
- data/lib/sodium/buffer.rb +11 -0
- data/lib/sodium/secret_buffer.rb +58 -0
- data/lib/sodium/utils.rb +71 -0
- data/lib/sodium/version.rb +3 -0
- metadata +90 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 613f14c043ff696627ef5d0009113367982a57a7
|
4
|
+
data.tar.gz: cc4459674fa3bc66f33e1868726ab63699b9f30e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0aec4788173e268528120fac6a2504a3fdf2a0a2c721f4b809edb9aaef4da6e59191e851ad829e86573010845a7a45764f0a1fb3884b3945374f1f1e302ee0ac
|
7
|
+
data.tar.gz: 00ac3a7187a277456a03f2db49f4896ebd952a2d29c9d5014a4a17cad51c222cccb12a78543fc822acd43ca36c8423919e58b8a1c1860a797b8d493c87f3c4d2
|
data/LICENSE
ADDED
@@ -0,0 +1,191 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
Copyright 2014 Hendrik Beskow
|
179
|
+
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
181
|
+
you may not use this file except in compliance with the License.
|
182
|
+
You may obtain a copy of the License at
|
183
|
+
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
185
|
+
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
189
|
+
See the License for the specific language governing permissions and
|
190
|
+
limitations under the License.
|
191
|
+
|
data/README.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
ruby-ffi-libsodium
|
2
|
+
===============
|
3
|
+
|
4
|
+
Secret Key derivation and user authentication: Store the salt and the mac and you can verify a user and give him a Secret Key without storing the password or key in a database.
|
5
|
+
|
6
|
+
```bash
|
7
|
+
bundle update
|
8
|
+
```
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
require 'bundler/setup'
|
12
|
+
require 'libsodium'
|
13
|
+
|
14
|
+
password = 'test123'
|
15
|
+
|
16
|
+
salt = Crypto::PwHash::ScryptSalsa208SHA256.salt
|
17
|
+
key = Crypto::PwHash.scryptsalsa208sha256(password, Crypto::Auth::KEYBYTES, salt)
|
18
|
+
mac = Crypto.auth(password, key)
|
19
|
+
|
20
|
+
puts Crypto::Auth.verify(mac, password, key)
|
21
|
+
```
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require_relative '../../sodium'
|
2
|
+
require_relative '../../sodium/utils'
|
3
|
+
require_relative '../../sodium/secret_buffer'
|
4
|
+
require_relative '../../random_bytes'
|
5
|
+
|
6
|
+
module Crypto
|
7
|
+
module AEAD
|
8
|
+
module Chacha20Poly1305
|
9
|
+
PRIMITIVE = 'chacha20poly1305'.freeze
|
10
|
+
|
11
|
+
extend FFI::Library
|
12
|
+
extend Sodium::Utils
|
13
|
+
|
14
|
+
ffi_lib :libsodium
|
15
|
+
|
16
|
+
class << self
|
17
|
+
def crypto_aead_chacha20poly1305_primitive
|
18
|
+
PRIMITIVE
|
19
|
+
end
|
20
|
+
|
21
|
+
alias_method :primitive, :crypto_aead_chacha20poly1305_primitive
|
22
|
+
end
|
23
|
+
|
24
|
+
attach_function :keybytes, :crypto_aead_chacha20poly1305_keybytes, [], :size_t
|
25
|
+
attach_function :npubbytes, :crypto_aead_chacha20poly1305_npubbytes, [], :size_t
|
26
|
+
attach_function :abytes, :crypto_aead_chacha20poly1305_abytes, [], :size_t
|
27
|
+
|
28
|
+
KEYBYTES = keybytes.freeze
|
29
|
+
NPUBBYTES = npubbytes.freeze
|
30
|
+
ABYTES = abytes.freeze
|
31
|
+
|
32
|
+
attach_function :crypto_aead_chacha20poly1305_encrypt, [:buffer_out, :buffer_out, :buffer_in, :ulong_long, :buffer_in, :ulong_long, :pointer, :buffer_in, :buffer_in], :int
|
33
|
+
attach_function :crypto_aead_chacha20poly1305_decrypt, [:buffer_out, :buffer_out, :pointer, :buffer_in, :ulong_long, :buffer_in, :ulong_long, :buffer_in, :buffer_in], :int
|
34
|
+
|
35
|
+
module_function
|
36
|
+
|
37
|
+
def nonce
|
38
|
+
RandomBytes.buf(NPUBBYTES)
|
39
|
+
end
|
40
|
+
|
41
|
+
def encrypt(message, additional_data, nonce, key)
|
42
|
+
message_len = get_size(message)
|
43
|
+
additional_data_len = get_size(additional_data)
|
44
|
+
check_length(nonce, NPUBBYTES, :Nonce)
|
45
|
+
check_length(key, KEYBYTES, :SecretKey)
|
46
|
+
|
47
|
+
ciphertext = FFI::MemoryPointer.new(:uchar, message_len + ABYTES)
|
48
|
+
ciphertext_len = FFI::MemoryPointer.new(:ulong_long)
|
49
|
+
key.readonly if key.is_a?(Sodium::SecretBuffer)
|
50
|
+
crypto_aead_chacha20poly1305_encrypt(ciphertext, ciphertext_len, message, message_len, additional_data, additional_data_len, nil, nonce, key)
|
51
|
+
key.noaccess if key.is_a?(Sodium::SecretBuffer)
|
52
|
+
|
53
|
+
[ciphertext, ciphertext_len.read_ulong_long]
|
54
|
+
end
|
55
|
+
|
56
|
+
def decrypt(ciphertext, clen, additional_data, nonce, key)
|
57
|
+
ciphertext_len = get_int(clen)
|
58
|
+
additional_data_len = get_size(additional_data)
|
59
|
+
check_length(nonce, NPUBBYTES, :Nonce)
|
60
|
+
check_length(key, KEYBYTES, :SecretKey)
|
61
|
+
|
62
|
+
decrypted = FFI::MemoryPointer.new(:uchar, ciphertext_len)
|
63
|
+
decrypted_len = FFI::MemoryPointer.new(:ulong_long)
|
64
|
+
key.readonly if key.is_a?(Sodium::SecretBuffer)
|
65
|
+
rc = crypto_aead_chacha20poly1305_decrypt(decrypted, decrypted_len, nil, ciphertext, ciphertext_len, additional_data, additional_data_len, nonce, key)
|
66
|
+
key.noaccess if key.is_a?(Sodium::SecretBuffer)
|
67
|
+
if rc == -1
|
68
|
+
raise Sodium::CryptoError, "Message forged", caller
|
69
|
+
end
|
70
|
+
|
71
|
+
[decrypted, decrypted_len.read_ulong_long]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
data/lib/crypto/auth.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require_relative '../sodium'
|
2
|
+
require_relative '../sodium/utils'
|
3
|
+
require_relative '../sodium/buffer'
|
4
|
+
require_relative '../sodium/secret_buffer'
|
5
|
+
|
6
|
+
module Crypto
|
7
|
+
module Auth
|
8
|
+
extend FFI::Library
|
9
|
+
extend Sodium::Utils
|
10
|
+
|
11
|
+
ffi_lib :libsodium
|
12
|
+
|
13
|
+
attach_function :primitive, :crypto_auth_primitive, [], :string
|
14
|
+
attach_function :bytes, :crypto_auth_bytes, [], :size_t
|
15
|
+
attach_function :keybytes, :crypto_auth_keybytes, [], :size_t
|
16
|
+
|
17
|
+
PRIMITIVE = primitive.freeze
|
18
|
+
BYTES = bytes.freeze
|
19
|
+
KEYBYTES = keybytes.freeze
|
20
|
+
|
21
|
+
attach_function :crypto_auth, [:buffer_out, :buffer_in, :ulong_long, :buffer_in], :int, blocking: true
|
22
|
+
attach_function :crypto_auth_verify, [:buffer_in, :buffer_in, :ulong_long, :buffer_in], :int, blocking: true
|
23
|
+
|
24
|
+
module_function
|
25
|
+
|
26
|
+
def auth(message, key)
|
27
|
+
message_len = get_size(message)
|
28
|
+
check_length(key, KEYBYTES, :SecretKey)
|
29
|
+
|
30
|
+
mac = Sodium::Buffer.new(:uchar, BYTES)
|
31
|
+
key.readonly if key.is_a?(Sodium::SecretBuffer)
|
32
|
+
crypto_auth(mac, message, message_len, key)
|
33
|
+
key.noaccess if key.is_a?(Sodium::SecretBuffer)
|
34
|
+
|
35
|
+
mac
|
36
|
+
end
|
37
|
+
|
38
|
+
def verify(mac, message, key)
|
39
|
+
check_length(mac, BYTES, :Mac)
|
40
|
+
message_len = get_size(message)
|
41
|
+
check_length(key, KEYBYTES, :SecretKey)
|
42
|
+
|
43
|
+
key.readonly if key.is_a?(Sodium::SecretBuffer)
|
44
|
+
rc = crypto_auth_verify(mac, message, message_len, key)
|
45
|
+
key.noaccess if key.is_a?(Sodium::SecretBuffer)
|
46
|
+
|
47
|
+
rc == 0
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.auth(*args)
|
52
|
+
Auth.auth(*args)
|
53
|
+
end
|
54
|
+
end
|
data/lib/crypto/box.rb
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
require_relative '../sodium'
|
2
|
+
require_relative '../sodium/utils'
|
3
|
+
require_relative '../sodium/buffer'
|
4
|
+
require_relative '../sodium/secret_buffer'
|
5
|
+
require_relative '../random_bytes'
|
6
|
+
|
7
|
+
module Crypto
|
8
|
+
module Box
|
9
|
+
extend FFI::Library
|
10
|
+
extend Sodium::Utils
|
11
|
+
|
12
|
+
ffi_lib :libsodium
|
13
|
+
|
14
|
+
attach_function :primitive, :crypto_box_primitive, [], :string
|
15
|
+
attach_function :seedbytes, :crypto_box_seedbytes, [], :size_t
|
16
|
+
attach_function :publickeybytes, :crypto_box_publickeybytes, [], :size_t
|
17
|
+
attach_function :secretkeybytes, :crypto_box_secretkeybytes, [], :size_t
|
18
|
+
attach_function :noncebytes, :crypto_box_noncebytes, [], :size_t
|
19
|
+
attach_function :macbytes, :crypto_box_macbytes, [], :size_t
|
20
|
+
|
21
|
+
PRIMITIVE = primitive.freeze
|
22
|
+
SEEDBYTES = seedbytes.freeze
|
23
|
+
PUBLICKEYBYTES = publickeybytes.freeze
|
24
|
+
SECRETKEYBYTES = secretkeybytes.freeze
|
25
|
+
NONCEBYTES = noncebytes.freeze
|
26
|
+
MACBYTES = macbytes.freeze
|
27
|
+
|
28
|
+
attach_function :crypto_box_keypair, [:buffer_out, :buffer_out], :int, blocking: true
|
29
|
+
attach_function :crypto_box_seed_keypair, [:buffer_out, :buffer_out, :buffer_in], :int, blocking: true
|
30
|
+
|
31
|
+
attach_function :crypto_box_easy, [:buffer_out, :buffer_in, :ulong_long, :buffer_in, :buffer_in, :buffer_in], :int, blocking: true
|
32
|
+
attach_function :crypto_box_open_easy, [:buffer_out, :buffer_in, :ulong_long, :buffer_in, :buffer_in, :buffer_in], :int, blocking: true
|
33
|
+
|
34
|
+
module_function
|
35
|
+
|
36
|
+
def nonce
|
37
|
+
RandomBytes.buf(NONCEBYTES)
|
38
|
+
end
|
39
|
+
|
40
|
+
def keypair
|
41
|
+
public_key = Sodium::Buffer.new(:uchar, PUBLICKEYBYTES)
|
42
|
+
secret_key = Sodium::Buffer.new(:uchar, SECRETKEYBYTES)
|
43
|
+
crypto_box_keypair(public_key, secret_key)
|
44
|
+
|
45
|
+
[public_key, secret_key]
|
46
|
+
end
|
47
|
+
|
48
|
+
def seed_keypair(seed)
|
49
|
+
check_length(seed, SEEDBYTES, :Seed)
|
50
|
+
|
51
|
+
public_key = Sodium::Buffer.new(:uchar, PUBLICKEYBYTES)
|
52
|
+
secret_key = Sodium::Buffer.new(:uchar, SECRETKEYBYTES)
|
53
|
+
crypto_box_seed_keypair(public_key, secret_key, seed)
|
54
|
+
|
55
|
+
[public_key, secret_key]
|
56
|
+
end
|
57
|
+
|
58
|
+
def memory_locked_keypair
|
59
|
+
public_key = Sodium::Buffer.new(:uchar, PUBLICKEYBYTES)
|
60
|
+
secret_key = Sodium::SecretBuffer.new(SECRETKEYBYTES)
|
61
|
+
crypto_box_keypair(public_key, secret_key)
|
62
|
+
secret_key.noaccess
|
63
|
+
|
64
|
+
[public_key, secret_key]
|
65
|
+
end
|
66
|
+
|
67
|
+
def memory_locked_seed_keypair(seed)
|
68
|
+
check_length(seed, SEEDBYTES, :Seed)
|
69
|
+
|
70
|
+
public_key = Sodium::Buffer.new(:uchar, PUBLICKEYBYTES)
|
71
|
+
secret_key = Sodium::SecretBuffer.new(:uchar, SECRETKEYBYTES)
|
72
|
+
crypto_box_seed_keypair(public_key, secret_key, seed)
|
73
|
+
secret_key.noaccess
|
74
|
+
|
75
|
+
[public_key, secret_key]
|
76
|
+
end
|
77
|
+
|
78
|
+
def easy(message, nonce, public_key, secret_key)
|
79
|
+
message_len = get_size(message)
|
80
|
+
check_length(nonce, NONCEBYTES, :Nonce)
|
81
|
+
check_length(public_key, PUBLICKEYBYTES, :PublicKey)
|
82
|
+
check_length(secret_key, SECRETKEYBYTES, :SecretKey)
|
83
|
+
|
84
|
+
ciphertext = Sodium::Buffer.new(:uchar, MACBYTES + message_len)
|
85
|
+
secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer)
|
86
|
+
crypto_box_easy(ciphertext, message, message_len, nonce, public_key, secret_key)
|
87
|
+
secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer)
|
88
|
+
|
89
|
+
ciphertext
|
90
|
+
end
|
91
|
+
|
92
|
+
def open_easy(ciphertext, nonce, public_key, secret_key)
|
93
|
+
ciphertext_len = get_size(ciphertext)
|
94
|
+
check_length(nonce, NONCEBYTES, :Nonce)
|
95
|
+
check_length(public_key, PUBLICKEYBYTES, :PublicKey)
|
96
|
+
check_length(secret_key, SECRETKEYBYTES, :SecretKey)
|
97
|
+
|
98
|
+
decrypted = Sodium::Buffer.new(:uchar, ciphertext_len - MACBYTES)
|
99
|
+
secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer)
|
100
|
+
rc = crypto_box_open_easy(decrypted, ciphertext, ciphertext_len, nonce, public_key, secret_key)
|
101
|
+
secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer)
|
102
|
+
if rc == -1
|
103
|
+
raise Sodium::CryptoError, "Message forged", caller
|
104
|
+
end
|
105
|
+
|
106
|
+
decrypted
|
107
|
+
end
|
108
|
+
|
109
|
+
def easy_in_place(data, nonce, public_key, secret_key)
|
110
|
+
message = get_string(data)
|
111
|
+
check_length(nonce, NONCEBYTES, :Nonce)
|
112
|
+
check_length(public_key, PUBLICKEYBYTES, :PublicKey)
|
113
|
+
check_length(secret_key, SECRETKEYBYTES, :SecretKey)
|
114
|
+
|
115
|
+
message_len = message.bytesize
|
116
|
+
message << zeros(MACBYTES)
|
117
|
+
secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer)
|
118
|
+
crypto_box_easy(message, message, message_len, nonce, public_key, secret_key)
|
119
|
+
secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer)
|
120
|
+
|
121
|
+
message
|
122
|
+
end
|
123
|
+
|
124
|
+
def open_easy_in_place(data, nonce, public_key, secret_key, utf8 = false)
|
125
|
+
ciphertext = get_string(data)
|
126
|
+
unless (message_len = ciphertext.bytesize - MACBYTES) > 0
|
127
|
+
fail Sodium::LengthError, "Ciphertext is too short", caller
|
128
|
+
end
|
129
|
+
|
130
|
+
check_length(nonce, NONCEBYTES, :Nonce)
|
131
|
+
check_length(public_key, PUBLICKEYBYTES, :PublicKey)
|
132
|
+
check_length(secret_key, SECRETKEYBYTES, :SecretKey)
|
133
|
+
|
134
|
+
secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer)
|
135
|
+
rc = crypto_box_open_easy(ciphertext, ciphertext, ciphertext.bytesize, nonce, public_key, secret_key)
|
136
|
+
secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer)
|
137
|
+
if rc == -1
|
138
|
+
raise Sodium::CryptoError, "Message forged", caller
|
139
|
+
end
|
140
|
+
|
141
|
+
if utf8
|
142
|
+
ciphertext.slice!(message_len..-1).force_encoding(Encoding::UTF_8)
|
143
|
+
else
|
144
|
+
ciphertext.slice!(message_len..-1)
|
145
|
+
end
|
146
|
+
|
147
|
+
ciphertext
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|