-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Support ChaChaPoly1305 on Android if available #52814
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cf96d67
Initial support for ChaChaPoly1305 on Android
vcsjones 3826605
Implement IsSupported correctly for ChaCha20Poly1305 on Android
vcsjones 5db9b38
Don't describe the exception in an expected-exception path.
vcsjones b4dda52
Detect Android SDK version for ChaChaPoly1305 IsSupported.
vcsjones 504ada0
Refactor "HasTag" implementation.
vcsjones 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
159 changes: 159 additions & 0 deletions
159
...rity.Cryptography.Algorithms/src/System/Security/Cryptography/ChaCha20Poly1305.Android.cs
This file contains hidden or 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,159 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Diagnostics; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using Microsoft.Win32.SafeHandles; | ||
|
|
||
| namespace System.Security.Cryptography | ||
| { | ||
| public sealed partial class ChaCha20Poly1305 | ||
| { | ||
| private SafeEvpCipherCtxHandle _ctxHandle; | ||
|
|
||
| public static bool IsSupported { get; } = Interop.Crypto.CipherIsSupported(Interop.Crypto.EvpChaCha20Poly1305()); | ||
|
|
||
| [MemberNotNull(nameof(_ctxHandle))] | ||
| private void ImportKey(ReadOnlySpan<byte> key) | ||
| { | ||
| // Constructors should check key size before calling ImportKey. | ||
| Debug.Assert(key.Length == KeySizeInBytes); | ||
| _ctxHandle = Interop.Crypto.EvpCipherCreatePartial(Interop.Crypto.EvpChaCha20Poly1305()); | ||
|
|
||
| Interop.Crypto.CheckValidOpenSslHandle(_ctxHandle); | ||
| Interop.Crypto.EvpCipherSetKeyAndIV( | ||
| _ctxHandle, | ||
| key, | ||
| Span<byte>.Empty, | ||
| Interop.Crypto.EvpCipherDirection.NoChange); | ||
|
|
||
| Interop.Crypto.CipherSetNonceLength(_ctxHandle, NonceSizeInBytes); | ||
| } | ||
|
|
||
| private void EncryptCore( | ||
| ReadOnlySpan<byte> nonce, | ||
| ReadOnlySpan<byte> plaintext, | ||
| Span<byte> ciphertext, | ||
| Span<byte> tag, | ||
| ReadOnlySpan<byte> associatedData = default) | ||
| { | ||
|
|
||
| Interop.Crypto.EvpCipherSetKeyAndIV( | ||
| _ctxHandle, | ||
| Span<byte>.Empty, | ||
| nonce, | ||
| Interop.Crypto.EvpCipherDirection.Encrypt); | ||
|
|
||
| if (!associatedData.IsEmpty) | ||
| { | ||
| Interop.Crypto.CipherUpdateAAD(_ctxHandle, associatedData); | ||
| } | ||
|
|
||
| byte[]? rented = null; | ||
| int ciphertextAndTagLength = checked(ciphertext.Length + tag.Length); | ||
|
|
||
| try | ||
| { | ||
| // Arbitrary limit. | ||
| const int StackAllocMax = 128; | ||
| Span<byte> ciphertextAndTag = stackalloc byte[StackAllocMax]; | ||
|
|
||
| if (ciphertextAndTagLength > StackAllocMax) | ||
| { | ||
| rented = CryptoPool.Rent(ciphertextAndTagLength); | ||
| ciphertextAndTag = rented; | ||
| } | ||
|
|
||
| ciphertextAndTag = ciphertextAndTag.Slice(0, ciphertextAndTagLength); | ||
|
|
||
| if (!Interop.Crypto.EvpCipherUpdate(_ctxHandle, ciphertextAndTag, out int ciphertextBytesWritten, plaintext)) | ||
| { | ||
| throw new CryptographicException(); | ||
| } | ||
|
|
||
| if (!Interop.Crypto.EvpCipherFinalEx( | ||
| _ctxHandle, | ||
| ciphertextAndTag.Slice(ciphertextBytesWritten), | ||
| out int bytesWritten)) | ||
| { | ||
| throw new CryptographicException(); | ||
| } | ||
|
|
||
| ciphertextBytesWritten += bytesWritten; | ||
|
|
||
| // NOTE: Android appends tag to the end of the ciphertext in case of ChaCha20Poly1305 and "encryption" mode | ||
|
|
||
| if (ciphertextBytesWritten != ciphertextAndTagLength) | ||
| { | ||
| Debug.Fail($"ChaCha20Poly1305 encrypt wrote {ciphertextBytesWritten} of {ciphertextAndTagLength} bytes."); | ||
| throw new CryptographicException(); | ||
| } | ||
|
|
||
| ciphertextAndTag.Slice(0, ciphertext.Length).CopyTo(ciphertext); | ||
| ciphertextAndTag.Slice(ciphertext.Length).CopyTo(tag); | ||
| } | ||
| finally | ||
| { | ||
| if (rented is not null) | ||
| { | ||
| CryptoPool.Return(rented, clearSize: ciphertextAndTagLength); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void DecryptCore( | ||
| ReadOnlySpan<byte> nonce, | ||
| ReadOnlySpan<byte> ciphertext, | ||
| ReadOnlySpan<byte> tag, | ||
| Span<byte> plaintext, | ||
| ReadOnlySpan<byte> associatedData) | ||
| { | ||
| Interop.Crypto.EvpCipherSetKeyAndIV( | ||
| _ctxHandle, | ||
| ReadOnlySpan<byte>.Empty, | ||
| nonce, | ||
| Interop.Crypto.EvpCipherDirection.Decrypt); | ||
|
|
||
| if (!associatedData.IsEmpty) | ||
| { | ||
| Interop.Crypto.CipherUpdateAAD(_ctxHandle, associatedData); | ||
| } | ||
|
|
||
| if (!Interop.Crypto.EvpCipherUpdate(_ctxHandle, plaintext, out int plaintextBytesWritten, ciphertext)) | ||
| { | ||
| CryptographicOperations.ZeroMemory(plaintext); | ||
| throw new CryptographicException(); | ||
| } | ||
|
|
||
| if (!Interop.Crypto.EvpCipherUpdate(_ctxHandle, plaintext.Slice(plaintextBytesWritten), out int bytesWritten, tag)) | ||
| { | ||
| CryptographicOperations.ZeroMemory(plaintext); | ||
| throw new CryptographicException(); | ||
| } | ||
|
|
||
| plaintextBytesWritten += bytesWritten; | ||
|
|
||
| if (!Interop.Crypto.EvpCipherFinalEx( | ||
| _ctxHandle, | ||
| plaintext.Slice(plaintextBytesWritten), | ||
| out bytesWritten)) | ||
| { | ||
| CryptographicOperations.ZeroMemory(plaintext); | ||
| throw new CryptographicException(SR.Cryptography_AuthTagMismatch); | ||
| } | ||
bartonjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| plaintextBytesWritten += bytesWritten; | ||
|
|
||
| if (plaintextBytesWritten != plaintext.Length) | ||
| { | ||
| Debug.Fail($"ChaCha20Poly1305 decrypt wrote {plaintextBytesWritten} of {plaintext.Length} bytes."); | ||
| throw new CryptographicException(); | ||
| } | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| _ctxHandle.Dispose(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.