block_cipher.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * \file block_cipher.c
  3. *
  4. * \brief Lightweight abstraction layer for block ciphers with 128 bit blocks,
  5. * for use by the GCM and CCM modules.
  6. */
  7. /*
  8. * Copyright The Mbed TLS Contributors
  9. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  10. */
  11. #include "common.h"
  12. #if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA)
  13. #include "psa/crypto.h"
  14. #include "psa_crypto_core.h"
  15. #include "psa_util_internal.h"
  16. #endif
  17. #include "block_cipher_internal.h"
  18. #if defined(MBEDTLS_BLOCK_CIPHER_C)
  19. #if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA)
  20. static psa_key_type_t psa_key_type_from_block_cipher_id(mbedtls_block_cipher_id_t cipher_id)
  21. {
  22. switch (cipher_id) {
  23. #if defined(MBEDTLS_BLOCK_CIPHER_AES_VIA_PSA)
  24. case MBEDTLS_BLOCK_CIPHER_ID_AES:
  25. return PSA_KEY_TYPE_AES;
  26. #endif
  27. #if defined(MBEDTLS_BLOCK_CIPHER_ARIA_VIA_PSA)
  28. case MBEDTLS_BLOCK_CIPHER_ID_ARIA:
  29. return PSA_KEY_TYPE_ARIA;
  30. #endif
  31. #if defined(MBEDTLS_BLOCK_CIPHER_CAMELLIA_VIA_PSA)
  32. case MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA:
  33. return PSA_KEY_TYPE_CAMELLIA;
  34. #endif
  35. default:
  36. return PSA_KEY_TYPE_NONE;
  37. }
  38. }
  39. static int mbedtls_cipher_error_from_psa(psa_status_t status)
  40. {
  41. return PSA_TO_MBEDTLS_ERR_LIST(status, psa_to_cipher_errors,
  42. psa_generic_status_to_mbedtls);
  43. }
  44. #endif /* MBEDTLS_BLOCK_CIPHER_SOME_PSA */
  45. void mbedtls_block_cipher_free(mbedtls_block_cipher_context_t *ctx)
  46. {
  47. if (ctx == NULL) {
  48. return;
  49. }
  50. #if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA)
  51. if (ctx->engine == MBEDTLS_BLOCK_CIPHER_ENGINE_PSA) {
  52. psa_destroy_key(ctx->psa_key_id);
  53. return;
  54. }
  55. #endif
  56. switch (ctx->id) {
  57. #if defined(MBEDTLS_AES_C)
  58. case MBEDTLS_BLOCK_CIPHER_ID_AES:
  59. mbedtls_aes_free(&ctx->ctx.aes);
  60. break;
  61. #endif
  62. #if defined(MBEDTLS_ARIA_C)
  63. case MBEDTLS_BLOCK_CIPHER_ID_ARIA:
  64. mbedtls_aria_free(&ctx->ctx.aria);
  65. break;
  66. #endif
  67. #if defined(MBEDTLS_CAMELLIA_C)
  68. case MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA:
  69. mbedtls_camellia_free(&ctx->ctx.camellia);
  70. break;
  71. #endif
  72. default:
  73. break;
  74. }
  75. ctx->id = MBEDTLS_BLOCK_CIPHER_ID_NONE;
  76. }
  77. int mbedtls_block_cipher_setup(mbedtls_block_cipher_context_t *ctx,
  78. mbedtls_cipher_id_t cipher_id)
  79. {
  80. ctx->id = (cipher_id == MBEDTLS_CIPHER_ID_AES) ? MBEDTLS_BLOCK_CIPHER_ID_AES :
  81. (cipher_id == MBEDTLS_CIPHER_ID_ARIA) ? MBEDTLS_BLOCK_CIPHER_ID_ARIA :
  82. (cipher_id == MBEDTLS_CIPHER_ID_CAMELLIA) ? MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA :
  83. MBEDTLS_BLOCK_CIPHER_ID_NONE;
  84. #if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA)
  85. psa_key_type_t psa_key_type = psa_key_type_from_block_cipher_id(ctx->id);
  86. if (psa_key_type != PSA_KEY_TYPE_NONE &&
  87. psa_can_do_cipher(psa_key_type, PSA_ALG_ECB_NO_PADDING)) {
  88. ctx->engine = MBEDTLS_BLOCK_CIPHER_ENGINE_PSA;
  89. return 0;
  90. }
  91. ctx->engine = MBEDTLS_BLOCK_CIPHER_ENGINE_LEGACY;
  92. #endif
  93. switch (ctx->id) {
  94. #if defined(MBEDTLS_AES_C)
  95. case MBEDTLS_BLOCK_CIPHER_ID_AES:
  96. mbedtls_aes_init(&ctx->ctx.aes);
  97. return 0;
  98. #endif
  99. #if defined(MBEDTLS_ARIA_C)
  100. case MBEDTLS_BLOCK_CIPHER_ID_ARIA:
  101. mbedtls_aria_init(&ctx->ctx.aria);
  102. return 0;
  103. #endif
  104. #if defined(MBEDTLS_CAMELLIA_C)
  105. case MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA:
  106. mbedtls_camellia_init(&ctx->ctx.camellia);
  107. return 0;
  108. #endif
  109. default:
  110. ctx->id = MBEDTLS_BLOCK_CIPHER_ID_NONE;
  111. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  112. }
  113. }
  114. int mbedtls_block_cipher_setkey(mbedtls_block_cipher_context_t *ctx,
  115. const unsigned char *key,
  116. unsigned key_bitlen)
  117. {
  118. #if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA)
  119. if (ctx->engine == MBEDTLS_BLOCK_CIPHER_ENGINE_PSA) {
  120. psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
  121. psa_status_t status;
  122. psa_set_key_type(&key_attr, psa_key_type_from_block_cipher_id(ctx->id));
  123. psa_set_key_bits(&key_attr, key_bitlen);
  124. psa_set_key_algorithm(&key_attr, PSA_ALG_ECB_NO_PADDING);
  125. psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_ENCRYPT);
  126. status = psa_import_key(&key_attr, key, PSA_BITS_TO_BYTES(key_bitlen), &ctx->psa_key_id);
  127. if (status != PSA_SUCCESS) {
  128. return mbedtls_cipher_error_from_psa(status);
  129. }
  130. psa_reset_key_attributes(&key_attr);
  131. return 0;
  132. }
  133. #endif /* MBEDTLS_BLOCK_CIPHER_SOME_PSA */
  134. switch (ctx->id) {
  135. #if defined(MBEDTLS_AES_C)
  136. case MBEDTLS_BLOCK_CIPHER_ID_AES:
  137. return mbedtls_aes_setkey_enc(&ctx->ctx.aes, key, key_bitlen);
  138. #endif
  139. #if defined(MBEDTLS_ARIA_C)
  140. case MBEDTLS_BLOCK_CIPHER_ID_ARIA:
  141. return mbedtls_aria_setkey_enc(&ctx->ctx.aria, key, key_bitlen);
  142. #endif
  143. #if defined(MBEDTLS_CAMELLIA_C)
  144. case MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA:
  145. return mbedtls_camellia_setkey_enc(&ctx->ctx.camellia, key, key_bitlen);
  146. #endif
  147. default:
  148. return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
  149. }
  150. }
  151. int mbedtls_block_cipher_encrypt(mbedtls_block_cipher_context_t *ctx,
  152. const unsigned char input[16],
  153. unsigned char output[16])
  154. {
  155. #if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA)
  156. if (ctx->engine == MBEDTLS_BLOCK_CIPHER_ENGINE_PSA) {
  157. psa_status_t status;
  158. size_t olen;
  159. status = psa_cipher_encrypt(ctx->psa_key_id, PSA_ALG_ECB_NO_PADDING,
  160. input, 16, output, 16, &olen);
  161. if (status != PSA_SUCCESS) {
  162. return mbedtls_cipher_error_from_psa(status);
  163. }
  164. return 0;
  165. }
  166. #endif /* MBEDTLS_BLOCK_CIPHER_SOME_PSA */
  167. switch (ctx->id) {
  168. #if defined(MBEDTLS_AES_C)
  169. case MBEDTLS_BLOCK_CIPHER_ID_AES:
  170. return mbedtls_aes_crypt_ecb(&ctx->ctx.aes, MBEDTLS_AES_ENCRYPT,
  171. input, output);
  172. #endif
  173. #if defined(MBEDTLS_ARIA_C)
  174. case MBEDTLS_BLOCK_CIPHER_ID_ARIA:
  175. return mbedtls_aria_crypt_ecb(&ctx->ctx.aria, input, output);
  176. #endif
  177. #if defined(MBEDTLS_CAMELLIA_C)
  178. case MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA:
  179. return mbedtls_camellia_crypt_ecb(&ctx->ctx.camellia,
  180. MBEDTLS_CAMELLIA_ENCRYPT,
  181. input, output);
  182. #endif
  183. default:
  184. return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
  185. }
  186. }
  187. #endif /* MBEDTLS_BLOCK_CIPHER_C */