blob: 1118d3abbb36fd63f740903d130d96b3a923b59b [file] [log] [blame]
Manuel Pégourié-Gonnard21718762023-11-10 11:21:17 +01001/**
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
12#include "common.h"
13
14#include "block_cipher_internal.h"
15
16#if defined(MBEDTLS_BLOCK_CIPHER_C)
17
18void mbedtls_block_cipher_free(mbedtls_block_cipher_context_t *ctx)
19{
20 switch (ctx->id) {
21#if defined(MBEDTLS_AES_C)
22 case MBEDTLS_BLOCK_CIPHER_ID_AES:
23 mbedtls_aes_free(&ctx->ctx.aes);
24 break;
25#endif
26#if defined(MBEDTLS_ARIA_C)
27 case MBEDTLS_BLOCK_CIPHER_ID_ARIA:
28 mbedtls_aria_free(&ctx->ctx.aria);
29 break;
30#endif
31#if defined(MBEDTLS_CAMELLIA_C)
32 case MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA:
33 mbedtls_camellia_free(&ctx->ctx.camellia);
34 break;
35#endif
36 default:
37 break;
38 }
39 ctx->id = MBEDTLS_BLOCK_CIPHER_ID_NONE;
40}
41
42int mbedtls_block_cipher_setup(mbedtls_block_cipher_context_t *ctx,
43 mbedtls_cipher_id_t cipher_id)
44{
45 switch (cipher_id) {
46#if defined(MBEDTLS_AES_C)
47 case MBEDTLS_CIPHER_ID_AES:
48 ctx->id = MBEDTLS_BLOCK_CIPHER_ID_AES;
49 mbedtls_aes_init(&ctx->ctx.aes);
50 return 0;
51#endif
52#if defined(MBEDTLS_ARIA_C)
53 case MBEDTLS_CIPHER_ID_ARIA:
54 ctx->id = MBEDTLS_BLOCK_CIPHER_ID_ARIA;
55 mbedtls_aria_init(&ctx->ctx.aria);
56 return 0;
57#endif
58#if defined(MBEDTLS_CAMELLIA_C)
59 case MBEDTLS_CIPHER_ID_CAMELLIA:
60 ctx->id = MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA;
61 mbedtls_camellia_init(&ctx->ctx.camellia);
62 return 0;
63#endif
64 default:
65 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
66 }
67}
68
Manuel Pégourié-Gonnard3e0884f2023-11-10 11:52:10 +010069int mbedtls_block_cipher_setkey(mbedtls_block_cipher_context_t *ctx,
70 const unsigned char *key,
71 unsigned key_bitlen)
72{
73 switch (ctx->id) {
74#if defined(MBEDTLS_AES_C)
75 case MBEDTLS_BLOCK_CIPHER_ID_AES:
76 return mbedtls_aes_setkey_enc(&ctx->ctx.aes, key, key_bitlen);
77#endif
78#if defined(MBEDTLS_ARIA_C)
79 case MBEDTLS_BLOCK_CIPHER_ID_ARIA:
80 return mbedtls_aria_setkey_enc(&ctx->ctx.aria, key, key_bitlen);
81#endif
82#if defined(MBEDTLS_CAMELLIA_C)
83 case MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA:
84 return mbedtls_camellia_setkey_enc(&ctx->ctx.camellia, key, key_bitlen);
85#endif
86 default:
87 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
88 }
89}
Manuel Pégourié-Gonnard76fa16c2023-11-10 12:02:53 +010090
91int mbedtls_block_cipher_encrypt(mbedtls_block_cipher_context_t *ctx,
92 const unsigned char input[16],
93 unsigned char output[16])
94{
95 switch (ctx->id) {
96#if defined(MBEDTLS_AES_C)
97 case MBEDTLS_BLOCK_CIPHER_ID_AES:
98 return mbedtls_aes_crypt_ecb(&ctx->ctx.aes, MBEDTLS_AES_ENCRYPT,
99 input, output);
100#endif
101#if defined(MBEDTLS_ARIA_C)
102 case MBEDTLS_BLOCK_CIPHER_ID_ARIA:
103 return mbedtls_aria_crypt_ecb(&ctx->ctx.aria, input, output);
104#endif
105#if defined(MBEDTLS_CAMELLIA_C)
106 case MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA:
107 return mbedtls_camellia_crypt_ecb(&ctx->ctx.camellia,
108 MBEDTLS_CAMELLIA_ENCRYPT,
109 input, output);
110#endif
111 default:
112 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
113 }
114}
115
Manuel Pégourié-Gonnard21718762023-11-10 11:21:17 +0100116#endif /* MBEDTLS_BLOCK_CIPHER_C */