Embed:
(wiki syntax)
Show/hide line numbers
des3.c
Go to the documentation of this file.
00001 /** 00002 * @file des3.c 00003 * @brief Triple DES (Triple Data Encryption Algorithm) 00004 * 00005 * @section License 00006 * 00007 * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved. 00008 * 00009 * This file is part of CycloneCrypto Open. 00010 * 00011 * This program is free software; you can redistribute it and/or 00012 * modify it under the terms of the GNU General Public License 00013 * as published by the Free Software Foundation; either version 2 00014 * of the License, or (at your option) any later version. 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU General Public License 00022 * along with this program; if not, write to the Free Software Foundation, 00023 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00024 * 00025 * @section Description 00026 * 00027 * Triple DES is an encryption algorithm designed to encipher and decipher blocks 00028 * of 64 bits under control of a 192-bit key. Refer to FIPS 46-3 for more details 00029 * 00030 * @author Oryx Embedded SARL (www.oryx-embedded.com) 00031 * @version 1.7.6 00032 **/ 00033 00034 //Switch to the appropriate trace level 00035 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL 00036 00037 //Dependencies 00038 #include <string.h> 00039 #include "crypto.h" 00040 #include "des3.h" 00041 #include "des.h" 00042 00043 //Check crypto library configuration 00044 #if (DES3_SUPPORT == ENABLED) 00045 00046 //Common interface for encryption algorithms 00047 const CipherAlgo des3CipherAlgo = 00048 { 00049 "3DES", 00050 sizeof(Des3Context), 00051 CIPHER_ALGO_TYPE_BLOCK, 00052 DES3_BLOCK_SIZE, 00053 (CipherAlgoInit) des3Init, 00054 NULL, 00055 NULL, 00056 (CipherAlgoEncryptBlock) des3EncryptBlock, 00057 (CipherAlgoDecryptBlock) des3DecryptBlock 00058 }; 00059 00060 00061 /** 00062 * @brief Initialize a Triple DES context using the supplied key 00063 * @param[in] context Pointer to the Triple DES context to initialize 00064 * @param[in] key Pointer to the key 00065 * @param[in] keyLength Length of the key 00066 * @return Error code 00067 **/ 00068 00069 error_t des3Init(Des3Context *context, const uint8_t *key, size_t keyLength) 00070 { 00071 //Check key length 00072 if(keyLength == 8) 00073 { 00074 //This option provides backward compatibility with DES, because the 00075 //first and second DES operations cancel out 00076 desInit(&context->k1, key, 8); 00077 desInit(&context->k2, key, 8); 00078 desInit(&context->k3, key, 8); 00079 } 00080 else if(keyLength == 16) 00081 { 00082 //If the key length is 128 bits including parity, the first 8 bytes of the 00083 //encoding represent the key used for the two outer DES operations, and 00084 //the second 8 bytes represent the key used for the inner DES operation 00085 desInit(&context->k1, key, 8); 00086 desInit(&context->k2, key + 8, 8); 00087 desInit(&context->k3, key, 8); 00088 } 00089 else if(keyLength == 24) 00090 { 00091 //If the key length is 192 bits including parity, then three independent DES 00092 //keys are represented, in the order in which they are used for encryption 00093 desInit(&context->k1, key, 8); 00094 desInit(&context->k2, key + 8, 8); 00095 desInit(&context->k3, key + 16, 8); 00096 } 00097 else 00098 { 00099 //Invalid key length... 00100 return ERROR_INVALID_KEY_LENGTH; 00101 } 00102 00103 //No error to report 00104 return NO_ERROR; 00105 } 00106 00107 00108 /** 00109 * @brief Encrypt a 8-byte block using Triple DES algorithm 00110 * @param[in] context Pointer to the Triple DES context 00111 * @param[in] input Plaintext block to encrypt 00112 * @param[out] output Ciphertext block resulting from encryption 00113 **/ 00114 00115 void des3EncryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output) 00116 { 00117 //The first pass is a DES encryption 00118 desEncryptBlock(&context->k1, input, output); 00119 //The second pass is a DES decryption of the first ciphertext result 00120 desDecryptBlock(&context->k2, output, output); 00121 //The third pass is a DES encryption of the second pass result 00122 desEncryptBlock(&context->k3, output, output); 00123 } 00124 00125 00126 /** 00127 * @brief Decrypt a 8-byte block using Triple DES algorithm 00128 * @param[in] context Pointer to the Triple DES context 00129 * @param[in] input Ciphertext block to decrypt 00130 * @param[out] output Plaintext block resulting from decryption 00131 **/ 00132 00133 void des3DecryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output) 00134 { 00135 //The first pass is a DES decryption 00136 desDecryptBlock(&context->k3, input, output); 00137 //The second pass is a DES encryption of the first pass result 00138 desEncryptBlock(&context->k2, output, output); 00139 //The third pass is a DES decryption of the second ciphertext result 00140 desDecryptBlock(&context->k1, output, output); 00141 } 00142 00143 #endif 00144
Generated on Tue Jul 12 2022 17:10:12 by
