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