Class: PorkyLib::Symmetric

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/porky_lib/symmetric.rb

Defined Under Namespace

Classes: DecryptedText, EncryptedText

Constant Summary collapse

CMK_KEY_ORIGIN =
'AWS_KMS'
CMK_KEY_USAGE =
'ENCRYPT_DECRYPT'
SYMMETRIC_KEY_SPEC =
'AES_256'

Instance Method Summary collapse

Instance Method Details

#clientObject



18
19
20
21
# File 'lib/porky_lib/symmetric.rb', line 18

def client
  require 'porky_lib/aws/kms/client' if PorkyLib::Config.config[:aws_client_mock]
  @client ||= Aws::KMS::Client.new
end

#cmk_alias_exists?(key_alias) ⇒ Boolean

rubocop:enable Style/OptionalBooleanParameter

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
# File 'lib/porky_lib/symmetric.rb', line 38

def cmk_alias_exists?(key_alias)
  alias_list = client.list_aliases.to_h[:aliases]
  alias_list.each do |item|
    return true if item[:alias_name] == key_alias
  end

  false
end

#create_alias(key_id, key_alias) ⇒ Object



51
52
53
# File 'lib/porky_lib/symmetric.rb', line 51

def create_alias(key_id, key_alias)
  client.create_alias(target_key_id: key_id, alias_name: key_alias)
end

#create_key(tags, key_alias = nil, key_rotation_enabled = true) ⇒ Object

rubocop:disable Style/OptionalBooleanParameter



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/porky_lib/symmetric.rb', line 24

def create_key(tags, key_alias = nil, key_rotation_enabled = true)
  resp = client.create_key(key_usage: CMK_KEY_USAGE, origin: CMK_KEY_ORIGIN, tags: tags)
  key_id = resp.to_h[:key_metadata][:key_id]

  # Enable automatic key rotation for the newly created CMK
  enable_key_rotation(key_id) if key_rotation_enabled

  # Create an alias for the newly created CMK
  create_alias(key_id, key_alias) if key_alias

  key_id
end

#decrypt(ciphertext_dek, ciphertext, nonce, encryption_context = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/porky_lib/symmetric.rb', line 95

def decrypt(ciphertext_dek, ciphertext, nonce, encryption_context = nil)
  return if ciphertext.nil? || ciphertext_dek.nil? || nonce.nil?

  # Decrypt the data encryption key
  plaintext_key = decrypt_data_encryption_key(ciphertext_dek, encryption_context)
  secret_box = RbNaCl::SecretBox.new(plaintext_key)

  should_reencrypt = false
  begin
    # Decrypt the message
    message = secret_box.decrypt(nonce, ciphertext)
  rescue RbNaCl::CryptoError
    # For backwards compatibility due to a code error in a previous release
    plaintext_key.replace(secure_delete_plaintext_key(plaintext_key.bytesize))
    message = secret_box.decrypt(nonce, ciphertext)
    should_reencrypt = true
  end

  # Securely delete the plaintext value from memory
  plaintext_key.replace(secure_delete_plaintext_key(plaintext_key.bytesize))

  [message, should_reencrypt]
end

#decrypt_data_encryption_key(ciphertext_key, encryption_context = nil) ⇒ Object



63
64
65
66
67
68
# File 'lib/porky_lib/symmetric.rb', line 63

def decrypt_data_encryption_key(ciphertext_key, encryption_context = nil)
  return client.decrypt(ciphertext_blob: ciphertext_key, encryption_context: encryption_context).plaintext if encryption_context

  resp = client.decrypt(ciphertext_blob: ciphertext_key)
  resp.plaintext
end

#decrypt_with_benchmark(ciphertext_dek, ciphertext, nonce, encryption_context = nil) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/porky_lib/symmetric.rb', line 160

