ncsisc 0.2.5

A library for school projects
Documentation
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
pub mod kleptographic {
    pub use curv::arithmetic::Converter;
    pub use curv::elliptic::curves::{Point, Scalar, Secp256k1};
    pub use curv::BigInt;
    pub use openssl::hash::{Hasher, MessageDigest};
    pub use rand::thread_rng;
    use rand::{AsByteSliceMut, Rng};
    use serde::{Deserialize, Serialize, Serializer};
    use sha3::{Digest, Keccak256};
    use std::env;
    use std::fs::File;
    use std::io::Write;

    #[derive(Clone, Debug)]
    pub struct Param {
        a: Scalar<Secp256k1>,
        b: Scalar<Secp256k1>,
        h: Scalar<Secp256k1>,
        e: Scalar<Secp256k1>,
    }

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct Signature {
        pub r: Scalar<Secp256k1>,
        pub s: Scalar<Secp256k1>,
        pub v: BigInt,
    }

    #[derive(Clone, Debug)]
    pub struct KeyPair {
        pub private: Scalar<Secp256k1>,
        pub public: Point<Secp256k1>,
    }

    impl KeyPair {
        pub fn new(private: Scalar<Secp256k1>) -> Self {
            KeyPair {
                private: private.clone(),
                public: private * Point::generator(),
            }
        }
        // the fingerprint will always end with '='
        pub fn fingerprint(&self) -> String {
            let bytes = self.public.to_bytes(false);
            let hash = fast_hash(&*bytes);
            base64::encode(&hash)
        }
        pub fn save(&self) -> std::io::Result<()> {
            let mut base_path = env::current_dir()?;
            base_path.push("keys");
            let mut path1 = base_path.clone();
            path1.push("server_host_key");
            let mut path2 = base_path.clone();
            path2.push("server_host_key");
            path2.set_extension("pub");

            let private_key = self.private.to_bigint().to_bytes();
            let mut public_key = self.public.to_bytes(false);

            if let Ok(mut file) = File::create(path1.clone()) {
                file.write(&private_key)?;
            }

            if let Ok(mut file) = File::create(path2.clone()) {
                file.write(&public_key)?;
            }

            Ok(())
        }
    }
    impl Signature {
        pub fn new() -> Self {
            let r: Scalar<Secp256k1> = Scalar::from(0);
            let s: Scalar<Secp256k1> = Scalar::from(0);
            let v = BigInt::from(0);
            Signature { r, s, v }
        }
    }

    impl Param {
        pub fn new() -> Self {
            Param {
                a: Scalar::random(),
                b: Scalar::random(),
                h: Scalar::random(),
                e: Scalar::random(),
            }
        }
        pub fn from(
            a: Scalar<Secp256k1>,
            b: Scalar<Secp256k1>,
            h: Scalar<Secp256k1>,
            e: Scalar<Secp256k1>,
        ) -> Self {
            Param { a, b, h, e }
        }
    }

    // fast hash function using sm3
    pub fn fast_hash(data: &[u8]) -> Vec<u8> {
        use libsm::sm3::hash::Sm3Hash;
        let mut hash = Sm3Hash::new(data);
        Vec::from(hash.get_hash())
    }

