-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Implement X25519 for Android #129129
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
Open
vcsjones
wants to merge
11
commits into
dotnet:main
Choose a base branch
from
vcsjones:x25519-android-3
base: main
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
Implement X25519 for Android #129129
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7992879
Implement Android X25519
vcsjones 599ce17
Extract SPKI logic out
vcsjones 3f26ad2
Make the same change for PKCS#8
vcsjones efa27dc
Refactor a bit, descope unsafe
vcsjones b72dbd1
Use correct algorithm name
vcsjones 97346da
Fix private key decoding.
vcsjones e81a670
fix variable name
vcsjones ba9c15e
More robust error handling
vcsjones 020b966
Better comment
vcsjones 3c6386b
Implement proper one-shot key agreement with public key bytes
vcsjones 52e34e8
Code review feedback, be more defensive when ToGRef fails
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
221 changes: 221 additions & 0 deletions
221
.../Common/src/Interop/Android/System.Security.Cryptography.Native.Android/Interop.X25519.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,221 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Runtime.InteropServices; | ||
| using System.Security.Cryptography; | ||
|
|
||
| internal static partial class Interop | ||
| { | ||
| internal static partial class AndroidCrypto | ||
| { | ||
| [LibraryImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X25519IsSupported")] | ||
| [return: MarshalAs(UnmanagedType.Bool)] | ||
| internal static partial bool X25519IsSupported(); | ||
|
|
||
| [LibraryImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X25519DestroyKey")] | ||
| internal static partial void X25519DestroyKey(IntPtr key); | ||
|
|
||
| [LibraryImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X25519GenerateKey")] | ||
| private static partial int X25519GenerateKeyNative( | ||
| out SafeX25519PublicKeyHandle publicKey, | ||
| out SafeX25519PrivateKeyHandle privateKey); | ||
|
|
||
| internal static void X25519GenerateKey( | ||
| out SafeX25519PublicKeyHandle publicKey, | ||
| out SafeX25519PrivateKeyHandle privateKey) | ||
| { | ||
| const int Success = 1; | ||
|
|
||
| int result = X25519GenerateKeyNative(out publicKey, out privateKey); | ||
|
|
||
| if (result != Success) | ||
| { | ||
| publicKey.Dispose(); | ||
| privateKey.Dispose(); | ||
| throw new CryptographicException(); | ||
| } | ||
| } | ||
|
|
||
| [LibraryImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X25519ExportSubjectPublicKeyInfo")] | ||
| private static partial int X25519ExportSubjectPublicKeyInfoNative( | ||
| SafeX25519PublicKeyHandle publicKey, | ||
| Span<byte> buffer, | ||
| int bufferLength, | ||
| out int bytesWritten); | ||
|
|
||
| internal static bool X25519TryExportSubjectPublicKeyInfo( | ||
| SafeX25519PublicKeyHandle publicKey, | ||
| Span<byte> buffer, | ||
| out int bytesWritten) | ||
| { | ||
| const int Success = 1; | ||
| const int InsufficientBuffer = -1; | ||
|
|
||
| int result = X25519ExportSubjectPublicKeyInfoNative( | ||
| publicKey, | ||
| buffer, | ||
| buffer.Length, | ||
| out bytesWritten); | ||
|
|
||
| return result switch | ||
| { | ||
| Success => true, | ||
| InsufficientBuffer => false, | ||
| _ => throw new CryptographicException(), | ||
| }; | ||
| } | ||
|
|
||
| [LibraryImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X25519ExportPkcs8PrivateKey")] | ||
| private static partial int X25519ExportPkcs8PrivateKeyNative( | ||
| SafeX25519PrivateKeyHandle privateKey, | ||
| Span<byte> buffer, | ||
| int bufferLength, | ||
| out int bytesWritten); | ||
|
|
||
| internal static bool X25519TryExportPkcs8PrivateKey( | ||
| SafeX25519PrivateKeyHandle privateKey, | ||
| Span<byte> buffer, | ||
| out int bytesWritten) | ||
| { | ||
| const int Success = 1; | ||
| const int InsufficientBuffer = -1; | ||
|
|
||
| int result = X25519ExportPkcs8PrivateKeyNative( | ||
| privateKey, | ||
| buffer, | ||
| buffer.Length, | ||
| out bytesWritten); | ||
|
|
||
| return result switch | ||
| { | ||
| Success => true, | ||
| InsufficientBuffer => false, | ||
| _ => throw new CryptographicException(), | ||
| }; | ||
| } | ||
|
|
||
| [LibraryImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X25519ImportSubjectPublicKeyInfo")] | ||
| private static partial SafeX25519PublicKeyHandle X25519ImportSubjectPublicKeyInfoNative( | ||
| ReadOnlySpan<byte> buffer, | ||
| int bufferLength); | ||
|
|
||
| internal static SafeX25519PublicKeyHandle X25519ImportSubjectPublicKeyInfo(ReadOnlySpan<byte> spki) | ||
| { | ||
| SafeX25519PublicKeyHandle handle = X25519ImportSubjectPublicKeyInfoNative(spki, spki.Length); | ||
|
|
||
| if (handle.IsInvalid) | ||
| { | ||
| handle.Dispose(); | ||
| throw new CryptographicException(SR.Cryptography_NotValidPublicOrPrivateKey); | ||
| } | ||
|
|
||
| return handle; | ||
| } | ||
|
|
||
| [LibraryImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X25519ImportPkcs8PrivateKey")] | ||
| private static partial SafeX25519PrivateKeyHandle X25519ImportPkcs8PrivateKeyNative( | ||
| ReadOnlySpan<byte> buffer, | ||
| int bufferLength); | ||
|
|
||
| internal static SafeX25519PrivateKeyHandle X25519ImportPkcs8PrivateKey(ReadOnlySpan<byte> pkcs8) | ||
| { | ||
| SafeX25519PrivateKeyHandle handle = X25519ImportPkcs8PrivateKeyNative(pkcs8, pkcs8.Length); | ||
|
|
||
| if (handle.IsInvalid) | ||
| { | ||
| handle.Dispose(); | ||
| throw new CryptographicException(SR.Cryptography_NotValidPublicOrPrivateKey); | ||
| } | ||
|
|
||
| return handle; | ||
| } | ||
|
|
||
| [LibraryImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X25519DeriveSecret")] | ||
| private static partial int X25519DeriveSecretNative( | ||
| SafeX25519PrivateKeyHandle privateKey, | ||
| SafeX25519PublicKeyHandle publicKey, | ||
| Span<byte> destination, | ||
| int destinationLength); | ||
|
|
||
| internal static void X25519DeriveSecret( | ||
| SafeX25519PrivateKeyHandle privateKey, | ||
| SafeX25519PublicKeyHandle publicKey, | ||
| Span<byte> destination) | ||
| { | ||
| const int Success = 1; | ||
|
|
||
| int result = X25519DeriveSecretNative(privateKey, publicKey, destination, destination.Length); | ||
|
|
||
| if (result != Success) | ||
| { | ||
| throw new CryptographicException(); | ||
| } | ||
| } | ||
|
|
||
| [LibraryImport(Libraries.AndroidCryptoNative, EntryPoint = "AndroidCryptoNative_X25519DeriveSecretWithSubjectPublicKeyInfo")] | ||
| private static partial int X25519DeriveSecretWithSubjectPublicKeyInfoNative( | ||
| SafeX25519PrivateKeyHandle privateKey, | ||
| ReadOnlySpan<byte> subjectPublicKeyInfo, | ||
| int subjectPublicKeyInfoLength, | ||
| Span<byte> destination, | ||
| int destinationLength); | ||
|
|
||
| internal static void X25519DeriveSecretWithSubjectPublicKeyInfo( | ||
| SafeX25519PrivateKeyHandle privateKey, | ||
| ReadOnlySpan<byte> subjectPublicKeyInfo, | ||
| Span<byte> destination) | ||
| { | ||
| const int Success = 1; | ||
|
|
||
| int result = X25519DeriveSecretWithSubjectPublicKeyInfoNative( | ||
| privateKey, | ||
| subjectPublicKeyInfo, | ||
| subjectPublicKeyInfo.Length, | ||
| destination, | ||
| destination.Length); | ||
|
|
||
| if (result != Success) | ||
| { | ||
| throw new CryptographicException(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| namespace System.Security.Cryptography | ||
| { | ||
| internal sealed class SafeX25519PublicKeyHandle : SafeHandle | ||
| { | ||
| public SafeX25519PublicKeyHandle() | ||
| : base(IntPtr.Zero, ownsHandle: true) | ||
| { | ||
| } | ||
|
|
||
| public override bool IsInvalid => handle == IntPtr.Zero; | ||
|
|
||
| protected override bool ReleaseHandle() | ||
| { | ||
| Interop.AndroidCrypto.X25519DestroyKey(handle); | ||
| SetHandle(IntPtr.Zero); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| internal sealed class SafeX25519PrivateKeyHandle : SafeHandle | ||
| { | ||
| public SafeX25519PrivateKeyHandle() | ||
| : base(IntPtr.Zero, ownsHandle: true) | ||
| { | ||
| } | ||
|
|
||
| public override bool IsInvalid => handle == IntPtr.Zero; | ||
|
|
||
| protected override bool ReleaseHandle() | ||
| { | ||
| Interop.AndroidCrypto.X25519DestroyKey(handle); | ||
| SetHandle(IntPtr.Zero); | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.