mbedtls_adapter.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* This file is part of Graphene Library OS.
  2. Graphene Library OS is free software: you can redistribute it and/or
  3. modify it under the terms of the GNU General Public License
  4. as published by the Free Software Foundation, either version 3 of the
  5. License, or (at your option) any later version.
  6. Graphene Library OS is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  12. #include <stdint.h>
  13. #include <limits.h>
  14. #include "pal.h"
  15. #include "pal_crypto.h"
  16. #include "pal_error.h"
  17. #include "pal_debug.h"
  18. #include "assert.h"
  19. #include "crypto/mbedtls/mbedtls/cmac.h"
  20. #include "crypto/mbedtls/mbedtls/sha256.h"
  21. #include "crypto/mbedtls/mbedtls/rsa.h"
  22. #define BITS_PER_BYTE 8
  23. /* This is declared in pal_internal.h, but that can't be included here. */
  24. int _DkRandomBitsRead(void *buffer, int size);
  25. /* Wrapper to provide mbedtls the RNG interface it expects. It passes an
  26. * extra context parameter, and expects a return value of 0 for success
  27. * and nonzero for failure. */
  28. static int RandomWrapper(void *private, unsigned char *data, size_t size)
  29. {
  30. return _DkRandomBitsRead(data, size) != size;
  31. }
  32. #define BITS_PER_BYTE 8
  33. int lib_SHA256Init(LIB_SHA256_CONTEXT *context)
  34. {
  35. mbedtls_sha256_init(context);
  36. mbedtls_sha256_starts(context, 0 /* 0 = use SSH256 */);
  37. return 0;
  38. }
  39. int lib_SHA256Update(LIB_SHA256_CONTEXT *context, const uint8_t *data,
  40. uint64_t len)
  41. {
  42. /* For compatibility with other SHA256 providers, don't support
  43. * large lengths. */
  44. if (len > UINT32_MAX) {
  45. return -PAL_ERROR_INVAL;
  46. }
  47. mbedtls_sha256_update(context, data, len);
  48. return 0;
  49. }
  50. int lib_SHA256Final(LIB_SHA256_CONTEXT *context, uint8_t *output)
  51. {
  52. mbedtls_sha256_finish(context, output);
  53. /* This function is called free, but it doesn't actually free the memory.
  54. * It zeroes out the context to avoid potentially leaking information
  55. * about the hash that was just performed. */
  56. mbedtls_sha256_free(context);
  57. return 0;
  58. }
  59. int lib_AESCMAC(const uint8_t *key, uint64_t key_len, const uint8_t *input,
  60. uint64_t input_len, uint8_t *mac, uint64_t mac_len) {
  61. mbedtls_cipher_type_t cipher;
  62. switch (key_len) {
  63. case 16:
  64. cipher = MBEDTLS_CIPHER_AES_128_ECB;
  65. break;
  66. case 24:
  67. cipher = MBEDTLS_CIPHER_AES_192_ECB;
  68. break;
  69. case 32:
  70. cipher = MBEDTLS_CIPHER_AES_256_ECB;
  71. break;
  72. default:
  73. return -PAL_ERROR_INVAL;
  74. }
  75. const mbedtls_cipher_info_t *cipher_info =
  76. mbedtls_cipher_info_from_type(cipher);
  77. if (mac_len < cipher_info->block_size) {
  78. return -PAL_ERROR_INVAL;
  79. }
  80. return mbedtls_cipher_cmac(cipher_info,
  81. key, key_len * BITS_PER_BYTE,
  82. input, input_len, mac);
  83. }
  84. int lib_RSAInitKey(LIB_RSA_KEY *key)
  85. {
  86. /* For now, we only need PKCS_V15 type padding. If we need to support
  87. * multiple padding types, I guess we'll need to add the padding type
  88. * to this API. We might need to add a wrapper type around the crypto
  89. * library's key/context type, since not all crypto providers store this
  90. * in the conext, and instead require you to pass it on each call. */
  91. /* Last parameter here is the hash type, which is only used for
  92. * PKCS padding type 2.0. */
  93. mbedtls_rsa_init(key, MBEDTLS_RSA_PKCS_V15, 0);
  94. return 0;
  95. }
  96. int lib_RSAGenerateKey(LIB_RSA_KEY *key, uint64_t length_in_bits, uint64_t exponent)
  97. {
  98. if (length_in_bits > UINT_MAX) {
  99. return -PAL_ERROR_INVAL;
  100. }
  101. if (exponent > UINT_MAX || (int) exponent < 0) {
  102. return -PAL_ERROR_INVAL;
  103. }
  104. return mbedtls_rsa_gen_key(key, RandomWrapper, NULL, length_in_bits,
  105. exponent);
  106. }
  107. int lib_RSAExportPublicKey(LIB_RSA_KEY *key, uint8_t *e, uint64_t *e_size,
  108. uint8_t *n, uint64_t *n_size)
  109. {
  110. int ret;
  111. /* Public exponent. */
  112. if ((ret = mbedtls_mpi_write_binary(&key->E, e, *e_size)) != 0) {
  113. return ret;
  114. }
  115. /* Modulus. */
  116. if ((ret = mbedtls_mpi_write_binary(&key->N, n, *n_size)) != 0) {
  117. return ret;
  118. }
  119. return 0;
  120. }
  121. int lib_RSAImportPublicKey(LIB_RSA_KEY *key, const uint8_t *e, uint64_t e_size,
  122. const uint8_t *n, uint64_t n_size)
  123. {
  124. int ret;
  125. /* Public exponent. */
  126. if ((ret = mbedtls_mpi_read_binary(&key->E, e, e_size)) != 0) {
  127. return ret;
  128. }
  129. /* Modulus. */
  130. if ((ret = mbedtls_mpi_read_binary(&key->N, n, n_size)) != 0) {
  131. return ret;
  132. }
  133. /* This length is in bytes. */
  134. key->len = (mbedtls_mpi_bitlen(&key->N) + 7) >> 3;
  135. return 0;
  136. }
  137. int lib_RSAVerifySHA256(LIB_RSA_KEY *key, const uint8_t *signature,
  138. uint64_t signature_len, uint8_t *signed_data_out,
  139. uint64_t signed_data_out_len)
  140. {
  141. size_t real_data_out_len;
  142. /* The mbedtls decrypt API assumes that you have a memory buffer that
  143. * is as large as the key size and take the length as a parameter. We
  144. * check, so that in the event the caller makes a mistake, you'll get
  145. * an error instead of reading off the end of the buffer. */
  146. if (signature_len != key->len) {
  147. return -PAL_ERROR_INVAL;
  148. }
  149. int ret = mbedtls_rsa_rsaes_pkcs1_v15_decrypt(key, NULL, NULL,
  150. MBEDTLS_RSA_PUBLIC,
  151. &real_data_out_len,
  152. signature,
  153. signed_data_out,
  154. signed_data_out_len);
  155. if (ret == 0) {
  156. if (real_data_out_len != SHA256_DIGEST_LEN) {
  157. return -PAL_ERROR_INVAL;
  158. }
  159. }
  160. return ret;
  161. }
  162. int lib_RSAFreeKey(LIB_RSA_KEY *key)
  163. {
  164. mbedtls_rsa_free(key);
  165. return 0;
  166. }