Skip to content

Commit

Permalink
Merge pull request #148 from cryptosphere/last-minute-api-changes
Browse files Browse the repository at this point in the history
Last minute changes to the ChaCha20Poly1305 API
  • Loading branch information
tarcieri committed Dec 24, 2016
2 parents 1b77358 + 910af10 commit 79aba15
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 34 deletions.
4 changes: 2 additions & 2 deletions lib/rbnacl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"

#
Expand Down
4 changes: 2 additions & 2 deletions lib/rbnacl/aead/aead.rb → lib/rbnacl/aead/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/rbnacl/aead/chacha20poly1305_ietf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 6 additions & 8 deletions spec/rbnacl/aead/chacha20poly1305_ietf_spec.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions spec/rbnacl/aead/chacha20poly1305_legacy_spec.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 0 additions & 14 deletions spec/rbnacl/aead/chacha20poly1305_orig_spec.rb

This file was deleted.

14 changes: 8 additions & 6 deletions spec/shared/aead.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 79aba15

Please sign in to comment.