Module: Botan

Defined in:
lib/botan/error.rb,
lib/botan/kdf.rb,
lib/botan/mac.rb,
lib/botan/rng.rb,
lib/botan/utils.rb,
lib/botan/bcrypt.rb,
lib/botan/cipher.rb,
lib/botan/digest.rb,
lib/botan/version.rb,
lib/botan/defaults.rb,
lib/botan/pk/mceies.rb,
lib/botan/pk/op/sign.rb,
lib/botan/pk/op/verify.rb,
lib/botan/pk/publickey.rb,
lib/botan/pk/op/decrypt.rb,
lib/botan/pk/op/encrypt.rb,
lib/botan/pk/privatekey.rb,
lib/botan/x509/certificate.rb,
lib/botan/x509/constraints.rb,
lib/botan/pk/op/keyagreement.rb

Overview

© 2017 Ribose Inc.

Defined Under Namespace

Modules: BCrypt, KDF, PK, X509 Classes: Cipher, Digest, Error, MAC, RNG

Constant Summary collapse

VERSION =
'0.1.2'
DEFAULT_KDF_ALGO =
'KDF2(SHA-256)'
DEFAULT_KDF_SALT_LENGTH =
16
DEFAULT_KDF_ITERATIONS =
100_000
DEFAULT_PBKDF_ALGO =
'PBKDF2(SHA-256)'
DEFAULT_EME =
'EME1(SHA-256)'
DEFAULT_EMSA =
{ 'RSA' => 'EMSA4(SHA-256)',
'DSA' => 'EMSA1(SHA-256)',
'ECDSA' => 'EMSA1(SHA-256)',
'ECKCDSA' => 'EMSA1(SHA-256)',
'ECGDSA' => 'EMSA1(SHA-256)',
'GOST-34.10' => 'EMSA1(SHA-256)' }.freeze
DEFAULT_AEAD =
'AES-256/OCB'

Class Method Summary collapse

Class Method Details

.call_ffi(fn, *args) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Calls the LibBotan FFI function indicated. If the return code is <0, an error will be raised.

Parameters:

  • fn (Symbol)

    the name of the function to call

  • args

    the arguments to pass to the FFI function



49
50
51
52
# File 'lib/botan/utils.rb', line 49

def self.call_ffi(fn, *args)
  call_ffi_rc(fn, *args)
  nil
end

.call_ffi_rc(fn, *args) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calls the LibBotan FFI function indicated and returns the return code. If the return code is <0, an error will be raised.

Parameters:

  • fn (Symbol)

    the name of the function to call

  • args

    the arguments to pass to the FFI function

Returns:

  • (Integer)

    the return code

Raises:



35
36
37
38
39
# File 'lib/botan/utils.rb', line 35

def self.call_ffi_rc(fn, *args)
  rc = LibBotan.method(fn).call(*args)
  raise Botan::Error, "FFI call to #{fn} failed (rc: #{rc})" if rc.negative?
  rc
end

.call_ffi_with_buffer(fn, guess: 4096, string: false) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calls the LibBotan FFI function indicated If the return code is <0, an error will be raised.

Parameters:

  • fn (#call)

    a proc/lambda taking two parameters.

  • guess (Integer) (defaults to: 4096)

    an initial guess for the buffer size

  • string (Boolean) (defaults to: false)

    true if the returned buffer is expected to be a NULL-terminated string.

Returns:

  • (String)

    the data



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/botan/utils.rb', line 64

def self.call_ffi_with_buffer(fn, guess: 4096, string: false)
  buf = FFI::MemoryPointer.new(:uint8, guess)
  buf_len_ptr = FFI::MemoryPointer.new(:size_t)
  buf_len_ptr.write(:size_t, buf.size)

  rc = fn.call(buf, buf_len_ptr)
  buf_len = buf_len_ptr.read(:size_t)
  # Call should only fail if buffer was inadequate, and should
  # only succeed if buffer was adequate.
  if (rc.negative? && buf_len <= buf.size) || (rc >= 0 && buf_len > buf.size)
    raise Botan::Error, 'FFI call unexpectedly failed'
  end

  if rc.negative?
    return call_ffi_with_buffer(fn, guess: buf_len, string: string)
  else
    string ? buf.read_string : buf.read_bytes(buf_len)
  end
end

.Digest(algo) ⇒ Class

Returns a Digest subclass by name.

Parameters:

  • algo (String)

    the hash algorithm name

Returns:

  • (Class)


139
140
141
# File 'lib/botan/digest.rb', line 139

def Digest(algo)
  Botan::Digest.const_get(algo)
end

.hex_decode(hexs) ⇒ String

Decodes the provided hexadecimal string to a byte string.

Parameters:

  • hexs (String)

    the hexadecimal string

Returns:

  • (String)

    the decoded data



23
24
25
# File 'lib/botan/utils.rb', line 23

def self.hex_decode(hexs)
  [hexs].pack('H*')
end

.hex_encode(data) ⇒ String

Encodes the provided data as a hexadecimal string.

Parameters:

  • data (String)

    the data to encode

Returns:

  • (String)

    the data as a hexadecimal string



15
16
17
# File 'lib/botan/utils.rb', line 15

def self.hex_encode(data)
  data.unpack('H*')[0]
end

.inspect_ptr(myself) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



85
86
87
88
89
90
# File 'lib/botan/utils.rb', line 85

def self.inspect_ptr(myself)
  ptr_format = "0x%0#{FFI::Pointer.size * 2}x"
  ptr_s = format(ptr_format, myself.instance_variable_get(:@ptr).address)
  class_name = myself.class.to_s
  "#<#{class_name}:#{ptr_s}>"
end