-
Notifications
You must be signed in to change notification settings - Fork 16
chore: Add ECDH examples #1461
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
chore: Add ECDH examples #1461
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2b9b458
chore: Add ECDH examples
josecorella 38ab8e6
cfn update
josecorella 77d0000
names are hard
josecorella b9a6692
m
josecorella 6c80f5e
m
josecorella 837c491
names
josecorella 854c984
address typo
josecorella cf4f403
add net examples
josecorella fc6c9a9
format
josecorella c320fe5
address feedback
josecorella a628e11
Merge branch 'main' into jocorell/update-examples
josecorella 351ed09
Apply suggestions from code review
josecorella 3d7c233
Merge branch 'main' into jocorell/update-examples
josecorella 0345d95
address feddback
josecorella 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
693 changes: 693 additions & 0 deletions
693
...on/src/main/java/software/amazon/cryptography/examples/keyring/KmsEcdhKeyringExample.java
Large diffs are not rendered by default.
Oops, something went wrong.
885 changes: 885 additions & 0 deletions
885
...on/src/main/java/software/amazon/cryptography/examples/keyring/RawEcdhKeyringExample.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
49 changes: 49 additions & 0 deletions
49
...rc/test/java/software/amazon/cryptography/examples/keyring/TestKmsEcdhKeyringExample.java
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,49 @@ | ||
package software.amazon.cryptography.examples.keyring; | ||
|
||
import static software.amazon.cryptography.examples.keyring.KmsEcdhKeyringExample.EXAMPLE_ECC_PUBLIC_KEY_RECIPIENT_FILENAME; | ||
import static software.amazon.cryptography.examples.keyring.KmsEcdhKeyringExample.EXAMPLE_ECC_PUBLIC_KEY_SENDER_FILENAME; | ||
import static software.amazon.cryptography.examples.keyring.KmsEcdhKeyringExample.shouldGetNewPublicKeys; | ||
import static software.amazon.cryptography.examples.keyring.KmsEcdhKeyringExample.writePublicKeyPemForEccKey; | ||
|
||
import org.testng.annotations.Test; | ||
import software.amazon.cryptography.examples.TestUtils; | ||
|
||
public class TestKmsEcdhKeyringExample { | ||
|
||
@Test | ||
public void TestKmsEcdhKeyringExampleStatic() { | ||
// You may provide your own ECC public keys at | ||
// - EXAMPLE_ECC_PUBLIC_KEY_SENDER_FILENAME | ||
// - EXAMPLE_ECC_PUBLIC_KEY_RECIPIENT_FILENAME. | ||
// If you provide these, the keys MUST be on curve P256 | ||
// This must be the public key for the ECC key represented at eccKeyArn | ||
// If this file is not present, this will write a UTF-8 encoded PEM file for you. | ||
if (shouldGetNewPublicKeys()) { | ||
writePublicKeyPemForEccKey( | ||
TestUtils.TEST_KMS_ECDH_KEY_ID_P256_SENDER, | ||
EXAMPLE_ECC_PUBLIC_KEY_SENDER_FILENAME | ||
); | ||
writePublicKeyPemForEccKey( | ||
TestUtils.TEST_KMS_ECDH_KEY_ID_P256_RECIPIENT, | ||
EXAMPLE_ECC_PUBLIC_KEY_RECIPIENT_FILENAME | ||
); | ||
} | ||
|
||
KmsEcdhKeyringExample.KmsEcdhKeyringGetItemPutItem( | ||
TestUtils.TEST_DDB_TABLE_NAME, | ||
TestUtils.TEST_KMS_ECDH_KEY_ID_P256_SENDER | ||
); | ||
} | ||
|
||
@Test | ||
public void TestKmsEcdhKeyringExampleDiscovery() { | ||
// In this example you do not need to provide the recipient ECC Public Key. | ||
// On initialization, the keyring will call KMS:getPublicKey on the configured | ||
// recipientKmsIdentifier set on the keyring. This example uses the previous example | ||
// to write an item meant for the recipient. | ||
KmsEcdhKeyringExample.KmsEcdhDiscoveryGetItem( | ||
TestUtils.TEST_DDB_TABLE_NAME, | ||
TestUtils.TEST_KMS_ECDH_KEY_ID_P256_RECIPIENT | ||
); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...rc/test/java/software/amazon/cryptography/examples/keyring/TestRawEcdhKeyringExample.java
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,83 @@ | ||
package software.amazon.cryptography.examples.keyring; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.security.spec.ECGenParameterSpec; | ||
import org.testng.annotations.Test; | ||
import software.amazon.cryptography.examples.TestUtils; | ||
import software.amazon.cryptography.primitives.model.ECDHCurveSpec; | ||
|
||
public class TestRawEcdhKeyringExample { | ||
|
||
@Test | ||
public void TestStaticRawEcdhKeyringExample() { | ||
// You may provide your own ECC Key pairs in the files located at | ||
// - EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER | ||
// - EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT | ||
// If you provide this, the keys MUST be on curve P256 | ||
// If these files are not present, this will generate a pair for you. | ||
// For this example we will use the curve P256. | ||
if (RawEcdhKeyringExample.shouldGenerateNewEccKeyPairs()) { | ||
RawEcdhKeyringExample.generateEccKeyPairs(); | ||
} | ||
|
||
// Part of using these keyrings is knowing which curve the keys used in the key agreement | ||
// lie on. The keyring will fail if the keys do not lie on the configured curve. | ||
RawEcdhKeyringExample.RawEcdhKeyringGetItemPutItem( | ||
TestUtils.TEST_DDB_TABLE_NAME, | ||
ECDHCurveSpec.ECC_NIST_P256 | ||
); | ||
} | ||
|
||
@Test | ||
public void TestEphemeralRawEcdhKeyringExample() { | ||
// You may provide your own ECC Public Key in the files located at | ||
// - EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT | ||
// If you provide this, the keys MUST be on curve P256 | ||
// If these files are not present, this will generate a pair for you. | ||
// For this example we will use the curve P256. | ||
if (RawEcdhKeyringExample.shouldGenerateNewEccKeyPairs()) { | ||
RawEcdhKeyringExample.generateEccKeyPairs(); | ||
} | ||
|
||
// Part of using these keyrings is knowing which curve the keys used in the key agreement | ||
// lie on. The keyring will fail if the keys do not lie on the configured curve. | ||
RawEcdhKeyringExample.EphemeralRawEcdhKeyringPutItem( | ||
TestUtils.TEST_DDB_TABLE_NAME, | ||
ECDHCurveSpec.ECC_NIST_P256 | ||
); | ||
} | ||
|
||
@Test | ||
public void TestDiscoveryRawEcdhKeyringExample() { | ||
// You may provide your own ECC Public Key in the files located at | ||
// - EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT | ||
// - EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT | ||
// If you provide this, the keys MUST be on curve P256 | ||
// If these files are not present, this will generate a pair for you. | ||
// For this example we will use the curve P256. | ||
if (RawEcdhKeyringExample.shouldGenerateNewEccKeyPairs()) { | ||
RawEcdhKeyringExample.generateEccKeyPairs(); | ||
} | ||
|
||
// The discovery configuration is not allowed to encrypt | ||
// To understand this example best, we will write a record with the ephemeral configuration | ||
// in the previous example. This means that the recipient public key configured on | ||
// both keyrings is the same. This means that the other party has the recipient public key | ||
// and is writing messages meant only for the owner of the recipient public key to decrypt. | ||
|
||
// In this call we are writing a record that is written with an ephemeral sender key pair. | ||
// The recipient will be able to decrypt the message | ||
RawEcdhKeyringExample.EphemeralRawEcdhKeyringPutItem( | ||
TestUtils.TEST_DDB_TABLE_NAME, | ||
ECDHCurveSpec.ECC_NIST_P256 | ||
); | ||
|
||
// In this call we are reading a record that was written with the recipient's public key. | ||
// It will use the recipient's private key and the sender's public key stored in the message to | ||
// calculate the appropriate shared secret to successfully decrypt the message. | ||
RawEcdhKeyringExample.DiscoveryRawEcdhKeyringGetItem( | ||
TestUtils.TEST_DDB_TABLE_NAME, | ||
ECDHCurveSpec.ECC_NIST_P256 | ||
); | ||
} | ||
} |
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.