|
| 1 | +use devolutions_crypto::Error; |
| 2 | +use devolutions_crypto::ciphertext::{Ciphertext, CiphertextVersion, encrypt_asymmetric}; |
| 3 | +use devolutions_crypto::key::{KeyVersion, PublicKey, generate_keypair, mix_key_exchange}; |
| 4 | +use devolutions_crypto::online_ciphertext::{ |
| 5 | + OnlineCiphertextEncryptor, OnlineCiphertextHeader, OnlineCiphertextVersion, |
| 6 | +}; |
| 7 | + |
| 8 | +use std::convert::TryFrom; |
| 9 | + |
| 10 | +// Curve25519 points of small order, including non-canonical encodings |
| 11 | +const LOW_ORDER_POINTS: [[u8; 32]; 7] = [ |
| 12 | + hex("0000000000000000000000000000000000000000000000000000000000000000"), |
| 13 | + hex("0100000000000000000000000000000000000000000000000000000000000000"), |
| 14 | + hex("e0eb7a7c3b41b8ae1656e3faf19fc46ada098deb9c32b1fd866205165f49b800"), |
| 15 | + hex("5f9c95bca3508c24b1d0b1559c83ef5b04445cc4581c8e86d8224eddd09f1157"), |
| 16 | + hex("ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f"), |
| 17 | + hex("edffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f"), |
| 18 | + hex("eeffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f"), |
| 19 | +]; |
| 20 | + |
| 21 | +const fn hex(s: &str) -> [u8; 32] { |
| 22 | + const fn nibble(c: u8) -> u8 { |
| 23 | + match c { |
| 24 | + b'0'..=b'9' => c - b'0', |
| 25 | + b'a'..=b'f' => c - b'a' + 10, |
| 26 | + _ => panic!("invalid hex"), |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + let s = s.as_bytes(); |
| 31 | + let mut out = [0u8; 32]; |
| 32 | + let mut i = 0; |
| 33 | + while i < 32 { |
| 34 | + out[i] = (nibble(s[i * 2]) << 4) | nibble(s[i * 2 + 1]); |
| 35 | + i += 1; |
| 36 | + } |
| 37 | + out |
| 38 | +} |
| 39 | + |
| 40 | +fn with_last_32(bytes: &[u8], point: &[u8; 32]) -> Vec<u8> { |
| 41 | + let mut bytes = bytes.to_vec(); |
| 42 | + let offset = bytes.len() - 32; |
| 43 | + bytes[offset..].copy_from_slice(point); |
| 44 | + bytes |
| 45 | +} |
| 46 | + |
| 47 | +fn low_order_public_key(point: &[u8; 32]) -> PublicKey { |
| 48 | + let template: Vec<u8> = generate_keypair(KeyVersion::Latest).public_key.into(); |
| 49 | + PublicKey::try_from(with_last_32(&template, point).as_slice()).unwrap() |
| 50 | +} |
| 51 | + |
| 52 | +#[test] |
| 53 | +fn mix_key_exchange_rejects_low_order_points() { |
| 54 | + let keypair = generate_keypair(KeyVersion::Latest); |
| 55 | + |
| 56 | + for point in &LOW_ORDER_POINTS { |
| 57 | + let public_key = low_order_public_key(point); |
| 58 | + let result = mix_key_exchange(&keypair.private_key, &public_key); |
| 59 | + assert!(matches!(result, Err(Error::InvalidData))); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[test] |
| 64 | +fn encrypt_asymmetric_rejects_low_order_points() { |
| 65 | + for point in &LOW_ORDER_POINTS { |
| 66 | + let public_key = low_order_public_key(point); |
| 67 | + let result = encrypt_asymmetric(b"data", &public_key, CiphertextVersion::Latest); |
| 68 | + assert!(matches!(result, Err(Error::InvalidData))); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[test] |
| 73 | +fn decrypt_asymmetric_rejects_low_order_points() { |
| 74 | + let keypair = generate_keypair(KeyVersion::Latest); |
| 75 | + let ciphertext: Vec<u8> = |
| 76 | + encrypt_asymmetric(b"data", &keypair.public_key, CiphertextVersion::Latest) |
| 77 | + .unwrap() |
| 78 | + .into(); |
| 79 | + |
| 80 | + for point in &LOW_ORDER_POINTS { |
| 81 | + // The ephemeral public key follows the 8-byte header |
| 82 | + let mut forged = ciphertext.clone(); |
| 83 | + forged[8..40].copy_from_slice(point); |
| 84 | + |
| 85 | + let forged = Ciphertext::try_from(forged.as_slice()).unwrap(); |
| 86 | + let result = forged.decrypt_asymmetric(&keypair.private_key); |
| 87 | + assert!(matches!(result, Err(Error::InvalidData))); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +#[test] |
| 92 | +fn online_ciphertext_rejects_low_order_points() { |
| 93 | + let keypair = generate_keypair(KeyVersion::Latest); |
| 94 | + let encryptor = OnlineCiphertextEncryptor::new_asymmetric( |
| 95 | + &keypair.public_key, |
| 96 | + b"", |
| 97 | + 16, |
| 98 | + OnlineCiphertextVersion::Latest, |
| 99 | + ) |
| 100 | + .unwrap(); |
| 101 | + let header: Vec<u8> = (&encryptor.get_header()).into(); |
| 102 | + |
| 103 | + for point in &LOW_ORDER_POINTS { |
| 104 | + let public_key = low_order_public_key(point); |
| 105 | + let result = OnlineCiphertextEncryptor::new_asymmetric( |
| 106 | + &public_key, |
| 107 | + b"", |
| 108 | + 16, |
| 109 | + OnlineCiphertextVersion::Latest, |
| 110 | + ); |
| 111 | + assert!(matches!(result, Err(Error::InvalidData))); |
| 112 | + |
| 113 | + // The ephemeral public key is the last field of the header |
| 114 | + let forged = |
| 115 | + OnlineCiphertextHeader::try_from(with_last_32(&header, point).as_slice()).unwrap(); |
| 116 | + let result = forged.into_decryptor_asymmetric(&keypair.private_key, b""); |
| 117 | + assert!(matches!(result, Err(Error::InvalidData))); |
| 118 | + } |
| 119 | +} |
0 commit comments