Class: Dommy::SubtleCrypto

Inherits:
Object
  • Object
show all
Includes:
Bridge::Methods
Defined in:
lib/dommy/crypto.rb

Overview

‘SubtleCrypto` — `window.crypto.subtle`. Currently covers `digest` (SHA-1 / SHA-256 / SHA-384 / SHA-512), which is by far the most commonly used operation in test contexts. Encrypt / decrypt / sign / verify / key generation are out of scope; tests that need them should mock `crypto.subtle` directly.

Returned values are byte arrays — real ‘SubtleCrypto.digest` resolves to an `ArrayBuffer`; we expose the equivalent Ruby byte array so callers can convert as needed.

Constant Summary collapse

ALGORITHMS =
{
  "SHA-1" => -> (data) { Digest::SHA1.digest(data) },
  "SHA-256" => -> (data) { Digest::SHA256.digest(data) },
  "SHA-384" => -> (data) { Digest::SHA384.digest(data) },
  "SHA-512" => -> (data) { Digest::SHA512.digest(data) }
}.freeze

Instance Method Summary collapse

Methods included from Bridge::Methods

included

Constructor Details

#initialize(window = nil) ⇒ SubtleCrypto

Returns a new instance of SubtleCrypto.



94
95
96
# File 'lib/dommy/crypto.rb', line 94

def initialize(window = nil)
  @window = window
end

Instance Method Details

#__js_call__(method, args) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/dommy/crypto.rb', line 239

def __js_call__(method, args)
  case method
  when "digest"
    digest(args[0], args[1])
  when "generateKey"
    generate_key(args[0], args[1], args[2])
  when "importKey"
    import_key(args[0], args[1], args[2], args[3], args[4])
  when "sign"
    sign(args[0], args[1], args[2])
  when "verify"
    verify(args[0], args[1], args[2], args[3])
  when "encrypt"
    encrypt(args[0], args[1], args[2])
  when "decrypt"
    decrypt(args[0], args[1], args[2])
  end
end

#decrypt(algorithm, key, data) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/dommy/crypto.rb', line 223

def decrypt(algorithm, key, data)
  promise do
    bytes = coerce_bytes(data)
    tag_len = aes_gcm_tag_length(algorithm)
    raise ArgumentError, "ciphertext shorter than auth tag" if bytes.bytesize < tag_len

    ct = bytes.byteslice(0, bytes.bytesize - tag_len)
    tag = bytes.byteslice(bytes.bytesize - tag_len, tag_len)
    cipher = build_gcm_cipher(:decrypt, algorithm, key)
    cipher.auth_tag = tag
    (cipher.update(ct) + cipher.final).bytes
  end
end

#digest(algorithm, data) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dommy/crypto.rb', line 98

def digest(algorithm, data)
  promise do
    name = algorithm_name(algorithm)
    hasher = ALGORITHMS[name]
    raise ArgumentError, "unsupported algorithm: #{name}" unless hasher

    # WHATWG: digest() resolves to an ArrayBuffer — wrap so it crosses the
    # JS boundary as a bare ArrayBuffer (not a plain Array).
    Bridge::ArrayBuffer.new(hasher.call(coerce_bytes(data)).bytes)
  end
end

#encrypt(algorithm, key, data) ⇒ Object

AES-GCM encrypt. ‘algorithm` must be `“AES-GCM”, iv: <bytes>, additionalData?: <bytes>, tagLength?: 128`. Output is `ciphertext || authTag`, matching WebCrypto.



212
213
214
215
216
217
218
219
220
221
# File 'lib/dommy/crypto.rb', line 212

def encrypt(algorithm, key, data)
  promise do
    cipher = build_gcm_cipher(:encrypt, algorithm, key)
    ct = cipher.update(coerce_bytes(data)) + cipher.final
    # OpenSSL always produces a 16-byte tag for GCM; truncate to
    # the requested `tagLength` to honour the spec.
    tag = cipher.auth_tag.byteslice(0, aes_gcm_tag_length(algorithm))
    (ct + tag).bytes
  end
end

#generate_key(algorithm, extractable = true, usages = nil) ⇒ Object Also known as: generateKey

Generate a fresh symmetric key. ‘algorithm` is `“HMAC”, hash: “SHA-256”` or `“AES-GCM”, length: 128|256`.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/dommy/crypto.rb', line 112

