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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
|
/** @file
Common interfaces to call Security library.
Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "IpSecCryptIo.h"
//
// The informations for the supported Encrypt/Decrpt Alogrithm.
//
GLOBAL_REMOVE_IF_UNREFERENCED ENCRYPT_ALGORITHM mIpsecEncryptAlgorithmList[IPSEC_ENCRYPT_ALGORITHM_LIST_SIZE] = {
{IKE_EALG_NULL, 0, 0, 1, NULL, NULL, NULL, NULL},
{IKE_EALG_NONE, 0, 0, 1, NULL, NULL, NULL, NULL},
{IKE_EALG_3DESCBC, 24, 8, 8, TdesGetContextSize, TdesInit, TdesCbcEncrypt, TdesCbcDecrypt},
{IKE_EALG_AESCBC, 16, 16, 16, AesGetContextSize, AesInit, AesCbcEncrypt, AesCbcDecrypt}
};
//
// The informations for the supported Authentication algorithm
//
GLOBAL_REMOVE_IF_UNREFERENCED AUTH_ALGORITHM mIpsecAuthAlgorithmList[IPSEC_AUTH_ALGORITHM_LIST_SIZE] = {
{IKE_AALG_NONE, 0, 0, 0, NULL, NULL, NULL, NULL},
{IKE_AALG_NULL, 0, 0, 0, NULL, NULL, NULL, NULL},
{IKE_AALG_SHA1HMAC, 20, 12, 64, HmacSha1GetContextSize, HmacSha1Init, HmacSha1Update, HmacSha1Final}
};
//
// The information for the supported Hash aglorithm
//
GLOBAL_REMOVE_IF_UNREFERENCED HASH_ALGORITHM mIpsecHashAlgorithmList[IPSEC_HASH_ALGORITHM_LIST_SIZE] = {
{IKE_AALG_NONE, 0, 0, 0, NULL, NULL, NULL, NULL},
{IKE_AALG_NULL, 0, 0, 0, NULL, NULL, NULL, NULL},
{IKE_AALG_SHA1HMAC, 20, 12, 64, Sha1GetContextSize, Sha1Init, Sha1Update, Sha1Final}
};
BOOLEAN mInitialRandomSeed = FALSE;
/**
Get the block size of specified encryption alogrithm.
@param[in] AlgorithmId The encryption algorithm ID.
@return The value of block size.
**/
UINTN
IpSecGetEncryptBlockSize (
IN UINT8 AlgorithmId
)
{
UINT8 Index;
for (Index = 0; Index < IPSEC_ENCRYPT_ALGORITHM_LIST_SIZE; Index++) {
if (AlgorithmId == mIpsecEncryptAlgorithmList[Index].AlgorithmId) {
return mIpsecEncryptAlgorithmList[Index].BlockSize;
}
}
return (UINTN) -1;
}
/**
Get the key length of the specified encryption alogrithm.
@param[in] AlgorithmId The encryption algorithm ID.
@return The value of key length.
**/
UINTN
IpSecGetEncryptKeyLength (
IN UINT8 AlgorithmId
)
{
UINT8 Index;
for (Index = 0; Index < IPSEC_ENCRYPT_ALGORITHM_LIST_SIZE; Index++) {
if (AlgorithmId == mIpsecEncryptAlgorithmList[Index].AlgorithmId) {
return mIpsecEncryptAlgorithmList[Index].KeyLength;
}
}
return (UINTN) -1;
}
/**
Get the IV size of the specified encryption alogrithm.
@param[in] AlgorithmId The encryption algorithm ID.
@return The value of IV size.
**/
UINTN
IpSecGetEncryptIvLength (
IN UINT8 AlgorithmId
)
{
UINT8 Index;
for (Index = 0; Index < IPSEC_ENCRYPT_ALGORITHM_LIST_SIZE; Index++) {
if (AlgorithmId == mIpsecEncryptAlgorithmList[Index].AlgorithmId) {
return mIpsecEncryptAlgorithmList[Index].IvLength;
}
}
return (UINTN) -1;
}
/**
Get the HMAC digest length by the specified Algorithm ID.
@param[in] AlgorithmId The specified Alogrithm ID.
@return The digest length of the specified Authentication Algorithm ID.
**/
UINTN
IpSecGetHmacDigestLength (
IN UINT8 AlgorithmId
)
{
UINT8 Index;
for (Index = 0; Index < IPSEC_AUTH_ALGORITHM_LIST_SIZE; Index++) {
if (mIpsecAuthAlgorithmList[Index].AlgorithmId == AlgorithmId) {
//
// Return the Digest Length of the Algorithm.
//
return mIpsecAuthAlgorithmList[Index].DigestLength;
}
}
return 0;
}
/**
Get the ICV size of the specified Authenticaion alogrithm.
@param[in] AlgorithmId The Authentication algorithm ID.
@return The value of ICV size.
**/
UINTN
IpSecGetIcvLength (
IN UINT8 AlgorithmId
)
{
UINT8 Index;
for (Index = 0; Index < IPSEC_AUTH_ALGORITHM_LIST_SIZE; Index++) {
if (AlgorithmId == mIpsecAuthAlgorithmList[Index].AlgorithmId) {
return mIpsecAuthAlgorithmList[Index].IcvLength;
}
}
return (UINTN) -1;
}
/**
Generate a random data for IV. If the IvSize is zero, not needed to create
IV and return EFI_SUCCESS.
@param[in] IvBuffer The pointer of the IV buffer.
@param[in] IvSize The IV size in bytes.
@retval EFI_SUCCESS Create a random data for IV.
**/
EFI_STATUS
IpSecGenerateIv (
IN UINT8 *IvBuffer,
IN UINTN IvSize
)
{
if (IvSize != 0) {
return IpSecCryptoIoGenerateRandomBytes (IvBuffer, IvSize);
}
return EFI_SUCCESS;
}
/**
Get index of the specified encryption alogrithm from the mIpsecEncryptAlgorithemList.
@param[in] AlgorithmId The encryption algorithm ID.
@return the index.
**/
UINTN
IpSecGetIndexFromEncList (
IN UINT8 AlgorithmId
)
{
UINT8 Index;
for (Index = 0; Index < IPSEC_ENCRYPT_ALGORITHM_LIST_SIZE; Index++) {
if (AlgorithmId == mIpsecEncryptAlgorithmList[Index].AlgorithmId) {
return Index;
}
}
return (UINTN) -1;
}
/**
Get index of the specified encryption alogrithm from the mIpsecAuthAlgorithemList.
@param[in] AlgorithmId The encryption algorithm ID.
@return the index.
**/
UINTN
IpSecGetIndexFromAuthList (
IN UINT8 AlgorithmId
)
{
UINT8 Index;
for (Index = 0; Index < IPSEC_AUTH_ALGORITHM_LIST_SIZE; Index++) {
if (AlgorithmId == mIpsecAuthAlgorithmList[Index].AlgorithmId) {
//
// The BlockSize is same with IvSize.
//
return Index;
}
}
return (UINTN) -1;
}
/**
Encrypt the buffer.
This function calls relevant encryption interface from CryptoLib according to
the input alogrithm ID. The InData should be multiple of block size. This function
doesn't perform the padding. If it has the Ivec data, the length of it should be
same with the block size. The block size is different from the different algorithm.
@param[in] AlgorithmId The Alogrithem identification defined in RFC.
@param[in] Key Pointer to the buffer containing encrypting key.
@param[in] KeyBits The length of the key in bits.
@param[in] Ivec Point to the buffer containning the Initializeion
Vector (IV) data.
@param[in] InData Point to the buffer containing the data to be
encrypted.
@param[in] InDataLength The length of InData in Bytes.
@param[out] OutData Point to the buffer that receives the encryption
output.
@retval EFI_UNSUPPORTED The input Algorithm is not supported.
@retval EFI_OUT_OF_RESOURCE The required resource can't be allocated.
@retval EFI_SUCCESS The operation completed successfully.
**/
EFI_STATUS
IpSecCryptoIoEncrypt (
IN CONST UINT8 AlgorithmId,
IN CONST UINT8 *Key,
IN CONST UINTN KeyBits,
IN CONST UINT8 *Ivec, OPTIONAL
IN UINT8 *InData,
IN UINTN InDataLength,
OUT UINT8 *OutData
)
{
UINTN Index;
UINTN ContextSize;
UINT8 *Context;
EFI_STATUS Status;
Status = EFI_UNSUPPORTED;
switch (AlgorithmId) {
case IKE_EALG_NULL:
case IKE_EALG_NONE:
CopyMem (OutData, InData, InDataLength);
return EFI_SUCCESS;
case IKE_EALG_3DESCBC:
case IKE_EALG_AESCBC:
Index = IpSecGetIndexFromEncList (AlgorithmId);
if (Index == -1) {
return Status;
}
//
// Get Context Size
//
ContextSize = mIpsecEncryptAlgorithmList[Index].CipherGetContextSize ();
Context = AllocateZeroPool (ContextSize);
if (Context == NULL) {
return EFI_OUT_OF_RESOURCES;
}
//
// Initiate Context
//
if (mIpsecEncryptAlgorithmList[Index].CipherInitiate (Context, Key, KeyBits)) {
if (mIpsecEncryptAlgorithmList[Index].CipherEncrypt (Context, InData, InDataLength, Ivec, OutData)) {
Status = EFI_SUCCESS;
}
}
break;
default:
return Status;
}
if (Context != NULL) {
FreePool (Context);
}
return Status;
}
/**
Decrypts the buffer.
This function calls relevant Decryption interface from CryptoLib according to
the input alogrithm ID. The InData should be multiple of block size. This function
doesn't perform the padding. If it has the Ivec data, the length of it should be
same with the block size. The block size is different from the different algorithm.
@param[in] AlgorithmId The Alogrithem identification defined in RFC.
@param[in] Key Pointer to the buffer containing encrypting key.
@param[in] KeyBits The length of the key in bits.
@param[in] Ivec Point to the buffer containning the Initializeion
Vector (IV) data.
@param[in] InData Point to the buffer containing the data to be
decrypted.
@param[in] InDataLength The length of InData in Bytes.
@param[out] OutData Pointer to the buffer that receives the decryption
output.
@retval EFI_UNSUPPORTED The input Algorithm is not supported.
@retval EFI_OUT_OF_RESOURCE The required resource can't be allocated.
@retval EFI_SUCCESS The operation completed successfully.
**/
EFI_STATUS
IpSecCryptoIoDecrypt (
IN CONST UINT8 AlgorithmId,
IN CONST UINT8 *Key,
IN CONST UINTN KeyBits,
IN CONST UINT8 *Ivec, OPTIONAL
IN UINT8 *InData,
IN UINTN InDataLength,
OUT UINT8 *OutData
)
{
UINTN Index;
UINTN ContextSize;
UINT8 *Context;
EFI_STATUS Status;
Status = EFI_UNSUPPORTED;
switch (AlgorithmId) {
case IKE_EALG_NULL:
case IKE_EALG_NONE:
CopyMem (OutData, InData, InDataLength);
return EFI_SUCCESS;
case IKE_EALG_3DESCBC:
case IKE_EALG_AESCBC:
Index = IpSecGetIndexFromEncList(AlgorithmId);
if (Index == -1) {
return Status;
}
//
// Get Context Size
//
ContextSize = mIpsecEncryptAlgorithmList[Index].CipherGetContextSize();
Context = AllocateZeroPool (ContextSize);
if (Context == NULL) {
return EFI_OUT_OF_RESOURCES;
}
//
// Initiate Context
//
if (mIpsecEncryptAlgorithmList[Index].CipherInitiate (Context, Key, KeyBits)) {
if (mIpsecEncryptAlgorithmList[Index].CipherDecrypt (Context, InData, InDataLength, Ivec, OutData)) {
Status = EFI_SUCCESS;
}
}
break;
default:
return Status;
}
if (Context != NULL) {
FreePool (Context);
}
return Status;
}
/**
Digests the Payload with key and store the result into the OutData.
This function calls relevant Hmac interface from CryptoLib according to
the input alogrithm ID. It computes all datas from InDataFragment and output
the result into the OutData buffer. If the OutDataSize is larger than the related
HMAC alogrithm output size, return EFI_INVALID_PARAMETER.
@param[in] AlgorithmId The authentication Identification.
@param[in] Key Pointer of the authentication key.
@param[in] KeyLength The length of the Key in bytes.
@param[in] InDataFragment The list contains all data to be authenticated.
@param[in] FragmentCount The size of the InDataFragment.
@param[out] OutData For in, the buffer to receive the output data.
For out, the buffer contains the authenticated data.
@param[in] OutDataSize The size of the buffer of OutData.
@retval EFI_UNSUPPORTED If the AuthAlg is not in the support list.
@retval EFI_INVALID_PARAMETER The OutData buffer size is larger than algorithm digest size.
@retval EFI_SUCCESS Authenticate the payload successfully.
@retval otherwise Authentication of the payload fails.
**/
EFI_STATUS
IpSecCryptoIoHmac (
IN CONST UINT8 AlgorithmId,
IN CONST UINT8 *Key,
IN UINTN KeyLength,
IN HASH_DATA_FRAGMENT *InDataFragment,
IN UINTN FragmentCount,
OUT UINT8 *OutData,
IN UINTN OutDataSize
)
{
UINTN ContextSize;
UINTN Index;
UINT8 FragmentIndex;
UINT8 *HashContext;
EFI_STATUS Status;
UINT8 *OutHashData;
UINTN OutHashSize;
Status = EFI_UNSUPPORTED;
OutHashData = NULL;
OutHashSize = IpSecGetHmacDigestLength (AlgorithmId);
//
// If the expected hash data size is larger than the related Hash algorithm
// output length, return EFI_INVALID_PARAMETER.
//
if (OutDataSize > OutHashSize) {
return EFI_INVALID_PARAMETER;
}
OutHashData = AllocatePool (OutHashSize);
if (OutHashData == NULL) {
return EFI_OUT_OF_RESOURCES;
}
switch (AlgorithmId) {
case IKE_AALG_NONE :
case IKE_AALG_NULL :
return EFI_SUCCESS;
case IKE_AALG_SHA1HMAC:
Index = IpSecGetIndexFromAuthList (AlgorithmId);
if (Index == -1) {
return Status;
}
//
// Get Context Size
//
ContextSize = mIpsecAuthAlgorithmList[Index].HmacGetContextSize();
HashContext = AllocateZeroPool (ContextSize);
if (HashContext == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto Exit;
}
//
// Initiate HMAC context and hash the input data.
//
if (mIpsecAuthAlgorithmList[Index].HmacInitiate(HashContext, Key, KeyLength)) {
for (FragmentIndex = 0; FragmentIndex < FragmentCount; FragmentIndex++) {
if (!mIpsecAuthAlgorithmList[Index].HmacUpdate (
HashContext,
InDataFragment[FragmentIndex].Data,
InDataFragment[FragmentIndex].DataSize
)
) {
goto Exit;
}
}
if (mIpsecAuthAlgorithmList[Index].HmacFinal (HashContext, OutHashData)) {
//
// In some cases, like the Icv computing, the Icv size might be less than
// the key length size, so copy the part of hash data to the OutData.
//
CopyMem (OutData, OutHashData, OutDataSize);
Status = EFI_SUCCESS;
}
goto Exit;
}
default:
return Status;
}
Exit:
if (HashContext != NULL) {
FreePool (HashContext);
}
if (OutHashData != NULL) {
FreePool (OutHashData);
}
return Status;
}
/**
Digests the Payload and store the result into the OutData.
This function calls relevant Hash interface from CryptoLib according to
the input alogrithm ID. It computes all datas from InDataFragment and output
the result into the OutData buffer. If the OutDataSize is larger than the related
Hash alogrithm output size, return EFI_INVALID_PARAMETER.
@param[in] AlgorithmId The authentication Identification.
@param[in] InDataFragment A list contains all data to be authenticated.
@param[in] FragmentCount The size of the InDataFragment.
@param[out] OutData For in, the buffer to receive the output data.
For out, the buffer contains the authenticated data.
@param[in] OutDataSize The size of the buffer of OutData.
@retval EFI_UNSUPPORTED If the AuthAlg is not in the support list.
@retval EFI_SUCCESS Authenticated the payload successfully.
@retval EFI_INVALID_PARAMETER If the OutDataSize is larger than the related Hash
algorithm could handle.
@retval otherwise Authentication of the payload failed.
**/
EFI_STATUS
IpSecCryptoIoHash (
IN CONST UINT8 AlgorithmId,
IN HASH_DATA_FRAGMENT *InDataFragment,
IN UINTN FragmentCount,
OUT UINT8 *OutData,
IN UINTN OutDataSize
)
{
UINTN ContextSize;
UINTN Index;
UINT8 FragmentIndex;
UINT8 *HashContext;
EFI_STATUS Status;
UINT8 *OutHashData;
UINTN OutHashSize;
Status = EFI_UNSUPPORTED;
OutHashData = NULL;
OutHashSize = IpSecGetHmacDigestLength (AlgorithmId);
//
// If the expected hash data size is larger than the related Hash algorithm
// output length, return EFI_INVALID_PARAMETER.
//
if (OutDataSize > OutHashSize) {
return EFI_INVALID_PARAMETER;
}
OutHashData = AllocatePool (OutHashSize);
if (OutHashData == NULL) {
return EFI_OUT_OF_RESOURCES;
}
switch (AlgorithmId) {
case IKE_AALG_NONE:
case IKE_AALG_NULL:
return EFI_SUCCESS;
case IKE_AALG_SHA1HMAC:
Index = IpSecGetIndexFromAuthList (AlgorithmId);
if (Index == -1) {
return Status;
}
//
// Get Context Size
//
ContextSize = mIpsecHashAlgorithmList[Index].HashGetContextSize();
HashContext = AllocateZeroPool (ContextSize);
if (HashContext == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto Exit;
}
//
// Initiate Hash context and hash the input data.
//
if (mIpsecHashAlgorithmList[Index].HashInitiate(HashContext)) {
for (FragmentIndex = 0; FragmentIndex < FragmentCount; FragmentIndex++) {
if (!mIpsecHashAlgorithmList[Index].HashUpdate (
HashContext,
InDataFragment[FragmentIndex].Data,
InDataFragment[FragmentIndex].DataSize
)
) {
goto Exit;
}
}
if (mIpsecHashAlgorithmList[Index].HashFinal (HashContext, OutHashData)) {
//
// In some cases, like the Icv computing, the Icv size might be less than
// the key length size, so copy the part of hash data to the OutData.
//
CopyMem (OutData, OutHashData, OutDataSize);
Status = EFI_SUCCESS;
}
goto Exit;
}
default:
return Status;
}
Exit:
if (HashContext != NULL) {
FreePool (HashContext);
}
if (OutHashData != NULL) {
FreePool (OutHashData);
}
return Status;
}
/**
Generates the Diffie-Hellman public key.
This function first initiate a DHContext, then call the DhSetParameter() to set
the prime and primelenght, at end call the DhGenerateKey() to generates random
secret exponent, and computes the public key. The output returned via parameter
PublicKey and PublicKeySize. DH context is updated accordingly. If the PublicKey
buffer is too small to hold the public key, EFI_INVALID_PARAMETER is returned
and PublicKeySize is set to the required buffer size to obtain the public key.
@param[in, out] DhContext Pointer to the DH context.
@param[in] Generator Vlaue of generator.
@param[in] PrimeLength Length in bits of prime to be generated.
@param[in] Prime Pointer to the buffer to receive the generated
prime number.
@param[out] PublicKey Pointer to the buffer to receive generated public key.
@param[in, out] PublicKeySize For in, the size of PublicKey buffer in bytes.
For out, the size of data returned in PublicKey
buffer in bytes.
@retval EFI_SUCCESS The operation perfoms successfully.
@retval Otherwise The operation is failed.
**/
EFI_STATUS
IpSecCryptoIoDhGetPublicKey (
IN OUT UINT8 **DhContext,
IN UINTN Generator,
IN UINTN PrimeLength,
IN CONST UINT8 *Prime,
OUT UINT8 *PublicKey,
IN OUT UINTN *PublicKeySize
)
{
EFI_STATUS Status;
*DhContext = DhNew ();
ASSERT (*DhContext != NULL);
if (!DhSetParameter (*DhContext, Generator, PrimeLength, Prime)) {
Status = EFI_INVALID_PARAMETER;
goto Exit;
}
if (!DhGenerateKey (*DhContext, PublicKey, PublicKeySize)) {
Status = EFI_INVALID_PARAMETER;
goto Exit;
}
return EFI_SUCCESS;
Exit:
if (*DhContext != NULL) {
DhFree (*DhContext);
DhContext = NULL;
}
return Status;
}
/**
Generates exchanged common key.
Given peer's public key, this function computes the exchanged common key, based
on its own context including value of prime modulus and random secret exponent.
@param[in, out] DhContext Pointer to the DH context.
@param[in] PeerPublicKey Pointer to the peer's Public Key.
@param[in] PeerPublicKeySize Size of peer's public key in bytes.
@param[out] Key Pointer to the buffer to receive generated key.
@param[in, out] KeySize For in, the size of Key buffer in bytes.
For out, the size of data returned in Key
buffer in bytes.
@retval EFI_SUCCESS The operation perfoms successfully.
@retval Otherwise The operation is failed.
**/
EFI_STATUS
IpSecCryptoIoDhComputeKey (
IN OUT UINT8 *DhContext,
IN CONST UINT8 *PeerPublicKey,
IN UINTN PeerPublicKeySize,
OUT UINT8 *Key,
IN OUT UINTN *KeySize
)
{
if (!DhComputeKey (DhContext, PeerPublicKey, PeerPublicKeySize, Key, KeySize)) {
return EFI_INVALID_PARAMETER;
}
return EFI_SUCCESS;
}
/**
Releases the DH context. If DhContext is NULL, return EFI_INVALID_PARAMETER.
@param[in, out] DhContext Pointer to the DH context to be freed.
@retval EFI_SUCCESS The operation perfoms successfully.
@retval EFI_INVALID_PARAMETER The DhContext is NULL.
**/
EFI_STATUS
IpSecCryptoIoFreeDh (
IN OUT UINT8 **DhContext
)
{
if (*DhContext == NULL) {
return EFI_INVALID_PARAMETER;
}
DhFree (*DhContext);
return EFI_SUCCESS;
}
/**
Generates random numbers of specified size.
If the Random Generator wasn't initiated, initiate it first, then call RandomBytes.
@param[out] OutBuffer Pointer to buffer to receive random value.
@param[in] Bytes Size of randome bytes to generate.
@retval EFI_SUCCESS The operation perfoms successfully.
@retval Otherwise The operation is failed.
**/
EFI_STATUS
IpSecCryptoIoGenerateRandomBytes (
OUT UINT8* OutBuffer,
IN UINTN Bytes
)
{
if (!mInitialRandomSeed) {
RandomSeed (NULL, 0);
mInitialRandomSeed = TRUE;
}
if (RandomBytes (OutBuffer, Bytes)) {
return EFI_SUCCESS;
} else {
return EFI_INVALID_PARAMETER;
}
}
/**
Authenticate data with the certificate.
@param[in] InData Pointer to the Data to be signed.
@param[in] InDataSize InData size in bytes.
@param[in] PrivateKey Pointer to the private key.
@param[in] PrivateKeySize The size of Private Key in bytes.
@param[in] KeyPassWord Pointer to the password for retrieving private key.
@param[in] KeyPwdSize The size of Key Password in bytes.
@param[out] OutData The pointer to the signed data.
@param[in, out] OutDataSize Pointer to contain the size of out data.
**/
VOID
IpSecCryptoIoAuthDataWithCertificate (
IN UINT8 *InData,
IN UINTN InDataSize,
IN UINT8 *PrivateKey,
IN UINTN PrivateKeySize,
IN UINT8 *KeyPassWord,
IN UINTN KeyPwdSize,
OUT UINT8 **OutData,
IN OUT UINTN *OutDataSize
)
{
UINT8 *RsaContext;
UINT8 *Signature;
UINTN SigSize;
SigSize = 0;
RsaContext = NULL;
//
// Retrieve RSA Private Key from password-protected PEM data
//
RsaGetPrivateKeyFromPem (
(CONST UINT8 *)PrivateKey,
PrivateKeySize,
(CONST CHAR8 *)KeyPassWord,
(VOID **) &RsaContext
);
if (RsaContext == NULL) {
return;
}
//
// Sign data
//
Signature = NULL;
if (!RsaPkcs1Sign (RsaContext, InData, InDataSize, Signature, &SigSize)) {
Signature = AllocateZeroPool (SigSize);
} else {
return;
}
RsaPkcs1Sign (RsaContext, InData, InDataSize, Signature, &SigSize);
*OutData = Signature;
*OutDataSize = SigSize;
if (RsaContext != NULL) {
RsaFree (RsaContext);
}
}
/**
Verify the singed data with the public key which is contained in a certificate.
@param[in] InCert Pointer to the Certificate which contains the
public key.
@param[in] CertLen The size of Certificate in bytes.
@param[in] InCa Pointer to the CA certificate
@param[in] CaLen The size of CA certificate in bytes.
@param[in] InData Pointer to octect message hash to be checked.
@param[in] InDataSize Size of the message hash in bytes.
@param[in] Singnature The pointer to the RSA PKCS1-V1_5 signature to be verifed.
@param[in] SigSize Size of signature in bytes.
@retval TRUE Valid signature encoded in PKCS1-v1_5.
@retval FALSE Invalid signature or invalid RSA context.
**/
BOOLEAN
IpSecCryptoIoVerifySignDataByCertificate (
IN UINT8 *InCert,
IN UINTN CertLen,
IN UINT8 *InCa,
IN UINTN CaLen,
IN UINT8 *InData,
IN UINTN InDataSize,
IN UINT8 *Singnature,
IN UINTN SigSize
)
{
UINT8 *RsaContext;
BOOLEAN Status;
//
// Create the RSA Context
//
RsaContext = RsaNew ();
if (RsaContext == NULL) {
return FALSE;
}
//
// Verify the validity of X509 Certificate
//
if (!X509VerifyCert (InCert, CertLen, InCa, CaLen)) {
return FALSE;
}
//
// Retrieve the RSA public Key from Certificate
//
RsaGetPublicKeyFromX509 ((CONST UINT8 *)InCert, CertLen, (VOID **)&RsaContext);
//
// Verify data
//
Status = RsaPkcs1Verify (RsaContext, InData, InDataSize, Singnature, SigSize);
if (RsaContext != NULL) {
RsaFree (RsaContext);
}
return Status;
}
/**
Retrieves the RSA Public Key from one X509 certificate (DER format only).
@param[in] InCert Pointer to the certificate.
@param[in] CertLen The size of the certificate in bytes.
@param[out] PublicKey Pointer to the retrieved public key.
@param[out] PublicKeyLen Size of Public Key in bytes.
@retval EFI_SUCCESS Successfully get the public Key.
@retval EFI_INVALID_PARAMETER The certificate is malformed.
**/
EFI_STATUS
IpSecCryptoIoGetPublicKeyFromCert (
IN UINT8 *InCert,
IN UINTN CertLen,
OUT UINT8 **PublicKey,
OUT UINTN *PublicKeyLen
)
{
UINT8 *RsaContext;
EFI_STATUS Status;
Status = EFI_SUCCESS;
//
// Create the RSA Context
//
RsaContext = RsaNew ();
//
// Retrieve the RSA public key from CA Certificate
//
if (!RsaGetPublicKeyFromX509 ((CONST UINT8 *)InCert, CertLen, (VOID **) &RsaContext)) {
Status = EFI_INVALID_PARAMETER;
goto EXIT;
}
*PublicKeyLen = 0;
RsaGetKey (RsaContext, RsaKeyN, NULL, PublicKeyLen);
*PublicKey = AllocateZeroPool (*PublicKeyLen);
ASSERT (*PublicKey != NULL);
if (!RsaGetKey (RsaContext, RsaKeyN, *PublicKey, PublicKeyLen)) {
Status = EFI_INVALID_PARAMETER;
}
EXIT:
if (RsaContext != NULL) {
RsaFree (RsaContext);
}
return Status;
}
/**
Retrieves the subject name from one X509 certificate (DER format only).
@param[in] InCert Pointer to the X509 certificate.
@param[in] CertSize The size of the X509 certificate in bytes.
@param[out] CertSubject Pointer to the retrieved certificate subject.
@param[out] SubjectSize The size of Certificate Subject in bytes.
@retval EFI_SUCCESS Retrieved the certificate subject successfully.
@retval EFI_INVALID_PARAMETER The certificate is malformed.
**/
EFI_STATUS
IpSecCryptoIoGetSubjectFromCert (
IN UINT8 *InCert,
IN UINTN CertSize,
OUT UINT8 **CertSubject,
OUT UINTN *SubjectSize
)
{
EFI_STATUS Status;
Status = EFI_SUCCESS;
*SubjectSize = 0;
X509GetSubjectName (InCert, CertSize, *CertSubject, SubjectSize);
*CertSubject = AllocateZeroPool (*SubjectSize);
if (!X509GetSubjectName (InCert, CertSize, *CertSubject, SubjectSize)) {
Status = EFI_INVALID_PARAMETER;
}
return Status;
}
|