    pub fn sign(message: String, keypair: KeyPair, k: Scalar<Secp256k1>) -> Option<Signature> {
        let mut hasher = Hasher::new(MessageDigest::sha256()).expect("Init hasher error");
        hasher.update(message.as_bytes()).expect("Hash error");
        let m: Scalar<Secp256k1> = Scalar::from_bytes(hasher.finish().unwrap().as_ref()).unwrap();
        let signature_point = k.clone() * Point::generator();

        let mut v = signature_point.y_coord().unwrap() & BigInt::from(1 as u16);

        let r: Scalar<Secp256k1> = Scalar::from_bigint(&signature_point.x_coord().unwrap());
        let s: Scalar<Secp256k1> =
            k.clone().invert().unwrap() * (m.clone() + r.clone() * keypair.private.clone());

        let n = Scalar::<Secp256k1>::group_order();
        if s.clone().to_bigint() > n / 2 {
            v = v ^ BigInt::from(1 as u16);
        }

        Some(Signature { r, s, v })
    }
    pub fn sign_hash(hash: Vec<u8>, keypair: KeyPair, k: Scalar<Secp256k1>) -> Option<Signature> {
        let m: Scalar<Secp256k1> = Scalar::from_bytes(&hash).unwrap();
        let signature_point = k.clone() * Point::generator();

        let mut v = signature_point.y_coord().unwrap() & BigInt::from(1 as u16);

        let r: Scalar<Secp256k1> = Scalar::from_bigint(&signature_point.x_coord().unwrap());
        let s: Scalar<Secp256k1> =
            k.clone().invert().unwrap() * (m.clone() + r.clone() * keypair.private.clone());

        let n = Scalar::<Secp256k1>::group_order();
        if s.clone().to_bigint() > n / 2 {
            v = v ^ BigInt::from(1 as u16);
        }
        Some(Signature { r, s, v })
    }
    pub fn mal_sign(
        message1: String,
        message2: String,
        param: Param,
        user_key_pair: KeyPair,
        attacker_key_pair: KeyPair,
    ) -> Option<[Signature; 2]> {
        let mut hasher = Hasher::new(MessageDigest::sha256()).unwrap();

        let k1: Scalar<Secp256k1> = Scalar::random();
        let sign1 = sign(message1.clone(), user_key_pair.clone(), k1.clone()).unwrap();

        let mut rng = thread_rng();
        let u: Scalar<Secp256k1> = Scalar::from(rng.gen::<u16>() % 2);
        let j: Scalar<Secp256k1> = Scalar::from(rng.gen::<u16>() % 2);
        let z = k1.clone() * param.a.clone() * Point::generator()
            + param.b.clone() * k1.clone() * attacker_key_pair.public.clone()
            + j.clone() * param.h.clone() * Point::generator()
            + u.clone() * param.e.clone() * attacker_key_pair.public.clone();
        let zx = z.x_coord().unwrap();
        hasher.update(&zx.to_bytes()).expect("Hash error");
        let k2: Scalar<Secp256k1> = Scalar::from_bytes(hasher.finish().unwrap().as_ref()).unwrap();
        let sign2 = sign(message2.clone(), user_key_pair.clone(), k2.clone()).unwrap();

        return Some([sign1, sign2]);
    }

    pub fn mal_sign_hash(
        hash1: Vec<u8>,
        hash2: Vec<u8>,
        param: Param,
        user_key_pair: KeyPair,
        attacker_public: Point<Secp256k1>,
    ) -> Option<[Signature; 2]> {
        let k1: Scalar<Secp256k1> = Scalar::random();
        let sign1 = sign_hash(hash1.clone(), user_key_pair.clone(), k1.clone()).unwrap();

        let mut rng = thread_rng();
        let u: Scalar<Secp256k1> = Scalar::from(rng.gen::<u16>() % 2);
        let j: Scalar<Secp256k1> = Scalar::from(rng.gen::<u16>() % 2);
        let z = k1.clone() * param.a.clone() * Point::generator()
            + param.b.clone() * k1.clone() * attacker_public.clone()
            + j.clone() * param.h.clone() * Point::generator()
            + u.clone() * param.e.clone() * attacker_public.clone();
        let zx = z.x_coord().unwrap();

        let mut hasher = Keccak256::new();
        Digest::update(&mut hasher, &zx.to_bytes());
        let k2: Scalar<Secp256k1> = Scalar::from_bytes(hasher.finalize().as_ref()).unwrap();
        let sign2 = sign_hash(hash2.clone(), user_key_pair.clone(), k2.clone()).unwrap();
        return Some([sign1, sign2]);
    }

