Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ARIA implementation |
| 3 | * |
| 4 | * Copyright (C) 2006-2017, ARM Limited, All Rights Reserved |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
| 24 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
| 45 | * |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 46 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 47 | */ |
| 48 | |
Manuel Pégourié-Gonnard | a6d639e | 2018-02-20 13:45:44 +0100 | [diff] [blame] | 49 | /* |
| 50 | * This implementation is based on the following standards: |
| 51 | * [1] http://210.104.33.10/ARIA/doc/ARIA-specification-e.pdf |
| 52 | * [2] https://tools.ietf.org/html/rfc5794 |
| 53 | */ |
| 54 | |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 55 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 56 | #include "mbedtls/config.h" |
| 57 | #else |
| 58 | #include MBEDTLS_CONFIG_FILE |
| 59 | #endif |
| 60 | |
| 61 | #if defined(MBEDTLS_ARIA_C) |
| 62 | |
| 63 | #include "mbedtls/aria.h" |
| 64 | |
| 65 | #include <string.h> |
| 66 | |
| 67 | #if defined(MBEDTLS_SELF_TEST) |
| 68 | #if defined(MBEDTLS_PLATFORM_C) |
| 69 | #include "mbedtls/platform.h" |
| 70 | #else |
| 71 | #include <stdio.h> |
| 72 | #define mbedtls_printf printf |
| 73 | #endif /* MBEDTLS_PLATFORM_C */ |
| 74 | #endif /* MBEDTLS_SELF_TEST */ |
| 75 | |
| 76 | #if !defined(MBEDTLS_ARIA_ALT) |
| 77 | |
Manuel Pégourié-Gonnard | 7124fb6 | 2018-05-22 16:05:33 +0200 | [diff] [blame] | 78 | #include "mbedtls/platform_util.h" |
| 79 | |
Manuel Pégourié-Gonnard | c0bb66f | 2018-02-28 12:38:04 +0100 | [diff] [blame] | 80 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ |
| 81 | !defined(inline) && !defined(__cplusplus) |
| 82 | #define inline __inline |
| 83 | #endif |
| 84 | |
Hanno Becker | b54ae0b | 2018-12-11 21:51:32 +0000 | [diff] [blame] | 85 | /* Parameter validation macros */ |
| 86 | #define ARIA_VALIDATE_RET( cond ) \ |
| 87 | MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ARIA_BAD_INPUT_DATA ) |
| 88 | #define ARIA_VALIDATE( cond ) \ |
| 89 | MBEDTLS_INTERNAL_VALIDATE( cond ) |
| 90 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 91 | /* |
| 92 | * 32-bit integer manipulation macros (little endian) |
| 93 | */ |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 94 | #ifndef GET_UINT32_LE |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 95 | #define GET_UINT32_LE( n, b, i ) \ |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 96 | { \ |
| 97 | (n) = ( (uint32_t) (b)[(i) ] ) \ |
| 98 | | ( (uint32_t) (b)[(i) + 1] << 8 ) \ |
| 99 | | ( (uint32_t) (b)[(i) + 2] << 16 ) \ |
| 100 | | ( (uint32_t) (b)[(i) + 3] << 24 ); \ |
| 101 | } |
| 102 | #endif |
| 103 | |
| 104 | #ifndef PUT_UINT32_LE |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 105 | #define PUT_UINT32_LE( n, b, i ) \ |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 106 | { \ |
| 107 | (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ |
| 108 | (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ |
| 109 | (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ |
| 110 | (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ |
| 111 | } |
| 112 | #endif |
| 113 | |
Manuel Pégourié-Gonnard | 35ad891 | 2018-02-26 11:59:16 +0100 | [diff] [blame] | 114 | /* |
Manuel Pégourié-Gonnard | f205a01 | 2018-02-26 14:10:23 +0100 | [diff] [blame] | 115 | * modify byte order: ( A B C D ) -> ( B A D C ), i.e. swap pairs of bytes |
Manuel Pégourié-Gonnard | 35ad891 | 2018-02-26 11:59:16 +0100 | [diff] [blame] | 116 | * |
| 117 | * This is submatrix P1 in [1] Appendix B.1 |
Manuel Pégourié-Gonnard | fb0e4f0 | 2018-02-26 16:08:40 +0100 | [diff] [blame] | 118 | * |
| 119 | * Common compilers fail to translate this to minimal number of instructions, |
| 120 | * so let's provide asm versions for common platforms with C fallback. |
Manuel Pégourié-Gonnard | 35ad891 | 2018-02-26 11:59:16 +0100 | [diff] [blame] | 121 | */ |
Manuel Pégourié-Gonnard | 377b2b6 | 2018-02-27 10:22:26 +0100 | [diff] [blame] | 122 | #if defined(MBEDTLS_HAVE_ASM) |
Manuel Pégourié-Gonnard | 2078725 | 2018-03-01 10:37:47 +0100 | [diff] [blame] | 123 | #if defined(__arm__) /* rev16 available from v6 up */ |
Manuel Pégourié-Gonnard | 377b2b6 | 2018-02-27 10:22:26 +0100 | [diff] [blame] | 124 | /* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */ |
| 125 | #if defined(__GNUC__) && \ |
Manuel Pégourié-Gonnard | 2078725 | 2018-03-01 10:37:47 +0100 | [diff] [blame] | 126 | ( !defined(__ARMCC_VERSION) || __ARMCC_VERSION >= 6000000 ) && \ |
| 127 | __ARM_ARCH >= 6 |
Manuel Pégourié-Gonnard | 377b2b6 | 2018-02-27 10:22:26 +0100 | [diff] [blame] | 128 | static inline uint32_t aria_p1( uint32_t x ) |
| 129 | { |
| 130 | uint32_t r; |
Manuel Pégourié-Gonnard | 2166214 | 2018-03-01 11:27:14 +0100 | [diff] [blame] | 131 | __asm( "rev16 %0, %1" : "=l" (r) : "l" (x) ); |
Manuel Pégourié-Gonnard | 377b2b6 | 2018-02-27 10:22:26 +0100 | [diff] [blame] | 132 | return( r ); |
| 133 | } |
| 134 | #define ARIA_P1 aria_p1 |
Manuel Pégourié-Gonnard | 2078725 | 2018-03-01 10:37:47 +0100 | [diff] [blame] | 135 | #elif defined(__ARMCC_VERSION) && __ARMCC_VERSION < 6000000 && \ |
| 136 | ( __TARGET_ARCH_ARM >= 6 || __TARGET_ARCH_THUMB >= 3 ) |
Manuel Pégourié-Gonnard | c0bb66f | 2018-02-28 12:38:04 +0100 | [diff] [blame] | 137 | static inline uint32_t aria_p1( uint32_t x ) |
Manuel Pégourié-Gonnard | 377b2b6 | 2018-02-27 10:22:26 +0100 | [diff] [blame] | 138 | { |
| 139 | uint32_t r; |
| 140 | __asm( "rev16 r, x" ); |
| 141 | return( r ); |
| 142 | } |
| 143 | #define ARIA_P1 aria_p1 |
| 144 | #endif |
| 145 | #endif /* arm */ |
| 146 | #if defined(__GNUC__) && \ |
| 147 | defined(__i386__) || defined(__amd64__) || defined( __x86_64__) |
Manuel Pégourié-Gonnard | 2df4bfe | 2018-05-22 13:39:01 +0200 | [diff] [blame] | 148 | /* I couldn't find an Intel equivalent of rev16, so two instructions */ |
Manuel Pégourié-Gonnard | fb0e4f0 | 2018-02-26 16:08:40 +0100 | [diff] [blame] | 149 | #define ARIA_P1(x) ARIA_P2( ARIA_P3( x ) ) |
Manuel Pégourié-Gonnard | 377b2b6 | 2018-02-27 10:22:26 +0100 | [diff] [blame] | 150 | #endif /* x86 gnuc */ |
Manuel Pégourié-Gonnard | fb0e4f0 | 2018-02-26 16:08:40 +0100 | [diff] [blame] | 151 | #endif /* MBEDTLS_HAVE_ASM && GNUC */ |
| 152 | #if !defined(ARIA_P1) |
Manuel Pégourié-Gonnard | 35ad891 | 2018-02-26 11:59:16 +0100 | [diff] [blame] | 153 | #define ARIA_P1(x) ((((x) >> 8) & 0x00FF00FF) ^ (((x) & 0x00FF00FF) << 8)) |
Manuel Pégourié-Gonnard | fb0e4f0 | 2018-02-26 16:08:40 +0100 | [diff] [blame] | 154 | #endif |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 155 | |
Manuel Pégourié-Gonnard | 35ad891 | 2018-02-26 11:59:16 +0100 | [diff] [blame] | 156 | /* |
| 157 | * modify byte order: ( A B C D ) -> ( C D A B ), i.e. rotate by 16 bits |
| 158 | * |
| 159 | * This is submatrix P2 in [1] Appendix B.1 |
Manuel Pégourié-Gonnard | fb0e4f0 | 2018-02-26 16:08:40 +0100 | [diff] [blame] | 160 | * |
| 161 | * Common compilers will translate this to a single instruction. |
Manuel Pégourié-Gonnard | 35ad891 | 2018-02-26 11:59:16 +0100 | [diff] [blame] | 162 | */ |
| 163 | #define ARIA_P2(x) (((x) >> 16) ^ ((x) << 16)) |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 164 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 165 | /* |
Manuel Pégourié-Gonnard | cac5008 | 2018-02-26 15:23:03 +0100 | [diff] [blame] | 166 | * modify byte order: ( A B C D ) -> ( D C B A ), i.e. change endianness |
| 167 | * |
| 168 | * This is submatrix P3 in [1] Appendix B.1 |
Manuel Pégourié-Gonnard | fb0e4f0 | 2018-02-26 16:08:40 +0100 | [diff] [blame] | 169 | * |
| 170 | * Some compilers fail to translate this to a single instruction, |
| 171 | * so let's provide asm versions for common platforms with C fallback. |
Manuel Pégourié-Gonnard | cac5008 | 2018-02-26 15:23:03 +0100 | [diff] [blame] | 172 | */ |
Manuel Pégourié-Gonnard | 377b2b6 | 2018-02-27 10:22:26 +0100 | [diff] [blame] | 173 | #if defined(MBEDTLS_HAVE_ASM) |
Manuel Pégourié-Gonnard | 2078725 | 2018-03-01 10:37:47 +0100 | [diff] [blame] | 174 | #if defined(__arm__) /* rev available from v6 up */ |
Manuel Pégourié-Gonnard | 377b2b6 | 2018-02-27 10:22:26 +0100 | [diff] [blame] | 175 | /* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */ |
| 176 | #if defined(__GNUC__) && \ |
Manuel Pégourié-Gonnard | 2078725 | 2018-03-01 10:37:47 +0100 | [diff] [blame] | 177 | ( !defined(__ARMCC_VERSION) || __ARMCC_VERSION >= 6000000 ) && \ |
| 178 | __ARM_ARCH >= 6 |
Manuel Pégourié-Gonnard | 377b2b6 | 2018-02-27 10:22:26 +0100 | [diff] [blame] | 179 | static inline uint32_t aria_p3( uint32_t x ) |
| 180 | { |
| 181 | uint32_t r; |
Manuel Pégourié-Gonnard | 2166214 | 2018-03-01 11:27:14 +0100 | [diff] [blame] | 182 | __asm( "rev %0, %1" : "=l" (r) : "l" (x) ); |
Manuel Pégourié-Gonnard | 377b2b6 | 2018-02-27 10:22:26 +0100 | [diff] [blame] | 183 | return( r ); |
| 184 | } |
| 185 | #define ARIA_P3 aria_p3 |
Manuel Pégourié-Gonnard | 2078725 | 2018-03-01 10:37:47 +0100 | [diff] [blame] | 186 | #elif defined(__ARMCC_VERSION) && __ARMCC_VERSION < 6000000 && \ |
| 187 | ( __TARGET_ARCH_ARM >= 6 || __TARGET_ARCH_THUMB >= 3 ) |
Manuel Pégourié-Gonnard | c0bb66f | 2018-02-28 12:38:04 +0100 | [diff] [blame] | 188 | static inline uint32_t aria_p3( uint32_t x ) |
Manuel Pégourié-Gonnard | 377b2b6 | 2018-02-27 10:22:26 +0100 | [diff] [blame] | 189 | { |
| 190 | uint32_t r; |
| 191 | __asm( "rev r, x" ); |
| 192 | return( r ); |
| 193 | } |
| 194 | #define ARIA_P3 aria_p3 |
| 195 | #endif |
| 196 | #endif /* arm */ |
| 197 | #if defined(__GNUC__) && \ |
| 198 | defined(__i386__) || defined(__amd64__) || defined( __x86_64__) |
Manuel Pégourié-Gonnard | fb0e4f0 | 2018-02-26 16:08:40 +0100 | [diff] [blame] | 199 | static inline uint32_t aria_p3( uint32_t x ) |
| 200 | { |
Manuel Pégourié-Gonnard | 2166214 | 2018-03-01 11:27:14 +0100 | [diff] [blame] | 201 | __asm( "bswap %0" : "=r" (x) : "0" (x) ); |
Manuel Pégourié-Gonnard | fb0e4f0 | 2018-02-26 16:08:40 +0100 | [diff] [blame] | 202 | return( x ); |
| 203 | } |
| 204 | #define ARIA_P3 aria_p3 |
Manuel Pégourié-Gonnard | 377b2b6 | 2018-02-27 10:22:26 +0100 | [diff] [blame] | 205 | #endif /* x86 gnuc */ |
Manuel Pégourié-Gonnard | fb0e4f0 | 2018-02-26 16:08:40 +0100 | [diff] [blame] | 206 | #endif /* MBEDTLS_HAVE_ASM && GNUC */ |
| 207 | #if !defined(ARIA_P3) |
Manuel Pégourié-Gonnard | cac5008 | 2018-02-26 15:23:03 +0100 | [diff] [blame] | 208 | #define ARIA_P3(x) ARIA_P2( ARIA_P1 ( x ) ) |
Manuel Pégourié-Gonnard | fb0e4f0 | 2018-02-26 16:08:40 +0100 | [diff] [blame] | 209 | #endif |
Manuel Pégourié-Gonnard | cac5008 | 2018-02-26 15:23:03 +0100 | [diff] [blame] | 210 | |
| 211 | /* |
Manuel Pégourié-Gonnard | 64744f8 | 2018-02-21 12:35:19 +0100 | [diff] [blame] | 212 | * ARIA Affine Transform |
Manuel Pégourié-Gonnard | f205a01 | 2018-02-26 14:10:23 +0100 | [diff] [blame] | 213 | * (a, b, c, d) = state in/out |
| 214 | * |
Manuel Pégourié-Gonnard | d418b0d | 2018-05-22 12:56:11 +0200 | [diff] [blame] | 215 | * If we denote the first byte of input by 0, ..., the last byte by f, |
Manuel Pégourié-Gonnard | f205a01 | 2018-02-26 14:10:23 +0100 | [diff] [blame] | 216 | * then inputs are: a = 0123, b = 4567, c = 89ab, d = cdef. |
| 217 | * |
Manuel Pégourié-Gonnard | f3a46a9 | 2018-02-28 12:38:21 +0100 | [diff] [blame] | 218 | * Reading [1] 2.4 or [2] 2.4.3 in columns and performing simple |
Manuel Pégourié-Gonnard | f205a01 | 2018-02-26 14:10:23 +0100 | [diff] [blame] | 219 | * rearrangements on adjacent pairs, output is: |
| 220 | * |
| 221 | * a = 3210 + 4545 + 6767 + 88aa + 99bb + dccd + effe |
| 222 | * = 3210 + 4567 + 6745 + 89ab + 98ba + dcfe + efcd |
Manuel Pégourié-Gonnard | 366e1b0 | 2018-03-01 14:48:10 +0100 | [diff] [blame] | 223 | * b = 0101 + 2323 + 5476 + 8998 + baab + eecc + ffdd |
Manuel Pégourié-Gonnard | f205a01 | 2018-02-26 14:10:23 +0100 | [diff] [blame] | 224 | * = 0123 + 2301 + 5476 + 89ab + ba98 + efcd + fedc |
Manuel Pégourié-Gonnard | 366e1b0 | 2018-03-01 14:48:10 +0100 | [diff] [blame] | 225 | * c = 0022 + 1133 + 4554 + 7667 + ab89 + dcdc + fefe |
Manuel Pégourié-Gonnard | f205a01 | 2018-02-26 14:10:23 +0100 | [diff] [blame] | 226 | * = 0123 + 1032 + 4567 + 7654 + ab89 + dcfe + fedc |
Manuel Pégourié-Gonnard | 366e1b0 | 2018-03-01 14:48:10 +0100 | [diff] [blame] | 227 | * d = 1001 + 2332 + 6644 + 7755 + 9898 + baba + cdef |
Manuel Pégourié-Gonnard | f205a01 | 2018-02-26 14:10:23 +0100 | [diff] [blame] | 228 | * = 1032 + 2301 + 6745 + 7654 + 98ba + ba98 + cdef |
| 229 | * |
| 230 | * Note: another presentation of the A transform can be found as the first |
| 231 | * half of App. B.1 in [1] in terms of 4-byte operators P1, P2, P3 and P4. |
| 232 | * The implementation below uses only P1 and P2 as they are sufficient. |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 233 | */ |
Manuel Pégourié-Gonnard | 64744f8 | 2018-02-21 12:35:19 +0100 | [diff] [blame] | 234 | static inline void aria_a( uint32_t *a, uint32_t *b, |
| 235 | uint32_t *c, uint32_t *d ) |
| 236 | { |
| 237 | uint32_t ta, tb, tc; |
Manuel Pégourié-Gonnard | f205a01 | 2018-02-26 14:10:23 +0100 | [diff] [blame] | 238 | ta = *b; // 4567 |
| 239 | *b = *a; // 0123 |
| 240 | *a = ARIA_P2( ta ); // 6745 |
| 241 | tb = ARIA_P2( *d ); // efcd |
| 242 | *d = ARIA_P1( *c ); // 98ba |
| 243 | *c = ARIA_P1( tb ); // fedc |
| 244 | ta ^= *d; // 4567+98ba |
| 245 | tc = ARIA_P2( *b ); // 2301 |
| 246 | ta = ARIA_P1( ta ) ^ tc ^ *c; // 2301+5476+89ab+fedc |
| 247 | tb ^= ARIA_P2( *d ); // ba98+efcd |
| 248 | tc ^= ARIA_P1( *a ); // 2301+7654 |
| 249 | *b ^= ta ^ tb; // 0123+2301+5476+89ab+ba98+efcd+fedc OUT |
| 250 | tb = ARIA_P2( tb ) ^ ta; // 2301+5476+89ab+98ba+cdef+fedc |
| 251 | *a ^= ARIA_P1( tb ); // 3210+4567+6745+89ab+98ba+dcfe+efcd OUT |
| 252 | ta = ARIA_P2( ta ); // 0123+7654+ab89+dcfe |
| 253 | *d ^= ARIA_P1( ta ) ^ tc; // 1032+2301+6745+7654+98ba+ba98+cdef OUT |
| 254 | tc = ARIA_P2( tc ); // 0123+5476 |
| 255 | *c ^= ARIA_P1( tc ) ^ ta; // 0123+1032+4567+7654+ab89+dcfe+fedc OUT |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 258 | /* |
Manuel Pégourié-Gonnard | 64744f8 | 2018-02-21 12:35:19 +0100 | [diff] [blame] | 259 | * ARIA Substitution Layer SL1 / SL2 |
| 260 | * (a, b, c, d) = state in/out |
Manuel Pégourié-Gonnard | a6d639e | 2018-02-20 13:45:44 +0100 | [diff] [blame] | 261 | * (sa, sb, sc, sd) = 256 8-bit S-Boxes (see below) |
Manuel Pégourié-Gonnard | a6d639e | 2018-02-20 13:45:44 +0100 | [diff] [blame] | 262 | * |
Manuel Pégourié-Gonnard | 64744f8 | 2018-02-21 12:35:19 +0100 | [diff] [blame] | 263 | * By passing sb1, sb2, is1, is2 as S-Boxes you get SL1 |
| 264 | * By passing is1, is2, sb1, sb2 as S-Boxes you get SL2 |
Manuel Pégourié-Gonnard | a6d639e | 2018-02-20 13:45:44 +0100 | [diff] [blame] | 265 | */ |
Manuel Pégourié-Gonnard | 64744f8 | 2018-02-21 12:35:19 +0100 | [diff] [blame] | 266 | static inline void aria_sl( uint32_t *a, uint32_t *b, |
| 267 | uint32_t *c, uint32_t *d, |
Manuel Pégourié-Gonnard | 12e2fbd | 2018-05-22 13:01:09 +0200 | [diff] [blame] | 268 | const uint8_t sa[256], const uint8_t sb[256], |
| 269 | const uint8_t sc[256], const uint8_t sd[256] ) |
Manuel Pégourié-Gonnard | 8c76a94 | 2018-02-21 12:03:22 +0100 | [diff] [blame] | 270 | { |
Manuel Pégourié-Gonnard | 7fc0879 | 2018-03-01 09:33:20 +0100 | [diff] [blame] | 271 | *a = ( (uint32_t) sa[ *a & 0xFF] ) ^ |
Manuel Pégourié-Gonnard | 64744f8 | 2018-02-21 12:35:19 +0100 | [diff] [blame] | 272 | (((uint32_t) sb[(*a >> 8) & 0xFF]) << 8) ^ |
| 273 | (((uint32_t) sc[(*a >> 16) & 0xFF]) << 16) ^ |
| 274 | (((uint32_t) sd[ *a >> 24 ]) << 24); |
Manuel Pégourié-Gonnard | 7fc0879 | 2018-03-01 09:33:20 +0100 | [diff] [blame] | 275 | *b = ( (uint32_t) sa[ *b & 0xFF] ) ^ |
Manuel Pégourié-Gonnard | 64744f8 | 2018-02-21 12:35:19 +0100 | [diff] [blame] | 276 | (((uint32_t) sb[(*b >> 8) & 0xFF]) << 8) ^ |
| 277 | (((uint32_t) sc[(*b >> 16) & 0xFF]) << 16) ^ |
| 278 | (((uint32_t) sd[ *b >> 24 ]) << 24); |
Manuel Pégourié-Gonnard | 7fc0879 | 2018-03-01 09:33:20 +0100 | [diff] [blame] | 279 | *c = ( (uint32_t) sa[ *c & 0xFF] ) ^ |
Manuel Pégourié-Gonnard | 64744f8 | 2018-02-21 12:35:19 +0100 | [diff] [blame] | 280 | (((uint32_t) sb[(*c >> 8) & 0xFF]) << 8) ^ |
| 281 | (((uint32_t) sc[(*c >> 16) & 0xFF]) << 16) ^ |
| 282 | (((uint32_t) sd[ *c >> 24 ]) << 24); |
Manuel Pégourié-Gonnard | 7fc0879 | 2018-03-01 09:33:20 +0100 | [diff] [blame] | 283 | *d = ( (uint32_t) sa[ *d & 0xFF] ) ^ |
Manuel Pégourié-Gonnard | 64744f8 | 2018-02-21 12:35:19 +0100 | [diff] [blame] | 284 | (((uint32_t) sb[(*d >> 8) & 0xFF]) << 8) ^ |
| 285 | (((uint32_t) sc[(*d >> 16) & 0xFF]) << 16) ^ |
| 286 | (((uint32_t) sd[ *d >> 24 ]) << 24); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 289 | /* |
| 290 | * S-Boxes |
| 291 | */ |
Manuel Pégourié-Gonnard | 12e2fbd | 2018-05-22 13:01:09 +0200 | [diff] [blame] | 292 | static const uint8_t aria_sb1[256] = |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 293 | { |
| 294 | 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, |
| 295 | 0xFE, 0xD7, 0xAB, 0x76, 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, |
| 296 | 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0, 0xB7, 0xFD, 0x93, 0x26, |
| 297 | 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15, |
| 298 | 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, |
| 299 | 0xEB, 0x27, 0xB2, 0x75, 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, |
| 300 | 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84, 0x53, 0xD1, 0x00, 0xED, |
| 301 | 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF, |
| 302 | 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, |
| 303 | 0x50, 0x3C, 0x9F, 0xA8, 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, |
| 304 | 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2, 0xCD, 0x0C, 0x13, 0xEC, |
| 305 | 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73, |
| 306 | 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, |
| 307 | 0xDE, 0x5E, 0x0B, 0xDB, 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, |
| 308 | 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79, 0xE7, 0xC8, 0x37, 0x6D, |
| 309 | 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08, |
| 310 | 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, |
| 311 | 0x4B, 0xBD, 0x8B, 0x8A, 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, |
| 312 | 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E, 0xE1, 0xF8, 0x98, 0x11, |
| 313 | 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF, |
| 314 | 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, |
| 315 | 0xB0, 0x54, 0xBB, 0x16 |
| 316 | }; |
| 317 | |
Manuel Pégourié-Gonnard | 12e2fbd | 2018-05-22 13:01:09 +0200 | [diff] [blame] | 318 | static const uint8_t aria_sb2[256] = |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 319 | { |
| 320 | 0xE2, 0x4E, 0x54, 0xFC, 0x94, 0xC2, 0x4A, 0xCC, 0x62, 0x0D, 0x6A, 0x46, |
| 321 | 0x3C, 0x4D, 0x8B, 0xD1, 0x5E, 0xFA, 0x64, 0xCB, 0xB4, 0x97, 0xBE, 0x2B, |
| 322 | 0xBC, 0x77, 0x2E, 0x03, 0xD3, 0x19, 0x59, 0xC1, 0x1D, 0x06, 0x41, 0x6B, |
| 323 | 0x55, 0xF0, 0x99, 0x69, 0xEA, 0x9C, 0x18, 0xAE, 0x63, 0xDF, 0xE7, 0xBB, |
| 324 | 0x00, 0x73, 0x66, 0xFB, 0x96, 0x4C, 0x85, 0xE4, 0x3A, 0x09, 0x45, 0xAA, |
| 325 | 0x0F, 0xEE, 0x10, 0xEB, 0x2D, 0x7F, 0xF4, 0x29, 0xAC, 0xCF, 0xAD, 0x91, |
| 326 | 0x8D, 0x78, 0xC8, 0x95, 0xF9, 0x2F, 0xCE, 0xCD, 0x08, 0x7A, 0x88, 0x38, |
| 327 | 0x5C, 0x83, 0x2A, 0x28, 0x47, 0xDB, 0xB8, 0xC7, 0x93, 0xA4, 0x12, 0x53, |
| 328 | 0xFF, 0x87, 0x0E, 0x31, 0x36, 0x21, 0x58, 0x48, 0x01, 0x8E, 0x37, 0x74, |
| 329 | 0x32, 0xCA, 0xE9, 0xB1, 0xB7, 0xAB, 0x0C, 0xD7, 0xC4, 0x56, 0x42, 0x26, |
| 330 | 0x07, 0x98, 0x60, 0xD9, 0xB6, 0xB9, 0x11, 0x40, 0xEC, 0x20, 0x8C, 0xBD, |
| 331 | 0xA0, 0xC9, 0x84, 0x04, 0x49, 0x23, 0xF1, 0x4F, 0x50, 0x1F, 0x13, 0xDC, |
| 332 | 0xD8, 0xC0, 0x9E, 0x57, 0xE3, 0xC3, 0x7B, 0x65, 0x3B, 0x02, 0x8F, 0x3E, |
| 333 | 0xE8, 0x25, 0x92, 0xE5, 0x15, 0xDD, 0xFD, 0x17, 0xA9, 0xBF, 0xD4, 0x9A, |
| 334 | 0x7E, 0xC5, 0x39, 0x67, 0xFE, 0x76, 0x9D, 0x43, 0xA7, 0xE1, 0xD0, 0xF5, |
| 335 | 0x68, 0xF2, 0x1B, 0x34, 0x70, 0x05, 0xA3, 0x8A, 0xD5, 0x79, 0x86, 0xA8, |
| 336 | 0x30, 0xC6, 0x51, 0x4B, 0x1E, 0xA6, 0x27, 0xF6, 0x35, 0xD2, 0x6E, 0x24, |
| 337 | 0x16, 0x82, 0x5F, 0xDA, 0xE6, 0x75, 0xA2, 0xEF, 0x2C, 0xB2, 0x1C, 0x9F, |
| 338 | 0x5D, 0x6F, 0x80, 0x0A, 0x72, 0x44, 0x9B, 0x6C, 0x90, 0x0B, 0x5B, 0x33, |
| 339 | 0x7D, 0x5A, 0x52, 0xF3, 0x61, 0xA1, 0xF7, 0xB0, 0xD6, 0x3F, 0x7C, 0x6D, |
| 340 | 0xED, 0x14, 0xE0, 0xA5, 0x3D, 0x22, 0xB3, 0xF8, 0x89, 0xDE, 0x71, 0x1A, |
| 341 | 0xAF, 0xBA, 0xB5, 0x81 |
| 342 | }; |
| 343 | |
Manuel Pégourié-Gonnard | 12e2fbd | 2018-05-22 13:01:09 +0200 | [diff] [blame] | 344 | static const uint8_t aria_is1[256] = |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 345 | { |
| 346 | 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E, |
| 347 | 0x81, 0xF3, 0xD7, 0xFB, 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, |
| 348 | 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB, 0x54, 0x7B, 0x94, 0x32, |
| 349 | 0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E, |
| 350 | 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49, |
| 351 | 0x6D, 0x8B, 0xD1, 0x25, 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, |
| 352 | 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92, 0x6C, 0x70, 0x48, 0x50, |
| 353 | 0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84, |
| 354 | 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05, |
| 355 | 0xB8, 0xB3, 0x45, 0x06, 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, |
| 356 | 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B, 0x3A, 0x91, 0x11, 0x41, |
| 357 | 0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73, |
| 358 | 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8, |
| 359 | 0x1C, 0x75, 0xDF, 0x6E, 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, |
| 360 | 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B, 0xFC, 0x56, 0x3E, 0x4B, |
| 361 | 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4, |
| 362 | 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, |
| 363 | 0x27, 0x80, 0xEC, 0x5F, 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, |
| 364 | 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF, 0xA0, 0xE0, 0x3B, 0x4D, |
| 365 | 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61, |
| 366 | 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, |
| 367 | 0x55, 0x21, 0x0C, 0x7D |
| 368 | }; |
| 369 | |
Manuel Pégourié-Gonnard | 12e2fbd | 2018-05-22 13:01:09 +0200 | [diff] [blame] | 370 | static const uint8_t aria_is2[256] = |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 371 | { |
| 372 | 0x30, 0x68, 0x99, 0x1B, 0x87, 0xB9, 0x21, 0x78, 0x50, 0x39, 0xDB, 0xE1, |
| 373 | 0x72, 0x09, 0x62, 0x3C, 0x3E, 0x7E, 0x5E, 0x8E, 0xF1, 0xA0, 0xCC, 0xA3, |
| 374 | 0x2A, 0x1D, 0xFB, 0xB6, 0xD6, 0x20, 0xC4, 0x8D, 0x81, 0x65, 0xF5, 0x89, |
| 375 | 0xCB, 0x9D, 0x77, 0xC6, 0x57, 0x43, 0x56, 0x17, 0xD4, 0x40, 0x1A, 0x4D, |
| 376 | 0xC0, 0x63, 0x6C, 0xE3, 0xB7, 0xC8, 0x64, 0x6A, 0x53, 0xAA, 0x38, 0x98, |
| 377 | 0x0C, 0xF4, 0x9B, 0xED, 0x7F, 0x22, 0x76, 0xAF, 0xDD, 0x3A, 0x0B, 0x58, |
| 378 | 0x67, 0x88, 0x06, 0xC3, 0x35, 0x0D, 0x01, 0x8B, 0x8C, 0xC2, 0xE6, 0x5F, |
| 379 | 0x02, 0x24, 0x75, 0x93, 0x66, 0x1E, 0xE5, 0xE2, 0x54, 0xD8, 0x10, 0xCE, |
| 380 | 0x7A, 0xE8, 0x08, 0x2C, 0x12, 0x97, 0x32, 0xAB, 0xB4, 0x27, 0x0A, 0x23, |
| 381 | 0xDF, 0xEF, 0xCA, 0xD9, 0xB8, 0xFA, 0xDC, 0x31, 0x6B, 0xD1, 0xAD, 0x19, |
| 382 | 0x49, 0xBD, 0x51, 0x96, 0xEE, 0xE4, 0xA8, 0x41, 0xDA, 0xFF, 0xCD, 0x55, |
| 383 | 0x86, 0x36, 0xBE, 0x61, 0x52, 0xF8, 0xBB, 0x0E, 0x82, 0x48, 0x69, 0x9A, |
| 384 | 0xE0, 0x47, 0x9E, 0x5C, 0x04, 0x4B, 0x34, 0x15, 0x79, 0x26, 0xA7, 0xDE, |
| 385 | 0x29, 0xAE, 0x92, 0xD7, 0x84, 0xE9, 0xD2, 0xBA, 0x5D, 0xF3, 0xC5, 0xB0, |
| 386 | 0xBF, 0xA4, 0x3B, 0x71, 0x44, 0x46, 0x2B, 0xFC, 0xEB, 0x6F, 0xD5, 0xF6, |
| 387 | 0x14, 0xFE, 0x7C, 0x70, 0x5A, 0x7D, 0xFD, 0x2F, 0x18, 0x83, 0x16, 0xA5, |
| 388 | 0x91, 0x1F, 0x05, 0x95, 0x74, 0xA9, 0xC1, 0x5B, 0x4A, 0x85, 0x6D, 0x13, |
| 389 | 0x07, 0x4F, 0x4E, 0x45, 0xB2, 0x0F, 0xC9, 0x1C, 0xA6, 0xBC, 0xEC, 0x73, |
| 390 | 0x90, 0x7B, 0xCF, 0x59, 0x8F, 0xA1, 0xF9, 0x2D, 0xF2, 0xB1, 0x00, 0x94, |
| 391 | 0x37, 0x9F, 0xD0, 0x2E, 0x9C, 0x6E, 0x28, 0x3F, 0x80, 0xF0, 0x3D, 0xD3, |
| 392 | 0x25, 0x8A, 0xB5, 0xE7, 0x42, 0xB3, 0xC7, 0xEA, 0xF7, 0x4C, 0x11, 0x33, |
| 393 | 0x03, 0xA2, 0xAC, 0x60 |
| 394 | }; |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 395 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 396 | /* |
| 397 | * Helper for key schedule: r = FO( p, k ) ^ x |
| 398 | */ |
Manuel Pégourié-Gonnard | 7fc0879 | 2018-03-01 09:33:20 +0100 | [diff] [blame] | 399 | static void aria_fo_xor( uint32_t r[4], const uint32_t p[4], |
| 400 | const uint32_t k[4], const uint32_t x[4] ) |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 401 | { |
| 402 | uint32_t a, b, c, d; |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 403 | |
| 404 | a = p[0] ^ k[0]; |
| 405 | b = p[1] ^ k[1]; |
| 406 | c = p[2] ^ k[2]; |
| 407 | d = p[3] ^ k[3]; |
| 408 | |
Manuel Pégourié-Gonnard | 64744f8 | 2018-02-21 12:35:19 +0100 | [diff] [blame] | 409 | aria_sl( &a, &b, &c, &d, aria_sb1, aria_sb2, aria_is1, aria_is2 ); |
| 410 | aria_a( &a, &b, &c, &d ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 411 | |
| 412 | r[0] = a ^ x[0]; |
| 413 | r[1] = b ^ x[1]; |
| 414 | r[2] = c ^ x[2]; |
| 415 | r[3] = d ^ x[3]; |
| 416 | } |
| 417 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 418 | /* |
| 419 | * Helper for key schedule: r = FE( p, k ) ^ x |
| 420 | */ |
Manuel Pégourié-Gonnard | 7fc0879 | 2018-03-01 09:33:20 +0100 | [diff] [blame] | 421 | static void aria_fe_xor( uint32_t r[4], const uint32_t p[4], |
| 422 | const uint32_t k[4], const uint32_t x[4] ) |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 423 | { |
| 424 | uint32_t a, b, c, d; |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 425 | |
| 426 | a = p[0] ^ k[0]; |
| 427 | b = p[1] ^ k[1]; |
| 428 | c = p[2] ^ k[2]; |
| 429 | d = p[3] ^ k[3]; |
| 430 | |
Manuel Pégourié-Gonnard | 64744f8 | 2018-02-21 12:35:19 +0100 | [diff] [blame] | 431 | aria_sl( &a, &b, &c, &d, aria_is1, aria_is2, aria_sb1, aria_sb2 ); |
| 432 | aria_a( &a, &b, &c, &d ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 433 | |
| 434 | r[0] = a ^ x[0]; |
| 435 | r[1] = b ^ x[1]; |
| 436 | r[2] = c ^ x[2]; |
| 437 | r[3] = d ^ x[3]; |
| 438 | } |
| 439 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 440 | /* |
| 441 | * Big endian 128-bit rotation: r = a ^ (b <<< n), used only in key setup. |
| 442 | * |
| 443 | * We chose to store bytes into 32-bit words in little-endian format (see |
| 444 | * GET/PUT_UINT32_LE) so we need to reverse bytes here. |
| 445 | */ |
Manuel Pégourié-Gonnard | 7fc0879 | 2018-03-01 09:33:20 +0100 | [diff] [blame] | 446 | static void aria_rot128( uint32_t r[4], const uint32_t a[4], |
| 447 | const uint32_t b[4], uint8_t n ) |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 448 | { |
Manuel Pégourié-Gonnard | 9cc8924 | 2018-02-21 09:44:29 +0100 | [diff] [blame] | 449 | uint8_t i, j; |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 450 | uint32_t t, u; |
| 451 | |
Manuel Pégourié-Gonnard | c76ceb6 | 2018-02-21 09:50:17 +0100 | [diff] [blame] | 452 | const uint8_t n1 = n % 32; // bit offset |
| 453 | const uint8_t n2 = n1 ? 32 - n1 : 0; // reverse bit offset |
Manuel Pégourié-Gonnard | 9cc8924 | 2018-02-21 09:44:29 +0100 | [diff] [blame] | 454 | |
Manuel Pégourié-Gonnard | 12e2fbd | 2018-05-22 13:01:09 +0200 | [diff] [blame] | 455 | j = ( n / 32 ) % 4; // initial word offset |
Manuel Pégourié-Gonnard | cac5008 | 2018-02-26 15:23:03 +0100 | [diff] [blame] | 456 | t = ARIA_P3( b[j] ); // big endian |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 457 | for( i = 0; i < 4; i++ ) |
| 458 | { |
Manuel Pégourié-Gonnard | 12e2fbd | 2018-05-22 13:01:09 +0200 | [diff] [blame] | 459 | j = ( j + 1 ) % 4; // get next word, big endian |
Manuel Pégourié-Gonnard | cac5008 | 2018-02-26 15:23:03 +0100 | [diff] [blame] | 460 | u = ARIA_P3( b[j] ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 461 | t <<= n1; // rotate |
Manuel Pégourié-Gonnard | c76ceb6 | 2018-02-21 09:50:17 +0100 | [diff] [blame] | 462 | t |= u >> n2; |
Manuel Pégourié-Gonnard | cac5008 | 2018-02-26 15:23:03 +0100 | [diff] [blame] | 463 | t = ARIA_P3( t ); // back to little endian |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 464 | r[i] = a[i] ^ t; // store |
| 465 | t = u; // move to next word |
| 466 | } |
| 467 | } |
| 468 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 469 | /* |
| 470 | * Set encryption key |
| 471 | */ |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 472 | int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx, |
| 473 | const unsigned char *key, unsigned int keybits ) |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 474 | { |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 475 | /* round constant masks */ |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 476 | const uint32_t rc[3][4] = |
| 477 | { |
| 478 | { 0xB7C17C51, 0x940A2227, 0xE8AB13FE, 0xE06E9AFA }, |
| 479 | { 0xCC4AB16D, 0x20C8219E, 0xD5B128FF, 0xB0E25DEF }, |
| 480 | { 0x1D3792DB, 0x70E92621, 0x75972403, 0x0EC9E804 } |
| 481 | }; |
| 482 | |
| 483 | int i; |
| 484 | uint32_t w[4][4], *w2; |
Hanno Becker | b54ae0b | 2018-12-11 21:51:32 +0000 | [diff] [blame] | 485 | ARIA_VALIDATE_RET( ctx != NULL ); |
| 486 | ARIA_VALIDATE_RET( key != NULL ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 487 | |
Manuel Pégourié-Gonnard | 7fc0879 | 2018-03-01 09:33:20 +0100 | [diff] [blame] | 488 | if( keybits != 128 && keybits != 192 && keybits != 256 ) |
Hanno Becker | 2f47550 | 2018-12-17 13:19:06 +0000 | [diff] [blame] | 489 | return( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 490 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 491 | /* Copy key to W0 (and potential remainder to W1) */ |
| 492 | GET_UINT32_LE( w[0][0], key, 0 ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 493 | GET_UINT32_LE( w[0][1], key, 4 ); |
| 494 | GET_UINT32_LE( w[0][2], key, 8 ); |
| 495 | GET_UINT32_LE( w[0][3], key, 12 ); |
| 496 | |
Manuel Pégourié-Gonnard | 7fc0879 | 2018-03-01 09:33:20 +0100 | [diff] [blame] | 497 | memset( w[1], 0, 16 ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 498 | if( keybits >= 192 ) |
| 499 | { |
| 500 | GET_UINT32_LE( w[1][0], key, 16 ); // 192 bit key |
| 501 | GET_UINT32_LE( w[1][1], key, 20 ); |
| 502 | } |
| 503 | if( keybits == 256 ) |
| 504 | { |
| 505 | GET_UINT32_LE( w[1][2], key, 24 ); // 256 bit key |
| 506 | GET_UINT32_LE( w[1][3], key, 28 ); |
| 507 | } |
| 508 | |
Manuel Pégourié-Gonnard | 12e2fbd | 2018-05-22 13:01:09 +0200 | [diff] [blame] | 509 | i = ( keybits - 128 ) >> 6; // index: 0, 1, 2 |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 510 | ctx->nr = 12 + 2 * i; // no. rounds: 12, 14, 16 |
| 511 | |
Manuel Pégourié-Gonnard | a6d639e | 2018-02-20 13:45:44 +0100 | [diff] [blame] | 512 | aria_fo_xor( w[1], w[0], rc[i], w[1] ); // W1 = FO(W0, CK1) ^ KR |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 513 | i = i < 2 ? i + 1 : 0; |
Manuel Pégourié-Gonnard | a6d639e | 2018-02-20 13:45:44 +0100 | [diff] [blame] | 514 | aria_fe_xor( w[2], w[1], rc[i], w[0] ); // W2 = FE(W1, CK2) ^ W0 |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 515 | i = i < 2 ? i + 1 : 0; |
Manuel Pégourié-Gonnard | a6d639e | 2018-02-20 13:45:44 +0100 | [diff] [blame] | 516 | aria_fo_xor( w[3], w[2], rc[i], w[1] ); // W3 = FO(W2, CK3) ^ W1 |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 517 | |
| 518 | for( i = 0; i < 4; i++ ) // create round keys |
| 519 | { |
| 520 | w2 = w[(i + 1) & 3]; |
Manuel Pégourié-Gonnard | 9cc8924 | 2018-02-21 09:44:29 +0100 | [diff] [blame] | 521 | aria_rot128( ctx->rk[i ], w[i], w2, 128 - 19 ); |
| 522 | aria_rot128( ctx->rk[i + 4], w[i], w2, 128 - 31 ); |
| 523 | aria_rot128( ctx->rk[i + 8], w[i], w2, 61 ); |
| 524 | aria_rot128( ctx->rk[i + 12], w[i], w2, 31 ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 525 | } |
| 526 | aria_rot128( ctx->rk[16], w[0], w[1], 19 ); |
| 527 | |
Manuel Pégourié-Gonnard | 89924dd | 2018-05-22 13:07:07 +0200 | [diff] [blame] | 528 | /* w holds enough info to reconstruct the round keys */ |
Manuel Pégourié-Gonnard | 7124fb6 | 2018-05-22 16:05:33 +0200 | [diff] [blame] | 529 | mbedtls_platform_zeroize( w, sizeof( w ) ); |
Manuel Pégourié-Gonnard | 89924dd | 2018-05-22 13:07:07 +0200 | [diff] [blame] | 530 | |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 531 | return( 0 ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 534 | /* |
| 535 | * Set decryption key |
| 536 | */ |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 537 | int mbedtls_aria_setkey_dec( mbedtls_aria_context *ctx, |
| 538 | const unsigned char *key, unsigned int keybits ) |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 539 | { |
| 540 | int i, j, k, ret; |
Hanno Becker | b54ae0b | 2018-12-11 21:51:32 +0000 | [diff] [blame] | 541 | ARIA_VALIDATE_RET( ctx != NULL ); |
| 542 | ARIA_VALIDATE_RET( key != NULL ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 543 | |
| 544 | ret = mbedtls_aria_setkey_enc( ctx, key, keybits ); |
| 545 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 546 | return( ret ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 547 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 548 | /* flip the order of round keys */ |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 549 | for( i = 0, j = ctx->nr; i < j; i++, j-- ) |
| 550 | { |
| 551 | for( k = 0; k < 4; k++ ) |
| 552 | { |
Manuel Pégourié-Gonnard | e1ad749 | 2018-02-20 13:59:05 +0100 | [diff] [blame] | 553 | uint32_t t = ctx->rk[i][k]; |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 554 | ctx->rk[i][k] = ctx->rk[j][k]; |
| 555 | ctx->rk[j][k] = t; |
| 556 | } |
| 557 | } |
| 558 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 559 | /* apply affine transform to middle keys */ |
Manuel Pégourié-Gonnard | 7fc0879 | 2018-03-01 09:33:20 +0100 | [diff] [blame] | 560 | for( i = 1; i < ctx->nr; i++ ) |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 561 | { |
| 562 | aria_a( &ctx->rk[i][0], &ctx->rk[i][1], |
| 563 | &ctx->rk[i][2], &ctx->rk[i][3] ); |
| 564 | } |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 565 | |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 566 | return( 0 ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 569 | /* |
| 570 | * Encrypt a block |
| 571 | */ |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 572 | int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx, |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 573 | const unsigned char input[MBEDTLS_ARIA_BLOCKSIZE], |
| 574 | unsigned char output[MBEDTLS_ARIA_BLOCKSIZE] ) |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 575 | { |
| 576 | int i; |
| 577 | |
| 578 | uint32_t a, b, c, d; |
Hanno Becker | b54ae0b | 2018-12-11 21:51:32 +0000 | [diff] [blame] | 579 | ARIA_VALIDATE_RET( ctx != NULL ); |
| 580 | ARIA_VALIDATE_RET( input != NULL ); |
| 581 | ARIA_VALIDATE_RET( output != NULL ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 582 | |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 583 | GET_UINT32_LE( a, input, 0 ); |
| 584 | GET_UINT32_LE( b, input, 4 ); |
| 585 | GET_UINT32_LE( c, input, 8 ); |
| 586 | GET_UINT32_LE( d, input, 12 ); |
| 587 | |
| 588 | i = 0; |
Manuel Pégourié-Gonnard | 7fc0879 | 2018-03-01 09:33:20 +0100 | [diff] [blame] | 589 | while( 1 ) |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 590 | { |
| 591 | a ^= ctx->rk[i][0]; |
| 592 | b ^= ctx->rk[i][1]; |
| 593 | c ^= ctx->rk[i][2]; |
| 594 | d ^= ctx->rk[i][3]; |
| 595 | i++; |
Manuel Pégourié-Gonnard | 64744f8 | 2018-02-21 12:35:19 +0100 | [diff] [blame] | 596 | |
| 597 | aria_sl( &a, &b, &c, &d, aria_sb1, aria_sb2, aria_is1, aria_is2 ); |
| 598 | aria_a( &a, &b, &c, &d ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 599 | |
| 600 | a ^= ctx->rk[i][0]; |
| 601 | b ^= ctx->rk[i][1]; |
| 602 | c ^= ctx->rk[i][2]; |
| 603 | d ^= ctx->rk[i][3]; |
| 604 | i++; |
Manuel Pégourié-Gonnard | 64744f8 | 2018-02-21 12:35:19 +0100 | [diff] [blame] | 605 | |
| 606 | aria_sl( &a, &b, &c, &d, aria_is1, aria_is2, aria_sb1, aria_sb2 ); |
Manuel Pégourié-Gonnard | 7fc0879 | 2018-03-01 09:33:20 +0100 | [diff] [blame] | 607 | if( i >= ctx->nr ) |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 608 | break; |
Manuel Pégourié-Gonnard | 64744f8 | 2018-02-21 12:35:19 +0100 | [diff] [blame] | 609 | aria_a( &a, &b, &c, &d ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Manuel Pégourié-Gonnard | 64744f8 | 2018-02-21 12:35:19 +0100 | [diff] [blame] | 612 | /* final key mixing */ |
| 613 | a ^= ctx->rk[i][0]; |
| 614 | b ^= ctx->rk[i][1]; |
| 615 | c ^= ctx->rk[i][2]; |
| 616 | d ^= ctx->rk[i][3]; |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 617 | |
| 618 | PUT_UINT32_LE( a, output, 0 ); |
| 619 | PUT_UINT32_LE( b, output, 4 ); |
| 620 | PUT_UINT32_LE( c, output, 8 ); |
| 621 | PUT_UINT32_LE( d, output, 12 ); |
| 622 | |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 623 | return( 0 ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 626 | /* Initialize context */ |
Markku-Juhani O. Saarinen | 6ba68d4 | 2017-12-01 14:26:21 +0000 | [diff] [blame] | 627 | void mbedtls_aria_init( mbedtls_aria_context *ctx ) |
| 628 | { |
Hanno Becker | b54ae0b | 2018-12-11 21:51:32 +0000 | [diff] [blame] | 629 | ARIA_VALIDATE( ctx != NULL ); |
Markku-Juhani O. Saarinen | 6ba68d4 | 2017-12-01 14:26:21 +0000 | [diff] [blame] | 630 | memset( ctx, 0, sizeof( mbedtls_aria_context ) ); |
| 631 | } |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 632 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 633 | /* Clear context */ |
Markku-Juhani O. Saarinen | 6ba68d4 | 2017-12-01 14:26:21 +0000 | [diff] [blame] | 634 | void mbedtls_aria_free( mbedtls_aria_context *ctx ) |
| 635 | { |
| 636 | if( ctx == NULL ) |
| 637 | return; |
| 638 | |
Manuel Pégourié-Gonnard | 7124fb6 | 2018-05-22 16:05:33 +0200 | [diff] [blame] | 639 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_aria_context ) ); |
Markku-Juhani O. Saarinen | 6ba68d4 | 2017-12-01 14:26:21 +0000 | [diff] [blame] | 640 | } |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 641 | |
| 642 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 643 | /* |
| 644 | * ARIA-CBC buffer encryption/decryption |
| 645 | */ |
| 646 | int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx, |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 647 | int mode, |
| 648 | size_t length, |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 649 | unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE], |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 650 | const unsigned char *input, |
| 651 | unsigned char *output ) |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 652 | { |
| 653 | int i; |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 654 | unsigned char temp[MBEDTLS_ARIA_BLOCKSIZE]; |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 655 | |
Hanno Becker | b54ae0b | 2018-12-11 21:51:32 +0000 | [diff] [blame] | 656 | ARIA_VALIDATE_RET( ctx != NULL ); |
| 657 | ARIA_VALIDATE_RET( mode == MBEDTLS_ARIA_ENCRYPT || |
| 658 | mode == MBEDTLS_ARIA_DECRYPT ); |
| 659 | ARIA_VALIDATE_RET( length == 0 || input != NULL ); |
| 660 | ARIA_VALIDATE_RET( length == 0 || output != NULL ); |
| 661 | ARIA_VALIDATE_RET( iv != NULL ); |
| 662 | |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 663 | if( length % MBEDTLS_ARIA_BLOCKSIZE ) |
Hanno Becker | a034369 | 2018-12-18 09:42:58 +0000 | [diff] [blame] | 664 | return( MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 665 | |
| 666 | if( mode == MBEDTLS_ARIA_DECRYPT ) |
| 667 | { |
| 668 | while( length > 0 ) |
| 669 | { |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 670 | memcpy( temp, input, MBEDTLS_ARIA_BLOCKSIZE ); |
Manuel Pégourié-Gonnard | 08c337d | 2018-05-22 13:18:01 +0200 | [diff] [blame] | 671 | mbedtls_aria_crypt_ecb( ctx, input, output ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 672 | |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 673 | for( i = 0; i < MBEDTLS_ARIA_BLOCKSIZE; i++ ) |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 674 | output[i] = (unsigned char)( output[i] ^ iv[i] ); |
| 675 | |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 676 | memcpy( iv, temp, MBEDTLS_ARIA_BLOCKSIZE ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 677 | |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 678 | input += MBEDTLS_ARIA_BLOCKSIZE; |
| 679 | output += MBEDTLS_ARIA_BLOCKSIZE; |
| 680 | length -= MBEDTLS_ARIA_BLOCKSIZE; |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | else |
| 684 | { |
| 685 | while( length > 0 ) |
| 686 | { |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 687 | for( i = 0; i < MBEDTLS_ARIA_BLOCKSIZE; i++ ) |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 688 | output[i] = (unsigned char)( input[i] ^ iv[i] ); |
| 689 | |
Manuel Pégourié-Gonnard | 08c337d | 2018-05-22 13:18:01 +0200 | [diff] [blame] | 690 | mbedtls_aria_crypt_ecb( ctx, output, output ); |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 691 | memcpy( iv, output, MBEDTLS_ARIA_BLOCKSIZE ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 692 | |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 693 | input += MBEDTLS_ARIA_BLOCKSIZE; |
| 694 | output += MBEDTLS_ARIA_BLOCKSIZE; |
| 695 | length -= MBEDTLS_ARIA_BLOCKSIZE; |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
| 699 | return( 0 ); |
| 700 | } |
| 701 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 702 | |
| 703 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
| 704 | /* |
| 705 | * ARIA-CFB128 buffer encryption/decryption |
| 706 | */ |
| 707 | int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx, |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 708 | int mode, |
| 709 | size_t length, |
| 710 | size_t *iv_off, |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 711 | unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE], |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 712 | const unsigned char *input, |
| 713 | unsigned char *output ) |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 714 | { |
Manuel Pégourié-Gonnard | 565e4e0 | 2018-05-22 13:30:28 +0200 | [diff] [blame] | 715 | unsigned char c; |
Hanno Becker | b54ae0b | 2018-12-11 21:51:32 +0000 | [diff] [blame] | 716 | size_t n; |
| 717 | |
| 718 | ARIA_VALIDATE_RET( ctx != NULL ); |
| 719 | ARIA_VALIDATE_RET( mode == MBEDTLS_ARIA_ENCRYPT || |
| 720 | mode == MBEDTLS_ARIA_DECRYPT ); |
| 721 | ARIA_VALIDATE_RET( length == 0 || input != NULL ); |
| 722 | ARIA_VALIDATE_RET( length == 0 || output != NULL ); |
| 723 | ARIA_VALIDATE_RET( iv != NULL ); |
| 724 | ARIA_VALIDATE_RET( iv_off != NULL ); |
| 725 | |
| 726 | n = *iv_off; |
| 727 | |
| 728 | /* An overly large value of n can lead to an unlimited |
| 729 | * buffer overflow. Therefore, guard against this |
| 730 | * outside of parameter validation. */ |
| 731 | if( n >= MBEDTLS_ARIA_BLOCKSIZE ) |
| 732 | return( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 733 | |
| 734 | if( mode == MBEDTLS_ARIA_DECRYPT ) |
| 735 | { |
| 736 | while( length-- ) |
| 737 | { |
| 738 | if( n == 0 ) |
Manuel Pégourié-Gonnard | 08c337d | 2018-05-22 13:18:01 +0200 | [diff] [blame] | 739 | mbedtls_aria_crypt_ecb( ctx, iv, iv ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 740 | |
| 741 | c = *input++; |
Manuel Pégourié-Gonnard | 565e4e0 | 2018-05-22 13:30:28 +0200 | [diff] [blame] | 742 | *output++ = c ^ iv[n]; |
| 743 | iv[n] = c; |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 744 | |
| 745 | n = ( n + 1 ) & 0x0F; |
| 746 | } |
| 747 | } |
| 748 | else |
| 749 | { |
| 750 | while( length-- ) |
| 751 | { |
| 752 | if( n == 0 ) |
Manuel Pégourié-Gonnard | 08c337d | 2018-05-22 13:18:01 +0200 | [diff] [blame] | 753 | mbedtls_aria_crypt_ecb( ctx, iv, iv ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 754 | |
| 755 | iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ ); |
| 756 | |
| 757 | n = ( n + 1 ) & 0x0F; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | *iv_off = n; |
| 762 | |
| 763 | return( 0 ); |
| 764 | } |
| 765 | #endif /* MBEDTLS_CIPHER_MODE_CFB */ |
| 766 | |
| 767 | #if defined(MBEDTLS_CIPHER_MODE_CTR) |
| 768 | /* |
| 769 | * ARIA-CTR buffer encryption/decryption |
| 770 | */ |
| 771 | int mbedtls_aria_crypt_ctr( mbedtls_aria_context *ctx, |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 772 | size_t length, |
| 773 | size_t *nc_off, |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 774 | unsigned char nonce_counter[MBEDTLS_ARIA_BLOCKSIZE], |
| 775 | unsigned char stream_block[MBEDTLS_ARIA_BLOCKSIZE], |
Manuel Pégourié-Gonnard | 4231e7f | 2018-02-28 10:54:31 +0100 | [diff] [blame] | 776 | const unsigned char *input, |
| 777 | unsigned char *output ) |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 778 | { |
| 779 | int c, i; |
Hanno Becker | b54ae0b | 2018-12-11 21:51:32 +0000 | [diff] [blame] | 780 | size_t n; |
| 781 | |
| 782 | ARIA_VALIDATE_RET( ctx != NULL ); |
| 783 | ARIA_VALIDATE_RET( length == 0 || input != NULL ); |
| 784 | ARIA_VALIDATE_RET( length == 0 || output != NULL ); |
| 785 | ARIA_VALIDATE_RET( nonce_counter != NULL ); |
| 786 | ARIA_VALIDATE_RET( stream_block != NULL ); |
| 787 | ARIA_VALIDATE_RET( nc_off != NULL ); |
| 788 | |
| 789 | n = *nc_off; |
| 790 | /* An overly large value of n can lead to an unlimited |
| 791 | * buffer overflow. Therefore, guard against this |
| 792 | * outside of parameter validation. */ |
| 793 | if( n >= MBEDTLS_ARIA_BLOCKSIZE ) |
| 794 | return( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA ); |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 795 | |
| 796 | while( length-- ) |
| 797 | { |
| 798 | if( n == 0 ) { |
Manuel Pégourié-Gonnard | 08c337d | 2018-05-22 13:18:01 +0200 | [diff] [blame] | 799 | mbedtls_aria_crypt_ecb( ctx, nonce_counter, |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 800 | stream_block ); |
| 801 | |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 802 | for( i = MBEDTLS_ARIA_BLOCKSIZE; i > 0; i-- ) |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 803 | if( ++nonce_counter[i - 1] != 0 ) |
| 804 | break; |
| 805 | } |
| 806 | c = *input++; |
| 807 | *output++ = (unsigned char)( c ^ stream_block[n] ); |
| 808 | |
| 809 | n = ( n + 1 ) & 0x0F; |
| 810 | } |
| 811 | |
| 812 | *nc_off = n; |
| 813 | |
| 814 | return( 0 ); |
| 815 | } |
| 816 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ |
| 817 | #endif /* !MBEDTLS_ARIA_ALT */ |
| 818 | |
| 819 | #if defined(MBEDTLS_SELF_TEST) |
| 820 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 821 | /* |
| 822 | * Basic ARIA ECB test vectors from RFC 5794 |
| 823 | */ |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 824 | static const uint8_t aria_test1_ecb_key[32] = // test key |
| 825 | { |
| 826 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // 128 bit |
| 827 | 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 828 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, // 192 bit |
| 829 | 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F // 256 bit |
| 830 | }; |
| 831 | |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 832 | static const uint8_t aria_test1_ecb_pt[MBEDTLS_ARIA_BLOCKSIZE] = // plaintext |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 833 | { |
| 834 | 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, // same for all |
| 835 | 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF // key sizes |
| 836 | }; |
| 837 | |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 838 | static const uint8_t aria_test1_ecb_ct[3][MBEDTLS_ARIA_BLOCKSIZE] = // ciphertext |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 839 | { |
| 840 | { 0xD7, 0x18, 0xFB, 0xD6, 0xAB, 0x64, 0x4C, 0x73, // 128 bit |
| 841 | 0x9D, 0xA9, 0x5F, 0x3B, 0xE6, 0x45, 0x17, 0x78 }, |
| 842 | { 0x26, 0x44, 0x9C, 0x18, 0x05, 0xDB, 0xE7, 0xAA, // 192 bit |
| 843 | 0x25, 0xA4, 0x68, 0xCE, 0x26, 0x3A, 0x9E, 0x79 }, |
| 844 | { 0xF9, 0x2B, 0xD7, 0xC7, 0x9F, 0xB7, 0x2E, 0x2F, // 256 bit |
| 845 | 0x2B, 0x8F, 0x80, 0xC1, 0x97, 0x2D, 0x24, 0xFC } |
| 846 | }; |
| 847 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 848 | /* |
| 849 | * Mode tests from "Test Vectors for ARIA" Version 1.0 |
| 850 | * http://210.104.33.10/ARIA/doc/ARIA-testvector-e.pdf |
| 851 | */ |
Markku-Juhani O. Saarinen | 3c0b53b | 2017-11-30 16:00:34 +0000 | [diff] [blame] | 852 | #if (defined(MBEDTLS_CIPHER_MODE_CBC) || defined(MBEDTLS_CIPHER_MODE_CFB) || \ |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 853 | defined(MBEDTLS_CIPHER_MODE_CTR)) |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 854 | static const uint8_t aria_test2_key[32] = |
| 855 | { |
| 856 | 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, // 128 bit |
| 857 | 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, |
| 858 | 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, // 192 bit |
| 859 | 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff // 256 bit |
| 860 | }; |
| 861 | |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 862 | static const uint8_t aria_test2_pt[48] = |
| 863 | { |
| 864 | 0x11, 0x11, 0x11, 0x11, 0xaa, 0xaa, 0xaa, 0xaa, // same for all |
| 865 | 0x11, 0x11, 0x11, 0x11, 0xbb, 0xbb, 0xbb, 0xbb, |
| 866 | 0x11, 0x11, 0x11, 0x11, 0xcc, 0xcc, 0xcc, 0xcc, |
| 867 | 0x11, 0x11, 0x11, 0x11, 0xdd, 0xdd, 0xdd, 0xdd, |
| 868 | 0x22, 0x22, 0x22, 0x22, 0xaa, 0xaa, 0xaa, 0xaa, |
| 869 | 0x22, 0x22, 0x22, 0x22, 0xbb, 0xbb, 0xbb, 0xbb, |
| 870 | }; |
Markku-Juhani O. Saarinen | 3c0b53b | 2017-11-30 16:00:34 +0000 | [diff] [blame] | 871 | #endif |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 872 | |
Markku-Juhani O. Saarinen | 3c0b53b | 2017-11-30 16:00:34 +0000 | [diff] [blame] | 873 | #if (defined(MBEDTLS_CIPHER_MODE_CBC) || defined(MBEDTLS_CIPHER_MODE_CFB)) |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 874 | static const uint8_t aria_test2_iv[MBEDTLS_ARIA_BLOCKSIZE] = |
Markku-Juhani O. Saarinen | 3c0b53b | 2017-11-30 16:00:34 +0000 | [diff] [blame] | 875 | { |
| 876 | 0x0f, 0x1e, 0x2d, 0x3c, 0x4b, 0x5a, 0x69, 0x78, // same for CBC, CFB |
| 877 | 0x87, 0x96, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0 // CTR has zero IV |
| 878 | }; |
| 879 | #endif |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 880 | |
| 881 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Manuel Pégourié-Gonnard | f3a46a9 | 2018-02-28 12:38:21 +0100 | [diff] [blame] | 882 | static const uint8_t aria_test2_cbc_ct[3][48] = // CBC ciphertext |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 883 | { |
| 884 | { 0x49, 0xd6, 0x18, 0x60, 0xb1, 0x49, 0x09, 0x10, // 128-bit key |
| 885 | 0x9c, 0xef, 0x0d, 0x22, 0xa9, 0x26, 0x81, 0x34, |
| 886 | 0xfa, 0xdf, 0x9f, 0xb2, 0x31, 0x51, 0xe9, 0x64, |
| 887 | 0x5f, 0xba, 0x75, 0x01, 0x8b, 0xdb, 0x15, 0x38, |
| 888 | 0xb5, 0x33, 0x34, 0x63, 0x4b, 0xbf, 0x7d, 0x4c, |
| 889 | 0xd4, 0xb5, 0x37, 0x70, 0x33, 0x06, 0x0c, 0x15 }, |
| 890 | { 0xaf, 0xe6, 0xcf, 0x23, 0x97, 0x4b, 0x53, 0x3c, // 192-bit key |
| 891 | 0x67, 0x2a, 0x82, 0x62, 0x64, 0xea, 0x78, 0x5f, |
| 892 | 0x4e, 0x4f, 0x7f, 0x78, 0x0d, 0xc7, 0xf3, 0xf1, |
| 893 | 0xe0, 0x96, 0x2b, 0x80, 0x90, 0x23, 0x86, 0xd5, |
| 894 | 0x14, 0xe9, 0xc3, 0xe7, 0x72, 0x59, 0xde, 0x92, |
| 895 | 0xdd, 0x11, 0x02, 0xff, 0xab, 0x08, 0x6c, 0x1e }, |
| 896 | { 0x52, 0x3a, 0x8a, 0x80, 0x6a, 0xe6, 0x21, 0xf1, // 256-bit key |
| 897 | 0x55, 0xfd, 0xd2, 0x8d, 0xbc, 0x34, 0xe1, 0xab, |
| 898 | 0x7b, 0x9b, 0x42, 0x43, 0x2a, 0xd8, 0xb2, 0xef, |
| 899 | 0xb9, 0x6e, 0x23, 0xb1, 0x3f, 0x0a, 0x6e, 0x52, |
| 900 | 0xf3, 0x61, 0x85, 0xd5, 0x0a, 0xd0, 0x02, 0xc5, |
| 901 | 0xf6, 0x01, 0xbe, 0xe5, 0x49, 0x3f, 0x11, 0x8b } |
| 902 | }; |
| 903 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 904 | |
| 905 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
Manuel Pégourié-Gonnard | f3a46a9 | 2018-02-28 12:38:21 +0100 | [diff] [blame] | 906 | static const uint8_t aria_test2_cfb_ct[3][48] = // CFB ciphertext |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 907 | { |
| 908 | { 0x37, 0x20, 0xe5, 0x3b, 0xa7, 0xd6, 0x15, 0x38, // 128-bit key |
| 909 | 0x34, 0x06, 0xb0, 0x9f, 0x0a, 0x05, 0xa2, 0x00, |
| 910 | 0xc0, 0x7c, 0x21, 0xe6, 0x37, 0x0f, 0x41, 0x3a, |
| 911 | 0x5d, 0x13, 0x25, 0x00, 0xa6, 0x82, 0x85, 0x01, |
| 912 | 0x7c, 0x61, 0xb4, 0x34, 0xc7, 0xb7, 0xca, 0x96, |
| 913 | 0x85, 0xa5, 0x10, 0x71, 0x86, 0x1e, 0x4d, 0x4b }, |
| 914 | { 0x41, 0x71, 0xf7, 0x19, 0x2b, 0xf4, 0x49, 0x54, // 192-bit key |
| 915 | 0x94, 0xd2, 0x73, 0x61, 0x29, 0x64, 0x0f, 0x5c, |
| 916 | 0x4d, 0x87, 0xa9, 0xa2, 0x13, 0x66, 0x4c, 0x94, |
| 917 | 0x48, 0x47, 0x7c, 0x6e, 0xcc, 0x20, 0x13, 0x59, |
| 918 | 0x8d, 0x97, 0x66, 0x95, 0x2d, 0xd8, 0xc3, 0x86, |
| 919 | 0x8f, 0x17, 0xe3, 0x6e, 0xf6, 0x6f, 0xd8, 0x4b }, |
| 920 | { 0x26, 0x83, 0x47, 0x05, 0xb0, 0xf2, 0xc0, 0xe2, // 256-bit key |
| 921 | 0x58, 0x8d, 0x4a, 0x7f, 0x09, 0x00, 0x96, 0x35, |
| 922 | 0xf2, 0x8b, 0xb9, 0x3d, 0x8c, 0x31, 0xf8, 0x70, |
| 923 | 0xec, 0x1e, 0x0b, 0xdb, 0x08, 0x2b, 0x66, 0xfa, |
| 924 | 0x40, 0x2d, 0xd9, 0xc2, 0x02, 0xbe, 0x30, 0x0c, |
| 925 | 0x45, 0x17, 0xd1, 0x96, 0xb1, 0x4d, 0x4c, 0xe1 } |
| 926 | }; |
| 927 | #endif /* MBEDTLS_CIPHER_MODE_CFB */ |
| 928 | |
| 929 | #if defined(MBEDTLS_CIPHER_MODE_CTR) |
Manuel Pégourié-Gonnard | f3a46a9 | 2018-02-28 12:38:21 +0100 | [diff] [blame] | 930 | static const uint8_t aria_test2_ctr_ct[3][48] = // CTR ciphertext |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 931 | { |
| 932 | { 0xac, 0x5d, 0x7d, 0xe8, 0x05, 0xa0, 0xbf, 0x1c, // 128-bit key |
| 933 | 0x57, 0xc8, 0x54, 0x50, 0x1a, 0xf6, 0x0f, 0xa1, |
| 934 | 0x14, 0x97, 0xe2, 0xa3, 0x45, 0x19, 0xde, 0xa1, |
| 935 | 0x56, 0x9e, 0x91, 0xe5, 0xb5, 0xcc, 0xae, 0x2f, |
| 936 | 0xf3, 0xbf, 0xa1, 0xbf, 0x97, 0x5f, 0x45, 0x71, |
| 937 | 0xf4, 0x8b, 0xe1, 0x91, 0x61, 0x35, 0x46, 0xc3 }, |
| 938 | { 0x08, 0x62, 0x5c, 0xa8, 0xfe, 0x56, 0x9c, 0x19, // 192-bit key |
| 939 | 0xba, 0x7a, 0xf3, 0x76, 0x0a, 0x6e, 0xd1, 0xce, |
| 940 | 0xf4, 0xd1, 0x99, 0x26, 0x3e, 0x99, 0x9d, 0xde, |
| 941 | 0x14, 0x08, 0x2d, 0xbb, 0xa7, 0x56, 0x0b, 0x79, |
| 942 | 0xa4, 0xc6, 0xb4, 0x56, 0xb8, 0x70, 0x7d, 0xce, |
| 943 | 0x75, 0x1f, 0x98, 0x54, 0xf1, 0x88, 0x93, 0xdf }, |
| 944 | { 0x30, 0x02, 0x6c, 0x32, 0x96, 0x66, 0x14, 0x17, // 256-bit key |
| 945 | 0x21, 0x17, 0x8b, 0x99, 0xc0, 0xa1, 0xf1, 0xb2, |
| 946 | 0xf0, 0x69, 0x40, 0x25, 0x3f, 0x7b, 0x30, 0x89, |
| 947 | 0xe2, 0xa3, 0x0e, 0xa8, 0x6a, 0xa3, 0xc8, 0x8f, |
| 948 | 0x59, 0x40, 0xf0, 0x5a, 0xd7, 0xee, 0x41, 0xd7, |
| 949 | 0x13, 0x47, 0xbb, 0x72, 0x61, 0xe3, 0x48, 0xf1 } |
| 950 | }; |
| 951 | #endif /* MBEDTLS_CIPHER_MODE_CFB */ |
| 952 | |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 953 | #define ARIA_SELF_TEST_IF_FAIL \ |
| 954 | { \ |
| 955 | if( verbose ) \ |
Ron Eldor | d1a4762 | 2018-08-13 13:49:52 +0300 | [diff] [blame] | 956 | mbedtls_printf( "failed\n" ); \ |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 957 | return( 1 ); \ |
| 958 | } else { \ |
| 959 | if( verbose ) \ |
Ron Eldor | d1a4762 | 2018-08-13 13:49:52 +0300 | [diff] [blame] | 960 | mbedtls_printf( "passed\n" ); \ |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 963 | /* |
| 964 | * Checkup routine |
| 965 | */ |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 966 | int mbedtls_aria_self_test( int verbose ) |
| 967 | { |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 968 | int i; |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 969 | uint8_t blk[MBEDTLS_ARIA_BLOCKSIZE]; |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 970 | mbedtls_aria_context ctx; |
Markku-Juhani O. Saarinen | 3c0b53b | 2017-11-30 16:00:34 +0000 | [diff] [blame] | 971 | |
Markku-Juhani O. Saarinen | 6ba68d4 | 2017-12-01 14:26:21 +0000 | [diff] [blame] | 972 | #if (defined(MBEDTLS_CIPHER_MODE_CFB) || defined(MBEDTLS_CIPHER_MODE_CTR)) |
| 973 | size_t j; |
Markku-Juhani O. Saarinen | 3c0b53b | 2017-11-30 16:00:34 +0000 | [diff] [blame] | 974 | #endif |
| 975 | |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 976 | #if (defined(MBEDTLS_CIPHER_MODE_CBC) || \ |
| 977 | defined(MBEDTLS_CIPHER_MODE_CFB) || \ |
| 978 | defined(MBEDTLS_CIPHER_MODE_CTR)) |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 979 | uint8_t buf[48], iv[MBEDTLS_ARIA_BLOCKSIZE]; |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 980 | #endif |
| 981 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 982 | /* |
| 983 | * Test set 1 |
| 984 | */ |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 985 | for( i = 0; i < 3; i++ ) |
| 986 | { |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 987 | /* test ECB encryption */ |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 988 | if( verbose ) |
Ron Eldor | d1a4762 | 2018-08-13 13:49:52 +0300 | [diff] [blame] | 989 | mbedtls_printf( " ARIA-ECB-%d (enc): ", 128 + 64 * i ); |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 990 | mbedtls_aria_setkey_enc( &ctx, aria_test1_ecb_key, 128 + 64 * i ); |
Manuel Pégourié-Gonnard | 08c337d | 2018-05-22 13:18:01 +0200 | [diff] [blame] | 991 | mbedtls_aria_crypt_ecb( &ctx, aria_test1_ecb_pt, blk ); |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 992 | if( memcmp( blk, aria_test1_ecb_ct[i], MBEDTLS_ARIA_BLOCKSIZE ) != 0 ) |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 993 | ARIA_SELF_TEST_IF_FAIL; |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 994 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 995 | /* test ECB decryption */ |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 996 | if( verbose ) |
Ron Eldor | d1a4762 | 2018-08-13 13:49:52 +0300 | [diff] [blame] | 997 | mbedtls_printf( " ARIA-ECB-%d (dec): ", 128 + 64 * i ); |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 998 | mbedtls_aria_setkey_dec( &ctx, aria_test1_ecb_key, 128 + 64 * i ); |
Manuel Pégourié-Gonnard | 08c337d | 2018-05-22 13:18:01 +0200 | [diff] [blame] | 999 | mbedtls_aria_crypt_ecb( &ctx, aria_test1_ecb_ct[i], blk ); |
Manuel Pégourié-Gonnard | 7fc0879 | 2018-03-01 09:33:20 +0100 | [diff] [blame] | 1000 | if( memcmp( blk, aria_test1_ecb_pt, MBEDTLS_ARIA_BLOCKSIZE ) != 0 ) |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1001 | ARIA_SELF_TEST_IF_FAIL; |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 1002 | } |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1003 | if( verbose ) |
Ron Eldor | d1a4762 | 2018-08-13 13:49:52 +0300 | [diff] [blame] | 1004 | mbedtls_printf( "\n" ); |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1005 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 1006 | /* |
| 1007 | * Test set 2 |
| 1008 | */ |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1009 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 1010 | for( i = 0; i < 3; i++ ) |
| 1011 | { |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 1012 | /* Test CBC encryption */ |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1013 | if( verbose ) |
Ron Eldor | d1a4762 | 2018-08-13 13:49:52 +0300 | [diff] [blame] | 1014 | mbedtls_printf( " ARIA-CBC-%d (enc): ", 128 + 64 * i ); |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1015 | mbedtls_aria_setkey_enc( &ctx, aria_test2_key, 128 + 64 * i ); |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 1016 | memcpy( iv, aria_test2_iv, MBEDTLS_ARIA_BLOCKSIZE ); |
Manuel Pégourié-Gonnard | 7fc0879 | 2018-03-01 09:33:20 +0100 | [diff] [blame] | 1017 | memset( buf, 0x55, sizeof( buf ) ); |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1018 | mbedtls_aria_crypt_cbc( &ctx, MBEDTLS_ARIA_ENCRYPT, 48, iv, |
| 1019 | aria_test2_pt, buf ); |
| 1020 | if( memcmp( buf, aria_test2_cbc_ct[i], 48 ) != 0 ) |
| 1021 | ARIA_SELF_TEST_IF_FAIL; |
| 1022 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 1023 | /* Test CBC decryption */ |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1024 | if( verbose ) |
Ron Eldor | d1a4762 | 2018-08-13 13:49:52 +0300 | [diff] [blame] | 1025 | mbedtls_printf( " ARIA-CBC-%d (dec): ", 128 + 64 * i ); |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1026 | mbedtls_aria_setkey_dec( &ctx, aria_test2_key, 128 + 64 * i ); |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 1027 | memcpy( iv, aria_test2_iv, MBEDTLS_ARIA_BLOCKSIZE ); |
Manuel Pégourié-Gonnard | 7fc0879 | 2018-03-01 09:33:20 +0100 | [diff] [blame] | 1028 | memset( buf, 0xAA, sizeof( buf ) ); |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1029 | mbedtls_aria_crypt_cbc( &ctx, MBEDTLS_ARIA_DECRYPT, 48, iv, |
| 1030 | aria_test2_cbc_ct[i], buf ); |
| 1031 | if( memcmp( buf, aria_test2_pt, 48 ) != 0 ) |
| 1032 | ARIA_SELF_TEST_IF_FAIL; |
| 1033 | } |
| 1034 | if( verbose ) |
Ron Eldor | d1a4762 | 2018-08-13 13:49:52 +0300 | [diff] [blame] | 1035 | mbedtls_printf( "\n" ); |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1036 | |
| 1037 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 1038 | |
| 1039 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
| 1040 | for( i = 0; i < 3; i++ ) |
| 1041 | { |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 1042 | /* Test CFB encryption */ |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1043 | if( verbose ) |
Ron Eldor | d1a4762 | 2018-08-13 13:49:52 +0300 | [diff] [blame] | 1044 | mbedtls_printf( " ARIA-CFB-%d (enc): ", 128 + 64 * i ); |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1045 | mbedtls_aria_setkey_enc( &ctx, aria_test2_key, 128 + 64 * i ); |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 1046 | memcpy( iv, aria_test2_iv, MBEDTLS_ARIA_BLOCKSIZE ); |
Manuel Pégourié-Gonnard | 7fc0879 | 2018-03-01 09:33:20 +0100 | [diff] [blame] | 1047 | memset( buf, 0x55, sizeof( buf ) ); |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1048 | j = 0; |
| 1049 | mbedtls_aria_crypt_cfb128( &ctx, MBEDTLS_ARIA_ENCRYPT, 48, &j, iv, |
| 1050 | aria_test2_pt, buf ); |
| 1051 | if( memcmp( buf, aria_test2_cfb_ct[i], 48 ) != 0 ) |
| 1052 | ARIA_SELF_TEST_IF_FAIL; |
| 1053 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 1054 | /* Test CFB decryption */ |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1055 | if( verbose ) |
Ron Eldor | d1a4762 | 2018-08-13 13:49:52 +0300 | [diff] [blame] | 1056 | mbedtls_printf( " ARIA-CFB-%d (dec): ", 128 + 64 * i ); |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1057 | mbedtls_aria_setkey_enc( &ctx, aria_test2_key, 128 + 64 * i ); |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 1058 | memcpy( iv, aria_test2_iv, MBEDTLS_ARIA_BLOCKSIZE ); |
Manuel Pégourié-Gonnard | 7fc0879 | 2018-03-01 09:33:20 +0100 | [diff] [blame] | 1059 | memset( buf, 0xAA, sizeof( buf ) ); |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1060 | j = 0; |
| 1061 | mbedtls_aria_crypt_cfb128( &ctx, MBEDTLS_ARIA_DECRYPT, 48, &j, |
| 1062 | iv, aria_test2_cfb_ct[i], buf ); |
| 1063 | if( memcmp( buf, aria_test2_pt, 48 ) != 0 ) |
| 1064 | ARIA_SELF_TEST_IF_FAIL; |
| 1065 | } |
| 1066 | if( verbose ) |
Ron Eldor | d1a4762 | 2018-08-13 13:49:52 +0300 | [diff] [blame] | 1067 | mbedtls_printf( "\n" ); |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1068 | #endif /* MBEDTLS_CIPHER_MODE_CFB */ |
| 1069 | |
| 1070 | #if defined(MBEDTLS_CIPHER_MODE_CTR) |
| 1071 | for( i = 0; i < 3; i++ ) |
| 1072 | { |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 1073 | /* Test CTR encryption */ |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1074 | if( verbose ) |
Ron Eldor | d1a4762 | 2018-08-13 13:49:52 +0300 | [diff] [blame] | 1075 | mbedtls_printf( " ARIA-CTR-%d (enc): ", 128 + 64 * i ); |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1076 | mbedtls_aria_setkey_enc( &ctx, aria_test2_key, 128 + 64 * i ); |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 1077 | memset( iv, 0, MBEDTLS_ARIA_BLOCKSIZE ); // IV = 0 |
Manuel Pégourié-Gonnard | 7fc0879 | 2018-03-01 09:33:20 +0100 | [diff] [blame] | 1078 | memset( buf, 0x55, sizeof( buf ) ); |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1079 | j = 0; |
| 1080 | mbedtls_aria_crypt_ctr( &ctx, 48, &j, iv, blk, |
| 1081 | aria_test2_pt, buf ); |
| 1082 | if( memcmp( buf, aria_test2_ctr_ct[i], 48 ) != 0 ) |
| 1083 | ARIA_SELF_TEST_IF_FAIL; |
| 1084 | |
Manuel Pégourié-Gonnard | a41ecda | 2018-02-21 10:33:26 +0100 | [diff] [blame] | 1085 | /* Test CTR decryption */ |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1086 | if( verbose ) |
Ron Eldor | d1a4762 | 2018-08-13 13:49:52 +0300 | [diff] [blame] | 1087 | mbedtls_printf( " ARIA-CTR-%d (dec): ", 128 + 64 * i ); |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1088 | mbedtls_aria_setkey_enc( &ctx, aria_test2_key, 128 + 64 * i ); |
Manuel Pégourié-Gonnard | 5ad88b6 | 2018-03-01 09:20:47 +0100 | [diff] [blame] | 1089 | memset( iv, 0, MBEDTLS_ARIA_BLOCKSIZE ); // IV = 0 |
Manuel Pégourié-Gonnard | 7fc0879 | 2018-03-01 09:33:20 +0100 | [diff] [blame] | 1090 | memset( buf, 0xAA, sizeof( buf ) ); |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1091 | j = 0; |
| 1092 | mbedtls_aria_crypt_ctr( &ctx, 48, &j, iv, blk, |
| 1093 | aria_test2_ctr_ct[i], buf ); |
| 1094 | if( memcmp( buf, aria_test2_pt, 48 ) != 0 ) |
| 1095 | ARIA_SELF_TEST_IF_FAIL; |
| 1096 | } |
| 1097 | if( verbose ) |
Ron Eldor | d1a4762 | 2018-08-13 13:49:52 +0300 | [diff] [blame] | 1098 | mbedtls_printf( "\n" ); |
Markku-Juhani O. Saarinen | 259fa60 | 2017-11-30 15:48:37 +0000 | [diff] [blame] | 1099 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ |
Markku-Juhani O. Saarinen | 41efbaa | 2017-11-30 11:37:55 +0000 | [diff] [blame] | 1100 | |
| 1101 | return( 0 ); |
| 1102 | } |
| 1103 | |
| 1104 | #endif /* MBEDTLS_SELF_TEST */ |
| 1105 | |
| 1106 | #endif /* MBEDTLS_ARIA_C */ |