-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from AnIrishDuck/master
Add support for XChaCha20-Poly1305
- Loading branch information
Showing
4 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# encoding: binary | ||
# frozen_string_literal: true | ||
|
||
module RbNaCl | ||
module AEAD | ||
# This class contains wrappers for the IETF implementation of | ||
# Authenticated Encryption with Additional Data using ChaCha20-Poly1305 | ||
class XChaCha20Poly1305IETF < RbNaCl::AEAD::Base | ||
extend Sodium | ||
if Sodium::Version.supported_version?("1.0.12") | ||
sodium_type :aead | ||
sodium_primitive :xchacha20poly1305_ietf | ||
|
||
sodium_constant :KEYBYTES | ||
sodium_constant :NPUBBYTES | ||
sodium_constant :ABYTES | ||
|
||
sodium_function :aead_xchacha20poly1305_ietf_encrypt, | ||
:crypto_aead_xchacha20poly1305_ietf_encrypt, | ||
%i[pointer pointer pointer ulong_long pointer ulong_long pointer pointer pointer] | ||
|
||
sodium_function :aead_xchacha20poly1305_ietf_decrypt, | ||
:crypto_aead_xchacha20poly1305_ietf_decrypt, | ||
%i[pointer pointer pointer pointer ulong_long pointer ulong_long pointer pointer] | ||
|
||
private | ||
|
||
def do_encrypt(ciphertext, ciphertext_len, nonce, message, additional_data) | ||
self.class.aead_xchacha20poly1305_ietf_encrypt(ciphertext, ciphertext_len, | ||
message, data_len(message), | ||
additional_data, data_len(additional_data), | ||
nil, nonce, @key) | ||
end | ||
|
||
def do_decrypt(message, message_len, nonce, ciphertext, additional_data) | ||
self.class.aead_xchacha20poly1305_ietf_decrypt(message, message_len, nil, | ||
ciphertext, data_len(ciphertext), | ||
additional_data, data_len(additional_data), | ||
nonce, @key) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# encoding: binary | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RbNaCl::AEAD::XChaCha20Poly1305IETF do | ||
if RbNaCl::Sodium::Version.supported_version?("1.0.12") | ||
include_examples "aead" do | ||
let(:key) { vector :aead_xchacha20poly1305_ietf_key } | ||
let(:message) { vector :aead_xchacha20poly1305_ietf_message } | ||
let(:nonce) { vector :aead_xchacha20poly1305_ietf_nonce } | ||
let(:ad) { vector :aead_xchacha20poly1305_ietf_ad } | ||
let(:ciphertext) { vector :aead_xchacha20poly1305_ietf_ciphertext } | ||
end | ||
end | ||
end |