//! Cryptographic hash functions and hashing of OpenPGP data
//! structures.
//!
//! This module provides trait [`Digest`] representing a hash function
//! context independent of the cryptographic backend, as well as trait
//! [`Hash`] that handles hashing of OpenPGP data structures.
//!
//!
//! # Examples
//!
//! ```rust
//! # fn main() -> sequoia_openpgp::Result<()> {
//! use sequoia_openpgp::types::HashAlgorithm;
//!
//! // Create a context and feed data to it.
//! let mut ctx = HashAlgorithm::SHA512.context()?;
//! ctx.update(&b"The quick brown fox jumps over the lazy dog."[..]);
//!
//! // Extract the digest.
//! let mut digest = vec![0; ctx.digest_size()];
//! ctx.digest(&mut digest);
//!
//! use sequoia_openpgp::fmt::hex;
//! assert_eq!(&hex::encode(digest),
//! "91EA1245F20D46AE9A037A989F54F1F7\
//! 90F0A47607EEB8A14D12890CEA77A1BB\
//! C6C7ED9CF205E67B7F2B8FD4C7DFD3A7\
//! A8617E45F3C463D481C7E586C39AC1ED");
//! # Ok(()) }
//! ```
use std::convert::TryFrom;
use dyn_clone::DynClone;
use crate::HashAlgorithm;
use crate::packet::Key;
use crate::packet::UserID;
use crate::packet::UserAttribute;
use crate::packet::key;
use crate::packet::key::Key4;
use crate::packet::Signature;
use crate::packet::signature::{self, Signature3, Signature4};
use crate::Result;
use crate::types::Timestamp;
use std::fs::{File, OpenOptions};
use std::io::{self, Write};
// If set to e.g. Some("/tmp/hash"), we will dump everything that is
// hashed to files /tmp/hash-N, where N is a number.
const DUMP_HASHED_VALUES: Option<&str> = None;
// ASN.1 OID values copied from the nettle-rs crate:
// https://gitlab.com/sequoia-pgp/nettle-rs/-/blob/main/src/rsa/pkcs1.rs#L22
/// ASN.1 OID for MD5
const ASN1_OID_MD5: &[u8] = &[
0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
0x02, 0x05, 0x05, 0x00, 0x04, 0x10,
];
/// ASN.1 OID for RipeMD160
const ASN1_OID_RIPEMD160: &[u8] = &[
0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02, 0x01, 0x05,
0x00, 0x04, 0x14,
];
/// ASN.1 OID for SHA1
const ASN1_OID_SHA1: &[u8] = &[
0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
0x00, 0x04, 0x14,
];
/// ASN.1 OID for SHA224
const ASN1_OID_SHA224: &[u8] = &[
0x30, 0x2D, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1C,
];
/// ASN.1 OID for SHA256
const ASN1_OID_SHA256: &[u8] = &[
0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20,
];
/// ASN.1 OID for SHA384
const ASN1_OID_SHA384: &[u8] = &[
0x30, 0x41, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30,
];
/// ASN.1 OID for SHA512
const ASN1_OID_SHA512: &[u8] = &[
0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40,
];
lazy_static::lazy_static! {
/// List of hashes that the signer may produce.
/// This list is ordered by the preference so that the most preferred
/// hash algorithm is first.
pub(crate) static ref DEFAULT_HASHES: Vec<HashAlgorithm> = vec![
HashAlgorithm::default(),
HashAlgorithm::SHA512,
HashAlgorithm::SHA384,
HashAlgorithm::SHA256,