1
1
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
- use crate :: credentials:: Credentials ;
5
4
pub use bytes:: buf:: UninitSlice ;
6
5
use core:: fmt;
7
6
pub use s2n_quic_core:: packet:: KeyPhase ;
8
7
9
8
pub mod awslc;
10
- #[ cfg( any( test, feature = "testing" ) ) ]
11
- pub mod testing;
12
9
13
- pub mod encrypt {
10
+ pub mod seal {
14
11
use super :: * ;
15
12
16
- pub trait Key {
17
- fn credentials ( & self ) -> & Credentials ;
18
-
13
+ pub trait Application {
19
14
fn key_phase ( & self ) -> KeyPhase ;
20
15
21
16
fn tag_len ( & self ) -> usize ;
22
17
23
18
/// Encrypt a payload
24
- fn encrypt < N : IntoNonce > (
19
+ fn encrypt (
25
20
& self ,
26
- nonce : N ,
21
+ packet_number : u64 ,
27
22
header : & [ u8 ] ,
28
23
extra_payload : Option < & [ u8 ] > ,
29
24
payload_and_tag : & mut [ u8 ] ,
30
25
) ;
26
+ }
31
27
32
- fn retransmission_tag (
33
- & self ,
34
- original_packet_number : u64 ,
35
- retransmission_packet_number : u64 ,
36
- tag_out : & mut [ u8 ] ,
37
- ) ;
28
+ pub trait Control {
29
+ fn tag_len ( & self ) -> usize ;
30
+
31
+ fn sign ( & self , header : & [ u8 ] , tag : & mut [ u8 ] ) ;
32
+ }
33
+
34
+ pub mod control {
35
+ use super :: * ;
36
+
37
+ /// Marker trait for keys to be used with stream control packets
38
+ pub trait Stream : Control {
39
+ fn retransmission_tag (
40
+ & self ,
41
+ original_packet_number : u64 ,
42
+ retransmission_packet_number : u64 ,
43
+ tag_out : & mut [ u8 ] ,
44
+ ) ;
45
+ }
46
+
47
+ /// Marker trait for keys to be used with secret control packets
48
+ pub trait Secret : Control { }
38
49
}
39
50
}
40
51
41
- pub mod decrypt {
52
+ pub mod open {
42
53
use super :: * ;
43
54
44
55
#[ derive( PartialEq , Eq , Clone , Copy , Debug ) ]
@@ -47,6 +58,10 @@ pub mod decrypt {
47
58
ReplayPotentiallyDetected { gap : Option < u64 > } ,
48
59
ReplayDefinitelyDetected ,
49
60
InvalidTag ,
61
+ SingleUseKey ,
62
+ UnsupportedOperation ,
63
+ MacOnly ,
64
+ RotationNotSupported ,
50
65
}
51
66
52
67
impl fmt:: Display for Error {
@@ -61,6 +76,12 @@ pub mod decrypt {
61
76
write ! ( f, "key replay potentially detected: unknown gap" )
62
77
}
63
78
Self :: InvalidTag => "invalid tag" . fmt ( f) ,
79
+ Self :: SingleUseKey => "this key can only be used once" . fmt ( f) ,
80
+ Self :: UnsupportedOperation => {
81
+ "this key cannot be used with the given operation" . fmt ( f)
82
+ }
83
+ Self :: MacOnly => "this key is only capable of generating MACs" . fmt ( f) ,
84
+ Self :: RotationNotSupported => "this key does not support key rotation" . fmt ( f) ,
64
85
}
65
86
}
66
87
}
@@ -69,38 +90,83 @@ pub mod decrypt {
69
90
70
91
pub type Result < T = ( ) , E = Error > = core:: result:: Result < T , E > ;
71
92
72
- pub trait Key {
73
- fn credentials ( & self ) -> & Credentials ;
74
-
93
+ pub trait Application {
75
94
fn tag_len ( & self ) -> usize ;
76
95
77
96
/// Decrypt a payload
78
- fn decrypt < N : IntoNonce > (
97
+ fn decrypt (
79
98
& self ,
80
99
key_phase : KeyPhase ,
81
- nonce : N ,
100
+ packet_number : u64 ,
82
101
header : & [ u8 ] ,
83
102
payload_in : & [ u8 ] ,
84
103
tag : & [ u8 ] ,
85
104
payload_out : & mut UninitSlice ,
86
105
) -> Result ;
87
106
88
107
/// Decrypt a payload
89
- fn decrypt_in_place < N : IntoNonce > (
108
+ fn decrypt_in_place (
90
109
& self ,
91
110
key_phase : KeyPhase ,
92
- nonce : N ,
111
+ packet_number : u64 ,
93
112
header : & [ u8 ] ,
94
113
payload_and_tag : & mut [ u8 ] ,
95
114
) -> Result ;
115
+ }
96
116
97
- fn retransmission_tag (
98
- & self ,
99
- key_phase : KeyPhase ,
100
- original_packet_number : u64 ,
101
- retransmission_packet_number : u64 ,
102
- tag_out : & mut [ u8 ] ,
103
- ) ;
117
+ pub trait Control {
118
+ fn tag_len ( & self ) -> usize ;
119
+
120
+ fn verify ( & self , header : & [ u8 ] , tag : & [ u8 ] ) -> Result ;
121
+ }
122
+
123
+ pub mod control {
124
+ use super :: * ;
125
+
126
+ /// Marker trait for keys to be used with stream control packets
127
+ pub trait Stream : Control {
128
+ fn retransmission_tag (
129
+ & self ,
130
+ original_packet_number : u64 ,
131
+ retransmission_packet_number : u64 ,
132
+ tag_out : & mut [ u8 ] ,
133
+ ) -> Result ;
134
+ }
135
+
136
+ pub mod stream {
137
+ /// A no-op implementation for reliable transports
138
+ #[ derive( Clone , Default ) ]
139
+ pub struct Reliable ( ( ) ) ;
140
+
141
+ impl super :: Control for Reliable {
142
+ #[ inline]
143
+ fn tag_len ( & self ) -> usize {
144
+ 16
145
+ }
146
+
147
+ #[ inline]
148
+ fn verify ( & self , _header : & [ u8 ] , _tag : & [ u8 ] ) -> super :: Result {
149
+ // this method should not be used on reliable transports
150
+ Err ( super :: Error :: UnsupportedOperation )
151
+ }
152
+ }
153
+
154
+ impl super :: Stream for Reliable {
155
+ #[ inline]
156
+ fn retransmission_tag (
157
+ & self ,
158
+ _original_packet_number : u64 ,
159
+ _retransmission_packet_number : u64 ,
160
+ _tag_out : & mut [ u8 ] ,
161
+ ) -> super :: Result {
162
+ // this method should not be used on reliable transports
163
+ Err ( super :: Error :: UnsupportedOperation )
164
+ }
165
+ }
166
+ }
167
+
168
+ /// Marker trait for keys to be used with secret control packets
169
+ pub trait Secret : Control { }
104
170
}
105
171
}
106
172
0 commit comments