From 65f2ab5c15dba6d8ca4a2a85472f5a793adfe0e4 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Tue, 12 Apr 2022 13:21:58 +0200 Subject: openpgp: Collect ECDH parameters in statistics example. - Fixes #838. --- openpgp/examples/statistics.rs | 61 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'openpgp/examples') diff --git a/openpgp/examples/statistics.rs b/openpgp/examples/statistics.rs index d0c5023d..76f6e559 100644 --- a/openpgp/examples/statistics.rs +++ b/openpgp/examples/statistics.rs @@ -15,6 +15,7 @@ use anyhow::Context; use sequoia_openpgp as openpgp; use crate::openpgp::{Packet, Fingerprint, KeyID, KeyHandle}; +use crate::openpgp::crypto::mpi; use crate::openpgp::types::*; use crate::openpgp::packet::{user_attribute, header::BodyLength, Tag}; use crate::openpgp::packet::signature::subpacket::SubpacketTag; @@ -86,6 +87,12 @@ fn main() -> openpgp::Result<()> { let mut pk_algo_size: HashMap> = Default::default(); + // ECDH Parameter (KDF and KEK) statistics. + let mut ecdh_params: HashMap<(HashAlgorithm, SymmetricAlgorithm), usize> = + Default::default(); + let mut ecdh_params_by_curve: HashMap<(Curve, HashAlgorithm, SymmetricAlgorithm), usize> = + Default::default(); + // Current certificate. let mut current_fingerprint = KeyHandle::Fingerprint(Fingerprint::from_bytes(&vec![0; 20])); @@ -269,6 +276,24 @@ fn main() -> openpgp::Result<()> { size_hash.insert(bits, 1); pk_algo_size.insert(pk, size_hash); } + + fn inc(counter: &mut HashMap, key: T) + where + T: std::hash::Hash + Eq, + { + if let Some(count) = counter.get_mut(&key) { + *count += 1; + } else { + counter.insert(key, 1); + } + } + + if let mpi::PublicKey::ECDH { curve, hash, sym, .. } = k.mpis() { + inc(&mut ecdh_params, + (hash.clone(), sym.clone())); + inc(&mut ecdh_params_by_curve, + (curve.clone(), hash.clone(), sym.clone())); + } }; match packet { Packet::PublicKey(ref k) => @@ -568,6 +593,42 @@ fn main() -> openpgp::Result<()> { } } + if !ecdh_params.is_empty() { + println!(); + println!("# ECDH Parameter statistics"); + println!(); + println!("{:>70} {:>9}", "", "count",); + println!("----------------------------------------\ + ----------------------------------------"); + + // Sort by the number of occurrences. + let mut params = ecdh_params.iter() + .map(|((hash, sym), count)| { + (format!("{:?}, {:?}", hash, sym), count) + }).collect::>(); + params.sort_unstable_by(|a, b| b.1.cmp(a.1)); + for (a, n) in params { + println!("{:>70} {:>9}", a, n); + } + + println!(); + println!("# ECDH Parameter statistics by curve"); + println!(); + println!("{:>70} {:>9}", "", "count",); + println!("----------------------------------------\ + ----------------------------------------"); + + // Sort by the number of occurrences. + let mut params = ecdh_params_by_curve.iter() + .map(|((curve, hash, sym), count)| { + (format!("{:?}, {:?}, {:?}", curve, hash, sym), count) + }).collect::>(); + params.sort_unstable_by(|a, b| b.1.cmp(a.1)); + for (a, n) in params { + println!("{:>70} {:>9}", a, n); + } + } + println!(); println!("# Cert statistics\n\n\ {:>30} {:>9} {:>9} {:>9}", -- cgit v1.2.3