BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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