API change: pass the data after the context

This commit is contained in:
Rodolphe Bréard 2024-03-24 09:25:27 +01:00
parent c62029ee91
commit 90c8a2aa87
3 changed files with 15 additions and 15 deletions

View file

@ -13,7 +13,7 @@ fn decrypt_coffio(ikml: &str, input: &str) {
let key_ctx = KeyContext::from(KEY_CTX); let key_ctx = KeyContext::from(KEY_CTX);
let data_ctx = DataContext::from(DATA_CTX); let data_ctx = DataContext::from(DATA_CTX);
let cb = CipherBox::new(&ikm); let cb = CipherBox::new(&ikm);
if let Err(e) = cb.decrypt(&key_ctx, input, &data_ctx) { if let Err(e) = cb.decrypt(&key_ctx, &data_ctx, input) {
assert!(false, "{e}"); assert!(false, "{e}");
} }
} }

View file

@ -10,7 +10,7 @@ fn encrypt_coffio(ikml: &str, input: &str) {
let key_ctx = KeyContext::from(KEY_CTX); let key_ctx = KeyContext::from(KEY_CTX);
let data_ctx = DataContext::from(DATA_CTX); let data_ctx = DataContext::from(DATA_CTX);
let cb = CipherBox::new(&ikm); let cb = CipherBox::new(&ikm);
if let Err(e) = cb.encrypt(&key_ctx, input, &data_ctx) { if let Err(e) = cb.encrypt(&key_ctx, &data_ctx, input) {
assert!(false, "{e}"); assert!(false, "{e}");
} }
} }

View file

