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
|
use std::fmt;
#[cfg(test)]
use quickcheck::{Arbitrary, Gen};
use crate::packet;
use crate::Packet;
/// Holds a Padding packet.
///
/// Padding packets are used to obscure the size of cryptographic
/// artifacts.
///
/// See [Section 5.15 of RFC XXX] for details.
///
/// [Section 5.15 of RFC XXX]: https://openpgp-wg.gitlab.io/rfc4880bis/#name-padding-packet-tag-21
// IMPORTANT: If you add fields to this struct, you need to explicitly
// IMPORTANT: implement PartialEq, Eq, and Hash.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Padding {
pub(crate) common: packet::Common,
value: Vec<u8>,
}
assert_send_and_sync!(Padding);
impl From<Vec<u8>> for Padding {
fn from(u: Vec<u8>) -> Self {
Padding {
common: Default::default(),
value: u,
}
}
}
impl fmt::Display for Padding {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let padding = String::from_utf8_lossy(&self.value[..]);
write!(f, "{}", padding)
}
}
impl fmt::Debug for Padding {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Padding {{ {} bytes }}", self.value.len())
}
}
impl Padding {
/// Creates a new Padding packet of the given size.
///
/// Note that this is the net size, packet framing (CTB and packet
/// length) will come on top.
pub fn new(size: usize) -> Padding {
let mut v = vec![0; size];
crate::crypto::random(&mut v);
v.into()
}
/// Gets the padding packet's value.
pub(crate) fn value(&self) -> &[u8] {
self.value.as_slice()
}
}
impl From<Padding> for Packet {
fn from(s: Padding) -> Self {
Packet::Padding(s)
}
}
#[cfg(test)]
impl Arbitrary for Padding {
fn arbitrary(g: &mut Gen) -> Self {
Vec::<u8>::arbitrary(g).into()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::parse::Parse;
use crate::serialize::MarshalInto;
quickcheck! {
fn roundtrip(p: Padding) -> bool {
let q = Padding::from_bytes(&p.to_vec().unwrap()).unwrap();
assert_eq!(p, q);
true
}
}
}
|