blob: 1ed0ca4bd7cb33fdef43315e67e77c528794674b [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/*
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02007 * Copyright The Mbed TLS Contributors
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 * **********
Paul Bakker38119b12009-01-10 23:31:23 +000048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#ifndef MBEDTLS_CAMELLIA_H
50#define MBEDTLS_CAMELLIA_H
Paul Bakker477fd322009-10-04 13:22:13 +000051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020053#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020054#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#endif
Paul Bakker90995b52013-06-24 19:20:35 +020057
Rich Evans00ab4702015-02-06 13:43:58 +000058#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020059#include <stdint.h>
Paul Bakkerc81f6c32009-05-03 13:09:15 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#define MBEDTLS_CAMELLIA_ENCRYPT 1
62#define MBEDTLS_CAMELLIA_DECRYPT 0
Paul Bakker38119b12009-01-10 23:31:23 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH -0x0024 /**< Invalid key length. */
65#define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010066#define MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED -0x0027 /**< Camellia hardware accelerator failed. */
Paul Bakker2b222c82009-07-27 21:03:45 +000067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if !defined(MBEDTLS_CAMELLIA_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020069// Regular implementation
70//
71
Paul Bakker407a0da2013-06-27 14:29:21 +020072#ifdef __cplusplus
73extern "C" {
74#endif
75
Paul Bakker38119b12009-01-10 23:31:23 +000076/**
77 * \brief CAMELLIA context structure
78 */
79typedef struct
80{
81 int nr; /*!< number of rounds */
Paul Bakkerc81f6c32009-05-03 13:09:15 +000082 uint32_t rk[68]; /*!< CAMELLIA round keys */
Paul Bakker38119b12009-01-10 23:31:23 +000083}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084mbedtls_camellia_context;
Paul Bakker38119b12009-01-10 23:31:23 +000085
Paul Bakker38119b12009-01-10 23:31:23 +000086/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020087 * \brief Initialize CAMELLIA context
88 *
89 * \param ctx CAMELLIA context to be initialized
90 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091void mbedtls_camellia_init( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020092
93/**
94 * \brief Clear CAMELLIA context
95 *
96 * \param ctx CAMELLIA context to be cleared
97 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098void mbedtls_camellia_free( mbedtls_camellia_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020099
100/**
Paul Bakker38119b12009-01-10 23:31:23 +0000101 * \brief CAMELLIA key schedule (encryption)
102 *
103 * \param ctx CAMELLIA context to be initialized
104 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200105 * \param keybits must be 128, 192 or 256
Paul Bakker9af723c2014-05-01 13:03:14 +0200106 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200110 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +0000111
112/**
113 * \brief CAMELLIA key schedule (decryption)
114 *
115 * \param ctx CAMELLIA context to be initialized
116 * \param key decryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200117 * \param keybits must be 128, 192 or 256
Paul Bakker9af723c2014-05-01 13:03:14 +0200118 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 * \return 0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200122 unsigned int keybits );
Paul Bakker38119b12009-01-10 23:31:23 +0000123
124/**
125 * \brief CAMELLIA-ECB block encryption/decryption
126 *
127 * \param ctx CAMELLIA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Paul Bakker38119b12009-01-10 23:31:23 +0000129 * \param input 16-byte input block
130 * \param output 16-byte output block
Paul Bakker9af723c2014-05-01 13:03:14 +0200131 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000132 * \return 0 if successful
Paul Bakker38119b12009-01-10 23:31:23 +0000133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000135 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000136 const unsigned char input[16],
Paul Bakker38119b12009-01-10 23:31:23 +0000137 unsigned char output[16] );
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker38119b12009-01-10 23:31:23 +0000140/**
141 * \brief CAMELLIA-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +0000142 * Length should be a multiple of the block
143 * size (16 bytes)
Paul Bakker38119b12009-01-10 23:31:23 +0000144 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000145 * \note Upon exit, the content of the IV is updated so that you can
146 * call the function same function again on the following
147 * block(s) of data and get the same result as if it was
148 * encrypted in one call. This allows a "streaming" usage.
149 * If on the other hand you need to retain the contents of the
150 * IV, you should either save it manually or use the cipher
151 * module instead.
152 *
Paul Bakker38119b12009-01-10 23:31:23 +0000153 * \param ctx CAMELLIA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Paul Bakker38119b12009-01-10 23:31:23 +0000155 * \param length length of the input data
156 * \param iv initialization vector (updated after use)
157 * \param input buffer holding the input data
158 * \param output buffer holding the output data
Paul Bakker9af723c2014-05-01 13:03:14 +0200159 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200160 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000162 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000164 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000165 size_t length,
Paul Bakker38119b12009-01-10 23:31:23 +0000166 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000167 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000168 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker38119b12009-01-10 23:31:23 +0000170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker38119b12009-01-10 23:31:23 +0000172/**
173 * \brief CAMELLIA-CFB128 buffer encryption/decryption
174 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000175 * Note: Due to the nature of CFB you should use the same key schedule for
176 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000178 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000179 * \note Upon exit, the content of the IV is updated so that you can
180 * call the function same function again on the following
181 * block(s) of data and get the same result as if it was
182 * encrypted in one call. This allows a "streaming" usage.
183 * If on the other hand you need to retain the contents of the
184 * IV, you should either save it manually or use the cipher
185 * module instead.
186 *
Paul Bakker38119b12009-01-10 23:31:23 +0000187 * \param ctx CAMELLIA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 * \param mode MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT
Paul Bakker38119b12009-01-10 23:31:23 +0000189 * \param length length of the input data
190 * \param iv_off offset in IV (updated after use)
191 * \param iv initialization vector (updated after use)
192 * \param input buffer holding the input data
193 * \param output buffer holding the output data
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200194 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200195 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 * MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH
Paul Bakker38119b12009-01-10 23:31:23 +0000197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,
Paul Bakker38119b12009-01-10 23:31:23 +0000199 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000200 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000201 size_t *iv_off,
Paul Bakker38119b12009-01-10 23:31:23 +0000202 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000203 const unsigned char *input,
Paul Bakker38119b12009-01-10 23:31:23 +0000204 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker38119b12009-01-10 23:31:23 +0000206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker9a736322012-11-14 12:39:52 +0000208/**
Paul Bakker1ef71df2011-06-09 14:14:58 +0000209 * \brief CAMELLIA-CTR buffer encryption/decryption
210 *
211 * Warning: You have to keep the maximum use of your counter in mind!
212 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000213 * Note: Due to the nature of CTR you should use the same key schedule for
214 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and MBEDTLS_CAMELLIA_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000216 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200217 * \param ctx CAMELLIA context
Paul Bakker1ef71df2011-06-09 14:14:58 +0000218 * \param length The length of the data
219 * \param nc_off The offset in the current stream_block (for resuming
220 * within current cipher stream). The offset pointer to
221 * should be 0 at the start of a stream.
222 * \param nonce_counter The 128-bit nonce and counter.
223 * \param stream_block The saved stream-block for resuming. Is overwritten
224 * by the function.
225 * \param input The input data stream
226 * \param output The output data stream
227 *
228 * \return 0 if successful
229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000231 size_t length,
232 size_t *nc_off,
233 unsigned char nonce_counter[16],
234 unsigned char stream_block[16],
235 const unsigned char *input,
236 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker1ef71df2011-06-09 14:14:58 +0000238
Paul Bakker90995b52013-06-24 19:20:35 +0200239#ifdef __cplusplus
240}
241#endif
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#else /* MBEDTLS_CAMELLIA_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200244#include "camellia_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#endif /* MBEDTLS_CAMELLIA_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200246
247#ifdef __cplusplus
248extern "C" {
249#endif
250
Paul Bakker38119b12009-01-10 23:31:23 +0000251/**
252 * \brief Checkup routine
253 *
254 * \return 0 if successful, or 1 if the test failed
255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256int mbedtls_camellia_self_test( int verbose );
Paul Bakker38119b12009-01-10 23:31:23 +0000257
258#ifdef __cplusplus
259}
260#endif
261
262#endif /* camellia.h */