mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:137634ff4186 1 /*
ansond 0:137634ff4186 2 * Camellia implementation
ansond 0:137634ff4186 3 *
ansond 0:137634ff4186 4 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
ansond 0:137634ff4186 5 *
ansond 0:137634ff4186 6 * This file is part of mbed TLS (https://tls.mbed.org)
ansond 0:137634ff4186 7 *
ansond 0:137634ff4186 8 * This program is free software; you can redistribute it and/or modify
ansond 0:137634ff4186 9 * it under the terms of the GNU General Public License as published by
ansond 0:137634ff4186 10 * the Free Software Foundation; either version 2 of the License, or
ansond 0:137634ff4186 11 * (at your option) any later version.
ansond 0:137634ff4186 12 *
ansond 0:137634ff4186 13 * This program is distributed in the hope that it will be useful,
ansond 0:137634ff4186 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ansond 0:137634ff4186 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ansond 0:137634ff4186 16 * GNU General Public License for more details.
ansond 0:137634ff4186 17 *
ansond 0:137634ff4186 18 * You should have received a copy of the GNU General Public License along
ansond 0:137634ff4186 19 * with this program; if not, write to the Free Software Foundation, Inc.,
ansond 0:137634ff4186 20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ansond 0:137634ff4186 21 */
ansond 0:137634ff4186 22 /*
ansond 0:137634ff4186 23 * The Camellia block cipher was designed by NTT and Mitsubishi Electric
ansond 0:137634ff4186 24 * Corporation.
ansond 0:137634ff4186 25 *
ansond 0:137634ff4186 26 * http://info.isl.ntt.co.jp/crypt/eng/camellia/dl/01espec.pdf
ansond 0:137634ff4186 27 */
ansond 0:137634ff4186 28
ansond 0:137634ff4186 29 #if !defined(POLARSSL_CONFIG_FILE)
ansond 0:137634ff4186 30 #include "polarssl/config.h"
ansond 0:137634ff4186 31 #else
ansond 0:137634ff4186 32 #include POLARSSL_CONFIG_FILE
ansond 0:137634ff4186 33 #endif
ansond 0:137634ff4186 34
ansond 0:137634ff4186 35 #if defined(POLARSSL_CAMELLIA_C)
ansond 0:137634ff4186 36
ansond 0:137634ff4186 37 #include "polarssl/camellia.h"
ansond 0:137634ff4186 38
ansond 0:137634ff4186 39 #if defined(POLARSSL_SELF_TEST)
ansond 0:137634ff4186 40 #include <string.h>
ansond 0:137634ff4186 41 #if defined(POLARSSL_PLATFORM_C)
ansond 0:137634ff4186 42 #include "polarssl/platform.h"
ansond 0:137634ff4186 43 #else
ansond 0:137634ff4186 44 #include <stdio.h>
ansond 0:137634ff4186 45 #define polarssl_printf printf
ansond 0:137634ff4186 46 #endif /* POLARSSL_PLATFORM_C */
ansond 0:137634ff4186 47 #endif /* POLARSSL_SELF_TEST */
ansond 0:137634ff4186 48
ansond 0:137634ff4186 49 #if !defined(POLARSSL_CAMELLIA_ALT)
ansond 0:137634ff4186 50
ansond 0:137634ff4186 51 /* Implementation that should never be optimized out by the compiler */
ansond 0:137634ff4186 52 static void polarssl_zeroize( void *v, size_t n ) {
ansond 0:137634ff4186 53 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
ansond 0:137634ff4186 54 }
ansond 0:137634ff4186 55
ansond 0:137634ff4186 56 /*
ansond 0:137634ff4186 57 * 32-bit integer manipulation macros (big endian)
ansond 0:137634ff4186 58 */
ansond 0:137634ff4186 59 #ifndef GET_UINT32_BE
ansond 0:137634ff4186 60 #define GET_UINT32_BE(n,b,i) \
ansond 0:137634ff4186 61 { \
ansond 0:137634ff4186 62 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
ansond 0:137634ff4186 63 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
ansond 0:137634ff4186 64 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
ansond 0:137634ff4186 65 | ( (uint32_t) (b)[(i) + 3] ); \
ansond 0:137634ff4186 66 }
ansond 0:137634ff4186 67 #endif
ansond 0:137634ff4186 68
ansond 0:137634ff4186 69 #ifndef PUT_UINT32_BE
ansond 0:137634ff4186 70 #define PUT_UINT32_BE(n,b,i) \
ansond 0:137634ff4186 71 { \
ansond 0:137634ff4186 72 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
ansond 0:137634ff4186 73 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
ansond 0:137634ff4186 74 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
ansond 0:137634ff4186 75 (b)[(i) + 3] = (unsigned char) ( (n) ); \
ansond 0:137634ff4186 76 }
ansond 0:137634ff4186 77 #endif
ansond 0:137634ff4186 78
ansond 0:137634ff4186 79 static const unsigned char SIGMA_CHARS[6][8] =
ansond 0:137634ff4186 80 {
ansond 0:137634ff4186 81 { 0xa0, 0x9e, 0x66, 0x7f, 0x3b, 0xcc, 0x90, 0x8b },
ansond 0:137634ff4186 82 { 0xb6, 0x7a, 0xe8, 0x58, 0x4c, 0xaa, 0x73, 0xb2 },
ansond 0:137634ff4186 83 { 0xc6, 0xef, 0x37, 0x2f, 0xe9, 0x4f, 0x82, 0xbe },
ansond 0:137634ff4186 84 { 0x54, 0xff, 0x53, 0xa5, 0xf1, 0xd3, 0x6f, 0x1c },
ansond 0:137634ff4186 85 { 0x10, 0xe5, 0x27, 0xfa, 0xde, 0x68, 0x2d, 0x1d },
ansond 0:137634ff4186 86 { 0xb0, 0x56, 0x88, 0xc2, 0xb3, 0xe6, 0xc1, 0xfd }
ansond 0:137634ff4186 87 };
ansond 0:137634ff4186 88
ansond 0:137634ff4186 89 #if defined(POLARSSL_CAMELLIA_SMALL_MEMORY)
ansond 0:137634ff4186 90
ansond 0:137634ff4186 91 static const unsigned char FSb[256] =
ansond 0:137634ff4186 92 {
ansond 0:137634ff4186 93 112,130, 44,236,179, 39,192,229,228,133, 87, 53,234, 12,174, 65,
ansond 0:137634ff4186 94 35,239,107,147, 69, 25,165, 33,237, 14, 79, 78, 29,101,146,189,
ansond 0:137634ff4186 95 134,184,175,143,124,235, 31,206, 62, 48,220, 95, 94,197, 11, 26,
ansond 0:137634ff4186 96 166,225, 57,202,213, 71, 93, 61,217, 1, 90,214, 81, 86,108, 77,
ansond 0:137634ff4186 97 139, 13,154,102,251,204,176, 45,116, 18, 43, 32,240,177,132,153,
ansond 0:137634ff4186 98 223, 76,203,194, 52,126,118, 5,109,183,169, 49,209, 23, 4,215,
ansond 0:137634ff4186 99 20, 88, 58, 97,222, 27, 17, 28, 50, 15,156, 22, 83, 24,242, 34,
ansond 0:137634ff4186 100 254, 68,207,178,195,181,122,145, 36, 8,232,168, 96,252,105, 80,
ansond 0:137634ff4186 101 170,208,160,125,161,137, 98,151, 84, 91, 30,149,224,255,100,210,
ansond 0:137634ff4186 102 16,196, 0, 72,163,247,117,219,138, 3,230,218, 9, 63,221,148,
ansond 0:137634ff4186 103 135, 92,131, 2,205, 74,144, 51,115,103,246,243,157,127,191,226,
ansond 0:137634ff4186 104 82,155,216, 38,200, 55,198, 59,129,150,111, 75, 19,190, 99, 46,
ansond 0:137634ff4186 105 233,121,167,140,159,110,188,142, 41,245,249,182, 47,253,180, 89,
ansond 0:137634ff4186 106 120,152, 6,106,231, 70,113,186,212, 37,171, 66,136,162,141,250,
ansond 0:137634ff4186 107 114, 7,185, 85,248,238,172, 10, 54, 73, 42,104, 60, 56,241,164,
ansond 0:137634ff4186 108 64, 40,211,123,187,201, 67,193, 21,227,173,244,119,199,128,158
ansond 0:137634ff4186 109 };
ansond 0:137634ff4186 110
ansond 0:137634ff4186 111 #define SBOX1(n) FSb[(n)]
ansond 0:137634ff4186 112 #define SBOX2(n) (unsigned char)((FSb[(n)] >> 7 ^ FSb[(n)] << 1) & 0xff)
ansond 0:137634ff4186 113 #define SBOX3(n) (unsigned char)((FSb[(n)] >> 1 ^ FSb[(n)] << 7) & 0xff)
ansond 0:137634ff4186 114 #define SBOX4(n) FSb[((n) << 1 ^ (n) >> 7) &0xff]
ansond 0:137634ff4186 115
ansond 0:137634ff4186 116 #else /* POLARSSL_CAMELLIA_SMALL_MEMORY */
ansond 0:137634ff4186 117
ansond 0:137634ff4186 118 static const unsigned char FSb[256] =
ansond 0:137634ff4186 119 {
ansond 0:137634ff4186 120 112, 130, 44, 236, 179, 39, 192, 229, 228, 133, 87, 53, 234, 12, 174, 65,
ansond 0:137634ff4186 121 35, 239, 107, 147, 69, 25, 165, 33, 237, 14, 79, 78, 29, 101, 146, 189,
ansond 0:137634ff4186 122 134, 184, 175, 143, 124, 235, 31, 206, 62, 48, 220, 95, 94, 197, 11, 26,
ansond 0:137634ff4186 123 166, 225, 57, 202, 213, 71, 93, 61, 217, 1, 90, 214, 81, 86, 108, 77,
ansond 0:137634ff4186 124 139, 13, 154, 102, 251, 204, 176, 45, 116, 18, 43, 32, 240, 177, 132, 153,
ansond 0:137634ff4186 125 223, 76, 203, 194, 52, 126, 118, 5, 109, 183, 169, 49, 209, 23, 4, 215,
ansond 0:137634ff4186 126 20, 88, 58, 97, 222, 27, 17, 28, 50, 15, 156, 22, 83, 24, 242, 34,
ansond 0:137634ff4186 127 254, 68, 207, 178, 195, 181, 122, 145, 36, 8, 232, 168, 96, 252, 105, 80,
ansond 0:137634ff4186 128 170, 208, 160, 125, 161, 137, 98, 151, 84, 91, 30, 149, 224, 255, 100, 210,
ansond 0:137634ff4186 129 16, 196, 0, 72, 163, 247, 117, 219, 138, 3, 230, 218, 9, 63, 221, 148,
ansond 0:137634ff4186 130 135, 92, 131, 2, 205, 74, 144, 51, 115, 103, 246, 243, 157, 127, 191, 226,
ansond 0:137634ff4186 131 82, 155, 216, 38, 200, 55, 198, 59, 129, 150, 111, 75, 19, 190, 99, 46,
ansond 0:137634ff4186 132 233, 121, 167, 140, 159, 110, 188, 142, 41, 245, 249, 182, 47, 253, 180, 89,
ansond 0:137634ff4186 133 120, 152, 6, 106, 231, 70, 113, 186, 212, 37, 171, 66, 136, 162, 141, 250,
ansond 0:137634ff4186 134 114, 7, 185, 85, 248, 238, 172, 10, 54, 73, 42, 104, 60, 56, 241, 164,
ansond 0:137634ff4186 135 64, 40, 211, 123, 187, 201, 67, 193, 21, 227, 173, 244, 119, 199, 128, 158
ansond 0:137634ff4186 136 };
ansond 0:137634ff4186 137
ansond 0:137634ff4186 138 static const unsigned char FSb2[256] =
ansond 0:137634ff4186 139 {
ansond 0:137634ff4186 140 224, 5, 88, 217, 103, 78, 129, 203, 201, 11, 174, 106, 213, 24, 93, 130,
ansond 0:137634ff4186 141 70, 223, 214, 39, 138, 50, 75, 66, 219, 28, 158, 156, 58, 202, 37, 123,
ansond 0:137634ff4186 142 13, 113, 95, 31, 248, 215, 62, 157, 124, 96, 185, 190, 188, 139, 22, 52,
ansond 0:137634ff4186 143 77, 195, 114, 149, 171, 142, 186, 122, 179, 2, 180, 173, 162, 172, 216, 154,
ansond 0:137634ff4186 144 23, 26, 53, 204, 247, 153, 97, 90, 232, 36, 86, 64, 225, 99, 9, 51,
ansond 0:137634ff4186 145 191, 152, 151, 133, 104, 252, 236, 10, 218, 111, 83, 98, 163, 46, 8, 175,
ansond 0:137634ff4186 146 40, 176, 116, 194, 189, 54, 34, 56, 100, 30, 57, 44, 166, 48, 229, 68,
ansond 0:137634ff4186 147 253, 136, 159, 101, 135, 107, 244, 35, 72, 16, 209, 81, 192, 249, 210, 160,
ansond 0:137634ff4186 148 85, 161, 65, 250, 67, 19, 196, 47, 168, 182, 60, 43, 193, 255, 200, 165,
ansond 0:137634ff4186 149 32, 137, 0, 144, 71, 239, 234, 183, 21, 6, 205, 181, 18, 126, 187, 41,
ansond 0:137634ff4186 150 15, 184, 7, 4, 155, 148, 33, 102, 230, 206, 237, 231, 59, 254, 127, 197,
ansond 0:137634ff4186 151 164, 55, 177, 76, 145, 110, 141, 118, 3, 45, 222, 150, 38, 125, 198, 92,
ansond 0:137634ff4186 152 211, 242, 79, 25, 63, 220, 121, 29, 82, 235, 243, 109, 94, 251, 105, 178,
ansond 0:137634ff4186 153 240, 49, 12, 212, 207, 140, 226, 117, 169, 74, 87, 132, 17, 69, 27, 245,
ansond 0:137634ff4186 154 228, 14, 115, 170, 241, 221, 89, 20, 108, 146, 84, 208, 120, 112, 227, 73,
ansond 0:137634ff4186 155 128, 80, 167, 246, 119, 147, 134, 131, 42, 199, 91, 233, 238, 143, 1, 61
ansond 0:137634ff4186 156 };
ansond 0:137634ff4186 157
ansond 0:137634ff4186 158 static const unsigned char FSb3[256] =
ansond 0:137634ff4186 159 {
ansond 0:137634ff4186 160 56, 65, 22, 118, 217, 147, 96, 242, 114, 194, 171, 154, 117, 6, 87, 160,
ansond 0:137634ff4186 161 145, 247, 181, 201, 162, 140, 210, 144, 246, 7, 167, 39, 142, 178, 73, 222,
ansond 0:137634ff4186 162 67, 92, 215, 199, 62, 245, 143, 103, 31, 24, 110, 175, 47, 226, 133, 13,
ansond 0:137634ff4186 163 83, 240, 156, 101, 234, 163, 174, 158, 236, 128, 45, 107, 168, 43, 54, 166,
ansond 0:137634ff4186 164 197, 134, 77, 51, 253, 102, 88, 150, 58, 9, 149, 16, 120, 216, 66, 204,
ansond 0:137634ff4186 165 239, 38, 229, 97, 26, 63, 59, 130, 182, 219, 212, 152, 232, 139, 2, 235,
ansond 0:137634ff4186 166 10, 44, 29, 176, 111, 141, 136, 14, 25, 135, 78, 11, 169, 12, 121, 17,
ansond 0:137634ff4186 167 127, 34, 231, 89, 225, 218, 61, 200, 18, 4, 116, 84, 48, 126, 180, 40,
ansond 0:137634ff4186 168 85, 104, 80, 190, 208, 196, 49, 203, 42, 173, 15, 202, 112, 255, 50, 105,
ansond 0:137634ff4186 169 8, 98, 0, 36, 209, 251, 186, 237, 69, 129, 115, 109, 132, 159, 238, 74,
ansond 0:137634ff4186 170 195, 46, 193, 1, 230, 37, 72, 153, 185, 179, 123, 249, 206, 191, 223, 113,
ansond 0:137634ff4186 171 41, 205, 108, 19, 100, 155, 99, 157, 192, 75, 183, 165, 137, 95, 177, 23,
ansond 0:137634ff4186 172 244, 188, 211, 70, 207, 55, 94, 71, 148, 250, 252, 91, 151, 254, 90, 172,
ansond 0:137634ff4186 173 60, 76, 3, 53, 243, 35, 184, 93, 106, 146, 213, 33, 68, 81, 198, 125,
ansond 0:137634ff4186 174 57, 131, 220, 170, 124, 119, 86, 5, 27, 164, 21, 52, 30, 28, 248, 82,
ansond 0:137634ff4186 175 32, 20, 233, 189, 221, 228, 161, 224, 138, 241, 214, 122, 187, 227, 64, 79
ansond 0:137634ff4186 176 };
ansond 0:137634ff4186 177
ansond 0:137634ff4186 178 static const unsigned char FSb4[256] =
ansond 0:137634ff4186 179 {
ansond 0:137634ff4186 180 112, 44, 179, 192, 228, 87, 234, 174, 35, 107, 69, 165, 237, 79, 29, 146,
ansond 0:137634ff4186 181 134, 175, 124, 31, 62, 220, 94, 11, 166, 57, 213, 93, 217, 90, 81, 108,
ansond 0:137634ff4186 182 139, 154, 251, 176, 116, 43, 240, 132, 223, 203, 52, 118, 109, 169, 209, 4,
ansond 0:137634ff4186 183 20, 58, 222, 17, 50, 156, 83, 242, 254, 207, 195, 122, 36, 232, 96, 105,
ansond 0:137634ff4186 184 170, 160, 161, 98, 84, 30, 224, 100, 16, 0, 163, 117, 138, 230, 9, 221,
ansond 0:137634ff4186 185 135, 131, 205, 144, 115, 246, 157, 191, 82, 216, 200, 198, 129, 111, 19, 99,
ansond 0:137634ff4186 186 233, 167, 159, 188, 41, 249, 47, 180, 120, 6, 231, 113, 212, 171, 136, 141,
ansond 0:137634ff4186 187 114, 185, 248, 172, 54, 42, 60, 241, 64, 211, 187, 67, 21, 173, 119, 128,
ansond 0:137634ff4186 188 130, 236, 39, 229, 133, 53, 12, 65, 239, 147, 25, 33, 14, 78, 101, 189,
ansond 0:137634ff4186 189 184, 143, 235, 206, 48, 95, 197, 26, 225, 202, 71, 61, 1, 214, 86, 77,
ansond 0:137634ff4186 190 13, 102, 204, 45, 18, 32, 177, 153, 76, 194, 126, 5, 183, 49, 23, 215,
ansond 0:137634ff4186 191 88, 97, 27, 28, 15, 22, 24, 34, 68, 178, 181, 145, 8, 168, 252, 80,
ansond 0:137634ff4186 192 208, 125, 137, 151, 91, 149, 255, 210, 196, 72, 247, 219, 3, 218, 63, 148,
ansond 0:137634ff4186 193 92, 2, 74, 51, 103, 243, 127, 226, 155, 38, 55, 59, 150, 75, 190, 46,
ansond 0:137634ff4186 194 121, 140, 110, 142, 245, 182, 253, 89, 152, 106, 70, 186, 37, 66, 162, 250,
ansond 0:137634ff4186 195 7, 85, 238, 10, 73, 104, 56, 164, 40, 123, 201, 193, 227, 244, 199, 158
ansond 0:137634ff4186 196 };
ansond 0:137634ff4186 197
ansond 0:137634ff4186 198 #define SBOX1(n) FSb[(n)]
ansond 0:137634ff4186 199 #define SBOX2(n) FSb2[(n)]
ansond 0:137634ff4186 200 #define SBOX3(n) FSb3[(n)]
ansond 0:137634ff4186 201 #define SBOX4(n) FSb4[(n)]
ansond 0:137634ff4186 202
ansond 0:137634ff4186 203 #endif /* POLARSSL_CAMELLIA_SMALL_MEMORY */
ansond 0:137634ff4186 204
ansond 0:137634ff4186 205 static const unsigned char shifts[2][4][4] =
ansond 0:137634ff4186 206 {
ansond 0:137634ff4186 207 {
ansond 0:137634ff4186 208 { 1, 1, 1, 1 }, /* KL */
ansond 0:137634ff4186 209 { 0, 0, 0, 0 }, /* KR */
ansond 0:137634ff4186 210 { 1, 1, 1, 1 }, /* KA */
ansond 0:137634ff4186 211 { 0, 0, 0, 0 } /* KB */
ansond 0:137634ff4186 212 },
ansond 0:137634ff4186 213 {
ansond 0:137634ff4186 214 { 1, 0, 1, 1 }, /* KL */
ansond 0:137634ff4186 215 { 1, 1, 0, 1 }, /* KR */
ansond 0:137634ff4186 216 { 1, 1, 1, 0 }, /* KA */
ansond 0:137634ff4186 217 { 1, 1, 0, 1 } /* KB */
ansond 0:137634ff4186 218 }
ansond 0:137634ff4186 219 };
ansond 0:137634ff4186 220
ansond 0:137634ff4186 221 static const signed char indexes[2][4][20] =
ansond 0:137634ff4186 222 {
ansond 0:137634ff4186 223 {
ansond 0:137634ff4186 224 { 0, 1, 2, 3, 8, 9, 10, 11, 38, 39,
ansond 0:137634ff4186 225 36, 37, 23, 20, 21, 22, 27, -1, -1, 26 }, /* KL -> RK */
ansond 0:137634ff4186 226 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
ansond 0:137634ff4186 227 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, /* KR -> RK */
ansond 0:137634ff4186 228 { 4, 5, 6, 7, 12, 13, 14, 15, 16, 17,
ansond 0:137634ff4186 229 18, 19, -1, 24, 25, -1, 31, 28, 29, 30 }, /* KA -> RK */
ansond 0:137634ff4186 230 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
ansond 0:137634ff4186 231 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } /* KB -> RK */
ansond 0:137634ff4186 232 },
ansond 0:137634ff4186 233 {
ansond 0:137634ff4186 234 { 0, 1, 2, 3, 61, 62, 63, 60, -1, -1,
ansond 0:137634ff4186 235 -1, -1, 27, 24, 25, 26, 35, 32, 33, 34 }, /* KL -> RK */
ansond 0:137634ff4186 236 { -1, -1, -1, -1, 8, 9, 10, 11, 16, 17,
ansond 0:137634ff4186 237 18, 19, -1, -1, -1, -1, 39, 36, 37, 38 }, /* KR -> RK */
ansond 0:137634ff4186 238 { -1, -1, -1, -1, 12, 13, 14, 15, 58, 59,
ansond 0:137634ff4186 239 56, 57, 31, 28, 29, 30, -1, -1, -1, -1 }, /* KA -> RK */
ansond 0:137634ff4186 240 { 4, 5, 6, 7, 65, 66, 67, 64, 20, 21,
ansond 0:137634ff4186 241 22, 23, -1, -1, -1, -1, 43, 40, 41, 42 } /* KB -> RK */
ansond 0:137634ff4186 242 }
ansond 0:137634ff4186 243 };
ansond 0:137634ff4186 244
ansond 0:137634ff4186 245 static const signed char transposes[2][20] =
ansond 0:137634ff4186 246 {
ansond 0:137634ff4186 247 {
ansond 0:137634ff4186 248 21, 22, 23, 20,
ansond 0:137634ff4186 249 -1, -1, -1, -1,
ansond 0:137634ff4186 250 18, 19, 16, 17,
ansond 0:137634ff4186 251 11, 8, 9, 10,
ansond 0:137634ff4186 252 15, 12, 13, 14
ansond 0:137634ff4186 253 },
ansond 0:137634ff4186 254 {
ansond 0:137634ff4186 255 25, 26, 27, 24,
ansond 0:137634ff4186 256 29, 30, 31, 28,
ansond 0:137634ff4186 257 18, 19, 16, 17,
ansond 0:137634ff4186 258 -1, -1, -1, -1,
ansond 0:137634ff4186 259 -1, -1, -1, -1
ansond 0:137634ff4186 260 }
ansond 0:137634ff4186 261 };
ansond 0:137634ff4186 262
ansond 0:137634ff4186 263 /* Shift macro for 128 bit strings with rotation smaller than 32 bits (!) */
ansond 0:137634ff4186 264 #define ROTL(DEST, SRC, SHIFT) \
ansond 0:137634ff4186 265 { \
ansond 0:137634ff4186 266 (DEST)[0] = (SRC)[0] << (SHIFT) ^ (SRC)[1] >> (32 - (SHIFT)); \
ansond 0:137634ff4186 267 (DEST)[1] = (SRC)[1] << (SHIFT) ^ (SRC)[2] >> (32 - (SHIFT)); \
ansond 0:137634ff4186 268 (DEST)[2] = (SRC)[2] << (SHIFT) ^ (SRC)[3] >> (32 - (SHIFT)); \
ansond 0:137634ff4186 269 (DEST)[3] = (SRC)[3] << (SHIFT) ^ (SRC)[0] >> (32 - (SHIFT)); \
ansond 0:137634ff4186 270 }
ansond 0:137634ff4186 271
ansond 0:137634ff4186 272 #define FL(XL, XR, KL, KR) \
ansond 0:137634ff4186 273 { \
ansond 0:137634ff4186 274 (XR) = ((((XL) & (KL)) << 1) | (((XL) & (KL)) >> 31)) ^ (XR); \
ansond 0:137634ff4186 275 (XL) = ((XR) | (KR)) ^ (XL); \
ansond 0:137634ff4186 276 }
ansond 0:137634ff4186 277
ansond 0:137634ff4186 278 #define FLInv(YL, YR, KL, KR) \
ansond 0:137634ff4186 279 { \
ansond 0:137634ff4186 280 (YL) = ((YR) | (KR)) ^ (YL); \
ansond 0:137634ff4186 281 (YR) = ((((YL) & (KL)) << 1) | (((YL) & (KL)) >> 31)) ^ (YR); \
ansond 0:137634ff4186 282 }
ansond 0:137634ff4186 283
ansond 0:137634ff4186 284 #define SHIFT_AND_PLACE(INDEX, OFFSET) \
ansond 0:137634ff4186 285 { \
ansond 0:137634ff4186 286 TK[0] = KC[(OFFSET) * 4 + 0]; \
ansond 0:137634ff4186 287 TK[1] = KC[(OFFSET) * 4 + 1]; \
ansond 0:137634ff4186 288 TK[2] = KC[(OFFSET) * 4 + 2]; \
ansond 0:137634ff4186 289 TK[3] = KC[(OFFSET) * 4 + 3]; \
ansond 0:137634ff4186 290 \
ansond 0:137634ff4186 291 for( i = 1; i <= 4; i++ ) \
ansond 0:137634ff4186 292 if( shifts[(INDEX)][(OFFSET)][i -1] ) \
ansond 0:137634ff4186 293 ROTL(TK + i * 4, TK, ( 15 * i ) % 32); \
ansond 0:137634ff4186 294 \
ansond 0:137634ff4186 295 for( i = 0; i < 20; i++ ) \
ansond 0:137634ff4186 296 if( indexes[(INDEX)][(OFFSET)][i] != -1 ) { \
ansond 0:137634ff4186 297 RK[indexes[(INDEX)][(OFFSET)][i]] = TK[ i ]; \
ansond 0:137634ff4186 298 } \
ansond 0:137634ff4186 299 }
ansond 0:137634ff4186 300
ansond 0:137634ff4186 301 static void camellia_feistel( const uint32_t x[2], const uint32_t k[2],
ansond 0:137634ff4186 302 uint32_t z[2])
ansond 0:137634ff4186 303 {
ansond 0:137634ff4186 304 uint32_t I0, I1;
ansond 0:137634ff4186 305 I0 = x[0] ^ k[0];
ansond 0:137634ff4186 306 I1 = x[1] ^ k[1];
ansond 0:137634ff4186 307
ansond 0:137634ff4186 308 I0 = ((uint32_t) SBOX1((I0 >> 24) & 0xFF) << 24) |
ansond 0:137634ff4186 309 ((uint32_t) SBOX2((I0 >> 16) & 0xFF) << 16) |
ansond 0:137634ff4186 310 ((uint32_t) SBOX3((I0 >> 8) & 0xFF) << 8) |
ansond 0:137634ff4186 311 ((uint32_t) SBOX4((I0 ) & 0xFF) );
ansond 0:137634ff4186 312 I1 = ((uint32_t) SBOX2((I1 >> 24) & 0xFF) << 24) |
ansond 0:137634ff4186 313 ((uint32_t) SBOX3((I1 >> 16) & 0xFF) << 16) |
ansond 0:137634ff4186 314 ((uint32_t) SBOX4((I1 >> 8) & 0xFF) << 8) |
ansond 0:137634ff4186 315 ((uint32_t) SBOX1((I1 ) & 0xFF) );
ansond 0:137634ff4186 316
ansond 0:137634ff4186 317 I0 ^= (I1 << 8) | (I1 >> 24);
ansond 0:137634ff4186 318 I1 ^= (I0 << 16) | (I0 >> 16);
ansond 0:137634ff4186 319 I0 ^= (I1 >> 8) | (I1 << 24);
ansond 0:137634ff4186 320 I1 ^= (I0 >> 8) | (I0 << 24);
ansond 0:137634ff4186 321
ansond 0:137634ff4186 322 z[0] ^= I1;
ansond 0:137634ff4186 323 z[1] ^= I0;
ansond 0:137634ff4186 324 }
ansond 0:137634ff4186 325
ansond 0:137634ff4186 326 void camellia_init( camellia_context *ctx )
ansond 0:137634ff4186 327 {
ansond 0:137634ff4186 328 memset( ctx, 0, sizeof( camellia_context ) );
ansond 0:137634ff4186 329 }
ansond 0:137634ff4186 330
ansond 0:137634ff4186 331 void camellia_free( camellia_context *ctx )
ansond 0:137634ff4186 332 {
ansond 0:137634ff4186 333 if( ctx == NULL )
ansond 0:137634ff4186 334 return;
ansond 0:137634ff4186 335
ansond 0:137634ff4186 336 polarssl_zeroize( ctx, sizeof( camellia_context ) );
ansond 0:137634ff4186 337 }
ansond 0:137634ff4186 338
ansond 0:137634ff4186 339 /*
ansond 0:137634ff4186 340 * Camellia key schedule (encryption)
ansond 0:137634ff4186 341 */
ansond 0:137634ff4186 342 int camellia_setkey_enc( camellia_context *ctx, const unsigned char *key,
ansond 0:137634ff4186 343 unsigned int keysize )
ansond 0:137634ff4186 344 {
ansond 0:137634ff4186 345 int idx;
ansond 0:137634ff4186 346 size_t i;
ansond 0:137634ff4186 347 uint32_t *RK;
ansond 0:137634ff4186 348 unsigned char t[64];
ansond 0:137634ff4186 349 uint32_t SIGMA[6][2];
ansond 0:137634ff4186 350 uint32_t KC[16];
ansond 0:137634ff4186 351 uint32_t TK[20];
ansond 0:137634ff4186 352
ansond 0:137634ff4186 353 RK = ctx->rk;
ansond 0:137634ff4186 354
ansond 0:137634ff4186 355 memset( t, 0, 64 );
ansond 0:137634ff4186 356 memset( RK, 0, sizeof(ctx->rk) );
ansond 0:137634ff4186 357
ansond 0:137634ff4186 358 switch( keysize )
ansond 0:137634ff4186 359 {
ansond 0:137634ff4186 360 case 128: ctx->nr = 3; idx = 0; break;
ansond 0:137634ff4186 361 case 192:
ansond 0:137634ff4186 362 case 256: ctx->nr = 4; idx = 1; break;
ansond 0:137634ff4186 363 default : return( POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH );
ansond 0:137634ff4186 364 }
ansond 0:137634ff4186 365
ansond 0:137634ff4186 366 for( i = 0; i < keysize / 8; ++i )
ansond 0:137634ff4186 367 t[i] = key[i];
ansond 0:137634ff4186 368
ansond 0:137634ff4186 369 if( keysize == 192 ) {
ansond 0:137634ff4186 370 for( i = 0; i < 8; i++ )
ansond 0:137634ff4186 371 t[24 + i] = ~t[16 + i];
ansond 0:137634ff4186 372 }
ansond 0:137634ff4186 373
ansond 0:137634ff4186 374 /*
ansond 0:137634ff4186 375 * Prepare SIGMA values
ansond 0:137634ff4186 376 */
ansond 0:137634ff4186 377 for( i = 0; i < 6; i++ ) {
ansond 0:137634ff4186 378 GET_UINT32_BE( SIGMA[i][0], SIGMA_CHARS[i], 0 );
ansond 0:137634ff4186 379 GET_UINT32_BE( SIGMA[i][1], SIGMA_CHARS[i], 4 );
ansond 0:137634ff4186 380 }
ansond 0:137634ff4186 381
ansond 0:137634ff4186 382 /*
ansond 0:137634ff4186 383 * Key storage in KC
ansond 0:137634ff4186 384 * Order: KL, KR, KA, KB
ansond 0:137634ff4186 385 */
ansond 0:137634ff4186 386 memset( KC, 0, sizeof(KC) );
ansond 0:137634ff4186 387
ansond 0:137634ff4186 388 /* Store KL, KR */
ansond 0:137634ff4186 389 for( i = 0; i < 8; i++ )
ansond 0:137634ff4186 390 GET_UINT32_BE( KC[i], t, i * 4 );
ansond 0:137634ff4186 391
ansond 0:137634ff4186 392 /* Generate KA */
ansond 0:137634ff4186 393 for( i = 0; i < 4; ++i )
ansond 0:137634ff4186 394 KC[8 + i] = KC[i] ^ KC[4 + i];
ansond 0:137634ff4186 395
ansond 0:137634ff4186 396 camellia_feistel( KC + 8, SIGMA[0], KC + 10 );
ansond 0:137634ff4186 397 camellia_feistel( KC + 10, SIGMA[1], KC + 8 );
ansond 0:137634ff4186 398
ansond 0:137634ff4186 399 for( i = 0; i < 4; ++i )
ansond 0:137634ff4186 400 KC[8 + i] ^= KC[i];
ansond 0:137634ff4186 401
ansond 0:137634ff4186 402 camellia_feistel( KC + 8, SIGMA[2], KC + 10 );
ansond 0:137634ff4186 403 camellia_feistel( KC + 10, SIGMA[3], KC + 8 );
ansond 0:137634ff4186 404
ansond 0:137634ff4186 405 if( keysize > 128 ) {
ansond 0:137634ff4186 406 /* Generate KB */
ansond 0:137634ff4186 407 for( i = 0; i < 4; ++i )
ansond 0:137634ff4186 408 KC[12 + i] = KC[4 + i] ^ KC[8 + i];
ansond 0:137634ff4186 409
ansond 0:137634ff4186 410 camellia_feistel( KC + 12, SIGMA[4], KC + 14 );
ansond 0:137634ff4186 411 camellia_feistel( KC + 14, SIGMA[5], KC + 12 );
ansond 0:137634ff4186 412 }
ansond 0:137634ff4186 413
ansond 0:137634ff4186 414 /*
ansond 0:137634ff4186 415 * Generating subkeys
ansond 0:137634ff4186 416 */
ansond 0:137634ff4186 417
ansond 0:137634ff4186 418 /* Manipulating KL */
ansond 0:137634ff4186 419 SHIFT_AND_PLACE( idx, 0 );
ansond 0:137634ff4186 420
ansond 0:137634ff4186 421 /* Manipulating KR */
ansond 0:137634ff4186 422 if( keysize > 128 ) {
ansond 0:137634ff4186 423 SHIFT_AND_PLACE( idx, 1 );
ansond 0:137634ff4186 424 }
ansond 0:137634ff4186 425
ansond 0:137634ff4186 426 /* Manipulating KA */
ansond 0:137634ff4186 427 SHIFT_AND_PLACE( idx, 2 );
ansond 0:137634ff4186 428
ansond 0:137634ff4186 429 /* Manipulating KB */
ansond 0:137634ff4186 430 if( keysize > 128 ) {
ansond 0:137634ff4186 431 SHIFT_AND_PLACE( idx, 3 );
ansond 0:137634ff4186 432 }
ansond 0:137634ff4186 433
ansond 0:137634ff4186 434 /* Do transpositions */
ansond 0:137634ff4186 435 for( i = 0; i < 20; i++ ) {
ansond 0:137634ff4186 436 if( transposes[idx][i] != -1 ) {
ansond 0:137634ff4186 437 RK[32 + 12 * idx + i] = RK[transposes[idx][i]];
ansond 0:137634ff4186 438 }
ansond 0:137634ff4186 439 }
ansond 0:137634ff4186 440
ansond 0:137634ff4186 441 return( 0 );
ansond 0:137634ff4186 442 }
ansond 0:137634ff4186 443
ansond 0:137634ff4186 444 /*
ansond 0:137634ff4186 445 * Camellia key schedule (decryption)
ansond 0:137634ff4186 446 */
ansond 0:137634ff4186 447 int camellia_setkey_dec( camellia_context *ctx, const unsigned char *key,
ansond 0:137634ff4186 448 unsigned int keysize )
ansond 0:137634ff4186 449 {
ansond 0:137634ff4186 450 int idx, ret;
ansond 0:137634ff4186 451 size_t i;
ansond 0:137634ff4186 452 camellia_context cty;
ansond 0:137634ff4186 453 uint32_t *RK;
ansond 0:137634ff4186 454 uint32_t *SK;
ansond 0:137634ff4186 455
ansond 0:137634ff4186 456 camellia_init( &cty );
ansond 0:137634ff4186 457
ansond 0:137634ff4186 458 /* Also checks keysize */
ansond 0:137634ff4186 459 if( ( ret = camellia_setkey_enc( &cty, key, keysize ) ) != 0 )
ansond 0:137634ff4186 460 goto exit;
ansond 0:137634ff4186 461
ansond 0:137634ff4186 462 ctx->nr = cty.nr;
ansond 0:137634ff4186 463 idx = ( ctx->nr == 4 );
ansond 0:137634ff4186 464
ansond 0:137634ff4186 465 RK = ctx->rk;
ansond 0:137634ff4186 466 SK = cty.rk + 24 * 2 + 8 * idx * 2;
ansond 0:137634ff4186 467
ansond 0:137634ff4186 468 *RK++ = *SK++;
ansond 0:137634ff4186 469 *RK++ = *SK++;
ansond 0:137634ff4186 470 *RK++ = *SK++;
ansond 0:137634ff4186 471 *RK++ = *SK++;
ansond 0:137634ff4186 472
ansond 0:137634ff4186 473 for( i = 22 + 8 * idx, SK -= 6; i > 0; i--, SK -= 4 )
ansond 0:137634ff4186 474 {
ansond 0:137634ff4186 475 *RK++ = *SK++;
ansond 0:137634ff4186 476 *RK++ = *SK++;
ansond 0:137634ff4186 477 }
ansond 0:137634ff4186 478
ansond 0:137634ff4186 479 SK -= 2;
ansond 0:137634ff4186 480
ansond 0:137634ff4186 481 *RK++ = *SK++;
ansond 0:137634ff4186 482 *RK++ = *SK++;
ansond 0:137634ff4186 483 *RK++ = *SK++;
ansond 0:137634ff4186 484 *RK++ = *SK++;
ansond 0:137634ff4186 485
ansond 0:137634ff4186 486 exit:
ansond 0:137634ff4186 487 camellia_free( &cty );
ansond 0:137634ff4186 488
ansond 0:137634ff4186 489 return( ret );
ansond 0:137634ff4186 490 }
ansond 0:137634ff4186 491
ansond 0:137634ff4186 492 /*
ansond 0:137634ff4186 493 * Camellia-ECB block encryption/decryption
ansond 0:137634ff4186 494 */
ansond 0:137634ff4186 495 int camellia_crypt_ecb( camellia_context *ctx,
ansond 0:137634ff4186 496 int mode,
ansond 0:137634ff4186 497 const unsigned char input[16],
ansond 0:137634ff4186 498 unsigned char output[16] )
ansond 0:137634ff4186 499 {
ansond 0:137634ff4186 500 int NR;
ansond 0:137634ff4186 501 uint32_t *RK, X[4];
ansond 0:137634ff4186 502
ansond 0:137634ff4186 503 ( (void) mode );
ansond 0:137634ff4186 504
ansond 0:137634ff4186 505 NR = ctx->nr;
ansond 0:137634ff4186 506 RK = ctx->rk;
ansond 0:137634ff4186 507
ansond 0:137634ff4186 508 GET_UINT32_BE( X[0], input, 0 );
ansond 0:137634ff4186 509 GET_UINT32_BE( X[1], input, 4 );
ansond 0:137634ff4186 510 GET_UINT32_BE( X[2], input, 8 );
ansond 0:137634ff4186 511 GET_UINT32_BE( X[3], input, 12 );
ansond 0:137634ff4186 512
ansond 0:137634ff4186 513 X[0] ^= *RK++;
ansond 0:137634ff4186 514 X[1] ^= *RK++;
ansond 0:137634ff4186 515 X[2] ^= *RK++;
ansond 0:137634ff4186 516 X[3] ^= *RK++;
ansond 0:137634ff4186 517
ansond 0:137634ff4186 518 while( NR ) {
ansond 0:137634ff4186 519 --NR;
ansond 0:137634ff4186 520 camellia_feistel( X, RK, X + 2 );
ansond 0:137634ff4186 521 RK += 2;
ansond 0:137634ff4186 522 camellia_feistel( X + 2, RK, X );
ansond 0:137634ff4186 523 RK += 2;
ansond 0:137634ff4186 524 camellia_feistel( X, RK, X + 2 );
ansond 0:137634ff4186 525 RK += 2;
ansond 0:137634ff4186 526 camellia_feistel( X + 2, RK, X );
ansond 0:137634ff4186 527 RK += 2;
ansond 0:137634ff4186 528 camellia_feistel( X, RK, X + 2 );
ansond 0:137634ff4186 529 RK += 2;
ansond 0:137634ff4186 530 camellia_feistel( X + 2, RK, X );
ansond 0:137634ff4186 531 RK += 2;
ansond 0:137634ff4186 532
ansond 0:137634ff4186 533 if( NR ) {
ansond 0:137634ff4186 534 FL(X[0], X[1], RK[0], RK[1]);
ansond 0:137634ff4186 535 RK += 2;
ansond 0:137634ff4186 536 FLInv(X[2], X[3], RK[0], RK[1]);
ansond 0:137634ff4186 537 RK += 2;
ansond 0:137634ff4186 538 }
ansond 0:137634ff4186 539 }
ansond 0:137634ff4186 540
ansond 0:137634ff4186 541 X[2] ^= *RK++;
ansond 0:137634ff4186 542 X[3] ^= *RK++;
ansond 0:137634ff4186 543 X[0] ^= *RK++;
ansond 0:137634ff4186 544 X[1] ^= *RK++;
ansond 0:137634ff4186 545
ansond 0:137634ff4186 546 PUT_UINT32_BE( X[2], output, 0 );
ansond 0:137634ff4186 547 PUT_UINT32_BE( X[3], output, 4 );
ansond 0:137634ff4186 548 PUT_UINT32_BE( X[0], output, 8 );
ansond 0:137634ff4186 549 PUT_UINT32_BE( X[1], output, 12 );
ansond 0:137634ff4186 550
ansond 0:137634ff4186 551 return( 0 );
ansond 0:137634ff4186 552 }
ansond 0:137634ff4186 553
ansond 0:137634ff4186 554 #if defined(POLARSSL_CIPHER_MODE_CBC)
ansond 0:137634ff4186 555 /*
ansond 0:137634ff4186 556 * Camellia-CBC buffer encryption/decryption
ansond 0:137634ff4186 557 */
ansond 0:137634ff4186 558 int camellia_crypt_cbc( camellia_context *ctx,
ansond 0:137634ff4186 559 int mode,
ansond 0:137634ff4186 560 size_t length,
ansond 0:137634ff4186 561 unsigned char iv[16],
ansond 0:137634ff4186 562 const unsigned char *input,
ansond 0:137634ff4186 563 unsigned char *output )
ansond 0:137634ff4186 564 {
ansond 0:137634ff4186 565 int i;
ansond 0:137634ff4186 566 unsigned char temp[16];
ansond 0:137634ff4186 567
ansond 0:137634ff4186 568 if( length % 16 )
ansond 0:137634ff4186 569 return( POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH );
ansond 0:137634ff4186 570
ansond 0:137634ff4186 571 if( mode == CAMELLIA_DECRYPT )
ansond 0:137634ff4186 572 {
ansond 0:137634ff4186 573 while( length > 0 )
ansond 0:137634ff4186 574 {
ansond 0:137634ff4186 575 memcpy( temp, input, 16 );
ansond 0:137634ff4186 576 camellia_crypt_ecb( ctx, mode, input, output );
ansond 0:137634ff4186 577
ansond 0:137634ff4186 578 for( i = 0; i < 16; i++ )
ansond 0:137634ff4186 579 output[i] = (unsigned char)( output[i] ^ iv[i] );
ansond 0:137634ff4186 580
ansond 0:137634ff4186 581 memcpy( iv, temp, 16 );
ansond 0:137634ff4186 582
ansond 0:137634ff4186 583 input += 16;
ansond 0:137634ff4186 584 output += 16;
ansond 0:137634ff4186 585 length -= 16;
ansond 0:137634ff4186 586 }
ansond 0:137634ff4186 587 }
ansond 0:137634ff4186 588 else
ansond 0:137634ff4186 589 {
ansond 0:137634ff4186 590 while( length > 0 )
ansond 0:137634ff4186 591 {
ansond 0:137634ff4186 592 for( i = 0; i < 16; i++ )
ansond 0:137634ff4186 593 output[i] = (unsigned char)( input[i] ^ iv[i] );
ansond 0:137634ff4186 594
ansond 0:137634ff4186 595 camellia_crypt_ecb( ctx, mode, output, output );
ansond 0:137634ff4186 596 memcpy( iv, output, 16 );
ansond 0:137634ff4186 597
ansond 0:137634ff4186 598 input += 16;
ansond 0:137634ff4186 599 output += 16;
ansond 0:137634ff4186 600 length -= 16;
ansond 0:137634ff4186 601 }
ansond 0:137634ff4186 602 }
ansond 0:137634ff4186 603
ansond 0:137634ff4186 604 return( 0 );
ansond 0:137634ff4186 605 }
ansond 0:137634ff4186 606 #endif /* POLARSSL_CIPHER_MODE_CBC */
ansond 0:137634ff4186 607
ansond 0:137634ff4186 608 #if defined(POLARSSL_CIPHER_MODE_CFB)
ansond 0:137634ff4186 609 /*
ansond 0:137634ff4186 610 * Camellia-CFB128 buffer encryption/decryption
ansond 0:137634ff4186 611 */
ansond 0:137634ff4186 612 int camellia_crypt_cfb128( camellia_context *ctx,
ansond 0:137634ff4186 613 int mode,
ansond 0:137634ff4186 614 size_t length,
ansond 0:137634ff4186 615 size_t *iv_off,
ansond 0:137634ff4186 616 unsigned char iv[16],
ansond 0:137634ff4186 617 const unsigned char *input,
ansond 0:137634ff4186 618 unsigned char *output )
ansond 0:137634ff4186 619 {
ansond 0:137634ff4186 620 int c;
ansond 0:137634ff4186 621 size_t n = *iv_off;
ansond 0:137634ff4186 622
ansond 0:137634ff4186 623 if( mode == CAMELLIA_DECRYPT )
ansond 0:137634ff4186 624 {
ansond 0:137634ff4186 625 while( length-- )
ansond 0:137634ff4186 626 {
ansond 0:137634ff4186 627 if( n == 0 )
ansond 0:137634ff4186 628 camellia_crypt_ecb( ctx, CAMELLIA_ENCRYPT, iv, iv );
ansond 0:137634ff4186 629
ansond 0:137634ff4186 630 c = *input++;
ansond 0:137634ff4186 631 *output++ = (unsigned char)( c ^ iv[n] );
ansond 0:137634ff4186 632 iv[n] = (unsigned char) c;
ansond 0:137634ff4186 633
ansond 0:137634ff4186 634 n = ( n + 1 ) & 0x0F;
ansond 0:137634ff4186 635 }
ansond 0:137634ff4186 636 }
ansond 0:137634ff4186 637 else
ansond 0:137634ff4186 638 {
ansond 0:137634ff4186 639 while( length-- )
ansond 0:137634ff4186 640 {
ansond 0:137634ff4186 641 if( n == 0 )
ansond 0:137634ff4186 642 camellia_crypt_ecb( ctx, CAMELLIA_ENCRYPT, iv, iv );
ansond 0:137634ff4186 643
ansond 0:137634ff4186 644 iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
ansond 0:137634ff4186 645
ansond 0:137634ff4186 646 n = ( n + 1 ) & 0x0F;
ansond 0:137634ff4186 647 }
ansond 0:137634ff4186 648 }
ansond 0:137634ff4186 649
ansond 0:137634ff4186 650 *iv_off = n;
ansond 0:137634ff4186 651
ansond 0:137634ff4186 652 return( 0 );
ansond 0:137634ff4186 653 }
ansond 0:137634ff4186 654 #endif /* POLARSSL_CIPHER_MODE_CFB */
ansond 0:137634ff4186 655
ansond 0:137634ff4186 656 #if defined(POLARSSL_CIPHER_MODE_CTR)
ansond 0:137634ff4186 657 /*
ansond 0:137634ff4186 658 * Camellia-CTR buffer encryption/decryption
ansond 0:137634ff4186 659 */
ansond 0:137634ff4186 660 int camellia_crypt_ctr( camellia_context *ctx,
ansond 0:137634ff4186 661 size_t length,
ansond 0:137634ff4186 662 size_t *nc_off,
ansond 0:137634ff4186 663 unsigned char nonce_counter[16],
ansond 0:137634ff4186 664 unsigned char stream_block[16],
ansond 0:137634ff4186 665 const unsigned char *input,
ansond 0:137634ff4186 666 unsigned char *output )
ansond 0:137634ff4186 667 {
ansond 0:137634ff4186 668 int c, i;
ansond 0:137634ff4186 669 size_t n = *nc_off;
ansond 0:137634ff4186 670
ansond 0:137634ff4186 671 while( length-- )
ansond 0:137634ff4186 672 {
ansond 0:137634ff4186 673 if( n == 0 ) {
ansond 0:137634ff4186 674 camellia_crypt_ecb( ctx, CAMELLIA_ENCRYPT, nonce_counter,
ansond 0:137634ff4186 675 stream_block );
ansond 0:137634ff4186 676
ansond 0:137634ff4186 677 for( i = 16; i > 0; i-- )
ansond 0:137634ff4186 678 if( ++nonce_counter[i - 1] != 0 )
ansond 0:137634ff4186 679 break;
ansond 0:137634ff4186 680 }
ansond 0:137634ff4186 681 c = *input++;
ansond 0:137634ff4186 682 *output++ = (unsigned char)( c ^ stream_block[n] );
ansond 0:137634ff4186 683
ansond 0:137634ff4186 684 n = ( n + 1 ) & 0x0F;
ansond 0:137634ff4186 685 }
ansond 0:137634ff4186 686
ansond 0:137634ff4186 687 *nc_off = n;
ansond 0:137634ff4186 688
ansond 0:137634ff4186 689 return( 0 );
ansond 0:137634ff4186 690 }
ansond 0:137634ff4186 691 #endif /* POLARSSL_CIPHER_MODE_CTR */
ansond 0:137634ff4186 692 #endif /* !POLARSSL_CAMELLIA_ALT */
ansond 0:137634ff4186 693
ansond 0:137634ff4186 694 #if defined(POLARSSL_SELF_TEST)
ansond 0:137634ff4186 695
ansond 0:137634ff4186 696 /*
ansond 0:137634ff4186 697 * Camellia test vectors from:
ansond 0:137634ff4186 698 *
ansond 0:137634ff4186 699 * http://info.isl.ntt.co.jp/crypt/eng/camellia/technology.html:
ansond 0:137634ff4186 700 * http://info.isl.ntt.co.jp/crypt/eng/camellia/dl/cryptrec/intermediate.txt
ansond 0:137634ff4186 701 * http://info.isl.ntt.co.jp/crypt/eng/camellia/dl/cryptrec/t_camellia.txt
ansond 0:137634ff4186 702 * (For each bitlength: Key 0, Nr 39)
ansond 0:137634ff4186 703 */
ansond 0:137634ff4186 704 #define CAMELLIA_TESTS_ECB 2
ansond 0:137634ff4186 705
ansond 0:137634ff4186 706 static const unsigned char camellia_test_ecb_key[3][CAMELLIA_TESTS_ECB][32] =
ansond 0:137634ff4186 707 {
ansond 0:137634ff4186 708 {
ansond 0:137634ff4186 709 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
ansond 0:137634ff4186 710 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 },
ansond 0:137634ff4186 711 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
ansond 0:137634ff4186 712 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
ansond 0:137634ff4186 713 },
ansond 0:137634ff4186 714 {
ansond 0:137634ff4186 715 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
ansond 0:137634ff4186 716 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
ansond 0:137634ff4186 717 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 },
ansond 0:137634ff4186 718 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
ansond 0:137634ff4186 719 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
ansond 0:137634ff4186 720 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
ansond 0:137634ff4186 721 },
ansond 0:137634ff4186 722 {
ansond 0:137634ff4186 723 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
ansond 0:137634ff4186 724 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
ansond 0:137634ff4186 725 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
ansond 0:137634ff4186 726 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
ansond 0:137634ff4186 727 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
ansond 0:137634ff4186 728 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
ansond 0:137634ff4186 729 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
ansond 0:137634ff4186 730 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
ansond 0:137634ff4186 731 },
ansond 0:137634ff4186 732 };
ansond 0:137634ff4186 733
ansond 0:137634ff4186 734 static const unsigned char camellia_test_ecb_plain[CAMELLIA_TESTS_ECB][16] =
ansond 0:137634ff4186 735 {
ansond 0:137634ff4186 736 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
ansond 0:137634ff4186 737 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 },
ansond 0:137634ff4186 738 { 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
ansond 0:137634ff4186 739 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
ansond 0:137634ff4186 740 };
ansond 0:137634ff4186 741
ansond 0:137634ff4186 742 static const unsigned char camellia_test_ecb_cipher[3][CAMELLIA_TESTS_ECB][16] =
ansond 0:137634ff4186 743 {
ansond 0:137634ff4186 744 {
ansond 0:137634ff4186 745 { 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
ansond 0:137634ff4186 746 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43 },
ansond 0:137634ff4186 747 { 0x38, 0x3C, 0x6C, 0x2A, 0xAB, 0xEF, 0x7F, 0xDE,
ansond 0:137634ff4186 748 0x25, 0xCD, 0x47, 0x0B, 0xF7, 0x74, 0xA3, 0x31 }
ansond 0:137634ff4186 749 },
ansond 0:137634ff4186 750 {
ansond 0:137634ff4186 751 { 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
ansond 0:137634ff4186 752 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9 },
ansond 0:137634ff4186 753 { 0xD1, 0x76, 0x3F, 0xC0, 0x19, 0xD7, 0x7C, 0xC9,
ansond 0:137634ff4186 754 0x30, 0xBF, 0xF2, 0xA5, 0x6F, 0x7C, 0x93, 0x64 }
ansond 0:137634ff4186 755 },
ansond 0:137634ff4186 756 {
ansond 0:137634ff4186 757 { 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
ansond 0:137634ff4186 758 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09 },
ansond 0:137634ff4186 759 { 0x05, 0x03, 0xFB, 0x10, 0xAB, 0x24, 0x1E, 0x7C,
ansond 0:137634ff4186 760 0xF4, 0x5D, 0x8C, 0xDE, 0xEE, 0x47, 0x43, 0x35 }
ansond 0:137634ff4186 761 }
ansond 0:137634ff4186 762 };
ansond 0:137634ff4186 763
ansond 0:137634ff4186 764 #if defined(POLARSSL_CIPHER_MODE_CBC)
ansond 0:137634ff4186 765 #define CAMELLIA_TESTS_CBC 3
ansond 0:137634ff4186 766
ansond 0:137634ff4186 767 static const unsigned char camellia_test_cbc_key[3][32] =
ansond 0:137634ff4186 768 {
ansond 0:137634ff4186 769 { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
ansond 0:137634ff4186 770 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }
ansond 0:137634ff4186 771 ,
ansond 0:137634ff4186 772 { 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
ansond 0:137634ff4186 773 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
ansond 0:137634ff4186 774 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B }
ansond 0:137634ff4186 775 ,
ansond 0:137634ff4186 776 { 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
ansond 0:137634ff4186 777 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
ansond 0:137634ff4186 778 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
ansond 0:137634ff4186 779 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4 }
ansond 0:137634ff4186 780 };
ansond 0:137634ff4186 781
ansond 0:137634ff4186 782 static const unsigned char camellia_test_cbc_iv[16] =
ansond 0:137634ff4186 783
ansond 0:137634ff4186 784 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
ansond 0:137634ff4186 785 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }
ansond 0:137634ff4186 786 ;
ansond 0:137634ff4186 787
ansond 0:137634ff4186 788 static const unsigned char camellia_test_cbc_plain[CAMELLIA_TESTS_CBC][16] =
ansond 0:137634ff4186 789 {
ansond 0:137634ff4186 790 { 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
ansond 0:137634ff4186 791 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A },
ansond 0:137634ff4186 792 { 0xAE, 0x2D, 0x8A, 0x57, 0x1E, 0x03, 0xAC, 0x9C,
ansond 0:137634ff4186 793 0x9E, 0xB7, 0x6F, 0xAC, 0x45, 0xAF, 0x8E, 0x51 },
ansond 0:137634ff4186 794 { 0x30, 0xC8, 0x1C, 0x46, 0xA3, 0x5C, 0xE4, 0x11,
ansond 0:137634ff4186 795 0xE5, 0xFB, 0xC1, 0x19, 0x1A, 0x0A, 0x52, 0xEF }
ansond 0:137634ff4186 796
ansond 0:137634ff4186 797 };
ansond 0:137634ff4186 798
ansond 0:137634ff4186 799 static const unsigned char camellia_test_cbc_cipher[3][CAMELLIA_TESTS_CBC][16] =
ansond 0:137634ff4186 800 {
ansond 0:137634ff4186 801 {
ansond 0:137634ff4186 802 { 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
ansond 0:137634ff4186 803 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB },
ansond 0:137634ff4186 804 { 0xA2, 0xF2, 0xCF, 0x67, 0x16, 0x29, 0xEF, 0x78,
ansond 0:137634ff4186 805 0x40, 0xC5, 0xA5, 0xDF, 0xB5, 0x07, 0x48, 0x87 },
ansond 0:137634ff4186 806 { 0x0F, 0x06, 0x16, 0x50, 0x08, 0xCF, 0x8B, 0x8B,
ansond 0:137634ff4186 807 0x5A, 0x63, 0x58, 0x63, 0x62, 0x54, 0x3E, 0x54 }
ansond 0:137634ff4186 808 },
ansond 0:137634ff4186 809 {
ansond 0:137634ff4186 810 { 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
ansond 0:137634ff4186 811 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93 },
ansond 0:137634ff4186 812 { 0x5D, 0x5A, 0x86, 0x9B, 0xD1, 0x4C, 0xE5, 0x42,
ansond 0:137634ff4186 813 0x64, 0xF8, 0x92, 0xA6, 0xDD, 0x2E, 0xC3, 0xD5 },
ansond 0:137634ff4186 814 { 0x37, 0xD3, 0x59, 0xC3, 0x34, 0x98, 0x36, 0xD8,
ansond 0:137634ff4186 815 0x84, 0xE3, 0x10, 0xAD, 0xDF, 0x68, 0xC4, 0x49 }
ansond 0:137634ff4186 816 },
ansond 0:137634ff4186 817 {
ansond 0:137634ff4186 818 { 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
ansond 0:137634ff4186 819 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA },
ansond 0:137634ff4186 820 { 0x36, 0xCB, 0xEB, 0x73, 0xBD, 0x50, 0x4B, 0x40,
ansond 0:137634ff4186 821 0x70, 0xB1, 0xB7, 0xDE, 0x2B, 0x21, 0xEB, 0x50 },
ansond 0:137634ff4186 822 { 0xE3, 0x1A, 0x60, 0x55, 0x29, 0x7D, 0x96, 0xCA,
ansond 0:137634ff4186 823 0x33, 0x30, 0xCD, 0xF1, 0xB1, 0x86, 0x0A, 0x83 }
ansond 0:137634ff4186 824 }
ansond 0:137634ff4186 825 };
ansond 0:137634ff4186 826 #endif /* POLARSSL_CIPHER_MODE_CBC */
ansond 0:137634ff4186 827
ansond 0:137634ff4186 828 #if defined(POLARSSL_CIPHER_MODE_CTR)
ansond 0:137634ff4186 829 /*
ansond 0:137634ff4186 830 * Camellia-CTR test vectors from:
ansond 0:137634ff4186 831 *
ansond 0:137634ff4186 832 * http://www.faqs.org/rfcs/rfc5528.html
ansond 0:137634ff4186 833 */
ansond 0:137634ff4186 834
ansond 0:137634ff4186 835 static const unsigned char camellia_test_ctr_key[3][16] =
ansond 0:137634ff4186 836 {
ansond 0:137634ff4186 837 { 0xAE, 0x68, 0x52, 0xF8, 0x12, 0x10, 0x67, 0xCC,
ansond 0:137634ff4186 838 0x4B, 0xF7, 0xA5, 0x76, 0x55, 0x77, 0xF3, 0x9E },
ansond 0:137634ff4186 839 { 0x7E, 0x24, 0x06, 0x78, 0x17, 0xFA, 0xE0, 0xD7,
ansond 0:137634ff4186 840 0x43, 0xD6, 0xCE, 0x1F, 0x32, 0x53, 0x91, 0x63 },
ansond 0:137634ff4186 841 { 0x76, 0x91, 0xBE, 0x03, 0x5E, 0x50, 0x20, 0xA8,
ansond 0:137634ff4186 842 0xAC, 0x6E, 0x61, 0x85, 0x29, 0xF9, 0xA0, 0xDC }
ansond 0:137634ff4186 843 };
ansond 0:137634ff4186 844
ansond 0:137634ff4186 845 static const unsigned char camellia_test_ctr_nonce_counter[3][16] =
ansond 0:137634ff4186 846 {
ansond 0:137634ff4186 847 { 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00,
ansond 0:137634ff4186 848 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
ansond 0:137634ff4186 849 { 0x00, 0x6C, 0xB6, 0xDB, 0xC0, 0x54, 0x3B, 0x59,
ansond 0:137634ff4186 850 0xDA, 0x48, 0xD9, 0x0B, 0x00, 0x00, 0x00, 0x01 },
ansond 0:137634ff4186 851 { 0x00, 0xE0, 0x01, 0x7B, 0x27, 0x77, 0x7F, 0x3F,
ansond 0:137634ff4186 852 0x4A, 0x17, 0x86, 0xF0, 0x00, 0x00, 0x00, 0x01 }
ansond 0:137634ff4186 853 };
ansond 0:137634ff4186 854
ansond 0:137634ff4186 855 static const unsigned char camellia_test_ctr_pt[3][48] =
ansond 0:137634ff4186 856 {
ansond 0:137634ff4186 857 { 0x53, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x62,
ansond 0:137634ff4186 858 0x6C, 0x6F, 0x63, 0x6B, 0x20, 0x6D, 0x73, 0x67 },
ansond 0:137634ff4186 859
ansond 0:137634ff4186 860 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
ansond 0:137634ff4186 861 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
ansond 0:137634ff4186 862 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
ansond 0:137634ff4186 863 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F },
ansond 0:137634ff4186 864
ansond 0:137634ff4186 865 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
ansond 0:137634ff4186 866 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
ansond 0:137634ff4186 867 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
ansond 0:137634ff4186 868 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
ansond 0:137634ff4186 869 0x20, 0x21, 0x22, 0x23 }
ansond 0:137634ff4186 870 };
ansond 0:137634ff4186 871
ansond 0:137634ff4186 872 static const unsigned char camellia_test_ctr_ct[3][48] =
ansond 0:137634ff4186 873 {
ansond 0:137634ff4186 874 { 0xD0, 0x9D, 0xC2, 0x9A, 0x82, 0x14, 0x61, 0x9A,
ansond 0:137634ff4186 875 0x20, 0x87, 0x7C, 0x76, 0xDB, 0x1F, 0x0B, 0x3F },
ansond 0:137634ff4186 876 { 0xDB, 0xF3, 0xC7, 0x8D, 0xC0, 0x83, 0x96, 0xD4,
ansond 0:137634ff4186 877 0xDA, 0x7C, 0x90, 0x77, 0x65, 0xBB, 0xCB, 0x44,
ansond 0:137634ff4186 878 0x2B, 0x8E, 0x8E, 0x0F, 0x31, 0xF0, 0xDC, 0xA7,
ansond 0:137634ff4186 879 0x2C, 0x74, 0x17, 0xE3, 0x53, 0x60, 0xE0, 0x48 },
ansond 0:137634ff4186 880 { 0xB1, 0x9D, 0x1F, 0xCD, 0xCB, 0x75, 0xEB, 0x88,
ansond 0:137634ff4186 881 0x2F, 0x84, 0x9C, 0xE2, 0x4D, 0x85, 0xCF, 0x73,
ansond 0:137634ff4186 882 0x9C, 0xE6, 0x4B, 0x2B, 0x5C, 0x9D, 0x73, 0xF1,
ansond 0:137634ff4186 883 0x4F, 0x2D, 0x5D, 0x9D, 0xCE, 0x98, 0x89, 0xCD,
ansond 0:137634ff4186 884 0xDF, 0x50, 0x86, 0x96 }
ansond 0:137634ff4186 885 };
ansond 0:137634ff4186 886
ansond 0:137634ff4186 887 static const int camellia_test_ctr_len[3] =
ansond 0:137634ff4186 888 { 16, 32, 36 };
ansond 0:137634ff4186 889 #endif /* POLARSSL_CIPHER_MODE_CTR */
ansond 0:137634ff4186 890
ansond 0:137634ff4186 891 /*
ansond 0:137634ff4186 892 * Checkup routine
ansond 0:137634ff4186 893 */
ansond 0:137634ff4186 894 int camellia_self_test( int verbose )
ansond 0:137634ff4186 895 {
ansond 0:137634ff4186 896 int i, j, u, v;
ansond 0:137634ff4186 897 unsigned char key[32];
ansond 0:137634ff4186 898 unsigned char buf[64];
ansond 0:137634ff4186 899 unsigned char src[16];
ansond 0:137634ff4186 900 unsigned char dst[16];
ansond 0:137634ff4186 901 #if defined(POLARSSL_CIPHER_MODE_CBC)
ansond 0:137634ff4186 902 unsigned char iv[16];
ansond 0:137634ff4186 903 #endif
ansond 0:137634ff4186 904 #if defined(POLARSSL_CIPHER_MODE_CTR)
ansond 0:137634ff4186 905 size_t offset, len;
ansond 0:137634ff4186 906 unsigned char nonce_counter[16];
ansond 0:137634ff4186 907 unsigned char stream_block[16];
ansond 0:137634ff4186 908 #endif
ansond 0:137634ff4186 909
ansond 0:137634ff4186 910 camellia_context ctx;
ansond 0:137634ff4186 911
ansond 0:137634ff4186 912 memset( key, 0, 32 );
ansond 0:137634ff4186 913
ansond 0:137634ff4186 914 for( j = 0; j < 6; j++ ) {
ansond 0:137634ff4186 915 u = j >> 1;
ansond 0:137634ff4186 916 v = j & 1;
ansond 0:137634ff4186 917
ansond 0:137634ff4186 918 if( verbose != 0 )
ansond 0:137634ff4186 919 polarssl_printf( " CAMELLIA-ECB-%3d (%s): ", 128 + u * 64,
ansond 0:137634ff4186 920 (v == CAMELLIA_DECRYPT) ? "dec" : "enc");
ansond 0:137634ff4186 921
ansond 0:137634ff4186 922 for( i = 0; i < CAMELLIA_TESTS_ECB; i++ ) {
ansond 0:137634ff4186 923 memcpy( key, camellia_test_ecb_key[u][i], 16 + 8 * u );
ansond 0:137634ff4186 924
ansond 0:137634ff4186 925 if( v == CAMELLIA_DECRYPT ) {
ansond 0:137634ff4186 926 camellia_setkey_dec( &ctx, key, 128 + u * 64 );
ansond 0:137634ff4186 927 memcpy( src, camellia_test_ecb_cipher[u][i], 16 );
ansond 0:137634ff4186 928 memcpy( dst, camellia_test_ecb_plain[i], 16 );
ansond 0:137634ff4186 929 } else { /* CAMELLIA_ENCRYPT */
ansond 0:137634ff4186 930 camellia_setkey_enc( &ctx, key, 128 + u * 64 );
ansond 0:137634ff4186 931 memcpy( src, camellia_test_ecb_plain[i], 16 );
ansond 0:137634ff4186 932 memcpy( dst, camellia_test_ecb_cipher[u][i], 16 );
ansond 0:137634ff4186 933 }
ansond 0:137634ff4186 934
ansond 0:137634ff4186 935 camellia_crypt_ecb( &ctx, v, src, buf );
ansond 0:137634ff4186 936
ansond 0:137634ff4186 937 if( memcmp( buf, dst, 16 ) != 0 )
ansond 0:137634ff4186 938 {
ansond 0:137634ff4186 939 if( verbose != 0 )
ansond 0:137634ff4186 940 polarssl_printf( "failed\n" );
ansond 0:137634ff4186 941
ansond 0:137634ff4186 942 return( 1 );
ansond 0:137634ff4186 943 }
ansond 0:137634ff4186 944 }
ansond 0:137634ff4186 945
ansond 0:137634ff4186 946 if( verbose != 0 )
ansond 0:137634ff4186 947 polarssl_printf( "passed\n" );
ansond 0:137634ff4186 948 }
ansond 0:137634ff4186 949
ansond 0:137634ff4186 950 if( verbose != 0 )
ansond 0:137634ff4186 951 polarssl_printf( "\n" );
ansond 0:137634ff4186 952
ansond 0:137634ff4186 953 #if defined(POLARSSL_CIPHER_MODE_CBC)
ansond 0:137634ff4186 954 /*
ansond 0:137634ff4186 955 * CBC mode
ansond 0:137634ff4186 956 */
ansond 0:137634ff4186 957 for( j = 0; j < 6; j++ )
ansond 0:137634ff4186 958 {
ansond 0:137634ff4186 959 u = j >> 1;
ansond 0:137634ff4186 960 v = j & 1;
ansond 0:137634ff4186 961
ansond 0:137634ff4186 962 if( verbose != 0 )
ansond 0:137634ff4186 963 polarssl_printf( " CAMELLIA-CBC-%3d (%s): ", 128 + u * 64,
ansond 0:137634ff4186 964 ( v == CAMELLIA_DECRYPT ) ? "dec" : "enc" );
ansond 0:137634ff4186 965
ansond 0:137634ff4186 966 memcpy( src, camellia_test_cbc_iv, 16 );
ansond 0:137634ff4186 967 memcpy( dst, camellia_test_cbc_iv, 16 );
ansond 0:137634ff4186 968 memcpy( key, camellia_test_cbc_key[u], 16 + 8 * u );
ansond 0:137634ff4186 969
ansond 0:137634ff4186 970 if( v == CAMELLIA_DECRYPT ) {
ansond 0:137634ff4186 971 camellia_setkey_dec( &ctx, key, 128 + u * 64 );
ansond 0:137634ff4186 972 } else {
ansond 0:137634ff4186 973 camellia_setkey_enc( &ctx, key, 128 + u * 64 );
ansond 0:137634ff4186 974 }
ansond 0:137634ff4186 975
ansond 0:137634ff4186 976 for( i = 0; i < CAMELLIA_TESTS_CBC; i++ ) {
ansond 0:137634ff4186 977
ansond 0:137634ff4186 978 if( v == CAMELLIA_DECRYPT ) {
ansond 0:137634ff4186 979 memcpy( iv , src, 16 );
ansond 0:137634ff4186 980 memcpy( src, camellia_test_cbc_cipher[u][i], 16 );
ansond 0:137634ff4186 981 memcpy( dst, camellia_test_cbc_plain[i], 16 );
ansond 0:137634ff4186 982 } else { /* CAMELLIA_ENCRYPT */
ansond 0:137634ff4186 983 memcpy( iv , dst, 16 );
ansond 0:137634ff4186 984 memcpy( src, camellia_test_cbc_plain[i], 16 );
ansond 0:137634ff4186 985 memcpy( dst, camellia_test_cbc_cipher[u][i], 16 );
ansond 0:137634ff4186 986 }
ansond 0:137634ff4186 987
ansond 0:137634ff4186 988 camellia_crypt_cbc( &ctx, v, 16, iv, src, buf );
ansond 0:137634ff4186 989
ansond 0:137634ff4186 990 if( memcmp( buf, dst, 16 ) != 0 )
ansond 0:137634ff4186 991 {
ansond 0:137634ff4186 992 if( verbose != 0 )
ansond 0:137634ff4186 993 polarssl_printf( "failed\n" );
ansond 0:137634ff4186 994
ansond 0:137634ff4186 995 return( 1 );
ansond 0:137634ff4186 996 }
ansond 0:137634ff4186 997 }
ansond 0:137634ff4186 998
ansond 0:137634ff4186 999 if( verbose != 0 )
ansond 0:137634ff4186 1000 polarssl_printf( "passed\n" );
ansond 0:137634ff4186 1001 }
ansond 0:137634ff4186 1002 #endif /* POLARSSL_CIPHER_MODE_CBC */
ansond 0:137634ff4186 1003
ansond 0:137634ff4186 1004 if( verbose != 0 )
ansond 0:137634ff4186 1005 polarssl_printf( "\n" );
ansond 0:137634ff4186 1006
ansond 0:137634ff4186 1007 #if defined(POLARSSL_CIPHER_MODE_CTR)
ansond 0:137634ff4186 1008 /*
ansond 0:137634ff4186 1009 * CTR mode
ansond 0:137634ff4186 1010 */
ansond 0:137634ff4186 1011 for( i = 0; i < 6; i++ )
ansond 0:137634ff4186 1012 {
ansond 0:137634ff4186 1013 u = i >> 1;
ansond 0:137634ff4186 1014 v = i & 1;
ansond 0:137634ff4186 1015
ansond 0:137634ff4186 1016 if( verbose != 0 )
ansond 0:137634ff4186 1017 polarssl_printf( " CAMELLIA-CTR-128 (%s): ",
ansond 0:137634ff4186 1018 ( v == CAMELLIA_DECRYPT ) ? "dec" : "enc" );
ansond 0:137634ff4186 1019
ansond 0:137634ff4186 1020 memcpy( nonce_counter, camellia_test_ctr_nonce_counter[u], 16 );
ansond 0:137634ff4186 1021 memcpy( key, camellia_test_ctr_key[u], 16 );
ansond 0:137634ff4186 1022
ansond 0:137634ff4186 1023 offset = 0;
ansond 0:137634ff4186 1024 camellia_setkey_enc( &ctx, key, 128 );
ansond 0:137634ff4186 1025
ansond 0:137634ff4186 1026 if( v == CAMELLIA_DECRYPT )
ansond 0:137634ff4186 1027 {
ansond 0:137634ff4186 1028 len = camellia_test_ctr_len[u];
ansond 0:137634ff4186 1029 memcpy( buf, camellia_test_ctr_ct[u], len );
ansond 0:137634ff4186 1030
ansond 0:137634ff4186 1031 camellia_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block,
ansond 0:137634ff4186 1032 buf, buf );
ansond 0:137634ff4186 1033
ansond 0:137634ff4186 1034 if( memcmp( buf, camellia_test_ctr_pt[u], len ) != 0 )
ansond 0:137634ff4186 1035 {
ansond 0:137634ff4186 1036 if( verbose != 0 )
ansond 0:137634ff4186 1037 polarssl_printf( "failed\n" );
ansond 0:137634ff4186 1038
ansond 0:137634ff4186 1039 return( 1 );
ansond 0:137634ff4186 1040 }
ansond 0:137634ff4186 1041 }
ansond 0:137634ff4186 1042 else
ansond 0:137634ff4186 1043 {
ansond 0:137634ff4186 1044 len = camellia_test_ctr_len[u];
ansond 0:137634ff4186 1045 memcpy( buf, camellia_test_ctr_pt[u], len );
ansond 0:137634ff4186 1046
ansond 0:137634ff4186 1047 camellia_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block,
ansond 0:137634ff4186 1048 buf, buf );
ansond 0:137634ff4186 1049
ansond 0:137634ff4186 1050 if( memcmp( buf, camellia_test_ctr_ct[u], len ) != 0 )
ansond 0:137634ff4186 1051 {
ansond 0:137634ff4186 1052 if( verbose != 0 )
ansond 0:137634ff4186 1053 polarssl_printf( "failed\n" );
ansond 0:137634ff4186 1054
ansond 0:137634ff4186 1055 return( 1 );
ansond 0:137634ff4186 1056 }
ansond 0:137634ff4186 1057 }
ansond 0:137634ff4186 1058
ansond 0:137634ff4186 1059 if( verbose != 0 )
ansond 0:137634ff4186 1060 polarssl_printf( "passed\n" );
ansond 0:137634ff4186 1061 }
ansond 0:137634ff4186 1062
ansond 0:137634ff4186 1063 if( verbose != 0 )
ansond 0:137634ff4186 1064 polarssl_printf( "\n" );
ansond 0:137634ff4186 1065 #endif /* POLARSSL_CIPHER_MODE_CTR */
ansond 0:137634ff4186 1066
ansond 0:137634ff4186 1067 return( 0 );
ansond 0:137634ff4186 1068 }
ansond 0:137634ff4186 1069
ansond 0:137634ff4186 1070 #endif /* POLARSSL_SELF_TEST */
ansond 0:137634ff4186 1071
ansond 0:137634ff4186 1072 #endif /* POLARSSL_CAMELLIA_C */
ansond 0:137634ff4186 1073