crypto_mod.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2015-2023, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <common/debug.h>
  8. #include <drivers/auth/crypto_mod.h>
  9. /* Variable exported by the crypto library through REGISTER_CRYPTO_LIB() */
  10. /*
  11. * The crypto module is responsible for verifying digital signatures and hashes.
  12. * It relies on a crypto library to perform the cryptographic operations.
  13. *
  14. * The crypto module itself does not impose any specific format on signatures,
  15. * signature algorithm, keys or hashes, but most cryptographic libraries will
  16. * take the parameters as the following DER encoded ASN.1 structures:
  17. *
  18. * AlgorithmIdentifier ::= SEQUENCE {
  19. * algorithm OBJECT IDENTIFIER,
  20. * parameters ANY DEFINED BY algorithm OPTIONAL
  21. * }
  22. *
  23. * DigestInfo ::= SEQUENCE {
  24. * digestAlgorithm AlgorithmIdentifier,
  25. * digest OCTET STRING
  26. * }
  27. *
  28. * SubjectPublicKeyInfo ::= SEQUENCE {
  29. * algorithm AlgorithmIdentifier,
  30. * subjectPublicKey BIT STRING
  31. * }
  32. *
  33. * SignatureAlgorithm ::= AlgorithmIdentifier
  34. *
  35. * SignatureValue ::= BIT STRING
  36. */
  37. /*
  38. * Perform some static checking and call the library initialization function
  39. */
  40. void crypto_mod_init(void)
  41. {
  42. assert(crypto_lib_desc.name != NULL);
  43. assert(crypto_lib_desc.init != NULL);
  44. #if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
  45. CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
  46. assert(crypto_lib_desc.verify_signature != NULL);
  47. assert(crypto_lib_desc.verify_hash != NULL);
  48. #endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
  49. CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
  50. #if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
  51. CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
  52. assert(crypto_lib_desc.calc_hash != NULL);
  53. #endif /* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
  54. CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
  55. /* Initialize the cryptographic library */
  56. crypto_lib_desc.init();
  57. INFO("Using crypto library '%s'\n", crypto_lib_desc.name);
  58. }
  59. #if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
  60. CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
  61. /*
  62. * Function to verify a digital signature
  63. *
  64. * Parameters:
  65. *
  66. * data_ptr, data_len: signed data
  67. * sig_ptr, sig_len: the digital signature
  68. * sig_alg_ptr, sig_alg_len: the digital signature algorithm
  69. * pk_ptr, pk_len: the public key
  70. */
  71. int crypto_mod_verify_signature(void *data_ptr, unsigned int data_len,
  72. void *sig_ptr, unsigned int sig_len,
  73. void *sig_alg_ptr, unsigned int sig_alg_len,
  74. void *pk_ptr, unsigned int pk_len)
  75. {
  76. assert(data_ptr != NULL);
  77. assert(data_len != 0);
  78. assert(sig_ptr != NULL);
  79. assert(sig_len != 0);
  80. assert(sig_alg_ptr != NULL);
  81. assert(sig_alg_len != 0);
  82. assert(pk_ptr != NULL);
  83. assert(pk_len != 0);
  84. return crypto_lib_desc.verify_signature(data_ptr, data_len,
  85. sig_ptr, sig_len,
  86. sig_alg_ptr, sig_alg_len,
  87. pk_ptr, pk_len);
  88. }
  89. /*
  90. * Verify a hash by comparison
  91. *
  92. * Parameters:
  93. *
  94. * data_ptr, data_len: data to be hashed
  95. * digest_info_ptr, digest_info_len: hash to be compared
  96. */
  97. int crypto_mod_verify_hash(void *data_ptr, unsigned int data_len,
  98. void *digest_info_ptr, unsigned int digest_info_len)
  99. {
  100. assert(data_ptr != NULL);
  101. assert(data_len != 0);
  102. assert(digest_info_ptr != NULL);
  103. assert(digest_info_len != 0);
  104. return crypto_lib_desc.verify_hash(data_ptr, data_len,
  105. digest_info_ptr, digest_info_len);
  106. }
  107. #endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
  108. CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
  109. #if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
  110. CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
  111. /*
  112. * Calculate a hash
  113. *
  114. * Parameters:
  115. *
  116. * alg: message digest algorithm
  117. * data_ptr, data_len: data to be hashed
  118. * output: resulting hash
  119. */
  120. int crypto_mod_calc_hash(enum crypto_md_algo alg, void *data_ptr,
  121. unsigned int data_len,
  122. unsigned char output[CRYPTO_MD_MAX_SIZE])
  123. {
  124. assert(data_ptr != NULL);
  125. assert(data_len != 0);
  126. assert(output != NULL);
  127. return crypto_lib_desc.calc_hash(alg, data_ptr, data_len, output);
  128. }
  129. #endif /* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
  130. CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
  131. int crypto_mod_convert_pk(void *full_pk_ptr, unsigned int full_pk_len,
  132. void **hashed_pk_ptr, unsigned int *hashed_pk_len)
  133. {
  134. if (crypto_lib_desc.convert_pk != NULL) {
  135. return crypto_lib_desc.convert_pk(full_pk_ptr, full_pk_len,
  136. hashed_pk_ptr, hashed_pk_len);
  137. }
  138. *hashed_pk_ptr = full_pk_ptr;
  139. *hashed_pk_len = full_pk_len;
  140. return 0;
  141. }
  142. /*
  143. * Authenticated decryption of data
  144. *
  145. * Parameters:
  146. *
  147. * dec_algo: authenticated decryption algorithm
  148. * data_ptr, len: data to be decrypted (inout param)
  149. * key, key_len, key_flags: symmetric decryption key
  150. * iv, iv_len: initialization vector
  151. * tag, tag_len: authentication tag
  152. */
  153. int crypto_mod_auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
  154. size_t len, const void *key, unsigned int key_len,
  155. unsigned int key_flags, const void *iv,
  156. unsigned int iv_len, const void *tag,
  157. unsigned int tag_len)
  158. {
  159. assert(crypto_lib_desc.auth_decrypt != NULL);
  160. assert(data_ptr != NULL);
  161. assert(len != 0U);
  162. assert(key != NULL);
  163. assert(key_len != 0U);
  164. assert(iv != NULL);
  165. assert((iv_len != 0U) && (iv_len <= CRYPTO_MAX_IV_SIZE));
  166. assert(tag != NULL);
  167. assert((tag_len != 0U) && (tag_len <= CRYPTO_MAX_TAG_SIZE));
  168. return crypto_lib_desc.auth_decrypt(dec_algo, data_ptr, len, key,
  169. key_len, key_flags, iv, iv_len, tag,
  170. tag_len);
  171. }