-
Notifications
You must be signed in to change notification settings - Fork 107
/
spdm_crypt_lib.h
1141 lines (1063 loc) · 49 KB
/
spdm_crypt_lib.h
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
/**
* Copyright Notice:
* Copyright 2021-2024 DMTF. All rights reserved.
* License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
**/
#ifndef SPDM_CRYPT_LIB_H
#define SPDM_CRYPT_LIB_H
#ifdef __cplusplus
extern "C" {
#endif
#include "internal/libspdm_lib_config.h"
#include "hal/base.h"
#include "industry_standard/spdm.h"
#if (LIBSPDM_FFDHE_4096_SUPPORT)
#define LIBSPDM_MAX_DHE_KEY_SIZE 512
#elif (LIBSPDM_FFDHE_3072_SUPPORT)
#define LIBSPDM_MAX_DHE_KEY_SIZE 384
#elif (LIBSPDM_FFDHE_2048_SUPPORT)
#define LIBSPDM_MAX_DHE_KEY_SIZE 256
#elif (LIBSPDM_ECDHE_P521_SUPPORT)
#define LIBSPDM_MAX_DHE_KEY_SIZE (66 * 2)
#elif (LIBSPDM_ECDHE_P384_SUPPORT)
#define LIBSPDM_MAX_DHE_KEY_SIZE (48 * 2)
#elif ((LIBSPDM_ECDHE_P256_SUPPORT) || (LIBSPDM_SM2_KEY_EXCHANGE_P256_SUPPORT))
#define LIBSPDM_MAX_DHE_KEY_SIZE (32 * 2)
#else
/* set 1 to pass build only */
#define LIBSPDM_MAX_DHE_KEY_SIZE 1
#endif /* LIBSPDM_MAX_DHE_KEY_SIZE */
#if ((LIBSPDM_RSA_SSA_4096_SUPPORT) || (LIBSPDM_RSA_PSS_4096_SUPPORT))
#define LIBSPDM_MAX_ASYM_KEY_SIZE 512
#define LIBSPDM_MAX_ASYM_SIG_SIZE 512
#elif ((LIBSPDM_RSA_SSA_3072_SUPPORT) || (LIBSPDM_RSA_PSS_3072_SUPPORT))
#define LIBSPDM_MAX_ASYM_KEY_SIZE 384
#define LIBSPDM_MAX_ASYM_SIG_SIZE 384
#elif ((LIBSPDM_RSA_SSA_2048_SUPPORT) || (LIBSPDM_RSA_PSS_2048_SUPPORT))
#define LIBSPDM_MAX_ASYM_KEY_SIZE 256
#define LIBSPDM_MAX_ASYM_SIG_SIZE 256
#elif (LIBSPDM_ECDSA_P521_SUPPORT)
#define LIBSPDM_MAX_ASYM_KEY_SIZE (66 * 2)
#define LIBSPDM_MAX_ASYM_SIG_SIZE (66 * 2)
#elif (LIBSPDM_EDDSA_ED448_SUPPORT)
#define LIBSPDM_MAX_ASYM_KEY_SIZE (57 * 2)
#define LIBSPDM_MAX_ASYM_SIG_SIZE (57 * 2)
#elif (LIBSPDM_ECDSA_P384_SUPPORT)
#define LIBSPDM_MAX_ASYM_KEY_SIZE (48 * 2)
#define LIBSPDM_MAX_ASYM_SIG_SIZE (48 * 2)
#elif ((LIBSPDM_ECDSA_P256_SUPPORT) || (LIBSPDM_SM2_DSA_P256_SUPPORT) || \
(LIBSPDM_EDDSA_ED25519_SUPPORT))
#define LIBSPDM_MAX_ASYM_KEY_SIZE (32 * 2)
#define LIBSPDM_MAX_ASYM_SIG_SIZE (32 * 2)
#else
/* set 1 to pass build only */
#define LIBSPDM_MAX_ASYM_KEY_SIZE 1
#define LIBSPDM_MAX_ASYM_SIG_SIZE 1
#endif /* LIBSPDM_MAX_ASYM_KEY_SIZE */
#if ((LIBSPDM_SHA512_SUPPORT) || (LIBSPDM_SHA3_512_SUPPORT))
#define LIBSPDM_MAX_HASH_SIZE 64
#elif ((LIBSPDM_SHA384_SUPPORT) || (LIBSPDM_SHA3_384_SUPPORT))
#define LIBSPDM_MAX_HASH_SIZE 48
#elif ((LIBSPDM_SHA256_SUPPORT) || (LIBSPDM_SHA3_256_SUPPORT) || (LIBSPDM_SM3_256_SUPPORT))
#define LIBSPDM_MAX_HASH_SIZE 32
#endif /* LIBSPDM_MAX_HASH_SIZE */
#if ((LIBSPDM_AEAD_AES_256_GCM_SUPPORT) || (LIBSPDM_AEAD_CHACHA20_POLY1305_SUPPORT))
#define LIBSPDM_MAX_AEAD_KEY_SIZE 32
#define LIBSPDM_MAX_AEAD_IV_SIZE 12
#define LIBSPDM_MAX_AEAD_TAG_SIZE 16
#elif ((LIBSPDM_AEAD_AES_128_GCM_SUPPORT) || (LIBSPDM_AEAD_SM4_128_GCM_SUPPORT))
#define LIBSPDM_MAX_AEAD_KEY_SIZE 16
#define LIBSPDM_MAX_AEAD_IV_SIZE 12
#define LIBSPDM_MAX_AEAD_TAG_SIZE 16
#else
/* set 1 or 8 to pass build only */
#define LIBSPDM_MAX_AEAD_KEY_SIZE 1
#define LIBSPDM_MAX_AEAD_IV_SIZE 8
#define LIBSPDM_MAX_AEAD_TAG_SIZE 1
#endif /* LIBSPDM_MAX_AEAD_KEY_SIZE */
/**
* This function returns the SPDM hash algorithm size.
*
* @param base_hash_algo SPDM base_hash_algo
*
* @return SPDM hash algorithm size.
**/
uint32_t libspdm_get_hash_size(uint32_t base_hash_algo);
/**
* Return cipher ID, based upon the negotiated hash algorithm.
*
* @param base_hash_algo SPDM base_hash_algo
*
* @return hash cipher ID
**/
size_t libspdm_get_hash_nid(uint32_t base_hash_algo);
/**
* Allocates and initializes one HASH_CTX context for subsequent hash use.
*
* @param base_hash_algo SPDM base_hash_algo
*
* @return Pointer to the HASH_CTX context that has been initialized.
* If the allocations fails, libspdm_hash_new() returns NULL.
**/
void *libspdm_hash_new(uint32_t base_hash_algo);
/**
* Release the specified HASH_CTX context.
*
* @param base_hash_algo SPDM base_hash_algo
* @param hash_context Pointer to the HASH_CTX context to be released.
**/
void libspdm_hash_free(uint32_t base_hash_algo, void *hash_context);
/**
* Initializes user-supplied memory pointed by hash_context as hash context for
* subsequent use.
*
* @param base_hash_algo SPDM base_hash_algo
* @param hash_context Pointer to hash context being initialized.
*
* @retval true Hash context initialization succeeded.
* @retval false Hash context initialization failed.
**/
bool libspdm_hash_init(uint32_t base_hash_algo, void *hash_context);
/**
* Makes a copy of an existing hash context.
*
* If hash_ctx is NULL, then return false.
* If new_hash_ctx is NULL, then return false.
*
* @param[in] hash_ctx Pointer to hash context being copied.
* @param[out] new_hash_ctx Pointer to new hash context.
*
* @retval true Hash context copy succeeded.
* @retval false Hash context copy failed.
**/
bool libspdm_hash_duplicate(uint32_t base_hash_algo, const void *hash_ctx, void *new_hash_ctx);
/**
* Digests the input data and updates hash context.
*
* This function performs hash digest on a data buffer of the specified size.
* It can be called multiple times to compute the digest of long or discontinuous data streams.
* Hash context should be already correctly initialized by hash_init(), and should not be finalized
* by hash_final(). Behavior with invalid context is undefined.
*
* If hash_context is NULL, then return false.
*
* @param[in, out] hash_context Pointer to the MD context.
* @param[in] data Pointer to the buffer containing the data to be hashed.
* @param[in] data_size Size of data buffer in bytes.
*
* @retval true Hash data digest succeeded.
* @retval false Hash data digest failed.
**/
bool libspdm_hash_update(uint32_t base_hash_algo, void *hash_context,
const void *data, size_t data_size);
/**
* Completes computation of the hash digest value.
*
* This function completes hash computation and retrieves the digest value into
* the specified memory. After this function has been called, the hash context cannot
* be used again.
* hash context should be already correctly initialized by hash_init(), and should not be
* finalized by hash_final(). Behavior with invalid hash context is undefined.
*
* If hash_context is NULL, then return false.
* If hash_value is NULL, then return false.
*
* @param[in, out] hash_context Pointer to the hash context.
* @param[out] hash_value Pointer to a buffer that receives the hash digest value.
*
* @retval true Hash digest computation succeeded.
* @retval false Hash digest computation failed.
**/
bool libspdm_hash_final(uint32_t base_hash_algo, void *hash_context, uint8_t *hash_value);
/**
* Allocates and initializes one HMAC context for subsequent use.
*
* @param base_hash_algo SPDM base_hash_algo
*
* @return Pointer to the HMAC context that has been initialized.
* If the allocations fails, libspdm_hash_new() returns NULL.
**/
void *libspdm_hmac_new(uint32_t base_hash_algo);
/**
* Release the specified HMAC context.
*
* @param base_hash_algo SPDM base_hash_algo
* @param hmac_ctx Pointer to the HMAC context to be released.
**/
void libspdm_hmac_free(uint32_t base_hash_algo, void *hmac_ctx);
/**
* Set user-supplied key for subsequent use. It must be done before any
* calling to hmac_update().
*
* If hmac_ctx is NULL, then return false.
*
* @param[out] hmac_ctx Pointer to HMAC context.
* @param[in] key Pointer to the user-supplied key.
* @param[in] key_size Key size in bytes.
*
* @retval true The key is set successfully.
* @retval false The key is set unsuccessfully.
*
**/
bool libspdm_hmac_init(uint32_t base_hash_algo,
void *hmac_ctx,
const uint8_t *key,
size_t key_size);
/**
* Makes a copy of an existing HMAC context.
*
* If hmac_ctx is NULL, then return false.
* If new_hmac_ctx is NULL, then return false.
*
* @param[in] hmac_ctx Pointer to HMAC context being copied.
* @param[out] new_hmac_ctx Pointer to new HMAC context.
*
* @retval true HMAC context copy succeeded.
* @retval false HMAC context copy failed.
*
**/
bool libspdm_hmac_duplicate(uint32_t base_hash_algo, const void *hmac_ctx, void *new_hmac_ctx);
/**
* Digests the input data and updates HMAC context.
*
* This function performs HMAC digest on a data buffer of the specified size.
* It can be called multiple times to compute the digest of long or discontinuous data streams.
* HMAC context should be initialized by hmac_new(), and should not be finalized
* by hmac_final(). Behavior with invalid context is undefined.
*
* If hmac_ctx is NULL, then return false.
*
* @param[in, out] hmac_ctx Pointer to the HMAC context.
* @param[in] data Pointer to the buffer containing the data to be digested.
* @param[in] data_size Size of data buffer in bytes.
*
* @retval true HMAC data digest succeeded.
* @retval false HMAC data digest failed.
*
**/
bool libspdm_hmac_update(uint32_t base_hash_algo,
void *hmac_ctx, const void *data,
size_t data_size);
/**
* Completes computation of the HMAC digest value.
*
* This function completes HMAC hash computation and retrieves the digest value into
* the specified memory. After this function has been called, the HMAC context cannot
* be used again.
*
* If hmac_ctx is NULL, then return false.
* If hmac_value is NULL, then return false.
*
* @param[in, out] hmac_ctx Pointer to the HMAC context.
* @param[out] hmac_value Pointer to a buffer that receives the HMAC digest value.
*
* @retval true HMAC digest computation succeeded.
* @retval false HMAC digest computation failed.
*
**/
bool libspdm_hmac_final(uint32_t base_hash_algo, void *hmac_ctx, uint8_t *hmac_value);
/**
* Computes the hash of a input data buffer, based upon the negotiated hash algorithm.
*
* This function performs the hash of a given data buffer, and return the hash value.
*
* @param base_hash_algo SPDM base_hash_algo
* @param data Pointer to the buffer containing the data to be hashed.
* @param data_size Size of data buffer in bytes.
* @param hash_value Pointer to a buffer that receives the hash value.
*
* @retval true Hash computation succeeded.
* @retval false Hash computation failed.
**/
bool libspdm_hash_all(uint32_t base_hash_algo, const void *data,
size_t data_size, uint8_t *hash_value);
/**
* This function returns the SPDM measurement hash algorithm size.
*
* @param measurement_hash_algo SPDM measurement_hash_algo.
*
* @return SPDM measurement hash algorithm size.
* @return 0xFFFFFFFF for RAW_BIT_STREAM_ONLY.
**/
uint32_t libspdm_get_measurement_hash_size(uint32_t measurement_hash_algo);
/**
* Computes the HMAC of a input data buffer, based upon the negotiated HMAC algorithm.
*
* This function performs the HMAC of a given data buffer, and return the hash value.
*
* @param base_hash_algo SPDM base_hash_algo
* @param data Pointer to the buffer containing the data to be HMACed.
* @param data_size Size of data buffer in bytes.
* @param key Pointer to the user-supplied key.
* @param key_size Key size in bytes.
* @param hash_value Pointer to a buffer that receives the HMAC value.
*
* @retval true HMAC computation succeeded.
* @retval false HMAC computation failed.
**/
bool libspdm_hmac_all(uint32_t base_hash_algo, const void *data,
size_t data_size, const uint8_t *key,
size_t key_size, uint8_t *hmac_value);
/**
* Derive HMAC-based Extract key Derivation Function (HKDF) Extract, based upon the negotiated HKDF
* algorithm.
*
* @param ikm Pointer to the input key material.
* @param ikm_size Key size in bytes.
* @param salt Pointer to the salt value.
* @param salt_size Salt size in bytes.
* @param prk_out Pointer to buffer to receive hkdf value.
* @param prk_out_size Size of hkdf bytes to generate.
*
* @retval true Hkdf generated successfully.
* @retval false Hkdf generation failed.
**/
bool libspdm_hkdf_extract(uint32_t base_hash_algo, const uint8_t *ikm, size_t ikm_size,
const uint8_t *salt, size_t salt_size,
uint8_t *prk_out, size_t prk_out_size);
/**
* Derive HMAC-based Expand key Derivation Function (HKDF) Expand, based upon the negotiated HKDF
* algorithm.
*
* @param base_hash_algo SPDM base_hash_algo
* @param prk Pointer to the user-supplied key.
* @param prk_size Key size in bytes.
* @param info Pointer to the application specific info.
* @param info_size Info size in bytes.
* @param out Pointer to buffer to receive hkdf value.
* @param out_size Size of hkdf bytes to generate.
*
* @retval true Hkdf generated successfully.
* @retval false Hkdf generation failed.
**/
bool libspdm_hkdf_expand(uint32_t base_hash_algo, const uint8_t *prk,
size_t prk_size, const uint8_t *info,
size_t info_size, uint8_t *out, size_t out_size);
/**
* This function returns the SPDM asymmetric algorithm size.
*
* @param base_asym_algo SPDM base_hash_algo
*
* @return SPDM asymmetric algorithm size.
**/
uint32_t libspdm_get_asym_signature_size(uint32_t base_asym_algo);
/**
* Retrieve the asymmetric public key from one DER-encoded X509 certificate,
* based upon negotiated asymmetric algorithm.
*
* @param base_asym_algo SPDM base_asym_algo
* @param cert Pointer to the DER-encoded X509 certificate.
* @param cert_size Size of the X509 certificate in bytes.
* @param context Pointer to newly generated asymmetric context which contain the retrieved
* public key component. Use libspdm_asym_free() function to free the
* resource.
*
* @retval true Public key was retrieved successfully.
* @retval false Fail to retrieve public key from X509 certificate.
**/
bool libspdm_asym_get_public_key_from_x509(uint32_t base_asym_algo,
const uint8_t *cert,
size_t cert_size,
void **context);
/**
* Retrieve the asymmetric public key from the DER-encoded public key data,
* based upon negotiated asymmetric algorithm.
*
* @param base_asym_algo SPDM base_asym_algo
* @param der_data Pointer to the DER-encoded public key data.
* @param der_size Size of the DER-encoded public key data in bytes.
* @param context Pointer to newly generated asymmetric context which contain the
* retrieved public key component.
* Use libspdm_asym_free() function to free the resource.
*
* @retval true Private key was retrieved successfully.
* @retval false Invalid DER key data.
**/
bool libspdm_asym_get_public_key_from_der(uint32_t base_asym_algo,
const uint8_t *der_data,
size_t der_size,
void **context);
/**
* Release the specified asymmetric context, based upon negotiated asymmetric algorithm.
*
* @param base_asym_algo SPDM base_asym_algo
* @param context Pointer to the asymmetric context to be released.
**/
void libspdm_asym_free(uint32_t base_asym_algo, void *context);
/**
* Copies signature buffers from src to dst swapping endianness in the process.
*/
void libspdm_copy_signature_swap_endian(
uint32_t base_asym_algo,
uint8_t* dst,
size_t dst_size,
const uint8_t* src,
size_t src_size);
/**
* Verifies the asymmetric signature, based upon negotiated asymmetric algorithm.
*
* @param base_asym_algo SPDM base_asym_algo
* @param base_hash_algo SPDM base_hash_algo
* @param context Pointer to asymmetric context for signature verification.
* @param message Pointer to octet message to be checked (before hash).
* @param message_size Size of the message in bytes.
* @param signature Pointer to asymmetric signature to be verified.
* @param sig_size Size of signature in bytes.
* @param endian Endian to be tried. If both endians are selected,
* the one actually used successfully is returned.
*
* @retval true Valid asymmetric signature.
* @retval false Invalid asymmetric signature or invalid asymmetric context.
**/
bool libspdm_asym_verify(
spdm_version_number_t spdm_version, uint8_t op_code,
uint32_t base_asym_algo, uint32_t base_hash_algo,
void *context,
const uint8_t *message, size_t message_size,
const uint8_t *signature, size_t sig_size);
bool libspdm_asym_verify_ex(
spdm_version_number_t spdm_version, uint8_t op_code,
uint32_t base_asym_algo, uint32_t base_hash_algo,
void* context,
const uint8_t* message, size_t message_size,
const uint8_t* signature, size_t sig_size,
uint8_t *endian);
/**
* Verifies the asymmetric signature, based upon negotiated asymmetric algorithm.
*
* @param base_asym_algo SPDM base_asym_algo
* @param base_hash_algo SPDM base_hash_algo
* @param context Pointer to asymmetric context for signature verification.
* @param message_hash Pointer to octet message hash to be checked (after hash).
* @param hash_size Size of the hash in bytes.
* @param signature Pointer to asymmetric signature to be verified.
* @param sig_size Size of signature in bytes.
* @param endian Endian to be tried. If both endians are selected,
* the one actually used successfully is returned.
*
* @retval true Valid asymmetric signature.
* @retval false Invalid asymmetric signature or invalid asymmetric context.
**/
bool libspdm_asym_verify_hash(
spdm_version_number_t spdm_version, uint8_t op_code,
uint32_t base_asym_algo, uint32_t base_hash_algo, void *context,
const uint8_t *message_hash, size_t hash_size,
const uint8_t *signature, size_t sig_size);
bool libspdm_asym_verify_hash_ex(
spdm_version_number_t spdm_version, uint8_t op_code,
uint32_t base_asym_algo, uint32_t base_hash_algo, void* context,
const uint8_t* message_hash, size_t hash_size,
const uint8_t* signature, size_t sig_size,
uint8_t *endian);
/**
* Carries out the signature generation.
*
* If the signature buffer is too small to hold the contents of signature, false
* is returned and sig_size is set to the required buffer size to obtain the signature.
*
* @param base_asym_algo SPDM base_asym_algo
* @param base_hash_algo SPDM base_hash_algo
* @param context Pointer to asymmetric context for signature generation.
* @param message Pointer to octet message to be signed (before hash).
* @param message_size Size of the message in bytes.
* @param signature Pointer to buffer to receive signature.
* @param sig_size On input, the size of signature buffer in bytes.
* On output, the size of data returned in signature buffer in bytes.
*
* @retval true Signature successfully generated.
* @retval false Signature generation failed.
* @retval false sig_size is too small.
**/
bool libspdm_asym_sign(
spdm_version_number_t spdm_version, uint8_t op_code,
uint32_t base_asym_algo, uint32_t base_hash_algo,
void *context, const uint8_t *message,
size_t message_size, uint8_t *signature,
size_t *sig_size);
/**
* Carries out the signature generation.
*
* If the signature buffer is too small to hold the contents of signature, false
* is returned and sig_size is set to the required buffer size to obtain the signature.
*
* @param base_asym_algo SPDM base_asym_algo
* @param base_hash_algo SPDM base_hash_algo
* @param context Pointer to asymmetric context for signature generation.
* @param message_hash Pointer to octet message hash to be signed (after hash).
* @param hash_size Size of the hash in bytes.
* @param signature Pointer to buffer to receive signature.
* @param sig_size On input, the size of signature buffer in bytes.
* On output, the size of data returned in signature buffer in bytes.
*
* @retval true Signature successfully generated.
* @retval false Signature generation failed.
* @retval false sig_size is too small.
**/
bool libspdm_asym_sign_hash(
spdm_version_number_t spdm_version, uint8_t op_code,
uint32_t base_asym_algo, uint32_t base_hash_algo,
void *context, const uint8_t *message_hash,
size_t hash_size, uint8_t *signature,
size_t *sig_size);
/**
* This function returns the SPDM requester asymmetric algorithm size.
*
* @param req_base_asym_alg SPDM req_base_asym_alg
*
* @return SPDM requester asymmetric algorithm size.
**/
uint32_t libspdm_get_req_asym_signature_size(uint16_t req_base_asym_alg);
/**
* Retrieve the asymmetric public key from one DER-encoded X509 certificate,
* based upon negotiated requester asymmetric algorithm.
*
* @param req_base_asym_alg SPDM req_base_asym_alg
* @param cert Pointer to the DER-encoded X509 certificate.
* @param cert_size Size of the X509 certificate in bytes.
* @param context Pointer to newly generated asymmetric context which contain the
* retrieved public key component. Use libspdm_asym_free() function to
* free the resource.
*
* @retval true Public key was retrieved successfully.
* @retval false Fail to retrieve public key from X509 certificate.
**/
bool libspdm_req_asym_get_public_key_from_x509(uint16_t req_base_asym_alg,
const uint8_t *cert,
size_t cert_size,
void **context);
/**
* Retrieve the asymmetric public key from the DER-encoded public key data,
* based upon negotiated requester asymmetric algorithm.
*
* @param req_base_asym_alg SPDM req_base_asym_alg
* @param der_data Pointer to the DER-encoded public key data.
* @param der_size Size of the DER-encoded public key data in bytes.
* @param context Pointer to newly generated asymmetric context which contain the
* retrieved public key component.
* Use libspdm_req_asym_free() function to free the resource.
*
* @retval true Public key was retrieved successfully.
* @retval false Invalid DER key data.
**/
bool libspdm_req_asym_get_public_key_from_der(uint16_t req_base_asym_alg,
const uint8_t *der_data,
size_t der_size,
void **context);
/**
* Release the specified asymmetric context, based upon negotiated requester asymmetric algorithm.
*
* @param req_base_asym_alg SPDM req_base_asym_alg
* @param context Pointer to the asymmetric context to be released.
**/
void libspdm_req_asym_free(uint16_t req_base_asym_alg, void *context);
/**
* Verifies the asymmetric signature, based upon negotiated requester asymmetric algorithm.
*
* @param req_base_asym_alg SPDM req_base_asym_alg
* @param base_hash_algo SPDM base_hash_algo
* @param context Pointer to asymmetric context for signature verification.
* @param message Pointer to octet message to be checked (before hash).
* @param message_size Size of the message in bytes.
* @param signature Pointer to asymmetric signature to be verified.
* @param sig_size Size of signature in bytes.
* @param endian Endian to be tried. If both endians are selected,
* the one actually used successfully is returned.
*
* @retval true Valid asymmetric signature.
* @retval false Invalid asymmetric signature or invalid asymmetric context.
**/
bool libspdm_req_asym_verify(
spdm_version_number_t spdm_version, uint8_t op_code,
uint16_t req_base_asym_alg,
uint32_t base_hash_algo, void *context,
const uint8_t *message, size_t message_size,
const uint8_t *signature, size_t sig_size);
bool libspdm_req_asym_verify_ex(
spdm_version_number_t spdm_version, uint8_t op_code,
uint16_t req_base_asym_alg,
uint32_t base_hash_algo, void* context,
const uint8_t* message, size_t message_size,
const uint8_t* signature, size_t sig_size, uint8_t *endian);
/**
* Verifies the asymmetric signature, based upon negotiated requester asymmetric algorithm.
*
* @param req_base_asym_alg SPDM req_base_asym_alg
* @param base_hash_algo SPDM base_hash_algo
* @param context Pointer to asymmetric context for signature verification.
* @param message_hash Pointer to octet message hash to be checked (after hash).
* @param hash_size Size of the hash in bytes.
* @param signature Pointer to asymmetric signature to be verified.
* @param sig_size Size of signature in bytes.
* @param endian Endian to be tried. If both endians are selected,
* the one actually used successfully is returned.
*
* @retval true Valid asymmetric signature.
* @retval false Invalid asymmetric signature or invalid asymmetric context.
**/
bool libspdm_req_asym_verify_hash(
spdm_version_number_t spdm_version, uint8_t op_code,
uint16_t req_base_asym_alg,
uint32_t base_hash_algo, void *context,
const uint8_t *message_hash, size_t hash_size,
const uint8_t *signature, size_t sig_size);
bool libspdm_req_asym_verify_hash_ex(
spdm_version_number_t spdm_version, uint8_t op_code,
uint16_t req_base_asym_alg,
uint32_t base_hash_algo, void* context,
const uint8_t* message_hash, size_t hash_size,
const uint8_t* signature, size_t sig_size, uint8_t *endian);
/**
* Carries out the signature generation.
*
* If the signature buffer is too small to hold the contents of signature, false
* is returned and sig_size is set to the required buffer size to obtain the signature.
*
* @param req_base_asym_alg SPDM req_base_asym_alg
* @param base_hash_algo SPDM base_hash_algo
* @param context Pointer to asymmetric context for signature generation.
* @param message Pointer to octet message to be signed (before hash).
* @param message_size Size of the message in bytes.
* @param signature Pointer to buffer to receive signature.
* @param sig_size On input, the size of signature buffer in bytes.
* On output, the size of data returned in signature buffer in bytes.
*
* @retval true Signature successfully generated.
* @retval false Signature generation failed.
* @retval false sig_size is too small.
**/
bool libspdm_req_asym_sign(
spdm_version_number_t spdm_version, uint8_t op_code,
uint16_t req_base_asym_alg,
uint32_t base_hash_algo, void *context,
const uint8_t *message, size_t message_size,
uint8_t *signature, size_t *sig_size);
/**
* Carries out the signature generation.
*
* If the signature buffer is too small to hold the contents of signature, false
* is returned and sig_size is set to the required buffer size to obtain the signature.
*
* @param req_base_asym_alg SPDM req_base_asym_alg
* @param base_hash_algo SPDM base_hash_algo
* @param context Pointer to asymmetric context for signature generation.
* @param message_hash Pointer to octet message hash to be signed (after hash).
* @param hash_size Size of the hash in bytes.
* @param signature Pointer to buffer to receive signature.
* @param sig_size On input, the size of signature buffer in bytes.
* On output, the size of data returned in signature buffer in bytes.
*
* @retval true Signature successfully generated.
* @retval false Signature generation failed.
* @retval false sig_size is too small.
**/
bool libspdm_req_asym_sign_hash(
spdm_version_number_t spdm_version, uint8_t op_code,
uint16_t req_base_asym_alg,
uint32_t base_hash_algo, void *context,
const uint8_t *message_hash, size_t hash_size,
uint8_t *signature, size_t *sig_size);
/**
* This function returns the SPDM DHE algorithm key size.
*
* @param dhe_named_group SPDM dhe_named_group
*
* @return SPDM DHE algorithm key size.
**/
uint32_t libspdm_get_dhe_pub_key_size(uint16_t dhe_named_group);
/**
* Allocates and Initializes one Diffie-Hellman Ephemeral (DHE) context for subsequent use,
* based upon negotiated DHE algorithm.
*
* @param dhe_named_group SPDM dhe_named_group
* @param is_initiator If the caller is initiator.
*
* @return Pointer to the Diffie-Hellman context that has been initialized.
**/
void *libspdm_dhe_new(spdm_version_number_t spdm_version,
uint16_t dhe_named_group, bool is_initiator);
/**
* Release the specified DHE context, based upon negotiated DHE algorithm.
*
* @param dhe_named_group SPDM dhe_named_group
* @param context Pointer to the DHE context to be released.
**/
void libspdm_dhe_free(uint16_t dhe_named_group, void *context);
/**
* Generates DHE public key, based upon negotiated DHE algorithm.
*
* This function generates random secret exponent, and computes the public key, which is
* returned via parameter public_key and public_key_size. DH context is updated accordingly.
* If the public_key buffer is too small to hold the public key, false is returned and
* public_key_size is set to the required buffer size to obtain the public key.
*
* @param dhe_named_group SPDM dhe_named_group
* @param context Pointer to the DHE context.
* @param public_key Pointer to the buffer to receive generated public key.
* @param public_key_size On input, the size of public_key buffer in bytes.
* On output, the size of data returned in public_key buffer in bytes.
*
* @retval true DHE public key generation succeeded.
* @retval false DHE public key generation failed.
* @retval false public_key_size is not large enough.
**/
bool libspdm_dhe_generate_key(uint16_t dhe_named_group, void *context,
uint8_t *public_key,
size_t *public_key_size);
/**
* Computes exchanged common key, based upon negotiated DHE algorithm.
*
* 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 dhe_named_group SPDM dhe_named_group
* @param context Pointer to the DHE context.
* @param peer_public_key Pointer to the peer's public key.
* @param peer_public_key_size Size of peer's public key in bytes.
* @param key Pointer to the buffer to receive generated key.
* @param key_size On input, the size of key buffer in bytes.
* On output, the size of data returned in key buffer in bytes.
*
* @retval true DHE exchanged key generation succeeded.
* @retval false DHE exchanged key generation failed.
* @retval false key_size is not large enough.
**/
bool libspdm_dhe_compute_key(uint16_t dhe_named_group, void *context,
const uint8_t *peer_public,
size_t peer_public_size, uint8_t *key,
size_t *key_size);
/**
* This function returns the SPDM AEAD algorithm key size.
*
* @param aead_cipher_suite SPDM aead_cipher_suite
*
* @return SPDM AEAD algorithm key size.
**/
uint32_t libspdm_get_aead_key_size(uint16_t aead_cipher_suite);
/**
* This function returns the SPDM AEAD algorithm iv size.
*
* @param aead_cipher_suite SPDM aead_cipher_suite
*
* @return SPDM AEAD algorithm iv size.
**/
uint32_t libspdm_get_aead_iv_size(uint16_t aead_cipher_suite);
/**
* This function returns the SPDM AEAD algorithm tag size.
*
* @param aead_cipher_suite SPDM aead_cipher_suite
*
* @return SPDM AEAD algorithm tag size.
**/
uint32_t libspdm_get_aead_tag_size(uint16_t aead_cipher_suite);
/**
* Performs AEAD authenticated encryption on a data buffer and additional authenticated data (AAD),
* based upon negotiated AEAD algorithm.
*
* @param aead_cipher_suite SPDM aead_cipher_suite
* @param key Pointer to the encryption key.
* @param key_size Size of the encryption key in bytes.
* @param iv Pointer to the IV value.
* @param iv_size Size of the IV value in bytes.
* @param a_data Pointer to the additional authenticated data (AAD).
* @param a_data_size Size of the additional authenticated data (AAD) in bytes.
* @param data_in Pointer to the input data buffer to be encrypted.
* @param data_in_size Size of the input data buffer in bytes.
* @param tag_out Pointer to a buffer that receives the authentication tag output.
* @param tag_size Size of the authentication tag in bytes.
* @param data_out Pointer to a buffer that receives the encryption output.
* @param data_out_size Size of the output data buffer in bytes.
*
* @retval true AEAD authenticated encryption succeeded.
* @retval false AEAD authenticated encryption failed.
**/
bool libspdm_aead_encryption(const spdm_version_number_t secured_message_version,
uint16_t aead_cipher_suite, 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);
/**
* Performs AEAD authenticated decryption on a data buffer and additional authenticated data (AAD),
* based upon negotiated AEAD algorithm.
*
* @param aead_cipher_suite SPDM aead_cipher_suite
* @param key Pointer to the encryption key.
* @param key_size Size of the encryption key in bytes.
* @param iv Pointer to the IV value.
* @param iv_size Size of the IV value in bytes.
* @param a_data Pointer to the additional authenticated data (AAD).
* @param a_data_size Size of the additional authenticated data (AAD) in bytes.
* @param data_in Pointer to the input data buffer to be decrypted.
* @param data_in_size Size of the input data buffer in bytes.
* @param tag Pointer to a buffer that contains the authentication tag.
* @param tag_size Size of the authentication tag in bytes.
* @param data_out Pointer to a buffer that receives the decryption output.
* @param data_out_size Size of the output data buffer in bytes.
*
* @retval true AEAD authenticated decryption succeeded.
* @retval false AEAD authenticated decryption failed.
**/
bool libspdm_aead_decryption(const spdm_version_number_t secured_message_version,
uint16_t aead_cipher_suite, 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);
/**
* Generates a random byte stream of the specified size.
*
* @param spdm_context A pointer to the SPDM context.
* @param size Size of random bytes to generate.
* @param rand Pointer to buffer to receive random value.
*
* @retval true The random bytes were successfully generated.
* @retval false The random bytes were not successfully generated.
**/
bool libspdm_get_random_number(size_t size, uint8_t *rand);
#if LIBSPDM_CERT_PARSE_SUPPORT
/**
* Certificate Check for SPDM leaf cert.
*
* @param[in] cert Pointer to the DER-encoded certificate data.
* @param[in] cert_size The size of certificate data in bytes.
* @param[in] base_asym_algo SPDM base_asym_algo
* @param[in] base_hash_algo SPDM base_hash_algo
* @param[in] is_requester Is the function verifying a cert as a requester or responder.
* @param[in] is_device_cert_model If true, the local endpoint uses the DeviceCert model.
* If false, the local endpoint uses the AliasCert model.
*
* @retval true Success.
* @retval false Certificate is not valid.
**/
bool libspdm_x509_certificate_check(const uint8_t *cert, size_t cert_size,
uint32_t base_asym_algo, uint32_t base_hash_algo,
bool is_requester, bool is_device_cert_model);
/**
* Certificate Check for SPDM leaf cert. It is used for SPDM 1.3.
*
* @param[in] cert Pointer to the DER-encoded certificate data.
* @param[in] cert_size The size of certificate data in bytes.
* @param[in] base_asym_algo SPDM base_asym_algo
* @param[in] base_hash_algo SPDM base_hash_algo
* @param[in] is_requester Is the function verifying a cert as a requester or responder.
* @param[in] cert_model One of the SPDM_CERTIFICATE_INFO_CERT_MODEL_* macros.
*
* @retval true Success.
* @retval false Certificate is not valid.
**/
bool libspdm_x509_certificate_check_ex(const uint8_t *cert, size_t cert_size,
uint32_t base_asym_algo, uint32_t base_hash_algo,
bool is_requester, uint8_t cert_model);
/**
* Certificate Check for SPDM leaf cert when set_cert. It is used for SPDM 1.2.
*
* This function differs from libspdm_x509_set_cert_certificate_check_ex in that in SPDM 1.2 the
* BasicConstraints CA field is optional while in SPDM 1.3 and later it is mandatory.
*
* @param[in] cert Pointer to the DER-encoded certificate data.
* @param[in] cert_size The size of certificate data in bytes.
* @param[in] base_asym_algo SPDM base_asym_algo
* @param[in] base_hash_algo SPDM base_hash_algo
* @param[in] is_requester Is the function verifying a cert as a requester or responder.
* @param[in] is_device_cert_model If true, the local endpoint uses the DeviceCert model.
* If false, the local endpoint uses the AliasCert model.
*
* @retval true Success.
* @retval false Certificate is not valid.
**/
bool libspdm_x509_set_cert_certificate_check(const uint8_t *cert, size_t cert_size,
uint32_t base_asym_algo, uint32_t base_hash_algo,
bool is_requester, bool is_device_cert_model);
/**
* Certificate Check for SPDM leaf cert when set_cert. It is used for SPDM 1.3 and later.
*
* This function differs from libspdm_x509_set_cert_certificate_check in that in SPDM 1.2 the
* BasicConstraints CA field is optional while in SPDM 1.3 and later it is mandatory.
*
* @param[in] cert Pointer to the DER-encoded certificate data.
* @param[in] cert_size The size of certificate data in bytes.
* @param[in] base_asym_algo SPDM base_asym_algo
* @param[in] base_hash_algo SPDM base_hash_algo
* @param[in] is_requester Is the function verifying a cert as a requester or responder.
* @param[in] cert_model One of the SPDM_CERTIFICATE_INFO_CERT_MODEL_* macros.
*
* @retval true Success.
* @retval false Certificate is not valid.
**/
bool libspdm_x509_set_cert_certificate_check_ex(const uint8_t *cert, size_t cert_size,
uint32_t base_asym_algo, uint32_t base_hash_algo,
bool is_requester, uint8_t cert_model);
/**
* Return certificate is root cert or not.
* Certificate is considered as a root certificate if the subjectname equal issuername.
*
* @param[in] cert Pointer to the DER-encoded certificate data.
* @param[in] cert_size The size of certificate data in bytes.
*
* @retval true Certificate is self-signed.
* @retval false Certificate is not self-signed.
**/
bool libspdm_is_root_certificate(const uint8_t *cert, size_t cert_size);
/**
* Retrieve the SubjectAltName from SubjectAltName Bytes.
*
* @param[in] buffer Pointer to subjectAltName oct bytes.
* @param[in] len Size of buffer in bytes.
* @param[out] name_buffer Buffer to contain the retrieved certificate
* SubjectAltName. At most name_buffer_size bytes will be
* written. Maybe NULL in order to determine the size
* buffer needed.
* @param[in,out] name_buffer_size The size in bytes of the name buffer on input,
* and the size of buffer returned name on output.
* If name_buffer is NULL then the amount of space needed
* in buffer (including the final null) is returned.
* @param[out] oid OID of otherName
* @param[in,out] oid_size The buffersize for required OID
*
* @retval true Get the subjectAltName string successfully
* @retval failed Get the subjectAltName string failed
**/
bool libspdm_get_dmtf_subject_alt_name_from_bytes(
uint8_t *buffer, const size_t len, char *name_buffer,
size_t *name_buffer_size, uint8_t *oid,
size_t *oid_size);
/**
* Retrieve the SubjectAltName from one X.509 certificate.
*
* @param[in] cert Pointer to the DER-encoded X509 certificate.
* @param[in] cert_size Size of the X509 certificate in bytes.