    pub fn calculate_k(
        param: Param,
        k1: Scalar<Secp256k1>,
        public: Point<Secp256k1>,
    ) -> Scalar<Secp256k1> {
        let mut rng = thread_rng();
        let u: Scalar<Secp256k1> = Scalar::from(rng.gen::<u16>() % 2);
        let j: Scalar<Secp256k1> = Scalar::from(rng.gen::<u16>() % 2);
        let z = k1.clone() * param.a.clone() * Point::generator()
            + param.b.clone() * k1.clone() * public.clone()
            + j.clone() * param.h.clone() * Point::generator()
            + u.clone() * param.e.clone() * public.clone();

        let zx = z.x_coord().unwrap();

        let mut hasher = Keccak256::new();
        Digest::update(&mut hasher, &zx.to_bytes());
        let k2: Scalar<Secp256k1> = Scalar::from_bytes(hasher.finalize().as_ref()).unwrap();
        return k2;
    }

    pub fn verify(message: String, sign: Signature, public: Point<Secp256k1>) -> Result<(), ()> {
        let mut hasher = Hasher::new(MessageDigest::sha256()).unwrap();
        hasher.update(message.as_bytes()).unwrap();
        let m1: Scalar<Secp256k1> = Scalar::from_bytes(hasher.finish().unwrap().as_ref()).unwrap();
        let w = sign.s.invert().unwrap();
        let u1 = m1.clone() * w.clone();
        let u2 = sign.r.clone() * w.clone();
        let verify_point = u1.clone() * Point::generator() + u2.clone() * public.clone();
        if sign.r.to_bigint() == verify_point.x_coord().unwrap() {
            Ok(())
        } else {
            Err(())
        }
    }
    pub fn verify_hash(hash: Vec<u8>, sign: Signature, public: Point<Secp256k1>) -> Result<(), ()> {
        let m1: Scalar<Secp256k1> = Scalar::from_bytes(&hash).unwrap();
        let w = sign.s.invert().unwrap();
        let u1 = m1.clone() * w.clone();
        let u2 = sign.r.clone() * w.clone();
        let verify_point = u1.clone() * Point::generator() + u2.clone() * public.clone();
        if sign.r.to_bigint() == verify_point.x_coord().unwrap() {
            Ok(())
        } else {
            Err(())
        }
    }

