|
| 1 | +package software.amazon.cryptography.examples.keyring; |
| 2 | + |
| 3 | +import java.nio.ByteBuffer; |
| 4 | +import java.security.spec.ECGenParameterSpec; |
| 5 | +import org.testng.annotations.Test; |
| 6 | +import software.amazon.cryptography.examples.TestUtils; |
| 7 | +import software.amazon.cryptography.primitives.model.ECDHCurveSpec; |
| 8 | + |
| 9 | +public class TestRawEcdhKeyringExample { |
| 10 | + |
| 11 | + @Test |
| 12 | + public void TestStaticRawEcdhKeyringExample() { |
| 13 | + // You may provide your own ECC Key pairs in the files located at |
| 14 | + // - EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER |
| 15 | + // - EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT |
| 16 | + // If you provide this, the keys MUST be on curve P256 |
| 17 | + // If these files are not present, this will generate a pair for you. |
| 18 | + // For this example we will use the curve P256. |
| 19 | + if (RawEcdhKeyringExample.shouldGenerateNewEccKeyPairs()) { |
| 20 | + RawEcdhKeyringExample.generateEccKeyPairs(); |
| 21 | + } |
| 22 | + |
| 23 | + // Part of using these keyrings is knowing which curve the keys used in the key agreement |
| 24 | + // lie on. The keyring will fail if the keys do not lie on the configured curve. |
| 25 | + RawEcdhKeyringExample.RawEcdhKeyringGetItemPutItem( |
| 26 | + TestUtils.TEST_DDB_TABLE_NAME, |
| 27 | + ECDHCurveSpec.ECC_NIST_P256 |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void TestEphemeralRawEcdhKeyringExample() { |
| 33 | + // You may provide your own ECC Public Key in the files located at |
| 34 | + // - EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT |
| 35 | + // If you provide this, the keys MUST be on curve P256 |
| 36 | + // If these files are not present, this will generate a pair for you. |
| 37 | + // For this example we will use the curve P256. |
| 38 | + if (RawEcdhKeyringExample.shouldGenerateNewEccKeyPairs()) { |
| 39 | + RawEcdhKeyringExample.generateEccKeyPairs(); |
| 40 | + } |
| 41 | + |
| 42 | + // Part of using these keyrings is knowing which curve the keys used in the key agreement |
| 43 | + // lie on. The keyring will fail if the keys do not lie on the configured curve. |
| 44 | + RawEcdhKeyringExample.EphemeralRawEcdhKeyringPutItem( |
| 45 | + TestUtils.TEST_DDB_TABLE_NAME, |
| 46 | + ECDHCurveSpec.ECC_NIST_P256 |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void TestDiscoveryRawEcdhKeyringExample() { |
| 52 | + // You may provide your own ECC Public Key in the files located at |
| 53 | + // - EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT |
| 54 | + // - EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT |
| 55 | + // If you provide this, the keys MUST be on curve P256 |
| 56 | + // If these files are not present, this will generate a pair for you. |
| 57 | + // For this example we will use the curve P256. |
| 58 | + if (RawEcdhKeyringExample.shouldGenerateNewEccKeyPairs()) { |
| 59 | + RawEcdhKeyringExample.generateEccKeyPairs(); |
| 60 | + } |
| 61 | + |
| 62 | + // The discovery configuration is not allowed to encrypt |
| 63 | + // To understand this example best, we will write a record with the ephemeral configuration |
| 64 | + // in the previous example. This means that the recipient public key configured on |
| 65 | + // both keyrings is the same. This means that the other party has the recipient public key |
| 66 | + // and is writing messages meant only for the owner of the recipient public key to decrypt. |
| 67 | + |
| 68 | + // In this call we are writing a record that is written with an ephemeral sender key pair. |
| 69 | + // The recipient will be able to decrypt the message |
| 70 | + RawEcdhKeyringExample.EphemeralRawEcdhKeyringPutItem( |
| 71 | + TestUtils.TEST_DDB_TABLE_NAME, |
| 72 | + ECDHCurveSpec.ECC_NIST_P256 |
| 73 | + ); |
| 74 | + |
| 75 | + // In this call we are reading a record that was written with the recipient's public key. |
| 76 | + // It will use the recipient's private key and the sender's public key stored in the message to |
| 77 | + // calculate the appropriate shared secret to successfully decrypt the message. |
| 78 | + RawEcdhKeyringExample.DiscoveryRawEcdhKeyringGetItem( |
| 79 | + TestUtils.TEST_DDB_TABLE_NAME, |
| 80 | + ECDHCurveSpec.ECC_NIST_P256 |
| 81 | + ); |
| 82 | + } |
| 83 | +} |
0 commit comments