bdrck 0.22.5

Generic common foundational utilities.
Documentation
// Copyright 2015 Axel Rasmussen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::crypto::key::{AbstractKey, Key};
use crate::crypto::wrap::*;

#[test]
fn test_wrapping_roundtrip() {
    crate::init().unwrap();

    let a = Key::new_random().unwrap();
    let b = Key::new_random().unwrap();

    let wrapped = WrappedKey::wrap(&a, &b).unwrap();
    assert_ne!(wrapped.get_digest(), a.get_digest());
    assert_eq!(wrapped.get_wrapping_digest(), &b.get_digest());

    let unwrapped: Key = wrapped.unwrap(&b).unwrap();
    assert_eq!(a.get_digest(), unwrapped.get_digest());
}

#[test]
fn test_unwrapping_with_wrong_key_fails() {
    crate::init().unwrap();

    let a = Key::new_random().unwrap();
    let b = Key::new_random().unwrap();
    let wrong_key = Key::new_random().unwrap();

    let wrapped = WrappedKey::wrap(&a, &b).unwrap();
    assert!(wrapped.unwrap::<Key, Key>(&wrong_key).is_err());
}

#[test]
fn test_unwrap_with_wrong_digest_fast_fails() {
    crate::init().unwrap();

    // Wrap `a` with `b`, then try to unwrap with an unrelated key `c`.
    // Since `c.get_digest() != b.get_digest()`, we should hit the
    // "not the correct wrapping key" early-return branch rather than the
    // decryption failure path.
    let a = Key::new_random().unwrap();
    let b = Key::new_random().unwrap();
    let c = Key::new_random().unwrap();
    assert_ne!(b.get_digest(), c.get_digest());

    let wrapped = WrappedKey::wrap(&a, &b).unwrap();
    let msg = match wrapped.unwrap::<Key, Key>(&c) {
        Ok(_) => panic!("expected an error"),
        Err(e) => format!("{}", e),
    };
    assert!(
        msg.contains("not the correct wrapping key"),
        "unexpected error: {}",
        msg
    );
}