summaryrefslogtreecommitdiff
path: root/SecurityPkg/DeviceSecurity/OsStub/CryptlibWrapper/CryptlibWrapper.c
blob: 64db9750ff80191337e3753f02cdee2e27ad41e0 (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
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
/** @file
  EDKII Device Security library for SPDM device.
  It follows the SPDM Specification.

Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent

**/
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/BaseCryptLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include "hal/base.h"
#include "hal/library/cryptlib.h"

void *
libspdm_sha256_new (
  void
  )
{
  size_t  CtxSize;
  void    *HashCtx;

  HashCtx = NULL;
  CtxSize = Sha256GetContextSize ();
  HashCtx = AllocatePool (CtxSize);

  return HashCtx;
}

void
libspdm_sha256_free (
  void  *Sha256Ctx
  )
{
  if (Sha256Ctx != NULL) {
    FreePool (Sha256Ctx);
    Sha256Ctx = NULL;
  }
}

bool
libspdm_sha256_init (
  void  *Sha256Ctx
  )
{
  return Sha256Init (Sha256Ctx);
}

bool
libspdm_sha256_duplicate (
  const void  *Sha256Context,
  void        *NewSha256Context
  )
{
  return Sha256Duplicate (Sha256Context, NewSha256Context);
}

bool
libspdm_sha256_update (
  void        *Sha256Context,
  const void  *Data,
  size_t      DataSize
  )
{
  return Sha256Update (Sha256Context, Data, DataSize);
}

bool
libspdm_sha256_final (
  void     *sha256_context,
  uint8_t  *hash_value
  )
{
  return Sha256Final (sha256_context, hash_value);
}

bool
libspdm_sha256_hash_all (
  const void  *data,
  size_t      data_size,
  uint8_t     *hash_value
  )
{
  return Sha256HashAll (data, data_size, hash_value);
}

void *
libspdm_sha384_new (
  void
  )
{
  size_t  CtxSize;
  void    *HashCtx;

  HashCtx = NULL;
  CtxSize = Sha384GetContextSize ();
  HashCtx = AllocatePool (CtxSize);

  return HashCtx;
}

void
libspdm_sha384_free (
  void  *Sha384Ctx
  )
{
  if (Sha384Ctx != NULL) {
    FreePool (Sha384Ctx);
    Sha384Ctx = NULL;
  }
}

bool
libspdm_sha384_init (
  void  *sha384_context
  )
{
  return Sha384Init (sha384_context);
}

bool
libspdm_sha384_duplicate (
  const void  *sha384_context,
  void        *new_sha384_context
  )
{
  return Sha384Duplicate (sha384_context, new_sha384_context);
}

bool
libspdm_sha384_update (
  void        *sha384_context,
  const void  *data,
  size_t      data_size
  )
{
  return Sha384Update (sha384_context, data, data_size);
}

bool
libspdm_sha384_final (
  void     *sha384_context,
  uint8_t  *hash_value
  )
{
  return Sha384Final (sha384_context, hash_value);
}

bool
libspdm_sha384_hash_all (
  const void  *data,
  size_t      data_size,
  uint8_t     *hash_value
  )
{
  return Sha384HashAll (data, data_size, hash_value);
}

void *
libspdm_hmac_sha256_new (
  void
  )
{
  return HmacSha256New ();
}

void
libspdm_hmac_sha256_free (
  void  *hmac_sha256_ctx
  )
{
  HmacSha256Free (hmac_sha256_ctx);
}

bool
libspdm_hmac_sha256_set_key (
  void           *hmac_sha256_ctx,
  const uint8_t  *key,
  size_t         key_size
  )
{
  return HmacSha256SetKey (hmac_sha256_ctx, key, key_size);
}

bool
libspdm_hmac_sha256_duplicate (
  const void  *hmac_sha256_ctx,
  void        *new_hmac_sha256_ctx
  )
{
  return HmacSha256Duplicate (hmac_sha256_ctx, new_hmac_sha256_ctx);
}

bool
libspdm_hmac_sha256_update (
  void        *hmac_sha256_ctx,
  const void  *data,
  size_t      data_size
  )
{
  return HmacSha256Update (hmac_sha256_ctx, data, data_size);
}

bool
libspdm_hmac_sha256_final (
  void     *hmac_sha256_ctx,
  uint8_t  *hmac_value
  )
{
  return HmacSha256Final (hmac_sha256_ctx, hmac_value);
}

bool
libspdm_hmac_sha256_all (
  const void     *data,
  size_t         data_size,
  const uint8_t  *key,
  size_t         key_size,
  uint8_t        *hmac_value
  )
{
  return HmacSha256All (data, data_size, key, key_size, hmac_value);
}

void *
libspdm_hmac_sha384_new (
  void
  )
{
  return HmacSha384New ();
}

void
libspdm_hmac_sha384_free (
  void  *hmac_sha384_ctx
  )
{
  HmacSha384Free (hmac_sha384_ctx);
}

bool
libspdm_hmac_sha384_set_key (
  void           *hmac_sha384_ctx,
  const uint8_t  *key,
  size_t         key_size
  )
{
  return HmacSha384SetKey (hmac_sha384_ctx, key, key_size);
}

bool
libspdm_hmac_sha384_duplicate (
  const void  *hmac_sha384_ctx,
  void        *new_hmac_sha384_ctx
  )
{
  return HmacSha384Duplicate (hmac_sha384_ctx, new_hmac_sha384_ctx);
}

bool
libspdm_hmac_sha384_update (
  void        *hmac_sha384_ctx,
  const void  *data,
  size_t      data_size
  )
{
  return HmacSha384Update (hmac_sha384_ctx, data, data_size);
}

bool
libspdm_hmac_sha384_final (
  void     *hmac_sha384_ctx,
  uint8_t  *hmac_value
  )
{
  return HmacSha384Final (hmac_sha384_ctx, hmac_value);
}

bool
libspdm_hmac_sha384_all (
  const void     *data,
  size_t         data_size,
  const uint8_t  *key,
  size_t         key_size,
  uint8_t        *hmac_value
  )
{
  return HmacSha384All (data, data_size, key, key_size, hmac_value);
}

bool
libspdm_aead_aes_gcm_encrypt (
  const uint8_t  *key,
  size_t         key_size,
  const uint8_t  *iv,
  size_t         iv_size,
  const uint8_t  *a_data,
  size_t         a_data_size,
  const uint8_t  *data_in,
  size_t         data_in_size,
  uint8_t        *tag_out,
  size_t         tag_size,
  uint8_t        *data_out,
  size_t         *data_out_size
  )
{
  return AeadAesGcmEncrypt (
           key,
           key_size,
           iv,
           iv_size,
           a_data,
           a_data_size,
           data_in,
           data_in_size,
           tag_out,
           tag_size,
           data_out,
           data_out_size
           );
}

bool
libspdm_aead_aes_gcm_decrypt (
  const uint8_t  *key,
  size_t         key_size,
  const uint8_t  *iv,
  size_t         iv_size,
  const uint8_t  *a_data,
  size_t         a_data_size,
  const uint8_t  *data_in,
  size_t         data_in_size,
  const uint8_t  *tag,
  size_t         tag_size,
  uint8_t        *data_out,
  size_t         *data_out_size
  )
{
  return AeadAesGcmDecrypt (
           key,
           key_size,
           iv,
           iv_size,
           a_data,
           a_data_size,
           data_in,
           data_in_size,
           tag,
           tag_size,
           data_out,
           data_out_size
           );
}

void
libspdm_rsa_free (
  void  *rsa_context
  )
{
  RsaFree (rsa_context);
}

bool
libspdm_rsa_pkcs1_sign_with_nid (
  void           *rsa_context,
  size_t         hash_nid,
  const uint8_t  *message_hash,
  size_t         hash_size,
  uint8_t        *signature,
  size_t         *sig_size
  )
{
  switch (hash_nid) {
    case CRYPTO_NID_SHA256:
      if (hash_size != SHA256_DIGEST_SIZE) {
        return FALSE;
      }

      break;

    case CRYPTO_NID_SHA384:
      if (hash_size != SHA384_DIGEST_SIZE) {
        return FALSE;
      }

      break;

    case CRYPTO_NID_SHA512:
      if (hash_size != SHA512_DIGEST_SIZE) {
        return FALSE;
      }

      break;

    default:
      return FALSE;
  }

  return RsaPkcs1Sign (
           rsa_context,
           message_hash,
           hash_size,
           signature,
           sig_size
           );
}

bool
libspdm_rsa_pkcs1_verify_with_nid (
  void           *rsa_context,
  size_t         hash_nid,
  const uint8_t  *message_hash,
  size_t         hash_size,
  const uint8_t  *signature,
  size_t         sig_size
  )
{
  switch (hash_nid) {
    case CRYPTO_NID_SHA256:
      if (hash_size != SHA256_DIGEST_SIZE) {
        return false;
      }

      break;

    case CRYPTO_NID_SHA384:
      if (hash_size != SHA384_DIGEST_SIZE) {
        return false;
      }

      break;

    case CRYPTO_NID_SHA512:
      if (hash_size != SHA512_DIGEST_SIZE) {
        return false;
      }

      break;

    default:
      return false;
  }

  return RsaPkcs1Verify (
           rsa_context,
           message_hash,
           hash_size,
           signature,
           sig_size
           );
}

bool
libspdm_rsa_get_private_key_from_pem (
  const uint8_t  *pem_data,
  size_t         pem_size,
  const char     *password,
  void           **rsa_context
  )
{
  return RsaGetPrivateKeyFromPem (pem_data, pem_size, password, rsa_context);
}

bool
libspdm_rsa_get_public_key_from_x509 (
  const uint8_t  *cert,
  size_t         cert_size,
  void           **rsa_context
  )
{
  return RsaGetPublicKeyFromX509 (cert, cert_size, rsa_context);
}

bool
libspdm_ec_get_public_key_from_der (
  const uint8_t  *der_data,
  size_t         der_size,
  void           **ec_context
  )
{
  return false;
}

bool
libspdm_rsa_get_public_key_from_der (
  const uint8_t  *der_data,
  size_t         der_size,
  void           **rsa_context
  )
{
  return false;
}

bool
libspdm_ec_get_private_key_from_pem (
  const uint8_t  *pem_data,
  size_t         pem_size,
  const char     *password,
  void           **ec_context
  )
{
  return EcGetPrivateKeyFromPem (pem_data, pem_size, password, ec_context);
}

bool
libspdm_ec_get_public_key_from_x509 (
  const uint8_t  *cert,
  size_t         cert_size,
  void           **ec_context
  )
{
  return EcGetPublicKeyFromX509 (cert, cert_size, ec_context);
}

bool
libspdm_asn1_get_tag (
  uint8_t        **ptr,
  const uint8_t  *end,
  size_t         *length,
  uint32_t       tag
  )
{
  return Asn1GetTag (ptr, end, length, tag);
}

bool
libspdm_x509_get_subject_name (
  const uint8_t  *cert,
  size_t         cert_size,
  uint8_t        *cert_subject,
  size_t         *subject_size
  )
{
  return X509GetSubjectName (cert, cert_size, cert_subject, subject_size);
}

bool
libspdm_x509_get_common_name (
  const uint8_t  *cert,
  size_t         cert_size,
  char           *common_name,
  size_t         *common_name_size
  )
{
  EFI_STATUS  Status;

  Status = X509GetCommonName (cert, cert_size, common_name, common_name_size);
  if (EFI_ERROR (Status)) {
    return false;
  } else {
    return true;
  }
}

bool
libspdm_x509_get_organization_name (
  const uint8_t  *cert,
  size_t         cert_size,
  char           *name_buffer,
  size_t         *name_buffer_size
  )
{
  EFI_STATUS  Status;

  Status = X509GetOrganizationName (cert, cert_size, name_buffer, name_buffer_size);
  if (EFI_ERROR (Status)) {
    return false;
  } else {
    return true;
  }
}

bool
libspdm_x509_get_version (
  const uint8_t  *cert,
  size_t         cert_size,
  size_t         *version
  )
{
  return X509GetVersion (cert, cert_size, version);
}

bool
libspdm_x509_get_serial_number (
  const uint8_t  *cert,
  size_t         cert_size,
  uint8_t        *serial_number,
  size_t         *serial_number_size
  )
{
  return X509GetSerialNumber (cert, cert_size, serial_number, serial_number_size);
}

bool
libspdm_x509_get_issuer_name (
  const uint8_t  *cert,
  size_t         cert_size,
  uint8_t        *cert_issuer,
  size_t         *issuer_size
  )
{
  return X509GetIssuerName (cert, cert_size, cert_issuer, issuer_size);
}

bool
libspdm_x509_get_signature_algorithm (
  const uint8_t  *cert,
  size_t         cert_size,
  uint8_t        *oid,
  size_t         *oid_size
  )
{
  return X509GetSignatureAlgorithm (cert, cert_size, oid, oid_size);
}

bool
libspdm_x509_get_extension_data (
  const uint8_t  *cert,
  size_t         cert_size,
  const uint8_t  *oid,
  size_t         oid_size,
  uint8_t        *extension_data,
  size_t         *extension_data_size
  )
{
  return X509GetExtensionData (
           cert,
           cert_size,
           oid,
           oid_size,
           extension_data,
           extension_data_size
           );
}

bool
libspdm_x509_get_validity (
  const uint8_t  *cert,
  size_t         cert_size,
  uint8_t        *from,
  size_t         *from_size,
  uint8_t        *to,
  size_t         *to_size
  )
{
  return X509GetValidity (cert, cert_size, from, from_size, to, to_size);
}

bool
libspdm_x509_set_date_time (
  const char  *date_time_str,
  void        *date_time,
  size_t      *date_time_size
  )
{
  return X509FormatDateTime (date_time_str, date_time, date_time_size);
}

int32_t
libspdm_x509_compare_date_time (
  const void  *date_time1,
  const void  *date_time2
  )
{
  return X509CompareDateTime (date_time1, date_time2);
}

bool
libspdm_x509_get_key_usage (
  const uint8_t  *cert,
  size_t         cert_size,
  size_t         *usage
  )
{
  return X509GetKeyUsage (cert, cert_size, usage);
}

bool
libspdm_x509_get_extended_key_usage (
  const uint8_t  *cert,
  size_t         cert_size,
  uint8_t        *usage,
  size_t         *usage_size
  )
{
  return X509GetExtendedKeyUsage (cert, cert_size, usage, usage_size);
}

bool
libspdm_x509_verify_cert (
  const uint8_t  *cert,
  size_t         cert_size,
  const uint8_t  *ca_cert,
  size_t         ca_cert_size
  )
{
  return X509VerifyCert (cert, cert_size, ca_cert, ca_cert_size);
}

bool
libspdm_x509_verify_cert_chain (
  const uint8_t  *root_cert,
  size_t         root_cert_length,
  const uint8_t  *cert_chain,
  size_t         cert_chain_length
  )
{
  return X509VerifyCertChain (root_cert, root_cert_length, cert_chain, cert_chain_length);
}

bool
libspdm_x509_get_cert_from_cert_chain (
  const uint8_t  *cert_chain,
  size_t         cert_chain_length,
  const int32_t  cert_index,
  const uint8_t  **cert,
  size_t         *cert_length
  )
{
  return X509GetCertFromCertChain (
           cert_chain,
           cert_chain_length,
           cert_index,
           cert,
           cert_length
           );
}

bool
libspdm_x509_construct_certificate (
  const uint8_t  *cert,
  size_t         cert_size,
  uint8_t        **single_x509_cert
  )
{
  return X509ConstructCertificate (cert, cert_size, single_x509_cert);
}

bool
libspdm_x509_get_extended_basic_constraints (
  const uint8_t  *cert,
  size_t         cert_size,
  uint8_t        *basic_constraints,
  size_t         *basic_constraints_size
  )
{
  return X509GetExtendedBasicConstraints (
           cert,
           cert_size,
           basic_constraints,
           basic_constraints_size
           );
}

void *
libspdm_ec_new_by_nid (
  size_t  nid
  )
{
  return EcNewByNid (nid);
}

void
libspdm_ec_free (
  void  *ec_context
  )
{
  EcFree (ec_context);
}

bool
libspdm_ec_generate_key (
  void     *ec_context,
  uint8_t  *public_data,
  size_t   *public_size
  )
{
  return EcGenerateKey (ec_context, public_data, public_size);
}

bool
libspdm_ec_compute_key (
  void           *ec_context,
  const uint8_t  *peer_public,
  size_t         peer_public_size,
  uint8_t        *key,
  size_t         *key_size
  )
{
  return EcDhComputeKey (ec_context, peer_public, peer_public_size, NULL, key, key_size);
}

bool
libspdm_ecdsa_sign (
  void           *ec_context,
  size_t         hash_nid,
  const uint8_t  *message_hash,
  size_t         hash_size,
  uint8_t        *signature,
  size_t         *sig_size
  )
{
  return EcDsaSign (
           ec_context,
           hash_nid,
           message_hash,
           hash_size,
           signature,
           sig_size
           );
}

bool
libspdm_ecdsa_verify (
  void           *ec_context,
  size_t         hash_nid,
  const uint8_t  *message_hash,
  size_t         hash_size,
  const uint8_t  *signature,
  size_t         sig_size
  )
{
  return EcDsaVerify (
           ec_context,
           hash_nid,
           message_hash,
           hash_size,
           signature,
           sig_size
           );
}

bool
libspdm_random_bytes (
  uint8_t  *output,
  size_t   size
  )
{
  return RandomBytes (output, size);
}

bool
libspdm_hkdf_sha256_extract_and_expand (
  const uint8_t  *key,
  size_t         key_size,
  const uint8_t  *salt,
  size_t         salt_size,
  const uint8_t  *info,
  size_t         info_size,
  uint8_t        *out,
  size_t         out_size
  )
{
  return HkdfSha256ExtractAndExpand (
           key,
           key_size,
           salt,
           salt_size,
           info,
           info_size,
           out,
           out_size
           );
}

bool
libspdm_hkdf_sha256_extract (
  const uint8_t  *key,
  size_t         key_size,
  const uint8_t  *salt,
  size_t         salt_size,
  uint8_t        *prk_out,
  size_t         prk_out_size
  )
{
  return HkdfSha256Extract (
           key,
           key_size,
           salt,
           salt_size,
           prk_out,
           prk_out_size
           );
}

bool
libspdm_hkdf_sha256_expand (
  const uint8_t  *prk,
  size_t         prk_size,
  const uint8_t  *info,
  size_t         info_size,
  uint8_t        *out,
  size_t         out_size
  )
{
  return HkdfSha256Expand (
           prk,
           prk_size,
           info,
           info_size,
           out,
           out_size
           );
}

bool
libspdm_hkdf_sha384_extract_and_expand (
  const uint8_t  *key,
  size_t         key_size,
  const uint8_t  *salt,
  size_t         salt_size,
  const uint8_t  *info,
  size_t         info_size,
  uint8_t        *out,
  size_t         out_size
  )
{
  return HkdfSha384ExtractAndExpand (
           key,
           key_size,
           salt,
           salt_size,
           info,
           info_size,
           out,
           out_size
           );
}

bool
libspdm_hkdf_sha384_extract (
  const uint8_t  *key,
  size_t         key_size,
  const uint8_t  *salt,
  size_t         salt_size,
  uint8_t        *prk_out,
  size_t         prk_out_size
  )
{
  return HkdfSha384Extract (
           key,
           key_size,
           salt,
           salt_size,
           prk_out,
           prk_out_size
           );
}

bool
libspdm_hkdf_sha384_expand (
  const uint8_t  *prk,
  size_t         prk_size,
  const uint8_t  *info,
  size_t         info_size,
  uint8_t        *out,
  size_t         out_size
  )
{
  return HkdfSha384Expand (
           prk,
           prk_size,
           info,
           info_size,
           out,
           out_size
           );
}