blob: 2cde2d9a8bc65cc6e9e82d60561a922578a8bd9a [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
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9 *
10 * This file is provided under the Apache License 2.0, or the
11 * GNU General Public License v2.0 or later.
12 *
13 * **********
14 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020015 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000027 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020028 * **********
29 *
30 * **********
31 * GNU General Public License v2.0 or later:
32 *
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
37 *
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46 *
47 * **********
48 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000049 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker38119b12009-01-10 23:31:23 +000050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#ifndef MBEDTLS_CAMELLIA_H
52#define MBEDTLS_CAMELLIA_H
Paul Bakker477fd322009-10-04 13:22:13 +000053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020055#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020058#endif
Paul Bakker90995b52013-06-24 19:20:35 +020059
Rich Evans00ab4702015-02-06 13:43:58 +000060#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020061#include <stdint.h>
Paul Bakkerc81f6c32009-05-03 13:09:15 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#define MBEDTLS_CAMELLIA_ENCRYPT 1
64#define MBEDTLS_CAMELLIA_DECRYPT 0
Paul Bakker38119b12009-01-10 23:31:23 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH -0x0024 /**< Invalid key length. */
67#define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010068#define MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED -0x0027 /**< Camellia hardware accelerator failed. */
Paul Bakker2b222c82009-07-27 21:03:45 +000069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if !defined(MBEDTLS_CAMELLIA_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020071// Regular implementation
72//
73
Paul Bakker407a0da2013-06-27 14:29:21 +020074#ifdef __cplusplus
75extern "C" {
76#endif
77
Paul Bakker38119b12009-01-10 23:31:23 +000078/**
79 * \brief CAMELLIA context structure
80 */
81typedef struct
82{
83 int nr; /*!< number of rounds */
Paul Bakkerc81f6c32009-05-03 13:09:15 +000084 uint32_t rk[68]; /*!< CAMELLIA round keys */
Paul Bakker38119b12009-01-10 23:31:23 +000085}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086mbedtls_camellia_context;
Paul Bakker38119b12009-01-10 23:31:23 +000087
Paul Bakker38119b12009-01-10 23:31:23 +000088/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020089 * \brief Initialize CAMELLIA context
90 *
91 * \param ctx CAMELLIA context to be initialized
92 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093void mbedtls_camellia_init( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020094
95/**
96 * \brief Clear CAMELLIA context
97 *
98 * \param ctx CAMELLIA context to be cleared
99 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100void mbedtls_camellia_free( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200101
102/**
Paul Bakker38119b12009-01-10 23:31:23 +0000103 * \brief CAMELLIA key schedule (encryption)
104 *
105 * \param ctx CAMELLIA context to be initialized
106 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200107 * \param keybits must be 128, 192 or 256
Paul Bakker9af723c2014-05-01 13:03:14 +0200108 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200112 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +0000113
114/**
115 * \brief CAMELLIA key schedule (decryption)
116 *
117 * \param ctx CAMELLIA context to be initialized
118 * \param key decryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200119 * \param keybits must be 128, 192 or 256
Paul Bakker9af723c2014-05-01 13:03:14 +0200120 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200124 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +0000125
126/**
127 * \brief CAMELLIA-ECB block encryption/decryption
128 *
129 * \param ctx CAMELLIA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Paul Bakker38119b12009-01-10 23:31:23 +0000131 * \param input 16-byte input block
132 * \param output 16-byte output block
Paul Bakker9af723c2014-05-01 13:03:14 +0200133 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000134 * \return 0 if successful
Paul Bakker38119b12009-01-10 23:31:23 +0000135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000137 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000138 const unsigned char input[16],
Paul Bakker38119b12009-01-10 23:31:23 +0000139 unsigned char output[16] );
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker38119b12009-01-10 23:31:23 +0000142/**
143 * \brief CAMELLIA-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +0000144 * Length should be a multiple of the block
145 * size (16 bytes)
Paul Bakker38119b12009-01-10 23:31:23 +0000146 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000147 * \note Upon exit, the content of the IV is updated so that you can
148 * call the function same function again on the following
149 * block(s) of data and get the same result as if it was
150 * encrypted in one call. This allows a "streaming" usage.
151 * If on the other hand you need to retain the contents of the
152 * IV, you should either save it manually or use the cipher
153 * module instead.
154 *
Paul Bakker38119b12009-01-10 23:31:23 +0000155 * \param ctx CAMELLIA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Paul Bakker38119b12009-01-10 23:31:23 +0000157 * \param length length of the input data
158 * \param iv initialization vector (updated after use)
159 * \param input buffer holding the input data
160 * \param output buffer holding the output data
Paul Bakker9af723c2014-05-01 13:03:14 +0200161 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200162 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000166 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000167 size_t length,
Paul Bakker38119b12009-01-10 23:31:23 +0000168 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000169 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000170 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker38119b12009-01-10 23:31:23 +0000172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker38119b12009-01-10 23:31:23 +0000174/**
175 * \brief CAMELLIA-CFB128 buffer encryption/decryption
176 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000177 * Note: Due to the nature of CFB you should use the same key schedule for
178 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000180 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000181 * \note Upon exit, the content of the IV is updated so that you can
182 * call the function same function again on the following
183 * block(s) of data and get the same result as if it was
184 * encrypted in one call. This allows a "streaming" usage.
185 * If on the other hand you need to retain the contents of the
186 * IV, you should either save it manually or use the cipher
187 * module instead.
188 *
Paul Bakker38119b12009-01-10 23:31:23 +0000189 * \param ctx CAMELLIA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Paul Bakker38119b12009-01-10 23:31:23 +0000191 * \param length length of the input data
192 * \param iv_off offset in IV (updated after use)
193 * \param iv initialization vector (updated after use)
194 * \param input buffer holding the input data
195 * \param output buffer holding the output data
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200196 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200197 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000201 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000202 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000203 size_t *iv_off,
Paul Bakker38119b12009-01-10 23:31:23 +0000204 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000205 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000206 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker38119b12009-01-10 23:31:23 +0000208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000210/**
Paul Bakker1ef71df2011-06-09 14:14:58 +0000211 * \brief CAMELLIA-CTR buffer encryption/decryption
212 *
213 * Warning: You have to keep the maximum use of your counter in mind!
214 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000215 * Note: Due to the nature of CTR you should use the same key schedule for
216 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000218 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200219 * \param ctx CAMELLIA context
Paul Bakker1ef71df2011-06-09 14:14:58 +0000220 * \param length The length of the data
221 * \param nc_off The offset in the current stream_block (for resuming
222 * within current cipher stream). The offset pointer to
223 * should be 0 at the start of a stream.
224 * \param nonce_counter The 128-bit nonce and counter.
225 * \param stream_block The saved stream-block for resuming. Is overwritten
226 * by the function.
227 * \param input The input data stream
228 * \param output The output data stream
229 *
230 * \return 0 if successful
231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000233 size_t length,
234 size_t *nc_off,
235 unsigned char nonce_counter[16],
236 unsigned char stream_block[16],
237 const unsigned char *input,
238 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker1ef71df2011-06-09 14:14:58 +0000240
Paul Bakker90995b52013-06-24 19:20:35 +0200241#ifdef __cplusplus
242}
243#endif
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#else /* MBEDTLS_CAMELLIA_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200246#include "camellia_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#endif /* MBEDTLS_CAMELLIA_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200248
249#ifdef __cplusplus
250extern "C" {
251#endif
252
Paul Bakker38119b12009-01-10 23:31:23 +0000253/**
254 * \brief Checkup routine
255 *
256 * \return 0 if successful, or 1 if the test failed
257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258int mbedtls_camellia_self_test( int verbose );
Paul Bakker38119b12009-01-10 23:31:23 +0000259
260#ifdef __cplusplus
261}
262#endif
263
264#endif /* camellia.h */