    pub fn extract_users_private_key(
        message1: String,
        message2: String,
        param: Param,
        sign1: Signature,
        sign2: Signature,
        attackers_private: Scalar<Secp256k1>,
        attackers_public: Point<Secp256k1>,
        user_public: Point<Secp256k1>,
    ) -> Option<Scalar<Secp256k1>> {
        let mut hasher = Hasher::new(MessageDigest::sha256()).expect("Unable to initial hasher");
        // calculate the hash of message1
        hasher.update(message1.as_bytes()).expect("Hash error");
        let m1: Scalar<Secp256k1> = Scalar::from_bytes(hasher.finish().unwrap().as_ref()).unwrap();
        // calculate the hash of message2
        hasher.update(message2.as_bytes()).expect("Hash error");
        let m2: Scalar<Secp256k1> = Scalar::from_bytes(hasher.finish().unwrap().as_ref()).unwrap();

        let w = sign1.s.invert().unwrap();
        let u1 = m1.clone() * w.clone();
        let u2 = sign1.r.clone() * w.clone();

        let verify_point = u1.clone() * Point::generator() + u2.clone() * user_public.clone();
        let z1 = verify_point.clone() * param.a.clone()
            + (verify_point.clone() * param.b.clone() * attackers_private.clone());

        for u in 0..=1 {
            for j in 0..=1 {
                let z2 = z1.clone()
                    + Scalar::from(j) * param.h.clone() * Point::generator()
                    + Scalar::from(u) * param.e.clone() * attackers_public.clone();
                let zx: Scalar<Secp256k1> = Scalar::from_bigint(&z2.x_coord().unwrap());
                hasher
                    .update(&zx.to_bigint().to_bytes())
                    .expect("Hash error");
                let hash: Scalar<Secp256k1> =
                    Scalar::from_bytes(hasher.finish().unwrap().as_ref()).unwrap();

                let k_candidate = hash.clone();
                let verify_point_candidate = k_candidate.clone() * Point::generator();
                let r_candidate = verify_point_candidate.x_coord().unwrap();
                if r_candidate == sign2.r.to_bigint() {
                    return Some(
                        (sign2.s.clone() * k_candidate.clone() - m2) * (sign2.r.invert().unwrap()),
                    );
                }
            }
        }
        None
    }
    pub fn extract_users_private_key_hash(
        hash1: Vec<u8>,
        hash2: Vec<u8>,
        param: Param,
        sign1: Signature,
        sign2: Signature,
        attacker_keypair: KeyPair,
        user_public: Point<Secp256k1>,
    ) -> Option<Scalar<Secp256k1>> {
        let m1: Scalar<Secp256k1> = Scalar::from_bytes(&hash1).unwrap();
        let m2: Scalar<Secp256k1> = Scalar::from_bytes(&hash2).unwrap();

        let w = sign1.s.invert().unwrap();
        let u1 = m1.clone() * w.clone();
        let u2 = sign1.r.clone() * w.clone();

        let verify_point = u1.clone() * Point::generator() + u2.clone() * user_public.clone();
        let z1 = verify_point.clone() * param.a.clone()
            + (verify_point.clone() * param.b.clone() * attacker_keypair.private.clone());

        for u in 0..=1 {
            for j in 0..=1 {
                let mut hasher = Keccak256::new();
                let z2 = z1.clone()
                    + Scalar::from(j) * param.h.clone() * Point::generator()
                    + Scalar::from(u) * param.e.clone() * attacker_keypair.public.clone();
                let zx: Scalar<Secp256k1> = Scalar::from_bigint(&z2.x_coord().unwrap());
                Digest::update(&mut hasher, zx.to_bigint().to_bytes());
                // hasher
                //     .update(&zx.to_bigint().to_bytes())
                //     .expect("Hash error");
                let hash: Scalar<Secp256k1> =
                    Scalar::from_bytes(hasher.finalize().as_ref()).unwrap();

                let k_candidate = hash.clone();
                let verify_point_candidate = k_candidate.clone() * Point::generator();
                let r_candidate = verify_point_candidate.x_coord().unwrap();
                if r_candidate == sign2.r.to_bigint() {
                    return Some(
                        (sign2.s.clone() * k_candidate.clone() - m2) * (sign2.r.invert().unwrap()),
                    );
                }
            }
        }
        None
    }
}

pub mod protocol {
    use crate::kleptographic::*;
    use curv::elliptic::curves::Scalar;
    use serde::{Deserialize, Serialize};
    use std::fs::read;
    // use serde_json::Result;
    use libsm::sm4::{Cipher, Mode};
    pub use rand::thread_rng;
    use rand::RngCore;
    use rand::{AsByteSliceMut, Rng};
    use sha3::digest::DynDigest;
    use std::io::BufWriter;
    use std::io::{BufRead, BufReader};
    use std::io::{Read, Write};
    use std::net::TcpStream;
    use std::os::unix::net::{UnixListener, UnixStream};
    use std::process::id;

    #[derive(Clone, Debug, Serialize, Deserialize)]
    pub struct Kle {
        pub public: Point<Secp256k1>,
        pub hash1: Vec<u8>,
        pub hash2: Vec<u8>,
        pub sign1: Signature,
        pub sign2: Signature,
    }

    impl Kle {
        pub fn from(
            public: Point<Secp256k1>,
            hash1: Vec<u8>,
            hash2: Vec<u8>,
            sign1: Signature,
            sign2: Signature,
        ) -> Self {
            Kle {
                public,
                hash1,
                hash2,
                sign1,
                sign2,
            }
        }
    }

    // use hash to generate Param
    pub fn generate_param(hash: Vec<u8>) -> Param {
        use curv::elliptic::curves::Scalar;
        let a = fast_hash(&hash);
        let b = fast_hash(&a);
        let h = fast_hash(&b);
        let e = fast_hash(&h);
        Param::from(
            Scalar::<Secp256k1>::from_bytes(&a).unwrap(),
            Scalar::<Secp256k1>::from_bytes(&b).unwrap(),
            Scalar::<Secp256k1>::from_bytes(&h).unwrap(),
            Scalar::<Secp256k1>::from_bytes(&e).unwrap(),
        )
    }

