Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file camellia.h |
| 3 | * |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 4 | * \brief Camellia block cipher |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 5 | */ |
| 6 | /* |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 8 | * 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é-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 15 | * |
| 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 Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 27 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 28 | * ********** |
| 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é-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 49 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 50 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | #ifndef MBEDTLS_CAMELLIA_H |
| 52 | #define MBEDTLS_CAMELLIA_H |
Paul Bakker | 477fd32 | 2009-10-04 13:22:13 +0000 | [diff] [blame] | 53 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | #if !defined(MBEDTLS_CONFIG_FILE) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 55 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 56 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 57 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 58 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 59 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 60 | #include <stddef.h> |
Manuel Pégourié-Gonnard | ab22910 | 2015-04-15 11:53:16 +0200 | [diff] [blame] | 61 | #include <stdint.h> |
Paul Bakker | c81f6c3 | 2009-05-03 13:09:15 +0000 | [diff] [blame] | 62 | |
Hanno Becker | 4c029d0 | 2018-12-17 13:20:05 +0000 | [diff] [blame] | 63 | #include "platform_util.h" |
| 64 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | #define MBEDTLS_CAMELLIA_ENCRYPT 1 |
| 66 | #define MBEDTLS_CAMELLIA_DECRYPT 0 |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 67 | |
Hanno Becker | 4c029d0 | 2018-12-17 13:20:05 +0000 | [diff] [blame] | 68 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 69 | #define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x0024 ) |
Hanno Becker | 4c029d0 | 2018-12-17 13:20:05 +0000 | [diff] [blame] | 70 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 71 | #define MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA -0x0024 /**< Bad input data. */ |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 72 | |
Hanno Becker | 938f9e9 | 2018-12-18 09:40:25 +0000 | [diff] [blame] | 73 | #define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */ |
| 74 | |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 75 | /* MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED is deprecated and should not be used. |
| 76 | */ |
Gilles Peskine | 7ecab3d | 2018-01-26 17:56:38 +0100 | [diff] [blame] | 77 | #define MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED -0x0027 /**< Camellia hardware accelerator failed. */ |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 78 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 79 | #ifdef __cplusplus |
| 80 | extern "C" { |
| 81 | #endif |
| 82 | |
Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 83 | #if !defined(MBEDTLS_CAMELLIA_ALT) |
| 84 | // Regular implementation |
| 85 | // |
| 86 | |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 87 | /** |
| 88 | * \brief CAMELLIA context structure |
| 89 | */ |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 90 | typedef struct mbedtls_camellia_context |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 91 | { |
| 92 | int nr; /*!< number of rounds */ |
Paul Bakker | c81f6c3 | 2009-05-03 13:09:15 +0000 | [diff] [blame] | 93 | uint32_t rk[68]; /*!< CAMELLIA round keys */ |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 94 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 95 | mbedtls_camellia_context; |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 96 | |
Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 97 | #else /* MBEDTLS_CAMELLIA_ALT */ |
| 98 | #include "camellia_alt.h" |
| 99 | #endif /* MBEDTLS_CAMELLIA_ALT */ |
| 100 | |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 101 | /** |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 102 | * \brief Initialize a CAMELLIA context. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 103 | * |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 104 | * \param ctx The CAMELLIA context to be initialized. |
| 105 | * This must not be \c NULL. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 106 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 107 | void mbedtls_camellia_init( mbedtls_camellia_context *ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 108 | |
| 109 | /** |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 110 | * \brief Clear a CAMELLIA context. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 111 | * |
Hanno Becker | f10905a | 2018-12-13 15:15:36 +0000 | [diff] [blame] | 112 | * \param ctx The CAMELLIA context to be cleared. This may be \c NULL, |
Hanno Becker | c7579ec | 2018-12-17 15:18:02 +0000 | [diff] [blame] | 113 | * in which case this function returns immediately. If it is not |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 114 | * \c NULL, it must be initialized. |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 115 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 116 | void mbedtls_camellia_free( mbedtls_camellia_context *ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 117 | |
| 118 | /** |
Hanno Becker | 70ded36 | 2018-12-19 13:42:05 +0000 | [diff] [blame] | 119 | * \brief Perform a CAMELLIA key schedule operation for encryption. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 120 | * |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 121 | * \param ctx The CAMELLIA context to use. This must be initialized. |
Hanno Becker | f10905a | 2018-12-13 15:15:36 +0000 | [diff] [blame] | 122 | * \param key The encryption key to use. This must be a readable buffer |
Hanno Becker | e939de7 | 2018-12-13 15:39:24 +0000 | [diff] [blame] | 123 | * of size \p keybits Bits. |
Hanno Becker | f10905a | 2018-12-13 15:15:36 +0000 | [diff] [blame] | 124 | * \param keybits The length of \p key in Bits. This must be either \c 128, |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 125 | * \c 192 or \c 256. |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 126 | * |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 127 | * \return \c 0 if successful. |
| 128 | * \return A negative error code on failure. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 129 | */ |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 130 | int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, |
| 131 | const unsigned char *key, |
| 132 | unsigned int keybits ); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 133 | |
| 134 | /** |
Hanno Becker | 70ded36 | 2018-12-19 13:42:05 +0000 | [diff] [blame] | 135 | * \brief Perform a CAMELLIA key schedule operation for decryption. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 136 | * |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 137 | * \param ctx The CAMELLIA context to use. This must be initialized. |
Hanno Becker | f10905a | 2018-12-13 15:15:36 +0000 | [diff] [blame] | 138 | * \param key The decryption key. This must be a readable buffer |
Hanno Becker | e939de7 | 2018-12-13 15:39:24 +0000 | [diff] [blame] | 139 | * of size \p keybits Bits. |
Hanno Becker | f10905a | 2018-12-13 15:15:36 +0000 | [diff] [blame] | 140 | * \param keybits The length of \p key in Bits. This must be either \c 128, |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 141 | * \c 192 or \c 256. |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 142 | * |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 143 | * \return \c 0 if successful. |
| 144 | * \return A negative error code on failure. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 145 | */ |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 146 | int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, |
| 147 | const unsigned char *key, |
| 148 | unsigned int keybits ); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 149 | |
| 150 | /** |
Hanno Becker | 70ded36 | 2018-12-19 13:42:05 +0000 | [diff] [blame] | 151 | * \brief Perform a CAMELLIA-ECB block encryption/decryption operation. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 152 | * |
Hanno Becker | f10905a | 2018-12-13 15:15:36 +0000 | [diff] [blame] | 153 | * \param ctx The CAMELLIA context to use. This must be initialized |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 154 | * and bound to a key. |
Hanno Becker | f10905a | 2018-12-13 15:15:36 +0000 | [diff] [blame] | 155 | * \param mode The mode of operation. This must be either |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 156 | * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. |
Hanno Becker | f10905a | 2018-12-13 15:15:36 +0000 | [diff] [blame] | 157 | * \param input The input block. This must be a readable buffer |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 158 | * of size \c 16 Bytes. |
Hanno Becker | f10905a | 2018-12-13 15:15:36 +0000 | [diff] [blame] | 159 | * \param output The output block. This must be a writable buffer |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 160 | * of size \c 16 Bytes. |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 161 | * |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 162 | * \return \c 0 if successful. |
| 163 | * \return A negative error code on failure. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 164 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 165 | int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx, |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 166 | int mode, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 167 | const unsigned char input[16], |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 168 | unsigned char output[16] ); |
| 169 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 170 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 171 | /** |
Hanno Becker | 70ded36 | 2018-12-19 13:42:05 +0000 | [diff] [blame] | 172 | * \brief Perform a CAMELLIA-CBC buffer encryption/decryption operation. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 173 | * |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 174 | * \note Upon exit, the content of the IV is updated so that you can |
| 175 | * call the function same function again on the following |
| 176 | * block(s) of data and get the same result as if it was |
| 177 | * encrypted in one call. This allows a "streaming" usage. |
| 178 | * If on the other hand you need to retain the contents of the |
| 179 | * IV, you should either save it manually or use the cipher |
| 180 | * module instead. |
| 181 | * |
Hanno Becker | f10905a | 2018-12-13 15:15:36 +0000 | [diff] [blame] | 182 | * \param ctx The CAMELLIA context to use. This must be initialized |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 183 | * and bound to a key. |
Hanno Becker | e939de7 | 2018-12-13 15:39:24 +0000 | [diff] [blame] | 184 | * \param mode The mode of operation. This must be either |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 185 | * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. |
Hanno Becker | af4b83b | 2018-12-17 09:30:27 +0000 | [diff] [blame] | 186 | * \param length The length in Bytes of the input data \p input. |
Hanno Becker | 70ded36 | 2018-12-19 13:42:05 +0000 | [diff] [blame] | 187 | * This must be a multiple of \c 16 Bytes. |
Hanno Becker | c7579ec | 2018-12-17 15:18:02 +0000 | [diff] [blame] | 188 | * \param iv The initialization vector. This must be a read/write buffer |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 189 | * of length \c 16 Bytes. It is updated to allow streaming |
| 190 | * use as explained above. |
Hanno Becker | f10905a | 2018-12-13 15:15:36 +0000 | [diff] [blame] | 191 | * \param input The buffer holding the input data. This must point to a |
Hanno Becker | bdb7cd4 | 2018-12-18 17:49:48 +0000 | [diff] [blame] | 192 | * readable buffer of length \p length Bytes. |
Hanno Becker | af4b83b | 2018-12-17 09:30:27 +0000 | [diff] [blame] | 193 | * \param output The buffer holding the output data. This must point to a |
Hanno Becker | bdb7cd4 | 2018-12-18 17:49:48 +0000 | [diff] [blame] | 194 | * writable buffer of length \p length Bytes. |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 195 | * |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 196 | * \return \c 0 if successful. |
| 197 | * \return A negative error code on failure. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 198 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx, |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 200 | int mode, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 201 | size_t length, |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 202 | unsigned char iv[16], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 203 | const unsigned char *input, |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 204 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 205 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 206 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 207 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 208 | /** |
Hanno Becker | 70ded36 | 2018-12-19 13:42:05 +0000 | [diff] [blame] | 209 | * \brief Perform a CAMELLIA-CFB128 buffer encryption/decryption |
| 210 | * operation. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 211 | * |
Hanno Becker | 70ded36 | 2018-12-19 13:42:05 +0000 | [diff] [blame] | 212 | * \note Due to the nature of CFB mode, you should use the same |
| 213 | * key for both encryption and decryption. In particular, calls |
| 214 | * to this function should be preceded by a key-schedule via |
| 215 | * mbedtls_camellia_setkey_enc() regardless of whether \p mode |
| 216 | * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. |
Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 217 | * |
Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 218 | * \note Upon exit, the content of the IV is updated so that you can |
| 219 | * call the function same function again on the following |
| 220 | * block(s) of data and get the same result as if it was |
| 221 | * encrypted in one call. This allows a "streaming" usage. |
| 222 | * If on the other hand you need to retain the contents of the |
| 223 | * IV, you should either save it manually or use the cipher |
| 224 | * module instead. |
| 225 | * |
Hanno Becker | f10905a | 2018-12-13 15:15:36 +0000 | [diff] [blame] | 226 | * \param ctx The CAMELLIA context to use. This must be initialized |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 227 | * and bound to a key. |
Hanno Becker | e939de7 | 2018-12-13 15:39:24 +0000 | [diff] [blame] | 228 | * \param mode The mode of operation. This must be either |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 229 | * #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. |
Hanno Becker | af4b83b | 2018-12-17 09:30:27 +0000 | [diff] [blame] | 230 | * \param length The length of the input data \p input. Any value is allowed. |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 231 | * \param iv_off The current offset in the IV. This must be smaller |
Hanno Becker | c7579ec | 2018-12-17 15:18:02 +0000 | [diff] [blame] | 232 | * than \c 16 Bytes. It is updated after this call to allow |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 233 | * the aforementioned streaming usage. |
Hanno Becker | c7579ec | 2018-12-17 15:18:02 +0000 | [diff] [blame] | 234 | * \param iv The initialization vector. This must be a read/write buffer |
| 235 | * of length \c 16 Bytes. It is updated after this call to |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 236 | * allow the aforementioned streaming usage. |
Hanno Becker | f10905a | 2018-12-13 15:15:36 +0000 | [diff] [blame] | 237 | * \param input The buffer holding the input data. This must be a readable |
Hanno Becker | bdb7cd4 | 2018-12-18 17:49:48 +0000 | [diff] [blame] | 238 | * buffer of size \p length Bytes. |
Hanno Becker | f10905a | 2018-12-13 15:15:36 +0000 | [diff] [blame] | 239 | * \param output The buffer to hold the output data. This must be a writable |
Hanno Becker | bdb7cd4 | 2018-12-18 17:49:48 +0000 | [diff] [blame] | 240 | * buffer of length \p length Bytes. |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 241 | * |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 242 | * \return \c 0 if successful. |
| 243 | * \return A negative error code on failure. |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 244 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 245 | int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx, |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 246 | int mode, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 247 | size_t length, |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 248 | size_t *iv_off, |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 249 | unsigned char iv[16], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 250 | const unsigned char *input, |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 251 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 252 | #endif /* MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 253 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 254 | #if defined(MBEDTLS_CIPHER_MODE_CTR) |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 255 | /** |
Hanno Becker | 70ded36 | 2018-12-19 13:42:05 +0000 | [diff] [blame] | 256 | * \brief Perform a CAMELLIA-CTR buffer encryption/decryption operation. |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 257 | * |
Hanno Becker | 70ded36 | 2018-12-19 13:42:05 +0000 | [diff] [blame] | 258 | * *note Due to the nature of CTR mode, you should use the same |
| 259 | * key for both encryption and decryption. In particular, calls |
| 260 | * to this function should be preceded by a key-schedule via |
| 261 | * mbedtls_camellia_setkey_enc() regardless of whether \p mode |
| 262 | * is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. |
Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 263 | * |
Manuel Pégourié-Gonnard | 22997b7 | 2018-02-28 12:29:41 +0100 | [diff] [blame] | 264 | * \warning You must never reuse a nonce value with the same key. Doing so |
| 265 | * would void the encryption for the two messages encrypted with |
| 266 | * the same nonce and key. |
| 267 | * |
| 268 | * There are two common strategies for managing nonces with CTR: |
| 269 | * |
Manuel Pégourié-Gonnard | 4f24e95 | 2018-05-24 11:59:30 +0200 | [diff] [blame] | 270 | * 1. You can handle everything as a single message processed over |
| 271 | * successive calls to this function. In that case, you want to |
| 272 | * set \p nonce_counter and \p nc_off to 0 for the first call, and |
| 273 | * then preserve the values of \p nonce_counter, \p nc_off and \p |
| 274 | * stream_block across calls to this function as they will be |
| 275 | * updated by this function. |
Manuel Pégourié-Gonnard | 22997b7 | 2018-02-28 12:29:41 +0100 | [diff] [blame] | 276 | * |
Manuel Pégourié-Gonnard | 4f24e95 | 2018-05-24 11:59:30 +0200 | [diff] [blame] | 277 | * With this strategy, you must not encrypt more than 2**128 |
| 278 | * blocks of data with the same key. |
| 279 | * |
| 280 | * 2. You can encrypt separate messages by dividing the \p |
| 281 | * nonce_counter buffer in two areas: the first one used for a |
| 282 | * per-message nonce, handled by yourself, and the second one |
| 283 | * updated by this function internally. |
| 284 | * |
Hanno Becker | 70ded36 | 2018-12-19 13:42:05 +0000 | [diff] [blame] | 285 | * For example, you might reserve the first \c 12 Bytes for the |
| 286 | * per-message nonce, and the last \c 4 Bytes for internal use. |
| 287 | * In that case, before calling this function on a new message you |
| 288 | * need to set the first \c 12 Bytes of \p nonce_counter to your |
| 289 | * chosen nonce value, the last four to \c 0, and \p nc_off to \c 0 |
| 290 | * (which will cause \p stream_block to be ignored). That way, you |
| 291 | * can encrypt at most \c 2**96 messages of up to \c 2**32 blocks |
| 292 | * each with the same key. |
Manuel Pégourié-Gonnard | 4f24e95 | 2018-05-24 11:59:30 +0200 | [diff] [blame] | 293 | * |
| 294 | * The per-message nonce (or information sufficient to reconstruct |
Hanno Becker | df4b596 | 2018-12-19 15:50:02 +0000 | [diff] [blame] | 295 | * it) needs to be communicated with the ciphertext and must be |
Hanno Becker | 70ded36 | 2018-12-19 13:42:05 +0000 | [diff] [blame] | 296 | * unique. The recommended way to ensure uniqueness is to use a |
| 297 | * message counter. An alternative is to generate random nonces, |
| 298 | * but this limits the number of messages that can be securely |
| 299 | * encrypted: for example, with 96-bit random nonces, you should |
| 300 | * not encrypt more than 2**32 messages with the same key. |
Manuel Pégourié-Gonnard | 4f24e95 | 2018-05-24 11:59:30 +0200 | [diff] [blame] | 301 | * |
| 302 | * Note that for both stategies, sizes are measured in blocks and |
Hanno Becker | df4b596 | 2018-12-19 15:50:02 +0000 | [diff] [blame] | 303 | * that a CAMELLIA block is \c 16 Bytes. |
Manuel Pégourié-Gonnard | 22997b7 | 2018-02-28 12:29:41 +0100 | [diff] [blame] | 304 | * |
Manuel Pégourié-Gonnard | fa0c47d | 2018-05-24 19:02:06 +0200 | [diff] [blame] | 305 | * \warning Upon return, \p stream_block contains sensitive data. Its |
| 306 | * content must not be written to insecure storage and should be |
| 307 | * securely discarded as soon as it's no longer needed. |
| 308 | * |
Hanno Becker | af4b83b | 2018-12-17 09:30:27 +0000 | [diff] [blame] | 309 | * \param ctx The CAMELLIA context to use. This must be initialized |
| 310 | * and bound to a key. |
Hanno Becker | c7579ec | 2018-12-17 15:18:02 +0000 | [diff] [blame] | 311 | * \param length The length of the input data \p input in Bytes. |
Hanno Becker | af4b83b | 2018-12-17 09:30:27 +0000 | [diff] [blame] | 312 | * Any value is allowed. |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 313 | * \param nc_off The offset in the current \p stream_block (for resuming |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 314 | * within current cipher stream). The offset pointer to |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 315 | * should be \c 0 at the start of a stream. It is updated |
| 316 | * at the end of this call. |
Hanno Becker | c7579ec | 2018-12-17 15:18:02 +0000 | [diff] [blame] | 317 | * \param nonce_counter The 128-bit nonce and counter. This must be a read/write |
| 318 | * buffer of length \c 16 Bytes. |
| 319 | * \param stream_block The saved stream-block for resuming. This must be a |
| 320 | * read/write buffer of length \c 16 Bytes. |
Hanno Becker | f10905a | 2018-12-13 15:15:36 +0000 | [diff] [blame] | 321 | * \param input The input data stream. This must be a readable buffer of |
Hanno Becker | bdb7cd4 | 2018-12-18 17:49:48 +0000 | [diff] [blame] | 322 | * size \p length Bytes. |
Hanno Becker | f10905a | 2018-12-13 15:15:36 +0000 | [diff] [blame] | 323 | * \param output The output data stream. This must be a writable buffer |
Hanno Becker | bdb7cd4 | 2018-12-18 17:49:48 +0000 | [diff] [blame] | 324 | * of size \p length Bytes. |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 325 | * |
Hanno Becker | 7a16aad | 2018-12-12 14:54:16 +0000 | [diff] [blame] | 326 | * \return \c 0 if successful. |
| 327 | * \return A negative error code on failure. |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 328 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 329 | int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx, |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 330 | size_t length, |
| 331 | size_t *nc_off, |
| 332 | unsigned char nonce_counter[16], |
| 333 | unsigned char stream_block[16], |
| 334 | const unsigned char *input, |
| 335 | unsigned char *output ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 336 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ |
Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 337 | |
Ron Eldor | fa8f635 | 2017-06-20 15:48:46 +0300 | [diff] [blame] | 338 | #if defined(MBEDTLS_SELF_TEST) |
| 339 | |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 340 | /** |
| 341 | * \brief Checkup routine |
| 342 | * |
| 343 | * \return 0 if successful, or 1 if the test failed |
| 344 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 345 | int mbedtls_camellia_self_test( int verbose ); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 346 | |
Ron Eldor | fa8f635 | 2017-06-20 15:48:46 +0300 | [diff] [blame] | 347 | #endif /* MBEDTLS_SELF_TEST */ |
| 348 | |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 349 | #ifdef __cplusplus |
| 350 | } |
| 351 | #endif |
| 352 | |
| 353 | #endif /* camellia.h */ |