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