| 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 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #define MBEDTLS_CAMELLIA_ENCRYPT     1 | 
|  | 37 | #define MBEDTLS_CAMELLIA_DECRYPT     0 | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 38 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 39 | #define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH           -0x0024  /**< Invalid key length. */ | 
|  | 40 | #define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH         -0x0026  /**< Invalid data input length. */ | 
| Gilles Peskine | 7ecab3d | 2018-01-26 17:56:38 +0100 | [diff] [blame] | 41 | #define MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED              -0x0027  /**< Camellia hardware accelerator failed. */ | 
| Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 42 |  | 
| Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 43 | #ifdef __cplusplus | 
|  | 44 | extern "C" { | 
|  | 45 | #endif | 
|  | 46 |  | 
| Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 47 | #if !defined(MBEDTLS_CAMELLIA_ALT) | 
|  | 48 | // Regular implementation | 
|  | 49 | // | 
|  | 50 |  | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 51 | /** | 
|  | 52 | * \brief          CAMELLIA context structure | 
|  | 53 | */ | 
| Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 54 | typedef struct mbedtls_camellia_context | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 55 | { | 
|  | 56 | int nr;                     /*!<  number of rounds  */ | 
| Paul Bakker | c81f6c3 | 2009-05-03 13:09:15 +0000 | [diff] [blame] | 57 | uint32_t rk[68];            /*!<  CAMELLIA round keys    */ | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 58 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | mbedtls_camellia_context; | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 60 |  | 
| Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 61 | #else  /* MBEDTLS_CAMELLIA_ALT */ | 
|  | 62 | #include "camellia_alt.h" | 
|  | 63 | #endif /* MBEDTLS_CAMELLIA_ALT */ | 
|  | 64 |  | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 65 | /** | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 66 | * \brief          Initialize CAMELLIA context | 
|  | 67 | * | 
|  | 68 | * \param ctx      CAMELLIA context to be initialized | 
|  | 69 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 70 | void mbedtls_camellia_init( mbedtls_camellia_context *ctx ); | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 71 |  | 
|  | 72 | /** | 
|  | 73 | * \brief          Clear CAMELLIA context | 
|  | 74 | * | 
|  | 75 | * \param ctx      CAMELLIA context to be cleared | 
|  | 76 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 77 | void mbedtls_camellia_free( mbedtls_camellia_context *ctx ); | 
| Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 78 |  | 
|  | 79 | /** | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 80 | * \brief          CAMELLIA key schedule (encryption) | 
|  | 81 | * | 
|  | 82 | * \param ctx      CAMELLIA context to be initialized | 
|  | 83 | * \param key      encryption key | 
| Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 84 | * \param keybits  must be 128, 192 or 256 | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 85 | * | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 86 | * \return         0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 87 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 88 | int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned char *key, | 
| Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 89 | unsigned int keybits ); | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 90 |  | 
|  | 91 | /** | 
|  | 92 | * \brief          CAMELLIA key schedule (decryption) | 
|  | 93 | * | 
|  | 94 | * \param ctx      CAMELLIA context to be initialized | 
|  | 95 | * \param key      decryption key | 
| Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 96 | * \param keybits  must be 128, 192 or 256 | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 97 | * | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | * \return         0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 99 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 100 | int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, const unsigned char *key, | 
| Manuel Pégourié-Gonnard | b8186a5 | 2015-06-18 14:58:58 +0200 | [diff] [blame] | 101 | unsigned int keybits ); | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 102 |  | 
|  | 103 | /** | 
|  | 104 | * \brief          CAMELLIA-ECB block encryption/decryption | 
|  | 105 | * | 
|  | 106 | * \param ctx      CAMELLIA context | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 107 | * \param mode     MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 108 | * \param input    16-byte input block | 
|  | 109 | * \param output   16-byte output block | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 110 | * | 
| Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 111 | * \return         0 if successful | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 112 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx, | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 114 | int mode, | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 115 | const unsigned char input[16], | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 116 | unsigned char output[16] ); | 
|  | 117 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 118 | #if defined(MBEDTLS_CIPHER_MODE_CBC) | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 119 | /** | 
|  | 120 | * \brief          CAMELLIA-CBC buffer encryption/decryption | 
| Paul Bakker | 4c067eb | 2009-05-17 10:25:19 +0000 | [diff] [blame] | 121 | *                 Length should be a multiple of the block | 
|  | 122 | *                 size (16 bytes) | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 123 | * | 
| Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 124 | * \note           Upon exit, the content of the IV is updated so that you can | 
|  | 125 | *                 call the function same function again on the following | 
|  | 126 | *                 block(s) of data and get the same result as if it was | 
|  | 127 | *                 encrypted in one call. This allows a "streaming" usage. | 
|  | 128 | *                 If on the other hand you need to retain the contents of the | 
|  | 129 | *                 IV, you should either save it manually or use the cipher | 
|  | 130 | *                 module instead. | 
|  | 131 | * | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 132 | * \param ctx      CAMELLIA context | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | * \param mode     MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 134 | * \param length   length of the input data | 
|  | 135 | * \param iv       initialization vector (updated after use) | 
|  | 136 | * \param input    buffer holding the input data | 
|  | 137 | * \param output   buffer holding the output data | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 138 | * | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 139 | * \return         0 if successful, or | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | *                 MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 141 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 142 | int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx, | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 143 | int mode, | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 144 | size_t length, | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 145 | unsigned char iv[16], | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 146 | const unsigned char *input, | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 147 | unsigned char *output ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 148 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 149 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | #if defined(MBEDTLS_CIPHER_MODE_CFB) | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 151 | /** | 
|  | 152 | * \brief          CAMELLIA-CFB128 buffer encryption/decryption | 
|  | 153 | * | 
| Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 154 | * Note: Due to the nature of CFB you should use the same key schedule for | 
|  | 155 | * both encryption and decryption. So a context initialized with | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT. | 
| Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 157 | * | 
| Manuel Pégourié-Gonnard | 2be147a | 2015-01-23 16:19:47 +0000 | [diff] [blame] | 158 | * \note           Upon exit, the content of the IV is updated so that you can | 
|  | 159 | *                 call the function same function again on the following | 
|  | 160 | *                 block(s) of data and get the same result as if it was | 
|  | 161 | *                 encrypted in one call. This allows a "streaming" usage. | 
|  | 162 | *                 If on the other hand you need to retain the contents of the | 
|  | 163 | *                 IV, you should either save it manually or use the cipher | 
|  | 164 | *                 module instead. | 
|  | 165 | * | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 166 | * \param ctx      CAMELLIA context | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | * \param mode     MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 168 | * \param length   length of the input data | 
|  | 169 | * \param iv_off   offset in IV (updated after use) | 
|  | 170 | * \param iv       initialization vector (updated after use) | 
|  | 171 | * \param input    buffer holding the input data | 
|  | 172 | * \param output   buffer holding the output data | 
| Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 173 | * | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 174 | * \return         0 if successful, or | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 175 | *                 MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 176 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 177 | int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx, | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 178 | int mode, | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 179 | size_t length, | 
| Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 180 | size_t *iv_off, | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 181 | unsigned char iv[16], | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 182 | const unsigned char *input, | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 183 | unsigned char *output ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 184 | #endif /* MBEDTLS_CIPHER_MODE_CFB */ | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 185 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 186 | #if defined(MBEDTLS_CIPHER_MODE_CTR) | 
| Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 187 | /** | 
| Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 188 | * \brief               CAMELLIA-CTR buffer encryption/decryption | 
|  | 189 | * | 
| Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 190 | * Note: Due to the nature of CTR you should use the same key schedule for | 
|  | 191 | * both encryption and decryption. So a context initialized with | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and MBEDTLS_CAMELLIA_DECRYPT. | 
| Paul Bakker | ca6f3e2 | 2011-10-06 13:11:08 +0000 | [diff] [blame] | 193 | * | 
| Manuel Pégourié-Gonnard | 22997b7 | 2018-02-28 12:29:41 +0100 | [diff] [blame] | 194 | * \warning    You must never reuse a nonce value with the same key. Doing so | 
|  | 195 | *             would void the encryption for the two messages encrypted with | 
|  | 196 | *             the same nonce and key. | 
|  | 197 | * | 
|  | 198 | *             There are two common strategies for managing nonces with CTR: | 
|  | 199 | * | 
| Manuel Pégourié-Gonnard | 4f24e95 | 2018-05-24 11:59:30 +0200 | [diff] [blame] | 200 | *             1. You can handle everything as a single message processed over | 
|  | 201 | *             successive calls to this function. In that case, you want to | 
|  | 202 | *             set \p nonce_counter and \p nc_off to 0 for the first call, and | 
|  | 203 | *             then preserve the values of \p nonce_counter, \p nc_off and \p | 
|  | 204 | *             stream_block across calls to this function as they will be | 
|  | 205 | *             updated by this function. | 
| Manuel Pégourié-Gonnard | 22997b7 | 2018-02-28 12:29:41 +0100 | [diff] [blame] | 206 | * | 
| Manuel Pégourié-Gonnard | 4f24e95 | 2018-05-24 11:59:30 +0200 | [diff] [blame] | 207 | *             With this strategy, you must not encrypt more than 2**128 | 
|  | 208 | *             blocks of data with the same key. | 
|  | 209 | * | 
|  | 210 | *             2. You can encrypt separate messages by dividing the \p | 
|  | 211 | *             nonce_counter buffer in two areas: the first one used for a | 
|  | 212 | *             per-message nonce, handled by yourself, and the second one | 
|  | 213 | *             updated by this function internally. | 
|  | 214 | * | 
|  | 215 | *             For example, you might reserve the first 12 bytes for the | 
|  | 216 | *             per-message nonce, and the last 4 bytes for internal use. In that | 
|  | 217 | *             case, before calling this function on a new message you need to | 
|  | 218 | *             set the first 12 bytes of \p nonce_counter to your chosen nonce | 
|  | 219 | *             value, the last 4 to 0, and \p nc_off to 0 (which will cause \p | 
|  | 220 | *             stream_block to be ignored). That way, you can encrypt at most | 
|  | 221 | *             2**96 messages of up to 2**32 blocks each with the same key. | 
|  | 222 | * | 
|  | 223 | *             The per-message nonce (or information sufficient to reconstruct | 
|  | 224 | *             it) needs to be communicated with the ciphertext and must be unique. | 
|  | 225 | *             The recommended way to ensure uniqueness is to use a message | 
|  | 226 | *             counter. An alternative is to generate random nonces, but this | 
|  | 227 | *             limits the number of messages that can be securely encrypted: | 
|  | 228 | *             for example, with 96-bit random nonces, you should not encrypt | 
|  | 229 | *             more than 2**32 messages with the same key. | 
|  | 230 | * | 
|  | 231 | *             Note that for both stategies, sizes are measured in blocks and | 
|  | 232 | *             that a CAMELLIA block is 16 bytes. | 
| Manuel Pégourié-Gonnard | 22997b7 | 2018-02-28 12:29:41 +0100 | [diff] [blame] | 233 | * | 
| Manuel Pégourié-Gonnard | fa0c47d | 2018-05-24 19:02:06 +0200 | [diff] [blame] | 234 | * \warning    Upon return, \p stream_block contains sensitive data. Its | 
|  | 235 | *             content must not be written to insecure storage and should be | 
|  | 236 | *             securely discarded as soon as it's no longer needed. | 
|  | 237 | * | 
| Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 238 | * \param ctx           CAMELLIA context | 
| Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 239 | * \param length        The length of the data | 
|  | 240 | * \param nc_off        The offset in the current stream_block (for resuming | 
|  | 241 | *                      within current cipher stream). The offset pointer to | 
|  | 242 | *                      should be 0 at the start of a stream. | 
|  | 243 | * \param nonce_counter The 128-bit nonce and counter. | 
|  | 244 | * \param stream_block  The saved stream-block for resuming. Is overwritten | 
|  | 245 | *                      by the function. | 
|  | 246 | * \param input         The input data stream | 
|  | 247 | * \param output        The output data stream | 
|  | 248 | * | 
|  | 249 | * \return         0 if successful | 
|  | 250 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 251 | int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx, | 
| Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 252 | size_t length, | 
|  | 253 | size_t *nc_off, | 
|  | 254 | unsigned char nonce_counter[16], | 
|  | 255 | unsigned char stream_block[16], | 
|  | 256 | const unsigned char *input, | 
|  | 257 | unsigned char *output ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 258 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ | 
| Paul Bakker | 1ef71df | 2011-06-09 14:14:58 +0000 | [diff] [blame] | 259 |  | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 260 | /** | 
|  | 261 | * \brief          Checkup routine | 
|  | 262 | * | 
|  | 263 | * \return         0 if successful, or 1 if the test failed | 
|  | 264 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 265 | int mbedtls_camellia_self_test( int verbose ); | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 266 |  | 
|  | 267 | #ifdef __cplusplus | 
|  | 268 | } | 
|  | 269 | #endif | 
|  | 270 |  | 
|  | 271 | #endif /* camellia.h */ |