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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use arrayvec::ArrayVec;
use core::borrow::Borrow;
use core::convert::TryFrom;
use lipmaa_link::lipmaa;
pub mod decode;
pub mod encode;
pub mod publish;
pub mod verify;
pub use decode::decode;
pub use publish::publish;
pub use verify::verify;
#[cfg(feature = "std")]
pub use verify::verify_batch;
#[cfg(feature = "std")]
use crate::util::hex_serde::*;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use ed25519_dalek::PublicKey as DalekPublicKey;
use super::signature::{Signature, MAX_SIGNATURE_SIZE};
use super::yamf_hash::{YamfHash, MAX_YAMF_HASH_SIZE};
pub use ed25519_dalek::PUBLIC_KEY_LENGTH;
pub const TAG_BYTE_LENGTH: usize = 1;
pub const MAX_VARU64_SIZE: usize = 9;
pub const MAX_ENTRY_SIZE_: usize = TAG_BYTE_LENGTH
+ MAX_SIGNATURE_SIZE
+ PUBLIC_KEY_LENGTH
+ (MAX_YAMF_HASH_SIZE * 3)
+ (MAX_VARU64_SIZE * 3);
pub const MAX_ENTRY_SIZE: usize = 322;
const_assert_eq!(max_entry_size; MAX_ENTRY_SIZE_ as isize, MAX_ENTRY_SIZE as isize);
#[cfg_attr(feature = "std", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
#[derive(Debug, Eq, PartialEq)]
#[repr(C)]
pub struct Entry<H, S>
where
H: Borrow<[u8]>,
S: Borrow<[u8]>,
{
pub log_id: u64,
pub is_end_of_feed: bool,
#[cfg_attr(feature = "std", serde(bound(deserialize = "H: From<Vec<u8>>")))]
pub payload_hash: YamfHash<H>,
pub payload_size: u64,
#[cfg_attr(
feature = "std",
serde(
serialize_with = "serialize_pub_key",
deserialize_with = "deserialize_pub_key"
)
)]
pub author: DalekPublicKey,
pub seq_num: u64,
pub backlink: Option<YamfHash<H>>,
pub lipmaa_link: Option<YamfHash<H>>,
#[cfg_attr(feature = "std", serde(bound(deserialize = "S: From<Vec<u8>>")))]
pub sig: Option<Signature<S>>,
}
impl<'a> TryFrom<&'a [u8]> for Entry<&'a [u8], &'a [u8]> {
type Error = decode::Error;
fn try_from(bytes: &'a [u8]) -> Result<Entry<&'a [u8], &'a [u8]>, Self::Error> {
decode(bytes)
}
}
impl<'a, H, S> TryFrom<Entry<H, S>> for ArrayVec<[u8; 512]>
where
H: Borrow<[u8]>,
S: Borrow<[u8]>,
{
type Error = encode::Error;
fn try_from(entry: Entry<H, S>) -> Result<ArrayVec<[u8; 512]>, Self::Error> {
let mut buff = [0u8; 512];
let len = entry.encode(&mut buff)?;
let mut vec = ArrayVec::<[u8; 512]>::from(buff);
unsafe {
vec.set_len(len);
}
Ok(vec)
}
}
pub fn into_owned<H, S>(entry: &Entry<H, S>) -> Entry<ArrayVec<[u8; 64]>, ArrayVec<[u8; 64]>>
where
H: Borrow<[u8]>,
S: Borrow<[u8]>,
{
let sig = match entry.sig {
Some(Signature(ref s)) => {
let mut vec = ArrayVec::<[u8; 64]>::new();
vec.try_extend_from_slice(&s.borrow()[..]).unwrap();
Some(Signature(vec))
}
None => None,
};
let payload_hash = match entry.payload_hash {
YamfHash::Blake2b(ref s) => {
let mut vec = ArrayVec::<[u8; 64]>::new();
vec.try_extend_from_slice(&s.borrow()[..]).unwrap();
YamfHash::Blake2b(vec)
}
};
let backlink = match entry.backlink {
Some(YamfHash::Blake2b(ref s)) => {
let mut vec = ArrayVec::<[u8; 64]>::new();
vec.try_extend_from_slice(&s.borrow()[..]).unwrap();
Some(YamfHash::Blake2b(vec))
}
None => None,
};
let lipmaa_link = match entry.lipmaa_link {
Some(YamfHash::Blake2b(ref s)) => {
let mut vec = ArrayVec::<[u8; 64]>::new();
vec.try_extend_from_slice(&s.borrow()[..]).unwrap();
Some(YamfHash::Blake2b(vec))
}
None => None,
};
Entry {
is_end_of_feed: entry.is_end_of_feed,
payload_size: entry.payload_size,
seq_num: entry.seq_num,
log_id: entry.log_id,
payload_hash,
lipmaa_link,
backlink,
author: entry.author,
sig,
}
}
pub fn is_lipmaa_required(sequence_num: u64) -> bool {
lipmaa(sequence_num) != sequence_num - 1
}