-
Notifications
You must be signed in to change notification settings - Fork 5.4k
X25519DiffieHellman agreement with byte-based public keys #128086
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
94b358e
8347d5f
8aef0d6
38a3aa3
35d0ccb
4820cfd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,8 +19,9 @@ private X25519DiffieHellmanImplementation(SafeX25519KeyHandle key, bool hasPriva | |||||||||||
| _hasPrivate = hasPrivate; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| protected override unsafe void DeriveRawSecretAgreementCore(X25519DiffieHellman otherParty, Span<byte> destination) | ||||||||||||
| protected override void DeriveRawSecretAgreementCore(X25519DiffieHellman otherParty, Span<byte> destination) | ||||||||||||
| { | ||||||||||||
| Debug.Assert(destination.Length == SecretAgreementSizeInBytes); | ||||||||||||
| ThrowIfPrivateNeeded(); | ||||||||||||
|
|
||||||||||||
| if (otherParty is X25519DiffieHellmanImplementation x25519impl) | ||||||||||||
|
|
@@ -29,16 +30,23 @@ protected override unsafe void DeriveRawSecretAgreementCore(X25519DiffieHellman | |||||||||||
| } | ||||||||||||
| else | ||||||||||||
| { | ||||||||||||
| Span<byte> publicKeyBuffer = stackalloc byte[PublicKeySizeInBytes]; | ||||||||||||
| otherParty.ExportPublicKey(publicKeyBuffer); | ||||||||||||
|
|
||||||||||||
| using (SafeX25519KeyHandle publicKey = Interop.AppleCrypto.X25519ImportPublicKey(publicKeyBuffer)) | ||||||||||||
| unsafe | ||||||||||||
| { | ||||||||||||
| Interop.AppleCrypto.X25519DeriveRawSecretAgreement(_key, publicKey, destination); | ||||||||||||
| Span<byte> publicKeyBuffer = stackalloc byte[PublicKeySizeInBytes]; | ||||||||||||
| otherParty.ExportPublicKey(publicKeyBuffer); | ||||||||||||
| Interop.AppleCrypto.X25519DeriveRawSecretAgreementWithBytes(_key, publicKeyBuffer, destination); | ||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally, I'd write it as export then call DeriveRawSecretAgreementCore, but the logic is simple, so 🤷 The JIT would probably eat the redundant call to ThrowIfPrivateNeeded. |
||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| protected override void DeriveRawSecretAgreementCore(ReadOnlySpan<byte> otherPartyPublicKey, Span<byte> destination) | ||||||||||||
| { | ||||||||||||
| Debug.Assert(otherPartyPublicKey.Length == PublicKeySizeInBytes); | ||||||||||||
| Debug.Assert(destination.Length == SecretAgreementSizeInBytes); | ||||||||||||
| ThrowIfPrivateNeeded(); | ||||||||||||
| Interop.AppleCrypto.X25519DeriveRawSecretAgreementWithBytes(_key, otherPartyPublicKey, destination); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| protected override void ExportPrivateKeyCore(Span<byte> destination) | ||||||||||||
| { | ||||||||||||
| ThrowIfPrivateNeeded(); | ||||||||||||
|
|
@@ -92,5 +100,6 @@ private void ThrowIfPrivateNeeded() | |||||||||||
| if (!_hasPrivate) | ||||||||||||
| throw new CryptographicException(SR.Cryptography_CSP_NoPrivateKey); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| } | ||||||||||||
|
Comment on lines
102
to
104
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Nit: stray blank line |
||||||||||||
| } | ||||||||||||
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.
It feels a little strange that in Apple you called it WithBytes and in OSSL you called it WithPublicKey.
Doesn't really matter, but stood out as weird.