pal_crypto.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * Cryptographic primitive abstractions. This layer provides a way to
  15. * change the crypto library without changing the rest of Graphene code
  16. * by providing a small crypto library adaptor implementing these methods.
  17. */
  18. #ifndef PAL_CRYPTO_H
  19. #define PAL_CRYPTO_H
  20. /* These cryptosystems are still unconditionally provided by WolfSSL. */
  21. #include "crypto/cmac.h"
  22. #include "crypto/rsa.h"
  23. #define SHA256_DIGEST_LEN 32
  24. #ifdef CRYPTO_USE_WOLFSSL
  25. #define CRYPTO_PROVIDER_SPECIFIED
  26. #include "crypto/wolfssl/sha256.h"
  27. #include "crypto/wolfssl/dh.h"
  28. typedef SHA256 LIB_SHA256_CONTEXT;
  29. #define DH_SIZE 128
  30. typedef struct {
  31. uint8_t priv[DH_SIZE];
  32. uint32_t priv_size;
  33. DhKey key;
  34. } LIB_DH_CONTEXT __attribute__((aligned(DH_SIZE)));
  35. #endif /* CRYPTO_USE_WOLFSSL */
  36. #ifdef CRYPTO_USE_MBEDTLS
  37. #define CRYPTO_PROVIDER_SPECIFIED
  38. #include "crypto/mbedtls/mbedtls/sha256.h"
  39. typedef mbedtls_sha256_context LIB_SHA256_CONTEXT;
  40. /* DH_SIZE is tied to the choice of parameters in mbedtls_dh.c. */
  41. #define DH_SIZE 256
  42. #include "crypto/mbedtls/mbedtls/dhm.h"
  43. typedef mbedtls_dhm_context LIB_DH_CONTEXT;
  44. #endif /* CRYPTO_USE_MBEDTLS */
  45. #ifndef CRYPTO_PROVIDER_SPECIFIED
  46. # error "Unknown crypto provider. Set CRYPTO_PROVIDER in Makefile"
  47. #endif
  48. /* SHA256 */
  49. int lib_SHA256Init(LIB_SHA256_CONTEXT *context);
  50. int lib_SHA256Update(LIB_SHA256_CONTEXT *context, const uint8_t *data,
  51. uint64_t len);
  52. int lib_SHA256Final(LIB_SHA256_CONTEXT *context, uint8_t *output);
  53. /* Diffie-Hellman Key Exchange */
  54. int lib_DhInit(LIB_DH_CONTEXT *context);
  55. int lib_DhCreatePublic(LIB_DH_CONTEXT *context, uint8_t *public,
  56. uint64_t *public_size);
  57. int lib_DhCalcSecret(LIB_DH_CONTEXT *context, uint8_t *peer, uint64_t peer_size,
  58. uint8_t *secret, uint64_t *secret_size);
  59. void lib_DhFinal(LIB_DH_CONTEXT *context);
  60. #endif