-
Notifications
You must be signed in to change notification settings - Fork 9
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
Remove libsodium #86
Open
sgessa
wants to merge
6
commits into
Ianleeclark:develop
Choose a base branch
from
sgessa:feature/remove-libsodium
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Remove libsodium #86
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0b501e0
Implement native XChaCha20Poly1305
sgessa a726950
Implement native Ed25519
sgessa 0dc268b
Fix Ed25519.seed_keypair/1
sgessa 9a4a454
Update README
sgessa 82b70bb
Update README
sgessa bc6734d
Install hexpm in CI
sgessa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
elixir 1.13 | ||
erlang 24.1 | ||
erlang 24.3.4.14 |
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,36 @@ | ||
defmodule Paseto.Crypto.Ed25519 do | ||
@moduledoc """ | ||
Wrap crypto erlang functions to work with ED25519 | ||
""" | ||
|
||
@dialyzer {:nowarn_function, sign: 2} | ||
@spec sign(binary(), binary()) :: {:ok, binary()} | ||
def sign(message, private_key) when byte_size(private_key) == 32 do | ||
{:ok, ^private_key, public_key} = seed_keypair(private_key) | ||
signature = :public_key.sign(message, :none, {:ed_pri, :ed25519, public_key, private_key}) | ||
{:ok, signature} | ||
end | ||
|
||
@dialyzer {:nowarn_function, verify_detached: 3} | ||
@spec verify_detached(binary(), binary(), binary()) :: :ok | {:error, :invalid_signature} | ||
def verify_detached(message, signature, public_key) | ||
when byte_size(signature) == 64 and byte_size(public_key) == 32 do | ||
if :public_key.verify(message, :none, signature, {:ed_pub, :ed25519, public_key}) do | ||
:ok | ||
else | ||
{:error, :invalid_signature} | ||
end | ||
end | ||
|
||
@spec seed_keypair(binary()) :: {:ok, binary(), binary()} | ||
def seed_keypair(private_key) when byte_size(private_key) == 32 do | ||
{pk, sk} = :crypto.generate_key(:eddsa, :ed25519, private_key) | ||
{:ok, sk, pk} | ||
end | ||
|
||
@spec generate_keypair() :: {:ok, binary(), binary()} | ||
def generate_keypair do | ||
{pk, sk} = :crypto.generate_key(:eddsa, :ed25519) | ||
{:ok, sk, pk} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
defmodule Paseto.Crypto.XChaCha20Poly1305 do | ||
@moduledoc """ | ||
Implement XChaCha20Poly1305 encryption and decryption. | ||
""" | ||
|
||
@spec encrypt(binary(), binary(), binary(), binary()) :: {:ok, binary()} | ||
def encrypt(message, aad, <<iv::192-bits>>, <<key::256-bits>>) do | ||
# Perform the HChaCha20 operation to generate the subkey and nonce | ||
{subkey, nonce} = xchacha20_subkey_and_nonce(key, iv) | ||
|
||
# Perform the ChaCha20 operation to encrypt the message | ||
block_encrypt(subkey, nonce, {aad, message}) | ||
end | ||
|
||
@spec decrypt(binary(), binary(), binary(), binary()) :: {:ok, binary()} | ||
def decrypt(encrypted, aad, <<iv::192-bits>>, <<key::256-bits>>) do | ||
cipher_text_size = byte_size(encrypted) - 16 | ||
<<cipher_text::bytes-size(cipher_text_size), cipher_tag::128-bits>> = encrypted | ||
|
||
# Perform the HChaCha20 operation to generate the subkey and nonce | ||
{subkey, nonce} = xchacha20_subkey_and_nonce(key, iv) | ||
|
||
# Perform the ChaCha20 operation to decrypt the message | ||
block_decrypt(:chacha20_poly1305, subkey, nonce, {aad, cipher_text, cipher_tag}) | ||
end | ||
|
||
defp xchacha20_subkey_and_nonce(key, <<nonce0::128-bits, nonce1::64-bits>>) do | ||
subkey = hchacha20(key, nonce0) | ||
nonce = <<0::32, nonce1::64-bits>> | ||
{subkey, nonce} | ||
end | ||
|
||
defp hchacha20(key, nonce) do | ||
# ChaCha20 has an internal blocksize of 512-bits (64-bytes). | ||
# Let's use a Mask of random 64-bytes to blind the intermediate keystream. | ||
mask = <<mask_h::128-bits, _::256-bits, mask_t::128-bits>> = :crypto.strong_rand_bytes(64) | ||
|
||
<<state_2h::128-bits, _::256-bits, state_2t::128-bits>> = | ||
:crypto.crypto_one_time(:chacha20, key, nonce, mask, true) | ||
|
||
<< | ||
x00::32-unsigned-little-integer, | ||
x01::32-unsigned-little-integer, | ||
x02::32-unsigned-little-integer, | ||
x03::32-unsigned-little-integer, | ||
x12::32-unsigned-little-integer, | ||
x13::32-unsigned-little-integer, | ||
x14::32-unsigned-little-integer, | ||
x15::32-unsigned-little-integer | ||
>> = | ||
:crypto.exor( | ||
<<mask_h::128-bits, mask_t::128-bits>>, | ||
<<state_2h::128-bits, state_2t::128-bits>> | ||
) | ||
|
||
## The final step of ChaCha20 is `State2 = State0 + State1', so let's | ||
## recover `State1' with subtraction: `State1 = State2 - State0' | ||
<< | ||
y00::32-unsigned-little-integer, | ||
y01::32-unsigned-little-integer, | ||
y02::32-unsigned-little-integer, | ||
y03::32-unsigned-little-integer, | ||
y12::32-unsigned-little-integer, | ||
y13::32-unsigned-little-integer, | ||
y14::32-unsigned-little-integer, | ||
y15::32-unsigned-little-integer | ||
>> = <<"expand 32-byte k", nonce::128-bits>> | ||
|
||
<< | ||
x00 - y00::32-unsigned-little-integer, | ||
x01 - y01::32-unsigned-little-integer, | ||
x02 - y02::32-unsigned-little-integer, | ||
x03 - y03::32-unsigned-little-integer, | ||
x12 - y12::32-unsigned-little-integer, | ||
x13 - y13::32-unsigned-little-integer, | ||
x14 - y14::32-unsigned-little-integer, | ||
x15 - y15::32-unsigned-little-integer | ||
>> | ||
end | ||
|
||
defp block_encrypt(key, iv, {aad, payload}) do | ||
{cipher_text, cipher_tag} = | ||
:crypto.crypto_one_time_aead(:chacha20_poly1305, key, iv, payload, aad, true) | ||
|
||
{:ok, cipher_text <> cipher_tag} | ||
catch | ||
:error, :notsup -> raise_notsup() | ||
end | ||
|
||
defp block_decrypt(cipher, key, iv, {aad, payload, tag}) do | ||
plain = :crypto.crypto_one_time_aead(cipher, key, iv, payload, aad, tag, false) | ||
{:ok, plain} | ||
catch | ||
:error, :notsup -> raise_notsup() | ||
end | ||
|
||
defp raise_notsup do | ||
raise "The algorithm chacha20_poly1305 is not supported by your Erlang/OTP installation. " <> | ||
"Please make sure it was compiled with the correct OpenSSL/BoringSSL bindings" | ||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
each key in Ed25519 is 32 bytes