def generate_key(algorithm, extractable = true, usages = nil)
  promise do
    case primary_algorithm_name(algorithm)
    when "HMAC"
      hash = hmac_hash_from(algorithm)
      CryptoKey.new(
        :secret,
        "HMAC",
        hash,
        SecureRandom.bytes(openssl_digest_size(hash)),
        extractable: extractable,
        usages: usages || %w[sign verify]
      )
    when "AES-GCM", "AES-CBC", "AES-CTR"
      length = (algorithm.is_a?(Hash) && (algorithm["length"] || algorithm[:length])) || 256
      raise ArgumentError, "AES key length must be 128/192/256" unless [128, 192, 256].include?(length)

      CryptoKey.new(
        :secret,
        primary_algorithm_name(algorithm),
        nil,
        SecureRandom.bytes(length / 8),
        extractable: extractable,
        usages: usages || %w[encrypt decrypt]
      )
    else
      raise ArgumentError, "unsupported algorithm: #{primary_algorithm_name(algorithm)}"
    end
  end
end

#import_key(format, key_data, algorithm, extractable = true, usages = nil) ⇒ Object Also known as: importKey

Import a raw key. Supports HMAC and AES-GCM/CBC/CTR.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/dommy/crypto.rb', line 146

def import_key(format, key_data, algorithm, extractable = true, usages = nil)
  promise do
    raise ArgumentError, "only raw format supported" unless format.to_s == "raw"

    bytes = coerce_bytes(key_data)
    case primary_algorithm_name(algorithm)
    when "HMAC"
      hash = hmac_hash_from(algorithm)
      CryptoKey.new(
        :secret,
        "HMAC",
        hash,
        bytes,
        extractable: extractable,
        usages: usages || %w[sign verify]
      )
    when "AES-GCM", "AES-CBC", "AES-CTR"
      unless [16, 24, 32].include?(bytes.bytesize)
        raise ArgumentError, "AES key must be 16/24/32 bytes"
      end

      CryptoKey.new(
        :secret,
        primary_algorithm_name(algorithm),
        nil,
        bytes,
        extractable: extractable,
        usages: usages || %w[encrypt decrypt]
      )
    else
      raise ArgumentError, "unsupported algorithm: #{primary_algorithm_name(algorithm)}"
    end
  end
end

#sign(_algorithm, key, data) ⇒ Object

HMAC sign — returns the MAC as a byte array.



184
185
186
187
188
189
190
191
# File 'lib/dommy/crypto.rb', line 184

def sign(_algorithm, key, data)
  promise do
    raise ArgumentError, "HMAC key required" unless key.is_a?(CryptoKey) && key.algorithm_name == "HMAC"
    raise ArgumentError, "key.usages must include 'sign'" unless key.usages.include?("sign")

    OpenSSL::HMAC.digest(openssl_digest_name(key.hash_name), key.__dommy_bytes__, coerce_bytes(data)).bytes
  end
end

#verify(_algorithm, key, signature, data) ⇒ Object

HMAC verify — constant-time compare of the MAC.



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/dommy/crypto.rb', line 194

def verify(_algorithm, key, signature, data)
  promise do
    raise ArgumentError, "HMAC key required" unless key.is_a?(CryptoKey) && key.algorithm_name == "HMAC"
    raise ArgumentError, "key.usages must include 'verify'" unless key.usages.include?("verify")

    expected = OpenSSL::HMAC.digest(openssl_digest_name(key.hash_name), key.__dommy_bytes__, coerce_bytes(data))
    sig_bytes = coerce_bytes(signature)
    if expected.bytesize == sig_bytes.bytesize
      OpenSSL.fixed_length_secure_compare(expected, sig_bytes)
    else
      false
    end
  end
end