blob: f0466bfd7e7a702eff89377435a35d3d852b045e [file] [log] [blame]
Paul Bakker38119b12009-01-10 23:31:23 +00001/**
2 * \file camellia.h
3 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief Camellia block cipher
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000021 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker38119b12009-01-10 23:31:23 +000023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_CAMELLIA_H
25#define MBEDTLS_CAMELLIA_H
Paul Bakker477fd322009-10-04 13:22:13 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker90995b52013-06-24 19:20:35 +020032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020034#include <stdint.h>
Paul Bakkerc81f6c32009-05-03 13:09:15 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#define MBEDTLS_CAMELLIA_ENCRYPT 1
37#define MBEDTLS_CAMELLIA_DECRYPT 0
Paul Bakker38119b12009-01-10 23:31:23 +000038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH -0x0024 /**< Invalid key length. */
40#define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010041#define MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED -0x0027 /**< Camellia hardware accelerator failed. */
Paul Bakker2b222c82009-07-27 21:03:45 +000042
Paul Bakker407a0da2013-06-27 14:29:21 +020043#ifdef __cplusplus
44extern "C" {
45#endif
46
Ron Eldorb2aacec2017-05-18 16:53:08 +030047#if !defined(MBEDTLS_CAMELLIA_ALT)
48// Regular implementation
49//
50
Paul Bakker38119b12009-01-10 23:31:23 +000051/**
52 * \brief CAMELLIA context structure
53 */
54typedef struct
55{
56 int nr; /*!< number of rounds */
Paul Bakkerc81f6c32009-05-03 13:09:15 +000057 uint32_t rk[68]; /*!< CAMELLIA round keys */
Paul Bakker38119b12009-01-10 23:31:23 +000058}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059mbedtls_camellia_context;
Paul Bakker38119b12009-01-10 23:31:23 +000060
Ron Eldorb2aacec2017-05-18 16:53:08 +030061#else /* MBEDTLS_CAMELLIA_ALT */
62#include "camellia_alt.h"
63#endif /* MBEDTLS_CAMELLIA_ALT */
64
Paul Bakker38119b12009-01-10 23:31:23 +000065/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020066 * \brief Initialize CAMELLIA context
67 *
68 * \param ctx CAMELLIA context to be initialized
69 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070void mbedtls_camellia_init( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020071
72/**
73 * \brief Clear CAMELLIA context
74 *
75 * \param ctx CAMELLIA context to be cleared
76 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077void mbedtls_camellia_free( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020078
79/**
Paul Bakker38119b12009-01-10 23:31:23 +000080 * \brief CAMELLIA key schedule (encryption)
81 *
82 * \param ctx CAMELLIA context to be initialized
83 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020084 * \param keybits must be 128, 192 or 256
Paul Bakker9af723c2014-05-01 13:03:14 +020085 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +000087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020089 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +000090
91/**
92 * \brief CAMELLIA key schedule (decryption)
93 *
94 * \param ctx CAMELLIA context to be initialized
95 * \param key decryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020096 * \param keybits must be 128, 192 or 256
Paul Bakker9af723c2014-05-01 13:03:14 +020097 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +000099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200101 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +0000102
103/**
104 * \brief CAMELLIA-ECB block encryption/decryption
105 *
106 * \param ctx CAMELLIA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Paul Bakker38119b12009-01-10 23:31:23 +0000108 * \param input 16-byte input block
109 * \param output 16-byte output block
Paul Bakker9af723c2014-05-01 13:03:14 +0200110 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000111 * \return 0 if successful
Paul Bakker38119b12009-01-10 23:31:23 +0000112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000114 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000115 const unsigned char input[16],
Paul Bakker38119b12009-01-10 23:31:23 +0000116 unsigned char output[16] );
117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker38119b12009-01-10 23:31:23 +0000119/**
120 * \brief CAMELLIA-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +0000121 * Length should be a multiple of the block
122 * size (16 bytes)
Paul Bakker38119b12009-01-10 23:31:23 +0000123 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000124 * \note Upon exit, the content of the IV is updated so that you can
125 * call the function same function again on the following
126 * block(s) of data and get the same result as if it was
127 * encrypted in one call. This allows a "streaming" usage.
128 * If on the other hand you need to retain the contents of the
129 * IV, you should either save it manually or use the cipher
130 * module instead.
131 *
Paul Bakker38119b12009-01-10 23:31:23 +0000132 * \param ctx CAMELLIA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Paul Bakker38119b12009-01-10 23:31:23 +0000134 * \param length length of the input data
135 * \param iv initialization vector (updated after use)
136 * \param input buffer holding the input data
137 * \param output buffer holding the output data
Paul Bakker9af723c2014-05-01 13:03:14 +0200138 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200139 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000143 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000144 size_t length,
Paul Bakker38119b12009-01-10 23:31:23 +0000145 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000146 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000147 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker38119b12009-01-10 23:31:23 +0000149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker38119b12009-01-10 23:31:23 +0000151/**
152 * \brief CAMELLIA-CFB128 buffer encryption/decryption
153 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000154 * Note: Due to the nature of CFB you should use the same key schedule for
155 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000157 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000158 * \note Upon exit, the content of the IV is updated so that you can
159 * call the function same function again on the following
160 * block(s) of data and get the same result as if it was
161 * encrypted in one call. This allows a "streaming" usage.
162 * If on the other hand you need to retain the contents of the
163 * IV, you should either save it manually or use the cipher
164 * module instead.
165 *
Paul Bakker38119b12009-01-10 23:31:23 +0000166 * \param ctx CAMELLIA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Paul Bakker38119b12009-01-10 23:31:23 +0000168 * \param length length of the input data
169 * \param iv_off offset in IV (updated after use)
170 * \param iv initialization vector (updated after use)
171 * \param input buffer holding the input data
172 * \param output buffer holding the output data
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200173 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200174 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000178 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000179 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000180 size_t *iv_off,
Paul Bakker38119b12009-01-10 23:31:23 +0000181 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000182 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000183 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker38119b12009-01-10 23:31:23 +0000185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000187/**
Paul Bakker1ef71df2011-06-09 14:14:58 +0000188 * \brief CAMELLIA-CTR buffer encryption/decryption
189 *
190 * Warning: You have to keep the maximum use of your counter in mind!
191 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000192 * Note: Due to the nature of CTR you should use the same key schedule for
193 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000195 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200196 * \param ctx CAMELLIA context
Paul Bakker1ef71df2011-06-09 14:14:58 +0000197 * \param length The length of the data
198 * \param nc_off The offset in the current stream_block (for resuming
199 * within current cipher stream). The offset pointer to
200 * should be 0 at the start of a stream.
201 * \param nonce_counter The 128-bit nonce and counter.
202 * \param stream_block The saved stream-block for resuming. Is overwritten
203 * by the function.
204 * \param input The input data stream
205 * \param output The output data stream
206 *
207 * \return 0 if successful
208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000210 size_t length,
211 size_t *nc_off,
212 unsigned char nonce_counter[16],
213 unsigned char stream_block[16],
214 const unsigned char *input,
215 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker1ef71df2011-06-09 14:14:58 +0000217
Paul Bakker38119b12009-01-10 23:31:23 +0000218/**
219 * \brief Checkup routine
220 *
221 * \return 0 if successful, or 1 if the test failed
222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223int mbedtls_camellia_self_test( int verbose );
Paul Bakker38119b12009-01-10 23:31:23 +0000224
225#ifdef __cplusplus
226}
227#endif
228
229#endif /* camellia.h */