mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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