LCOV - code coverage report
Current view: top level - library/spdm_crypt_lib - libspdm_crypt_aead.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 45.2 % 62 28
Test Date: 2025-06-29 08:09:00 Functions: 100.0 % 5 5

            Line data    Source code
       1              : /**
       2              :  *  Copyright Notice:
       3              :  *  Copyright 2021-2022 DMTF. All rights reserved.
       4              :  *  License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
       5              :  **/
       6              : 
       7              : #include "internal/libspdm_crypt_lib.h"
       8              : 
       9          716 : uint32_t libspdm_get_aead_key_size(uint16_t aead_cipher_suite)
      10              : {
      11          716 :     switch (aead_cipher_suite) {
      12            0 :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_128_GCM:
      13              : #if LIBSPDM_AEAD_AES_128_GCM_SUPPORT
      14            0 :         return 16;
      15              : #else
      16              :         return 0;
      17              : #endif
      18          529 :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_256_GCM:
      19              : #if LIBSPDM_AEAD_AES_256_GCM_SUPPORT
      20          529 :         return 32;
      21              : #else
      22              :         return 0;
      23              : #endif
      24            1 :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_CHACHA20_POLY1305:
      25              : #if LIBSPDM_AEAD_CHACHA20_POLY1305_SUPPORT
      26              :         return 32;
      27              : #else
      28            1 :         return 0;
      29              : #endif
      30            0 :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AEAD_SM4_GCM:
      31              : #if LIBSPDM_AEAD_SM4_128_GCM_SUPPORT
      32              :         return 16;
      33              : #else
      34            0 :         return 0;
      35              : #endif
      36          186 :     default:
      37          186 :         return 0;
      38              :     }
      39              : }
      40              : 
      41          688 : uint32_t libspdm_get_aead_iv_size(uint16_t aead_cipher_suite)
      42              : {
      43          688 :     switch (aead_cipher_suite) {
      44            0 :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_128_GCM:
      45              : #if LIBSPDM_AEAD_AES_128_GCM_SUPPORT
      46            0 :         return 12;
      47              : #else
      48              :         return 0;
      49              : #endif
      50          503 :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_256_GCM:
      51              : #if LIBSPDM_AEAD_AES_256_GCM_SUPPORT
      52          503 :         return 12;
      53              : #else
      54              :         return 0;
      55              : #endif
      56            0 :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_CHACHA20_POLY1305:
      57              : #if LIBSPDM_AEAD_CHACHA20_POLY1305_SUPPORT
      58              :         return 12;
      59              : #else
      60            0 :         return 0;
      61              : #endif
      62            0 :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AEAD_SM4_GCM:
      63              : #if LIBSPDM_AEAD_SM4_128_GCM_SUPPORT
      64              :         return 12;
      65              : #else
      66            0 :         return 0;
      67              : #endif
      68          185 :     default:
      69          185 :         return 0;
      70              :     }
      71              : }
      72              : 
      73          688 : uint32_t libspdm_get_aead_tag_size(uint16_t aead_cipher_suite)
      74              : {
      75          688 :     switch (aead_cipher_suite) {
      76            0 :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_128_GCM:
      77              : #if LIBSPDM_AEAD_AES_128_GCM_SUPPORT
      78            0 :         return 16;
      79              : #else
      80              :         return 0;
      81              : #endif
      82          503 :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_256_GCM:
      83              : #if LIBSPDM_AEAD_AES_256_GCM_SUPPORT
      84          503 :         return 16;
      85              : #else
      86              :         return 0;
      87              : #endif
      88            0 :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_CHACHA20_POLY1305:
      89              : #if LIBSPDM_AEAD_CHACHA20_POLY1305_SUPPORT
      90              :         return 16;
      91              : #else
      92            0 :         return 0;
      93              : #endif
      94            0 :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AEAD_SM4_GCM:
      95              : #if LIBSPDM_AEAD_SM4_128_GCM_SUPPORT
      96              :         return 16;
      97              : #else
      98            0 :         return 0;
      99              : #endif
     100          185 :     default:
     101          185 :         return 0;
     102              :     }
     103              : }
     104              : 
     105          466 : bool libspdm_aead_encryption(const spdm_version_number_t secured_message_version,
     106              :                              uint16_t aead_cipher_suite, const uint8_t *key,
     107              :                              size_t key_size, const uint8_t *iv,
     108              :                              size_t iv_size, const uint8_t *a_data,
     109              :                              size_t a_data_size, const uint8_t *data_in,
     110              :                              size_t data_in_size, uint8_t *tag_out,
     111              :                              size_t tag_size, uint8_t *data_out,
     112              :                              size_t *data_out_size)
     113              : {
     114          466 :     switch (aead_cipher_suite) {
     115          466 :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_128_GCM:
     116              :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_256_GCM:
     117              : #if LIBSPDM_AEAD_GCM_SUPPORT
     118              : #if !LIBSPDM_AEAD_AES_128_GCM_SUPPORT
     119              :         LIBSPDM_ASSERT(aead_cipher_suite != SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_128_GCM);
     120              : #endif
     121              : #if !LIBSPDM_AEAD_AES_256_GCM_SUPPORT
     122              :         LIBSPDM_ASSERT(aead_cipher_suite != SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_256_GCM);
     123              : #endif
     124          466 :         return libspdm_aead_aes_gcm_encrypt(key, key_size, iv, iv_size, a_data,
     125              :                                             a_data_size, data_in, data_in_size, tag_out,
     126              :                                             tag_size, data_out, data_out_size);
     127              : #else
     128              :         LIBSPDM_ASSERT(false);
     129              :         return false;
     130              : #endif
     131            0 :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_CHACHA20_POLY1305:
     132              : #if LIBSPDM_AEAD_CHACHA20_POLY1305_SUPPORT
     133              :         return libspdm_aead_chacha20_poly1305_encrypt(key, key_size, iv, iv_size, a_data,
     134              :                                                       a_data_size, data_in, data_in_size, tag_out,
     135              :                                                       tag_size, data_out, data_out_size);
     136              : #else
     137            0 :         LIBSPDM_ASSERT(false);
     138            0 :         return false;
     139              : #endif
     140            0 :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AEAD_SM4_GCM:
     141              : #if LIBSPDM_AEAD_SM4_SUPPORT
     142              :         return libspdm_aead_sm4_gcm_encrypt(key, key_size, iv, iv_size, a_data,
     143              :                                             a_data_size, data_in, data_in_size, tag_out,
     144              :                                             tag_size, data_out, data_out_size);
     145              : #else
     146            0 :         LIBSPDM_ASSERT(false);
     147            0 :         return false;
     148              : #endif
     149            0 :     default:
     150            0 :         LIBSPDM_ASSERT(false);
     151            0 :         return false;
     152              :     }
     153              : }
     154              : 
     155          327 : bool libspdm_aead_decryption(const spdm_version_number_t secured_message_version,
     156              :                              uint16_t aead_cipher_suite, const uint8_t *key,
     157              :                              size_t key_size, const uint8_t *iv,
     158              :                              size_t iv_size, const uint8_t *a_data,
     159              :                              size_t a_data_size, const uint8_t *data_in,
     160              :                              size_t data_in_size, const uint8_t *tag,
     161              :                              size_t tag_size, uint8_t *data_out,
     162              :                              size_t *data_out_size)
     163              : {
     164          327 :     switch (aead_cipher_suite) {
     165          327 :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_128_GCM:
     166              :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_256_GCM:
     167              : #if LIBSPDM_AEAD_GCM_SUPPORT
     168              : #if !LIBSPDM_AEAD_AES_128_GCM_SUPPORT
     169              :         LIBSPDM_ASSERT(aead_cipher_suite != SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_128_GCM);
     170              : #endif
     171              : #if !LIBSPDM_AEAD_AES_256_GCM_SUPPORT
     172              :         LIBSPDM_ASSERT(aead_cipher_suite != SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AES_256_GCM);
     173              : #endif
     174          327 :         return libspdm_aead_aes_gcm_decrypt(key, key_size, iv, iv_size, a_data,
     175              :                                             a_data_size, data_in, data_in_size, tag,
     176              :                                             tag_size, data_out, data_out_size);
     177              : #else
     178              :         LIBSPDM_ASSERT(false);
     179              :         return false;
     180              : #endif
     181            0 :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_CHACHA20_POLY1305:
     182              : #if LIBSPDM_AEAD_CHACHA20_POLY1305_SUPPORT
     183              :         return libspdm_aead_chacha20_poly1305_decrypt(key, key_size, iv, iv_size, a_data,
     184              :                                                       a_data_size, data_in, data_in_size, tag,
     185              :                                                       tag_size, data_out, data_out_size);
     186              : #else
     187            0 :         LIBSPDM_ASSERT(false);
     188            0 :         return false;
     189              : #endif
     190            0 :     case SPDM_ALGORITHMS_AEAD_CIPHER_SUITE_AEAD_SM4_GCM:
     191              : #if LIBSPDM_AEAD_SM4_SUPPORT
     192              :         return libspdm_aead_sm4_gcm_decrypt(key, key_size, iv, iv_size, a_data,
     193              :                                             a_data_size, data_in, data_in_size, tag,
     194              :                                             tag_size, data_out, data_out_size);
     195              : #else
     196            0 :         LIBSPDM_ASSERT(false);
     197            0 :         return false;
     198              : #endif
     199            0 :     default:
     200            0 :         LIBSPDM_ASSERT(false);
     201            0 :         return false;
     202              :     }
     203              : }
        

Generated by: LCOV version 2.0-1