@ -38,8 +38,8 @@ impl<'a> CipherBox<'a> {
pub fn encrypt( pub fn encrypt(
&self, &self,
key_context: &KeyContext, key_context: &KeyContext,
data: impl AsRef<[u8]>,
data_context: &DataContext, data_context: &DataContext,
data: impl AsRef<[u8]>,
) -> Result<String> { ) -> Result<String> {
let tp = if key_context.is_periodic() { let tp = if key_context.is_periodic() {
let ts = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs(); let ts = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs();
@ -60,8 +60,8 @@ impl<'a> CipherBox<'a> {
pub fn decrypt( pub fn decrypt(
&self, &self,
key_context: &KeyContext, key_context: &KeyContext,
stored_data: &str,
data_context: &DataContext, data_context: &DataContext,
stored_data: &str,
) -> Result<Vec<u8>> { ) -> Result<Vec<u8>> {
let (ikm_id, encrypted_data, tp) = storage::decode_cipher(stored_data)?; let (ikm_id, encrypted_data, tp) = storage::decode_cipher(stored_data)?;
let ikm = self.ikm_list.get_ikm_by_id(ikm_id)?; let ikm = self.ikm_list.get_ikm_by_id(ikm_id)?;
@ -109,14 +109,14 @@ mod tests {
let cb = CipherBox::new(&lst); let cb = CipherBox::new(&lst);
// Encrypt // Encrypt
let res = cb.encrypt(&key_ctx, TEST_DATA, &data_ctx); let res = cb.encrypt(&key_ctx, &data_ctx, TEST_DATA);
assert!(res.is_ok(), "res: {res:?}"); assert!(res.is_ok(), "res: {res:?}");
let ciphertext = res.unwrap(); let ciphertext = res.unwrap();
assert!(ciphertext.starts_with("AQAAAA:")); assert!(ciphertext.starts_with("AQAAAA:"));
assert_eq!(ciphertext.len(), 98); assert_eq!(ciphertext.len(), 98);
// Decrypt // Decrypt
let res = cb.decrypt(&key_ctx, &ciphertext, &data_ctx); let res = cb.decrypt(&key_ctx, &data_ctx, &ciphertext);
assert!(res.is_ok(), "res: {res:?}"); assert!(res.is_ok(), "res: {res:?}");
let plaintext = res.unwrap(); let plaintext = res.unwrap();
assert_eq!(plaintext, TEST_DATA); assert_eq!(plaintext, TEST_DATA);
@ -130,14 +130,14 @@ mod tests {
let cb = CipherBox::new(&lst); let cb = CipherBox::new(&lst);
// Encrypt // Encrypt
let res = cb.encrypt(&key_ctx, TEST_DATA, &data_ctx); let res = cb.encrypt(&key_ctx, &data_ctx, TEST_DATA);
assert!(res.is_ok(), "res: {res:?}"); assert!(res.is_ok(), "res: {res:?}");
let ciphertext = res.unwrap(); let ciphertext = res.unwrap();
assert!(ciphertext.starts_with("AQAAAA:")); assert!(ciphertext.starts_with("AQAAAA:"));
assert_eq!(ciphertext.len(), 98); assert_eq!(ciphertext.len(), 98);
// Decrypt // Decrypt
let res = cb.decrypt(&key_ctx, &ciphertext, &data_ctx); let res = cb.decrypt(&key_ctx, &data_ctx, &ciphertext);
assert!(res.is_ok(), "res: {res:?}"); assert!(res.is_ok(), "res: {res:?}");
let plaintext = res.unwrap(); let plaintext = res.unwrap();
assert_eq!(plaintext, TEST_DATA); assert_eq!(plaintext, TEST_DATA);
@ -151,14 +151,14 @@ mod tests {
let cb = CipherBox::new(&lst); let cb = CipherBox::new(&lst);
// Encrypt // Encrypt
let res = cb.encrypt(&key_ctx, TEST_DATA, &data_ctx); let res = cb.encrypt(&key_ctx, &data_ctx, TEST_DATA);
assert!(res.is_ok(), "res: {res:?}"); assert!(res.is_ok(), "res: {res:?}");
let ciphertext = res.unwrap(); let ciphertext = res.unwrap();
assert!(ciphertext.starts_with("AQAAAA:")); assert!(ciphertext.starts_with("AQAAAA:"));
assert_eq!(ciphertext.len(), 110); assert_eq!(ciphertext.len(), 110);
// Decrypt // Decrypt
let res = cb.decrypt(&key_ctx, &ciphertext, &data_ctx); let res = cb.decrypt(&key_ctx, &data_ctx, &ciphertext);
assert!(res.is_ok(), "res: {res:?}"); assert!(res.is_ok(), "res: {res:?}");
let plaintext = res.unwrap(); let plaintext = res.unwrap();
assert_eq!(plaintext, TEST_DATA); assert_eq!(plaintext, TEST_DATA);
@ -182,12 +182,12 @@ mod tests {
let cb = CipherBox::new(&lst); let cb = CipherBox::new(&lst);
// Test if the reference ciphertext used for the tests is actually valid // Test if the reference ciphertext used for the tests is actually valid
let res = cb.decrypt(&key_ctx, TEST_CIPHERTEXT, &data_ctx); let res = cb.decrypt(&key_ctx, &data_ctx, TEST_CIPHERTEXT);
assert!(res.is_ok(), "invalid reference ciphertext"); assert!(res.is_ok(), "invalid reference ciphertext");
// Test if altered versions of the reference ciphertext are refused // Test if altered versions of the reference ciphertext are refused
for (ciphertext, error_str) in tests { for (ciphertext, error_str) in tests {
let res = cb.decrypt(&key_ctx, ciphertext, &data_ctx); let res = cb.decrypt(&key_ctx, &data_ctx, ciphertext);
assert!(res.is_err(), "failed error detection: {error_str}"); assert!(res.is_err(), "failed error detection: {error_str}");
} }
} }
@ -199,15 +199,15 @@ mod tests {
let data_ctx = DataContext::from(TEST_DATA_CTX); let data_ctx = DataContext::from(TEST_DATA_CTX);
let cb = CipherBox::new(&lst); let cb = CipherBox::new(&lst);
let res = cb.decrypt(&key_ctx, TEST_CIPHERTEXT, &data_ctx); let res = cb.decrypt(&key_ctx, &data_ctx, TEST_CIPHERTEXT);
assert!(res.is_ok(), "invalid reference ciphertext"); assert!(res.is_ok(), "invalid reference ciphertext");
let invalid_key_ctx = KeyContext::from(["invalid", "key", "context"]); let invalid_key_ctx = KeyContext::from(["invalid", "key", "context"]);
let res = cb.decrypt(&invalid_key_ctx, TEST_CIPHERTEXT, &data_ctx); let res = cb.decrypt(&invalid_key_ctx, &data_ctx, TEST_CIPHERTEXT);
assert!(res.is_err(), "failed error detection: invalid key context"); assert!(res.is_err(), "failed error detection: invalid key context");
let invalid_data_ctx = DataContext::from(["invalid", "data", "context"]); let invalid_data_ctx = DataContext::from(["invalid", "data", "context"]);
let res = cb.decrypt(&key_ctx, TEST_CIPHERTEXT, &invalid_data_ctx); let res = cb.decrypt(&key_ctx, &invalid_data_ctx, TEST_CIPHERTEXT);
assert!(res.is_err(), "failed error detection: invalid key context"); assert!(res.is_err(), "failed error detection: invalid key context");
} }
} }