xaes_256_gcm 0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3ef71e358ba4956fd8741d2f0a2b40c1fb43af910c83ed100f93e2f4b8115af5
4
+ data.tar.gz: 8121bbb3b12266bd94e4a516226a880d41d22719af8c273b1969fe3ac096503f
5
+ SHA512:
6
+ metadata.gz: 2cdbd2478078a837754587ad5afb56d74904d55429a9764975eed58ec783c1060f96f52237247599bcfb7f7fed7bedcf0f9a8d170511906be12b42c8d1716e9d
7
+ data.tar.gz: f9e10e5a46c21b7c082c410a833119b6bc1220d578e20242fa250296b8e040870d7b7c81d82b363c364cee763841b29ff056db21ec0cd3f768879a902e36895f
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Kevin Jones
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ module Xaes256Gcm
5
+ # A base error that all Xaes256Gcm errors derive from.
6
+ Error = Class.new(StandardError)
7
+
8
+ # Indicates that the provided key is invalid because it is nil or the incorrect length.
9
+ InvalidKeyError = Class.new(Error)
10
+
11
+ # Indicates that the provided nonce is invalid because it is nil or the incorrect length.
12
+ InvalidNonceError = Class.new(Error)
13
+
14
+ # Indicates that the provided ciphertext is invalid because the authentication tag failed to verify.
15
+ #
16
+ # This may indicate that one of the inputs to open was invalid, or the ciphertext has been modified.
17
+ InvalidCiphertextError = Class.new(Error)
18
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Xaes256Gcm
4
+ # The current version of the gem.
5
+ VERSION = "0.3"
6
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # The module containing XAES-256-GCM.
5
+ module Xaes256Gcm
6
+
7
+ # Represents an instance of the XAES-256-GCM algorithm.
8
+ class Xaes256GcmCipher
9
+
10
+ # The nonce size of the algorithm, in bytes.
11
+ NONCE_SIZE = 24.freeze
12
+
13
+ # The key size of the algorithm, in bytes.
14
+ KEY_SIZE = 32.freeze
15
+
16
+ # The overhead of a ciphertext.
17
+ #
18
+ # When a plaintext is encrypted, the resulting ciphertext will be larger because of the authentication tag.
19
+ # This value indicates how much larger a ciphertext will be than a plaintext.
20
+ OVERHEAD = 16.freeze
21
+
22
+ BLOCK_SIZE = 16.freeze
23
+ private_constant :BLOCK_SIZE
24
+
25
+ # Creates a new instance of the XAES-256-GCM algorithm.
26
+ def initialize(key)
27
+ raise InvalidKeyError.new if key.nil? || !key.is_a?(String) || key.bytesize != KEY_SIZE
28
+
29
+ @aes = OpenSSL::Cipher::AES256.new(:ECB)
30
+ @aes.encrypt
31
+ @aes.padding = 0
32
+ @aes.key = key
33
+
34
+ k1init = "\0".b * BLOCK_SIZE
35
+ k1init = @aes.update(k1init)
36
+
37
+ msb = 0
38
+ k1 = k1init.bytes
39
+
40
+ for i in (k1.length - 1).downto(0)
41
+ msbC = msb
42
+ msb = k1[i] >> 7
43
+ k1[i] = ((k1[i] << 1) & 0b11111111) | msbC;
44
+ end
45
+
46
+ k1[-1] = k1[-1] ^ ((msb * 0b10000111) & 0b11111111)
47
+ @k1 = NoInspectBox.new(k1)
48
+ end
49
+
50
+ # Seals, or encrypts, a plaintext with a nonce. Optional additional authenticated data can be provided.
51
+ def seal(plaintext, nonce, additionalData = nil)
52
+ raise InvalidNonceError if nonce.bytesize != NONCE_SIZE
53
+
54
+ key = derive_key(nonce.byteslice(0, 12))
55
+ gcm = OpenSSL::Cipher::AES256.new(:GCM)
56
+ gcm.encrypt
57
+ gcm.iv = nonce.byteslice(12, 12)
58
+ gcm.key = key
59
+ gcm.auth_data = additionalData unless additionalData.nil?
60
+
61
+ ciphertext = ""
62
+ ciphertext = gcm.update(plaintext) if plaintext.bytesize > 0
63
+ ciphertext += gcm.final
64
+ ciphertext + gcm.auth_tag
65
+ end
66
+
67
+ # Opens, or decrypts, a ciphertext with a nonce. Optional additional authenticated data can be provided.
68
+ def open(ciphertext, nonce, additionalData = nil)
69
+ ct_bytes = ciphertext.bytesize
70
+ raise InvalidNonceError if nonce.bytesize != NONCE_SIZE
71
+ raise InvalidCiphertextError if ciphertext.bytesize < OVERHEAD
72
+
73
+ tagless_ciphertext = ciphertext.byteslice(0, ct_bytes - OVERHEAD)
74
+ tag = ciphertext.byteslice(ct_bytes - OVERHEAD, OVERHEAD)
75
+
76
+ key = derive_key(nonce.byteslice(0, 12))
77
+ gcm = OpenSSL::Cipher::AES256.new(:GCM)
78
+ gcm.decrypt
79
+ gcm.iv = nonce.byteslice(12, 12)
80
+ gcm.key = key
81
+ gcm.auth_data = additionalData unless additionalData.nil?
82
+ gcm.auth_tag = tag
83
+
84
+ plaintext = ""
85
+ plaintext = gcm.update(tagless_ciphertext) if tagless_ciphertext.bytesize > 0
86
+
87
+ begin
88
+ plaintext += gcm.final
89
+ rescue OpenSSL::Cipher::CipherError => ex
90
+ raise InvalidCiphertextError.new
91
+ end
92
+
93
+ return plaintext
94
+ end
95
+
96
+ private
97
+
98
+ def derive_key(nonce)
99
+ nonce_bytes = nonce.bytes
100
+
101
+ # 0x58 is the ASCII value for "X"
102
+ m1 = [0, 1, 0x58, 0] + nonce_bytes
103
+ m2 = [0, 2, 0x58, 0] + nonce_bytes
104
+ xor(m1, @k1.value)
105
+ xor(m2, @k1.value)
106
+
107
+ m12 = (m1 + m2).pack("C*")
108
+ @aes.update(m12)
109
+ end
110
+
111
+ def xor(destination, other)
112
+ destination.each_with_index do |value, index|
113
+ destination[index] = destination[index] ^ other[index]
114
+ end
115
+ end
116
+
117
+ # This is just a smaller helper class that returns a redacted value for to_s and inspect.
118
+ # It exists to simply make sure any state on the instance is not accidentally logged or printed.
119
+ class NoInspectBox
120
+ def initialize(value)
121
+ @value = value
122
+ end
123
+
124
+ def value
125
+ @value
126
+ end
127
+
128
+ def inspect
129
+ return "hidden"
130
+ end
131
+
132
+ def to_s
133
+ return "hidden"
134
+ end
135
+ end
136
+
137
+ private_constant :NoInspectBox
138
+ end
139
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openssl"
4
+ require "xaes_256_gcm/errors.rb"
5
+ require "xaes_256_gcm/version.rb"
6
+ require "xaes_256_gcm/xaes_256_gcm_cipher.rb"
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xaes_256_gcm
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.3'
5
+ platform: ruby
6
+ authors:
7
+ - vcsjones
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rspec
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '3.13'
19
+ type: :development
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '3.13'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rake
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '13.3'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '13.3'
40
+ email: kevin@vcsjones.dev
41
+ executables: []
42
+ extensions: []
43
+ extra_rdoc_files: []
44
+ files:
45
+ - "./LICENSE.md"
46
+ - "./lib/xaes_256_gcm.rb"
47
+ - "./lib/xaes_256_gcm/errors.rb"
48
+ - "./lib/xaes_256_gcm/version.rb"
49
+ - "./lib/xaes_256_gcm/xaes_256_gcm_cipher.rb"
50
+ homepage: https://github.com/vcsjones/xaes_256_gcm
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '2.7'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.6.9
69
+ specification_version: 4
70
+ summary: Implements the XAES-256-GCM algorithm.
71
+ test_files: []