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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//! Tests to verify we match with `jose jwk exc`
//!
//! Quoting from <https://www.mankier.com/1/jose-jwk-exc>:
//!
//!
//! > The ECMR algorithm has three modes of operation. Where the local key has a
//! > private key (the "d" property), it performs exactly like ECDH. If the local key
//! > does not have a private key and the remote key does have a private key, elliptic
//! > curve addition is performed on the two values. Otherwise, if neither the local
//! > key nor the remote key have a private key, the remote key is subtracted from the
//! > local key using elliptic curve subtraction. When using ECMR, be sure to validate
//! > the content of your inputs to avoid triggering the incorrect operation!
//!
//! We provide each mode with a separate function and test against the jose output
use Value;
use *;
use crateJwk;
// PUB and PRIV are random keys, not related
// It seems like jose doesn't do what we want unless we slap an `alg: ECMR` on everything
const PUB1: &str = r#"{
"alg": "ECMR",
"kty":"EC",
"crv":"P-521",
"x":"ARMEJ-j3d1XTYnWVIcTBLx-CsLW2L1j31j91AFw7Q-XIo1EvNQnSp9jZMp4BG9B2UOpeA9CpuByzCWkKR8j4Xlu4",
"y":"Abhfvfn8PCMeaD-zXRYGc2PNLPvx9lfXbO4ujG-oA2qVST0f_Gm1n1Fo64yhOA-POepFuzO3VQBSu2HBh2W_0AZY"
}"#;
const PUB2: &str = r#"{
"alg": "ECMR",
"crv": "P-521",
"d": "AfzVlb8M1oqMD81H9CM9WA29rAp6AwlHLYcHWnqhB-9qX3JfLAza2WiNS8WXpThl80QTiEyyFk-WWLoyAJ-dEWGU",
"kty": "EC",
"x": "AKUjAYzkwyvYMf_kaPB7sCpMmSXtAB0fEtpoomxJItcJh_uUbVAoCa2CEHmKvNYnDbOBlpuaZYHkfmrn00LaC-ys",
"y": "ASH_5_uGnl_nrtbaRYclJbK_6bxhz2oI7ZcAq9oIRGKF1YBX0X7cm50uXcqIn0E2ixNd8zvxG996_27ydO9KKTh8"
}"#;
const PRIV: &str = r#"{
"alg": "ECMR",
"kty":"EC",
"crv":"P-521",
"d":"AQYMXLDMMZ3zUX2fDquuF6DdKCv006AWC8JXWN380xPwnXkPucVnAYKcYPdHlAxVaN702rMY7Zy0ZdhzdCVE4MOy",
"x":"APEGU4eUd47tN9NMZUUZw5gdUI8ye7rV0DD46YIm2ilq3kHCsQAmqQzeBoW0CwtRia0lJTab3qs75EcNGMmgHCOZ",
"y":"AS0bF_r4j82E2hLXJCPtaHHPxX8JGTj97gyzIl29kiLrJbdWmYtSBM9OLLUuyZ5quexlKE2R5oKmCZgVMeq1pSjK"
}"#;
// Output from `jose jwk exc -l- -r- <<< "$priv$pub1"`
const DIFFIE_OUT: &str = r#"{
"crv": "P-521",
"kty": "EC",
"x": "AAZrHpTgNhFuxODvOSF8xgu1rB5jxlMQFHMEuen4LnIfJaHfKWOP0bfLWfoFV-t_myDTmXLJPPvIr__JJUGJOEyg",
"y": "AHCpxNlTWWRJ4eEK-wOgO6CM2HdJyMUj8ccKBWKDGZ0orxv6NI-XzXhFiYOe07blUAr2rkmuZI-EgoshRopkXgKO"
}"#;
// Output from `jose jwk exc -l- -r- <<< "$pub1$priv"`
const ADDITION: &str = r#"{
"crv": "P-521",
"kty": "EC",
"x": "AU9biY0WqoT-mi1-kJ0fUE5dpmnTTGNrNICn3j83t2GIjvd-Novwxrm8ktYj06fIcovfGJfhmVVu5VJWlVFVd7ax",
"y": "Aa44e2GyQKArXq39QPvTu4D_u8kNdSIXOZuWduRzwIXRwfkCGV177sAC4bQcKQdtb1kU20U589qeGG0dUwNCMpJD"
}"#;
// // Output from `jose jwk exc -l- -r- <<< "$pub1$pub2"`
// // This doesn't appear to actually be subtraction since this doesn't add up...
// const SUBTRACTION: &str = r#"{
// "crv": "P-521",
// "kty": "EC",
// "x": "AaG_X2ASudUbzUj2P3WkRc5EDmXrQhOykyqUMZzwe9hLecXqJ-lh5gETEHS1O0X0OIPR1CgBEMA4E_rnVJlQivx_",
// "y": "AL7_5J63H2BnK7g_jlyHs14E_HLTGTKLxgiGTCFBH1x66xPRiM9sgepOp1FKX2izNH0ZyrkMUSon2npDwkbAs7tG"
// }"#;
// FIXME:ecmr_sub: this is how ECMR subtraction is supposed to work according to the docs.
// However, I must be missing something because our results don't match.
// #[test]
// fn test_ecmr_sub() {
// let out = ecmr_sub(&pub1_jwk(), &pub2_jwk()).unwrap();
// assert_json_eq(&out.to_jwk_string(), SUBTRACTION);
// }
// FIXME:ecmr_sub: nothing to fix here, but note that this round trip test does
// indeed work, even though the `jose jwk` result above does not match.
/// Test the "Understanding the Algorithm" section of Tang
///
/// ```text
/// s = g * S # sJWK (Server advertisement)
/// c = g * C # cJWK (Client provisioning)
/// K = s * C # dJWK (Client provisioning)
///
/// K = c * S # dJWK (Server recovery)
/// ```seems
/// Verify the math that should work
/// Same as above but using our JWK wrapper functions
// /// Check that our encryption with a key matches `jose`
// ///
// /// Sample:
// ///
// /// ```sh
// /// jwe_base='{"protected":{"alg":"ECDH-ES","enc":"A256GCM"}}'
// /// jose jwe enc -i- -k- -I- -c <<< "$jwe_base$jwk\nhello"
// /// ```
// ///
// /// This generates a different output each time. For the sake of this test we will use the
// /// JWE in the test body with this jwk:
// ///
// /// ```text
// /// {
// /// "crv": "P-521",
// /// "kty": "EC",
// /// "x": "AbkDg4XtZlIWV-gkfQd6YYx7T7sMBds_xk-YANmBucBCPL4CMmiJP0VS6A0GvbimkZANrjmnC2PGwYvhVxY4StWd",
// /// "y": "AeIkvpdwKd-8l5sl01u-rP9ebNRvpgK3bSKCvI2BhhwLO7KD-EI8IFBC3_x8_hkpurNXhD5t2t8dInT37O3Y4Vr2"
// /// }
// /// ```
// #[test]
// fn test_encrypt() {
// use aes_gcm::{
// aead::{Aead, AeadCore, KeyInit, OsRng},
// Aes256Gcm, Key, Nonce,
// };
// use base64ct::{Base64UrlUnpadded, Encoding};
// use serde_json::Value;
// const JWE: &str = "\
// eyJhbGciOiJFQ0RILUVTIiwiZW5jIjoiQTI1NkdDTSIsImVwayI6eyJjcnYiOiJQLTUyMSIsImt0eSI6IkVDI\
// iwieCI6IkFMRkNUQ2s0YWdlRDlsUjlJWXVDVVpmbGFpN0wwNjJveUc5Q1FadFozLUhuanpYcWlJT2hCVnJBVH\
// JmdHQ1VEhSa2NiOVlSbDdnMnNqQmFxdGJIM1VhbE4iLCJ5IjoiQVQxby1falFBUFB1TWs2blFNM2dXOEVxaGV\
// 0Vmxxb3NPLXhjMWlFcDdoTDBOQUdFN1hCYkpUQTh2b05MZWw1bWxkVFJJRDUtclBKSG13WVp0UkNDeWVfcSJ9\
// fQ..YV2pQ8H5sCIEGEas.WpRgDKhTDf8.V0ZyG9OeGc68IZoGugqVEA";
// // Undo JWE compact serialization
// let mut split = JWE.split('.');
// let header = split.next().unwrap();
// let encrypted_key = split.next().unwrap();
// assert!(encrypted_key.is_empty());
// let iv = split.next().unwrap();
// let ciphertext = split.next().unwrap();
// let auth_tag = split.next().unwrap();
// assert!(split.next().is_none());
// let header = Base64UrlUnpadded::decode_vec(header).unwrap();
// let header = std::str::from_utf8(&header).unwrap();
// println!("header: {header}");
// let header_json: Value = serde_json::from_str(header).unwrap();
// let epk: Jwk =
// serde_json::from_value(header_json.as_object().unwrap().get("epk").unwrap().clone())
// .unwrap();
// // // public key used as encryption
// // const SAMPLE_ENC_JWK: &str = r#"{
// // "crv": "P-521",
// // "kty": "EC",
// // "x": "AbkDg4XtZlIWV-gkfQd6YYx7T7sMBds_xk-YANmBucBCPL4CMmiJP0VS6A0GvbimkZANrjmnC2PGwYvhVxY4StWd",
// // "y": "AeIkvpdwKd-8l5sl01u-rP9ebNRvpgK3bSKCvI2BhhwLO7KD-EI8IFBC3_x8_hkpurNXhD5t2t8dInT37O3Y4Vr2"
// // }"#;
// // let adv: Advertisment = serde_json::from_str(SAMPLE_JWS).unwrap();
// // let (keys, thp) = adv.validate_into_keys(None).unwrap();
// // let mut gk = keys.make_tang_enc_key::<32>("wxyz", thp).unwrap();
// // // Configure things to match up with Clevis, and use a fixed EPC for repeated tests
// // gk.meta.enc = Some("A256GCM".into());
// // gk.meta.epk = serde_json::from_str(SAMPLE_EPK).unwrap();
// // // This output is `jwe_base` in our command
// // let mut val = serde_json::to_value(&gk.meta).unwrap();
// // val.as_object_mut().unwrap().remove("epk");
// // let jwe_base = serde_json::json!({"protected": val});
// // println!("{}", serde_json::to_string(&jwe_base).unwrap());
// // let key: &Key<Aes256Gcm> = gk.encryption_key.as_bytes().into();
// // // let nonce: & Nonce<Aes256Gcm::NonceSize> =
// panic!()
// }