pal_crypto.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Copyright (C) 2014 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 Lesser 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser 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. #include <stddef.h>
  21. #include <stdint.h>
  22. #include <stdbool.h>
  23. #include <unistd.h>
  24. #define SHA256_DIGEST_LEN 32
  25. #ifdef CRYPTO_USE_MBEDTLS
  26. #define CRYPTO_PROVIDER_SPECIFIED
  27. #include "mbedtls/cmac.h"
  28. typedef struct AES LIB_AES_CONTEXT;
  29. #include "mbedtls/dhm.h"
  30. #include "mbedtls/rsa.h"
  31. #include "mbedtls/sha256.h"
  32. typedef mbedtls_sha256_context LIB_SHA256_CONTEXT;
  33. /* DH_SIZE is tied to the choice of parameters in mbedtls_dh.c. */
  34. #define DH_SIZE 256
  35. #include "mbedtls/dhm.h"
  36. typedef mbedtls_dhm_context LIB_DH_CONTEXT;
  37. typedef mbedtls_rsa_context LIB_RSA_KEY;
  38. typedef struct {
  39. mbedtls_cipher_type_t cipher;
  40. mbedtls_cipher_context_t ctx;
  41. } LIB_AESCMAC_CONTEXT;
  42. #include "mbedtls/ctr_drbg.h"
  43. #include "mbedtls/entropy.h"
  44. #include "mbedtls/ssl.h"
  45. typedef struct {
  46. mbedtls_entropy_context entropy;
  47. mbedtls_ctr_drbg_context ctr_drbg;
  48. mbedtls_ssl_config conf;
  49. mbedtls_ssl_context ssl;
  50. int ciphersuites[2]; /* [0] is actual ciphersuite, [1] must be 0 to indicate end of array */
  51. ssize_t (*pal_recv_cb)(int fd, void* buf, size_t len);
  52. ssize_t (*pal_send_cb)(int fd, const void* buf, size_t len);
  53. int stream_fd;
  54. } LIB_SSL_CONTEXT;
  55. #endif /* CRYPTO_USE_MBEDTLS */
  56. #ifndef CRYPTO_PROVIDER_SPECIFIED
  57. # error "Unknown crypto provider. Set CRYPTO_PROVIDER in Makefile"
  58. #endif
  59. /* SHA256 */
  60. int lib_SHA256Init(LIB_SHA256_CONTEXT *context);
  61. int lib_SHA256Update(LIB_SHA256_CONTEXT *context, const uint8_t *data,
  62. uint64_t len);
  63. int lib_SHA256Final(LIB_SHA256_CONTEXT *context, uint8_t *output);
  64. /* Diffie-Hellman Key Exchange */
  65. int lib_DhInit(LIB_DH_CONTEXT *context);
  66. int lib_DhCreatePublic(LIB_DH_CONTEXT *context, uint8_t *public,
  67. uint64_t *public_size);
  68. int lib_DhCalcSecret(LIB_DH_CONTEXT *context, uint8_t *peer, uint64_t peer_size,
  69. uint8_t *secret, uint64_t *secret_size);
  70. void lib_DhFinal(LIB_DH_CONTEXT *context);
  71. /* AES-CMAC */
  72. int lib_AESCMAC(const uint8_t *key, uint64_t key_len, const uint8_t *input,
  73. uint64_t input_len, uint8_t *mac, uint64_t mac_len);
  74. /* note: 'lib_AESCMAC' is the combination of 'lib_AESCMACInit',
  75. * 'lib_AESCMACUpdate', and 'lib_AESCMACFinish'. */
  76. int lib_AESCMACInit(LIB_AESCMAC_CONTEXT * context,
  77. const uint8_t *key, uint64_t key_len);
  78. int lib_AESCMACUpdate(LIB_AESCMAC_CONTEXT * context, const uint8_t * input,
  79. uint64_t input_len);
  80. int lib_AESCMACFinish(LIB_AESCMAC_CONTEXT * context, uint8_t * mac,
  81. uint64_t mac_len);
  82. /* RSA. Limited functionality. */
  83. // Initializes the key structure
  84. int lib_RSAInitKey(LIB_RSA_KEY *key);
  85. // Must call lib_RSAInitKey first
  86. int lib_RSAGenerateKey(LIB_RSA_KEY *key, uint64_t length_in_bits,
  87. uint64_t exponent);
  88. int lib_RSAExportPublicKey(LIB_RSA_KEY *key, uint8_t *e, uint64_t *e_size,
  89. uint8_t *n, uint64_t *n_size);
  90. int lib_RSAImportPublicKey(LIB_RSA_KEY *key, const uint8_t *e, uint64_t e_size,
  91. const uint8_t *n, uint64_t n_size);
  92. // Sign and verify signatures.
  93. // This function must implement RSA signature verification using PKCS#1 v1.5
  94. // padding, with SHA256 as the hash mechanism. These signatures are generated
  95. // by the Graphene filesystem build (so outside of a running Graphene
  96. // application), but are verified within the Graphene application.
  97. int lib_RSAVerifySHA256(LIB_RSA_KEY* key, const uint8_t* hash, uint64_t hash_len,
  98. const uint8_t* signature, uint64_t signature_len);
  99. // Frees memory allocated in lib_RSAInitKey.
  100. int lib_RSAFreeKey(LIB_RSA_KEY *key);
  101. // Encode and decode Base64 messages.
  102. // These two functions can be used to query encode and decode sizes if dst is given NULL
  103. int lib_Base64Encode(const uint8_t* src, size_t slen, char* dst, size_t* dlen);
  104. int lib_Base64Decode(const char *src, size_t slen, uint8_t* dst, size_t* dlen);
  105. #ifdef CRYPTO_USE_MBEDTLS
  106. #include "mbedtls/asn1.h"
  107. enum asn1_tag {
  108. ASN1_BOOLEAN = MBEDTLS_ASN1_BOOLEAN,
  109. ASN1_INTEGET = MBEDTLS_ASN1_INTEGER,
  110. ASN1_BIT_STRING = MBEDTLS_ASN1_BIT_STRING,
  111. ASN1_OCTET_STRING = MBEDTLS_ASN1_OCTET_STRING,
  112. ASN1_NULL = MBEDTLS_ASN1_NULL,
  113. ASN1_OID = MBEDTLS_ASN1_OID,
  114. ASN1_UTF8_STRING = MBEDTLS_ASN1_UTF8_STRING,
  115. ASN1_SEQUENCE = MBEDTLS_ASN1_SEQUENCE,
  116. ASN1_SET = MBEDTLS_ASN1_SET,
  117. ASN1_PRINTABLE_STRING = MBEDTLS_ASN1_PRINTABLE_STRING,
  118. ASN1_T61_STRING = MBEDTLS_ASN1_T61_STRING,
  119. ASN1_IA5_STRING = MBEDTLS_ASN1_IA5_STRING,
  120. ASN1_UTC_TIME = MBEDTLS_ASN1_UTC_TIME,
  121. ASN1_GENERALIZED_TIME = MBEDTLS_ASN1_GENERALIZED_TIME,
  122. ASN1_UNIVERSAL_STRING = MBEDTLS_ASN1_UNIVERSAL_STRING,
  123. ASN1_BMP_STRING = MBEDTLS_ASN1_BMP_STRING,
  124. };
  125. #endif /* CRYPTO_USE_MBEDTLS */
  126. int lib_ASN1GetSerial(uint8_t** ptr, const uint8_t* end, enum asn1_tag* tag, bool* is_construct,
  127. uint8_t** buf, size_t* len);
  128. int lib_ASN1GetBitstring(uint8_t** ptr, const uint8_t* end, uint8_t** str, size_t* len);
  129. int lib_ASN1GetLargeNumberLength(uint8_t** ptr, const uint8_t* end, size_t* len);
  130. /* SSL/TLS */
  131. int lib_SSLInit(LIB_SSL_CONTEXT* ssl_ctx, int stream_fd, bool is_server,
  132. const uint8_t* psk, size_t psk_size,
  133. ssize_t (*pal_recv_cb)(int fd, void* buf, size_t len),
  134. ssize_t (*pal_send_cb)(int fd, const void* buf, size_t len));
  135. int lib_SSLFree(LIB_SSL_CONTEXT* ssl_ctx);
  136. int lib_SSLRead(LIB_SSL_CONTEXT* ssl_ctx, uint8_t* buf, size_t len);
  137. int lib_SSLWrite(LIB_SSL_CONTEXT* ssl_ctx, const uint8_t* buf, size_t len);
  138. #endif