| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
#![cfg(not(feature = "disable-encryption"))] |
| 8 |
#![cfg(test)] |
| 9 |
|
| 10 |
use neqo_crypto::{ |
| 11 |
constants::{TLS_AES_128_GCM_SHA256, TLS_VERSION_1_3}, |
| 12 |
init, |
| 13 |
selfencrypt::SelfEncrypt, |
| 14 |
Error, |
| 15 |
}; |
| 16 |
|
| 17 |
#[test] |
| 18 |
fn se_create() { |
| 19 |
init().unwrap(); |
| 20 |
SelfEncrypt::new(TLS_VERSION_1_3, TLS_AES_128_GCM_SHA256).expect("constructor works"); |
| 21 |
} |
| 22 |
|
| 23 |
const PLAINTEXT: &[u8] = b"PLAINTEXT"; |
| 24 |
const AAD: &[u8] = b"AAD"; |
| 25 |
|
| 26 |
fn sealed() -> (SelfEncrypt, Vec<u8>) { |
| 27 |
init().unwrap(); |
| 28 |
let se = SelfEncrypt::new(TLS_VERSION_1_3, TLS_AES_128_GCM_SHA256).unwrap(); |
| 29 |
let sealed = se.seal(AAD, PLAINTEXT).expect("sealing works"); |
| 30 |
(se, sealed) |
| 31 |
} |
| 32 |
|
| 33 |
#[test] |
| 34 |
fn seal_open() { |
| 35 |
let (se, sealed) = sealed(); |
| 36 |
let opened = se.open(AAD, &sealed).expect("opening works"); |
| 37 |
assert_eq!(&opened[..], PLAINTEXT); |
| 38 |
} |
| 39 |
|
| 40 |
#[test] |
| 41 |
fn seal_rotate_open() { |
| 42 |
let (mut se, sealed) = sealed(); |
| 43 |
se.rotate().expect("rotate should be infallible"); |
| 44 |
let opened = se.open(AAD, &sealed).expect("opening works"); |
| 45 |
assert_eq!(&opened[..], PLAINTEXT); |
| 46 |
} |
| 47 |
|
| 48 |
#[test] |
| 49 |
fn seal_rotate_twice_open() { |
| 50 |
let (mut se, sealed) = sealed(); |
| 51 |
se.rotate().expect("rotate should be infallible"); |
| 52 |
se.rotate().expect("rotate should be infallible"); |
| 53 |
let res = se.open(AAD, &sealed); |
| 54 |
assert_eq!(res.unwrap_err(), Error::SelfEncryptFailure); |
| 55 |
} |
| 56 |
|
| 57 |
#[test] |
| 58 |
fn damage_version() { |
| 59 |
let (se, mut sealed) = sealed(); |
| 60 |
sealed[0] ^= 0x80; |
| 61 |
let res = se.open(AAD, &sealed); |
| 62 |
assert_eq!(res.unwrap_err(), Error::SelfEncryptFailure); |
| 63 |
} |
| 64 |
|
| 65 |
fn assert_bad_data<T>(res: Result<T, Error>) { |
| 66 |
if let Err(Error::NssError { name, .. }) = res { |
| 67 |
assert_eq!(name, "SEC_ERROR_BAD_DATA"); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
#[test] |
| 72 |
fn damage_salt() { |
| 73 |
let (se, mut sealed) = sealed(); |
| 74 |
sealed[4] ^= 0x10; |
| 75 |
let res = se.open(AAD, &sealed); |
| 76 |
assert_bad_data(res); |
| 77 |
} |
| 78 |
|
| 79 |
#[test] |
| 80 |
fn damage_ciphertext() { |
| 81 |
let (se, mut sealed) = sealed(); |
| 82 |
sealed[20] ^= 0x2f; |
| 83 |
let res = se.open(AAD, &sealed); |
| 84 |
assert_bad_data(res); |
| 85 |
} |
| 86 |
|
| 87 |
#[test] |
| 88 |
fn damage_auth_tag() { |
| 89 |
let (se, mut sealed) = sealed(); |
| 90 |
let idx = sealed.len() - 1; |
| 91 |
sealed[idx] ^= 0x3; |
| 92 |
let res = se.open(AAD, &sealed); |
| 93 |
assert_bad_data(res); |
| 94 |
} |
| 95 |
|
| 96 |
#[test] |
| 97 |
fn truncate() { |
| 98 |
let (se, sealed) = sealed(); |
| 99 |
let res = se.open(AAD, &sealed[0..(sealed.len() - 1)]); |
| 100 |
assert_bad_data(res); |
| 101 |
} |