-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/develop'
- Loading branch information
Showing
25 changed files
with
541 additions
and
394 deletions.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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,61 @@ | ||
module JOSE::JWA::XChaCha20Poly1305 | ||
|
||
extend self | ||
|
||
MUTEX = Mutex.new | ||
|
||
@__implementations__ = [] | ||
@__ruby_implementations__ = [] | ||
|
||
def __implementation__ | ||
return MUTEX.synchronize { @__implementation__ ||= __pick_best_implementation__ } | ||
end | ||
|
||
def __implementation__=(implementation) | ||
return MUTEX.synchronize { @__implementation__ = implementation } | ||
end | ||
|
||
def __register__(implementation, ruby = false) | ||
MUTEX.synchronize { | ||
if ruby | ||
@__ruby_implementations__.unshift(implementation) | ||
else | ||
@__implementations__.unshift(implementation) | ||
end | ||
__config_change__(false) | ||
implementation | ||
} | ||
end | ||
|
||
def __config_change__(lock = true) | ||
MUTEX.lock if lock | ||
@__implementation__ ||= nil | ||
@__implementation__ = __pick_best_implementation__ if @__implementation__.nil? or @__implementation__.__ruby__? or not @__implementation__.__supported__? | ||
MUTEX.unlock if lock | ||
end | ||
|
||
def xchacha20poly1305_aead_encrypt(key, nonce, aad, plaintext) | ||
return (@__implementation__ || __implementation__).xchacha20poly1305_aead_encrypt(key, nonce, aad, plaintext) | ||
end | ||
|
||
def xchacha20poly1305_aead_decrypt(key, nonce, aad, ciphertext, tag) | ||
return (@__implementation__ || __implementation__).xchacha20poly1305_aead_decrypt(key, nonce, aad, ciphertext, tag) | ||
end | ||
|
||
private | ||
def __pick_best_implementation__ | ||
implementation = nil | ||
implementation = @__implementations__.detect do |mod| | ||
next mod.__supported__? | ||
end | ||
implementation ||= @__ruby_implementations__.detect do |mod| | ||
next mod.__supported__? | ||
end | ||
implementation ||= JOSE::JWA::XChaCha20Poly1305_Unsupported | ||
return implementation | ||
end | ||
|
||
end | ||
|
||
require 'jose/jwa/xchacha20poly1305_unsupported' | ||
require 'jose/jwa/xchacha20poly1305_rbnacl' |
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,35 @@ | ||
module JOSE::JWA::XChaCha20Poly1305_RbNaCl | ||
|
||
extend self | ||
|
||
def __ruby__?; false; end | ||
|
||
def __supported__? | ||
return @supported ||= begin | ||
begin | ||
require 'rbnacl/libsodium' | ||
rescue LoadError | ||
end | ||
begin | ||
require 'rbnacl' | ||
rescue LoadError | ||
end | ||
!!(defined?(RbNaCl::AEAD::XChaCha20Poly1305IETF)) | ||
end | ||
end | ||
|
||
def xchacha20poly1305_aead_encrypt(key, nonce, aad, plaintext) | ||
cipher = RbNaCl::AEAD::XChaCha20Poly1305IETF.new(key) | ||
ciphertext_with_tag = cipher.encrypt(nonce, plaintext, aad) | ||
return [ciphertext_with_tag[0..-17], ciphertext_with_tag[-16..-1]] | ||
end | ||
|
||
def xchacha20poly1305_aead_decrypt(key, nonce, aad, ciphertext, tag) | ||
cipher = RbNaCl::AEAD::XChaCha20Poly1305IETF.new(key) | ||
ciphertext_with_tag = [ciphertext, tag].join() | ||
return cipher.decrypt(nonce, ciphertext_with_tag, aad) | ||
end | ||
|
||
end | ||
|
||
JOSE::JWA::XChaCha20Poly1305.__register__(JOSE::JWA::XChaCha20Poly1305_RbNaCl) |
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,16 @@ | ||
module JOSE::JWA::XChaCha20Poly1305_Unsupported | ||
|
||
extend self | ||
|
||
def __ruby__?; true; end | ||
def __supported__?; false; end | ||
|
||
def xchacha20poly1305_aead_encrypt(key, nonce, aad, plaintext) | ||
raise NotImplementedError | ||
end | ||
|
||
def xchacha20poly1305_aead_decrypt(key, nonce, aad, ciphertext, tag) | ||
raise NotImplementedError | ||
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
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
Oops, something went wrong.