diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2022-04-12 13:21:58 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2022-04-12 13:21:58 +0200 |
commit | 65f2ab5c15dba6d8ca4a2a85472f5a793adfe0e4 (patch) | |
tree | a3f8dd5664ce92823bd8291b0104484231163048 /openpgp/examples | |
parent | b41958f99f24022f0f71e67bb57ab4def74267ca (diff) |
openpgp: Collect ECDH parameters in statistics example.
- Fixes #838.
Diffstat (limited to 'openpgp/examples')
-rw-r--r-- | openpgp/examples/statistics.rs | 61 |
1 files changed, 61 insertions, 0 deletions
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<PublicKeyAlgorithm, HashMap<usize, usize>> = 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<T>(counter: &mut HashMap<T, usize>, 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::<Vec<_>>(); + 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::<Vec<_>>(); + 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}", |