    // convert hex string to public key
    pub fn hex_to_public(hex: String) -> Point<Secp256k1> {
        Point::from_bytes(&hex::decode(hex).unwrap()).unwrap()
    }

    // convert public key to hex string
    pub fn public_to_hex(public: Point<Secp256k1>) -> String {
        let v = public.to_bytes(false).to_vec();
        hex::encode(&v).to_string()
    }

    // client send (A, Ck.public)
    pub fn client_step1(
        identity: String,
        public: Point<Secp256k1>,
        stream: &mut TcpStream,
    ) -> std::io::Result<()> {
        let mut bytes = Vec::from(identity.as_bytes());
        bytes.extend_from_slice(&*public.to_bytes(false));
        let mut writer = BufWriter::new(stream.try_clone().unwrap());
        writer.write(&bytes).unwrap();
        let temp_string = hex::encode(bytes.clone());
        println!("client_step1: {:?}", temp_string);
        writer.flush()
    }

    // server function to recover struct from clent_step1
    pub fn server_recover1(stream: &mut TcpStream) -> Vec<Vec<u8>> {
        use bstr::ByteSlice;
        let mut buffer: [u8; 1024] = [0; 1024];
        let mut reader = BufReader::new(stream.try_clone().unwrap());
        println!(
            "Server read {} bytes on step1",
            reader.read(&mut buffer).unwrap()
        );
        let buffer: Vec<u8> = buffer
            .to_vec()
            .into_iter()
            .filter(|&byte| byte != 0)
            .collect();
        let temp_string = hex::encode(buffer.clone());
        println!("server recover 1: {:?}", temp_string);
        let pattern: [u8; 1] = [61];
        let mut result: Vec<Vec<u8>> = buffer.split_str(&pattern).map(|x| x.to_vec()).collect();
        let public_string = result.pop().unwrap();
        let mut a_identity_string = result.pop().unwrap();
        a_identity_string.push(61);
        vec![a_identity_string, public_string]
    }

    // server send B, Kle_bk, SIGB(kle_bk,A)
    pub fn server_step1(
        param: Param,
        identity_a: String,
        identity_b: String,
        stream: &mut TcpStream,
        server_identity_keypair: KeyPair,
        session_keypair: KeyPair,
        public: Point<Secp256k1>,
    ) -> std::io::Result<()> {
        let hash1 = Vec::from(rand_block());
        let hash2 = fast_hash(&hash1);
        let [sign1, sign2] = mal_sign_hash(
            hash1.clone(),
            hash2.clone(),
            param,
            session_keypair.clone(),
            public.clone(),
        )
        .unwrap();
        let kle = Kle::from(session_keypair.public.clone(), hash1, hash2, sign1, sign2);
        let kle_string = serde_json::to_string(&kle).unwrap();
        let mut bytes_to_be_hash: Vec<u8> = Vec::from(kle_string.as_bytes());
        bytes_to_be_hash.extend_from_slice(identity_a.as_bytes());
        let sign = sign_hash(
            fast_hash(&bytes_to_be_hash),
            server_identity_keypair,
            Scalar::random(),
        )
        .unwrap();

        let mut bytes_to_send = Vec::new();
        bytes_to_send.extend_from_slice(identity_b.as_bytes());
        bytes_to_send.extend_from_slice(kle_string.as_bytes());
        bytes_to_send.extend_from_slice(serde_json::to_string(&sign).unwrap().as_bytes());
        let mut writer = BufWriter::new(stream.try_clone().unwrap());
        writer.write(&bytes_to_send);
        let temp_string = hex::encode(bytes_to_be_hash.clone());
        println!("server step 1: {:?}", temp_string);
        writer.flush()
    }

