2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* FIPS-46-3 compliant Triple-DES implementation
|
|
|
|
*
|
2015-07-27 11:11:48 +02:00
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* DES, on which TDES is based, was originally designed by Horst Feistel
|
|
|
|
* at IBM in 1974, and was adopted as a standard by NIST (formerly NBS).
|
|
|
|
*
|
|
|
|
* http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
|
|
|
|
*/
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 12:39:06 +02:00
|
|
|
#else
|
2015-04-08 12:49:31 +02:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 12:39:06 +02:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_DES_C)
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/des.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2014-02-01 22:50:26 +01:00
|
|
|
#else
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <stdio.h>
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_printf printf
|
|
|
|
#endif /* MBEDTLS_PLATFORM_C */
|
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2014-02-01 22:50:26 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_DES_ALT)
|
2013-06-24 19:20:35 +02:00
|
|
|
|
2014-06-13 17:20:13 +02:00
|
|
|
/* Implementation that should never be optimized out by the compiler */
|
2015-04-08 12:49:31 +02:00
|
|
|
static void mbedtls_zeroize( void *v, size_t n ) {
|
2014-06-13 17:20:13 +02:00
|
|
|
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* 32-bit integer manipulation macros (big endian)
|
|
|
|
*/
|
2012-10-01 16:41:15 +02:00
|
|
|
#ifndef GET_UINT32_BE
|
|
|
|
#define GET_UINT32_BE(n,b,i) \
|
2009-01-03 22:22:43 +01:00
|
|
|
{ \
|
2012-10-01 16:41:15 +02:00
|
|
|
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
|
|
|
|
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
|
|
|
|
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
|
|
|
|
| ( (uint32_t) (b)[(i) + 3] ); \
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-01 16:41:15 +02:00
|
|
|
#ifndef PUT_UINT32_BE
|
|
|
|
#define PUT_UINT32_BE(n,b,i) \
|
2009-01-03 22:22:43 +01:00
|
|
|
{ \
|
|
|
|
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
|
|
|
|
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
|
|
|
|
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
|
|
|
|
(b)[(i) + 3] = (unsigned char) ( (n) ); \
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Expanded DES S-boxes
|
|
|
|
*/
|
2012-10-01 16:41:15 +02:00
|
|
|
static const uint32_t SB1[64] =
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
0x01010400, 0x00000000, 0x00010000, 0x01010404,
|
|
|
|
0x01010004, 0x00010404, 0x00000004, 0x00010000,
|
|
|
|
0x00000400, 0x01010400, 0x01010404, 0x00000400,
|
|
|
|
0x01000404, 0x01010004, 0x01000000, 0x00000004,
|
|
|
|
0x00000404, 0x01000400, 0x01000400, 0x00010400,
|
|
|
|
0x00010400, 0x01010000, 0x01010000, 0x01000404,
|
|
|
|
0x00010004, 0x01000004, 0x01000004, 0x00010004,
|
|
|
|
0x00000000, 0x00000404, 0x00010404, 0x01000000,
|
|
|
|
0x00010000, 0x01010404, 0x00000004, 0x01010000,
|
|
|
|
0x01010400, 0x01000000, 0x01000000, 0x00000400,
|
|
|
|
0x01010004, 0x00010000, 0x00010400, 0x01000004,
|
|
|
|
0x00000400, 0x00000004, 0x01000404, 0x00010404,
|
|
|
|
0x01010404, 0x00010004, 0x01010000, 0x01000404,
|
|
|
|
0x01000004, 0x00000404, 0x00010404, 0x01010400,
|
|
|
|
0x00000404, 0x01000400, 0x01000400, 0x00000000,
|
|
|
|
0x00010004, 0x00010400, 0x00000000, 0x01010004
|
|
|
|
};
|
|
|
|
|
2012-10-01 16:41:15 +02:00
|
|
|
static const uint32_t SB2[64] =
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
0x80108020, 0x80008000, 0x00008000, 0x00108020,
|
|
|
|
0x00100000, 0x00000020, 0x80100020, 0x80008020,
|
|
|
|
0x80000020, 0x80108020, 0x80108000, 0x80000000,
|
|
|
|
0x80008000, 0x00100000, 0x00000020, 0x80100020,
|
|
|
|
0x00108000, 0x00100020, 0x80008020, 0x00000000,
|
|
|
|
0x80000000, 0x00008000, 0x00108020, 0x80100000,
|
|
|
|
0x00100020, 0x80000020, 0x00000000, 0x00108000,
|
|
|
|
0x00008020, 0x80108000, 0x80100000, 0x00008020,
|
|
|
|
0x00000000, 0x00108020, 0x80100020, 0x00100000,
|
|
|
|
0x80008020, 0x80100000, 0x80108000, 0x00008000,
|
|
|
|
0x80100000, 0x80008000, 0x00000020, 0x80108020,
|
|
|
|
0x00108020, 0x00000020, 0x00008000, 0x80000000,
|
|
|
|
0x00008020, 0x80108000, 0x00100000, 0x80000020,
|
|
|
|
0x00100020, 0x80008020, 0x80000020, 0x00100020,
|
|
|
|
0x00108000, 0x00000000, 0x80008000, 0x00008020,
|
|
|
|
0x80000000, 0x80100020, 0x80108020, 0x00108000
|
|
|
|
};
|
|
|
|
|
2012-10-01 16:41:15 +02:00
|
|
|
static const uint32_t SB3[64] =
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
0x00000208, 0x08020200, 0x00000000, 0x08020008,
|
|
|
|
0x08000200, 0x00000000, 0x00020208, 0x08000200,
|
|
|
|
0x00020008, 0x08000008, 0x08000008, 0x00020000,
|
|
|
|
0x08020208, 0x00020008, 0x08020000, 0x00000208,
|
|
|
|
0x08000000, 0x00000008, 0x08020200, 0x00000200,
|
|
|
|
0x00020200, 0x08020000, 0x08020008, 0x00020208,
|
|
|
|
0x08000208, 0x00020200, 0x00020000, 0x08000208,
|
|
|
|
0x00000008, 0x08020208, 0x00000200, 0x08000000,
|
|
|
|
0x08020200, 0x08000000, 0x00020008, 0x00000208,
|
|
|
|
0x00020000, 0x08020200, 0x08000200, 0x00000000,
|
|
|
|
0x00000200, 0x00020008, 0x08020208, 0x08000200,
|
|
|
|
0x08000008, 0x00000200, 0x00000000, 0x08020008,
|
|
|
|
0x08000208, 0x00020000, 0x08000000, 0x08020208,
|
|
|
|
0x00000008, 0x00020208, 0x00020200, 0x08000008,
|
|
|
|
0x08020000, 0x08000208, 0x00000208, 0x08020000,
|
|
|
|
0x00020208, 0x00000008, 0x08020008, 0x00020200
|
|
|
|
};
|
|
|
|
|
2012-10-01 16:41:15 +02:00
|
|
|
static const uint32_t SB4[64] =
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
0x00802001, 0x00002081, 0x00002081, 0x00000080,
|
|
|
|
0x00802080, 0x00800081, 0x00800001, 0x00002001,
|
|
|
|
0x00000000, 0x00802000, 0x00802000, 0x00802081,
|
|
|
|
0x00000081, 0x00000000, 0x00800080, 0x00800001,
|
|
|
|
0x00000001, 0x00002000, 0x00800000, 0x00802001,
|
|
|
|
0x00000080, 0x00800000, 0x00002001, 0x00002080,
|
|
|
|
0x00800081, 0x00000001, 0x00002080, 0x00800080,
|
|
|
|
0x00002000, 0x00802080, 0x00802081, 0x00000081,
|
|
|
|
0x00800080, 0x00800001, 0x00802000, 0x00802081,
|
|
|
|
0x00000081, 0x00000000, 0x00000000, 0x00802000,
|
|
|
|
0x00002080, 0x00800080, 0x00800081, 0x00000001,
|
|
|
|
0x00802001, 0x00002081, 0x00002081, 0x00000080,
|
|
|
|
0x00802081, 0x00000081, 0x00000001, 0x00002000,
|
|
|
|
0x00800001, 0x00002001, 0x00802080, 0x00800081,
|
|
|
|
0x00002001, 0x00002080, 0x00800000, 0x00802001,
|
|
|
|
0x00000080, 0x00800000, 0x00002000, 0x00802080
|
|
|
|
};
|
|
|
|
|
2012-10-01 16:41:15 +02:00
|
|
|
static const uint32_t SB5[64] =
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
0x00000100, 0x02080100, 0x02080000, 0x42000100,
|
|
|
|
0x00080000, 0x00000100, 0x40000000, 0x02080000,
|
|
|
|
0x40080100, 0x00080000, 0x02000100, 0x40080100,
|
|
|
|
0x42000100, 0x42080000, 0x00080100, 0x40000000,
|
|
|
|
0x02000000, 0x40080000, 0x40080000, 0x00000000,
|
|
|
|
0x40000100, 0x42080100, 0x42080100, 0x02000100,
|
|
|
|
0x42080000, 0x40000100, 0x00000000, 0x42000000,
|
|
|
|
0x02080100, 0x02000000, 0x42000000, 0x00080100,
|
|
|
|
0x00080000, 0x42000100, 0x00000100, 0x02000000,
|
|
|
|
0x40000000, 0x02080000, 0x42000100, 0x40080100,
|
|
|
|
0x02000100, 0x40000000, 0x42080000, 0x02080100,
|
|
|
|
0x40080100, 0x00000100, 0x02000000, 0x42080000,
|
|
|
|
0x42080100, 0x00080100, 0x42000000, 0x42080100,
|
|
|
|
0x02080000, 0x00000000, 0x40080000, 0x42000000,
|
|
|
|
0x00080100, 0x02000100, 0x40000100, 0x00080000,
|
|
|
|
0x00000000, 0x40080000, 0x02080100, 0x40000100
|
|
|
|
};
|
|
|
|
|
2012-10-01 16:41:15 +02:00
|
|
|
static const uint32_t SB6[64] =
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
0x20000010, 0x20400000, 0x00004000, 0x20404010,
|
|
|
|
0x20400000, 0x00000010, 0x20404010, 0x00400000,
|
|
|
|
0x20004000, 0x00404010, 0x00400000, 0x20000010,
|
|
|
|
0x00400010, 0x20004000, 0x20000000, 0x00004010,
|
|
|
|
0x00000000, 0x00400010, 0x20004010, 0x00004000,
|
|
|
|
0x00404000, 0x20004010, 0x00000010, 0x20400010,
|
|
|
|
0x20400010, 0x00000000, 0x00404010, 0x20404000,
|
|
|
|
0x00004010, 0x00404000, 0x20404000, 0x20000000,
|
|
|
|
0x20004000, 0x00000010, 0x20400010, 0x00404000,
|
|
|
|
0x20404010, 0x00400000, 0x00004010, 0x20000010,
|
|
|
|
0x00400000, 0x20004000, 0x20000000, 0x00004010,
|
|
|
|
0x20000010, 0x20404010, 0x00404000, 0x20400000,
|
|
|
|
0x00404010, 0x20404000, 0x00000000, 0x20400010,
|
|
|
|
0x00000010, 0x00004000, 0x20400000, 0x00404010,
|
|
|
|
0x00004000, 0x00400010, 0x20004010, 0x00000000,
|
|
|
|
0x20404000, 0x20000000, 0x00400010, 0x20004010
|
|
|
|
};
|
|
|
|
|
2012-10-01 16:41:15 +02:00
|
|
|
static const uint32_t SB7[64] =
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
0x00200000, 0x04200002, 0x04000802, 0x00000000,
|
|
|
|
0x00000800, 0x04000802, 0x00200802, 0x04200800,
|
|
|
|
0x04200802, 0x00200000, 0x00000000, 0x04000002,
|
|
|
|
0x00000002, 0x04000000, 0x04200002, 0x00000802,
|
|
|
|
0x04000800, 0x00200802, 0x00200002, 0x04000800,
|
|
|
|
0x04000002, 0x04200000, 0x04200800, 0x00200002,
|
|
|
|
0x04200000, 0x00000800, 0x00000802, 0x04200802,
|
|
|
|
0x00200800, 0x00000002, 0x04000000, 0x00200800,
|
|
|
|
0x04000000, 0x00200800, 0x00200000, 0x04000802,
|
|
|
|
0x04000802, 0x04200002, 0x04200002, 0x00000002,
|
|
|
|
0x00200002, 0x04000000, 0x04000800, 0x00200000,
|
|
|
|
0x04200800, 0x00000802, 0x00200802, 0x04200800,
|
|
|
|
0x00000802, 0x04000002, 0x04200802, 0x04200000,
|
|
|
|
0x00200800, 0x00000000, 0x00000002, 0x04200802,
|
|
|
|
0x00000000, 0x00200802, 0x04200000, 0x00000800,
|
|
|
|
0x04000002, 0x04000800, 0x00000800, 0x00200002
|
|
|
|
};
|
|
|
|
|
2012-10-01 16:41:15 +02:00
|
|
|
static const uint32_t SB8[64] =
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
0x10001040, 0x00001000, 0x00040000, 0x10041040,
|
|
|
|
0x10000000, 0x10001040, 0x00000040, 0x10000000,
|
|
|
|
0x00040040, 0x10040000, 0x10041040, 0x00041000,
|
|
|
|
0x10041000, 0x00041040, 0x00001000, 0x00000040,
|
|
|
|
0x10040000, 0x10000040, 0x10001000, 0x00001040,
|
|
|
|
0x00041000, 0x00040040, 0x10040040, 0x10041000,
|
|
|
|
0x00001040, 0x00000000, 0x00000000, 0x10040040,
|
|
|
|
0x10000040, 0x10001000, 0x00041040, 0x00040000,
|
|
|
|
0x00041040, 0x00040000, 0x10041000, 0x00001000,
|
|
|
|
0x00000040, 0x10040040, 0x00001000, 0x00041040,
|
|
|
|
0x10001000, 0x00000040, 0x10000040, 0x10040000,
|
|
|
|
0x10040040, 0x10000000, 0x00040000, 0x10001040,
|
|
|
|
0x00000000, 0x10041040, 0x00040040, 0x10000040,
|
|
|
|
0x10040000, 0x10001000, 0x10001040, 0x00000000,
|
|
|
|
0x10041040, 0x00041000, 0x00041000, 0x00001040,
|
|
|
|
0x00001040, 0x00040040, 0x10000000, 0x10041000
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PC1: left and right halves bit-swap
|
|
|
|
*/
|
2012-10-01 16:41:15 +02:00
|
|
|
static const uint32_t LHs[16] =
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
0x00000000, 0x00000001, 0x00000100, 0x00000101,
|
|
|
|
0x00010000, 0x00010001, 0x00010100, 0x00010101,
|
|
|
|
0x01000000, 0x01000001, 0x01000100, 0x01000101,
|
|
|
|
0x01010000, 0x01010001, 0x01010100, 0x01010101
|
|
|
|
};
|
|
|
|
|
2012-10-01 16:41:15 +02:00
|
|
|
static const uint32_t RHs[16] =
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
0x00000000, 0x01000000, 0x00010000, 0x01010000,
|
|
|
|
0x00000100, 0x01000100, 0x00010100, 0x01010100,
|
|
|
|
0x00000001, 0x01000001, 0x00010001, 0x01010001,
|
|
|
|
0x00000101, 0x01000101, 0x00010101, 0x01010101,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initial Permutation macro
|
|
|
|
*/
|
|
|
|
#define DES_IP(X,Y) \
|
|
|
|
{ \
|
|
|
|
T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \
|
|
|
|
T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \
|
|
|
|
T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \
|
|
|
|
T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \
|
|
|
|
Y = ((Y << 1) | (Y >> 31)) & 0xFFFFFFFF; \
|
|
|
|
T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T; \
|
|
|
|
X = ((X << 1) | (X >> 31)) & 0xFFFFFFFF; \
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Final Permutation macro
|
|
|
|
*/
|
|
|
|
#define DES_FP(X,Y) \
|
|
|
|
{ \
|
|
|
|
X = ((X << 31) | (X >> 1)) & 0xFFFFFFFF; \
|
|
|
|
T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T; \
|
|
|
|
Y = ((Y << 31) | (Y >> 1)) & 0xFFFFFFFF; \
|
|
|
|
T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \
|
|
|
|
T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \
|
|
|
|
T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \
|
|
|
|
T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DES round macro
|
|
|
|
*/
|
|
|
|
#define DES_ROUND(X,Y) \
|
|
|
|
{ \
|
|
|
|
T = *SK++ ^ X; \
|
|
|
|
Y ^= SB8[ (T ) & 0x3F ] ^ \
|
|
|
|
SB6[ (T >> 8) & 0x3F ] ^ \
|
|
|
|
SB4[ (T >> 16) & 0x3F ] ^ \
|
|
|
|
SB2[ (T >> 24) & 0x3F ]; \
|
|
|
|
\
|
|
|
|
T = *SK++ ^ ((X << 28) | (X >> 4)); \
|
|
|
|
Y ^= SB7[ (T ) & 0x3F ] ^ \
|
|
|
|
SB5[ (T >> 8) & 0x3F ] ^ \
|
|
|
|
SB3[ (T >> 16) & 0x3F ] ^ \
|
|
|
|
SB1[ (T >> 24) & 0x3F ]; \
|
|
|
|
}
|
|
|
|
|
2012-10-01 16:41:15 +02:00
|
|
|
#define SWAP(a,b) { uint32_t t = a; a = b; b = t; t = 0; }
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_des_init( mbedtls_des_context *ctx )
|
2014-06-18 11:12:03 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
memset( ctx, 0, sizeof( mbedtls_des_context ) );
|
2014-06-18 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_des_free( mbedtls_des_context *ctx )
|
2014-06-18 11:12:03 +02:00
|
|
|
{
|
|
|
|
if( ctx == NULL )
|
|
|
|
return;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_zeroize( ctx, sizeof( mbedtls_des_context ) );
|
2014-06-18 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_des3_init( mbedtls_des3_context *ctx )
|
2014-06-18 11:12:03 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
memset( ctx, 0, sizeof( mbedtls_des3_context ) );
|
2014-06-18 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_des3_free( mbedtls_des3_context *ctx )
|
2014-06-18 11:12:03 +02:00
|
|
|
{
|
|
|
|
if( ctx == NULL )
|
|
|
|
return;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_zeroize( ctx, sizeof( mbedtls_des3_context ) );
|
2014-06-18 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
2011-01-15 18:32:24 +01:00
|
|
|
static const unsigned char odd_parity_table[128] = { 1, 2, 4, 7, 8,
|
|
|
|
11, 13, 14, 16, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44,
|
|
|
|
47, 49, 50, 52, 55, 56, 59, 61, 62, 64, 67, 69, 70, 73, 74, 76, 79, 81,
|
|
|
|
82, 84, 87, 88, 91, 93, 94, 97, 98, 100, 103, 104, 107, 109, 110, 112,
|
|
|
|
115, 117, 118, 121, 122, 124, 127, 128, 131, 133, 134, 137, 138, 140,
|
|
|
|
143, 145, 146, 148, 151, 152, 155, 157, 158, 161, 162, 164, 167, 168,
|
|
|
|
171, 173, 174, 176, 179, 181, 182, 185, 186, 188, 191, 193, 194, 196,
|
|
|
|
199, 200, 203, 205, 206, 208, 211, 213, 214, 217, 218, 220, 223, 224,
|
|
|
|
227, 229, 230, 233, 234, 236, 239, 241, 242, 244, 247, 248, 251, 253,
|
|
|
|
254 };
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] )
|
2011-01-15 18:32:24 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
for( i = 0; i < MBEDTLS_DES_KEY_SIZE; i++ )
|
2011-01-15 18:32:24 +01:00
|
|
|
key[i] = odd_parity_table[key[i] / 2];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the given key's parity, returns 1 on failure, 0 on SUCCESS
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
|
2011-01-15 18:32:24 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
for( i = 0; i < MBEDTLS_DES_KEY_SIZE; i++ )
|
2014-06-17 16:39:18 +02:00
|
|
|
if( key[i] != odd_parity_table[key[i] / 2] )
|
2011-01-15 18:32:24 +01:00
|
|
|
return( 1 );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Table of weak and semi-weak keys
|
|
|
|
*
|
|
|
|
* Source: http://en.wikipedia.org/wiki/Weak_key
|
|
|
|
*
|
|
|
|
* Weak:
|
|
|
|
* Alternating ones + zeros (0x0101010101010101)
|
|
|
|
* Alternating 'F' + 'E' (0xFEFEFEFEFEFEFEFE)
|
|
|
|
* '0xE0E0E0E0F1F1F1F1'
|
|
|
|
* '0x1F1F1F1F0E0E0E0E'
|
|
|
|
*
|
|
|
|
* Semi-weak:
|
|
|
|
* 0x011F011F010E010E and 0x1F011F010E010E01
|
|
|
|
* 0x01E001E001F101F1 and 0xE001E001F101F101
|
|
|
|
* 0x01FE01FE01FE01FE and 0xFE01FE01FE01FE01
|
|
|
|
* 0x1FE01FE00EF10EF1 and 0xE01FE01FF10EF10E
|
|
|
|
* 0x1FFE1FFE0EFE0EFE and 0xFE1FFE1FFE0EFE0E
|
|
|
|
* 0xE0FEE0FEF1FEF1FE and 0xFEE0FEE0FEF1FEF1
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define WEAK_KEY_COUNT 16
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
static const unsigned char weak_key_table[WEAK_KEY_COUNT][MBEDTLS_DES_KEY_SIZE] =
|
2011-01-15 18:32:24 +01:00
|
|
|
{
|
|
|
|
{ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
|
|
|
|
{ 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
|
|
|
|
{ 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E },
|
|
|
|
{ 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
|
|
|
|
|
|
|
|
{ 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
|
|
|
|
{ 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
|
|
|
|
{ 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
|
|
|
|
{ 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
|
|
|
|
{ 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
|
|
|
|
{ 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
|
|
|
|
{ 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
|
|
|
|
{ 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
|
|
|
|
{ 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
|
|
|
|
{ 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
|
|
|
|
{ 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
|
|
|
|
{ 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
|
|
|
|
};
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
|
2011-01-15 18:32:24 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for( i = 0; i < WEAK_KEY_COUNT; i++ )
|
2015-04-08 12:49:31 +02:00
|
|
|
if( memcmp( weak_key_table[i], key, MBEDTLS_DES_KEY_SIZE) == 0 )
|
2011-07-06 16:37:33 +02:00
|
|
|
return( 1 );
|
2011-01-15 18:32:24 +01:00
|
|
|
|
2011-07-06 16:37:33 +02:00
|
|
|
return( 0 );
|
2011-01-15 18:32:24 +01:00
|
|
|
}
|
|
|
|
|
2015-05-12 15:02:45 +02:00
|
|
|
#if !defined(MBEDTLS_DES_SETKEY_ALT)
|
|
|
|
void mbedtls_des_setkey( uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
int i;
|
2012-10-01 16:41:15 +02:00
|
|
|
uint32_t X, Y, T;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2012-10-01 16:41:15 +02:00
|
|
|
GET_UINT32_BE( X, key, 0 );
|
|
|
|
GET_UINT32_BE( Y, key, 4 );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Permuted Choice 1
|
|
|
|
*/
|
|
|
|
T = ((Y >> 4) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << 4);
|
|
|
|
T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T );
|
|
|
|
|
|
|
|
X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2)
|
|
|
|
| (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] )
|
|
|
|
| (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6)
|
|
|
|
| (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4);
|
|
|
|
|
|
|
|
Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2)
|
|
|
|
| (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] )
|
|
|
|
| (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6)
|
|
|
|
| (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4);
|
|
|
|
|
|
|
|
X &= 0x0FFFFFFF;
|
|
|
|
Y &= 0x0FFFFFFF;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* calculate subkeys
|
|
|
|
*/
|
|
|
|
for( i = 0; i < 16; i++ )
|
|
|
|
{
|
|
|
|
if( i < 2 || i == 8 || i == 15 )
|
|
|
|
{
|
|
|
|
X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF;
|
|
|
|
Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF;
|
|
|
|
Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
*SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000)
|
|
|
|
| ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000)
|
|
|
|
| ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000)
|
|
|
|
| ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000)
|
|
|
|
| ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000)
|
|
|
|
| ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000)
|
|
|
|
| ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400)
|
|
|
|
| ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100)
|
|
|
|
| ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010)
|
|
|
|
| ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004)
|
|
|
|
| ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001);
|
|
|
|
|
|
|
|
*SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000)
|
|
|
|
| ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000)
|
|
|
|
| ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000)
|
|
|
|
| ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000)
|
|
|
|
| ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000)
|
|
|
|
| ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000)
|
|
|
|
| ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000)
|
|
|
|
| ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400)
|
|
|
|
| ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100)
|
|
|
|
| ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011)
|
|
|
|
| ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002);
|
|
|
|
}
|
|
|
|
}
|
2015-05-12 15:02:45 +02:00
|
|
|
#endif /* !MBEDTLS_DES_SETKEY_ALT */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* DES key schedule (56-bit, encryption)
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2015-05-12 15:02:45 +02:00
|
|
|
mbedtls_des_setkey( ctx->sk, key );
|
2011-01-06 16:37:30 +01:00
|
|
|
|
|
|
|
return( 0 );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DES key schedule (56-bit, decryption)
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2015-05-12 15:02:45 +02:00
|
|
|
mbedtls_des_setkey( ctx->sk, key );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
for( i = 0; i < 16; i += 2 )
|
|
|
|
{
|
|
|
|
SWAP( ctx->sk[i ], ctx->sk[30 - i] );
|
|
|
|
SWAP( ctx->sk[i + 1], ctx->sk[31 - i] );
|
|
|
|
}
|
2011-01-06 16:37:30 +01:00
|
|
|
|
|
|
|
return( 0 );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2012-10-01 16:41:15 +02:00
|
|
|
static void des3_set2key( uint32_t esk[96],
|
|
|
|
uint32_t dsk[96],
|
2015-04-08 12:49:31 +02:00
|
|
|
const unsigned char key[MBEDTLS_DES_KEY_SIZE*2] )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2015-05-12 15:02:45 +02:00
|
|
|
mbedtls_des_setkey( esk, key );
|
|
|
|
mbedtls_des_setkey( dsk + 32, key + 8 );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
for( i = 0; i < 32; i += 2 )
|
|
|
|
{
|
|
|
|
dsk[i ] = esk[30 - i];
|
|
|
|
dsk[i + 1] = esk[31 - i];
|
|
|
|
|
|
|
|
esk[i + 32] = dsk[62 - i];
|
|
|
|
esk[i + 33] = dsk[63 - i];
|
|
|
|
|
|
|
|
esk[i + 64] = esk[i ];
|
|
|
|
esk[i + 65] = esk[i + 1];
|
|
|
|
|
|
|
|
dsk[i + 64] = dsk[i ];
|
|
|
|
dsk[i + 65] = dsk[i + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Triple-DES key schedule (112-bit, encryption)
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
|
|
|
|
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2012-10-01 16:41:15 +02:00
|
|
|
uint32_t sk[96];
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
des3_set2key( ctx->sk, sk, key );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_zeroize( sk, sizeof( sk ) );
|
2011-01-06 16:37:30 +01:00
|
|
|
|
|
|
|
return( 0 );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Triple-DES key schedule (112-bit, decryption)
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
|
|
|
|
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2012-10-01 16:41:15 +02:00
|
|
|
uint32_t sk[96];
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
des3_set2key( sk, ctx->sk, key );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_zeroize( sk, sizeof( sk ) );
|
2011-01-06 16:37:30 +01:00
|
|
|
|
|
|
|
return( 0 );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2012-10-01 16:41:15 +02:00
|
|
|
static void des3_set3key( uint32_t esk[96],
|
|
|
|
uint32_t dsk[96],
|
2010-03-16 22:09:09 +01:00
|
|
|
const unsigned char key[24] )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2015-05-12 15:02:45 +02:00
|
|
|
mbedtls_des_setkey( esk, key );
|
|
|
|
mbedtls_des_setkey( dsk + 32, key + 8 );
|
|
|
|
mbedtls_des_setkey( esk + 64, key + 16 );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
for( i = 0; i < 32; i += 2 )
|
|
|
|
{
|
|
|
|
dsk[i ] = esk[94 - i];
|
|
|
|
dsk[i + 1] = esk[95 - i];
|
|
|
|
|
|
|
|
esk[i + 32] = dsk[62 - i];
|
|
|
|
esk[i + 33] = dsk[63 - i];
|
|
|
|
|
|
|
|
dsk[i + 64] = esk[30 - i];
|
|
|
|
dsk[i + 65] = esk[31 - i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Triple-DES key schedule (168-bit, encryption)
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
|
|
|
|
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2012-10-01 16:41:15 +02:00
|
|
|
uint32_t sk[96];
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
des3_set3key( ctx->sk, sk, key );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_zeroize( sk, sizeof( sk ) );
|
2011-01-06 16:37:30 +01:00
|
|
|
|
|
|
|
return( 0 );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Triple-DES key schedule (168-bit, decryption)
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
|
|
|
|
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2012-10-01 16:41:15 +02:00
|
|
|
uint32_t sk[96];
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
des3_set3key( sk, ctx->sk, key );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_zeroize( sk, sizeof( sk ) );
|
2011-01-06 16:37:30 +01:00
|
|
|
|
|
|
|
return( 0 );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DES-ECB block encryption/decryption
|
|
|
|
*/
|
2015-05-12 15:02:45 +02:00
|
|
|
#if !defined(MBEDTLS_DES_CRYPT_ECB_ALT)
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
|
2010-03-16 22:09:09 +01:00
|
|
|
const unsigned char input[8],
|
2009-01-03 22:22:43 +01:00
|
|
|
unsigned char output[8] )
|
|
|
|
{
|
|
|
|
int i;
|
2012-10-01 16:41:15 +02:00
|
|
|
uint32_t X, Y, T, *SK;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
SK = ctx->sk;
|
|
|
|
|
2012-10-01 16:41:15 +02:00
|
|
|
GET_UINT32_BE( X, input, 0 );
|
|
|
|
GET_UINT32_BE( Y, input, 4 );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
DES_IP( X, Y );
|
|
|
|
|
|
|
|
for( i = 0; i < 8; i++ )
|
|
|
|
{
|
|
|
|
DES_ROUND( Y, X );
|
|
|
|
DES_ROUND( X, Y );
|
|
|
|
}
|
|
|
|
|
|
|
|
DES_FP( Y, X );
|
|
|
|
|
2012-10-01 16:41:15 +02:00
|
|
|
PUT_UINT32_BE( Y, output, 0 );
|
|
|
|
PUT_UINT32_BE( X, output, 4 );
|
2010-03-18 22:21:02 +01:00
|
|
|
|
|
|
|
return( 0 );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2015-05-12 15:02:45 +02:00
|
|
|
#endif /* !MBEDTLS_DES_CRYPT_ECB_ALT */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* DES-CBC buffer encryption/decryption
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
|
2009-01-03 22:22:43 +01:00
|
|
|
int mode,
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t length,
|
2009-01-03 22:22:43 +01:00
|
|
|
unsigned char iv[8],
|
2010-03-16 22:09:09 +01:00
|
|
|
const unsigned char *input,
|
2009-01-03 22:22:43 +01:00
|
|
|
unsigned char *output )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned char temp[8];
|
|
|
|
|
2010-03-18 22:21:02 +01:00
|
|
|
if( length % 8 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH );
|
2010-03-18 22:21:02 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mode == MBEDTLS_DES_ENCRYPT )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
while( length > 0 )
|
|
|
|
{
|
|
|
|
for( i = 0; i < 8; i++ )
|
|
|
|
output[i] = (unsigned char)( input[i] ^ iv[i] );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des_crypt_ecb( ctx, output, output );
|
2009-01-03 22:22:43 +01:00
|
|
|
memcpy( iv, output, 8 );
|
|
|
|
|
|
|
|
input += 8;
|
|
|
|
output += 8;
|
|
|
|
length -= 8;
|
|
|
|
}
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
else /* MBEDTLS_DES_DECRYPT */
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
while( length > 0 )
|
|
|
|
{
|
|
|
|
memcpy( temp, input, 8 );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des_crypt_ecb( ctx, input, output );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
for( i = 0; i < 8; i++ )
|
|
|
|
output[i] = (unsigned char)( output[i] ^ iv[i] );
|
|
|
|
|
|
|
|
memcpy( iv, temp, 8 );
|
|
|
|
|
|
|
|
input += 8;
|
|
|
|
output += 8;
|
|
|
|
length -= 8;
|
|
|
|
}
|
|
|
|
}
|
2010-03-18 22:21:02 +01:00
|
|
|
|
|
|
|
return( 0 );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 3DES-ECB block encryption/decryption
|
|
|
|
*/
|
2015-05-12 15:02:45 +02:00
|
|
|
#if !defined(MBEDTLS_DES3_CRYPT_ECB_ALT)
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
|
2010-03-16 22:09:09 +01:00
|
|
|
const unsigned char input[8],
|
2009-01-03 22:22:43 +01:00
|
|
|
unsigned char output[8] )
|
|
|
|
{
|
|
|
|
int i;
|
2012-10-01 16:41:15 +02:00
|
|
|
uint32_t X, Y, T, *SK;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
SK = ctx->sk;
|
|
|
|
|
2012-10-01 16:41:15 +02:00
|
|
|
GET_UINT32_BE( X, input, 0 );
|
|
|
|
GET_UINT32_BE( Y, input, 4 );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
DES_IP( X, Y );
|
|
|
|
|
|
|
|
for( i = 0; i < 8; i++ )
|
|
|
|
{
|
|
|
|
DES_ROUND( Y, X );
|
|
|
|
DES_ROUND( X, Y );
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < 8; i++ )
|
|
|
|
{
|
|
|
|
DES_ROUND( X, Y );
|
|
|
|
DES_ROUND( Y, X );
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < 8; i++ )
|
|
|
|
{
|
|
|
|
DES_ROUND( Y, X );
|
|
|
|
DES_ROUND( X, Y );
|
|
|
|
}
|
|
|
|
|
|
|
|
DES_FP( Y, X );
|
|
|
|
|
2012-10-01 16:41:15 +02:00
|
|
|
PUT_UINT32_BE( Y, output, 0 );
|
|
|
|
PUT_UINT32_BE( X, output, 4 );
|
2010-03-18 22:21:02 +01:00
|
|
|
|
|
|
|
return( 0 );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2015-05-12 15:02:45 +02:00
|
|
|
#endif /* !MBEDTLS_DES3_CRYPT_ECB_ALT */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* 3DES-CBC buffer encryption/decryption
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
|
2009-01-03 22:22:43 +01:00
|
|
|
int mode,
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t length,
|
2009-01-03 22:22:43 +01:00
|
|
|
unsigned char iv[8],
|
2010-03-16 22:09:09 +01:00
|
|
|
const unsigned char *input,
|
2009-01-03 22:22:43 +01:00
|
|
|
unsigned char *output )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned char temp[8];
|
|
|
|
|
2010-03-18 22:21:02 +01:00
|
|
|
if( length % 8 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH );
|
2010-03-18 22:21:02 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mode == MBEDTLS_DES_ENCRYPT )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
while( length > 0 )
|
|
|
|
{
|
|
|
|
for( i = 0; i < 8; i++ )
|
|
|
|
output[i] = (unsigned char)( input[i] ^ iv[i] );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des3_crypt_ecb( ctx, output, output );
|
2009-01-03 22:22:43 +01:00
|
|
|
memcpy( iv, output, 8 );
|
|
|
|
|
|
|
|
input += 8;
|
|
|
|
output += 8;
|
|
|
|
length -= 8;
|
|
|
|
}
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
else /* MBEDTLS_DES_DECRYPT */
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
while( length > 0 )
|
|
|
|
{
|
|
|
|
memcpy( temp, input, 8 );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des3_crypt_ecb( ctx, input, output );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
for( i = 0; i < 8; i++ )
|
|
|
|
output[i] = (unsigned char)( output[i] ^ iv[i] );
|
|
|
|
|
|
|
|
memcpy( iv, temp, 8 );
|
|
|
|
|
|
|
|
input += 8;
|
|
|
|
output += 8;
|
|
|
|
length -= 8;
|
|
|
|
}
|
|
|
|
}
|
2010-03-18 22:21:02 +01:00
|
|
|
|
|
|
|
return( 0 );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* !MBEDTLS_DES_ALT */
|
2013-06-24 19:20:35 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* DES and 3DES test vectors from:
|
|
|
|
*
|
|
|
|
* http://csrc.nist.gov/groups/STM/cavp/documents/des/tripledes-vectors.zip
|
|
|
|
*/
|
|
|
|
static const unsigned char des3_test_keys[24] =
|
|
|
|
{
|
|
|
|
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
|
|
|
|
0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01,
|
|
|
|
0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char des3_test_buf[8] =
|
|
|
|
{
|
|
|
|
0x4E, 0x6F, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char des3_test_ecb_dec[3][8] =
|
|
|
|
{
|
|
|
|
{ 0xCD, 0xD6, 0x4F, 0x2F, 0x94, 0x27, 0xC1, 0x5D },
|
|
|
|
{ 0x69, 0x96, 0xC8, 0xFA, 0x47, 0xA2, 0xAB, 0xEB },
|
|
|
|
{ 0x83, 0x25, 0x39, 0x76, 0x44, 0x09, 0x1A, 0x0A }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char des3_test_ecb_enc[3][8] =
|
|
|
|
{
|
|
|
|
{ 0x6A, 0x2A, 0x19, 0xF4, 0x1E, 0xCA, 0x85, 0x4B },
|
|
|
|
{ 0x03, 0xE6, 0x9F, 0x5B, 0xFA, 0x58, 0xEB, 0x42 },
|
|
|
|
{ 0xDD, 0x17, 0xE8, 0xB8, 0xB4, 0x37, 0xD2, 0x32 }
|
|
|
|
};
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
2014-03-10 11:32:07 +01:00
|
|
|
static const unsigned char des3_test_iv[8] =
|
|
|
|
{
|
|
|
|
0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF,
|
|
|
|
};
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
static const unsigned char des3_test_cbc_dec[3][8] =
|
|
|
|
{
|
|
|
|
{ 0x12, 0x9F, 0x40, 0xB9, 0xD2, 0x00, 0x56, 0xB3 },
|
|
|
|
{ 0x47, 0x0E, 0xFC, 0x9A, 0x6B, 0x8E, 0xE3, 0x93 },
|
|
|
|
{ 0xC5, 0xCE, 0xCF, 0x63, 0xEC, 0xEC, 0x51, 0x4C }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char des3_test_cbc_enc[3][8] =
|
|
|
|
{
|
|
|
|
{ 0x54, 0xF1, 0x5A, 0xF6, 0xEB, 0xE3, 0xA4, 0xB4 },
|
|
|
|
{ 0x35, 0x76, 0x11, 0x56, 0x5F, 0xA1, 0x8E, 0x4D },
|
|
|
|
{ 0xCB, 0x19, 0x1F, 0x85, 0xD1, 0xED, 0x84, 0x39 }
|
|
|
|
};
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Checkup routine
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_des_self_test( int verbose )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2014-06-18 11:12:03 +02:00
|
|
|
int i, j, u, v, ret = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des_context ctx;
|
|
|
|
mbedtls_des3_context ctx3;
|
2009-01-03 22:22:43 +01:00
|
|
|
unsigned char buf[8];
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
2009-01-03 22:22:43 +01:00
|
|
|
unsigned char prv[8];
|
|
|
|
unsigned char iv[8];
|
2013-09-13 16:24:20 +02:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des_init( &ctx );
|
|
|
|
mbedtls_des3_init( &ctx3 );
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* ECB mode
|
|
|
|
*/
|
|
|
|
for( i = 0; i < 6; i++ )
|
|
|
|
{
|
|
|
|
u = i >> 1;
|
|
|
|
v = i & 1;
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( " DES%c-ECB-%3d (%s): ",
|
2014-02-01 22:50:26 +01:00
|
|
|
( u == 0 ) ? ' ' : '3', 56 + u * 56,
|
2015-04-08 12:49:31 +02:00
|
|
|
( v == MBEDTLS_DES_DECRYPT ) ? "dec" : "enc" );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
memcpy( buf, des3_test_buf, 8 );
|
|
|
|
|
|
|
|
switch( i )
|
|
|
|
{
|
|
|
|
case 0:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des_setkey_dec( &ctx, des3_test_keys );
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des_setkey_enc( &ctx, des3_test_keys );
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des3_set2key_dec( &ctx3, des3_test_keys );
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des3_set2key_enc( &ctx3, des3_test_keys );
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des3_set3key_dec( &ctx3, des3_test_keys );
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des3_set3key_enc( &ctx3, des3_test_keys );
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
for( j = 0; j < 10000; j++ )
|
|
|
|
{
|
|
|
|
if( u == 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des_crypt_ecb( &ctx, buf, buf );
|
2009-01-03 22:22:43 +01:00
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des3_crypt_ecb( &ctx3, buf, buf );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( v == MBEDTLS_DES_DECRYPT &&
|
2009-01-03 22:22:43 +01:00
|
|
|
memcmp( buf, des3_test_ecb_dec[u], 8 ) != 0 ) ||
|
2015-04-08 12:49:31 +02:00
|
|
|
( v != MBEDTLS_DES_DECRYPT &&
|
2009-01-03 22:22:43 +01:00
|
|
|
memcmp( buf, des3_test_ecb_enc[u], 8 ) != 0 ) )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "failed\n" );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-06-18 11:12:03 +02:00
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "passed\n" );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "\n" );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* CBC mode
|
|
|
|
*/
|
|
|
|
for( i = 0; i < 6; i++ )
|
|
|
|
{
|
|
|
|
u = i >> 1;
|
|
|
|
v = i & 1;
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( " DES%c-CBC-%3d (%s): ",
|
2014-02-01 22:50:26 +01:00
|
|
|
( u == 0 ) ? ' ' : '3', 56 + u * 56,
|
2015-04-08 12:49:31 +02:00
|
|
|
( v == MBEDTLS_DES_DECRYPT ) ? "dec" : "enc" );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
memcpy( iv, des3_test_iv, 8 );
|
|
|
|
memcpy( prv, des3_test_iv, 8 );
|
|
|
|
memcpy( buf, des3_test_buf, 8 );
|
|
|
|
|
|
|
|
switch( i )
|
|
|
|
{
|
|
|
|
case 0:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des_setkey_dec( &ctx, des3_test_keys );
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des_setkey_enc( &ctx, des3_test_keys );
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des3_set2key_dec( &ctx3, des3_test_keys );
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des3_set2key_enc( &ctx3, des3_test_keys );
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des3_set3key_dec( &ctx3, des3_test_keys );
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des3_set3key_enc( &ctx3, des3_test_keys );
|
2009-01-03 22:22:43 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( v == MBEDTLS_DES_DECRYPT )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
for( j = 0; j < 10000; j++ )
|
|
|
|
{
|
|
|
|
if( u == 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des_crypt_cbc( &ctx, v, 8, iv, buf, buf );
|
2009-01-03 22:22:43 +01:00
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( j = 0; j < 10000; j++ )
|
|
|
|
{
|
|
|
|
unsigned char tmp[8];
|
|
|
|
|
|
|
|
if( u == 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des_crypt_cbc( &ctx, v, 8, iv, buf, buf );
|
2009-01-03 22:22:43 +01:00
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
memcpy( tmp, prv, 8 );
|
|
|
|
memcpy( prv, buf, 8 );
|
|
|
|
memcpy( buf, tmp, 8 );
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy( buf, prv, 8 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( v == MBEDTLS_DES_DECRYPT &&
|
2009-01-03 22:22:43 +01:00
|
|
|
memcmp( buf, des3_test_cbc_dec[u], 8 ) != 0 ) ||
|
2015-04-08 12:49:31 +02:00
|
|
|
( v != MBEDTLS_DES_DECRYPT &&
|
2009-01-03 22:22:43 +01:00
|
|
|
memcmp( buf, des3_test_cbc_enc[u], 8 ) != 0 ) )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "failed\n" );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-06-18 11:12:03 +02:00
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "passed\n" );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "\n" );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-06-18 11:12:03 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des_free( &ctx );
|
|
|
|
mbedtls_des3_free( &ctx3 );
|
2014-06-18 11:12:03 +02:00
|
|
|
|
|
|
|
return( ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_DES_C */
|