mbedtls_adapter.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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_AESCMACInit(LIB_AESCMAC_CONTEXT * context,
  87. const uint8_t *key, uint64_t key_len)
  88. {
  89. int ret;
  90. switch (key_len) {
  91. case 16:
  92. context->cipher = MBEDTLS_CIPHER_AES_128_ECB;
  93. break;
  94. case 24:
  95. context->cipher = MBEDTLS_CIPHER_AES_192_ECB;
  96. break;
  97. case 32:
  98. context->cipher = MBEDTLS_CIPHER_AES_256_ECB;
  99. break;
  100. default:
  101. return -PAL_ERROR_INVAL;
  102. }
  103. const mbedtls_cipher_info_t *cipher_info =
  104. mbedtls_cipher_info_from_type(context->cipher);
  105. if ( ( ret = mbedtls_cipher_setup( &context->ctx, cipher_info ) ) != 0 )
  106. return ret;
  107. return mbedtls_cipher_cmac_starts( &context->ctx, key,
  108. key_len * BITS_PER_BYTE );
  109. }
  110. int lib_AESCMACUpdate(LIB_AESCMAC_CONTEXT * context, const uint8_t * input,
  111. uint64_t input_len)
  112. {
  113. return mbedtls_cipher_cmac_update( &context->ctx, input, input_len );
  114. }
  115. int lib_AESCMACFinish(LIB_AESCMAC_CONTEXT * context, uint8_t * mac,
  116. uint64_t mac_len)
  117. {
  118. const mbedtls_cipher_info_t *cipher_info =
  119. mbedtls_cipher_info_from_type(context->cipher);
  120. int ret = -PAL_ERROR_INVAL;
  121. if (mac_len < cipher_info->block_size)
  122. goto exit;
  123. ret = mbedtls_cipher_cmac_finish( &context->ctx, mac );
  124. exit:
  125. mbedtls_cipher_free( &context->ctx );
  126. return( ret );
  127. }
  128. int lib_RSAInitKey(LIB_RSA_KEY *key)
  129. {
  130. /* For now, we only need PKCS_V15 type padding. If we need to support
  131. * multiple padding types, I guess we'll need to add the padding type
  132. * to this API. We might need to add a wrapper type around the crypto
  133. * library's key/context type, since not all crypto providers store this
  134. * in the conext, and instead require you to pass it on each call. */
  135. /* Last parameter here is the hash type, which is only used for
  136. * PKCS padding type 2.0. */
  137. mbedtls_rsa_init(key, MBEDTLS_RSA_PKCS_V15, 0);
  138. return 0;
  139. }
  140. int lib_RSAGenerateKey(LIB_RSA_KEY *key, uint64_t length_in_bits, uint64_t exponent)
  141. {
  142. if (length_in_bits > UINT_MAX) {
  143. return -PAL_ERROR_INVAL;
  144. }
  145. if (exponent > UINT_MAX || (int) exponent < 0) {
  146. return -PAL_ERROR_INVAL;
  147. }
  148. return mbedtls_rsa_gen_key(key, RandomWrapper, NULL, length_in_bits,
  149. exponent);
  150. }
  151. int lib_RSAExportPublicKey(LIB_RSA_KEY *key, uint8_t *e, uint64_t *e_size,
  152. uint8_t *n, uint64_t *n_size)
  153. {
  154. int ret;
  155. /* Public exponent. */
  156. if ((ret = mbedtls_mpi_write_binary(&key->E, e, *e_size)) != 0) {
  157. return ret;
  158. }
  159. /* Modulus. */
  160. if ((ret = mbedtls_mpi_write_binary(&key->N, n, *n_size)) != 0) {
  161. return ret;
  162. }
  163. return 0;
  164. }
  165. int lib_RSAImportPublicKey(LIB_RSA_KEY *key, const uint8_t *e, uint64_t e_size,
  166. const uint8_t *n, uint64_t n_size)
  167. {
  168. int ret;
  169. /* Public exponent. */
  170. if ((ret = mbedtls_mpi_read_binary(&key->E, e, e_size)) != 0) {
  171. return ret;
  172. }
  173. /* Modulus. */
  174. if ((ret = mbedtls_mpi_read_binary(&key->N, n, n_size)) != 0) {
  175. return ret;
  176. }
  177. /* This length is in bytes. */
  178. key->len = (mbedtls_mpi_bitlen(&key->N) + 7) >> 3;
  179. return 0;
  180. }
  181. int lib_RSAVerifySHA256(LIB_RSA_KEY *key, const uint8_t *signature,
  182. uint64_t signature_len, uint8_t *signed_data_out,
  183. uint64_t signed_data_out_len)
  184. {
  185. size_t real_data_out_len;
  186. /* The mbedtls decrypt API assumes that you have a memory buffer that
  187. * is as large as the key size and take the length as a parameter. We
  188. * check, so that in the event the caller makes a mistake, you'll get
  189. * an error instead of reading off the end of the buffer. */
  190. if (signature_len != key->len) {
  191. return -PAL_ERROR_INVAL;
  192. }
  193. int ret = mbedtls_rsa_rsaes_pkcs1_v15_decrypt(key, NULL, NULL,
  194. MBEDTLS_RSA_PUBLIC,
  195. &real_data_out_len,
  196. signature,
  197. signed_data_out,
  198. signed_data_out_len);
  199. if (ret == 0) {
  200. if (real_data_out_len != SHA256_DIGEST_LEN) {
  201. return -PAL_ERROR_INVAL;
  202. }
  203. }
  204. return ret;
  205. }
  206. int lib_RSAFreeKey(LIB_RSA_KEY *key)
  207. {
  208. mbedtls_rsa_free(key);
  209. return 0;
  210. }