|
| 1 | +// Copyright 2020 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +use e2e_tests::TestClient; |
| 4 | +use parsec_client::core::interface::requests::ResponseStatus; |
| 5 | +use rsa::{RSAPublicKey, PaddingScheme, PublicKey}; |
| 6 | +use rand::rngs::OsRng; |
| 7 | + |
| 8 | + |
| 9 | +const PLAINTEXT_MESSAGE: [u8; 32] = [ |
| 10 | + 0x69, 0x3E, 0xDB, 0x1B, 0x22, 0x79, 0x03, 0xF4, 0xC0, 0xBF, 0xD6, 0x91, 0x76, 0x37, 0x84, 0xA2, |
| 11 | + 0x94, 0x8E, 0x92, 0x50, 0x35, 0xC2, 0x8C, 0x5C, 0x3C, 0xCA, 0xFE, 0x18, 0xE8, 0x81, 0x37, 0x78, |
| 12 | +]; |
| 13 | + |
| 14 | +#[test] |
| 15 | +fn simple_asym_encrypt_rsa_pkcs() { |
| 16 | + let key_name = String::from("asym_encrypt_and_decrypt_rsa_pkcs"); |
| 17 | + let mut client = TestClient::new(); |
| 18 | + client.generate_rsa_encryption_keys_rsapkcs1v15crypt(key_name.clone()).unwrap(); |
| 19 | + let _ciphertext = client.asymmetric_encrypt_message_with_rsapkcs1v15( |
| 20 | + key_name.clone(), |
| 21 | + PLAINTEXT_MESSAGE.to_vec(), |
| 22 | + ).unwrap(); |
| 23 | +} |
| 24 | + |
| 25 | +#[test] |
| 26 | +fn asym_encrypt_no_key() { |
| 27 | + let key_name = String::from("asym_encrypt_no_key"); |
| 28 | + let mut client = TestClient::new(); |
| 29 | + let status = client. |
| 30 | + asymmetric_encrypt_message_with_rsapkcs1v15( |
| 31 | + key_name, |
| 32 | + PLAINTEXT_MESSAGE.to_vec(), |
| 33 | + ) |
| 34 | + .expect_err("Key should not exist."); |
| 35 | + assert_eq!(status, ResponseStatus::PsaErrorDoesNotExist); |
| 36 | +} |
| 37 | + |
| 38 | +#[test] |
| 39 | +fn asym_decrypt_no_key() { |
| 40 | + let key_name = String::from("asym_decrypt_no_key"); |
| 41 | + let mut client = TestClient::new(); |
| 42 | + let status = client. |
| 43 | + asymmetric_decrypt_message_with_rsapkcs1v15( |
| 44 | + key_name, |
| 45 | + PLAINTEXT_MESSAGE.to_vec(), |
| 46 | + ) |
| 47 | + .expect_err("Key should not exist."); |
| 48 | + assert_eq!(status, ResponseStatus::PsaErrorDoesNotExist); |
| 49 | +} |
| 50 | + |
| 51 | +#[test] |
| 52 | +fn asym_encrypt_wrong_algorithm() { |
| 53 | + let key_name = String::from("asym_encrypt_wrong_algorithm"); |
| 54 | + let mut client = TestClient::new(); |
| 55 | + let _key_id = client.generate_rsa_encryption_keys_rsaoaep_sha256(key_name.clone()).unwrap(); |
| 56 | + let status = client.asymmetric_encrypt_message_with_rsapkcs1v15( |
| 57 | + key_name.clone(), |
| 58 | + PLAINTEXT_MESSAGE.to_vec(), |
| 59 | + ).unwrap_err(); |
| 60 | + assert_eq!(status, ResponseStatus::PsaErrorNotPermitted); |
| 61 | +} |
| 62 | + |
| 63 | +#[test] |
| 64 | +fn asym_encrypt_and_decrypt_rsa_pkcs() { |
| 65 | + let key_name = String::from("asym_encrypt_and_decrypt_rsa_pkcs"); |
| 66 | + let mut client = TestClient::new(); |
| 67 | + client.generate_rsa_encryption_keys_rsapkcs1v15crypt(key_name.clone()).unwrap(); |
| 68 | + let ciphertext = client.asymmetric_encrypt_message_with_rsapkcs1v15( |
| 69 | + key_name.clone(), |
| 70 | + PLAINTEXT_MESSAGE.to_vec(), |
| 71 | + ).unwrap(); |
| 72 | + let plaintext = client.asymmetric_decrypt_message_with_rsapkcs1v15( |
| 73 | + key_name, |
| 74 | + ciphertext, |
| 75 | + ).unwrap(); |
| 76 | + assert_eq!(PLAINTEXT_MESSAGE.to_vec(), plaintext); |
| 77 | +} |
| 78 | + |
| 79 | +#[test] |
| 80 | +fn asym_encrypt_decrypt_rsa_pkcs_different_keys() { |
| 81 | + let key_name_1 = String::from("asym_encrypt_and_decrypt_rsa_pkcs_different_keys_1"); |
| 82 | + let key_name_2 = String::from("asym_encrypt_and_decrypt_rsa_pkcs_different_keys_2"); |
| 83 | + let mut client = TestClient::new(); |
| 84 | + client.generate_rsa_encryption_keys_rsapkcs1v15crypt(key_name_1.clone()).unwrap(); |
| 85 | + client.generate_rsa_encryption_keys_rsapkcs1v15crypt(key_name_2.clone()).unwrap(); |
| 86 | + let ciphertext = client.asymmetric_encrypt_message_with_rsapkcs1v15( |
| 87 | + key_name_1.clone(), |
| 88 | + PLAINTEXT_MESSAGE.to_vec(), |
| 89 | + ).unwrap(); |
| 90 | + let _res = client.asymmetric_decrypt_message_with_rsapkcs1v15( |
| 91 | + key_name_2.clone(), |
| 92 | + ciphertext, |
| 93 | + ).unwrap_err(); |
| 94 | +} |
| 95 | + |
| 96 | +#[test] |
| 97 | +fn asym_encrypt_verify_decrypt_with_rsa_crate() { |
| 98 | + let key_name = String::from("asym_encrypt_verify_decrypt_with_rsa_crate"); |
| 99 | + let mut client = TestClient::new(); |
| 100 | + |
| 101 | + client.generate_rsa_encryption_keys_rsapkcs1v15crypt(key_name.clone()).unwrap(); |
| 102 | + let pub_key = client.export_public_key(key_name.clone()).unwrap(); |
| 103 | + |
| 104 | + let rsa_pub_key = RSAPublicKey::from_pkcs1(&pub_key).unwrap(); |
| 105 | + let ciphertext = rsa_pub_key.encrypt(&mut OsRng, PaddingScheme::new_pkcs1v15_encrypt(), &PLAINTEXT_MESSAGE).unwrap(); |
| 106 | + |
| 107 | + let plaintext = client.asymmetric_decrypt_message_with_rsapkcs1v15( |
| 108 | + key_name.clone(), |
| 109 | + ciphertext, |
| 110 | + ).unwrap(); |
| 111 | + |
| 112 | + assert_eq!(&PLAINTEXT_MESSAGE[..], &plaintext[..]); |
| 113 | + |
| 114 | +} |
| 115 | + |
0 commit comments