Class: Themis::ScellContextImprint

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

Overview

Secure Cell in Context Imprint mode.

Constant Summary

Constants included from ThemisImport

ThemisImport::THEMIS_KEY_EC_PRIVATE, ThemisImport::THEMIS_KEY_EC_PUBLIC, ThemisImport::THEMIS_KEY_INVALID, ThemisImport::THEMIS_KEY_RSA_PRIVATE, ThemisImport::THEMIS_KEY_RSA_PUBLIC

Constants inherited from Scell

Themis::Scell::CONTEXT_IMPRINT_MODE, Themis::Scell::SEAL_MODE, Themis::Scell::TOKEN_PROTECT_MODE

Instance Method Summary collapse

Methods included from ThemisImport

canonical_themis_paths, load_themis

Methods included from ThemisCommon

empty?, string_to_pointer_size

Constructor Details

#initialize(key) ⇒ ScellContextImprint

Make a new Secure Cell with given key. The key must not be empty and is treated as binary data. You can use Themis::gen_sym_key to generate new keys.



877
878
879
880
881
882
# File 'lib/rbthemis.rb', line 877

def initialize(key)
  if empty? key
    raise ThemisError, "key cannot be empty"
  end
  @key, @key_length = string_to_pointer_size(key)
end

Instance Method Details

#decrypt(message, context) ⇒ Object

Decrypts message with given context. The context must be the same as the one used during encryption. Since Context Imprint mode does not include authentication data, integrity of the resulting message is not guaranteed. You need to verify it via some other means. Decrypted message is returned as binary data.



927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
# File 'lib/rbthemis.rb', line 927

def decrypt(message, context)
  if empty? message
    raise ThemisError, "message cannot be empty"
  end
  if empty? context
    raise ThemisError, "message cannot be empty"
  end

  message_, message_length_ = string_to_pointer_size(message)
  context_, context_length_ =
    context.nil? ? [nil, 0] : string_to_pointer_size(context)

  decrypted_length = FFI::MemoryPointer.new(:uint)
  res = themis_secure_cell_decrypt_context_imprint(
    @key, @key_length, message_, message_length_,
    context_, context_length_, nil, decrypted_length)
  if res != BUFFER_TOO_SMALL
    raise ThemisError.new(res), "decrypt failed"
  end

  decrypted_message = FFI::MemoryPointer.new(:char, decrypted_length.read_uint)
  res = themis_secure_cell_decrypt_context_imprint(
    @key, @key_length, message_, message_length_,
    context_, context_length_, decrypted_message, decrypted_length)
  if res != SUCCESS
    raise ThemisError.new(res), "decrypt failed"
  end

  decrypted_message.get_bytes(0, decrypted_length.read_uint)
end

#encrypt(message, context) ⇒ Object

Encrypts message with given context. The context is cryptographically combined with message but is not included into encrypted data, you will need to provide the same context for decryption. Resulting encrypted message has the same length as input and does not include authentication data, so its integrity cannot be verified. Message and context must not be empty, both are treated as binary data.



890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
# File 'lib/rbthemis.rb', line 890

def encrypt(message, context)
  if empty? message
    raise ThemisError, "message cannot be empty"
  end
  if empty? context
    raise ThemisError, "context cannot be empty"
  end

  message_, message_length_ = string_to_pointer_size(message)
  context_, context_length_ =
    context.nil? ? [nil, 0] : string_to_pointer_size(context)

  encrypted_length = FFI::MemoryPointer.new(:uint)
  res = themis_secure_cell_encrypt_context_imprint(
    @key, @key_length, message_, message_length_,
    context_, context_length_, nil, encrypted_length)
  if res != BUFFER_TOO_SMALL
    raise ThemisError.new(res), "encrypt failed"
  end

  encrypted_message = FFI::MemoryPointer.new(:char, encrypted_length.read_uint)
  res = themis_secure_cell_encrypt_context_imprint(
    @key, @key_length, message_, message_length_,
    context_, context_length_, encrypted_message, encrypted_length)
  if res != SUCCESS
    raise ThemisError.new(res), "encrypt failed"
  end

  encrypted_message.get_bytes(0, encrypted_length.read_uint)
end