aboutsummaryrefslogtreecommitdiff
path: root/CpriSym.c
blob: e05a4d13ddc23e3a22df92113a9b226b339d0050 (plain)
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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
// This file was extracted from the TCG Published
// Trusted Platform Module Library
// Part 4: Supporting Routines
// Family "2.0"
// Level 00 Revision 01.16
// October 30, 2014

#include       "OsslCryptoEngine.h"
//
//     The following sets of defines are used to allow use of the SM4 algorithm identifier while waiting for the
//     SM4 implementation code to appear.
//
typedef   AES_KEY SM4_KEY;
#define   SM4_set_encrypt_key            AES_set_encrypt_key
#define   SM4_set_decrypt_key            AES_set_decrypt_key
#define   SM4_decrypt                    AES_decrypt
#define   SM4_encrypt                    AES_encrypt
//
//
//      Utility Functions
//
//      _cpri_SymStartup()
//
LIB_EXPORT BOOL
_cpri__SymStartup(
      void
)
{
      return TRUE;
}
//
//
//      _cpri__GetSymmetricBlockSize()
//
//     This function returns the block size of the algorithm.
//
//     Return Value                      Meaning
//
//     <= 0                              cipher not supported
//     >0                                the cipher block size in bytes
//
LIB_EXPORT INT16
_cpri__GetSymmetricBlockSize(
      TPM_ALG_ID         symmetricAlg,        // IN: the symmetric algorithm
      UINT16             keySizeInBits        // IN: the key size
      )
{
   switch (symmetricAlg)
   {
#ifdef TPM_ALG_AES
   case TPM_ALG_AES:
#endif
#ifdef TPM_ALG_SM4 // Both AES and SM4 use the same block size
   case TPM_ALG_SM4:
#endif
       if(keySizeInBits != 0) // This is mostly to have a reference to
              // keySizeInBits for the compiler
              return 16;
         else
             return 0;
         break;
    default:
        return 0;
    }
}
//
//
//      AES Encryption
//
//      _cpri__AESEncryptCBC()
//
//     This function performs AES encryption in CBC chain mode. The input dIn buffer is encrypted into dOut.
//     The input iv buffer is required to have a size equal to the block size (16 bytes). The dInSize is required to
//     be a multiple of the block size.
//
//     Return Value                      Meaning
//
//     CRYPT_SUCCESS                     if success
//     CRYPT_PARAMETER                   dInSize is not a multiple of the block size
//
LIB_EXPORT CRYPT_RESULT
_cpri__AESEncryptCBC(
    BYTE                *dOut,          // OUT:
    UINT32               keySizeInBits, // IN: key size in bit
    BYTE                *key,           // IN: key buffer. The size of this buffer in
                                        //      bytes is (keySizeInBits + 7) / 8
    BYTE                *iv,            // IN/OUT: IV for decryption.
    UINT32               dInSize,       // IN: data size (is required to be a multiple
                                        //      of 16 bytes)
    BYTE                *dIn            // IN: data buffer
    )
{
    AES_KEY         AesKey;
    BYTE           *pIv;
    INT32           dSize;              // Need a signed version
    int             i;
    pAssert(dOut != NULL && key != NULL && iv != NULL && dIn != NULL);
    if(dInSize == 0)
        return CRYPT_SUCCESS;
    pAssert(dInSize <= INT32_MAX);
    dSize = (INT32)dInSize;
    // For CBC, the data size must be an even multiple of the
    // cipher block size
    if((dSize % 16) != 0)
        return CRYPT_PARAMETER;
    // Create AES encrypt key schedule
    if (AES_set_encrypt_key(key, keySizeInBits, &AesKey) != 0)
        FAIL(FATAL_ERROR_INTERNAL);
    // XOR the data block into the IV, encrypt the IV into the IV
    // and then copy the IV to the output
    for(; dSize > 0; dSize -= 16)
    {
         pIv = iv;
         for(i = 16; i > 0; i--)
             *pIv++ ^= *dIn++;
         AES_encrypt(iv, iv, &AesKey);
         pIv = iv;
         for(i = 16; i > 0; i--)
             *dOut++ = *pIv++;
    }
    return CRYPT_SUCCESS;
}
//
//
//       _cpri__AESDecryptCBC()
//
//      This function performs AES decryption in CBC chain mode. The input dIn buffer is decrypted into dOut.
//      The input iv buffer is required to have a size equal to the block size (16 bytes). The dInSize is required to
//      be a multiple of the block size.
//
//      Return Value                     Meaning
//
//      CRYPT_SUCCESS                    if success
//      CRYPT_PARAMETER                  dInSize is not a multiple of the block size
//
LIB_EXPORT CRYPT_RESULT
_cpri__AESDecryptCBC(
    BYTE                *dOut,          // OUT: the decrypted data
    UINT32               keySizeInBits, // IN: key size in bit
    BYTE                *key,           // IN: key buffer. The size of this buffer in
                                        //     bytes is (keySizeInBits + 7) / 8
    BYTE                *iv,            // IN/OUT: IV for decryption. The size of this
                                        //     buffer is 16 byte
    UINT32               dInSize,       // IN: data size
    BYTE                *dIn            // IN: data buffer
    )
{
    AES_KEY         AesKey;
    BYTE           *pIv;
    int             i;
    BYTE            tmp[16];
    BYTE           *pT = NULL;
    INT32           dSize;
    pAssert(dOut != NULL && key != NULL && iv != NULL && dIn != NULL);
    if(dInSize == 0)
        return CRYPT_SUCCESS;
    pAssert(dInSize <= INT32_MAX);
    dSize = (INT32)dInSize;
    // For CBC, the data size must be an even multiple of the
    // cipher block size
    if((dSize % 16) != 0)
        return CRYPT_PARAMETER;
    // Create AES key schedule
    if (AES_set_decrypt_key(key, keySizeInBits, &AesKey) != 0)
        FAIL(FATAL_ERROR_INTERNAL);
    // Copy the input data to a temp buffer, decrypt the buffer into the output;
    // XOR in the IV, and copy the temp buffer to the IV and repeat.
    for(; dSize > 0; dSize -= 16)
    {
//
        pT = tmp;
        for(i = 16; i> 0; i--)
            *pT++ = *dIn++;
        AES_decrypt(tmp, dOut, &AesKey);
        pIv = iv;
        pT = tmp;
        for(i = 16; i> 0; i--)
        {
            *dOut++ ^= *pIv;
            *pIv++ = *pT++;
        }
   }
   return CRYPT_SUCCESS;
}
//
//
//       _cpri__AESEncryptCFB()
//
//      This function performs AES encryption in CFB chain mode. The dOut buffer receives the values
//      encrypted dIn. The input iv is assumed to be the size of an encryption block (16 bytes). The iv buffer will
//      be modified to contain the last encrypted block.
//
//      Return Value                      Meaning
//
//      CRYPT_SUCCESS                     no non-fatal errors
//
LIB_EXPORT CRYPT_RESULT
_cpri__AESEncryptCFB(
   BYTE                *dOut,          // OUT: the encrypted
   UINT32               keySizeInBits, // IN: key size in bit
   BYTE                *key,           // IN: key buffer. The size of this buffer in
                                       //     bytes is (keySizeInBits + 7) / 8
   BYTE                *iv,            // IN/OUT: IV for decryption.
   UINT32               dInSize,       // IN: data size
   BYTE                *dIn            // IN: data buffer
   )
{
   BYTE           *pIv = NULL;
   AES_KEY         AesKey;
   INT32           dSize;               // Need a signed version of dInSize
   int             i;
   pAssert(dOut != NULL && key != NULL && iv != NULL && dIn != NULL);
   if(dInSize == 0)
       return CRYPT_SUCCESS;
   pAssert(dInSize <= INT32_MAX);
   dSize = (INT32)dInSize;
   // Create AES encryption key schedule
   if (AES_set_encrypt_key(key, keySizeInBits, &AesKey) != 0)
       FAIL(FATAL_ERROR_INTERNAL);
   // Encrypt the IV into the IV, XOR in the data, and copy to output
   for(; dSize > 0; dSize -= 16)
   {
       // Encrypt the current value of the IV
       AES_encrypt(iv, iv, &AesKey);
       pIv = iv;
       for(i = (int)(dSize < 16) ? dSize : 16; i > 0; i--)
           // XOR the data into the IV to create the cipher text
           // and put into the output
           *dOut++ = *pIv++ ^= *dIn++;
   }
   // If the inner loop (i loop) was smaller than 16, then dSize would have been
   // smaller than 16 and it is now negative. If it is negative, then it indicates
   // how many bytes are needed to pad out the IV for the next round.
   for(; dSize < 0; dSize++)
       *pIv++ = 0;
   return CRYPT_SUCCESS;
}
//
//
//       _cpri__AESDecryptCFB()
//
//      This function performs AES decrypt in CFB chain mode. The dOut buffer receives the values decrypted
//      from dIn.
//      The input iv is assumed to be the size of an encryption block (16 bytes). The iv buffer will be modified to
//      contain the last decoded block, padded with zeros
//
//      Return Value                    Meaning
//
//      CRYPT_SUCCESS                   no non-fatal errors
//
LIB_EXPORT CRYPT_RESULT
_cpri__AESDecryptCFB(
   BYTE                *dOut,          // OUT: the decrypted data
   UINT32               keySizeInBits, // IN: key size in bit
   BYTE                *key,           // IN: key buffer. The size of this buffer in
                                       //     bytes is (keySizeInBits + 7) / 8
   BYTE                *iv,            // IN/OUT: IV for decryption.
   UINT32               dInSize,       // IN: data size
   BYTE                *dIn            // IN: data buffer
   )
{
   BYTE           *pIv = NULL;
   BYTE            tmp[16];
   int             i;
   BYTE           *pT;
   AES_KEY         AesKey;
   INT32           dSize;
   pAssert(dOut != NULL && key != NULL && iv != NULL && dIn != NULL);
   if(dInSize == 0)
       return CRYPT_SUCCESS;
   pAssert(dInSize <= INT32_MAX);
   dSize = (INT32)dInSize;
   // Create AES encryption key schedule
   if (AES_set_encrypt_key(key, keySizeInBits, &AesKey) != 0)
       FAIL(FATAL_ERROR_INTERNAL);
   for(; dSize > 0; dSize -= 16)
   {
       // Encrypt the IV into the temp buffer
       AES_encrypt(iv, tmp, &AesKey);
       pT = tmp;
       pIv = iv;
       for(i = (dSize < 16) ? dSize : 16; i > 0; i--)
           // Copy the current cipher text to IV, XOR
           // with the temp buffer and put into the output
           *dOut++ = *pT++ ^ (*pIv++ = *dIn++);
   }
   // If the inner loop (i loop) was smaller than 16, then dSize
   // would have been smaller than 16 and it is now negative
   // If it is negative, then it indicates how may fill bytes
   // are needed to pad out the IV for the next round.
   for(; dSize < 0; dSize++)
       *pIv++ = 0;
   return CRYPT_SUCCESS;
}
//
//
//       _cpri__AESEncryptCTR()
//
//      This function performs AES encryption/decryption in CTR chain mode. The dIn buffer is encrypted into
//      dOut. The input iv buffer is assumed to have a size equal to the AES block size (16 bytes). The iv will be
//      incremented by the number of blocks (full and partial) that were encrypted.
//
//      Return Value                      Meaning
//
//      CRYPT_SUCCESS                     no non-fatal errors
//
LIB_EXPORT CRYPT_RESULT
_cpri__AESEncryptCTR(
   BYTE                *dOut,          // OUT: the encrypted data
   UINT32               keySizeInBits, // IN: key size in bit
   BYTE                *key,           // IN: key buffer. The size of this buffer in
                                       //     bytes is (keySizeInBits + 7) / 8
   BYTE                *iv,            // IN/OUT: IV for decryption.
   UINT32               dInSize,       // IN: data size
   BYTE                *dIn            // IN: data buffer
   )
{
   BYTE            tmp[16];
   BYTE           *pT;
   AES_KEY         AesKey;
   int             i;
   INT32           dSize;
   pAssert(dOut != NULL && key != NULL && iv != NULL && dIn != NULL);
   if(dInSize == 0)
       return CRYPT_SUCCESS;
   pAssert(dInSize <= INT32_MAX);
   dSize = (INT32)dInSize;
   // Create AES encryption schedule
   if (AES_set_encrypt_key(key, keySizeInBits, &AesKey) != 0)
       FAIL(FATAL_ERROR_INTERNAL);
   for(; dSize > 0; dSize -= 16)
   {
       // Encrypt the current value of the IV(counter)
       AES_encrypt(iv, (BYTE *)tmp, &AesKey);
        //increment the counter (counter is big-endian so start at end)
        for(i = 15; i >= 0; i--)
            if((iv[i] += 1) != 0)
                break;
        // XOR the encrypted counter value with input and put into output
        pT = tmp;
        for(i = (dSize < 16) ? dSize : 16; i > 0; i--)
            *dOut++ = *dIn++ ^ *pT++;
   }
   return CRYPT_SUCCESS;
}
//
//       _cpri__AESEncryptECB()
//
//      AES encryption in ECB mode. The data buffer is modified to contain the cipher text.
//
//      Return Value                      Meaning
//
//      CRYPT_SUCCESS                     no non-fatal errors
//
LIB_EXPORT CRYPT_RESULT
_cpri__AESEncryptECB(
    BYTE                *dOut,          // OUT: encrypted data
    UINT32               keySizeInBits, // IN: key size in bit
    BYTE                *key,           // IN: key buffer. The size of this buffer in
                                        //     bytes is (keySizeInBits + 7) / 8
    UINT32               dInSize,       // IN: data size
    BYTE                *dIn            // IN: clear text buffer
    )
{
    AES_KEY          AesKey;
    INT32            dSize;
    pAssert(dOut != NULL && key != NULL && dIn != NULL);
    if(dInSize == 0)
        return CRYPT_SUCCESS;
    pAssert(dInSize <= INT32_MAX);
    dSize = (INT32)dInSize;
    // For ECB, the data size must be an even multiple of the
    // cipher block size
    if((dSize % 16) != 0)
        return CRYPT_PARAMETER;
    // Create AES encrypting key schedule
    if (AES_set_encrypt_key(key, keySizeInBits, &AesKey) != 0)
        FAIL(FATAL_ERROR_INTERNAL);
    for(; dSize > 0; dSize -= 16)
    {
        AES_encrypt(dIn, dOut, &AesKey);
        dIn = &dIn[16];
        dOut = &dOut[16];
    }
   return CRYPT_SUCCESS;
}
//
//
//       _cpri__AESDecryptECB()
//
//      This function performs AES decryption using ECB (not recommended). The cipher text dIn is decrypted
//      into dOut.
//
//      Return Value                      Meaning
//
//      CRYPT_SUCCESS                     no non-fatal errors
//
LIB_EXPORT CRYPT_RESULT
_cpri__AESDecryptECB(
   BYTE                *dOut,          // OUT: the clear text data
   UINT32               keySizeInBits, // IN: key size in bit
   BYTE                *key,           // IN: key buffer. The size of this buffer in
                                       //     bytes is (keySizeInBits + 7) / 8
   UINT32               dInSize,       // IN: data size
   BYTE                *dIn            // IN: cipher text buffer
   )
{
   AES_KEY         AesKey;
   INT32           dSize;
   pAssert(dOut != NULL && key != NULL && dIn != NULL);
   if(dInSize == 0)
       return CRYPT_SUCCESS;
   pAssert(dInSize <= INT32_MAX);
   dSize = (INT32)dInSize;
   // For ECB, the data size must be an even multiple of the
   // cipher block size
   if((dSize % 16) != 0)
       return CRYPT_PARAMETER;
   // Create AES decryption key schedule
   if (AES_set_decrypt_key(key, keySizeInBits, &AesKey) != 0)
       FAIL(FATAL_ERROR_INTERNAL);
   for(; dSize > 0; dSize -= 16)
   {
       AES_decrypt(dIn, dOut, &AesKey);
       dIn = &dIn[16];
       dOut = &dOut[16];
   }
   return CRYPT_SUCCESS;
}
//
//
//       _cpri__AESEncryptOFB()
//
//      This function performs AES encryption/decryption in OFB chain mode. The dIn buffer is modified to
//      contain the encrypted/decrypted text.
//      The input iv buffer is assumed to have a size equal to the block size (16 bytes). The returned value of iv
//      will be the nth encryption of the IV, where n is the number of blocks (full or partial) in the data stream.
//
//
//
//
//      Return Value                  Meaning
//
//      CRYPT_SUCCESS                 no non-fatal errors
//
LIB_EXPORT CRYPT_RESULT
_cpri__AESEncryptOFB(
   BYTE               *dOut,          // OUT: the encrypted/decrypted data
   UINT32              keySizeInBits, // IN: key size in bit
   BYTE               *key,           // IN: key buffer. The size of this buffer in
                                      //     bytes is (keySizeInBits + 7) / 8
   BYTE               *iv,            // IN/OUT: IV for decryption. The size of this
                                      //     buffer is 16 byte
   UINT32              dInSize,       // IN: data size
   BYTE               *dIn            // IN: data buffer
   )
{
   BYTE           *pIv;
   AES_KEY         AesKey;
   INT32           dSize;
   int             i;
   pAssert(dOut != NULL && key != NULL && iv != NULL && dIn != NULL);
   if(dInSize == 0)
       return CRYPT_SUCCESS;
   pAssert(dInSize <= INT32_MAX);
   dSize = (INT32)dInSize;
   // Create AES key schedule
   if (AES_set_encrypt_key(key, keySizeInBits, &AesKey) != 0)
       FAIL(FATAL_ERROR_INTERNAL);
   // This is written so that dIn and dOut may be the same
   for(; dSize > 0; dSize -= 16)
   {
       // Encrypt the current value of the "IV"
       AES_encrypt(iv, iv, &AesKey);
        // XOR the encrypted IV into dIn to create the cipher text (dOut)
        pIv = iv;
        for(i = (dSize < 16) ? dSize : 16; i > 0; i--)
            *dOut++ = (*pIv++ ^ *dIn++);
   }
   return CRYPT_SUCCESS;
}
#ifdef    TPM_ALG_SM4
//
//
//       SM4 Encryption
//
//       _cpri__SM4EncryptCBC()
//
//      This function performs SM4 encryption in CBC chain mode. The input dIn buffer is encrypted into dOut.
//      The input iv buffer is required to have a size equal to the block size (16 bytes). The dInSize is required to
//      be a multiple of the block size.
//
//      Return Value                      Meaning
//
//      CRYPT_SUCCESS                     if success
//      CRYPT_PARAMETER                   dInSize is not a multiple of the block size
//
LIB_EXPORT CRYPT_RESULT
_cpri__SM4EncryptCBC(
    BYTE                *dOut,          // OUT:
    UINT32               keySizeInBits, // IN: key size in bit
    BYTE                *key,           // IN: key buffer. The size of this buffer in
                                        //      bytes is (keySizeInBits + 7) / 8
    BYTE                *iv,            // IN/OUT: IV for decryption.
    UINT32               dInSize,       // IN: data size (is required to be a multiple
                                        //      of 16 bytes)
    BYTE                *dIn            // IN: data buffer
    )
{
    SM4_KEY         Sm4Key;
    BYTE           *pIv;
    INT32           dSize;              // Need a signed version
    int             i;
    pAssert(dOut != NULL && key != NULL && iv != NULL && dIn != NULL);
    if(dInSize == 0)
        return CRYPT_SUCCESS;
    pAssert(dInSize <= INT32_MAX);
    dSize = (INT32)dInSize;
    // For CBC, the data size must be an even multiple of the
    // cipher block size
    if((dSize % 16) != 0)
        return CRYPT_PARAMETER;
    // Create SM4 encrypt key schedule
    if (SM4_set_encrypt_key(key, keySizeInBits, &Sm4Key) != 0)
        FAIL(FATAL_ERROR_INTERNAL);
    // XOR the data block into the IV, encrypt the IV into the IV
    // and then copy the IV to the output
    for(; dSize > 0; dSize -= 16)
    {
        pIv = iv;
        for(i = 16; i > 0; i--)
            *pIv++ ^= *dIn++;
        SM4_encrypt(iv, iv, &Sm4Key);
        pIv = iv;
        for(i = 16; i > 0; i--)
            *dOut++ = *pIv++;
    }
    return CRYPT_SUCCESS;
}
//
//
//       _cpri__SM4DecryptCBC()
//
//      This function performs SM4 decryption in CBC chain mode. The input dIn buffer is decrypted into dOut.
//      The input iv buffer is required to have a size equal to the block size (16 bytes). The dInSize is required to
//      be a multiple of the block size.
//
//      Return Value                     Meaning
//
//      CRYPT_SUCCESS                    if success
//      CRYPT_PARAMETER                  dInSize is not a multiple of the block size
//
LIB_EXPORT CRYPT_RESULT
_cpri__SM4DecryptCBC(
    BYTE                *dOut,          // OUT: the decrypted data
    UINT32               keySizeInBits, // IN: key size in bit
    BYTE                *key,           // IN: key buffer. The size of this buffer in
                                        //     bytes is (keySizeInBits + 7) / 8
    BYTE                *iv,            // IN/OUT: IV for decryption. The size of this
                                        //     buffer is 16 byte
    UINT32               dInSize,       // IN: data size
    BYTE                *dIn            // IN: data buffer
    )
{
    SM4_KEY         Sm4Key;
    BYTE           *pIv;
    int             i;
    BYTE            tmp[16];
    BYTE           *pT = NULL;
    INT32           dSize;
    pAssert(dOut != NULL && key != NULL && iv != NULL && dIn != NULL);
    if(dInSize == 0)
        return CRYPT_SUCCESS;
    pAssert(dInSize <= INT32_MAX);
    dSize = (INT32)dInSize;
    // For CBC, the data size must be an even multiple of the
    // cipher block size
    if((dSize % 16) != 0)
        return CRYPT_PARAMETER;
    // Create SM4 key schedule
    if (SM4_set_decrypt_key(key, keySizeInBits, &Sm4Key) != 0)
        FAIL(FATAL_ERROR_INTERNAL);
    // Copy the input data to a temp buffer, decrypt the buffer into the output;
    // XOR in the IV, and copy the temp buffer to the IV and repeat.
    for(; dSize > 0; dSize -= 16)
    {
        pT = tmp;
        for(i = 16; i> 0; i--)
            *pT++ = *dIn++;
        SM4_decrypt(tmp, dOut, &Sm4Key);
        pIv = iv;
        pT = tmp;
        for(i = 16; i> 0; i--)
        {
            *dOut++ ^= *pIv;
//
              *pIv++ = *pT++;
        }
   }
   return CRYPT_SUCCESS;
}
//
//
//       _cpri__SM4EncryptCFB()
//
//      This function performs SM4 encryption in CFB chain mode. The dOut buffer receives the values
//      encrypted dIn. The input iv is assumed to be the size of an encryption block (16 bytes). The iv buffer will
//      be modified to contain the last encrypted block.
//
//      Return Value                      Meaning
//
//      CRYPT_SUCCESS                     no non-fatal errors
//
LIB_EXPORT CRYPT_RESULT
_cpri__SM4EncryptCFB(
   BYTE                *dOut,          // OUT: the encrypted
   UINT32               keySizeInBits, // IN: key size in bit
   BYTE                *key,           // IN: key buffer. The size of this buffer in
                                       //     bytes is (keySizeInBits + 7) / 8
   BYTE                *iv,            // IN/OUT: IV for decryption.
   UINT32               dInSize,       // IN: data size
   BYTE                *dIn            // IN: data buffer
   )
{
   BYTE           *pIv;
   SM4_KEY         Sm4Key;
   INT32           dSize;               // Need a signed version of dInSize
   int             i;
   pAssert(dOut != NULL && key != NULL && iv != NULL && dIn != NULL);
   if(dInSize == 0)
       return CRYPT_SUCCESS;
   pAssert(dInSize <= INT32_MAX);
   dSize = (INT32)dInSize;
   // Create SM4 encryption key schedule
   if (SM4_set_encrypt_key(key, keySizeInBits, &Sm4Key) != 0)
       FAIL(FATAL_ERROR_INTERNAL);
   // Encrypt the IV into the IV, XOR in the data, and copy to output
   for(; dSize > 0; dSize -= 16)
   {
       // Encrypt the current value of the IV
       SM4_encrypt(iv, iv, &Sm4Key);
       pIv = iv;
       for(i = (int)(dSize < 16) ? dSize : 16; i > 0; i--)
           // XOR the data into the IV to create the cipher text
           // and put into the output
           *dOut++ = *pIv++ ^= *dIn++;
   }
   return CRYPT_SUCCESS;
}
//
//
//       _cpri__SM4DecryptCFB()
//
//      This function performs SM4 decrypt in CFB chain mode. The dOut buffer receives the values decrypted
//      from dIn.
//
//      The input iv is assumed to be the size of an encryption block (16 bytes). The iv buffer will be modified to
//      contain the last decoded block, padded with zeros
//
//      Return Value                    Meaning
//
//      CRYPT_SUCCESS                   no non-fatal errors
//
LIB_EXPORT CRYPT_RESULT
_cpri__SM4DecryptCFB(
   BYTE                *dOut,          // OUT: the decrypted data
   UINT32               keySizeInBits, // IN: key size in bit
   BYTE                *key,           // IN: key buffer. The size of this buffer in
                                       //     bytes is (keySizeInBits + 7) / 8
   BYTE                *iv,            // IN/OUT: IV for decryption.
   UINT32               dInSize,       // IN: data size
   BYTE                *dIn            // IN: data buffer
   )
{
   BYTE           *pIv;
   BYTE            tmp[16];
   int             i;
   BYTE           *pT;
   SM4_KEY         Sm4Key;
   INT32           dSize;
   pAssert(dOut != NULL && key != NULL && iv != NULL && dIn != NULL);
   if(dInSize == 0)
       return CRYPT_SUCCESS;
   pAssert(dInSize <= INT32_MAX);
   dSize = (INT32)dInSize;
   // Create SM4 encryption key schedule
   if (SM4_set_encrypt_key(key, keySizeInBits, &Sm4Key) != 0)
       FAIL(FATAL_ERROR_INTERNAL);
   for(; dSize > 0; dSize -= 16)
   {
       // Encrypt the IV into the temp buffer
       SM4_encrypt(iv, tmp, &Sm4Key);
       pT = tmp;
       pIv = iv;
       for(i = (dSize < 16) ? dSize : 16; i > 0; i--)
           // Copy the current cipher text to IV, XOR
           // with the temp buffer and put into the output
           *dOut++ = *pT++ ^ (*pIv++ = *dIn++);
   }
   // If the inner loop (i loop) was smaller than 16, then dSize
   // would have been smaller than 16 and it is now negative
   // If it is negative, then it indicates how may fill bytes
   // are needed to pad out the IV for the next round.
   for(; dSize < 0; dSize++)
       *iv++ = 0;
   return CRYPT_SUCCESS;
}
//
//
//       _cpri__SM4EncryptCTR()
//
//      This function performs SM4 encryption/decryption in CTR chain mode. The dIn buffer is encrypted into
//      dOut. The input iv buffer is assumed to have a size equal to the SM4 block size (16 bytes). The iv will be
//      incremented by the number of blocks (full and partial) that were encrypted.
//
//      Return Value                      Meaning
//
//      CRYPT_SUCCESS                     no non-fatal errors
//
LIB_EXPORT CRYPT_RESULT
_cpri__SM4EncryptCTR(
   BYTE               *dOut,          // OUT: the encrypted data
   UINT32              keySizeInBits, // IN: key size in bit
   BYTE               *key,           // IN: key buffer. The size of this buffer in
                                      //     bytes is (keySizeInBits + 7) / 8
   BYTE               *iv,            // IN/OUT: IV for decryption.
   UINT32              dInSize,       // IN: data size
   BYTE               *dIn            // IN: data buffer
   )
{
   BYTE            tmp[16];
   BYTE           *pT;
   SM4_KEY         Sm4Key;
   int             i;
   INT32           dSize;
   pAssert(dOut != NULL && key != NULL && iv != NULL && dIn != NULL);
   if(dInSize == 0)
       return CRYPT_SUCCESS;
   pAssert(dInSize <= INT32_MAX);
   dSize = (INT32)dInSize;
   // Create SM4 encryption schedule
   if (SM4_set_encrypt_key(key, keySizeInBits, &Sm4Key) != 0)
       FAIL(FATAL_ERROR_INTERNAL);
   for(; dSize > 0; dSize--)
   {
       // Encrypt the current value of the IV(counter)
       SM4_encrypt(iv, (BYTE *)tmp, &Sm4Key);
        //increment the counter
        for(i = 0; i < 16; i++)
            if((iv[i] += 1) != 0)
                break;
        // XOR the encrypted counter value with input and put into output
        pT = tmp;
        for(i = (dSize < 16) ? dSize : 16; i > 0; i--)
            *dOut++ = *dIn++ ^ *pT++;
   }
   return CRYPT_SUCCESS;
}
//
//       _cpri__SM4EncryptECB()
//
//      SM4 encryption in ECB mode. The data buffer is modified to contain the cipher text.
//
//      Return Value                      Meaning
//
//      CRYPT_SUCCESS                     no non-fatal errors
//
LIB_EXPORT CRYPT_RESULT
_cpri__SM4EncryptECB(
    BYTE                *dOut,          // OUT: encrypted data
    UINT32               keySizeInBits, // IN: key size in bit
    BYTE                *key,           // IN: key buffer. The size of this buffer in
                                        //     bytes is (keySizeInBits + 7) / 8
    UINT32               dInSize,       // IN: data size
    BYTE                *dIn            // IN: clear text buffer
    )
{
    SM4_KEY          Sm4Key;
    INT32            dSize;
    pAssert(dOut != NULL && key != NULL && dIn != NULL);
    if(dInSize == 0)
        return CRYPT_SUCCESS;
    pAssert(dInSize <= INT32_MAX);
    dSize = (INT32)dInSize;
    // For ECB, the data size must be an even multiple of the
    // cipher block size
    if((dSize % 16) != 0)
        return CRYPT_PARAMETER;
    // Create SM4 encrypting key schedule
    if (SM4_set_encrypt_key(key, keySizeInBits, &Sm4Key) != 0)
        FAIL(FATAL_ERROR_INTERNAL);
    for(; dSize > 0; dSize -= 16)
    {
        SM4_encrypt(dIn, dOut, &Sm4Key);
        dIn = &dIn[16];
        dOut = &dOut[16];
    }
    return CRYPT_SUCCESS;
}
//
//
//       _cpri__SM4DecryptECB()
//
//      This function performs SM4 decryption using ECB (not recommended). The cipher text dIn is decrypted
//      into dOut.
//
//
//
//
//      Return Value                      Meaning
//
//      CRYPT_SUCCESS                     no non-fatal errors
//
LIB_EXPORT CRYPT_RESULT
_cpri__SM4DecryptECB(
   BYTE                *dOut,          // OUT: the clear text data
   UINT32               keySizeInBits, // IN: key size in bit
   BYTE                *key,           // IN: key buffer. The size of this buffer in
                                       //     bytes is (keySizeInBits + 7) / 8
   UINT32               dInSize,       // IN: data size
   BYTE                *dIn            // IN: cipher text buffer
   )
{
   SM4_KEY         Sm4Key;
   INT32           dSize;
   pAssert(dOut != NULL && key != NULL && dIn != NULL);
   if(dInSize == 0)
       return CRYPT_SUCCESS;
   pAssert(dInSize <= INT32_MAX);
   dSize = (INT32)dInSize;
   // For ECB, the data size must be an even multiple of the
   // cipher block size
   if((dSize % 16) != 0)
       return CRYPT_PARAMETER;
   // Create SM4 decryption key schedule
   if (SM4_set_decrypt_key(key, keySizeInBits, &Sm4Key) != 0)
       FAIL(FATAL_ERROR_INTERNAL);
   for(; dSize > 0; dSize -= 16)
   {
       SM4_decrypt(dIn, dOut, &Sm4Key);
       dIn = &dIn[16];
       dOut = &dOut[16];
   }
   return CRYPT_SUCCESS;
}
//
//
//       _cpri__SM4EncryptOFB()
//
//      This function performs SM4 encryption/decryption in OFB chain mode. The dIn buffer is modified to
//      contain the encrypted/decrypted text.
//      The input iv buffer is assumed to have a size equal to the block size (16 bytes). The returned value of iv
//      will be the nth encryption of the IV, where n is the number of blocks (full or partial) in the data stream.
//
//      Return Value                      Meaning
//
//      CRYPT_SUCCESS                     no non-fatal errors
//
LIB_EXPORT CRYPT_RESULT
_cpri__SM4EncryptOFB(
   BYTE                *dOut,          // OUT: the encrypted/decrypted data
   UINT32               keySizeInBits, // IN: key size in bit
   BYTE                *key,           // IN: key buffer. The size of this buffer in
                                       //     bytes is (keySizeInBits + 7) / 8
   BYTE                *iv,            // IN/OUT: IV for decryption. The size of this
                                       //     buffer is 16 byte
   UINT32              dInSize,         // IN: data size
   BYTE               *dIn              // IN: data buffer
   )
{
   BYTE           *pIv;
   SM4_KEY         Sm4Key;
   INT32           dSize;
   int             i;
   pAssert(dOut != NULL && key != NULL && iv != NULL && dIn != NULL);
   if(dInSize == 0)
       return CRYPT_SUCCESS;
   pAssert(dInSize <= INT32_MAX);
   dSize = (INT32)dInSize;
   // Create SM4 key schedule
   if (SM4_set_encrypt_key(key, keySizeInBits, &Sm4Key) != 0)
       FAIL(FATAL_ERROR_INTERNAL);
   // This is written so that dIn and dOut may be the same
   for(; dSize > 0; dSize -= 16)
   {
       // Encrypt the current value of the "IV"
       SM4_encrypt(iv, iv, &Sm4Key);
        // XOR the encrypted IV into dIn to create the cipher text (dOut)
        pIv = iv;
        for(i = (dSize < 16) ? dSize : 16; i > 0; i--)
            *dOut++ = (*pIv++ ^ *dIn++);
   }
   return CRYPT_SUCCESS;
}
#endif      //% TPM_ALG_SM4