def decrypt_with_benchmark(ciphertext_dek, ciphertext, nonce, encryption_context = nil)
  return if ciphertext.nil? || ciphertext_dek.nil? || nonce.nil?

  encryption_statistics = {}

  plaintext_key = benchmark_block(encryption_statistics, :decrypt_key) do
    # Decrypt the data encryption key
    decrypt_data_encryption_key(ciphertext_dek, encryption_context)
  end

  message, should_reencrypt = benchmark_block(encryption_statistics, :decrypt) do
    secret_box = RbNaCl::SecretBox.new(plaintext_key)

    should_reencrypt = false
    begin
      # Decrypt the message
      message = secret_box.decrypt(nonce, ciphertext)
    rescue RbNaCl::CryptoError
      # For backwards compatibility due to a code error in a previous release
      plaintext_key.replace(secure_delete_plaintext_key(plaintext_key.bytesize))
      message = secret_box.decrypt(nonce, ciphertext)
      should_reencrypt = true
    end

    [message, should_reencrypt, encryption_statistics]
  end

  benchmark_block(encryption_statistics, :clear_key) do
    # Securely delete the plaintext value from memory
    plaintext_key.replace(secure_delete_plaintext_key(plaintext_key.bytesize))
  end

  [message, should_reencrypt, encryption_statistics]
end

#decrypt_with_key(ciphertext, plaintext_key, nonce) ⇒ Object



214
215
216
217
218
219
220
221
222
# File 'lib/porky_lib/symmetric.rb', line 214

def decrypt_with_key(ciphertext, plaintext_key, nonce)
  # Initialize the box
  secret_box = RbNaCl::SecretBox.new(plaintext_key)

  # Decrypt the message
  plaintext = secret_box.decrypt(nonce, ciphertext)

  DecryptedText.new(plaintext, nil)
end

#decrypt_with_key_with_benchmark(ciphertext, plaintext_key, nonce) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/porky_lib/symmetric.rb', line 245

def decrypt_with_key_with_benchmark(ciphertext, plaintext_key, nonce)
  encryption_statistics = {}

  plaintext = benchmark_block(encryption_statistics, :decrypt) do
    secret_box = RbNaCl::SecretBox.new(plaintext_key)

    # Decrypt the message
    plaintext = secret_box.decrypt(nonce, ciphertext)

    plaintext
  end

  DecryptedText.new(plaintext, encryption_statistics)
end

#enable_key_rotation(key_id) ⇒ Object



47
48
49
# File 'lib/porky_lib/symmetric.rb', line 47

def enable_key_rotation(key_id)
  client.enable_key_rotation(key_id: key_id)
end

#encrypt(data, cmk_key_id, ciphertext_dek = nil, encryption_context = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/porky_lib/symmetric.rb', line 70

def encrypt(data, cmk_key_id, ciphertext_dek = nil, encryption_context = nil)
  return if data.nil? || cmk_key_id.nil?

  # Generate a new data encryption key or decrypt existing key, if provided
  plaintext_key = decrypt_data_encryption_key(ciphertext_dek, encryption_context) if ciphertext_dek
  ciphertext_key = ciphertext_dek if ciphertext_dek
  plaintext_key, ciphertext_key = generate_data_encryption_key(cmk_key_id, encryption_context) unless ciphertext_dek

  # Initialize the box
  secret_box = RbNaCl::SecretBox.new(plaintext_key)

  # First, make a nonce: A single-use value never repeated under the same key
  # The nonce isn't secret, and can be sent with the ciphertext.
  # The cipher instance has a nonce_bytes method for determining how many bytes should be in a nonce
  nonce = RbNaCl::Random.random_bytes(secret_box.nonce_bytes)

  # Encrypt a message with SecretBox
  ciphertext = secret_box.encrypt(nonce, data)

  # Securely delete the plaintext value from memory
  plaintext_key.replace(secure_delete_plaintext_key(plaintext_key.bytesize))

  [ciphertext_key, ciphertext, nonce]
end

#encrypt_with_benchmark(data, cmk_key_id, ciphertext_dek = nil, encryption_context = nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/porky_lib/symmetric.rb', line 119

