nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nitsshukla
Date:
Fri Nov 04 12:06:04 2016 +0000
Revision:
7:3a65ef12ba31
Parent:
1:55a6170b404f
kghj;

Who changed what in which revision?

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