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