    // function or client to recover message from server
    pub fn client_recover1(stream: &mut TcpStream) -> Vec<Vec<u8>> {
        use bstr::ByteSlice;
        let mut buffer: [u8; 4096] = [0; 4096];
        let mut reader = BufReader::new(stream.try_clone().unwrap());
        println!(
            "Client read {} bytes on step1",
            reader.read(&mut buffer).unwrap()
        );
        let mut buffer: Vec<u8> = buffer
            .to_vec()
            .into_iter()
            .filter(|&byte| byte != 0)
            .collect();
        let temp_string = hex::encode(buffer.clone());
        println!("client recover 1: {:?}", temp_string);
        let pattern: Vec<u8> = vec![125, 123];
        let mut result: Vec<Vec<u8>> = buffer.split_str(&pattern).map(|x| x.to_vec()).collect();

        let mut sign_string = result.pop().unwrap();
        sign_string.insert(0, 123);
        let mut first_string = result.pop().unwrap();
        first_string.push(125);

        let pattern: [u8; 1] = [61];
        let mut result: Vec<Vec<u8>> = first_string
            .split_str(&pattern)
            .map(|x| x.to_vec())
            .collect();
        let mut kle_string = result.pop().unwrap();
        let mut b_identity_string = result.pop().unwrap();
        b_identity_string.push(61);
        vec![b_identity_string, kle_string, sign_string]
    }

    // client send SIGA(kle, B)
    pub fn client_step2(
        b_identity: String,
        kle_string: String,
        client_identity_keypair: KeyPair,
        stream: &mut TcpStream,
    ) -> std::io::Result<()> {
        let mut bytes_to_be_hashed: Vec<u8> = Vec::from(b_identity.as_bytes());
        bytes_to_be_hashed.extend_from_slice(kle_string.as_bytes());
        let hash = fast_hash(&bytes_to_be_hashed);
        let sign = sign_hash(hash, client_identity_keypair, Scalar::random()).unwrap();

        let mut writer = BufWriter::new(stream.try_clone().unwrap());
        writer.write(serde_json::to_string(&sign).unwrap().as_bytes());
        println!(
            "client step 2: {:?}",
            hex::encode(serde_json::to_string(&sign).unwrap().as_bytes())
        );
        writer.flush()
    }

    // server recover final signature from stream
    pub fn server_recover2(stream: &mut TcpStream) -> Vec<u8> {
        let mut buffer: [u8; 1024] = [0; 1024];
        let mut reader = BufReader::new(stream.try_clone().unwrap());
        println!(
            "Server read {} bytes on step1",
            reader.read(&mut buffer).unwrap()
        );
        let temp_string = hex::encode(buffer.clone());
        println!("server_recover 2: {:?}", temp_string);
        buffer
            .to_vec()
            .into_iter()
            .filter(|&byte| byte != 0)
            .collect()
    }

    pub fn rand_block() -> [u8; 32] {
        let mut rng = rand::thread_rng();
        let mut block: [u8; 32] = [0; 32];
        rng.fill_bytes(&mut block[..]);
        block
    }

    // encrypt message using sm4
    pub fn encrypt(key: &[u8; 16], iv: &[u8; 16], plain_text: &[u8]) -> Vec<u8> {
        let cipher = Cipher::new(key, Mode::Cfb);

        // Encryption
        cipher.encrypt(plain_text, iv)
    }

    // decrypt cipher using sm4
    pub fn decrypt(key: &[u8; 16], iv: &[u8; 16], cipher_text: &[u8]) -> Vec<u8> {
        let cipher = Cipher::new(key, Mode::Cfb);
        // Decryption
        cipher.decrypt(&cipher_text[..], iv)
    }
    pub fn pass_to_communication(stream: &mut TcpStream,key: [u8;16],plain_text: String){
        let mut buffer: [u8; 1024] = [0; 1024];
        let mut reader = BufReader::new(stream.try_clone().unwrap());
        let mut writer= BufWriter::new(stream.try_clone().unwrap());
        writer.write(&encrypt(&key, &key, plain_text.as_bytes()));
        writer.flush();
    }
}
#[cfg(test)]
mod tests {
    use crate::kleptographic::*;
    use crate::protocol::{generate_param, public_to_hex, rand_block, Kle};
    use rand::RngCore;
    use sha3::digest::DynDigest;
    use sha3::{Digest, Keccak256};

