Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  *  FIPS-46-3 compliant Triple-DES implementation
  3.  *
  4.  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5.  *  SPDX-License-Identifier: GPL-2.0
  6.  *
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 2 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License along
  18.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  19.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20.  *
  21.  *  This file is part of mbed TLS (https://tls.mbed.org)
  22.  */
  23. /*
  24.  *  DES, on which TDES is based, was originally designed by Horst Feistel
  25.  *  at IBM in 1974, and was adopted as a standard by NIST (formerly NBS).
  26.  *
  27.  *  http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
  28.  */
  29.  
  30. #if !defined(MBEDTLS_CONFIG_FILE)
  31. #include "mbedtls/config.h"
  32. #else
  33. #include MBEDTLS_CONFIG_FILE
  34. #endif
  35.  
  36. #if defined(MBEDTLS_DES_C)
  37.  
  38. #include "mbedtls/des.h"
  39. #include "mbedtls/platform_util.h"
  40.  
  41. #include <string.h>
  42.  
  43. #if defined(MBEDTLS_SELF_TEST)
  44. #if defined(MBEDTLS_PLATFORM_C)
  45. #include "mbedtls/platform.h"
  46. #else
  47. #include <stdio.h>
  48. #define mbedtls_printf printf
  49. #endif /* MBEDTLS_PLATFORM_C */
  50. #endif /* MBEDTLS_SELF_TEST */
  51.  
  52. #if !defined(MBEDTLS_DES_ALT)
  53.  
  54. /*
  55.  * 32-bit integer manipulation macros (big endian)
  56.  */
  57. #ifndef GET_UINT32_BE
  58. #define GET_UINT32_BE(n,b,i)                            \
  59. {                                                       \
  60.     (n) = ( (uint32_t) (b)[(i)    ] << 24 )             \
  61.         | ( (uint32_t) (b)[(i) + 1] << 16 )             \
  62.         | ( (uint32_t) (b)[(i) + 2] <<  8 )             \
  63.         | ( (uint32_t) (b)[(i) + 3]       );            \
  64. }
  65. #endif
  66.  
  67. #ifndef PUT_UINT32_BE
  68. #define PUT_UINT32_BE(n,b,i)                            \
  69. {                                                       \
  70.     (b)[(i)    ] = (unsigned char) ( (n) >> 24 );       \
  71.     (b)[(i) + 1] = (unsigned char) ( (n) >> 16 );       \
  72.     (b)[(i) + 2] = (unsigned char) ( (n) >>  8 );       \
  73.     (b)[(i) + 3] = (unsigned char) ( (n)       );       \
  74. }
  75. #endif
  76.  
  77. /*
  78.  * Expanded DES S-boxes
  79.  */
  80. static const uint32_t SB1[64] =
  81. {
  82.     0x01010400, 0x00000000, 0x00010000, 0x01010404,
  83.     0x01010004, 0x00010404, 0x00000004, 0x00010000,
  84.     0x00000400, 0x01010400, 0x01010404, 0x00000400,
  85.     0x01000404, 0x01010004, 0x01000000, 0x00000004,
  86.     0x00000404, 0x01000400, 0x01000400, 0x00010400,
  87.     0x00010400, 0x01010000, 0x01010000, 0x01000404,
  88.     0x00010004, 0x01000004, 0x01000004, 0x00010004,
  89.     0x00000000, 0x00000404, 0x00010404, 0x01000000,
  90.     0x00010000, 0x01010404, 0x00000004, 0x01010000,
  91.     0x01010400, 0x01000000, 0x01000000, 0x00000400,
  92.     0x01010004, 0x00010000, 0x00010400, 0x01000004,
  93.     0x00000400, 0x00000004, 0x01000404, 0x00010404,
  94.     0x01010404, 0x00010004, 0x01010000, 0x01000404,
  95.     0x01000004, 0x00000404, 0x00010404, 0x01010400,
  96.     0x00000404, 0x01000400, 0x01000400, 0x00000000,
  97.     0x00010004, 0x00010400, 0x00000000, 0x01010004
  98. };
  99.  
  100. static const uint32_t SB2[64] =
  101. {
  102.     0x80108020, 0x80008000, 0x00008000, 0x00108020,
  103.     0x00100000, 0x00000020, 0x80100020, 0x80008020,
  104.     0x80000020, 0x80108020, 0x80108000, 0x80000000,
  105.     0x80008000, 0x00100000, 0x00000020, 0x80100020,
  106.     0x00108000, 0x00100020, 0x80008020, 0x00000000,
  107.     0x80000000, 0x00008000, 0x00108020, 0x80100000,
  108.     0x00100020, 0x80000020, 0x00000000, 0x00108000,
  109.     0x00008020, 0x80108000, 0x80100000, 0x00008020,
  110.     0x00000000, 0x00108020, 0x80100020, 0x00100000,
  111.     0x80008020, 0x80100000, 0x80108000, 0x00008000,
  112.     0x80100000, 0x80008000, 0x00000020, 0x80108020,
  113.     0x00108020, 0x00000020, 0x00008000, 0x80000000,
  114.     0x00008020, 0x80108000, 0x00100000, 0x80000020,
  115.     0x00100020, 0x80008020, 0x80000020, 0x00100020,
  116.     0x00108000, 0x00000000, 0x80008000, 0x00008020,
  117.     0x80000000, 0x80100020, 0x80108020, 0x00108000
  118. };
  119.  
  120. static const uint32_t SB3[64] =
  121. {
  122.     0x00000208, 0x08020200, 0x00000000, 0x08020008,
  123.     0x08000200, 0x00000000, 0x00020208, 0x08000200,
  124.     0x00020008, 0x08000008, 0x08000008, 0x00020000,
  125.     0x08020208, 0x00020008, 0x08020000, 0x00000208,
  126.     0x08000000, 0x00000008, 0x08020200, 0x00000200,
  127.     0x00020200, 0x08020000, 0x08020008, 0x00020208,
  128.     0x08000208, 0x00020200, 0x00020000, 0x08000208,
  129.     0x00000008, 0x08020208, 0x00000200, 0x08000000,
  130.     0x08020200, 0x08000000, 0x00020008, 0x00000208,
  131.     0x00020000, 0x08020200, 0x08000200, 0x00000000,
  132.     0x00000200, 0x00020008, 0x08020208, 0x08000200,
  133.     0x08000008, 0x00000200, 0x00000000, 0x08020008,
  134.     0x08000208, 0x00020000, 0x08000000, 0x08020208,
  135.     0x00000008, 0x00020208, 0x00020200, 0x08000008,
  136.     0x08020000, 0x08000208, 0x00000208, 0x08020000,
  137.     0x00020208, 0x00000008, 0x08020008, 0x00020200
  138. };
  139.  
  140. static const uint32_t SB4[64] =
  141. {
  142.     0x00802001, 0x00002081, 0x00002081, 0x00000080,
  143.     0x00802080, 0x00800081, 0x00800001, 0x00002001,
  144.     0x00000000, 0x00802000, 0x00802000, 0x00802081,
  145.     0x00000081, 0x00000000, 0x00800080, 0x00800001,
  146.     0x00000001, 0x00002000, 0x00800000, 0x00802001,
  147.     0x00000080, 0x00800000, 0x00002001, 0x00002080,
  148.     0x00800081, 0x00000001, 0x00002080, 0x00800080,
  149.     0x00002000, 0x00802080, 0x00802081, 0x00000081,
  150.     0x00800080, 0x00800001, 0x00802000, 0x00802081,
  151.     0x00000081, 0x00000000, 0x00000000, 0x00802000,
  152.     0x00002080, 0x00800080, 0x00800081, 0x00000001,
  153.     0x00802001, 0x00002081, 0x00002081, 0x00000080,
  154.     0x00802081, 0x00000081, 0x00000001, 0x00002000,
  155.     0x00800001, 0x00002001, 0x00802080, 0x00800081,
  156.     0x00002001, 0x00002080, 0x00800000, 0x00802001,
  157.     0x00000080, 0x00800000, 0x00002000, 0x00802080
  158. };
  159.  
  160. static const uint32_t SB5[64] =
  161. {
  162.     0x00000100, 0x02080100, 0x02080000, 0x42000100,
  163.     0x00080000, 0x00000100, 0x40000000, 0x02080000,
  164.     0x40080100, 0x00080000, 0x02000100, 0x40080100,
  165.     0x42000100, 0x42080000, 0x00080100, 0x40000000,
  166.     0x02000000, 0x40080000, 0x40080000, 0x00000000,
  167.     0x40000100, 0x42080100, 0x42080100, 0x02000100,
  168.     0x42080000, 0x40000100, 0x00000000, 0x42000000,
  169.     0x02080100, 0x02000000, 0x42000000, 0x00080100,
  170.     0x00080000, 0x42000100, 0x00000100, 0x02000000,
  171.     0x40000000, 0x02080000, 0x42000100, 0x40080100,
  172.     0x02000100, 0x40000000, 0x42080000, 0x02080100,
  173.     0x40080100, 0x00000100, 0x02000000, 0x42080000,
  174.     0x42080100, 0x00080100, 0x42000000, 0x42080100,
  175.     0x02080000, 0x00000000, 0x40080000, 0x42000000,
  176.     0x00080100, 0x02000100, 0x40000100, 0x00080000,
  177.     0x00000000, 0x40080000, 0x02080100, 0x40000100
  178. };
  179.  
  180. static const uint32_t SB6[64] =
  181. {
  182.     0x20000010, 0x20400000, 0x00004000, 0x20404010,
  183.     0x20400000, 0x00000010, 0x20404010, 0x00400000,
  184.     0x20004000, 0x00404010, 0x00400000, 0x20000010,
  185.     0x00400010, 0x20004000, 0x20000000, 0x00004010,
  186.     0x00000000, 0x00400010, 0x20004010, 0x00004000,
  187.     0x00404000, 0x20004010, 0x00000010, 0x20400010,
  188.     0x20400010, 0x00000000, 0x00404010, 0x20404000,
  189.     0x00004010, 0x00404000, 0x20404000, 0x20000000,
  190.     0x20004000, 0x00000010, 0x20400010, 0x00404000,
  191.     0x20404010, 0x00400000, 0x00004010, 0x20000010,
  192.     0x00400000, 0x20004000, 0x20000000, 0x00004010,
  193.     0x20000010, 0x20404010, 0x00404000, 0x20400000,
  194.     0x00404010, 0x20404000, 0x00000000, 0x20400010,
  195.     0x00000010, 0x00004000, 0x20400000, 0x00404010,
  196.     0x00004000, 0x00400010, 0x20004010, 0x00000000,
  197.     0x20404000, 0x20000000, 0x00400010, 0x20004010
  198. };
  199.  
  200. static const uint32_t SB7[64] =
  201. {
  202.     0x00200000, 0x04200002, 0x04000802, 0x00000000,
  203.     0x00000800, 0x04000802, 0x00200802, 0x04200800,
  204.     0x04200802, 0x00200000, 0x00000000, 0x04000002,
  205.     0x00000002, 0x04000000, 0x04200002, 0x00000802,
  206.     0x04000800, 0x00200802, 0x00200002, 0x04000800,
  207.     0x04000002, 0x04200000, 0x04200800, 0x00200002,
  208.     0x04200000, 0x00000800, 0x00000802, 0x04200802,
  209.     0x00200800, 0x00000002, 0x04000000, 0x00200800,
  210.     0x04000000, 0x00200800, 0x00200000, 0x04000802,
  211.     0x04000802, 0x04200002, 0x04200002, 0x00000002,
  212.     0x00200002, 0x04000000, 0x04000800, 0x00200000,
  213.     0x04200800, 0x00000802, 0x00200802, 0x04200800,
  214.     0x00000802, 0x04000002, 0x04200802, 0x04200000,
  215.     0x00200800, 0x00000000, 0x00000002, 0x04200802,
  216.     0x00000000, 0x00200802, 0x04200000, 0x00000800,
  217.     0x04000002, 0x04000800, 0x00000800, 0x00200002
  218. };
  219.  
  220. static const uint32_t SB8[64] =
  221. {
  222.     0x10001040, 0x00001000, 0x00040000, 0x10041040,
  223.     0x10000000, 0x10001040, 0x00000040, 0x10000000,
  224.     0x00040040, 0x10040000, 0x10041040, 0x00041000,
  225.     0x10041000, 0x00041040, 0x00001000, 0x00000040,
  226.     0x10040000, 0x10000040, 0x10001000, 0x00001040,
  227.     0x00041000, 0x00040040, 0x10040040, 0x10041000,
  228.     0x00001040, 0x00000000, 0x00000000, 0x10040040,
  229.     0x10000040, 0x10001000, 0x00041040, 0x00040000,
  230.     0x00041040, 0x00040000, 0x10041000, 0x00001000,
  231.     0x00000040, 0x10040040, 0x00001000, 0x00041040,
  232.     0x10001000, 0x00000040, 0x10000040, 0x10040000,
  233.     0x10040040, 0x10000000, 0x00040000, 0x10001040,
  234.     0x00000000, 0x10041040, 0x00040040, 0x10000040,
  235.     0x10040000, 0x10001000, 0x10001040, 0x00000000,
  236.     0x10041040, 0x00041000, 0x00041000, 0x00001040,
  237.     0x00001040, 0x00040040, 0x10000000, 0x10041000
  238. };
  239.  
  240. /*
  241.  * PC1: left and right halves bit-swap
  242.  */
  243. static const uint32_t LHs[16] =
  244. {
  245.     0x00000000, 0x00000001, 0x00000100, 0x00000101,
  246.     0x00010000, 0x00010001, 0x00010100, 0x00010101,
  247.     0x01000000, 0x01000001, 0x01000100, 0x01000101,
  248.     0x01010000, 0x01010001, 0x01010100, 0x01010101
  249. };
  250.  
  251. static const uint32_t RHs[16] =
  252. {
  253.     0x00000000, 0x01000000, 0x00010000, 0x01010000,
  254.     0x00000100, 0x01000100, 0x00010100, 0x01010100,
  255.     0x00000001, 0x01000001, 0x00010001, 0x01010001,
  256.     0x00000101, 0x01000101, 0x00010101, 0x01010101,
  257. };
  258.  
  259. /*
  260.  * Initial Permutation macro
  261.  */
  262. #define DES_IP(X,Y)                                                       \
  263.     do                                                                    \
  264.     {                                                                     \
  265.         T = (((X) >>  4) ^ (Y)) & 0x0F0F0F0F; (Y) ^= T; (X) ^= (T <<  4); \
  266.         T = (((X) >> 16) ^ (Y)) & 0x0000FFFF; (Y) ^= T; (X) ^= (T << 16); \
  267.         T = (((Y) >>  2) ^ (X)) & 0x33333333; (X) ^= T; (Y) ^= (T <<  2); \
  268.         T = (((Y) >>  8) ^ (X)) & 0x00FF00FF; (X) ^= T; (Y) ^= (T <<  8); \
  269.         (Y) = (((Y) << 1) | ((Y) >> 31)) & 0xFFFFFFFF;                    \
  270.         T = ((X) ^ (Y)) & 0xAAAAAAAA; (Y) ^= T; (X) ^= T;                 \
  271.         (X) = (((X) << 1) | ((X) >> 31)) & 0xFFFFFFFF;                    \
  272.     } while( 0 )
  273.  
  274. /*
  275.  * Final Permutation macro
  276.  */
  277. #define DES_FP(X,Y)                                                       \
  278.     do                                                                    \
  279.     {                                                                     \
  280.         (X) = (((X) << 31) | ((X) >> 1)) & 0xFFFFFFFF;                    \
  281.         T = ((X) ^ (Y)) & 0xAAAAAAAA; (X) ^= T; (Y) ^= T;                 \
  282.         (Y) = (((Y) << 31) | ((Y) >> 1)) & 0xFFFFFFFF;                    \
  283.         T = (((Y) >>  8) ^ (X)) & 0x00FF00FF; (X) ^= T; (Y) ^= (T <<  8); \
  284.         T = (((Y) >>  2) ^ (X)) & 0x33333333; (X) ^= T; (Y) ^= (T <<  2); \
  285.         T = (((X) >> 16) ^ (Y)) & 0x0000FFFF; (Y) ^= T; (X) ^= (T << 16); \
  286.         T = (((X) >>  4) ^ (Y)) & 0x0F0F0F0F; (Y) ^= T; (X) ^= (T <<  4); \
  287.     } while( 0 )
  288.  
  289. /*
  290.  * DES round macro
  291.  */
  292. #define DES_ROUND(X,Y)                              \
  293.     do                                              \
  294.     {                                               \
  295.         T = *SK++ ^ (X);                            \
  296.         (Y) ^= SB8[ (T      ) & 0x3F ] ^            \
  297.                SB6[ (T >>  8) & 0x3F ] ^            \
  298.                SB4[ (T >> 16) & 0x3F ] ^            \
  299.                SB2[ (T >> 24) & 0x3F ];             \
  300.                                                     \
  301.         T = *SK++ ^ (((X) << 28) | ((X) >> 4));     \
  302.         (Y) ^= SB7[ (T      ) & 0x3F ] ^            \
  303.                SB5[ (T >>  8) & 0x3F ] ^            \
  304.                SB3[ (T >> 16) & 0x3F ] ^            \
  305.                SB1[ (T >> 24) & 0x3F ];             \
  306.     } while( 0 )
  307.  
  308. #define SWAP(a,b)                                       \
  309.     do                                                  \
  310.     {                                                   \
  311.         uint32_t t = (a); (a) = (b); (b) = t; t = 0;    \
  312.     } while( 0 )
  313.  
  314. void mbedtls_des_init( mbedtls_des_context *ctx )
  315. {
  316.     memset( ctx, 0, sizeof( mbedtls_des_context ) );
  317. }
  318.  
  319. void mbedtls_des_free( mbedtls_des_context *ctx )
  320. {
  321.     if( ctx == NULL )
  322.         return;
  323.  
  324.     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_des_context ) );
  325. }
  326.  
  327. void mbedtls_des3_init( mbedtls_des3_context *ctx )
  328. {
  329.     memset( ctx, 0, sizeof( mbedtls_des3_context ) );
  330. }
  331.  
  332. void mbedtls_des3_free( mbedtls_des3_context *ctx )
  333. {
  334.     if( ctx == NULL )
  335.         return;
  336.  
  337.     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_des3_context ) );
  338. }
  339.  
  340. static const unsigned char odd_parity_table[128] = { 1,  2,  4,  7,  8,
  341.         11, 13, 14, 16, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44,
  342.         47, 49, 50, 52, 55, 56, 59, 61, 62, 64, 67, 69, 70, 73, 74, 76, 79, 81,
  343.         82, 84, 87, 88, 91, 93, 94, 97, 98, 100, 103, 104, 107, 109, 110, 112,
  344.         115, 117, 118, 121, 122, 124, 127, 128, 131, 133, 134, 137, 138, 140,
  345.         143, 145, 146, 148, 151, 152, 155, 157, 158, 161, 162, 164, 167, 168,
  346.         171, 173, 174, 176, 179, 181, 182, 185, 186, 188, 191, 193, 194, 196,
  347.         199, 200, 203, 205, 206, 208, 211, 213, 214, 217, 218, 220, 223, 224,
  348.         227, 229, 230, 233, 234, 236, 239, 241, 242, 244, 247, 248, 251, 253,
  349.         254 };
  350.  
  351. void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  352. {
  353.     int i;
  354.  
  355.     for( i = 0; i < MBEDTLS_DES_KEY_SIZE; i++ )
  356.         key[i] = odd_parity_table[key[i] / 2];
  357. }
  358.  
  359. /*
  360.  * Check the given key's parity, returns 1 on failure, 0 on SUCCESS
  361.  */
  362. int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  363. {
  364.     int i;
  365.  
  366.     for( i = 0; i < MBEDTLS_DES_KEY_SIZE; i++ )
  367.         if( key[i] != odd_parity_table[key[i] / 2] )
  368.             return( 1 );
  369.  
  370.     return( 0 );
  371. }
  372.  
  373. /*
  374.  * Table of weak and semi-weak keys
  375.  *
  376.  * Source: http://en.wikipedia.org/wiki/Weak_key
  377.  *
  378.  * Weak:
  379.  * Alternating ones + zeros (0x0101010101010101)
  380.  * Alternating 'F' + 'E' (0xFEFEFEFEFEFEFEFE)
  381.  * '0xE0E0E0E0F1F1F1F1'
  382.  * '0x1F1F1F1F0E0E0E0E'
  383.  *
  384.  * Semi-weak:
  385.  * 0x011F011F010E010E and 0x1F011F010E010E01
  386.  * 0x01E001E001F101F1 and 0xE001E001F101F101
  387.  * 0x01FE01FE01FE01FE and 0xFE01FE01FE01FE01
  388.  * 0x1FE01FE00EF10EF1 and 0xE01FE01FF10EF10E
  389.  * 0x1FFE1FFE0EFE0EFE and 0xFE1FFE1FFE0EFE0E
  390.  * 0xE0FEE0FEF1FEF1FE and 0xFEE0FEE0FEF1FEF1
  391.  *
  392.  */
  393.  
  394. #define WEAK_KEY_COUNT 16
  395.  
  396. static const unsigned char weak_key_table[WEAK_KEY_COUNT][MBEDTLS_DES_KEY_SIZE] =
  397. {
  398.     { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  399.     { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  400.     { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E },
  401.     { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  402.  
  403.     { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  404.     { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  405.     { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  406.     { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  407.     { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  408.     { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  409.     { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  410.     { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  411.     { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  412.     { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  413.     { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  414.     { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  415. };
  416.  
  417. int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  418. {
  419.     int i;
  420.  
  421.     for( i = 0; i < WEAK_KEY_COUNT; i++ )
  422.         if( memcmp( weak_key_table[i], key, MBEDTLS_DES_KEY_SIZE) == 0 )
  423.             return( 1 );
  424.  
  425.     return( 0 );
  426. }
  427.  
  428. #if !defined(MBEDTLS_DES_SETKEY_ALT)
  429. void mbedtls_des_setkey( uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  430. {
  431.     int i;
  432.     uint32_t X, Y, T;
  433.  
  434.     GET_UINT32_BE( X, key, 0 );
  435.     GET_UINT32_BE( Y, key, 4 );
  436.  
  437.     /*
  438.      * Permuted Choice 1
  439.      */
  440.     T =  ((Y >>  4) ^ X) & 0x0F0F0F0F;  X ^= T; Y ^= (T <<  4);
  441.     T =  ((Y      ) ^ X) & 0x10101010;  X ^= T; Y ^= (T      );
  442.  
  443.     X =   (LHs[ (X      ) & 0xF] << 3) | (LHs[ (X >>  8) & 0xF ] << 2)
  444.         | (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ]     )
  445.         | (LHs[ (X >>  5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6)
  446.         | (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4);
  447.  
  448.     Y =   (RHs[ (Y >>  1) & 0xF] << 3) | (RHs[ (Y >>  9) & 0xF ] << 2)
  449.         | (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ]     )
  450.         | (RHs[ (Y >>  4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6)
  451.         | (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4);
  452.  
  453.     X &= 0x0FFFFFFF;
  454.     Y &= 0x0FFFFFFF;
  455.  
  456.     /*
  457.      * calculate subkeys
  458.      */
  459.     for( i = 0; i < 16; i++ )
  460.     {
  461.         if( i < 2 || i == 8 || i == 15 )
  462.         {
  463.             X = ((X <<  1) | (X >> 27)) & 0x0FFFFFFF;
  464.             Y = ((Y <<  1) | (Y >> 27)) & 0x0FFFFFFF;
  465.         }
  466.         else
  467.         {
  468.             X = ((X <<  2) | (X >> 26)) & 0x0FFFFFFF;
  469.             Y = ((Y <<  2) | (Y >> 26)) & 0x0FFFFFFF;
  470.         }
  471.  
  472.         *SK++ =   ((X <<  4) & 0x24000000) | ((X << 28) & 0x10000000)
  473.                 | ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000)
  474.                 | ((X <<  6) & 0x01000000) | ((X <<  9) & 0x00200000)
  475.                 | ((X >>  1) & 0x00100000) | ((X << 10) & 0x00040000)
  476.                 | ((X <<  2) & 0x00020000) | ((X >> 10) & 0x00010000)
  477.                 | ((Y >> 13) & 0x00002000) | ((Y >>  4) & 0x00001000)
  478.                 | ((Y <<  6) & 0x00000800) | ((Y >>  1) & 0x00000400)
  479.                 | ((Y >> 14) & 0x00000200) | ((Y      ) & 0x00000100)
  480.                 | ((Y >>  5) & 0x00000020) | ((Y >> 10) & 0x00000010)
  481.                 | ((Y >>  3) & 0x00000008) | ((Y >> 18) & 0x00000004)
  482.                 | ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001);
  483.  
  484.         *SK++ =   ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000)
  485.                 | ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000)
  486.                 | ((X >>  2) & 0x02000000) | ((X <<  1) & 0x01000000)
  487.                 | ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000)
  488.                 | ((X <<  3) & 0x00080000) | ((X >>  6) & 0x00040000)
  489.                 | ((X << 15) & 0x00020000) | ((X >>  4) & 0x00010000)
  490.                 | ((Y >>  2) & 0x00002000) | ((Y <<  8) & 0x00001000)
  491.                 | ((Y >> 14) & 0x00000808) | ((Y >>  9) & 0x00000400)
  492.                 | ((Y      ) & 0x00000200) | ((Y <<  7) & 0x00000100)
  493.                 | ((Y >>  7) & 0x00000020) | ((Y >>  3) & 0x00000011)
  494.                 | ((Y <<  2) & 0x00000004) | ((Y >> 21) & 0x00000002);
  495.     }
  496. }
  497. #endif /* !MBEDTLS_DES_SETKEY_ALT */
  498.  
  499. /*
  500.  * DES key schedule (56-bit, encryption)
  501.  */
  502. int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  503. {
  504.     mbedtls_des_setkey( ctx->sk, key );
  505.  
  506.     return( 0 );
  507. }
  508.  
  509. /*
  510.  * DES key schedule (56-bit, decryption)
  511.  */
  512. int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  513. {
  514.     int i;
  515.  
  516.     mbedtls_des_setkey( ctx->sk, key );
  517.  
  518.     for( i = 0; i < 16; i += 2 )
  519.     {
  520.         SWAP( ctx->sk[i    ], ctx->sk[30 - i] );
  521.         SWAP( ctx->sk[i + 1], ctx->sk[31 - i] );
  522.     }
  523.  
  524.     return( 0 );
  525. }
  526.  
  527. static void des3_set2key( uint32_t esk[96],
  528.                           uint32_t dsk[96],
  529.                           const unsigned char key[MBEDTLS_DES_KEY_SIZE*2] )
  530. {
  531.     int i;
  532.  
  533.     mbedtls_des_setkey( esk, key );
  534.     mbedtls_des_setkey( dsk + 32, key + 8 );
  535.  
  536.     for( i = 0; i < 32; i += 2 )
  537.     {
  538.         dsk[i     ] = esk[30 - i];
  539.         dsk[i +  1] = esk[31 - i];
  540.  
  541.         esk[i + 32] = dsk[62 - i];
  542.         esk[i + 33] = dsk[63 - i];
  543.  
  544.         esk[i + 64] = esk[i    ];
  545.         esk[i + 65] = esk[i + 1];
  546.  
  547.         dsk[i + 64] = dsk[i    ];
  548.         dsk[i + 65] = dsk[i + 1];
  549.     }
  550. }
  551.  
  552. /*
  553.  * Triple-DES key schedule (112-bit, encryption)
  554.  */
  555. int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
  556.                       const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] )
  557. {
  558.     uint32_t sk[96];
  559.  
  560.     des3_set2key( ctx->sk, sk, key );
  561.     mbedtls_platform_zeroize( sk,  sizeof( sk ) );
  562.  
  563.     return( 0 );
  564. }
  565.  
  566. /*
  567.  * Triple-DES key schedule (112-bit, decryption)
  568.  */
  569. int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
  570.                       const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] )
  571. {
  572.     uint32_t sk[96];
  573.  
  574.     des3_set2key( sk, ctx->sk, key );
  575.     mbedtls_platform_zeroize( sk,  sizeof( sk ) );
  576.  
  577.     return( 0 );
  578. }
  579.  
  580. static void des3_set3key( uint32_t esk[96],
  581.                           uint32_t dsk[96],
  582.                           const unsigned char key[24] )
  583. {
  584.     int i;
  585.  
  586.     mbedtls_des_setkey( esk, key );
  587.     mbedtls_des_setkey( dsk + 32, key +  8 );
  588.     mbedtls_des_setkey( esk + 64, key + 16 );
  589.  
  590.     for( i = 0; i < 32; i += 2 )
  591.     {
  592.         dsk[i     ] = esk[94 - i];
  593.         dsk[i +  1] = esk[95 - i];
  594.  
  595.         esk[i + 32] = dsk[62 - i];
  596.         esk[i + 33] = dsk[63 - i];
  597.  
  598.         dsk[i + 64] = esk[30 - i];
  599.         dsk[i + 65] = esk[31 - i];
  600.     }
  601. }
  602.  
  603. /*
  604.  * Triple-DES key schedule (168-bit, encryption)
  605.  */
  606. int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
  607.                       const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] )
  608. {
  609.     uint32_t sk[96];
  610.  
  611.     des3_set3key( ctx->sk, sk, key );
  612.     mbedtls_platform_zeroize( sk,  sizeof( sk ) );
  613.  
  614.     return( 0 );
  615. }
  616.  
  617. /*
  618.  * Triple-DES key schedule (168-bit, decryption)
  619.  */
  620. int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
  621.                       const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] )
  622. {
  623.     uint32_t sk[96];
  624.  
  625.     des3_set3key( sk, ctx->sk, key );
  626.     mbedtls_platform_zeroize( sk,  sizeof( sk ) );
  627.  
  628.     return( 0 );
  629. }
  630.  
  631. /*
  632.  * DES-ECB block encryption/decryption
  633.  */
  634. #if !defined(MBEDTLS_DES_CRYPT_ECB_ALT)
  635. int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
  636.                     const unsigned char input[8],
  637.                     unsigned char output[8] )
  638. {
  639.     int i;
  640.     uint32_t X, Y, T, *SK;
  641.  
  642.     SK = ctx->sk;
  643.  
  644.     GET_UINT32_BE( X, input, 0 );
  645.     GET_UINT32_BE( Y, input, 4 );
  646.  
  647.     DES_IP( X, Y );
  648.  
  649.     for( i = 0; i < 8; i++ )
  650.     {
  651.         DES_ROUND( Y, X );
  652.         DES_ROUND( X, Y );
  653.     }
  654.  
  655.     DES_FP( Y, X );
  656.  
  657.     PUT_UINT32_BE( Y, output, 0 );
  658.     PUT_UINT32_BE( X, output, 4 );
  659.  
  660.     return( 0 );
  661. }
  662. #endif /* !MBEDTLS_DES_CRYPT_ECB_ALT */
  663.  
  664. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  665. /*
  666.  * DES-CBC buffer encryption/decryption
  667.  */
  668. int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
  669.                     int mode,
  670.                     size_t length,
  671.                     unsigned char iv[8],
  672.                     const unsigned char *input,
  673.                     unsigned char *output )
  674. {
  675.     int i;
  676.     unsigned char temp[8];
  677.  
  678.     if( length % 8 )
  679.         return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH );
  680.  
  681.     if( mode == MBEDTLS_DES_ENCRYPT )
  682.     {
  683.         while( length > 0 )
  684.         {
  685.             for( i = 0; i < 8; i++ )
  686.                 output[i] = (unsigned char)( input[i] ^ iv[i] );
  687.  
  688.             mbedtls_des_crypt_ecb( ctx, output, output );
  689.             memcpy( iv, output, 8 );
  690.  
  691.             input  += 8;
  692.             output += 8;
  693.             length -= 8;
  694.         }
  695.     }
  696.     else /* MBEDTLS_DES_DECRYPT */
  697.     {
  698.         while( length > 0 )
  699.         {
  700.             memcpy( temp, input, 8 );
  701.             mbedtls_des_crypt_ecb( ctx, input, output );
  702.  
  703.             for( i = 0; i < 8; i++ )
  704.                 output[i] = (unsigned char)( output[i] ^ iv[i] );
  705.  
  706.             memcpy( iv, temp, 8 );
  707.  
  708.             input  += 8;
  709.             output += 8;
  710.             length -= 8;
  711.         }
  712.     }
  713.  
  714.     return( 0 );
  715. }
  716. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  717.  
  718. /*
  719.  * 3DES-ECB block encryption/decryption
  720.  */
  721. #if !defined(MBEDTLS_DES3_CRYPT_ECB_ALT)
  722. int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
  723.                      const unsigned char input[8],
  724.                      unsigned char output[8] )
  725. {
  726.     int i;
  727.     uint32_t X, Y, T, *SK;
  728.  
  729.     SK = ctx->sk;
  730.  
  731.     GET_UINT32_BE( X, input, 0 );
  732.     GET_UINT32_BE( Y, input, 4 );
  733.  
  734.     DES_IP( X, Y );
  735.  
  736.     for( i = 0; i < 8; i++ )
  737.     {
  738.         DES_ROUND( Y, X );
  739.         DES_ROUND( X, Y );
  740.     }
  741.  
  742.     for( i = 0; i < 8; i++ )
  743.     {
  744.         DES_ROUND( X, Y );
  745.         DES_ROUND( Y, X );
  746.     }
  747.  
  748.     for( i = 0; i < 8; i++ )
  749.     {
  750.         DES_ROUND( Y, X );
  751.         DES_ROUND( X, Y );
  752.     }
  753.  
  754.     DES_FP( Y, X );
  755.  
  756.     PUT_UINT32_BE( Y, output, 0 );
  757.     PUT_UINT32_BE( X, output, 4 );
  758.  
  759.     return( 0 );
  760. }
  761. #endif /* !MBEDTLS_DES3_CRYPT_ECB_ALT */
  762.  
  763. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  764. /*
  765.  * 3DES-CBC buffer encryption/decryption
  766.  */
  767. int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
  768.                      int mode,
  769.                      size_t length,
  770.                      unsigned char iv[8],
  771.                      const unsigned char *input,
  772.                      unsigned char *output )
  773. {
  774.     int i;
  775.     unsigned char temp[8];
  776.  
  777.     if( length % 8 )
  778.         return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH );
  779.  
  780.     if( mode == MBEDTLS_DES_ENCRYPT )
  781.     {
  782.         while( length > 0 )
  783.         {
  784.             for( i = 0; i < 8; i++ )
  785.                 output[i] = (unsigned char)( input[i] ^ iv[i] );
  786.  
  787.             mbedtls_des3_crypt_ecb( ctx, output, output );
  788.             memcpy( iv, output, 8 );
  789.  
  790.             input  += 8;
  791.             output += 8;
  792.             length -= 8;
  793.         }
  794.     }
  795.     else /* MBEDTLS_DES_DECRYPT */
  796.     {
  797.         while( length > 0 )
  798.         {
  799.             memcpy( temp, input, 8 );
  800.             mbedtls_des3_crypt_ecb( ctx, input, output );
  801.  
  802.             for( i = 0; i < 8; i++ )
  803.                 output[i] = (unsigned char)( output[i] ^ iv[i] );
  804.  
  805.             memcpy( iv, temp, 8 );
  806.  
  807.             input  += 8;
  808.             output += 8;
  809.             length -= 8;
  810.         }
  811.     }
  812.  
  813.     return( 0 );
  814. }
  815. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  816.  
  817. #endif /* !MBEDTLS_DES_ALT */
  818.  
  819. #if defined(MBEDTLS_SELF_TEST)
  820. /*
  821.  * DES and 3DES test vectors from:
  822.  *
  823.  * http://csrc.nist.gov/groups/STM/cavp/documents/des/tripledes-vectors.zip
  824.  */
  825. static const unsigned char des3_test_keys[24] =
  826. {
  827.     0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  828.     0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01,
  829.     0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23
  830. };
  831.  
  832. static const unsigned char des3_test_buf[8] =
  833. {
  834.     0x4E, 0x6F, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74
  835. };
  836.  
  837. static const unsigned char des3_test_ecb_dec[3][8] =
  838. {
  839.     { 0xCD, 0xD6, 0x4F, 0x2F, 0x94, 0x27, 0xC1, 0x5D },
  840.     { 0x69, 0x96, 0xC8, 0xFA, 0x47, 0xA2, 0xAB, 0xEB },
  841.     { 0x83, 0x25, 0x39, 0x76, 0x44, 0x09, 0x1A, 0x0A }
  842. };
  843.  
  844. static const unsigned char des3_test_ecb_enc[3][8] =
  845. {
  846.     { 0x6A, 0x2A, 0x19, 0xF4, 0x1E, 0xCA, 0x85, 0x4B },
  847.     { 0x03, 0xE6, 0x9F, 0x5B, 0xFA, 0x58, 0xEB, 0x42 },
  848.     { 0xDD, 0x17, 0xE8, 0xB8, 0xB4, 0x37, 0xD2, 0x32 }
  849. };
  850.  
  851. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  852. static const unsigned char des3_test_iv[8] =
  853. {
  854.     0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF,
  855. };
  856.  
  857. static const unsigned char des3_test_cbc_dec[3][8] =
  858. {
  859.     { 0x12, 0x9F, 0x40, 0xB9, 0xD2, 0x00, 0x56, 0xB3 },
  860.     { 0x47, 0x0E, 0xFC, 0x9A, 0x6B, 0x8E, 0xE3, 0x93 },
  861.     { 0xC5, 0xCE, 0xCF, 0x63, 0xEC, 0xEC, 0x51, 0x4C }
  862. };
  863.  
  864. static const unsigned char des3_test_cbc_enc[3][8] =
  865. {
  866.     { 0x54, 0xF1, 0x5A, 0xF6, 0xEB, 0xE3, 0xA4, 0xB4 },
  867.     { 0x35, 0x76, 0x11, 0x56, 0x5F, 0xA1, 0x8E, 0x4D },
  868.     { 0xCB, 0x19, 0x1F, 0x85, 0xD1, 0xED, 0x84, 0x39 }
  869. };
  870. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  871.  
  872. /*
  873.  * Checkup routine
  874.  */
  875. int mbedtls_des_self_test( int verbose )
  876. {
  877.     int i, j, u, v, ret = 0;
  878.     mbedtls_des_context ctx;
  879.     mbedtls_des3_context ctx3;
  880.     unsigned char buf[8];
  881. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  882.     unsigned char prv[8];
  883.     unsigned char iv[8];
  884. #endif
  885.  
  886.     mbedtls_des_init( &ctx );
  887.     mbedtls_des3_init( &ctx3 );
  888.     /*
  889.      * ECB mode
  890.      */
  891.     for( i = 0; i < 6; i++ )
  892.     {
  893.         u = i >> 1;
  894.         v = i  & 1;
  895.  
  896.         if( verbose != 0 )
  897.             mbedtls_printf( "  DES%c-ECB-%3d (%s): ",
  898.                              ( u == 0 ) ? ' ' : '3', 56 + u * 56,
  899.                              ( v == MBEDTLS_DES_DECRYPT ) ? "dec" : "enc" );
  900.  
  901.         memcpy( buf, des3_test_buf, 8 );
  902.  
  903.         switch( i )
  904.         {
  905.         case 0:
  906.             mbedtls_des_setkey_dec( &ctx, des3_test_keys );
  907.             break;
  908.  
  909.         case 1:
  910.             mbedtls_des_setkey_enc( &ctx, des3_test_keys );
  911.             break;
  912.  
  913.         case 2:
  914.             mbedtls_des3_set2key_dec( &ctx3, des3_test_keys );
  915.             break;
  916.  
  917.         case 3:
  918.             mbedtls_des3_set2key_enc( &ctx3, des3_test_keys );
  919.             break;
  920.  
  921.         case 4:
  922.             mbedtls_des3_set3key_dec( &ctx3, des3_test_keys );
  923.             break;
  924.  
  925.         case 5:
  926.             mbedtls_des3_set3key_enc( &ctx3, des3_test_keys );
  927.             break;
  928.  
  929.         default:
  930.             return( 1 );
  931.         }
  932.  
  933.         for( j = 0; j < 10000; j++ )
  934.         {
  935.             if( u == 0 )
  936.                 mbedtls_des_crypt_ecb( &ctx, buf, buf );
  937.             else
  938.                 mbedtls_des3_crypt_ecb( &ctx3, buf, buf );
  939.         }
  940.  
  941.         if( ( v == MBEDTLS_DES_DECRYPT &&
  942.                 memcmp( buf, des3_test_ecb_dec[u], 8 ) != 0 ) ||
  943.             ( v != MBEDTLS_DES_DECRYPT &&
  944.                 memcmp( buf, des3_test_ecb_enc[u], 8 ) != 0 ) )
  945.         {
  946.             if( verbose != 0 )
  947.                 mbedtls_printf( "failed\n" );
  948.  
  949.             ret = 1;
  950.             goto exit;
  951.         }
  952.  
  953.         if( verbose != 0 )
  954.             mbedtls_printf( "passed\n" );
  955.     }
  956.  
  957.     if( verbose != 0 )
  958.         mbedtls_printf( "\n" );
  959.  
  960. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  961.     /*
  962.      * CBC mode
  963.      */
  964.     for( i = 0; i < 6; i++ )
  965.     {
  966.         u = i >> 1;
  967.         v = i  & 1;
  968.  
  969.         if( verbose != 0 )
  970.             mbedtls_printf( "  DES%c-CBC-%3d (%s): ",
  971.                              ( u == 0 ) ? ' ' : '3', 56 + u * 56,
  972.                              ( v == MBEDTLS_DES_DECRYPT ) ? "dec" : "enc" );
  973.  
  974.         memcpy( iv,  des3_test_iv,  8 );
  975.         memcpy( prv, des3_test_iv,  8 );
  976.         memcpy( buf, des3_test_buf, 8 );
  977.  
  978.         switch( i )
  979.         {
  980.         case 0:
  981.             mbedtls_des_setkey_dec( &ctx, des3_test_keys );
  982.             break;
  983.  
  984.         case 1:
  985.             mbedtls_des_setkey_enc( &ctx, des3_test_keys );
  986.             break;
  987.  
  988.         case 2:
  989.             mbedtls_des3_set2key_dec( &ctx3, des3_test_keys );
  990.             break;
  991.  
  992.         case 3:
  993.             mbedtls_des3_set2key_enc( &ctx3, des3_test_keys );
  994.             break;
  995.  
  996.         case 4:
  997.             mbedtls_des3_set3key_dec( &ctx3, des3_test_keys );
  998.             break;
  999.  
  1000.         case 5:
  1001.             mbedtls_des3_set3key_enc( &ctx3, des3_test_keys );
  1002.             break;
  1003.  
  1004.         default:
  1005.             return( 1 );
  1006.         }
  1007.  
  1008.         if( v == MBEDTLS_DES_DECRYPT )
  1009.         {
  1010.             for( j = 0; j < 10000; j++ )
  1011.             {
  1012.                 if( u == 0 )
  1013.                     mbedtls_des_crypt_cbc( &ctx, v, 8, iv, buf, buf );
  1014.                 else
  1015.                     mbedtls_des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf );
  1016.             }
  1017.         }
  1018.         else
  1019.         {
  1020.             for( j = 0; j < 10000; j++ )
  1021.             {
  1022.                 unsigned char tmp[8];
  1023.  
  1024.                 if( u == 0 )
  1025.                     mbedtls_des_crypt_cbc( &ctx, v, 8, iv, buf, buf );
  1026.                 else
  1027.                     mbedtls_des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf );
  1028.  
  1029.                 memcpy( tmp, prv, 8 );
  1030.                 memcpy( prv, buf, 8 );
  1031.                 memcpy( buf, tmp, 8 );
  1032.             }
  1033.  
  1034.             memcpy( buf, prv, 8 );
  1035.         }
  1036.  
  1037.         if( ( v == MBEDTLS_DES_DECRYPT &&
  1038.                 memcmp( buf, des3_test_cbc_dec[u], 8 ) != 0 ) ||
  1039.             ( v != MBEDTLS_DES_DECRYPT &&
  1040.                 memcmp( buf, des3_test_cbc_enc[u], 8 ) != 0 ) )
  1041.         {
  1042.             if( verbose != 0 )
  1043.                 mbedtls_printf( "failed\n" );
  1044.  
  1045.             ret = 1;
  1046.             goto exit;
  1047.         }
  1048.  
  1049.         if( verbose != 0 )
  1050.             mbedtls_printf( "passed\n" );
  1051.     }
  1052. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  1053.  
  1054.     if( verbose != 0 )
  1055.         mbedtls_printf( "\n" );
  1056.  
  1057. exit:
  1058.     mbedtls_des_free( &ctx );
  1059.     mbedtls_des3_free( &ctx3 );
  1060.  
  1061.     return( ret );
  1062. }
  1063.  
  1064. #endif /* MBEDTLS_SELF_TEST */
  1065.  
  1066. #endif /* MBEDTLS_DES_C */
  1067.