Skip to main content

RawPrivateKeyToStaticPublicKeyInput

Struct RawPrivateKeyToStaticPublicKeyInput 

Source
#[non_exhaustive]
pub struct RawPrivateKeyToStaticPublicKeyInput { pub recipient_public_key: Option<Blob>, pub sender_static_private_key: Option<Blob>, }
Expand description

Inputs for creating a RawPrivateKeyToStaticPublicKey Configuration.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§recipient_public_key: Option<Blob>

The recipient’s public key. MUST be DER encoded.

§sender_static_private_key: Option<Blob>

The sender’s private key. MUST be PEM encoded.

Implementations§

Source§

impl RawPrivateKeyToStaticPublicKeyInput

Source

pub fn recipient_public_key(&self) -> &Option<Blob>

The recipient’s public key. MUST be DER encoded.

Source

pub fn sender_static_private_key(&self) -> &Option<Blob>

The sender’s private key. MUST be PEM encoded.

Source§

impl RawPrivateKeyToStaticPublicKeyInput

Source

pub fn builder() -> RawPrivateKeyToStaticPublicKeyInputBuilder

Creates a new builder-style object to manufacture RawPrivateKeyToStaticPublicKeyInput.

Examples found in repository?
examples/keyring/ecdh/raw_ecdh_keyring_example.rs (line 123)
67pub async fn encrypt_and_decrypt_with_keyring(
68    example_data: &str,
69    ecdh_curve_spec: EcdhCurveSpec,
70) -> Result<(), crate::BoxError> {
71    // 1. Instantiate the encryption SDK client.
72    // This builds the default client with the RequireEncryptRequireDecrypt commitment policy,
73    // which enforces that this client only encrypts using committing algorithm suites and enforces
74    // that this client will only decrypt encrypted messages that were created with a committing
75    // algorithm suite.
76    let esdk_config = AwsEncryptionSdkConfig::builder().build()?;
77    let esdk_client = esdk_client::Client::from_conf(esdk_config)?;
78
79    // 2. Create encryption context.
80    // Remember that your encryption context is NOT SECRET.
81    // For more information, see
82    // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
83    let encryption_context = HashMap::from([
84        ("encryption".to_string(), "context".to_string()),
85        ("is not".to_string(), "secret".to_string()),
86        ("but adds".to_string(), "useful metadata".to_string()),
87        (
88            "that can help you".to_string(),
89            "be confident that".to_string(),
90        ),
91        (
92            "the data you are handling".to_string(),
93            "is what you think it is".to_string(),
94        ),
95    ]);
96
97    // 3. You may provide your own ECC keys in the files located at
98    // - EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER
99    // - EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT
100
101    // If you do not provide these files, running this example through this
102    // class' main method will generate three files required for all raw ECDH examples
103    // EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER, EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT
104    // and EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT for you.
105
106    // Do not use these files for any other purpose.
107    if should_generate_new_ecc_key_pair_raw_ecdh()? {
108        write_raw_ecdh_ecc_keys(ecdh_curve_spec)?;
109    }
110
111    // 4. Load keys from UTF-8 encoded PEM files.
112    let mut file = File::open(Path::new(EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER))?;
113    let mut private_key_sender_utf8_bytes = Vec::new();
114    file.read_to_end(&mut private_key_sender_utf8_bytes)?;
115
116    // Load public key from UTF-8 encoded PEM files into a DER encoded public key.
117    let public_key_file_content =
118        std::fs::read_to_string(Path::new(EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT))?;
119    let parsed_public_key_file_content = parse(public_key_file_content)?;
120    let public_key_recipient_utf8_bytes = parsed_public_key_file_content.contents();
121
122    // 5. Create the RawPrivateKeyToStaticPublicKeyInput
123    let raw_ecdh_static_configuration_input = RawPrivateKeyToStaticPublicKeyInput::builder()
124        // Must be a UTF8 PEM-encoded private key
125        .sender_static_private_key(private_key_sender_utf8_bytes)
126        // Must be a UTF8 DER-encoded X.509 public key
127        .recipient_public_key(public_key_recipient_utf8_bytes)
128        .build()?;
129
130    let raw_ecdh_static_configuration = RawEcdhStaticConfigurations::RawPrivateKeyToStaticPublicKey(
131        raw_ecdh_static_configuration_input,
132    );
133
134    // 6. Create the Raw ECDH keyring.
135    let mpl_config = MaterialProvidersConfig::builder().build()?;
136    let mpl = mpl_client::Client::from_conf(mpl_config)?;
137
138    // Create the keyring.
139    // This keyring uses static sender and recipient keys. This configuration calls for both of
140    // the keys to be on the same curve (P256 / P384 / P521).
141    // On encrypt, the shared secret is derived from the sender's private key and the recipient's public key.
142    // For this example, on decrypt, the shared secret is derived from the sender's private key and the recipient's public key;
143    // However, on decrypt, the recipient can construct a keyring such that the shared secret is calculated with
144    // the recipient's private key and the sender's public key. In both scenarios the shared secret will be the same.
145    let raw_ecdh_keyring = mpl
146        .create_raw_ecdh_keyring()
147        .curve_spec(ecdh_curve_spec)
148        .key_agreement_scheme(raw_ecdh_static_configuration)
149        .send()
150        .await?;
151
152    // 7. Encrypt the data with the encryption_context
153    let plaintext = example_data.as_bytes();
154
155    let encryption_response = esdk_client
156        .encrypt()
157        .plaintext(plaintext)
158        .keyring(raw_ecdh_keyring.clone())
159        .encryption_context(encryption_context.clone())
160        .send()
161        .await?;
162
163    let ciphertext = encryption_response
164        .ciphertext
165        .expect("Unable to unwrap ciphertext from encryption response");
166
167    // 8. Demonstrate that the ciphertext and plaintext are different.
168    // (This is an example for demonstration; you do not need to do this in your own code.)
169    assert_ne!(
170        ciphertext,
171        aws_smithy_types::Blob::new(plaintext),
172        "Ciphertext and plaintext data are the same. Invalid encryption"
173    );
174
175    // 9. Decrypt your encrypted data using the same keyring you used on encrypt.
176    let decryption_response = esdk_client
177        .decrypt()
178        .ciphertext(ciphertext)
179        .keyring(raw_ecdh_keyring)
180        // Provide the encryption context that was supplied to the encrypt method
181        .encryption_context(encryption_context)
182        .send()
183        .await?;
184
185    let decrypted_plaintext = decryption_response
186        .plaintext
187        .expect("Unable to unwrap plaintext from decryption response");
188
189    // 10. Demonstrate that the decrypted plaintext is identical to the original plaintext.
190    // (This is an example for demonstration; you do not need to do this in your own code.)
191    assert_eq!(
192        decrypted_plaintext,
193        aws_smithy_types::Blob::new(plaintext),
194        "Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
195    );
196
197    println!("Raw ECDH Keyring Example Completed Successfully");
198
199    Ok(())
200}

Trait Implementations§

Source§

impl Clone for RawPrivateKeyToStaticPublicKeyInput

Source§

fn clone(&self) -> RawPrivateKeyToStaticPublicKeyInput

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RawPrivateKeyToStaticPublicKeyInput

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for RawPrivateKeyToStaticPublicKeyInput

Source§

fn eq(&self, other: &RawPrivateKeyToStaticPublicKeyInput) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for RawPrivateKeyToStaticPublicKeyInput

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AnyRef for T
where T: 'static,

Source§

fn as_any_ref(&self) -> &(dyn Any + 'static)

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T
where T: ?Sized,

Source§

fn upcast(&self) -> Ptr<T>

Source§

impl<T> UpcastObject<T> for T
where T: ?Sized,

Source§

fn upcast(&self) -> Object<T>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more