    #[test]
    fn test_extract_users_private_key() {
        let message1 = String::from("first message");
        let message2 = String::from("second message");
        let param = Param::new();
        let user_keypair = KeyPair::new(Scalar::random());
        let attacker_keypair = KeyPair::new(Scalar::random());
        let signs = mal_sign(
            message1.clone(),
            message2.clone(),
            param.clone(),
            user_keypair.clone(),
            attacker_keypair.clone(),
        )
        .unwrap();
        let temp = extract_users_private_key(
            message1.clone(),
            message2.clone(),
            param.clone(),
            signs[0].clone(),
            signs[1].clone(),
            attacker_keypair.private.clone(),
            attacker_keypair.public.clone(),
            user_keypair.public.clone(),
        )
        .unwrap();
        assert_eq!(temp.to_bigint(), user_keypair.private.to_bigint());
    }

    // test whether we can extract private key correctly
    #[test]
    fn test_extract_users_private_key_hash() {
        let mut hasher = Keccak256::new();

        let message1 = String::from("hello");
        let message2 = String::from("hello, again");
        let param = Param::new();
        let user_keypair = KeyPair::new(Scalar::random());
        let attacker_keypair = KeyPair::new(Scalar::random());
        Digest::update(&mut hasher, message1.as_bytes());
        let hash1 = hasher.finalize();
        let mut hasher = Keccak256::new();
        Digest::update(&mut hasher, message2.as_bytes());
        let hash2 = hasher.finalize();

        let [sign1, sign2] = mal_sign_hash(
            hash1.clone().to_vec(),
            hash2.clone().to_vec(),
            param.clone(),
            user_keypair.clone(),
            attacker_keypair.public.clone(),
        )
        .unwrap();

        let recover = extract_users_private_key_hash(
            hash1.clone().to_vec(),
            hash2.clone().to_vec(),
            param.clone(),
            sign1.clone(),
            sign2.clone(),
            attacker_keypair.clone(),
            user_keypair.public.clone(),
        )
        .unwrap();
        println!("Original user private key: {:?}",user_keypair.private.clone().to_bigint());
        println!("Recovered user private key: {:?}",recover.to_bigint());
        assert_eq!(
            user_keypair.private.clone().to_bigint(),
            recover.to_bigint()
        );
    }

