Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file aria.h |
| 3 | * |
| 4 | * \brief ARIA block cipher |
| 5 | * |
| 6 | * Copyright (C) 2006-2017, ARM Limited, All Rights Reserved |
| 7 | * SPDX-License-Identifier: Apache-2.0 |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | * not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | * |
| 21 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 22 | */ |
| 23 | |
| 24 | #ifndef MBEDTLS_ARIA_H |
| 25 | #define MBEDTLS_ARIA_H |
| 26 | |
| 27 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 28 | #include "config.h" |
| 29 | #else |
| 30 | #include MBEDTLS_CONFIG_FILE |
| 31 | #endif |
| 32 | |
| 33 | #include <stddef.h> |
| 34 | #include <stdint.h> |
| 35 | |
| 36 | #define MBEDTLS_ARIA_ENCRYPT 1 |
| 37 | #define MBEDTLS_ARIA_DECRYPT 0 |
| 38 | |
| 39 | #define MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH -0x005C /**< Invalid key length. */ |
| 40 | #define MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH -0x005E /**< Invalid data input length. */ |
| 41 | |
| 42 | #if !defined(MBEDTLS_ARIA_ALT) |
| 43 | // Regular implementation |
| 44 | // |
| 45 | |
| 46 | #ifdef __cplusplus |
| 47 | extern "C" { |
| 48 | #endif |
| 49 | |
| 50 | /** |
| 51 | * \brief ARIA context structure |
| 52 | */ |
| 53 | |
| 54 | typedef struct |
| 55 | { |
| 56 | int nr; // rounds: nr = 12, 14, or 16 |
| 57 | uint32_t rk[17][4]; // nr+1 round keys (+1 for final) |
| 58 | } |
| 59 | mbedtls_aria_context; |
| 60 | |
| 61 | /** |
| 62 | * \brief Initialize ARIA context |
| 63 | * |
| 64 | * \param ctx ARIA context to be initialized |
| 65 | */ |
| 66 | void mbedtls_aria_init( mbedtls_aria_context *ctx ); |
| 67 | |
| 68 | /** |
| 69 | * \brief Clear ARIA context |
| 70 | * |
| 71 | * \param ctx ARIA context to be cleared |
| 72 | */ |
| 73 | void mbedtls_aria_free( mbedtls_aria_context *ctx ); |
| 74 | |
| 75 | /** |
| 76 | * \brief ARIA key schedule (encryption) |
| 77 | * |
| 78 | * \param ctx ARIA context to be initialized |
| 79 | * \param key encryption key |
| 80 | * \param keybits must be 128, 192 or 256 |
| 81 | * |
| 82 | * \return 0 if successful, or MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH |
| 83 | */ |
| 84 | int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx, const unsigned char *key, |
| 85 | unsigned int keybits ); |
| 86 | |
| 87 | /** |
| 88 | * \brief ARIA key schedule (decryption) |
| 89 | * |
| 90 | * \param ctx ARIA context to be initialized |
| 91 | * \param key decryption key |
| 92 | * \param keybits must be 128, 192 or 256 |
| 93 | * |
| 94 | * \return 0 if successful, or MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH |
| 95 | */ |
| 96 | int mbedtls_aria_setkey_dec( mbedtls_aria_context *ctx, const unsigned char *key, |
| 97 | unsigned int keybits ); |
| 98 | |
| 99 | /** |
| 100 | * \brief ARIA-ECB block encryption/decryption |
| 101 | * |
| 102 | * \param ctx ARIA context |
| 103 | * \param mode MBEDTLS_ARIA_ENCRYPT or MBEDTLS_ARIA_DECRYPT |
| 104 | * \param input 16-byte input block |
| 105 | * \param output 16-byte output block |
| 106 | * |
| 107 | * \return 0 if successful |
| 108 | */ |
| 109 | int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx, |
| 110 | int mode, |
| 111 | const unsigned char input[16], |
| 112 | unsigned char output[16] ); |
| 113 | |
| 114 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 115 | /** |
| 116 | * \brief ARIA-CBC buffer encryption/decryption |
| 117 | * Length should be a multiple of the block |
| 118 | * size (16 bytes) |
| 119 | * |
| 120 | * \note Upon exit, the content of the IV is updated so that you can |
| 121 | * call the function same function again on the following |
| 122 | * block(s) of data and get the same result as if it was |
| 123 | * encrypted in one call. This allows a "streaming" usage. |
| 124 | * If on the other hand you need to retain the contents of the |
| 125 | * IV, you should either save it manually or use the cipher |
| 126 | * module instead. |
| 127 | * |
| 128 | * \param ctx ARIA context |
| 129 | * \param mode MBEDTLS_ARIA_ENCRYPT or MBEDTLS_ARIA_DECRYPT |
| 130 | * \param length length of the input data |
| 131 | * \param iv initialization vector (updated after use) |
| 132 | * \param input buffer holding the input data |
| 133 | * \param output buffer holding the output data |
| 134 | * |
| 135 | * \return 0 if successful, or |
| 136 | * MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH |
| 137 | */ |
| 138 | int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx, |
| 139 | int mode, |
| 140 | size_t length, |
| 141 | unsigned char iv[16], |
| 142 | const unsigned char *input, |
| 143 | unsigned char *output ); |
| 144 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 145 | |
| 146 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
| 147 | /** |
| 148 | * \brief ARIA-CFB128 buffer encryption/decryption |
| 149 | * |
| 150 | * Note: Due to the nature of CFB you should use the same key schedule for |
| 151 | * both encryption and decryption. So a context initialized with |
| 152 | * mbedtls_aria_setkey_enc() for both MBEDTLS_ARIA_ENCRYPT and CAMELLIE_DECRYPT. |
| 153 | * |
| 154 | * \note Upon exit, the content of the IV is updated so that you can |
| 155 | * call the function same function again on the following |
| 156 | * block(s) of data and get the same result as if it was |
| 157 | * encrypted in one call. This allows a "streaming" usage. |
| 158 | * If on the other hand you need to retain the contents of the |
| 159 | * IV, you should either save it manually or use the cipher |
| 160 | * module instead. |
| 161 | * |
| 162 | * \param ctx ARIA context |
| 163 | * \param mode MBEDTLS_ARIA_ENCRYPT or MBEDTLS_ARIA_DECRYPT |
| 164 | * \param length length of the input data |
| 165 | * \param iv_off offset in IV (updated after use) |
| 166 | * \param iv initialization vector (updated after use) |
| 167 | * \param input buffer holding the input data |
| 168 | * \param output buffer holding the output data |
| 169 | * |
| 170 | * \return 0 if successful, or |
| 171 | * MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH |
| 172 | */ |
| 173 | int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx, |
| 174 | int mode, |
| 175 | size_t length, |
| 176 | size_t *iv_off, |
| 177 | unsigned char iv[16], |
| 178 | const unsigned char *input, |
| 179 | unsigned char *output ); |
| 180 | #endif /* MBEDTLS_CIPHER_MODE_CFB */ |
| 181 | |
| 182 | #if defined(MBEDTLS_CIPHER_MODE_CTR) |
| 183 | /** |
| 184 | * \brief ARIA-CTR buffer encryption/decryption |
| 185 | * |
| 186 | * Warning: You have to keep the maximum use of your counter in mind! |
| 187 | * |
| 188 | * Note: Due to the nature of CTR you should use the same key schedule for |
| 189 | * both encryption and decryption. So a context initialized with |
| 190 | * mbedtls_aria_setkey_enc() for both MBEDTLS_ARIA_ENCRYPT and MBEDTLS_ARIA_DECRYPT. |
| 191 | * |
| 192 | * \param ctx ARIA context |
| 193 | * \param length The length of the data |
| 194 | * \param nc_off The offset in the current stream_block (for resuming |
| 195 | * within current cipher stream). The offset pointer to |
| 196 | * should be 0 at the start of a stream. |
| 197 | * \param nonce_counter The 128-bit nonce and counter. |
| 198 | * \param stream_block The saved stream-block for resuming. Is overwritten |
| 199 | * by the function. |
| 200 | * \param input The input data stream |
| 201 | * \param output The output data stream |
| 202 | * |
| 203 | * \return 0 if successful |
| 204 | */ |
| 205 | int mbedtls_aria_crypt_ctr( mbedtls_aria_context *ctx, |
| 206 | size_t length, |
| 207 | size_t *nc_off, |
| 208 | unsigned char nonce_counter[16], |
| 209 | unsigned char stream_block[16], |
| 210 | const unsigned char *input, |
| 211 | unsigned char *output ); |
| 212 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ |
| 213 | |
| 214 | #ifdef __cplusplus |
| 215 | } |
| 216 | #endif |
| 217 | |
| 218 | #else /* MBEDTLS_ARIA_ALT */ |
| 219 | #include "aria_alt.h" |
| 220 | #endif /* MBEDTLS_ARIA_ALT */ |
| 221 | |
| 222 | #ifdef __cplusplus |
| 223 | extern "C" { |
| 224 | #endif |
| 225 | |
| 226 | /** |
| 227 | * \brief Checkup routine |
| 228 | * |
| 229 | * \return 0 if successful, or 1 if the test failed |
| 230 | */ |
| 231 | int mbedtls_aria_self_test( int verbose ); |
| 232 | |
| 233 | #ifdef __cplusplus |
| 234 | } |
| 235 | #endif |
| 236 | |
| 237 | #endif /* aria.h */ |