1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! # Packet module
//!
//! Handles everything in relation to
//! [OpenPGP packets](https://www.rfc-editor.org/rfc/rfc9580#name-packet-syntax).
//!
//! Every OpenPGP object consists of a set of "packets". Usually, those are used in
//! [`composed`](crate::pgp::composed) objects, such as Transferable Public Keys, or Messages.
//!
//! Users of rPGP will usually only use this low-level packet functionality indirectly via
//! composed objects.
//!
//! However, users are able to use this low-level module, to implement operations at the packet
//! level. Be aware that the packet level API makes no attempt at being safe to use:
//! Implementing operations on raw packets requires a deep understanding of the OpenPGP format.
//!
//! # Example
//!
//! The following example program:
//!
//! - Generates a pair of bare key packets (which could be used as the primary key in a composed
//! key object): A [`SecretKey`] and the corresponding [`PublicKey`].
//! - Configures a binary data signature using a [`SignatureConfig`].
//! - Produces a [`Signature`] packet with the secret key packet.
//! - Verifies that [`Signature`] packet against the same data, using the public key packet.
//! This operation checks if the cryptographic signature was indeed issued by the key holder
//! of that public key packet.
//!
//! ```
//! use pgp::{
//! composed::KeyType,
//! crypto::{hash::HashAlgorithm, public_key::PublicKeyAlgorithm},
//! packet::{
//! PubKeyInner, PublicKey, SecretKey, SignatureConfig, SignatureType, Subpacket,
//! SubpacketData,
//! },
//! types::{KeyDetails, KeyVersion, Password, Timestamp},
//! };
//! use rand::thread_rng;
//!
//! let mut rng = thread_rng();
//!
//! let now = Timestamp::now();
//!
//! // Generate a pair of bare key packets (a "SecretKey" and a "PublicKey").
//! //
//! // (In a composed "SignedSecretKey" or "SignedPublicKey" object,
//! // such packets would serve as primary keys.)
//! let (public_params, secret_params) = KeyType::Ed25519Legacy
//! .generate(&mut rng)
//! .expect("generate key");
//! let pub_key_inner = PubKeyInner::new(
//! KeyVersion::V4,
//! KeyType::Ed25519Legacy.to_alg(),
//! now,
//! None,
//! public_params,
//! )
//! .expect("create inner public key");
//! let pub_key = PublicKey::from_inner(pub_key_inner).expect("create public key");
//! let sec_key = SecretKey::new(pub_key.clone(), secret_params).expect("create secret key");
//!
//! let mut sig_cfg = SignatureConfig::v4(
//! SignatureType::Binary,
//! PublicKeyAlgorithm::RSA,
//! HashAlgorithm::Sha256,
//! );
//! sig_cfg.hashed_subpackets = vec![
//! Subpacket::regular(SubpacketData::SignatureCreationTime(now)).unwrap(),
//! Subpacket::regular(SubpacketData::IssuerFingerprint(pub_key.fingerprint())).unwrap(),
//! ];
//!
//! const DATA: &'static [u8] = b"Hello World";
//!
//! let signature_packet = sig_cfg
//! .sign(&sec_key, &Password::empty(), DATA)
//! .expect("create signature packet");
//!
//! signature_packet
//! .verify(&pub_key, DATA)
//! .expect("Failed to validate signature");
//! ```
pub use ;