def encrypt_with_benchmark(data, cmk_key_id, ciphertext_dek = nil, encryption_context = nil)
  return if data.nil? || cmk_key_id.nil?

  encryption_statistics = {}

  # Generate a new data encryption key or decrypt existing key, if provided
  if ciphertext_dek
    plaintext_key = benchmark_block(encryption_statistics, :decrypt_key) do
      decrypt_data_encryption_key(ciphertext_dek, encryption_context)
    end

    ciphertext_key = ciphertext_dek
  else
    plaintext_key, ciphertext_key = benchmark_block(encryption_statistics, :generate_key) do
      generate_data_encryption_key(cmk_key_id, encryption_context)
    end
  end

  nonce, ciphertext = benchmark_block(encryption_statistics, :encrypt) do
    # Initialize the box
    secret_box = RbNaCl::SecretBox.new(plaintext_key)

    # First, make a nonce: A single-use value never repeated under the same key
    # The nonce isn't secret, and can be sent with the ciphertext.
    # The cipher instance has a nonce_bytes method for determining how many bytes should be in a nonce
    nonce = RbNaCl::Random.random_bytes(secret_box.nonce_bytes)

    # Encrypt a message with SecretBox
    ciphertext = secret_box.encrypt(nonce, data)

    [nonce, ciphertext]
  end

  benchmark_block(encryption_statistics, :clear_key) do
    # Securely delete the plaintext value from memory
    plaintext_key.replace(secure_delete_plaintext_key(plaintext_key.bytesize))
  end

  [ciphertext_key, ciphertext, nonce, encryption_statistics]
end

#encrypt_with_key(plaintext, plaintext_key) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/porky_lib/symmetric.rb', line 199

def encrypt_with_key(plaintext, plaintext_key)
  # Initialize the box
  secret_box = RbNaCl::SecretBox.new(plaintext_key)

  # First, make a nonce: A single-use value never repeated under the same key
  # The nonce isn't secret, and can be sent with the ciphertext.
  # The cipher instance has a nonce_bytes method for determining how many bytes should be in a nonce
  nonce = RbNaCl::Random.random_bytes(secret_box.nonce_bytes)

  # Encrypt a message with SecretBox
  ciphertext = secret_box.encrypt(nonce, plaintext)

  EncryptedText.new(ciphertext, nonce, nil)
end

#encrypt_with_key_with_benchmark(plaintext, plaintext_key) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/porky_lib/symmetric.rb', line 224

def encrypt_with_key_with_benchmark(plaintext, plaintext_key)
  encryption_statistics = {}

  nonce, ciphertext = benchmark_block(encryption_statistics, :encrypt) do
    # Initialize the box
    secret_box = RbNaCl::SecretBox.new(plaintext_key)

    # First, make a nonce: A single-use value never repeated under the same key
    # The nonce isn't secret, and can be sent with the ciphertext.
    # The cipher instance has a nonce_bytes method for determining how many bytes should be in a nonce
    nonce = RbNaCl::Random.random_bytes(secret_box.nonce_bytes)

    # Encrypt a message with SecretBox
    ciphertext = secret_box.encrypt(nonce, plaintext)

    [nonce, ciphertext]
  end

  EncryptedText.new(ciphertext, nonce, encryption_statistics)
end

#generate_data_encryption_key(cmk_key_id, encryption_context = nil) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/porky_lib/symmetric.rb', line 55

def generate_data_encryption_key(cmk_key_id, encryption_context = nil)
  resp = {}
  resp = client.generate_data_key(key_id: cmk_key_id, key_spec: SYMMETRIC_KEY_SPEC, encryption_context: encryption_context) if encryption_context
  resp = client.generate_data_key(key_id: cmk_key_id, key_spec: SYMMETRIC_KEY_SPEC) unless encryption_context

  [resp.plaintext, resp.ciphertext_blob]
end

#secure_delete_plaintext_key(length) ⇒ Object



195
196
197
# File 'lib/porky_lib/symmetric.rb', line 195

def secure_delete_plaintext_key(length)
  "\0" * length
end