mbedtls_adapter.c 6.1 KB

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