forked from hyperledger-archives/indy-sdk
/
crypto.rs
711 lines (567 loc) · 27.5 KB
/
crypto.rs
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
#[macro_use]
mod utils;
inject_indy_dependencies!();
extern crate indyrs as indy;
extern crate indyrs as api;
use crate::utils::crypto;
use crate::utils::constants::*;
use crate::utils::Setup;
use self::indy::ErrorCode;
pub const ENCRYPTED_MESSAGE: &'static [u8; 45] = &[187, 227, 10, 29, 46, 178, 12, 179, 197, 69, 171, 70, 228, 204, 52, 22, 199, 54, 62, 13, 115, 5, 216, 66, 20, 131, 121, 29, 251, 224, 253, 201, 75, 73, 225, 237, 219, 133, 35, 217, 131, 135, 232, 129, 32];
pub const SIGNATURE: &'static [u8; 64] = &[169, 215, 8, 225, 7, 107, 110, 9, 193, 162, 202, 214, 162, 66, 238, 211, 63, 209, 12, 196, 8, 211, 55, 27, 120, 94, 204, 147, 53, 104, 103, 61, 60, 249, 237, 127, 103, 46, 220, 223, 10, 95, 75, 53, 245, 210, 241, 151, 191, 41, 48, 30, 9, 16, 78, 252, 157, 206, 210, 145, 125, 133, 109, 11];
mod high_cases {
use super::*;
mod create_key {
use super::*;
use rust_base58::FromBase58;
#[test]
fn indy_create_key_works_for_seed() {
let setup = Setup::wallet();
let verkey = crypto::create_key(setup.wallet_handle, Some(MY1_SEED)).unwrap();
assert_eq!(verkey.from_base58().unwrap().len(), 32);
}
#[test]
fn indy_create_key_works_without_seed() {
let setup = Setup::wallet();
let verkey = crypto::create_key(setup.wallet_handle, None).unwrap();
assert_eq!(verkey.from_base58().unwrap().len(), 32);
}
}
mod set_key_metadata {
use super::*;
#[test]
fn indy_set_key_metadata_works() {
let setup = Setup::did();
crypto::set_key_metadata(setup.wallet_handle, &setup.verkey, METADATA).unwrap();
}
#[test]
fn indy_set_key_metadata_works_for_replace() {
let setup = Setup::did();
crypto::set_key_metadata(setup.wallet_handle, &setup.verkey, METADATA).unwrap();
let metadata = crypto::get_key_metadata(setup.wallet_handle, &setup.verkey).unwrap();
assert_eq!(METADATA.to_string(), metadata);
let new_metadata = "updated metadata";
crypto::set_key_metadata(setup.wallet_handle, &setup.verkey, new_metadata).unwrap();
let updated_metadata = crypto::get_key_metadata(setup.wallet_handle, &setup.verkey).unwrap();
assert_eq!(new_metadata, updated_metadata);
}
}
mod get_key_metadata {
use super::*;
#[test]
fn indy_get_key_metadata_works() {
let setup = Setup::did();
crypto::set_key_metadata(setup.wallet_handle, &setup.verkey, METADATA).unwrap();
let metadata = crypto::get_key_metadata(setup.wallet_handle, &setup.verkey).unwrap();
assert_eq!(METADATA.to_string(), metadata);
}
#[test]
fn indy_get_key_metadata_works_for_no_metadata() {
let setup = Setup::did();
let res = crypto::get_key_metadata(setup.wallet_handle, &setup.verkey);
assert_code!(ErrorCode::WalletItemNotFound, res);
}
}
mod crypto_sign {
use super::*;
#[test]
fn indy_crypto_sign_works() {
let setup = Setup::wallet();
let my_vk = crypto::create_key(setup.wallet_handle, Some(MY1_SEED)).unwrap();
let signature = crypto::sign(setup.wallet_handle, &my_vk, MESSAGE.as_bytes()).unwrap();
assert_eq!(SIGNATURE.to_vec(), signature);
}
#[test]
fn indy_crypto_sign_works_for_unknown_signer() {
let setup = Setup::wallet();
let res = crypto::sign(setup.wallet_handle, VERKEY, MESSAGE.as_bytes());
assert_code!(ErrorCode::WalletItemNotFound, res);
}
}
mod crypto_verify {
use super::*;
#[test]
fn indy_crypto_verify_works() {
let valid = crypto::verify(&VERKEY_MY1, MESSAGE.as_bytes(), SIGNATURE).unwrap();
assert!(valid);
}
#[test]
fn indy_crypto_verify_works_for_other_signer() {
let valid = crypto::verify(&VERKEY_MY2, MESSAGE.as_bytes(), SIGNATURE).unwrap();
assert!(!valid);
}
#[test]
fn indy_crypto_verify_works_for_invalid_signature_len() {
let signature: Vec<u8> = vec![20, 191, 100, 213, 101, 12, 197, 198, 203, 49, 89, 220, 205, 192, 224, 221, 97, 77, 220, 190];
let res = crypto::verify(&VERKEY_MY1, MESSAGE.as_bytes(), &signature);
assert_code!(ErrorCode::CommonInvalidStructure, res);
}
}
mod auth_crypt {
use super::*;
#[test]
fn indy_crypto_auth_crypt_works_for_created_key() {
let setup = Setup::wallet();
let verkey = crypto::create_key(setup.wallet_handle, Some(MY1_SEED)).unwrap();
crypto::auth_crypt(setup.wallet_handle, &verkey, VERKEY_MY2, MESSAGE.as_bytes()).unwrap();
}
#[test]
fn indy_crypto_auth_crypt_works_for_created_did() {
let setup = Setup::did();
crypto::auth_crypt(setup.wallet_handle, &setup.verkey, VERKEY_MY2, MESSAGE.as_bytes()).unwrap();
}
#[test]
fn indy_crypto_auth_crypt_works_for_unknown_sender_verkey() {
let setup = Setup::wallet();
let res = crypto::auth_crypt(setup.wallet_handle, VERKEY_MY2, VERKEY, MESSAGE.as_bytes());
assert_code!(ErrorCode::WalletItemNotFound, res);
}
}
mod auth_decrypt {
use super::*;
#[test]
fn indy_crypto_auth_decrypt_works() {
let sender_setup = Setup::key();
let recipient_setup = Setup::key();
let encrypted_msg = crypto::auth_crypt(sender_setup.wallet_handle, &sender_setup.verkey, &recipient_setup.verkey, MESSAGE.as_bytes()).unwrap();
let (vk, msg) = crypto::auth_decrypt(recipient_setup.wallet_handle, &recipient_setup.verkey, &encrypted_msg).unwrap();
assert_eq!(MESSAGE.as_bytes().to_vec(), msg);
assert_eq!(sender_setup.verkey, vk);
}
#[test]
fn indy_crypto_auth_decrypt_works_for_unknown_recipient_vk() {
let setup = Setup::key();
let encrypted_msg = crypto::auth_crypt(setup.wallet_handle, &setup.verkey, &VERKEY_TRUSTEE, MESSAGE.as_bytes()).unwrap();
let res = crypto::anon_decrypt(setup.wallet_handle, &VERKEY_TRUSTEE, &encrypted_msg);
assert_code!(ErrorCode::WalletItemNotFound, res);
}
}
mod anon_crypt {
use super::*;
#[test]
fn indy_anon_crypt_works() {
Setup::empty();
crypto::anon_crypt(VERKEY_MY2, &MESSAGE.as_bytes()).unwrap();
}
}
mod anon_decrypt {
use super::*;
#[test]
fn indy_crypto_anon_decrypt_works() {
let setup = Setup::key();
let encrypted_msg = crypto::anon_crypt(&setup.verkey, MESSAGE.as_bytes()).unwrap();
let msg = crypto::anon_decrypt(setup.wallet_handle, &setup.verkey, &encrypted_msg).unwrap();
assert_eq!(MESSAGE.as_bytes().to_vec(), msg);
}
#[test]
fn indy_crypto_anon_decrypt_works_for_unknown_verkey() {
let setup = Setup::wallet();
let encrypted_msg = crypto::anon_crypt(&VERKEY_TRUSTEE, MESSAGE.as_bytes()).unwrap();
let res = crypto::anon_decrypt(setup.wallet_handle, &VERKEY_TRUSTEE, &encrypted_msg);
assert_code!(ErrorCode::WalletItemNotFound, res);
}
}
mod pack_message_authcrypt {
use super::*;
#[test]
fn indy_pack_message_authcrypt_works() {
let setup = Setup::key();
let rec_key_vec = vec![VERKEY_MY1, VERKEY_MY2, VERKEY_TRUSTEE];
let receiver_keys = serde_json::to_string(&rec_key_vec).unwrap();
let message = "Hello World".as_bytes();
let res = crypto::pack_message(setup.wallet_handle, message, &receiver_keys, Some(&setup.verkey));
assert!(res.is_ok());
}
}
mod pack_message_anoncrypt {
use super::*;
#[test]
fn indy_pack_message_anon_works() {
let setup = Setup::wallet();
let rec_key_vec = vec![VERKEY_MY1, VERKEY_MY2, VERKEY_TRUSTEE];
let receiver_keys = serde_json::to_string(&rec_key_vec).unwrap();
let message = "Hello World".as_bytes();
let res = crypto::pack_message(setup.wallet_handle, message, &receiver_keys, None);
assert!(res.is_ok());
}
}
mod unpack_message_authcrypt {
use super::*;
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct UnpackMessage {
pub message: String,
pub sender_verkey: String,
pub recipient_verkey: String,
}
#[test]
fn indy_unpack_message_authcrypt_works() {
//Test setup
let sender_setup = Setup::key();
let receiver_setup = Setup::key();
let rec_key_vec = vec![VERKEY_TRUSTEE, &receiver_setup.verkey];
let receiver_keys = serde_json::to_string(&rec_key_vec).unwrap();
let pack_message = crypto::pack_message(sender_setup.wallet_handle, AGENT_MESSAGE.as_bytes(), &receiver_keys, Some(&sender_setup.verkey)).unwrap();
//execute function
let res = crypto::unpack_message(receiver_setup.wallet_handle, pack_message.as_slice()).unwrap();
let res_serialized: UnpackMessage = serde_json::from_slice(res.as_slice()).unwrap();
//verify unpack ran correctly
assert_eq!(res_serialized.message, AGENT_MESSAGE.to_string());
assert_eq!(res_serialized.sender_verkey, sender_setup.verkey);
assert_eq!(res_serialized.recipient_verkey, receiver_setup.verkey);
}
#[test]
fn indy_unpack_message_authcrypt_fails_no_matching_key() {
//Test Setup
let sender_setup = Setup::key();
let receiver_setup = Setup::key();
let rec_key_vec = vec![VERKEY_TRUSTEE];
let receiver_keys = serde_json::to_string(&rec_key_vec).unwrap();
let message = "Hello World".as_bytes();
let pack_message = crypto::pack_message(sender_setup.wallet_handle, message, &receiver_keys, Some(&sender_setup.verkey)).unwrap();
//execute function
let res = crypto::unpack_message(receiver_setup.wallet_handle, pack_message.as_slice());
assert_code!(ErrorCode::WalletItemNotFound, res);
}
}
mod unpack_message_anoncrypt {
use super::*;
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct UnpackMessage {
pub message: String,
pub recipient_verkey: String,
}
#[test]
fn indy_unpack_message_anoncrypt_works() {
let sender_setup = Setup::key();
let receiver_setup = Setup::key();
let rec_key_vec = vec![VERKEY_TRUSTEE, &receiver_setup.verkey];
let receiver_keys = serde_json::to_string(&rec_key_vec).unwrap();
let pack_message = crypto::pack_message(sender_setup.wallet_handle, AGENT_MESSAGE.as_bytes(), &receiver_keys, None).unwrap();
let res = crypto::unpack_message(receiver_setup.wallet_handle, pack_message.as_slice()).unwrap();
let res_serialized: UnpackMessage = serde_json::from_slice(res.as_slice()).unwrap();
assert_eq!(res_serialized.message, AGENT_MESSAGE.to_string());
assert_eq!(res_serialized.recipient_verkey, receiver_setup.verkey);
}
#[test]
fn indy_unpack_message_anoncrypt_fails_no_matching_key() {
//Test Setup
let sender_setup = Setup::key();
let receiver_setup = Setup::key();
let rec_key_vec = vec![VERKEY_TRUSTEE];
let receiver_keys = serde_json::to_string(&rec_key_vec).unwrap();
let message = "Hello World".as_bytes();
let pack_message = crypto::pack_message(sender_setup.wallet_handle, message, &receiver_keys, None).unwrap();
//execute function
let res = crypto::unpack_message(receiver_setup.wallet_handle, pack_message.as_slice());
assert_code!(ErrorCode::WalletItemNotFound, res);
}
}
}
#[cfg(not(feature = "only_high_cases"))]
mod medium_cases {
use super::*;
use crate::utils::did;
use crate::api::INVALID_WALLET_HANDLE;
mod create_key {
use super::*;
#[test]
fn indy_create_key_works_for_invalid_wallet_handle() {
Setup::empty();
let res = crypto::create_key(INVALID_WALLET_HANDLE, None);
assert_code!(ErrorCode::WalletInvalidHandle, res);
}
}
mod set_key_metadata {
use super::*;
#[test]
fn indy_set_key_metadata_works_for_invalid_handle() {
let setup = Setup::did();
let res = crypto::set_key_metadata(INVALID_WALLET_HANDLE, &setup.verkey, METADATA);
assert_code!(ErrorCode::WalletInvalidHandle, res);
}
#[test]
fn indy_set_key_metadata_works_for_empty_string() {
let setup = Setup::did();
crypto::set_key_metadata(setup.wallet_handle, &setup.verkey, "").unwrap();
}
#[test]
fn indy_set_key_metadata_works_for_invalid_key() {
let setup = Setup::did();
let res = crypto::set_key_metadata(setup.wallet_handle, INVALID_BASE58_VERKEY, METADATA);
assert_code!(ErrorCode::CommonInvalidStructure, res);
}
}
mod get_key_metadata {
use super::*;
#[test]
fn indy_get_key_metadata_works_for_empty_string() {
let setup = Setup::did();
crypto::set_key_metadata(setup.wallet_handle, &setup.verkey, "").unwrap();
let metadata = crypto::get_key_metadata(setup.wallet_handle, &setup.verkey).unwrap();
assert_eq!("", metadata);
}
#[test]
fn indy_get_key_metadata_works_for_invalid_handle() {
let setup = Setup::did();
crypto::set_key_metadata(setup.wallet_handle, &setup.verkey, METADATA).unwrap();
let res = crypto::get_key_metadata(INVALID_WALLET_HANDLE, &setup.verkey);
assert_code!(ErrorCode::WalletInvalidHandle, res);
}
}
mod crypto_sign {
use super::*;
#[test]
fn indy_crypto_sign_works_for_invalid_wallet_handle() {
let setup = Setup::did();
let res = crypto::sign(INVALID_WALLET_HANDLE, &setup.verkey, MESSAGE.as_bytes());
assert_code!(ErrorCode::WalletInvalidHandle, res);
}
}
mod crypto_verify {
use super::*;
#[test]
fn indy_crypto_verify_works_for_verkey_with_correct_crypto_type() {
let verkey = VERKEY_MY1.to_owned() + ":ed25519";
let valid = crypto::verify(&verkey, MESSAGE.as_bytes(), SIGNATURE).unwrap();
assert!(valid);
}
#[test]
fn indy_crypto_verify_works_for_verkey_with_invalid_crypto_type() {
let verkey = VERKEY_MY1.to_owned() + ":unknown_crypto";
let res = crypto::verify(&verkey, MESSAGE.as_bytes(), SIGNATURE);
assert_code!(ErrorCode::UnknownCryptoTypeError, res);
}
}
mod auth_crypt {
use super::*;
#[test]
fn indy_crypto_auth_crypt_works_for_created_did_as_cid() {
let setup = Setup::wallet();
let (_, verkey) = did::create_my_did(setup.wallet_handle, &json!({ "seed": MY1_SEED, "cid": true }).to_string()).unwrap();
crypto::auth_crypt(setup.wallet_handle, &verkey, VERKEY_MY2, MESSAGE.as_bytes()).unwrap();
}
#[test]
fn indy_crypto_auth_crypt_works_for_invalid_wallet_handle() {
let setup = Setup::did();
let res = crypto::auth_crypt(INVALID_WALLET_HANDLE, &setup.verkey, VERKEY, MESSAGE.as_bytes());
assert_code!(ErrorCode::WalletInvalidHandle, res);
}
#[test]
fn indy_crypto_auth_crypt_works_for_invalid_recipient_vk() {
let setup = Setup::did();
let res = crypto::auth_crypt(setup.wallet_handle, &setup.verkey, INVALID_BASE58_VERKEY, MESSAGE.as_bytes());
assert_code!(ErrorCode::CommonInvalidStructure, res);
}
}
mod auth_decrypt {
use super::*;
#[test]
fn indy_crypto_auth_decrypt_works_for_invalid_msg() {
let sender_setup = Setup::key();
let recipient_setup = Setup::did();
did::store_their_did_from_parts(sender_setup.wallet_handle, &recipient_setup.did, &recipient_setup.verkey).unwrap();
let encrypted_msg = format!(r#"{{"nonce":"Th7MpTaRZVRYnPiabds81Y12","sender":"{:?}","msg":"{:?}"}}"#, VERKEY, ENCRYPTED_MESSAGE.to_vec());
let res = crypto::auth_decrypt(recipient_setup.wallet_handle, &recipient_setup.verkey, &encrypted_msg.as_bytes());
assert_code!(ErrorCode::CommonInvalidStructure, res);
}
#[test]
fn indy_crypto_auth_decrypt_works_invalid_handle() {
let sender_setup = Setup::key();
let recipient_setup = Setup::key();
let encrypted_msg = crypto::auth_crypt(sender_setup.wallet_handle, &sender_setup.verkey, &recipient_setup.verkey, MESSAGE.as_bytes()).unwrap();
let res = crypto::auth_decrypt(INVALID_WALLET_HANDLE, &recipient_setup.verkey, &encrypted_msg);
assert_code!(ErrorCode::WalletInvalidHandle, res);
}
}
mod anon_crypt {
use super::*;
#[test]
fn indy_anon_crypt_works_for_invalid_their_vk() {
Setup::empty();
let res = crypto::anon_crypt(INVALID_VERKEY_LENGTH, &MESSAGE.as_bytes());
assert_code!(ErrorCode::CommonInvalidStructure, res);
let res = crypto::anon_crypt(INVALID_BASE58_VERKEY, &MESSAGE.as_bytes());
assert_code!(ErrorCode::CommonInvalidStructure, res);
}
}
mod anon_decrypt {
use super::*;
#[test]
fn indy_crypto_anon_decrypt_works_for_invalid_msg() {
let setup = Setup::key();
let res = crypto::anon_decrypt(setup.wallet_handle, &setup.verkey, &"unencrypted message".as_bytes());
assert_code!(ErrorCode::CommonInvalidStructure, res);
}
#[test]
fn indy_crypto_anon_decrypt_works_invalid_handle() {
let setup = Setup::key();
let encrypted_msg = crypto::anon_crypt(&setup.verkey, MESSAGE.as_bytes()).unwrap();
let res = crypto::anon_decrypt(INVALID_WALLET_HANDLE, &setup.verkey, &encrypted_msg);
assert_code!(ErrorCode::WalletInvalidHandle, res);
}
}
mod pack_message_authcrypt {
use super::*;
#[test]
fn indy_pack_message_authcrypt_fails_empty_message() {
let setup = Setup::key();
let rec_key_vec = vec![VERKEY_MY1, VERKEY_MY2, VERKEY_TRUSTEE];
let receiver_keys = serde_json::to_string(&rec_key_vec).unwrap();
let message = "".as_bytes();
let res = crypto::pack_message(setup.wallet_handle, message, &receiver_keys, Some(&setup.verkey));
assert_code!(ErrorCode::CommonInvalidParam3, res);
}
#[test]
fn indy_pack_message_authcrypt_fails_no_receivers() {
let setup = Setup::key();
let receiver_keys = "[]";
let message = "Hello World".as_bytes();
let res = crypto::pack_message(setup.wallet_handle, message, &receiver_keys, Some(&setup.verkey));
assert_code!(ErrorCode::CommonInvalidParam4, res);
}
#[test]
fn indy_pack_message_authcrypt_fails_bad_wallet_handle() {
let setup = Setup::key();
let rec_key_vec = vec![VERKEY_MY1, VERKEY_MY2, VERKEY_TRUSTEE];
let receiver_keys = serde_json::to_string(&rec_key_vec).unwrap();
let message = "Hello World".as_bytes();
let res = crypto::pack_message(INVALID_WALLET_HANDLE, message, &receiver_keys, Some(&setup.verkey));
assert_code!(ErrorCode::WalletInvalidHandle, res);
}
#[test]
fn indy_pack_message_authcrypt_fails_invalid_verkey() {
let setup = Setup::wallet();
let rec_key_vec = vec![VERKEY_MY1, VERKEY_MY2, VERKEY_TRUSTEE];
let receiver_keys = serde_json::to_string(&rec_key_vec).unwrap();
let message = "Hello World".as_bytes();
let res = crypto::pack_message(setup.wallet_handle, message, &receiver_keys, Some(INVALID_BASE58_VERKEY));
assert_code!(ErrorCode::CommonInvalidStructure, res);
}
}
mod pack_message_anoncrypt {
use super::*;
#[test]
fn indy_pack_message_anoncrypt_fails_empty_message() {
let setup = Setup::wallet();
let rec_key_vec = vec![VERKEY_MY1, VERKEY_MY2, VERKEY_TRUSTEE];
let receiver_keys = serde_json::to_string(&rec_key_vec).unwrap();
let message = "".as_bytes();
let res = crypto::pack_message(setup.wallet_handle, message, &receiver_keys, None);
assert_code!(ErrorCode::CommonInvalidParam3, res);
}
#[test]
fn indy_pack_message_anoncrypt_fails_no_receivers() {
let setup = Setup::wallet();
let receiver_keys = "[]";
let message = "Hello World".as_bytes();
let res = crypto::pack_message(setup.wallet_handle, message, &receiver_keys, None);
assert_code!(ErrorCode::CommonInvalidParam4, res);
}
#[test]
fn indy_pack_message_anoncrypt_passes_bad_wallet_handle() {
let rec_key_vec = vec![VERKEY_MY1, VERKEY_MY2, VERKEY_TRUSTEE];
let receiver_keys = serde_json::to_string(&rec_key_vec).unwrap();
let message = "Hello World".as_bytes();
//The wallet_handle and sender aren't used in this case, so any wallet_handle whether inited or not will work
let res = crypto::pack_message(INVALID_WALLET_HANDLE, message, &receiver_keys, None);
assert!(res.is_ok());
}
}
}
#[cfg(not(feature = "only_high_cases"))]
mod load {
extern crate rand;
use super::*;
use self::rand::RngCore;
use self::rand::rngs::OsRng;
use std::cmp::max;
use std::thread;
use std::time::{Duration, SystemTime};
use crate::utils::{wallet, did};
const AGENT_CNT: usize = 10;
const DATA_SZ: usize = 10 * 1024;
const OPERATIONS_CNT: usize = 10;
/**
Environment variables can be used for tuning this test:
- AGENTS_CNT - count of parallel agents
- OPERATIONS_CNT - operations per agent (consequence in same agent)
- DATA_SZ - data size for encryption
*/
#[test]
fn parallel_auth_encrypt() {
Setup::empty();
let agent_cnt = std::env::var("AGENTS_CNT").ok().and_then(|s| s.parse::<usize>().ok()).unwrap_or(AGENT_CNT);
let data_sz = std::env::var("DATA_SZ").ok().and_then(|s| s.parse::<usize>().ok()).unwrap_or(DATA_SZ);
let operations_cnt = std::env::var("OPERATIONS_CNT").ok().and_then(|s| s.parse::<usize>().ok()).unwrap_or(OPERATIONS_CNT);
let mut agents = Vec::new();
let mut os_rng = OsRng;
for i in 0..agent_cnt {
let (wallet, wallet_config) = wallet::create_and_open_default_wallet(&format!("parallel_auth_encrypt-{}", i)).unwrap();
let (_did, verkey) = did::create_and_store_my_did(wallet, None).unwrap();
let mut data = vec![0u8; data_sz];
os_rng.fill_bytes(&mut data.as_mut_slice());
agents.push((wallet, verkey, data, wallet_config));
}
let start_time = SystemTime::now();
let mut results = Vec::new();
for (wallet, verkey, data, wallet_config) in agents {
let thread = thread::spawn(move || {
let mut time_diffs = Vec::new();
for _ in 0..operations_cnt {
let time = SystemTime::now();
let _encrypted = crypto::auth_crypt(wallet, &verkey, &verkey, data.as_slice()).unwrap();
let time_diff = SystemTime::now().duration_since(time).unwrap();
time_diffs.push(time_diff);
}
wallet::close_wallet(wallet).unwrap();
wallet::delete_wallet(&wallet_config, WALLET_CREDENTIALS).unwrap();
time_diffs
});
results.push(thread);
}
let mut all_diffs = Vec::new();
for result in results {
all_diffs.push(result.join().unwrap());
}
let total_duration = SystemTime::now().duration_since(start_time).unwrap();
let mut time_diff_max = Duration::from_secs(0);
let mut time_sum_diff = Duration::from_secs(0);
for time_diffs in all_diffs {
warn!("{:?}", time_diffs);
time_diff_max = time_diffs.iter().fold(time_diff_max, |acc, cur| max(acc, *cur));
time_sum_diff = time_diffs.iter().fold(time_sum_diff, |acc, cur| acc + *cur);
}
warn!("================= Settings =================\n\
Agent cnt: \t{:?}\n\
Operations per agent cnt:\t{:?}\n\
Data size: \t{:?}",
agent_cnt, operations_cnt, data_sz);
warn!("================= Summary =================\n\
Max pending: \t{:?}\n\
Total ops cnt: \t{:?}\n\
Sum pending: \t{:?}\n\
Total duration:\t{:?}",
time_diff_max, agent_cnt * operations_cnt, time_sum_diff, total_duration);
}
}
#[test] // aries-396
fn crypto_unpack_data_with_and_without_padding_works() {
let setup_trustee = Setup::trustee();
let actual_message = "Hello World";
// With padding
{
let jwe = json!({ "protected": "eyJlbmMiOiJ4Y2hhY2hhMjBwb2x5MTMwNV9pZXRmIiwidHlwIjoiSldNLzEuMCIsImFsZyI6IkF1dGhjcnlwdCIsInJlY2lwaWVudHMiOlt7ImVuY3J5cHRlZF9rZXkiOiIxamlhNk4zOUhYQVZvQzUzT2xpVE14T0ZobjVKVE1jLXlwOGVXcjVERndJM1psN01HNW5IcFF2ejNMUVJjMUpvIiwiaGVhZGVyIjp7ImtpZCI6IkdKMVN6b1d6YXZRWWZOTDlYa2FKZHJRZWpmenRONFhxZHNpVjRjdDNMWEtMIiwiaXYiOiJGTS1MUktQV1hEQjRGdVVvc1RDQWxYcHR5M2ZCWVZpTCIsInNlbmRlciI6IjQ4Q2l5UXZTWWNmcEdCbzQtSTlkeGpmOW8yamZsQ1ZYVzY0TnFETEVDSFVPVXNnY2ZDLTFWaHQyMzNHZmNqTWYyVWxiNDZzd2pDVjJoUGFCUlNOMDB5YTNCVzBETC11V3FQMXd3NEVET0dBdk1HQUNzeThHY0w5NEhRcz0ifX1dfQ==", "iv": "Q6Nv06ZdUXQ53woe", "ciphertext": "rFsCeJO8mHLwVU4=", "tag": "wt4oAZ74LCW40xRqfdc6Bg==" });
let jwe = serde_json::to_vec(&jwe).unwrap();
let res = crypto::unpack_message(setup_trustee.wallet_handle, &jwe).unwrap();
let res = serde_json::from_slice::<::serde_json::Value>(res.as_slice()).unwrap();
assert_eq!(actual_message, res["message"].as_str().unwrap());
}
// Without padding
{
let jwe = json!({ "protected": "eyJlbmMiOiJ4Y2hhY2hhMjBwb2x5MTMwNV9pZXRmIiwidHlwIjoiSldNLzEuMCIsImFsZyI6IkF1dGhjcnlwdCIsInJlY2lwaWVudHMiOlt7ImVuY3J5cHRlZF9rZXkiOiJMZENfMzZzRHBFeXI5dGlKOHZLVENhMUFkcExXYWtoUzMxbWtsdk9oQlF3QnFKSVpvV0FrWlZzazRVYUg2VDJXIiwiaGVhZGVyIjp7ImtpZCI6IkdKMVN6b1d6YXZRWWZOTDlYa2FKZHJRZWpmenRONFhxZHNpVjRjdDNMWEtMIiwiaXYiOiJjZjY0LUlHRUh4R09MWXRtRlVSU3F3NWQ2U05fN0xxTCIsInNlbmRlciI6Il9ITDlWUDIxZmtiX2R3QmdTLS1OaVRJY2FjZVVTTGR3UDFtSFRURmJEWEk0MkFLblZDd0tHcXY0bmxHTk5qSHVVampPRUV1cGZYR3drWjZBTGJiNGFtTzJsbHpNQzZLYm9BTVNob19UckpaTVdXbGN2dnljc3VrMlRxYyJ9fV19", "iv": "fzGusjZNVbCo17dT", "ciphertext": "4xrn1rPkpDx4eEg", "tag": "ff3cppEUYi-6yC_ZvWhjoQ" });
let jwe = serde_json::to_vec(&jwe).unwrap();
let res = crypto::unpack_message(setup_trustee.wallet_handle, &jwe).unwrap();
let res = serde_json::from_slice::<::serde_json::Value>(res.as_slice()).unwrap();
assert_eq!(actual_message, res["message"].as_str().unwrap());
}
}