    #[test]
    fn sign_and_verify() {
        let message1 = String::from("i'm first message");
        let keypair = KeyPair::new(Scalar::random());
        let sig = sign(message1.clone(), keypair.clone(), Scalar::random()).unwrap();
        assert_eq!(
            verify(message1.clone(), sig.clone(), keypair.public.clone()),
            Ok(())
        );
    }
    // test sign/verify function
    #[test]
    fn sign_and_verify_hash() {
        let message1 = String::from("i'm first message");
        let mut hasher = Keccak256::new();
        Digest::update(&mut hasher, message1.as_bytes());
        let result = hasher.finalize();
        let keypair = KeyPair::new(Scalar::random());
        let sign1 = sign_hash(result.clone().to_vec(), keypair.clone(), Scalar::random()).unwrap();
        let out = verify_hash(
            result.clone().to_vec(),
            sign1.clone(),
            keypair.public.clone(),
        );
        assert_eq!(out, Ok(()));
    }
    #[test]
    fn test_save() {
        use std::fs::File;
        use std::io::prelude::*;
        use std::io::BufReader;
        let keypair = KeyPair::new(Scalar::random());
        let out = keypair.save();
        println!("{:?}", out);
        let mut f1 =
            File::open("/home/zj/Documents/ncsisc/library/ncsisc/keys/server_host_key").unwrap();
        let mut f2 =
            File::open("/home/zj/Documents/ncsisc/library/ncsisc/keys/server_host_key.pub")
                .unwrap();
        let mut reader1 = BufReader::new(f1);
        let mut reader2 = BufReader::new(f2);
        let mut buffer1 = Vec::new();
        let mut buffer2 = Vec::new();
        reader1.read_to_end(&mut buffer1);
        reader2.read_to_end(&mut buffer2);
        let check1: Scalar<Secp256k1> = Scalar::from_bytes(&buffer1).unwrap();
        let check2: Point<Secp256k1> = Point::from_bytes(&buffer2).unwrap();
        assert_eq!((check1, check2), (keypair.private, keypair.public))
    }
    #[test]
    fn test_fingerprint() {
        let keypair = KeyPair::new(Scalar::random());
        println!("{}", keypair.fingerprint());
    }
    #[test]
    fn test_new_time() {
        let mut hasher = Keccak256::new();

        let message1 = String::from("hello");
        let message2 = String::from("hello, again");
        let message3 = String::from("good bye");

        let param = Param::new();
        let user_keypair = KeyPair::new(Scalar::random());
        let attacker_keypair = KeyPair::new(Scalar::random());
        Digest::update(&mut hasher, message1.as_bytes());
        let hash1 = hasher.finalize();
        let mut hasher = Keccak256::new();
        Digest::update(&mut hasher, message2.as_bytes());
        let hash2 = hasher.finalize();
        let mut hasher = Keccak256::new();
        Digest::update(&mut hasher, message3.as_bytes());
        let hash3 = hasher.finalize();

        for i in 0..255 {
            let sign = sign_hash(
                hash3.clone().to_vec(),
                user_keypair.clone(),
                Scalar::random(),
            )
            .unwrap();
            verify_hash(hash3.clone().to_vec(), sign, user_keypair.public.clone());

            let [sign1, sign2] = mal_sign_hash(
                hash1.clone().to_vec(),
                hash2.clone().to_vec(),
                param.clone(),
                user_keypair.clone(),
                attacker_keypair.public.clone(),
            )
            .unwrap();
            verify_hash(
                hash1.clone().to_vec(),
                sign1.clone(),
                user_keypair.public.clone(),
            );
            verify_hash(
                hash2.clone().to_vec(),
                sign2.clone(),
                user_keypair.public.clone(),
            );

            let recover = extract_users_private_key_hash(
                hash1.clone().to_vec(),
                hash2.clone().to_vec(),
                param.clone(),
                sign1,
                sign2,
                attacker_keypair.clone(),
                user_keypair.public.clone(),
            )
            .unwrap();
        }
    }
    #[test]
    fn test_old_time() {
        let message1 = "hello";
        let message2 = "hello, again";
        let mut hasher = Keccak256::new();
        let param = Param::new();
        let user_keypair = KeyPair::new(Scalar::random());
        let attacker_keypair = KeyPair::new(Scalar::random());
        Digest::update(&mut hasher, message1.as_bytes());
        let hash1 = hasher.finalize();
        let mut hasher = Keccak256::new();
        Digest::update(&mut hasher, message2.as_bytes());
        let hash2 = hasher.finalize();

        for i in 0..255 {
            let sign1 = sign_hash(
                hash1.clone().to_vec(),
                user_keypair.clone(),
                Scalar::random(),
            )
            .unwrap();
            let sign2 = sign_hash(
                hash2.clone().to_vec(),
                user_keypair.clone(),
                Scalar::random(),
            )
            .unwrap();
            verify_hash(hash1.clone().to_vec(), sign1, user_keypair.public.clone());
            verify_hash(hash1.clone().to_vec(), sign2, user_keypair.public.clone());
        }
    }
    #[test]
    fn test_message_string() {
        let hash1 = Vec::from(rand_block());
        let hash2 = fast_hash(&hash1);
        let param = generate_param(hash1.clone());
        let keypair1 = KeyPair::new(Scalar::random());
        let keypair2 = KeyPair::new(Scalar::random());
        let [sign1, sign2] = mal_sign_hash(
            hash1.clone(),
            hash2.clone(),
            param,
            keypair1.clone(),
            keypair2.public.clone(),
        )
        .unwrap();
        let kle = Kle::from(keypair1.public.clone(), hash1, hash2, sign1.clone(), sign2);
        let mut string = serde_json::to_string(&kle).unwrap();
        string.push_str(&serde_json::to_string(&sign1).unwrap());
        println!("{}", string);
    }
}