psa_crypto_aead.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * PSA AEAD entry points
  3. */
  4. /*
  5. * Copyright The Mbed TLS Contributors
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. * not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include "common.h"
  21. #if defined(MBEDTLS_PSA_CRYPTO_C)
  22. #include "psa_crypto_aead.h"
  23. #include "psa_crypto_core.h"
  24. #include "mbedtls/ccm.h"
  25. #include "mbedtls/chachapoly.h"
  26. #include "mbedtls/cipher.h"
  27. #include "mbedtls/gcm.h"
  28. typedef struct
  29. {
  30. psa_algorithm_t core_alg;
  31. uint8_t tag_length;
  32. union
  33. {
  34. unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
  35. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
  36. mbedtls_ccm_context ccm;
  37. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
  38. #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
  39. mbedtls_gcm_context gcm;
  40. #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
  41. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
  42. mbedtls_chachapoly_context chachapoly;
  43. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
  44. } ctx;
  45. } aead_operation_t;
  46. #define AEAD_OPERATION_INIT {0, 0, {0}}
  47. static void psa_aead_abort_internal( aead_operation_t *operation )
  48. {
  49. switch( operation->core_alg )
  50. {
  51. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
  52. case PSA_ALG_CCM:
  53. mbedtls_ccm_free( &operation->ctx.ccm );
  54. break;
  55. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
  56. #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
  57. case PSA_ALG_GCM:
  58. mbedtls_gcm_free( &operation->ctx.gcm );
  59. break;
  60. #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
  61. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
  62. case PSA_ALG_CHACHA20_POLY1305:
  63. mbedtls_chachapoly_free( &operation->ctx.chachapoly );
  64. break;
  65. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
  66. }
  67. }
  68. static psa_status_t psa_aead_setup(
  69. aead_operation_t *operation,
  70. const psa_key_attributes_t *attributes,
  71. const uint8_t *key_buffer,
  72. psa_algorithm_t alg )
  73. {
  74. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  75. size_t key_bits;
  76. const mbedtls_cipher_info_t *cipher_info;
  77. mbedtls_cipher_id_t cipher_id;
  78. size_t full_tag_length = 0;
  79. key_bits = attributes->core.bits;
  80. cipher_info = mbedtls_cipher_info_from_psa( alg,
  81. attributes->core.type, key_bits,
  82. &cipher_id );
  83. if( cipher_info == NULL )
  84. return( PSA_ERROR_NOT_SUPPORTED );
  85. switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
  86. {
  87. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
  88. case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
  89. operation->core_alg = PSA_ALG_CCM;
  90. full_tag_length = 16;
  91. /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
  92. * The call to mbedtls_ccm_encrypt_and_tag or
  93. * mbedtls_ccm_auth_decrypt will validate the tag length. */
  94. if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( attributes->core.type ) != 16 )
  95. return( PSA_ERROR_INVALID_ARGUMENT );
  96. mbedtls_ccm_init( &operation->ctx.ccm );
  97. status = mbedtls_to_psa_error(
  98. mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
  99. key_buffer, (unsigned int) key_bits ) );
  100. if( status != PSA_SUCCESS )
  101. return( status );
  102. break;
  103. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
  104. #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
  105. case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
  106. operation->core_alg = PSA_ALG_GCM;
  107. full_tag_length = 16;
  108. /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
  109. * The call to mbedtls_gcm_crypt_and_tag or
  110. * mbedtls_gcm_auth_decrypt will validate the tag length. */
  111. if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( attributes->core.type ) != 16 )
  112. return( PSA_ERROR_INVALID_ARGUMENT );
  113. mbedtls_gcm_init( &operation->ctx.gcm );
  114. status = mbedtls_to_psa_error(
  115. mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
  116. key_buffer, (unsigned int) key_bits ) );
  117. if( status != PSA_SUCCESS )
  118. return( status );
  119. break;
  120. #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
  121. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
  122. case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
  123. operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
  124. full_tag_length = 16;
  125. /* We only support the default tag length. */
  126. if( alg != PSA_ALG_CHACHA20_POLY1305 )
  127. return( PSA_ERROR_NOT_SUPPORTED );
  128. mbedtls_chachapoly_init( &operation->ctx.chachapoly );
  129. status = mbedtls_to_psa_error(
  130. mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
  131. key_buffer ) );
  132. if( status != PSA_SUCCESS )
  133. return( status );
  134. break;
  135. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
  136. default:
  137. (void) status;
  138. (void) key_buffer;
  139. return( PSA_ERROR_NOT_SUPPORTED );
  140. }
  141. if( PSA_AEAD_TAG_LENGTH( attributes->core.type,
  142. key_bits, alg )
  143. > full_tag_length )
  144. return( PSA_ERROR_INVALID_ARGUMENT );
  145. operation->tag_length = PSA_AEAD_TAG_LENGTH( attributes->core.type,
  146. key_bits,
  147. alg );
  148. return( PSA_SUCCESS );
  149. }
  150. psa_status_t mbedtls_psa_aead_encrypt(
  151. const psa_key_attributes_t *attributes,
  152. const uint8_t *key_buffer, size_t key_buffer_size,
  153. psa_algorithm_t alg,
  154. const uint8_t *nonce, size_t nonce_length,
  155. const uint8_t *additional_data, size_t additional_data_length,
  156. const uint8_t *plaintext, size_t plaintext_length,
  157. uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
  158. {
  159. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  160. aead_operation_t operation = AEAD_OPERATION_INIT;
  161. uint8_t *tag;
  162. (void) key_buffer_size;
  163. status = psa_aead_setup( &operation, attributes, key_buffer, alg );
  164. if( status != PSA_SUCCESS )
  165. goto exit;
  166. /* For all currently supported modes, the tag is at the end of the
  167. * ciphertext. */
  168. if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
  169. {
  170. status = PSA_ERROR_BUFFER_TOO_SMALL;
  171. goto exit;
  172. }
  173. tag = ciphertext + plaintext_length;
  174. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
  175. if( operation.core_alg == PSA_ALG_CCM )
  176. {
  177. status = mbedtls_to_psa_error(
  178. mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
  179. plaintext_length,
  180. nonce, nonce_length,
  181. additional_data,
  182. additional_data_length,
  183. plaintext, ciphertext,
  184. tag, operation.tag_length ) );
  185. }
  186. else
  187. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
  188. #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
  189. if( operation.core_alg == PSA_ALG_GCM )
  190. {
  191. status = mbedtls_to_psa_error(
  192. mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
  193. MBEDTLS_GCM_ENCRYPT,
  194. plaintext_length,
  195. nonce, nonce_length,
  196. additional_data, additional_data_length,
  197. plaintext, ciphertext,
  198. operation.tag_length, tag ) );
  199. }
  200. else
  201. #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
  202. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
  203. if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
  204. {
  205. if( nonce_length != 12 )
  206. {
  207. if( nonce_length == 8 )
  208. status = PSA_ERROR_NOT_SUPPORTED;
  209. else
  210. status = PSA_ERROR_INVALID_ARGUMENT;
  211. goto exit;
  212. }
  213. if( operation.tag_length != 16 )
  214. {
  215. status = PSA_ERROR_NOT_SUPPORTED;
  216. goto exit;
  217. }
  218. status = mbedtls_to_psa_error(
  219. mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
  220. plaintext_length,
  221. nonce,
  222. additional_data,
  223. additional_data_length,
  224. plaintext,
  225. ciphertext,
  226. tag ) );
  227. }
  228. else
  229. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
  230. {
  231. (void) tag;
  232. (void) nonce;
  233. (void) nonce_length;
  234. (void) additional_data;
  235. (void) additional_data_length;
  236. (void) plaintext;
  237. return( PSA_ERROR_NOT_SUPPORTED );
  238. }
  239. if( status == PSA_SUCCESS )
  240. *ciphertext_length = plaintext_length + operation.tag_length;
  241. exit:
  242. psa_aead_abort_internal( &operation );
  243. return( status );
  244. }
  245. /* Locate the tag in a ciphertext buffer containing the encrypted data
  246. * followed by the tag. Return the length of the part preceding the tag in
  247. * *plaintext_length. This is the size of the plaintext in modes where
  248. * the encrypted data has the same size as the plaintext, such as
  249. * CCM and GCM. */
  250. static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
  251. const uint8_t *ciphertext,
  252. size_t ciphertext_length,
  253. size_t plaintext_size,
  254. const uint8_t **p_tag )
  255. {
  256. size_t payload_length;
  257. if( tag_length > ciphertext_length )
  258. return( PSA_ERROR_INVALID_ARGUMENT );
  259. payload_length = ciphertext_length - tag_length;
  260. if( payload_length > plaintext_size )
  261. return( PSA_ERROR_BUFFER_TOO_SMALL );
  262. *p_tag = ciphertext + payload_length;
  263. return( PSA_SUCCESS );
  264. }
  265. psa_status_t mbedtls_psa_aead_decrypt(
  266. const psa_key_attributes_t *attributes,
  267. const uint8_t *key_buffer, size_t key_buffer_size,
  268. psa_algorithm_t alg,
  269. const uint8_t *nonce, size_t nonce_length,
  270. const uint8_t *additional_data, size_t additional_data_length,
  271. const uint8_t *ciphertext, size_t ciphertext_length,
  272. uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
  273. {
  274. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  275. aead_operation_t operation = AEAD_OPERATION_INIT;
  276. const uint8_t *tag = NULL;
  277. (void) key_buffer_size;
  278. status = psa_aead_setup( &operation, attributes, key_buffer, alg );
  279. if( status != PSA_SUCCESS )
  280. goto exit;
  281. status = psa_aead_unpadded_locate_tag( operation.tag_length,
  282. ciphertext, ciphertext_length,
  283. plaintext_size, &tag );
  284. if( status != PSA_SUCCESS )
  285. goto exit;
  286. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
  287. if( operation.core_alg == PSA_ALG_CCM )
  288. {
  289. status = mbedtls_to_psa_error(
  290. mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
  291. ciphertext_length - operation.tag_length,
  292. nonce, nonce_length,
  293. additional_data,
  294. additional_data_length,
  295. ciphertext, plaintext,
  296. tag, operation.tag_length ) );
  297. }
  298. else
  299. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
  300. #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
  301. if( operation.core_alg == PSA_ALG_GCM )
  302. {
  303. status = mbedtls_to_psa_error(
  304. mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
  305. ciphertext_length - operation.tag_length,
  306. nonce, nonce_length,
  307. additional_data,
  308. additional_data_length,
  309. tag, operation.tag_length,
  310. ciphertext, plaintext ) );
  311. }
  312. else
  313. #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
  314. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
  315. if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
  316. {
  317. if( nonce_length != 12 )
  318. {
  319. if( nonce_length == 8 )
  320. status = PSA_ERROR_NOT_SUPPORTED;
  321. else
  322. status = PSA_ERROR_INVALID_ARGUMENT;
  323. goto exit;
  324. }
  325. if( operation.tag_length != 16 )
  326. {
  327. status = PSA_ERROR_NOT_SUPPORTED;
  328. goto exit;
  329. }
  330. status = mbedtls_to_psa_error(
  331. mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
  332. ciphertext_length - operation.tag_length,
  333. nonce,
  334. additional_data,
  335. additional_data_length,
  336. tag,
  337. ciphertext,
  338. plaintext ) );
  339. }
  340. else
  341. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
  342. {
  343. (void) nonce;
  344. (void) nonce_length;
  345. (void) additional_data;
  346. (void) additional_data_length;
  347. (void) plaintext;
  348. return( PSA_ERROR_NOT_SUPPORTED );
  349. }
  350. if( status == PSA_SUCCESS )
  351. *plaintext_length = ciphertext_length - operation.tag_length;
  352. exit:
  353. psa_aead_abort_internal( &operation );
  354. if( status == PSA_SUCCESS )
  355. *plaintext_length = ciphertext_length - operation.tag_length;
  356. return( status );
  357. }
  358. #endif /* MBEDTLS_PSA_CRYPTO_C */