des3.c
Go to the documentation of this file.
1 /**
2  * @file des3.c
3  * @brief Triple DES (Triple Data Encryption Algorithm)
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneCrypto Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @section Description
26  *
27  * Triple DES is an encryption algorithm designed to encipher and decipher blocks
28  * of 64 bits under control of a 192-bit key. Refer to FIPS 46-3 for more details
29  *
30  * @author Oryx Embedded SARL (www.oryx-embedded.com)
31  * @version 1.9.0
32  **/
33 
34 //Switch to the appropriate trace level
35 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
36 
37 //Dependencies
38 #include "core/crypto.h"
39 #include "cipher/des3.h"
40 #include "cipher/des.h"
41 
42 //Check crypto library configuration
43 #if (DES3_SUPPORT == ENABLED)
44 
45 //Common interface for encryption algorithms
47 {
48  "3DES",
49  sizeof(Des3Context),
53  NULL,
54  NULL,
57 };
58 
59 
60 /**
61  * @brief Initialize a Triple DES context using the supplied key
62  * @param[in] context Pointer to the Triple DES context to initialize
63  * @param[in] key Pointer to the key
64  * @param[in] keyLen Length of the key
65  * @return Error code
66  **/
67 
68 error_t des3Init(Des3Context *context, const uint8_t *key, size_t keyLen)
69 {
70  //Check key length
71  if(keyLen == 8)
72  {
73  //This option provides backward compatibility with DES, because the
74  //first and second DES operations cancel out
75  desInit(&context->k1, key, 8);
76  desInit(&context->k2, key, 8);
77  desInit(&context->k3, key, 8);
78  }
79  else if(keyLen == 16)
80  {
81  //If the key length is 128 bits including parity, the first 8 bytes of the
82  //encoding represent the key used for the two outer DES operations, and
83  //the second 8 bytes represent the key used for the inner DES operation
84  desInit(&context->k1, key, 8);
85  desInit(&context->k2, key + 8, 8);
86  desInit(&context->k3, key, 8);
87  }
88  else if(keyLen == 24)
89  {
90  //If the key length is 192 bits including parity, then three independent DES
91  //keys are represented, in the order in which they are used for encryption
92  desInit(&context->k1, key, 8);
93  desInit(&context->k2, key + 8, 8);
94  desInit(&context->k3, key + 16, 8);
95  }
96  else
97  {
98  //Invalid key length...
100  }
101 
102  //No error to report
103  return NO_ERROR;
104 }
105 
106 
107 /**
108  * @brief Encrypt a 8-byte block using Triple DES algorithm
109  * @param[in] context Pointer to the Triple DES context
110  * @param[in] input Plaintext block to encrypt
111  * @param[out] output Ciphertext block resulting from encryption
112  **/
113 
114 void des3EncryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output)
115 {
116  //The first pass is a DES encryption
117  desEncryptBlock(&context->k1, input, output);
118  //The second pass is a DES decryption of the first ciphertext result
119  desDecryptBlock(&context->k2, output, output);
120  //The third pass is a DES encryption of the second pass result
121  desEncryptBlock(&context->k3, output, output);
122 }
123 
124 
125 /**
126  * @brief Decrypt a 8-byte block using Triple DES algorithm
127  * @param[in] context Pointer to the Triple DES context
128  * @param[in] input Ciphertext block to decrypt
129  * @param[out] output Plaintext block resulting from decryption
130  **/
131 
132 void des3DecryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output)
133 {
134  //The first pass is a DES decryption
135  desDecryptBlock(&context->k3, input, output);
136  //The second pass is a DES encryption of the first pass result
137  desEncryptBlock(&context->k2, output, output);
138  //The third pass is a DES decryption of the second ciphertext result
139  desDecryptBlock(&context->k1, output, output);
140 }
141 
142 #endif
Triple DES algorithm context.
Definition: des3.h:51
DesContext k1
Definition: des3.h:53
error_t desInit(DesContext *context, const uint8_t *key, size_t keyLen)
Initialize a DES context using the supplied key.
Definition: des.c:269
void desEncryptBlock(DesContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 8-byte block using DES algorithm.
Definition: des.c:341
void(* CipherAlgoDecryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:1030
General definitions for cryptographic algorithms.
error_t des3Init(Des3Context *context, const uint8_t *key, size_t keyLen)
Initialize a Triple DES context using the supplied key.
Definition: des3.c:68
Common interface for encryption algorithms.
Definition: crypto.h:1073
void desDecryptBlock(DesContext *context, const uint8_t *input, uint8_t *output)
Decrypt a 8-byte block using DES algorithm.
Definition: des.c:380
DesContext k2
Definition: des3.h:54
const CipherAlgo des3CipherAlgo
Definition: des3.c:46
error_t(* CipherAlgoInit)(void *context, const uint8_t *key, size_t keyLen)
Definition: crypto.h:1026
Triple DES (Triple Data Encryption Algorithm)
void des3EncryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output)
Encrypt a 8-byte block using Triple DES algorithm.
Definition: des3.c:114
DesContext k3
Definition: des3.h:55
void des3DecryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output)
Decrypt a 8-byte block using Triple DES algorithm.
Definition: des3.c:132
Success.
Definition: error.h:42
DES (Data Encryption Standard)
error_t
Error codes.
Definition: error.h:40
void(* CipherAlgoEncryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:1029
#define DES3_BLOCK_SIZE
Definition: des3.h:37