Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use native hkdf #3

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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