crypto_mod.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2015-2022, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef CRYPTO_MOD_H
  7. #define CRYPTO_MOD_H
  8. #define CRYPTO_AUTH_VERIFY_ONLY 1
  9. #define CRYPTO_HASH_CALC_ONLY 2
  10. #define CRYPTO_AUTH_VERIFY_AND_HASH_CALC 3
  11. /* Return values */
  12. enum crypto_ret_value {
  13. CRYPTO_SUCCESS = 0,
  14. CRYPTO_ERR_INIT,
  15. CRYPTO_ERR_HASH,
  16. CRYPTO_ERR_SIGNATURE,
  17. CRYPTO_ERR_DECRYPTION,
  18. CRYPTO_ERR_UNKNOWN
  19. };
  20. #define CRYPTO_MAX_IV_SIZE 16U
  21. #define CRYPTO_MAX_TAG_SIZE 16U
  22. /* Decryption algorithm */
  23. enum crypto_dec_algo {
  24. CRYPTO_GCM_DECRYPT = 0
  25. };
  26. /* Message digest algorithm */
  27. enum crypto_md_algo {
  28. CRYPTO_MD_SHA256,
  29. CRYPTO_MD_SHA384,
  30. CRYPTO_MD_SHA512,
  31. };
  32. /* Maximum size as per the known stronger hash algorithm i.e.SHA512 */
  33. #define CRYPTO_MD_MAX_SIZE 64U
  34. /*
  35. * Cryptographic library descriptor
  36. */
  37. typedef struct crypto_lib_desc_s {
  38. const char *name;
  39. /* Initialize library. This function is not expected to fail. All errors
  40. * must be handled inside the function, asserting or panicing in case of
  41. * a non-recoverable error */
  42. void (*init)(void);
  43. /* Verify a digital signature. Return one of the
  44. * 'enum crypto_ret_value' options */
  45. #if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
  46. CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
  47. int (*verify_signature)(void *data_ptr, unsigned int data_len,
  48. void *sig_ptr, unsigned int sig_len,
  49. void *sig_alg, unsigned int sig_alg_len,
  50. void *pk_ptr, unsigned int pk_len);
  51. /* Verify a hash. Return one of the 'enum crypto_ret_value' options */
  52. int (*verify_hash)(void *data_ptr, unsigned int data_len,
  53. void *digest_info_ptr, unsigned int digest_info_len);
  54. #endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
  55. CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
  56. #if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
  57. CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
  58. /* Calculate a hash. Return hash value */
  59. int (*calc_hash)(enum crypto_md_algo md_alg, void *data_ptr,
  60. unsigned int data_len,
  61. unsigned char output[CRYPTO_MD_MAX_SIZE]);
  62. #endif /* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
  63. CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
  64. /*
  65. * Authenticated decryption. Return one of the
  66. * 'enum crypto_ret_value' options.
  67. */
  68. int (*auth_decrypt)(enum crypto_dec_algo dec_algo, void *data_ptr,
  69. size_t len, const void *key, unsigned int key_len,
  70. unsigned int key_flags, const void *iv,
  71. unsigned int iv_len, const void *tag,
  72. unsigned int tag_len);
  73. } crypto_lib_desc_t;
  74. /* Public functions */
  75. #if CRYPTO_SUPPORT
  76. void crypto_mod_init(void);
  77. #else
  78. static inline void crypto_mod_init(void)
  79. {
  80. }
  81. #endif /* CRYPTO_SUPPORT */
  82. #if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
  83. CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
  84. int crypto_mod_verify_signature(void *data_ptr, unsigned int data_len,
  85. void *sig_ptr, unsigned int sig_len,
  86. void *sig_alg_ptr, unsigned int sig_alg_len,
  87. void *pk_ptr, unsigned int pk_len);
  88. int crypto_mod_verify_hash(void *data_ptr, unsigned int data_len,
  89. void *digest_info_ptr, unsigned int digest_info_len);
  90. #endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY || \
  91. CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
  92. int crypto_mod_auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
  93. size_t len, const void *key, unsigned int key_len,
  94. unsigned int key_flags, const void *iv,
  95. unsigned int iv_len, const void *tag,
  96. unsigned int tag_len);
  97. #if CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
  98. CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
  99. int crypto_mod_calc_hash(enum crypto_md_algo alg, void *data_ptr,
  100. unsigned int data_len,
  101. unsigned char output[CRYPTO_MD_MAX_SIZE]);
  102. #endif /* CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY || \
  103. CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
  104. #if CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC
  105. /* Macro to register a cryptographic library */
  106. #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \
  107. _calc_hash, _auth_decrypt) \
  108. const crypto_lib_desc_t crypto_lib_desc = { \
  109. .name = _name, \
  110. .init = _init, \
  111. .verify_signature = _verify_signature, \
  112. .verify_hash = _verify_hash, \
  113. .calc_hash = _calc_hash, \
  114. .auth_decrypt = _auth_decrypt \
  115. }
  116. #elif CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_ONLY
  117. #define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \
  118. _auth_decrypt) \
  119. const crypto_lib_desc_t crypto_lib_desc = { \
  120. .name = _name, \
  121. .init = _init, \
  122. .verify_signature = _verify_signature, \
  123. .verify_hash = _verify_hash, \
  124. .auth_decrypt = _auth_decrypt \
  125. }
  126. #elif CRYPTO_SUPPORT == CRYPTO_HASH_CALC_ONLY
  127. #define REGISTER_CRYPTO_LIB(_name, _init, _calc_hash) \
  128. const crypto_lib_desc_t crypto_lib_desc = { \
  129. .name = _name, \
  130. .init = _init, \
  131. .calc_hash = _calc_hash, \
  132. }
  133. #endif /* CRYPTO_SUPPORT == CRYPTO_AUTH_VERIFY_AND_HASH_CALC */
  134. extern const crypto_lib_desc_t crypto_lib_desc;
  135. #endif /* CRYPTO_MOD_H */