Module: Themis

Extended by:
Gem::Deprecate, ThemisCommon, ThemisImport
Defined in:
lib/rbthemis.rb

Defined Under Namespace

Classes: Callbacks, SKeyPairGen, Scell, ScellContextImprint, ScellSeal, ScellSealPassphrase, ScellTokenProtect, Scomparator, Smessage, Ssession, ThemisError

Constant Summary collapse

BUFFER_TOO_SMALL =
14
SUCCESS =
0
FAIL =
11
INVALID_ARGUMENT =
12
SEND_AS_IS =
1

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

Class Method Summary collapse

Methods included from ThemisCommon

empty?, string_to_pointer_size

Methods included from ThemisImport

canonical_themis_paths, load_themis

Class Method Details

.gen_sym_keyObject



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/rbthemis.rb', line 545

def gen_sym_key
  key_length = FFI::MemoryPointer.new(:uint)

  res = themis_gen_sym_key(nil, key_length)
  if res != BUFFER_TOO_SMALL
    raise ThemisError, "failed to get symmetric key size: #{res}"
  end

  key = FFI::MemoryPointer.new(:char, key_length.read_uint)

  res = themis_gen_sym_key(key, key_length)
  if res != SUCCESS
    raise ThemisError, "failed to generate symmetric key: #{res}"
  end

  return key.get_bytes(0, key_length.read_uint)
end

.private_key(key) ⇒ Object



273
274
275
276
277
278
# File 'lib/rbthemis.rb', line 273

def Themis.private_key(key)
  key_, len_ = string_to_pointer_size(key)
  kind = themis_get_asym_key_kind(key_, len_)
  return kind == ThemisImport::THEMIS_KEY_RSA_PRIVATE \
      || kind == ThemisImport::THEMIS_KEY_EC_PRIVATE
end

.public_key(key) ⇒ Object



280
281
282
283
284
285
# File 'lib/rbthemis.rb', line 280

def Themis.public_key(key)
  key_, len_ = string_to_pointer_size(key)
  kind = themis_get_asym_key_kind(key_, len_)
  return kind == ThemisImport::THEMIS_KEY_RSA_PUBLIC \
      || kind == ThemisImport::THEMIS_KEY_EC_PUBLIC
end

.s_sign(private_key, message) ⇒ Object



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# File 'lib/rbthemis.rb', line 478

def s_sign(private_key, message)
  if not valid_key(private_key)
    raise ThemisError, "Secure Message: invalid private key"
  end
  if not private_key(private_key)
    raise ThemisError, "Secure Message: public key used instead of private"
  end

  private_key_, private_key_length_ = string_to_pointer_size(private_key)
  message_, message_length_ = string_to_pointer_size(message)

  wrapped_message_length = FFI::MemoryPointer.new(:uint)
  res = themis_secure_message_sign(
    private_key_, private_key_length_, message_,
    message_length_, nil, wrapped_message_length)
  if res != BUFFER_TOO_SMALL
    raise ThemisError, "Secure Message failed to sign: #{res}"
  end

  wrapped_message = FFI::MemoryPointer.new(
    :char, wrapped_message_length.read_uint)
  res = themis_secure_message_sign(
    private_key_, private_key_length_, message_,
    message_length_, wrapped_message, wrapped_message_length)
  if res != SUCCESS
    raise ThemisError, "Secure Message failed to sign: #{res}"
  end

  wrapped_message.get_bytes(0, wrapped_message_length.read_uint)
end

.s_verify(peer_public_key, message) ⇒ Object



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/rbthemis.rb', line 514

def s_verify(peer_public_key, message)
  if not valid_key(peer_public_key)
    raise ThemisError, "Secure Message: invalid public key"
  end
  if not public_key(peer_public_key)
    raise ThemisError, "Secure Message: private key used instead of public"
  end

  public_key_, public_key_length_ = string_to_pointer_size(peer_public_key)
  message_, message_length_ = string_to_pointer_size(message)

  unwrapped_message_length = FFI::MemoryPointer.new(:uint)
  res = themis_secure_message_verify(
    public_key_, public_key_length_, message_,
    message_length_, nil, unwrapped_message_length)
  if res != BUFFER_TOO_SMALL
    raise ThemisError, "Secure Message failed to verify: #{res}"
  end

  unwrapped_message = FFI::MemoryPointer.new(
    :char, unwrapped_message_length.read_uint)
  res = themis_secure_message_verify(
    public_key_, public_key_length_, message_,
    message_length_, unwrapped_message, unwrapped_message_length)
  if res != SUCCESS
    raise ThemisError, "Secure Message failed to verify: #{res}"
  end

  unwrapped_message.get_bytes(0, unwrapped_message_length.read_uint)
end

.Ssign(*args) ⇒ Object



473
474
475
# File 'lib/rbthemis.rb', line 473

def Ssign(*args)
  s_sign(*args)
end

.Sverify(*args) ⇒ Object



509
510
511
# File 'lib/rbthemis.rb', line 509

def Sverify(*args)
  s_verify(*args)
end

.valid_key(key) ⇒ Object



265
266
267
268
269
270
271
# File 'lib/rbthemis.rb', line 265

def Themis.valid_key(key)
  if key.nil? || key.empty?
    return false
  end
  key_, len_ = string_to_pointer_size(key)
  return themis_is_valid_asym_key(key_, len_) == SUCCESS
end