mbedtls_adapter.c 6.2 KB

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