mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 09:26:29 +03:00
Merge pull request #484 from link2xt/safe_test_encryption_decryption
Safe test encryption decryption
This commit is contained in:
31
src/key.rs
31
src/key.rs
@@ -1,7 +1,6 @@
|
|||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::ffi::{CStr, CString};
|
use std::ffi::{CStr, CString};
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
use std::slice;
|
|
||||||
|
|
||||||
use libc;
|
use libc;
|
||||||
use pgp::composed::{Deserializable, SignedPublicKey, SignedSecretKey};
|
use pgp::composed::{Deserializable, SignedPublicKey, SignedSecretKey};
|
||||||
@@ -106,15 +105,6 @@ impl Key {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_binary(data: *const u8, len: libc::c_int, key_type: KeyType) -> Option<Self> {
|
|
||||||
if data.is_null() || len == 0 {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let bytes = unsafe { slice::from_raw_parts(data, len as usize) };
|
|
||||||
Self::from_slice(bytes, key_type)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_armored_string(
|
pub fn from_armored_string(
|
||||||
data: &str,
|
data: &str,
|
||||||
key_type: KeyType,
|
key_type: KeyType,
|
||||||
@@ -449,6 +439,27 @@ i8pcjGO+IZffvyZJVRWfVooBJmWWbPB1pueo3tx8w3+fcuzpxz+RLFKaPyqXO+dD
|
|||||||
assert_eq!(private_key, private_key2);
|
assert_eq!(private_key, private_key2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_from_slice_bad_data() {
|
||||||
|
let mut bad_data: [u8; 4096] = [0; 4096];
|
||||||
|
|
||||||
|
for i in 0..4096 {
|
||||||
|
bad_data[i] = (i & 0xff) as u8;
|
||||||
|
}
|
||||||
|
|
||||||
|
for j in 0..(4096 / 40) {
|
||||||
|
let bad_key = Key::from_slice(
|
||||||
|
&bad_data[j..j + 4096 / 2 + j],
|
||||||
|
if 0 != j & 1 {
|
||||||
|
KeyType::Public
|
||||||
|
} else {
|
||||||
|
KeyType::Private
|
||||||
|
},
|
||||||
|
);
|
||||||
|
assert!(bad_key.is_none());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore] // is too expensive
|
#[ignore] // is too expensive
|
||||||
fn test_ascii_roundtrip() {
|
fn test_ascii_roundtrip() {
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ use deltachat::contact::*;
|
|||||||
use deltachat::context::*;
|
use deltachat::context::*;
|
||||||
use deltachat::dc_imex::*;
|
use deltachat::dc_imex::*;
|
||||||
use deltachat::dc_tools::*;
|
use deltachat::dc_tools::*;
|
||||||
use deltachat::key::*;
|
|
||||||
use deltachat::keyring::*;
|
use deltachat::keyring::*;
|
||||||
use deltachat::oauth2::*;
|
use deltachat::oauth2::*;
|
||||||
use deltachat::pgp::*;
|
use deltachat::pgp::*;
|
||||||
@@ -461,30 +460,6 @@ unsafe fn stress_functions(context: &Context) {
|
|||||||
#[test]
|
#[test]
|
||||||
#[ignore] // is too expensive
|
#[ignore] // is too expensive
|
||||||
fn test_encryption_decryption() {
|
fn test_encryption_decryption() {
|
||||||
unsafe {
|
|
||||||
let mut bad_data: [libc::c_uchar; 4096] = [0; 4096];
|
|
||||||
let mut i_0: libc::c_int = 0i32;
|
|
||||||
while i_0 < 4096i32 {
|
|
||||||
bad_data[i_0 as usize] = (i_0 & 0xffi32) as libc::c_uchar;
|
|
||||||
i_0 += 1
|
|
||||||
}
|
|
||||||
let mut j: libc::c_int = 0i32;
|
|
||||||
|
|
||||||
while j < 4096 / 40 {
|
|
||||||
let bad_key = Key::from_binary(
|
|
||||||
&mut *bad_data.as_mut_ptr().offset(j as isize) as *const u8,
|
|
||||||
4096 / 2 + j,
|
|
||||||
if 0 != j & 1 {
|
|
||||||
KeyType::Public
|
|
||||||
} else {
|
|
||||||
KeyType::Private
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
assert!(bad_key.is_none());
|
|
||||||
j += 1
|
|
||||||
}
|
|
||||||
|
|
||||||
let (public_key, private_key) = dc_pgp_create_keypair("foo@bar.de").unwrap();
|
let (public_key, private_key) = dc_pgp_create_keypair("foo@bar.de").unwrap();
|
||||||
|
|
||||||
private_key.split_key().unwrap();
|
private_key.split_key().unwrap();
|
||||||
@@ -590,7 +565,6 @@ fn test_encryption_decryption() {
|
|||||||
dc_pgp_pk_decrypt(ctext_signed.as_bytes(), &keyring, &public_keyring, None).unwrap();
|
dc_pgp_pk_decrypt(ctext_signed.as_bytes(), &keyring, &public_keyring, None).unwrap();
|
||||||
|
|
||||||
assert_eq!(plain, original_text);
|
assert_eq!(plain, original_text);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "C" fn cb(
|
unsafe extern "C" fn cb(
|
||||||
|
|||||||
Reference in New Issue
Block a user