From 910af10b48f193d8be763134dc984544a909e72e Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Fri, 23 Dec 2016 17:44:46 -0800 Subject: [PATCH] Last minute changes to the ChaCha20Poly1305 API Unfortunately I already shipped 4.0.0. Technically this is a semantic versioning violation. However, the API is not yet documented and there have only been 40 downloads of 4.0.0, so I think it's still safe to ship some last-minute changes nobody will complain about (I hope!) - Add "Legacy" to the old ChaCha20Poly1305 API to impart it shouldn't be used except for compatibility reasons - Capitalize both 'C's in ChaCha - Rename the base class of AEAD ciphers to `RbNaCl::AEAD::Base` --- lib/rbnacl.rb | 4 ++-- lib/rbnacl/aead/{aead.rb => base.rb} | 4 ++-- lib/rbnacl/aead/chacha20poly1305_ietf.rb | 2 +- ...cha20poly1305.rb => chacha20poly1305_legacy.rb} | 2 +- spec/rbnacl/aead/chacha20poly1305_ietf_spec.rb | 14 ++++++-------- spec/rbnacl/aead/chacha20poly1305_legacy_spec.rb | 12 ++++++++++++ spec/rbnacl/aead/chacha20poly1305_orig_spec.rb | 14 -------------- spec/shared/aead.rb | 14 ++++++++------ 8 files changed, 32 insertions(+), 34 deletions(-) rename lib/rbnacl/aead/{aead.rb => base.rb} (98%) rename lib/rbnacl/aead/{chacha20poly1305.rb => chacha20poly1305_legacy.rb} (96%) create mode 100644 spec/rbnacl/aead/chacha20poly1305_legacy_spec.rb delete mode 100644 spec/rbnacl/aead/chacha20poly1305_orig_spec.rb diff --git a/lib/rbnacl.rb b/lib/rbnacl.rb index e714ad9..228e1f2 100644 --- a/lib/rbnacl.rb +++ b/lib/rbnacl.rb @@ -12,7 +12,7 @@ require "rbnacl/simple_box" require "rbnacl/test_vectors" require "rbnacl/init" -require "rbnacl/aead/aead" +require "rbnacl/aead/base" # NaCl/libsodium for Ruby module RbNaCl @@ -79,7 +79,7 @@ class BadAuthenticatorError < CryptoError; end require "rbnacl/hmac/sha512" # AEAD: ChaCha20-Poly1305 - require "rbnacl/aead/chacha20poly1305" + require "rbnacl/aead/chacha20poly1305_legacy" require "rbnacl/aead/chacha20poly1305_ietf" # diff --git a/lib/rbnacl/aead/aead.rb b/lib/rbnacl/aead/base.rb similarity index 98% rename from lib/rbnacl/aead/aead.rb rename to lib/rbnacl/aead/base.rb index 716d00f..d002eab 100644 --- a/lib/rbnacl/aead/aead.rb +++ b/lib/rbnacl/aead/base.rb @@ -3,14 +3,14 @@ module RbNaCl module AEAD - # Authenticated Encryption with Additional Data + # Abstract base class for Authenticated Encryption with Additional Data # # This construction encrypts a message, and computes an authentication # tag for the encrypted message and some optional additional data # # RbNaCl provides wrappers for both ChaCha20-Poly1305 AEAD implementations # in libsodium: the original, and the IETF version. - class GenericAEAD + class Base # Number of bytes in a valid key KEYBYTES = 0 diff --git a/lib/rbnacl/aead/chacha20poly1305_ietf.rb b/lib/rbnacl/aead/chacha20poly1305_ietf.rb index 91159bc..82016ae 100644 --- a/lib/rbnacl/aead/chacha20poly1305_ietf.rb +++ b/lib/rbnacl/aead/chacha20poly1305_ietf.rb @@ -5,7 +5,7 @@ module RbNaCl module AEAD # This class contains wrappers for the IETF implementation of # Authenticated Encryption with Additional Data using ChaCha20-Poly1305 - class Chacha20Poly1305IETF < GenericAEAD + class ChaCha20Poly1305IETF < RbNaCl::AEAD::Base extend Sodium if Sodium::Version.supported_version?("1.0.9") sodium_type :aead diff --git a/lib/rbnacl/aead/chacha20poly1305.rb b/lib/rbnacl/aead/chacha20poly1305_legacy.rb similarity index 96% rename from lib/rbnacl/aead/chacha20poly1305.rb rename to lib/rbnacl/aead/chacha20poly1305_legacy.rb index 69c315a..24b631e 100644 --- a/lib/rbnacl/aead/chacha20poly1305.rb +++ b/lib/rbnacl/aead/chacha20poly1305_legacy.rb @@ -5,7 +5,7 @@ module RbNaCl module AEAD # This class contains wrappers for the original libsodium implementation of # Authenticated Encryption with Additional Data using ChaCha20-Poly1305 - class Chacha20Poly1305 < GenericAEAD + class ChaCha20Poly1305Legacy < RbNaCl::AEAD::Base extend Sodium sodium_type :aead diff --git a/spec/rbnacl/aead/chacha20poly1305_ietf_spec.rb b/spec/rbnacl/aead/chacha20poly1305_ietf_spec.rb index e0d8937..2271726 100644 --- a/spec/rbnacl/aead/chacha20poly1305_ietf_spec.rb +++ b/spec/rbnacl/aead/chacha20poly1305_ietf_spec.rb @@ -1,16 +1,14 @@ # encoding: binary # frozen_string_literal: true -RSpec.describe RbNaCl::AEAD::Chacha20Poly1305IETF do +RSpec.describe RbNaCl::AEAD::ChaCha20Poly1305IETF do if RbNaCl::Sodium::Version.supported_version?("1.0.9") include_examples "aead" do - let(:key) {vector :aead_chacha20poly1305_ietf_key} - let(:message) {vector :aead_chacha20poly1305_ietf_message} - let(:nonce) {vector :aead_chacha20poly1305_ietf_nonce} - let(:ad) {vector :aead_chacha20poly1305_ietf_ad} - let(:ciphertext) {vector :aead_chacha20poly1305_ietf_ciphertext} - - let(:aead) { RbNaCl::AEAD::Chacha20Poly1305IETF.new(key) } + let(:key) { vector :aead_chacha20poly1305_ietf_key } + let(:message) { vector :aead_chacha20poly1305_ietf_message } + let(:nonce) { vector :aead_chacha20poly1305_ietf_nonce } + let(:ad) { vector :aead_chacha20poly1305_ietf_ad } + let(:ciphertext) { vector :aead_chacha20poly1305_ietf_ciphertext } end end end diff --git a/spec/rbnacl/aead/chacha20poly1305_legacy_spec.rb b/spec/rbnacl/aead/chacha20poly1305_legacy_spec.rb new file mode 100644 index 0000000..50dba7f --- /dev/null +++ b/spec/rbnacl/aead/chacha20poly1305_legacy_spec.rb @@ -0,0 +1,12 @@ +# encoding: binary +# frozen_string_literal: true + +RSpec.describe RbNaCl::AEAD::ChaCha20Poly1305Legacy do + include_examples "aead" do + let(:key) { vector :aead_chacha20poly1305_orig_key } + let(:message) { vector :aead_chacha20poly1305_orig_message } + let(:nonce) { vector :aead_chacha20poly1305_orig_nonce } + let(:ad) { vector :aead_chacha20poly1305_orig_ad } + let(:ciphertext) { vector :aead_chacha20poly1305_orig_ciphertext } + end +end diff --git a/spec/rbnacl/aead/chacha20poly1305_orig_spec.rb b/spec/rbnacl/aead/chacha20poly1305_orig_spec.rb deleted file mode 100644 index 78930a1..0000000 --- a/spec/rbnacl/aead/chacha20poly1305_orig_spec.rb +++ /dev/null @@ -1,14 +0,0 @@ -# encoding: binary -# frozen_string_literal: true - -RSpec.describe RbNaCl::AEAD::Chacha20Poly1305 do - include_examples "aead" do - let(:key) {vector :aead_chacha20poly1305_orig_key} - let(:message) {vector :aead_chacha20poly1305_orig_message} - let(:nonce) {vector :aead_chacha20poly1305_orig_nonce} - let(:ad) {vector :aead_chacha20poly1305_orig_ad} - let(:ciphertext) {vector :aead_chacha20poly1305_orig_ciphertext} - - let(:aead) { RbNaCl::AEAD::Chacha20Poly1305.new(key) } - end -end diff --git a/spec/shared/aead.rb b/spec/shared/aead.rb index 554d320..56560fb 100644 --- a/spec/shared/aead.rb +++ b/spec/shared/aead.rb @@ -2,13 +2,15 @@ # frozen_string_literal: true RSpec.shared_examples "aead" do - let(:corrupt_ciphertext) { ciphertext.succ} - let(:trunc_ciphertext) { ciphertext[0, 20]} - let(:invalid_nonce) { nonce[0, nonce.bytesize/2] } # too short! + let(:corrupt_ciphertext) { ciphertext.succ } + let(:trunc_ciphertext) { ciphertext[0, 20] } + let(:invalid_nonce) { nonce[0, nonce.bytesize/2] } # too short! let(:invalid_nonce_long) { nonce + nonce } # too long! - let(:nonce_error_regex) { /Nonce.*(Expected #{aead.nonce_bytes})/ } - let(:corrupt_ad) {ad.succ} - let(:trunc_ad) {ad[0, ad.bytesize/2]} + let(:nonce_error_regex) { %r{Nonce.*(Expected #{aead.nonce_bytes})} } + let(:corrupt_ad) { ad.succ } + let(:trunc_ad) { ad[0, ad.bytesize/2] } + + let(:aead) { described_class.new(key) } context "new" do it "accepts strings" do