Class: Saro::Dat::DatCrypto

Inherits:
Object
  • Object
show all
Defined in:
lib/saro/dat/crypto.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(algorithm, key_bytes, config = nil) ⇒ DatCrypto

Returns a new instance of DatCrypto.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/saro/dat/crypto.rb', line 33

def initialize(algorithm, key_bytes, config = nil)
  @config = config || Saro::Dat.get_crypto_config(algorithm)
  if key_bytes.bytesize != @config[:length]
    raise Saro::Dat::Error.new(
      Saro::Dat::ErrorCode::KEY_INVALID,
      "#{algorithm} key must be #{@config[:length]} bytes, got #{key_bytes.bytesize}"
    )
  end
  @algorithm = algorithm
  @key_bytes = key_bytes
end

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



31
32
33
# File 'lib/saro/dat/crypto.rb', line 31

def algorithm
  @algorithm
end

Class Method Details

.generate(algorithm) ⇒ Object



45
46
47
48
49
# File 'lib/saro/dat/crypto.rb', line 45

def self.generate(algorithm)
  config = Saro::Dat.get_crypto_config(algorithm)
  key_bytes = OpenSSL::Random.random_bytes(config[:length])
  new(algorithm, key_bytes, config)
end

.imports(algorithm, base64_str) ⇒ Object



51
52
53
54
# File 'lib/saro/dat/crypto.rb', line 51

def self.imports(algorithm, base64_str)
  key_bytes = Saro::Dat::Util.decode_base64_url(base64_str)
  new(algorithm, key_bytes)
end

Instance Method Details

#decrypt(data) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/saro/dat/crypto.rb', line 87

def decrypt(data)
  return "".b if data.nil? || data.empty?

  data = data.b if data.is_a?(String) && data.encoding != Encoding::BINARY

  if data.bytesize <= 12 + 16
    raise Saro::Dat::Error.new(Saro::Dat::ErrorCode::CRYPTO_DATA_INVALID, "ciphertext is shorter than iv(12) + tag(16)")
  end

  nonce = data[0, 12]
  tag = data[-16, 16]
  ciphertext = data[12...-16]

  cipher = OpenSSL::Cipher.new(@config[:name])
  cipher.decrypt
  cipher.key = @key_bytes
  cipher.iv_len = 12
  cipher.iv = nonce
  cipher.auth_tag = tag

  begin
    res = cipher.update(ciphertext) + cipher.final
  rescue OpenSSL::Cipher::CipherError => e
    raise Saro::Dat::Error.new(Saro::Dat::ErrorCode::CRYPTO_TAG_MISMATCH, "gcm authentication tag mismatch", cause: e)
  end
  res.force_encoding('BINARY')
  res
end

#decrypt_base64(base64_str) ⇒ Object



83
84
85
# File 'lib/saro/dat/crypto.rb', line 83

def decrypt_base64(base64_str)
  decrypt(Saro::Dat::Util.decode_base64_url(base64_str))
end

#encrypt(data) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/saro/dat/crypto.rb', line 60

def encrypt(data)
  if data.is_a?(String) && data.encoding != Encoding::BINARY && data.encoding != Encoding::UTF_8
    data = data.encode('utf-8')
  end
  return "".b if data.nil? || data.empty?

  cipher = OpenSSL::Cipher.new(@config[:name])
  cipher.encrypt
  cipher.key = @key_bytes
  nonce = OpenSSL::Random.random_bytes(12)
  cipher.iv_len = 12
  cipher.iv = nonce
  
  begin
    ciphertext = cipher.update(data) + cipher.final
    tag = cipher.auth_tag
  rescue OpenSSL::Cipher::CipherError => e
    raise Saro::Dat::Error.new(Saro::Dat::ErrorCode::CRYPTO_BACKEND, "aes-gcm encrypt failed", cause: e)
  end

  nonce + ciphertext + tag
end

#exportsObject



56
57
58
# File 'lib/saro/dat/crypto.rb', line 56

def exports
  Saro::Dat::Util.encode_base64_url_str(@key_bytes)
end