pal_crypto.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/rsa.h"
  22. #define SHA256_DIGEST_LEN 32
  23. #define AES_CMAC_KEY_LEN 16
  24. #define AES_CMAC_DIGEST_LEN 32
  25. #ifdef CRYPTO_USE_WOLFSSL
  26. #define CRYPTO_PROVIDER_SPECIFIED
  27. #include "crypto/wolfssl/cmac.h"
  28. #include "crypto/wolfssl/aes.h"
  29. #include "crypto/wolfssl/sha256.h"
  30. #include "crypto/wolfssl/dh.h"
  31. typedef SHA256 LIB_SHA256_CONTEXT;
  32. #define DH_SIZE 128
  33. typedef struct {
  34. uint8_t priv[DH_SIZE];
  35. uint32_t priv_size;
  36. DhKey key;
  37. } LIB_DH_CONTEXT __attribute__((aligned(DH_SIZE)));
  38. #endif /* CRYPTO_USE_WOLFSSL */
  39. #ifdef CRYPTO_USE_MBEDTLS
  40. #define CRYPTO_PROVIDER_SPECIFIED
  41. #include "crypto/mbedtls/mbedtls/cmac.h"
  42. typedef struct AES LIB_AES_CONTEXT;
  43. #include "crypto/mbedtls/mbedtls/dhm.h"
  44. #include "crypto/mbedtls/mbedtls/sha256.h"
  45. typedef mbedtls_sha256_context LIB_SHA256_CONTEXT;
  46. /* DH_SIZE is tied to the choice of parameters in mbedtls_dh.c. */
  47. #define DH_SIZE 256
  48. #include "crypto/mbedtls/mbedtls/dhm.h"
  49. typedef mbedtls_dhm_context LIB_DH_CONTEXT;
  50. #endif /* CRYPTO_USE_MBEDTLS */
  51. #ifndef CRYPTO_PROVIDER_SPECIFIED
  52. # error "Unknown crypto provider. Set CRYPTO_PROVIDER in Makefile"
  53. #endif
  54. /* SHA256 */
  55. int lib_SHA256Init(LIB_SHA256_CONTEXT *context);
  56. int lib_SHA256Update(LIB_SHA256_CONTEXT *context, const uint8_t *data,
  57. uint64_t len);
  58. int lib_SHA256Final(LIB_SHA256_CONTEXT *context, uint8_t *output);
  59. /* Diffie-Hellman Key Exchange */
  60. int lib_DhInit(LIB_DH_CONTEXT *context);
  61. int lib_DhCreatePublic(LIB_DH_CONTEXT *context, uint8_t *public,
  62. uint64_t *public_size);
  63. int lib_DhCalcSecret(LIB_DH_CONTEXT *context, uint8_t *peer, uint64_t peer_size,
  64. uint8_t *secret, uint64_t *secret_size);
  65. void lib_DhFinal(LIB_DH_CONTEXT *context);
  66. /* AES-CMAC */
  67. int lib_AESCMAC(const uint8_t *key, uint64_t key_len, const uint8_t *input,
  68. uint64_t input_len, uint8_t *mac, uint64_t mac_len);
  69. #endif