Class: BlockCipherKit::AES256GCMScheme
Constant Summary
collapse
- IV_LENGTH =
12
Instance Method Summary
collapse
Methods inherited from BaseScheme
#read_copy_stream_via_cipher, #streaming_decrypt_range, #write_copy_stream_via_cipher
Constructor Details
#initialize(encryption_key, iv_generator: SecureRandom, auth_data: "") ⇒ AES256GCMScheme
Returns a new instance of AES256GCMScheme.
4
5
6
7
8
9
|
# File 'lib/block_cipher_kit/aes_256_gcm_scheme.rb', line 4
def initialize(encryption_key, iv_generator: SecureRandom, auth_data: "")
raise ArgumentError, "#{required_encryption_key_length} bytes of key material needed, at the minimum" unless encryption_key.bytesize >= required_encryption_key_length
@iv_generator = iv_generator
@auth_data = BlockCipherKit::KeyMaterial.new(auth_data.b)
@key = BlockCipherKit::KeyMaterial.new(encryption_key.byteslice(0, 32))
end
|
Instance Method Details
#ctr_iv(initial_iv_from_input, for_block_n) ⇒ Object
100
101
102
103
104
105
106
107
|
# File 'lib/block_cipher_kit/aes_256_gcm_scheme.rb', line 100
def ctr_iv(initial_iv_from_input, for_block_n)
raise ArgumentError unless initial_iv_from_input.bytesize == 12
ctr = (2 + (for_block_n * 2))
initial_iv_from_input.b + [ctr].pack("N")
end
|
#decrypt_range(from_ciphertext_io:, range:) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/block_cipher_kit/aes_256_gcm_scheme.rb', line 66
def decrypt_range(from_ciphertext_io:, range:)
block_and_tag_size = 16 + 16
n_blocks_to_skip, offset_into_first_block = range.begin.divmod(block_and_tag_size)
n_bytes_to_read = range.end - range.begin + 1
n_blocks_to_read = ((offset_into_first_block + n_bytes_to_read) / block_and_tag_size.to_f).ceil
initial_iv_from_input = from_ciphertext_io.read(12)
ciphertext_starts_at = from_ciphertext_io.pos
cipher = OpenSSL::Cipher.new("aes-256-ctr")
cipher.decrypt
cipher.iv = ctr_iv(initial_iv_from_input, n_blocks_to_skip) cipher.key = @key
buf = StringIO.new.binmode
from_ciphertext_io.seek(ciphertext_starts_at + (n_blocks_to_skip * block_and_tag_size))
read_copy_stream_via_cipher(source_io: from_ciphertext_io, cipher: cipher, read_limit: n_blocks_to_read * block_and_tag_size, destination_io: buf)
buf.seek(offset_into_first_block) buf.read(n_bytes_to_read) end
|
#required_encryption_key_length ⇒ Object
11
12
13
|
# File 'lib/block_cipher_kit/aes_256_gcm_scheme.rb', line 11
def required_encryption_key_length
32
end
|
#streaming_decrypt(from_ciphertext_io:, into_plaintext_io: nil, &blk) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/block_cipher_kit/aes_256_gcm_scheme.rb', line 31
def streaming_decrypt(from_ciphertext_io:, into_plaintext_io: nil, &blk)
iv = from_ciphertext_io.read(IV_LENGTH)
start_at = from_ciphertext_io.pos
tag_len = 16
from_ciphertext_io.seek(from_ciphertext_io.size - tag_len)
auth_tag_from_io_tail = from_ciphertext_io.read(tag_len)
cipher = OpenSSL::Cipher.new("aes-256-gcm")
cipher.decrypt
cipher.iv = iv
cipher.key = @key
cipher.auth_tag = auth_tag_from_io_tail
cipher.auth_data = @auth_data
from_ciphertext_io.seek(start_at)
n_bytes_to_read_excluding_auth_tag = from_ciphertext_io.size - from_ciphertext_io.pos - tag_len
read_copy_stream_via_cipher(source_io: from_ciphertext_io, cipher: cipher, read_limit: n_bytes_to_read_excluding_auth_tag, destination_io: into_plaintext_io, &blk)
end
|
#streaming_encrypt(into_ciphertext_io:, from_plaintext_io: nil, &blk) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/block_cipher_kit/aes_256_gcm_scheme.rb', line 15
def streaming_encrypt(into_ciphertext_io:, from_plaintext_io: nil, &blk)
iv = @iv_generator.bytes(IV_LENGTH)
into_ciphertext_io.write(iv)
cipher = OpenSSL::Cipher.new("aes-256-gcm")
cipher.encrypt
cipher.iv = iv
cipher.key = @key
cipher.auth_data = @auth_data
write_copy_stream_via_cipher(source_io: from_plaintext_io, cipher: cipher, destination_io: into_ciphertext_io, &blk)
tag = cipher.auth_tag
into_ciphertext_io.write(tag)
end
|