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