mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-07-30 22:43:08 +03:00
Merge pull request #8519 from mpg/block-cipher
[G2] Add internal module block_cipher
This commit is contained in:
@ -22,6 +22,7 @@ set(src_crypto
|
||||
bignum_core.c
|
||||
bignum_mod.c
|
||||
bignum_mod_raw.c
|
||||
block_cipher.c
|
||||
camellia.c
|
||||
ccm.c
|
||||
chacha20.c
|
||||
|
@ -91,6 +91,7 @@ OBJS_CRYPTO= \
|
||||
bignum_core.o \
|
||||
bignum_mod.o \
|
||||
bignum_mod_raw.o \
|
||||
block_cipher.o \
|
||||
camellia.o \
|
||||
ccm.o \
|
||||
chacha20.o \
|
||||
|
116
library/block_cipher.c
Normal file
116
library/block_cipher.c
Normal file
@ -0,0 +1,116 @@
|
||||
/**
|
||||
* \file block_cipher.c
|
||||
*
|
||||
* \brief Lightweight abstraction layer for block ciphers with 128 bit blocks,
|
||||
* for use by the GCM and CCM modules.
|
||||
*/
|
||||
/*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "block_cipher_internal.h"
|
||||
|
||||
#if defined(MBEDTLS_BLOCK_CIPHER_C)
|
||||
|
||||
void mbedtls_block_cipher_free(mbedtls_block_cipher_context_t *ctx)
|
||||
{
|
||||
switch (ctx->id) {
|
||||
#if defined(MBEDTLS_AES_C)
|
||||
case MBEDTLS_BLOCK_CIPHER_ID_AES:
|
||||
mbedtls_aes_free(&ctx->ctx.aes);
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_ARIA_C)
|
||||
case MBEDTLS_BLOCK_CIPHER_ID_ARIA:
|
||||
mbedtls_aria_free(&ctx->ctx.aria);
|
||||
break;
|
||||
#endif
|
||||
#if defined(MBEDTLS_CAMELLIA_C)
|
||||
case MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA:
|
||||
mbedtls_camellia_free(&ctx->ctx.camellia);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
break;
|
||||
}
|
||||
ctx->id = MBEDTLS_BLOCK_CIPHER_ID_NONE;
|
||||
}
|
||||
|
||||
int mbedtls_block_cipher_setup(mbedtls_block_cipher_context_t *ctx,
|
||||
mbedtls_cipher_id_t cipher_id)
|
||||
{
|
||||
switch (cipher_id) {
|
||||
#if defined(MBEDTLS_AES_C)
|
||||
case MBEDTLS_CIPHER_ID_AES:
|
||||
ctx->id = MBEDTLS_BLOCK_CIPHER_ID_AES;
|
||||
mbedtls_aes_init(&ctx->ctx.aes);
|
||||
return 0;
|
||||
#endif
|
||||
#if defined(MBEDTLS_ARIA_C)
|
||||
case MBEDTLS_CIPHER_ID_ARIA:
|
||||
ctx->id = MBEDTLS_BLOCK_CIPHER_ID_ARIA;
|
||||
mbedtls_aria_init(&ctx->ctx.aria);
|
||||
return 0;
|
||||
#endif
|
||||
#if defined(MBEDTLS_CAMELLIA_C)
|
||||
case MBEDTLS_CIPHER_ID_CAMELLIA:
|
||||
ctx->id = MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA;
|
||||
mbedtls_camellia_init(&ctx->ctx.camellia);
|
||||
return 0;
|
||||
#endif
|
||||
default:
|
||||
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
|
||||
}
|
||||
}
|
||||
|
||||
int mbedtls_block_cipher_setkey(mbedtls_block_cipher_context_t *ctx,
|
||||
const unsigned char *key,
|
||||
unsigned key_bitlen)
|
||||
{
|
||||
switch (ctx->id) {
|
||||
#if defined(MBEDTLS_AES_C)
|
||||
case MBEDTLS_BLOCK_CIPHER_ID_AES:
|
||||
return mbedtls_aes_setkey_enc(&ctx->ctx.aes, key, key_bitlen);
|
||||
#endif
|
||||
#if defined(MBEDTLS_ARIA_C)
|
||||
case MBEDTLS_BLOCK_CIPHER_ID_ARIA:
|
||||
return mbedtls_aria_setkey_enc(&ctx->ctx.aria, key, key_bitlen);
|
||||
#endif
|
||||
#if defined(MBEDTLS_CAMELLIA_C)
|
||||
case MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA:
|
||||
return mbedtls_camellia_setkey_enc(&ctx->ctx.camellia, key, key_bitlen);
|
||||
#endif
|
||||
default:
|
||||
return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
|
||||
}
|
||||
}
|
||||
|
||||
int mbedtls_block_cipher_encrypt(mbedtls_block_cipher_context_t *ctx,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16])
|
||||
{
|
||||
switch (ctx->id) {
|
||||
#if defined(MBEDTLS_AES_C)
|
||||
case MBEDTLS_BLOCK_CIPHER_ID_AES:
|
||||
return mbedtls_aes_crypt_ecb(&ctx->ctx.aes, MBEDTLS_AES_ENCRYPT,
|
||||
input, output);
|
||||
#endif
|
||||
#if defined(MBEDTLS_ARIA_C)
|
||||
case MBEDTLS_BLOCK_CIPHER_ID_ARIA:
|
||||
return mbedtls_aria_crypt_ecb(&ctx->ctx.aria, input, output);
|
||||
#endif
|
||||
#if defined(MBEDTLS_CAMELLIA_C)
|
||||
case MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA:
|
||||
return mbedtls_camellia_crypt_ecb(&ctx->ctx.camellia,
|
||||
MBEDTLS_CAMELLIA_ENCRYPT,
|
||||
input, output);
|
||||
#endif
|
||||
default:
|
||||
return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_BLOCK_CIPHER_C */
|
99
library/block_cipher_internal.h
Normal file
99
library/block_cipher_internal.h
Normal file
@ -0,0 +1,99 @@
|
||||
/**
|
||||
* \file block_cipher_internal.h
|
||||
*
|
||||
* \brief Lightweight abstraction layer for block ciphers with 128 bit blocks,
|
||||
* for use by the GCM and CCM modules.
|
||||
*/
|
||||
/*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
||||
*/
|
||||
#ifndef MBEDTLS_BLOCK_CIPHER_INTERNAL_H
|
||||
#define MBEDTLS_BLOCK_CIPHER_INTERNAL_H
|
||||
|
||||
#include "mbedtls/build_info.h"
|
||||
|
||||
#include "mbedtls/cipher.h"
|
||||
|
||||
#include "mbedtls/block_cipher.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief Initialize the context.
|
||||
* This must be the first API call before using the context.
|
||||
*
|
||||
* \param ctx The context to initialize.
|
||||
*/
|
||||
static inline void mbedtls_block_cipher_init(mbedtls_block_cipher_context_t *ctx)
|
||||
{
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Set the block cipher to use with this context.
|
||||
* This must be called after mbedtls_block_cipher_init().
|
||||
*
|
||||
* \param ctx The context to set up.
|
||||
* \param cipher_id The identifier of the cipher to use.
|
||||
* This must be either AES, ARIA or Camellia.
|
||||
* Warning: this is a ::mbedtls_cipher_id_t,
|
||||
* not a ::mbedtls_block_cipher_id_t!
|
||||
*
|
||||
* \retval \c 0 on success.
|
||||
* \retval #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if \p cipher_id was
|
||||
* invalid.
|
||||
*/
|
||||
int mbedtls_block_cipher_setup(mbedtls_block_cipher_context_t *ctx,
|
||||
mbedtls_cipher_id_t cipher_id);
|
||||
|
||||
/**
|
||||
* \brief Set the key into the context.
|
||||
*
|
||||
* \param ctx The context to configure.
|
||||
* \param key The buffer holding the key material.
|
||||
* \param key_bitlen The size of the key in bits.
|
||||
*
|
||||
* \retval \c 0 on success.
|
||||
* \retval #MBEDTLS_ERR_CIPHER_INVALID_CONTEXT if the context was not
|
||||
* properly set up before calling this function.
|
||||
* \retval One of #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH,
|
||||
* #MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
|
||||
* #MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA if \p key_bitlen is
|
||||
* invalid.
|
||||
*/
|
||||
int mbedtls_block_cipher_setkey(mbedtls_block_cipher_context_t *ctx,
|
||||
const unsigned char *key,
|
||||
unsigned key_bitlen);
|
||||
|
||||
/**
|
||||
* \brief Encrypt one block (16 bytes) with the configured key.
|
||||
*
|
||||
* \param ctx The context holding the key.
|
||||
* \param input The buffer holding the input block. Must be 16 bytes.
|
||||
* \param output The buffer to which the output block will be written.
|
||||
* Must be writable and 16 bytes long.
|
||||
* This must either not overlap with \p input, or be equal.
|
||||
*
|
||||
* \retval \c 0 on success.
|
||||
* \retval #MBEDTLS_ERR_CIPHER_INVALID_CONTEXT if the context was not
|
||||
* properly set up before calling this function.
|
||||
* \retval Another negative value if encryption failed.
|
||||
*/
|
||||
int mbedtls_block_cipher_encrypt(mbedtls_block_cipher_context_t *ctx,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16]);
|
||||
/**
|
||||
* \brief Clear the context.
|
||||
*
|
||||
* \param ctx The context to clear.
|
||||
*/
|
||||
void mbedtls_block_cipher_free(mbedtls_block_cipher_context_t *ctx);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* MBEDTLS_BLOCK_CIPHER_INTERNAL_H */
|
Reference in New Issue
Block a user