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  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @section Description
28  *
29  * Triple DES is an encryption algorithm designed to encipher and decipher blocks
30  * of 64 bits under control of a 192-bit key. Refer to FIPS 46-3 for more details
31  *
32  * @author Oryx Embedded SARL (www.oryx-embedded.com)
33  * @version 2.4.0
34  **/
35 
36 //Switch to the appropriate trace level
37 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
38 
39 //Dependencies
40 #include "core/crypto.h"
41 #include "cipher/des3.h"
42 #include "cipher/des.h"
43 
44 //Check crypto library configuration
45 #if (DES3_SUPPORT == ENABLED)
46 
47 //DES-EDE3-CBC OID (1.2.840.113549.3.7)
48 const uint8_t DES_EDE3_CBC_OID[8] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03, 0x07};
49 
50 //Common interface for encryption algorithms
52 {
53  "3DES",
54  sizeof(Des3Context),
58  NULL,
59  NULL,
63 };
64 
65 
66 /**
67  * @brief Initialize a Triple DES context using the supplied key
68  * @param[in] context Pointer to the Triple DES context to initialize
69  * @param[in] key Pointer to the key
70  * @param[in] keyLen Length of the key
71  * @return Error code
72  **/
73 
74 __weak_func error_t des3Init(Des3Context *context, const uint8_t *key,
75  size_t keyLen)
76 {
77  //Check parameters
78  if(context == NULL || key == NULL)
80 
81  //Check key length
82  if(keyLen == 8)
83  {
84  //This option provides backward compatibility with DES, because the
85  //first and second DES operations cancel out
86  desInit(&context->k1, key, 8);
87  desInit(&context->k2, key, 8);
88  desInit(&context->k3, key, 8);
89  }
90  else if(keyLen == 16)
91  {
92  //If the key length is 128 bits including parity, the first 8 bytes of the
93  //encoding represent the key used for the two outer DES operations, and
94  //the second 8 bytes represent the key used for the inner DES operation
95  desInit(&context->k1, key, 8);
96  desInit(&context->k2, key + 8, 8);
97  desInit(&context->k3, key, 8);
98  }
99  else if(keyLen == 24)
100  {
101  //If the key length is 192 bits including parity, then 3 independent DES
102  //keys are represented, in the order in which they are used for encryption
103  desInit(&context->k1, key, 8);
104  desInit(&context->k2, key + 8, 8);
105  desInit(&context->k3, key + 16, 8);
106  }
107  else
108  {
109  //The length of the key is not valid
111  }
112 
113  //No error to report
114  return NO_ERROR;
115 }
116 
117 
118 /**
119  * @brief Encrypt a 8-byte block using Triple DES algorithm
120  * @param[in] context Pointer to the Triple DES context
121  * @param[in] input Plaintext block to encrypt
122  * @param[out] output Ciphertext block resulting from encryption
123  **/
124 
125 __weak_func void des3EncryptBlock(Des3Context *context, const uint8_t *input,
126  uint8_t *output)
127 {
128  //The first pass is a DES encryption
129  desEncryptBlock(&context->k1, input, output);
130  //The second pass is a DES decryption of the first ciphertext result
131  desDecryptBlock(&context->k2, output, output);
132  //The third pass is a DES encryption of the second pass result
133  desEncryptBlock(&context->k3, output, output);
134 }
135 
136 
137 /**
138  * @brief Decrypt a 8-byte block using Triple DES algorithm
139  * @param[in] context Pointer to the Triple DES context
140  * @param[in] input Ciphertext block to decrypt
141  * @param[out] output Plaintext block resulting from decryption
142  **/
143 
144 __weak_func void des3DecryptBlock(Des3Context *context, const uint8_t *input,
145  uint8_t *output)
146 {
147  //The first pass is a DES decryption
148  desDecryptBlock(&context->k3, input, output);
149  //The second pass is a DES encryption of the first pass result
150  desEncryptBlock(&context->k2, output, output);
151  //The third pass is a DES decryption of the second ciphertext result
152  desDecryptBlock(&context->k1, output, output);
153 }
154 
155 
156 /**
157  * @brief Release Triple DES context
158  * @param[in] context Pointer to the Triple DES context
159  **/
160 
161 __weak_func void des3Deinit(Des3Context *context)
162 {
163  //Clear Triple DES context
164  osMemset(context, 0, sizeof(Des3Context));
165 }
166 
167 #endif
General definitions for cryptographic algorithms.
void(* CipherAlgoDeinit)(void *context)
Definition: crypto.h:983
void(* CipherAlgoDecryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:980
error_t(* CipherAlgoInit)(void *context, const uint8_t *key, size_t keyLen)
Definition: crypto.h:968
void(* CipherAlgoEncryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:977
@ CIPHER_ALGO_TYPE_BLOCK
Definition: crypto.h:932
const uint8_t DES_EDE3_CBC_OID[8]
Definition: des3.c:48
__weak_func error_t des3Init(Des3Context *context, const uint8_t *key, size_t keyLen)
Initialize a Triple DES context using the supplied key.
Definition: des3.c:74
__weak_func void des3EncryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output)
Encrypt a 8-byte block using Triple DES algorithm.
Definition: des3.c:125
__weak_func void des3DecryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output)
Decrypt a 8-byte block using Triple DES algorithm.
Definition: des3.c:144
const CipherAlgo des3CipherAlgo
Definition: des3.c:51
__weak_func void des3Deinit(Des3Context *context)
Release Triple DES context.
Definition: des3.c:161
Triple DES (Triple Data Encryption Algorithm)
#define DES3_BLOCK_SIZE
Definition: des3.h:44
__weak_func void desEncryptBlock(DesContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 8-byte block using DES algorithm.
Definition: des.c:351
__weak_func void desDecryptBlock(DesContext *context, const uint8_t *input, uint8_t *output)
Decrypt a 8-byte block using DES algorithm.
Definition: des.c:391
__weak_func error_t desInit(DesContext *context, const uint8_t *key, size_t keyLen)
Initialize a DES context using the supplied key.
Definition: des.c:296
DES (Data Encryption Standard)
error_t
Error codes.
Definition: error.h:43
@ ERROR_INVALID_KEY_LENGTH
Definition: error.h:107
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define osMemset(p, value, length)
Definition: os_port.h:135
Common interface for encryption algorithms.
Definition: crypto.h:1036
Triple DES algorithm context.
Definition: des3.h:59
DesContext k2
Definition: des3.h:61
DesContext k3
Definition: des3.h:62
DesContext k1
Definition: des3.h:60