Class: Themis::Scell

Inherits:
Object
  • Object
show all
Includes:
ThemisCommon, ThemisImport
Defined in:
lib/rubythemis.rb

Constant Summary collapse

SEAL_MODE =
0
TOKEN_PROTECT_MODE =
1
CONTEXT_IMPRINT_MODE =
2

Instance Method Summary collapse

Methods included from ThemisCommon

string_to_pointer_size

Constructor Details

#initialize(key, mode) ⇒ Scell

Returns a new instance of Scell.



274
275
276
277
# File 'lib/rubythemis.rb', line 274

def initialize(key, mode)
    @key, @key_length = string_to_pointer_size(key)
    @mode = mode
end

Instance Method Details

#decrypt(message, context = nil) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/rubythemis.rb', line 312

def decrypt(message, context=nil)
    context_, context_length_ = context.nil? ? [nil,0] : string_to_pointer_size(context)
    decrypted_message_length=FFI::MemoryPointer.new(:uint)
    case @mode
    when SEAL_MODE
  message_, message_length_ = string_to_pointer_size(message)
  res=themis_secure_cell_decrypt_seal(@key, @key_length, context_, context_length_, message_, message_length_, nil, decrypted_message_length)
  raise ThemisError, "Secure Cell (Seal) failed decrypting: #{res}" unless res == BUFFER_TOO_SMALL
  decrypted_message = FFI::MemoryPointer.new(:char, decrypted_message_length.read_uint)
  res=themis_secure_cell_decrypt_seal(@key, @key_length, context_, context_length_, message_, message_length_, decrypted_message, decrypted_message_length)
  raise ThemisError, "Secure Cell (Seal) failed decrypting: #{res}" unless res == SUCCESS
  return decrypted_message.get_bytes(0, decrypted_message_length.read_uint)
    when TOKEN_PROTECT_MODE
  enccontext, message_ = message
  message__, message_length__ = string_to_pointer_size(message_)
  enccontext_, enccontext_length = string_to_pointer_size(enccontext)
  res=themis_secure_cell_decrypt_token_protect(@key, @key_length, context_, context_length_, message__, message_length__, enccontext_, enccontext_length, nil, decrypted_message_length)
  raise ThemisError, "Secure Cell (Token Protect) failed decrypting: #{res}" unless res == BUFFER_TOO_SMALL
  decrypted_message = FFI::MemoryPointer.new(:char, decrypted_message_length.read_uint)
  res=themis_secure_cell_decrypt_token_protect(@key, @key_length, context_, context_length_, message__, message_length__, enccontext_, enccontext_length, decrypted_message, decrypted_message_length)
  raise ThemisError, "Secure Cell (Token Protect) failed decrypting: #{res}" unless res == SUCCESS
  return  decrypted_message.get_bytes(0, decrypted_message_length.read_uint)
    when CONTEXT_IMPRINT_MODE
  message_, message_length_ = string_to_pointer_size(message)
  res=themis_secure_cell_decrypt_context_imprint(@key, @key_length, message_, message_length_, context_, context_length_, nil, decrypted_message_length)
  raise ThemisError, "Secure Cell (Context Imprint) failed decrypting: #{res}" unless res == BUFFER_TOO_SMALL
  decrypted_message = FFI::MemoryPointer.new(:char, decrypted_message_length.read_uint)
  res=themis_secure_cell_decrypt_context_imprint(@key, @key_length, message_, message_length_, context_, context_length_, decrypted_message, decrypted_message_length)
  raise ThemisError, "Secure Cell (Context Imprint) failed decrypting: #{res}" unless res == SUCCESS
  return decrypted_message.get_bytes(0, decrypted_message_length.read_uint)
    else
  raise ThemisError, "Secure Cell failed encrypting, undefined mode"
    end
end

#encrypt(message, context = nil) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/rubythemis.rb', line 279

def encrypt(message, context=nil)
    message_, message_length_ = string_to_pointer_size(message)
    context_, context_length_ = context.nil? ? [nil,0] : string_to_pointer_size(context)
    encrypted_message_length=FFI::MemoryPointer.new(:uint)
    enccontext_length=FFI::MemoryPointer.new(:uint)
    case @mode
    when SEAL_MODE
  res=themis_secure_cell_encrypt_seal(@key, @key_length, context_, context_length_, message_, message_length_, nil, encrypted_message_length)
  raise ThemisError, "Secure Cell (Seal) failed encrypting: #{res}" unless res == BUFFER_TOO_SMALL
  encrypted_message = FFI::MemoryPointer.new(:char, encrypted_message_length.read_uint)
  res=themis_secure_cell_encrypt_seal(@key, @key_length, context_, context_length_, message_, message_length_, encrypted_message, encrypted_message_length)
  raise ThemisError, "Secure Cell (Seal) failed encrypting: #{res}" unless res == SUCCESS
  return encrypted_message.get_bytes(0, encrypted_message_length.read_uint)
    when TOKEN_PROTECT_MODE
  res=themis_secure_cell_encrypt_token_protect(@key, @key_length, context_, context_length_, message_, message_length_, nil, enccontext_length, nil, encrypted_message_length)
  raise ThemisError, "Secure Cell (Token protect) failed encrypting: #{res}" unless res == BUFFER_TOO_SMALL
  encrypted_message = FFI::MemoryPointer.new(:char, encrypted_message_length.read_uint)
  enccontext = FFI::MemoryPointer.new(:char, enccontext_length.read_uint)
  res=themis_secure_cell_encrypt_token_protect(@key, @key_length, context_, context_length_, message_, message_length_, enccontext, enccontext_length, encrypted_message, encrypted_message_length)
  raise ThemisError, "Secure Cell (Token Protect) failed encrypting: #{res}" unless res == SUCCESS
  return enccontext.get_bytes(0, enccontext_length.read_uint), encrypted_message.get_bytes(0, encrypted_message_length.read_uint)
    when CONTEXT_IMPRINT_MODE
  res=themis_secure_cell_encrypt_context_imprint(@key, @key_length, message_, message_length_, context_, context_length_, nil, encrypted_message_length)
  raise ThemisError, "Secure Cell (Context Imprint) failed encrypting: #{res}" unless res == BUFFER_TOO_SMALL
  encrypted_message = FFI::MemoryPointer.new(:char, encrypted_message_length.read_uint)
  res=themis_secure_cell_encrypt_context_imprint(@key, @key_length, message_, message_length_, context_, context_length_, encrypted_message, encrypted_message_length)
  raise ThemisError, "Secure Cell (Context Imprint) failed encrypting: #{res}" unless res == SUCCESS
  return encrypted_message.get_bytes(0, encrypted_message_length.read_uint)
    else
  raise ThemisError, "Secure Cell failed encrypting, undefined mode"
    end
end