rbthemis 0.10.0 → 0.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/rbthemis.rb +992 -0
- metadata +12 -15
- data/lib/rubythemis.rb +0 -645
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rbthemis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.14.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- CossackLabs
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-12-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ffi
|
@@ -30,23 +30,21 @@ dependencies:
|
|
30
30
|
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: 1.9.8
|
33
|
-
description: Themis is a
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
and cross-platform availability.
|
33
|
+
description: Themis is a convenient cryptographic library for data protection. It
|
34
|
+
provides secure messaging with forward secrecy and secure data storage. Themis is
|
35
|
+
aimed at modern development practices and has a unified API across 12 platforms,
|
36
|
+
including Ruby, JavaScript, iOS/macOS, Python, and Java/Android.
|
38
37
|
email: dev@cossacklabs.com
|
39
38
|
executables: []
|
40
39
|
extensions: []
|
41
40
|
extra_rdoc_files: []
|
42
41
|
files:
|
43
|
-
- lib/
|
42
|
+
- lib/rbthemis.rb
|
44
43
|
homepage: http://cossacklabs.com/
|
45
44
|
licenses:
|
46
45
|
- Apache-2.0
|
47
46
|
metadata: {}
|
48
|
-
post_install_message:
|
49
|
-
your system using `gem uninstall rubythemis`
|
47
|
+
post_install_message:
|
50
48
|
rdoc_options: []
|
51
49
|
require_paths:
|
52
50
|
- lib
|
@@ -61,10 +59,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
59
|
- !ruby/object:Gem::Version
|
62
60
|
version: '0'
|
63
61
|
requirements:
|
64
|
-
- libthemis, v0.
|
65
|
-
|
66
|
-
|
67
|
-
signing_key:
|
62
|
+
- libthemis, v0.14.0
|
63
|
+
rubygems_version: 3.1.4
|
64
|
+
signing_key:
|
68
65
|
specification_version: 4
|
69
66
|
summary: Data security library for network communication and data storage for Ruby
|
70
67
|
test_files: []
|
data/lib/rubythemis.rb
DELETED
@@ -1,645 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright (c) 2015 Cossack Labs Limited
|
3
|
-
#
|
4
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
-
# you may not use this file except in compliance with the License.
|
6
|
-
# You may obtain a copy of the License at
|
7
|
-
#
|
8
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
-
#
|
10
|
-
# Unless required by applicable law or agreed to in writing, software
|
11
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
-
# See the License for the specific language governing permissions and
|
14
|
-
# limitations under the License.
|
15
|
-
#
|
16
|
-
|
17
|
-
require 'ffi'
|
18
|
-
|
19
|
-
module ThemisCommon
|
20
|
-
def string_to_pointer_size(string)
|
21
|
-
string_buf = FFI::MemoryPointer.new(
|
22
|
-
:char, string.force_encoding('BINARY').size)
|
23
|
-
string_buf.put_bytes(0, string.force_encoding('BINARY'),
|
24
|
-
0, string.force_encoding('BINARY').size)
|
25
|
-
[string_buf, string.force_encoding('BINARY').size]
|
26
|
-
end
|
27
|
-
module_function :string_to_pointer_size
|
28
|
-
end
|
29
|
-
|
30
|
-
module ThemisImport
|
31
|
-
extend FFI::Library
|
32
|
-
ffi_lib 'themis'
|
33
|
-
|
34
|
-
callback :get_pub_key_by_id_type,
|
35
|
-
[:pointer, :int, :pointer, :int, :pointer], :int
|
36
|
-
callback :send_callback_type, [:pointer, :int, :uint], :int
|
37
|
-
callback :receive_callback_type, [:pointer, :int, :uint], :int
|
38
|
-
|
39
|
-
class CallbacksStruct < FFI::Struct
|
40
|
-
layout :send_data, :send_callback_type,
|
41
|
-
:receive_data, :receive_callback_type,
|
42
|
-
:state_changed, :pointer,
|
43
|
-
:get_pub_key_for_id, :get_pub_key_by_id_type,
|
44
|
-
:user_data, :pointer
|
45
|
-
end
|
46
|
-
|
47
|
-
attach_function :secure_session_create,
|
48
|
-
[:pointer, :uint, :pointer, :uint, :pointer], :pointer
|
49
|
-
attach_function :secure_session_destroy, [:pointer], :int
|
50
|
-
attach_function :secure_session_generate_connect_request,
|
51
|
-
[:pointer, :pointer, :pointer], :int
|
52
|
-
|
53
|
-
attach_function :secure_session_wrap,
|
54
|
-
[:pointer, :pointer, :int, :pointer, :pointer], :int
|
55
|
-
attach_function :secure_session_unwrap,
|
56
|
-
[:pointer, :pointer, :int, :pointer, :pointer], :int
|
57
|
-
attach_function :secure_session_is_established, [:pointer], :bool
|
58
|
-
|
59
|
-
attach_function :themis_secure_message_wrap,
|
60
|
-
[:pointer, :int, :pointer, :int, :pointer,
|
61
|
-
:int, :pointer, :pointer], :int
|
62
|
-
attach_function :themis_secure_message_unwrap,
|
63
|
-
[:pointer, :int, :pointer, :int, :pointer,
|
64
|
-
:int, :pointer, :pointer], :int
|
65
|
-
|
66
|
-
attach_function :themis_gen_rsa_key_pair,
|
67
|
-
[:pointer, :pointer, :pointer, :pointer], :int
|
68
|
-
attach_function :themis_gen_ec_key_pair,
|
69
|
-
[:pointer, :pointer, :pointer, :pointer], :int
|
70
|
-
attach_function :themis_version, [], :string
|
71
|
-
|
72
|
-
attach_function :themis_secure_cell_encrypt_seal,
|
73
|
-
[:pointer, :int, :pointer, :int, :pointer, :int,
|
74
|
-
:pointer, :pointer], :int
|
75
|
-
attach_function :themis_secure_cell_decrypt_seal,
|
76
|
-
[:pointer, :int, :pointer, :int, :pointer, :int,
|
77
|
-
:pointer, :pointer], :int
|
78
|
-
|
79
|
-
attach_function :themis_secure_cell_encrypt_token_protect,
|
80
|
-
[:pointer, :int, :pointer, :int, :pointer, :int,
|
81
|
-
:pointer, :pointer, :pointer, :pointer], :int
|
82
|
-
attach_function :themis_secure_cell_decrypt_token_protect,
|
83
|
-
[:pointer, :int, :pointer, :int, :pointer, :int,
|
84
|
-
:pointer, :int, :pointer, :pointer], :int
|
85
|
-
|
86
|
-
attach_function :themis_secure_cell_encrypt_context_imprint,
|
87
|
-
[:pointer, :int, :pointer, :int, :pointer, :int,
|
88
|
-
:pointer, :pointer], :int
|
89
|
-
attach_function :themis_secure_cell_decrypt_context_imprint,
|
90
|
-
[:pointer, :int, :pointer, :int, :pointer, :int,
|
91
|
-
:pointer, :pointer], :int
|
92
|
-
|
93
|
-
begin
|
94
|
-
attach_function :secure_comparator_create, [], :pointer
|
95
|
-
attach_function :secure_comparator_destroy, [:pointer], :int
|
96
|
-
attach_function :secure_comparator_append_secret,
|
97
|
-
[:pointer, :pointer, :int], :int
|
98
|
-
attach_function :secure_comparator_begin_compare,
|
99
|
-
[:pointer, :pointer, :pointer], :int
|
100
|
-
attach_function :secure_comparator_proceed_compare,
|
101
|
-
[:pointer, :pointer, :int, :pointer, :pointer], :int
|
102
|
-
attach_function :secure_comparator_get_result, [:pointer], :int
|
103
|
-
rescue FFI::NotFoundError
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
module Themis
|
108
|
-
extend ThemisCommon
|
109
|
-
extend ThemisImport
|
110
|
-
|
111
|
-
extend Gem::Deprecate
|
112
|
-
|
113
|
-
BUFFER_TOO_SMALL = 14
|
114
|
-
SUCCESS = 0
|
115
|
-
FAIL = 11
|
116
|
-
SEND_AS_IS = 1
|
117
|
-
|
118
|
-
ThemisError = Class.new(StandardError)
|
119
|
-
|
120
|
-
class Callbacks
|
121
|
-
def get_pub_key_by_id(id)
|
122
|
-
raise ThemisError, 'Callback is not implemented: get_pub_key_by_id'
|
123
|
-
end
|
124
|
-
|
125
|
-
def send(message)
|
126
|
-
raise ThemisError, 'Callback is not implemented: send'
|
127
|
-
end
|
128
|
-
|
129
|
-
def receive
|
130
|
-
raise ThemisError, 'Callback is not implemented: receive'
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
class SKeyPairGen
|
135
|
-
include ThemisCommon
|
136
|
-
include ThemisImport
|
137
|
-
|
138
|
-
def ec
|
139
|
-
private_key_length = FFI::MemoryPointer.new(:uint)
|
140
|
-
public_key_length = FFI::MemoryPointer.new(:uint)
|
141
|
-
|
142
|
-
res = themis_gen_ec_key_pair(
|
143
|
-
nil, private_key_length, nil, public_key_length)
|
144
|
-
if res != BUFFER_TOO_SMALL
|
145
|
-
raise ThemisError, "Themis failed generating EC KeyPair: #{res}"
|
146
|
-
end
|
147
|
-
|
148
|
-
private_key = FFI::MemoryPointer.new(:char, private_key_length.read_uint)
|
149
|
-
public_key = FFI::MemoryPointer.new(:char, public_key_length.read_uint)
|
150
|
-
|
151
|
-
res = themis_gen_ec_key_pair(
|
152
|
-
private_key, private_key_length, public_key, public_key_length)
|
153
|
-
if res != SUCCESS
|
154
|
-
raise ThemisError, "Themis failed generating EC KeyPair: #{res}"
|
155
|
-
end
|
156
|
-
|
157
|
-
[private_key.get_bytes(0, private_key_length.read_uint),
|
158
|
-
public_key.get_bytes(0, public_key_length.read_uint)]
|
159
|
-
end
|
160
|
-
|
161
|
-
def rsa
|
162
|
-
private_key_length = FFI::MemoryPointer.new(:uint)
|
163
|
-
public_key_length = FFI::MemoryPointer.new(:uint)
|
164
|
-
|
165
|
-
res = themis_gen_rsa_key_pair(
|
166
|
-
nil, private_key_length, nil, public_key_length)
|
167
|
-
if res != BUFFER_TOO_SMALL
|
168
|
-
raise ThemisError, "Themis failed generating RSA KeyPair: #{res}"
|
169
|
-
end
|
170
|
-
|
171
|
-
private_key = FFI::MemoryPointer.new(:char, private_key_length.read_uint)
|
172
|
-
public_key = FFI::MemoryPointer.new(:char, public_key_length.read_uint)
|
173
|
-
|
174
|
-
res = themis_gen_rsa_key_pair(
|
175
|
-
private_key, private_key_length, public_key, public_key_length)
|
176
|
-
if res != SUCCESS
|
177
|
-
raise ThemisError, "Themis failed generating RSA KeyPair: #{res}"
|
178
|
-
end
|
179
|
-
|
180
|
-
[private_key.get_bytes(0, private_key_length.read_uint),
|
181
|
-
public_key.get_bytes(0, public_key_length.read_uint)]
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
class Ssession
|
186
|
-
include ThemisCommon
|
187
|
-
include ThemisImport
|
188
|
-
|
189
|
-
extend Gem::Deprecate
|
190
|
-
|
191
|
-
MAPPING = {}
|
192
|
-
|
193
|
-
GetPubKeyByIDCallback =
|
194
|
-
FFI::Function.new(:int, [:pointer, :int, :pointer, :int, :pointer]) do |id_buf, id_length, pubkey_buf, pubkey_length, obj|
|
195
|
-
pub_key = MAPPING[obj.read_uint64].get_pub_key_by_id(
|
196
|
-
id_buf.get_bytes(0, id_length))
|
197
|
-
return -1 unless pub_key
|
198
|
-
pubkey_buf.put_bytes(0, pub_key)
|
199
|
-
0
|
200
|
-
end
|
201
|
-
|
202
|
-
def initialize(id, private_key, transport)
|
203
|
-
id_buf, id_length = string_to_pointer_size(id)
|
204
|
-
private_key_buf, private_key_length = string_to_pointer_size(private_key)
|
205
|
-
|
206
|
-
@callbacks = CallbacksStruct.new
|
207
|
-
@callbacks[:get_pub_key_for_id] = GetPubKeyByIDCallback
|
208
|
-
|
209
|
-
MAPPING[transport.object_id] = transport
|
210
|
-
@transport_obj_id = transport.object_id
|
211
|
-
|
212
|
-
@callbacks[:user_data] = FFI::MemoryPointer.new(:uint64)
|
213
|
-
@callbacks[:user_data].write_uint64(@transport_obj_id)
|
214
|
-
|
215
|
-
@session = secure_session_create(
|
216
|
-
id_buf, id_length, private_key_buf, private_key_length, @callbacks)
|
217
|
-
|
218
|
-
raise ThemisError, 'Secure Session failed creating' unless @session
|
219
|
-
end
|
220
|
-
|
221
|
-
def is_established
|
222
|
-
established?
|
223
|
-
end
|
224
|
-
deprecate(:is_established, :established?, 2018, 6)
|
225
|
-
|
226
|
-
def established?
|
227
|
-
secure_session_is_established @session
|
228
|
-
end
|
229
|
-
|
230
|
-
def connect_request
|
231
|
-
connect_request_length = FFI::MemoryPointer.new(:uint)
|
232
|
-
res = secure_session_generate_connect_request(
|
233
|
-
@session, nil, connect_request_length)
|
234
|
-
if res != BUFFER_TOO_SMALL
|
235
|
-
raise(ThemisError,
|
236
|
-
"Secure Session failed making connection request: #{res}")
|
237
|
-
end
|
238
|
-
connect_request = FFI::MemoryPointer.new(
|
239
|
-
:char, connect_request_length.read_uint)
|
240
|
-
res = secure_session_generate_connect_request(
|
241
|
-
@session, connect_request, connect_request_length)
|
242
|
-
if res != SUCCESS
|
243
|
-
raise(ThemisError,
|
244
|
-
"Secure Session failed making connection request: #{res}")
|
245
|
-
end
|
246
|
-
connect_request.get_bytes(0, connect_request_length.read_uint)
|
247
|
-
end
|
248
|
-
|
249
|
-
def unwrap(message)
|
250
|
-
message_, message_length_ = string_to_pointer_size message
|
251
|
-
unwrapped_message_length = FFI::MemoryPointer.new(:uint)
|
252
|
-
|
253
|
-
res = secure_session_unwrap(
|
254
|
-
@session, message_, message_length_, nil, unwrapped_message_length)
|
255
|
-
return SUCCESS, '' if res == SUCCESS
|
256
|
-
if res != BUFFER_TOO_SMALL
|
257
|
-
raise ThemisError, "Secure Session failed decrypting: #{res}"
|
258
|
-
end
|
259
|
-
|
260
|
-
unwrapped_message = FFI::MemoryPointer.new(
|
261
|
-
:char, unwrapped_message_length.read_uint)
|
262
|
-
res = secure_session_unwrap(@session, message_, message_length_,
|
263
|
-
unwrapped_message, unwrapped_message_length)
|
264
|
-
if res != SUCCESS && res != SEND_AS_IS
|
265
|
-
raise ThemisError, "Secure Session failed decrypting: #{res}"
|
266
|
-
end
|
267
|
-
|
268
|
-
[res, unwrapped_message.get_bytes(0, unwrapped_message_length.read_uint)]
|
269
|
-
end
|
270
|
-
|
271
|
-
def wrap(message)
|
272
|
-
message_, message_length_ = string_to_pointer_size(message)
|
273
|
-
|
274
|
-
wrapped_message_length = FFI::MemoryPointer.new(:uint)
|
275
|
-
res = secure_session_wrap(
|
276
|
-
@session, message_, message_length_, nil, wrapped_message_length)
|
277
|
-
if res != BUFFER_TOO_SMALL
|
278
|
-
raise ThemisError, "Secure Session failed encrypting: #{res}"
|
279
|
-
end
|
280
|
-
|
281
|
-
wrapped_message = FFI::MemoryPointer.new(
|
282
|
-
:char, wrapped_message_length.read_uint)
|
283
|
-
res = secure_session_wrap(@session, message_, message_length_,
|
284
|
-
wrapped_message, wrapped_message_length)
|
285
|
-
if res != SUCCESS && res != SEND_AS_IS
|
286
|
-
raise ThemisError, "Secure Session failed encrypting: #{res}"
|
287
|
-
end
|
288
|
-
|
289
|
-
wrapped_message.get_bytes(0, wrapped_message_length.read_uint)
|
290
|
-
end
|
291
|
-
|
292
|
-
def finalize
|
293
|
-
res = secure_session_destroy(@session)
|
294
|
-
raise ThemisError, 'Secure Session failed destroying' if res != SUCCESS
|
295
|
-
end
|
296
|
-
end
|
297
|
-
|
298
|
-
class Smessage
|
299
|
-
include ThemisCommon
|
300
|
-
include ThemisImport
|
301
|
-
|
302
|
-
def initialize(private_key, peer_public_key)
|
303
|
-
@private_key, @private_key_length = string_to_pointer_size(private_key)
|
304
|
-
@peer_public_key, @peer_public_key_length =
|
305
|
-
string_to_pointer_size(peer_public_key)
|
306
|
-
end
|
307
|
-
|
308
|
-
def wrap(message)
|
309
|
-
message_, message_length_ = string_to_pointer_size(message)
|
310
|
-
|
311
|
-
wrapped_message_length = FFI::MemoryPointer.new(:uint)
|
312
|
-
res = themis_secure_message_wrap(
|
313
|
-
@private_key, @private_key_length, @peer_public_key,
|
314
|
-
@peer_public_key_length, message_, message_length_,
|
315
|
-
nil, wrapped_message_length)
|
316
|
-
if res != BUFFER_TOO_SMALL
|
317
|
-
raise ThemisError, "Secure Message failed encrypting: #{res}"
|
318
|
-
end
|
319
|
-
|
320
|
-
wrapped_message = FFI::MemoryPointer.new(
|
321
|
-
:char, wrapped_message_length.read_uint)
|
322
|
-
res = themis_secure_message_wrap(
|
323
|
-
@private_key, @private_key_length, @peer_public_key,
|
324
|
-
@peer_public_key_length, message_, message_length_,
|
325
|
-
wrapped_message, wrapped_message_length)
|
326
|
-
if res != SUCCESS
|
327
|
-
raise ThemisError, "Secure Message failed encrypting: #{res}"
|
328
|
-
end
|
329
|
-
|
330
|
-
wrapped_message.get_bytes(0, wrapped_message_length.read_uint)
|
331
|
-
end
|
332
|
-
|
333
|
-
def unwrap(message)
|
334
|
-
message_, message_length_ = string_to_pointer_size(message)
|
335
|
-
unwrapped_message_length = FFI::MemoryPointer.new(:uint)
|
336
|
-
res = themis_secure_message_unwrap(
|
337
|
-
@private_key, @private_key_length, @peer_public_key,
|
338
|
-
@peer_public_key_length, message_, message_length_,
|
339
|
-
nil, unwrapped_message_length)
|
340
|
-
if res != BUFFER_TOO_SMALL
|
341
|
-
raise ThemisError, "Secure Message failed decrypting: #{res}"
|
342
|
-
end
|
343
|
-
|
344
|
-
unwrapped_message = FFI::MemoryPointer.new(
|
345
|
-
:char, unwrapped_message_length.read_uint)
|
346
|
-
res = themis_secure_message_unwrap(
|
347
|
-
@private_key, @private_key_length, @peer_public_key,
|
348
|
-
@peer_public_key_length, message_, message_length_,
|
349
|
-
unwrapped_message, unwrapped_message_length)
|
350
|
-
if res != SUCCESS
|
351
|
-
raise ThemisError, "Secure Message failed decrypting: #{res}"
|
352
|
-
end
|
353
|
-
|
354
|
-
unwrapped_message.get_bytes(0, unwrapped_message_length.read_uint)
|
355
|
-
end
|
356
|
-
end
|
357
|
-
|
358
|
-
def Ssign(*args)
|
359
|
-
s_sign(*args)
|
360
|
-
end
|
361
|
-
deprecate :Ssign, :s_sign, 2018, 6
|
362
|
-
|
363
|
-
def s_sign(private_key, message)
|
364
|
-
private_key_, private_key_length_ = string_to_pointer_size(private_key)
|
365
|
-
message_, message_length_ = string_to_pointer_size(message)
|
366
|
-
|
367
|
-
wrapped_message_length = FFI::MemoryPointer.new(:uint)
|
368
|
-
res = themis_secure_message_wrap(
|
369
|
-
private_key_, private_key_length_, nil, 0, message_,
|
370
|
-
message_length_, nil, wrapped_message_length)
|
371
|
-
if res != BUFFER_TOO_SMALL
|
372
|
-
raise ThemisError, "Secure Message failed singing: #{res}"
|
373
|
-
end
|
374
|
-
|
375
|
-
wrapped_message = FFI::MemoryPointer.new(
|
376
|
-
:char, wrapped_message_length.read_uint)
|
377
|
-
res = themis_secure_message_wrap(
|
378
|
-
private_key_, private_key_length_, nil, 0, message_,
|
379
|
-
message_length_, wrapped_message, wrapped_message_length)
|
380
|
-
if res != SUCCESS
|
381
|
-
raise ThemisError, "Secure Message failed singing: #{res}"
|
382
|
-
end
|
383
|
-
|
384
|
-
wrapped_message.get_bytes(0, wrapped_message_length.read_uint)
|
385
|
-
end
|
386
|
-
|
387
|
-
def Sverify(*args)
|
388
|
-
s_verify(*args)
|
389
|
-
end
|
390
|
-
deprecate :Sverify, :s_verify, 2018, 6
|
391
|
-
|
392
|
-
def s_verify(peer_public_key, message)
|
393
|
-
include ThemisCommon
|
394
|
-
include ThemisImport
|
395
|
-
|
396
|
-
public_key_, public_key_length_ = string_to_pointer_size(peer_public_key)
|
397
|
-
message_, message_length_ = string_to_pointer_size(message)
|
398
|
-
|
399
|
-
unwrapped_message_length = FFI::MemoryPointer.new(:uint)
|
400
|
-
res = themis_secure_message_unwrap(
|
401
|
-
nil, 0, public_key_, public_key_length_, message_,
|
402
|
-
message_length_, nil, unwrapped_message_length)
|
403
|
-
if res != BUFFER_TOO_SMALL
|
404
|
-
raise ThemisError, "Secure Message failed verifying: #{res}"
|
405
|
-
end
|
406
|
-
|
407
|
-
unwrapped_message = FFI::MemoryPointer.new(
|
408
|
-
:char, unwrapped_message_length.read_uint)
|
409
|
-
res = themis_secure_message_unwrap(
|
410
|
-
nil, 0, public_key_, public_key_length_, message_,
|
411
|
-
message_length_, unwrapped_message, unwrapped_message_length)
|
412
|
-
if res != SUCCESS
|
413
|
-
raise ThemisError, "Secure Message failed verifying: #{res}"
|
414
|
-
end
|
415
|
-
|
416
|
-
unwrapped_message.get_bytes(0, unwrapped_message_length.read_uint)
|
417
|
-
end
|
418
|
-
|
419
|
-
class Scell
|
420
|
-
include ThemisCommon
|
421
|
-
include ThemisImport
|
422
|
-
|
423
|
-
SEAL_MODE = 0
|
424
|
-
TOKEN_PROTECT_MODE = 1
|
425
|
-
CONTEXT_IMPRINT_MODE = 2
|
426
|
-
|
427
|
-
def initialize(key, mode)
|
428
|
-
@key, @key_length = string_to_pointer_size(key)
|
429
|
-
@mode = mode
|
430
|
-
end
|
431
|
-
|
432
|
-
def encrypt(message, context = nil)
|
433
|
-
message_, message_length_ = string_to_pointer_size(message)
|
434
|
-
context_, context_length_ =
|
435
|
-
context.nil? ? [nil, 0] : string_to_pointer_size(context)
|
436
|
-
encrypted_message_length = FFI::MemoryPointer.new(:uint)
|
437
|
-
enccontext_length = FFI::MemoryPointer.new(:uint)
|
438
|
-
case @mode
|
439
|
-
when SEAL_MODE
|
440
|
-
res = themis_secure_cell_encrypt_seal(
|
441
|
-
@key, @key_length, context_, context_length_, message_,
|
442
|
-
message_length_, nil, encrypted_message_length)
|
443
|
-
if res != BUFFER_TOO_SMALL
|
444
|
-
raise ThemisError, "Secure Cell (Seal) failed encrypting: #{res}"
|
445
|
-
end
|
446
|
-
encrypted_message = FFI::MemoryPointer.new(
|
447
|
-
:char, encrypted_message_length.read_uint)
|
448
|
-
res = themis_secure_cell_encrypt_seal(
|
449
|
-
@key, @key_length, context_, context_length_, message_,
|
450
|
-
message_length_, encrypted_message, encrypted_message_length)
|
451
|
-
if res != SUCCESS
|
452
|
-
raise ThemisError, "Secure Cell (Seal) failed encrypting: #{res}"
|
453
|
-
end
|
454
|
-
encrypted_message.get_bytes(0, encrypted_message_length.read_uint)
|
455
|
-
when TOKEN_PROTECT_MODE
|
456
|
-
res = themis_secure_cell_encrypt_token_protect(
|
457
|
-
@key, @key_length, context_, context_length_, message_,
|
458
|
-
message_length_, nil, enccontext_length, nil,
|
459
|
-
encrypted_message_length)
|
460
|
-
if res != BUFFER_TOO_SMALL
|
461
|
-
raise(ThemisError,
|
462
|
-
"Secure Cell (Token protect) failed encrypting: #{res}")
|
463
|
-
end
|
464
|
-
encrypted_message = FFI::MemoryPointer.new(
|
465
|
-
:char, encrypted_message_length.read_uint)
|
466
|
-
enccontext = FFI::MemoryPointer.new(:char, enccontext_length.read_uint)
|
467
|
-
res = themis_secure_cell_encrypt_token_protect(
|
468
|
-
@key, @key_length, context_, context_length_, message_,
|
469
|
-
message_length_, enccontext, enccontext_length, encrypted_message,
|
470
|
-
encrypted_message_length)
|
471
|
-
if res != SUCCESS
|
472
|
-
raise(ThemisError,
|
473
|
-
"Secure Cell (Token Protect) failed encrypting: #{res}")
|
474
|
-
end
|
475
|
-
[encrypted_message.get_bytes(0, encrypted_message_length.read_uint),
|
476
|
-
enccontext.get_bytes(0, enccontext_length.read_uint),]
|
477
|
-
when CONTEXT_IMPRINT_MODE
|
478
|
-
res = themis_secure_cell_encrypt_context_imprint(
|
479
|
-
@key, @key_length, message_, message_length_, context_,
|
480
|
-
context_length_, nil, encrypted_message_length)
|
481
|
-
if res != BUFFER_TOO_SMALL
|
482
|
-
raise(ThemisError,
|
483
|
-
"Secure Cell (Context Imprint) failed encrypting: #{res}")
|
484
|
-
end
|
485
|
-
encrypted_message = FFI::MemoryPointer.new(
|
486
|
-
:char, encrypted_message_length.read_uint)
|
487
|
-
res = themis_secure_cell_encrypt_context_imprint(
|
488
|
-
@key, @key_length, message_, message_length_, context_,
|
489
|
-
context_length_, encrypted_message, encrypted_message_length)
|
490
|
-
if res != SUCCESS
|
491
|
-
raise(ThemisError,
|
492
|
-
"Secure Cell (Context Imprint) failed encrypting: #{res}")
|
493
|
-
end
|
494
|
-
encrypted_message.get_bytes(0, encrypted_message_length.read_uint)
|
495
|
-
else
|
496
|
-
raise ThemisError, 'Secure Cell failed encrypting, undefined mode'
|
497
|
-
end
|
498
|
-
end
|
499
|
-
|
500
|
-
def decrypt(message, context = nil)
|
501
|
-
context_, context_length_ =
|
502
|
-
context.nil? ? [nil, 0] : string_to_pointer_size(context)
|
503
|
-
decrypted_message_length = FFI::MemoryPointer.new(:uint)
|
504
|
-
case @mode
|
505
|
-
when SEAL_MODE
|
506
|
-
message_, message_length_ = string_to_pointer_size(message)
|
507
|
-
res = themis_secure_cell_decrypt_seal(
|
508
|
-
@key, @key_length, context_, context_length_, message_,
|
509
|
-
message_length_, nil, decrypted_message_length)
|
510
|
-
if res != BUFFER_TOO_SMALL
|
511
|
-
raise ThemisError, "Secure Cell (Seal) failed decrypting: #{res}"
|
512
|
-
end
|
513
|
-
decrypted_message = FFI::MemoryPointer.new(
|
514
|
-
:char, decrypted_message_length.read_uint)
|
515
|
-
res = themis_secure_cell_decrypt_seal(
|
516
|
-
@key, @key_length, context_, context_length_, message_,
|
517
|
-
message_length_, decrypted_message, decrypted_message_length)
|
518
|
-
if res != SUCCESS
|
519
|
-
raise ThemisError, "Secure Cell (Seal) failed decrypting: #{res}"
|
520
|
-
end
|
521
|
-
decrypted_message.get_bytes(0, decrypted_message_length.read_uint)
|
522
|
-
when TOKEN_PROTECT_MODE
|
523
|
-
message_, enccontext = message
|
524
|
-
message__, message_length__ = string_to_pointer_size(message_)
|
525
|
-
enccontext_, enccontext_length = string_to_pointer_size(enccontext)
|
526
|
-
res = themis_secure_cell_decrypt_token_protect(
|
527
|
-
@key, @key_length, context_, context_length_, message__,
|
528
|
-
message_length__, enccontext_, enccontext_length, nil,
|
529
|
-
decrypted_message_length)
|
530
|
-
if res != BUFFER_TOO_SMALL
|
531
|
-
raise(ThemisError,
|
532
|
-
"Secure Cell (Token Protect) failed decrypting: #{res}")
|
533
|
-
end
|
534
|
-
decrypted_message = FFI::MemoryPointer.new(
|
535
|
-
:char, decrypted_message_length.read_uint)
|
536
|
-
res = themis_secure_cell_decrypt_token_protect(
|
537
|
-
@key, @key_length, context_, context_length_, message__,
|
538
|
-
message_length__, enccontext_, enccontext_length,
|
539
|
-
decrypted_message, decrypted_message_length)
|
540
|
-
if res != SUCCESS
|
541
|
-
raise(ThemisError,
|
542
|
-
"Secure Cell (Token Protect) failed decrypting: #{res}")
|
543
|
-
end
|
544
|
-
decrypted_message.get_bytes(0, decrypted_message_length.read_uint)
|
545
|
-
when CONTEXT_IMPRINT_MODE
|
546
|
-
message_, message_length_ = string_to_pointer_size(message)
|
547
|
-
res = themis_secure_cell_decrypt_context_imprint(
|
548
|
-
@key, @key_length, message_, message_length_, context_,
|
549
|
-
context_length_, nil, decrypted_message_length)
|
550
|
-
if res != BUFFER_TOO_SMALL
|
551
|
-
raise(ThemisError,
|
552
|
-
"Secure Cell (Context Imprint) failed decrypting: #{res}")
|
553
|
-
end
|
554
|
-
decrypted_message = FFI::MemoryPointer.new(
|
555
|
-
:char, decrypted_message_length.read_uint)
|
556
|
-
res = themis_secure_cell_decrypt_context_imprint(@key, @key_length,
|
557
|
-
message_, message_length_, context_, context_length_,
|
558
|
-
decrypted_message, decrypted_message_length)
|
559
|
-
if res != SUCCESS
|
560
|
-
raise(ThemisError,
|
561
|
-
"Secure Cell (Context Imprint) failed decrypting: #{res}")
|
562
|
-
end
|
563
|
-
decrypted_message.get_bytes(0, decrypted_message_length.read_uint)
|
564
|
-
else
|
565
|
-
raise ThemisError, 'Secure Cell failed encrypting, undefined mode'
|
566
|
-
end
|
567
|
-
end
|
568
|
-
end
|
569
|
-
|
570
|
-
class Scomparator
|
571
|
-
include ThemisCommon
|
572
|
-
include ThemisImport
|
573
|
-
|
574
|
-
MATCH = 21
|
575
|
-
NOT_MATCH = 22
|
576
|
-
NOT_READY = 0
|
577
|
-
|
578
|
-
def initialize(shared_secret)
|
579
|
-
shared_secret_buf, shared_secret_length =
|
580
|
-
string_to_pointer_size(shared_secret)
|
581
|
-
@comparator = secure_comparator_create
|
582
|
-
raise ThemisError, 'Secure Comparator failed creating' unless @comparator
|
583
|
-
res = secure_comparator_append_secret(
|
584
|
-
@comparator, shared_secret_buf, shared_secret_length)
|
585
|
-
if res != SUCCESS
|
586
|
-
raise ThemisError, 'Secure Comparator failed appending secret'
|
587
|
-
end
|
588
|
-
end
|
589
|
-
|
590
|
-
def finalize
|
591
|
-
res = secure_comparator_destroy(@comparator)
|
592
|
-
if res != SUCCESS
|
593
|
-
raise ThemisError, 'Secure Comparator failed destroying'
|
594
|
-
end
|
595
|
-
end
|
596
|
-
|
597
|
-
def begin_compare
|
598
|
-
res_length = FFI::MemoryPointer.new(:uint)
|
599
|
-
res = secure_comparator_begin_compare(@comparator, nil, res_length)
|
600
|
-
if res != BUFFER_TOO_SMALL
|
601
|
-
raise(ThemisError,
|
602
|
-
'Secure Comparator failed making initialisation message')
|
603
|
-
end
|
604
|
-
|
605
|
-
res_buffer = FFI::MemoryPointer.new(:char, res_length.read_uint)
|
606
|
-
res = secure_comparator_begin_compare(@comparator, res_buffer, res_length)
|
607
|
-
if res != SUCCESS && res != SEND_AS_IS
|
608
|
-
raise(ThemisError,
|
609
|
-
'Secure Comparator failed making initialisation message')
|
610
|
-
end
|
611
|
-
|
612
|
-
res_buffer.get_bytes(0, res_length.read_uint)
|
613
|
-
end
|
614
|
-
|
615
|
-
def proceed_compare(control_message)
|
616
|
-
message, message_length = string_to_pointer_size(control_message)
|
617
|
-
res_length = FFI::MemoryPointer.new(:uint)
|
618
|
-
|
619
|
-
res = secure_comparator_proceed_compare(
|
620
|
-
@comparator, message, message_length, nil, res_length)
|
621
|
-
return '' if res == SUCCESS
|
622
|
-
if res != BUFFER_TOO_SMALL
|
623
|
-
raise ThemisError, 'Secure Comparator failed proceeding message'
|
624
|
-
end
|
625
|
-
|
626
|
-
res_buffer = FFI::MemoryPointer.new(:char, res_length.read_uint)
|
627
|
-
res = secure_comparator_proceed_compare(
|
628
|
-
@comparator, message, message_length, res_buffer, res_length)
|
629
|
-
if res != SUCCESS && res != SEND_AS_IS
|
630
|
-
raise ThemisError, 'Secure Comparator failed proceeding message'
|
631
|
-
end
|
632
|
-
|
633
|
-
res_buffer.get_bytes(0, res_length.read_uint)
|
634
|
-
end
|
635
|
-
|
636
|
-
def result
|
637
|
-
secure_comparator_get_result(@comparator)
|
638
|
-
end
|
639
|
-
end
|
640
|
-
|
641
|
-
module_function :Ssign
|
642
|
-
module_function :Sverify
|
643
|
-
module_function :s_sign
|
644
|
-
module_function :s_verify
|
645
|
-
end
|