Path: blob/master/thirdparty/mbedtls/include/mbedtls/aria.h
9903 views
/**1* \file aria.h2*3* \brief ARIA block cipher4*5* The ARIA algorithm is a symmetric block cipher that can encrypt and6* decrypt information. It is defined by the Korean Agency for7* Technology and Standards (KATS) in <em>KS X 1213:2004</em> (in8* Korean, but see http://210.104.33.10/ARIA/index-e.html in English)9* and also described by the IETF in <em>RFC 5794</em>.10*/11/*12* Copyright The Mbed TLS Contributors13* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later14*/1516#ifndef MBEDTLS_ARIA_H17#define MBEDTLS_ARIA_H18#include "mbedtls/private_access.h"1920#include "mbedtls/build_info.h"2122#include <stddef.h>23#include <stdint.h>2425#include "mbedtls/platform_util.h"2627#define MBEDTLS_ARIA_ENCRYPT 1 /**< ARIA encryption. */28#define MBEDTLS_ARIA_DECRYPT 0 /**< ARIA decryption. */2930#define MBEDTLS_ARIA_BLOCKSIZE 16 /**< ARIA block size in bytes. */31#define MBEDTLS_ARIA_MAX_ROUNDS 16 /**< Maximum number of rounds in ARIA. */32#define MBEDTLS_ARIA_MAX_KEYSIZE 32 /**< Maximum size of an ARIA key in bytes. */3334/** Bad input data. */35#define MBEDTLS_ERR_ARIA_BAD_INPUT_DATA -0x005C3637/** Invalid data input length. */38#define MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH -0x005E3940#ifdef __cplusplus41extern "C" {42#endif4344#if !defined(MBEDTLS_ARIA_ALT)45// Regular implementation46//4748/**49* \brief The ARIA context-type definition.50*/51typedef struct mbedtls_aria_context {52unsigned char MBEDTLS_PRIVATE(nr); /*!< The number of rounds (12, 14 or 16) */53/*! The ARIA round keys. */54uint32_t MBEDTLS_PRIVATE(rk)[MBEDTLS_ARIA_MAX_ROUNDS + 1][MBEDTLS_ARIA_BLOCKSIZE / 4];55}56mbedtls_aria_context;5758#else /* MBEDTLS_ARIA_ALT */59#include "aria_alt.h"60#endif /* MBEDTLS_ARIA_ALT */6162/**63* \brief This function initializes the specified ARIA context.64*65* It must be the first API called before using66* the context.67*68* \param ctx The ARIA context to initialize. This must not be \c NULL.69*/70void mbedtls_aria_init(mbedtls_aria_context *ctx);7172/**73* \brief This function releases and clears the specified ARIA context.74*75* \param ctx The ARIA context to clear. This may be \c NULL, in which76* case this function returns immediately. If it is not \c NULL,77* it must point to an initialized ARIA context.78*/79void mbedtls_aria_free(mbedtls_aria_context *ctx);8081/**82* \brief This function sets the encryption key.83*84* \param ctx The ARIA context to which the key should be bound.85* This must be initialized.86* \param key The encryption key. This must be a readable buffer87* of size \p keybits Bits.88* \param keybits The size of \p key in Bits. Valid options are:89* <ul><li>128 bits</li>90* <li>192 bits</li>91* <li>256 bits</li></ul>92*93* \return \c 0 on success.94* \return A negative error code on failure.95*/96int mbedtls_aria_setkey_enc(mbedtls_aria_context *ctx,97const unsigned char *key,98unsigned int keybits);99100#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)101/**102* \brief This function sets the decryption key.103*104* \param ctx The ARIA context to which the key should be bound.105* This must be initialized.106* \param key The decryption key. This must be a readable buffer107* of size \p keybits Bits.108* \param keybits The size of data passed. Valid options are:109* <ul><li>128 bits</li>110* <li>192 bits</li>111* <li>256 bits</li></ul>112*113* \return \c 0 on success.114* \return A negative error code on failure.115*/116int mbedtls_aria_setkey_dec(mbedtls_aria_context *ctx,117const unsigned char *key,118unsigned int keybits);119#endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */120121/**122* \brief This function performs an ARIA single-block encryption or123* decryption operation.124*125* It performs encryption or decryption (depending on whether126* the key was set for encryption on decryption) on the input127* data buffer defined in the \p input parameter.128*129* mbedtls_aria_init(), and either mbedtls_aria_setkey_enc() or130* mbedtls_aria_setkey_dec() must be called before the first131* call to this API with the same context.132*133* \param ctx The ARIA context to use for encryption or decryption.134* This must be initialized and bound to a key.135* \param input The 16-Byte buffer holding the input data.136* \param output The 16-Byte buffer holding the output data.137138* \return \c 0 on success.139* \return A negative error code on failure.140*/141int mbedtls_aria_crypt_ecb(mbedtls_aria_context *ctx,142const unsigned char input[MBEDTLS_ARIA_BLOCKSIZE],143unsigned char output[MBEDTLS_ARIA_BLOCKSIZE]);144145#if defined(MBEDTLS_CIPHER_MODE_CBC)146/**147* \brief This function performs an ARIA-CBC encryption or decryption operation148* on full blocks.149*150* It performs the operation defined in the \p mode151* parameter (encrypt/decrypt), on the input data buffer defined in152* the \p input parameter.153*154* It can be called as many times as needed, until all the input155* data is processed. mbedtls_aria_init(), and either156* mbedtls_aria_setkey_enc() or mbedtls_aria_setkey_dec() must be called157* before the first call to this API with the same context.158*159* \note This function operates on aligned blocks, that is, the input size160* must be a multiple of the ARIA block size of 16 Bytes.161*162* \note Upon exit, the content of the IV is updated so that you can163* call the same function again on the next164* block(s) of data and get the same result as if it was165* encrypted in one call. This allows a "streaming" usage.166* If you need to retain the contents of the IV, you should167* either save it manually or use the cipher module instead.168*169*170* \param ctx The ARIA context to use for encryption or decryption.171* This must be initialized and bound to a key.172* \param mode The mode of operation. This must be either173* #MBEDTLS_ARIA_ENCRYPT for encryption, or174* #MBEDTLS_ARIA_DECRYPT for decryption.175* \param length The length of the input data in Bytes. This must be a176* multiple of the block size (16 Bytes).177* \param iv Initialization vector (updated after use).178* This must be a readable buffer of size 16 Bytes.179* \param input The buffer holding the input data. This must180* be a readable buffer of length \p length Bytes.181* \param output The buffer holding the output data. This must182* be a writable buffer of length \p length Bytes.183*184* \return \c 0 on success.185* \return A negative error code on failure.186*/187int mbedtls_aria_crypt_cbc(mbedtls_aria_context *ctx,188int mode,189size_t length,190unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],191const unsigned char *input,192unsigned char *output);193#endif /* MBEDTLS_CIPHER_MODE_CBC */194195#if defined(MBEDTLS_CIPHER_MODE_CFB)196/**197* \brief This function performs an ARIA-CFB128 encryption or decryption198* operation.199*200* It performs the operation defined in the \p mode201* parameter (encrypt or decrypt), on the input data buffer202* defined in the \p input parameter.203*204* For CFB, you must set up the context with mbedtls_aria_setkey_enc(),205* regardless of whether you are performing an encryption or decryption206* operation, that is, regardless of the \p mode parameter. This is207* because CFB mode uses the same key schedule for encryption and208* decryption.209*210* \note Upon exit, the content of the IV is updated so that you can211* call the same function again on the next212* block(s) of data and get the same result as if it was213* encrypted in one call. This allows a "streaming" usage.214* If you need to retain the contents of the215* IV, you must either save it manually or use the cipher216* module instead.217*218*219* \param ctx The ARIA context to use for encryption or decryption.220* This must be initialized and bound to a key.221* \param mode The mode of operation. This must be either222* #MBEDTLS_ARIA_ENCRYPT for encryption, or223* #MBEDTLS_ARIA_DECRYPT for decryption.224* \param length The length of the input data \p input in Bytes.225* \param iv_off The offset in IV (updated after use).226* This must not be larger than 15.227* \param iv The initialization vector (updated after use).228* This must be a readable buffer of size 16 Bytes.229* \param input The buffer holding the input data. This must230* be a readable buffer of length \p length Bytes.231* \param output The buffer holding the output data. This must232* be a writable buffer of length \p length Bytes.233*234* \return \c 0 on success.235* \return A negative error code on failure.236*/237int mbedtls_aria_crypt_cfb128(mbedtls_aria_context *ctx,238int mode,239size_t length,240size_t *iv_off,241unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE],242const unsigned char *input,243unsigned char *output);244#endif /* MBEDTLS_CIPHER_MODE_CFB */245246#if defined(MBEDTLS_CIPHER_MODE_CTR)247/**248* \brief This function performs an ARIA-CTR encryption or decryption249* operation.250*251* Due to the nature of CTR, you must use the same key schedule252* for both encryption and decryption operations. Therefore, you253* must use the context initialized with mbedtls_aria_setkey_enc()254* for both #MBEDTLS_ARIA_ENCRYPT and #MBEDTLS_ARIA_DECRYPT.255*256* \warning You must never reuse a nonce value with the same key. Doing so257* would void the encryption for the two messages encrypted with258* the same nonce and key.259*260* There are two common strategies for managing nonces with CTR:261*262* 1. You can handle everything as a single message processed over263* successive calls to this function. In that case, you want to264* set \p nonce_counter and \p nc_off to 0 for the first call, and265* then preserve the values of \p nonce_counter, \p nc_off and \p266* stream_block across calls to this function as they will be267* updated by this function.268*269* With this strategy, you must not encrypt more than 2**128270* blocks of data with the same key.271*272* 2. You can encrypt separate messages by dividing the \p273* nonce_counter buffer in two areas: the first one used for a274* per-message nonce, handled by yourself, and the second one275* updated by this function internally.276*277* For example, you might reserve the first 12 bytes for the278* per-message nonce, and the last 4 bytes for internal use. In that279* case, before calling this function on a new message you need to280* set the first 12 bytes of \p nonce_counter to your chosen nonce281* value, the last 4 to 0, and \p nc_off to 0 (which will cause \p282* stream_block to be ignored). That way, you can encrypt at most283* 2**96 messages of up to 2**32 blocks each with the same key.284*285* The per-message nonce (or information sufficient to reconstruct286* it) needs to be communicated with the ciphertext and must be unique.287* The recommended way to ensure uniqueness is to use a message288* counter. An alternative is to generate random nonces, but this289* limits the number of messages that can be securely encrypted:290* for example, with 96-bit random nonces, you should not encrypt291* more than 2**32 messages with the same key.292*293* Note that for both strategies, sizes are measured in blocks and294* that an ARIA block is 16 bytes.295*296* \warning Upon return, \p stream_block contains sensitive data. Its297* content must not be written to insecure storage and should be298* securely discarded as soon as it's no longer needed.299*300* \param ctx The ARIA context to use for encryption or decryption.301* This must be initialized and bound to a key.302* \param length The length of the input data \p input in Bytes.303* \param nc_off The offset in Bytes in the current \p stream_block,304* for resuming within the current cipher stream. The305* offset pointer should be \c 0 at the start of a306* stream. This must not be larger than \c 15 Bytes.307* \param nonce_counter The 128-bit nonce and counter. This must point to308* a read/write buffer of length \c 16 bytes.309* \param stream_block The saved stream block for resuming. This must310* point to a read/write buffer of length \c 16 bytes.311* This is overwritten by the function.312* \param input The buffer holding the input data. This must313* be a readable buffer of length \p length Bytes.314* \param output The buffer holding the output data. This must315* be a writable buffer of length \p length Bytes.316*317* \return \c 0 on success.318* \return A negative error code on failure.319*/320int mbedtls_aria_crypt_ctr(mbedtls_aria_context *ctx,321size_t length,322size_t *nc_off,323unsigned char nonce_counter[MBEDTLS_ARIA_BLOCKSIZE],324unsigned char stream_block[MBEDTLS_ARIA_BLOCKSIZE],325const unsigned char *input,326unsigned char *output);327#endif /* MBEDTLS_CIPHER_MODE_CTR */328329#if defined(MBEDTLS_SELF_TEST)330/**331* \brief Checkup routine.332*333* \return \c 0 on success, or \c 1 on failure.334*/335int mbedtls_aria_self_test(int verbose);336#endif /* MBEDTLS_SELF_TEST */337338#ifdef __cplusplus339}340#endif341342#endif /* aria.h */343344345