psa.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* psa.h
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /**
  22. * Platform Security Architecture (PSA) header
  23. *
  24. * If WOLFSSL_HAVE_PSA is defined, wolfSSL can use the cryptographic primitives
  25. * exported by a PSA Crypto API.
  26. *
  27. * Defines:
  28. *
  29. * WOLFSSL_HAVE_PSA: Global switch to enable PSA
  30. * WOLFSSL_PSA_NO_RNG: disable PSA random generator support
  31. * WOLFSSL_PSA_NO_HASH: disable PSA hashing support
  32. * WOLFSSL_PSA_NO_AES: disable PSA AES support
  33. * WOLFSSL_PSA_GLOBAL_LOCK: serialize the access to the underlying PSA lib
  34. * WOLFSSL_PSA_NO_PKCBS: disable PK callbacks support
  35. */
  36. #ifndef WOLFSSL_PSA_H
  37. #define WOLFSSL_PSA_H
  38. #ifdef HAVE_CONFIG_H
  39. #include <config.h>
  40. #endif
  41. #include <wolfssl/wolfcrypt/types.h>
  42. /* PSA implementation takes over the Sha struct and Sha functions implementation
  43. completely. Devoiding the struct of the DevId field and hooks to make
  44. crypto_cb work. */
  45. #if !defined(WOLFSSL_PSA_NO_HASH) && defined(WOLF_CRYPTO_CB)
  46. #error "WOLFSSL PSA is not supported with WOLF_CRYPTO_CB"
  47. #endif
  48. #if defined(WOLFSSL_HAVE_PSA)
  49. #include <psa/crypto.h>
  50. #include <wolfssl/wolfcrypt/types.h>
  51. #include <wolfssl/wolfcrypt/visibility.h>
  52. #if !defined(WOLFSSL_PSA_NO_AES)
  53. #if !defined(NO_AES)
  54. #include <wolfssl/wolfcrypt/aes.h>
  55. #endif
  56. #endif /* WOLFSSL_PSA_NO_AES */
  57. #if !defined(WOLFSSL_PSA_NO_PKCB)
  58. #include <wolfssl/ssl.h>
  59. #endif
  60. #ifndef PSA_ALG_NONE
  61. #define PSA_ALG_NONE ((psa_algorithm_t)0)
  62. #endif
  63. #ifndef PSA_KEY_ID_NULL
  64. #define PSA_KEY_ID_NULL ((psa_key_id_t)0)
  65. #endif
  66. #if defined(WOLFSSL_PSA_GLOBAL_LOCK)
  67. void PSA_LOCK(void);
  68. void PSA_UNLOCK(void);
  69. #else
  70. #define PSA_LOCK() WC_DO_NOTHING
  71. #define PSA_UNLOCK() WC_DO_NOTHING
  72. #endif
  73. int wc_psa_init(void);
  74. #if !defined(WOLFSSL_PSA_NO_RNG)
  75. WOLFSSL_API int wc_psa_get_random(unsigned char *out, word32 sz);
  76. #ifndef HAVE_HASHDRBG
  77. #define CUSTOM_RAND_GENERATE_BLOCK wc_psa_get_random
  78. #else
  79. #define CUSTOM_RAND_GENERATE_SEED wc_psa_get_random
  80. #endif
  81. #endif /* WOLFSSL_HAVE_PSA_RNG */
  82. #if !defined(WOLFSSL_PSA_NO_AES) && !defined(NO_AES)
  83. int wc_psa_aes_init(Aes *aes);
  84. int wc_psa_aes_free(Aes *aes);
  85. int wc_psa_aes_get_key_size(Aes *aes, word32 *keySize);
  86. int wc_psa_aes_set_key(Aes *aes, const uint8_t *key,
  87. size_t key_length, uint8_t *iv,
  88. psa_algorithm_t alg, int dir);
  89. WOLFSSL_API int wc_psa_aes_encrypt_decrypt(Aes *aes, const uint8_t *input,
  90. uint8_t *output, size_t length,
  91. psa_algorithm_t alg, int direction);
  92. WOLFSSL_API int wc_AesEncrypt(Aes *aes, const byte *inBlock, byte *outBlock);
  93. #if defined(HAVE_AES_DECRYPT)
  94. WOLFSSL_API int wc_AesDecrypt(Aes *aes, const byte *inBlock, byte *outBlock);
  95. #endif
  96. #endif
  97. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_PSA_NO_PKCB)
  98. struct psa_ssl_ctx {
  99. psa_key_id_t private_key;
  100. psa_key_id_t dh_key;
  101. };
  102. WOLFSSL_API int wolfSSL_CTX_psa_enable(WOLFSSL_CTX *ctx);
  103. WOLFSSL_API int wolfSSL_set_psa_ctx(WOLFSSL *ssl, struct psa_ssl_ctx *ctx);
  104. WOLFSSL_API void wolfSSL_free_psa_ctx(struct psa_ssl_ctx *ctx);
  105. WOLFSSL_API int wolfSSL_psa_set_private_key_id(struct psa_ssl_ctx *ctx,
  106. psa_key_id_t id);
  107. #endif
  108. #endif /* WOLFSSL_HAVE_PSA */
  109. #endif /* WOLFSSL_PSA_H */