Skip to content

Commit

Permalink
Use native hkdf
Browse files Browse the repository at this point in the history
  • Loading branch information
collimarco authored Feb 2, 2023
2 parents 7e8c631 + 5784ab0 commit 5326a5f
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 8 deletions.
1 change: 0 additions & 1 deletion lib/web_push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require 'openssl'
require 'base64'
require 'hkdf'
require 'jwt'
require 'uri'
require 'net/http'
Expand Down
7 changes: 4 additions & 3 deletions lib/web_push/encryption.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def encrypt(message, p256dh, auth)
assert_arguments(message, p256dh, auth)

group_name = 'prime256v1'
hash = 'SHA256'
salt = Random.new.bytes(16)

server = OpenSSL::PKey::EC.generate(group_name)
Expand All @@ -25,11 +26,11 @@ def encrypt(message, p256dh, auth)
content_encryption_key_info = "Content-Encoding: aes128gcm\0"
nonce_info = "Content-Encoding: nonce\0"

prk = HKDF.new(shared_secret, salt: client_auth_token, algorithm: 'SHA256', info: info).read(32)
prk = OpenSSL::KDF.hkdf(shared_secret, salt: client_auth_token, info: info, hash: hash, length: 32)

content_encryption_key = HKDF.new(prk, salt: salt, info: content_encryption_key_info).read(16)
content_encryption_key = OpenSSL::KDF.hkdf(prk, salt: salt, info: content_encryption_key_info, hash: hash, length: 16)

nonce = HKDF.new(prk, salt: salt, info: nonce_info).read(12)
nonce = OpenSSL::KDF.hkdf(prk, salt: salt, info: nonce_info, hash: hash, length: 12)

ciphertext = encrypt_payload(message, content_encryption_key, nonce)

Expand Down
6 changes: 3 additions & 3 deletions spec/web_push/encryption_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ def decrypt payload
content_encryption_key_info = "Content-Encoding: aes128gcm\0"
nonce_info = "Content-Encoding: nonce\0"

prk = HKDF.new(shared_secret, salt: client_auth_token, algorithm: 'SHA256', info: info).read(32)
prk = OpenSSL::KDF.hkdf(shared_secret, salt: client_auth_token, info: info, hash: 'SHA256', length: 32)

content_encryption_key = HKDF.new(prk, salt: salt, info: content_encryption_key_info).read(16)
nonce = HKDF.new(prk, salt: salt, info: nonce_info).read(12)
content_encryption_key = OpenSSL::KDF.hkdf(prk, salt: salt, info: content_encryption_key_info, hash: 'SHA256', length: 16)
nonce = OpenSSL::KDF.hkdf(prk, salt: salt, info: nonce_info, hash: 'SHA256', length: 12)

decrypt_ciphertext(ciphertext, content_encryption_key, nonce)
end
Expand Down
1 change: 0 additions & 1 deletion web-push.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = '>= 3.0'

spec.add_dependency 'hkdf', '~> 1.0'
spec.add_dependency 'jwt', '~> 2.0'
spec.add_dependency 'openssl', '~> 3.0'

Expand Down

0 comments on commit 5326a5f

Please sign in to comment.