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-2019 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 1.9.6
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 //Common interface for encryption algorithms
49 {
50  "3DES",
51  sizeof(Des3Context),
55  NULL,
56  NULL,
59 };
60 
61 
62 /**
63  * @brief Initialize a Triple DES context using the supplied key
64  * @param[in] context Pointer to the Triple DES context to initialize
65  * @param[in] key Pointer to the key
66  * @param[in] keyLen Length of the key
67  * @return Error code
68  **/
69 
70 error_t des3Init(Des3Context *context, const uint8_t *key, size_t keyLen)
71 {
72  //Check parameters
73  if(context == NULL || key == NULL)
75 
76  //Check key length
77  if(keyLen == 8)
78  {
79  //This option provides backward compatibility with DES, because the
80  //first and second DES operations cancel out
81  desInit(&context->k1, key, 8);
82  desInit(&context->k2, key, 8);
83  desInit(&context->k3, key, 8);
84  }
85  else if(keyLen == 16)
86  {
87  //If the key length is 128 bits including parity, the first 8 bytes of the
88  //encoding represent the key used for the two outer DES operations, and
89  //the second 8 bytes represent the key used for the inner DES operation
90  desInit(&context->k1, key, 8);
91  desInit(&context->k2, key + 8, 8);
92  desInit(&context->k3, key, 8);
93  }
94  else if(keyLen == 24)
95  {
96  //If the key length is 192 bits including parity, then three independent DES
97  //keys are represented, in the order in which they are used for encryption
98  desInit(&context->k1, key, 8);
99  desInit(&context->k2, key + 8, 8);
100  desInit(&context->k3, key + 16, 8);
101  }
102  else
103  {
104  //Invalid key length...
106  }
107 
108  //No error to report
109  return NO_ERROR;
110 }
111 
112 
113 /**
114  * @brief Encrypt a 8-byte block using Triple DES algorithm
115  * @param[in] context Pointer to the Triple DES context
116  * @param[in] input Plaintext block to encrypt
117  * @param[out] output Ciphertext block resulting from encryption
118  **/
119 
120 void des3EncryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output)
121 {
122  //The first pass is a DES encryption
123  desEncryptBlock(&context->k1, input, output);
124  //The second pass is a DES decryption of the first ciphertext result
125  desDecryptBlock(&context->k2, output, output);
126  //The third pass is a DES encryption of the second pass result
127  desEncryptBlock(&context->k3, output, output);
128 }
129 
130 
131 /**
132  * @brief Decrypt a 8-byte block using Triple DES algorithm
133  * @param[in] context Pointer to the Triple DES context
134  * @param[in] input Ciphertext block to decrypt
135  * @param[out] output Plaintext block resulting from decryption
136  **/
137 
138 void des3DecryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output)
139 {
140  //The first pass is a DES decryption
141  desDecryptBlock(&context->k3, input, output);
142  //The second pass is a DES encryption of the first pass result
143  desEncryptBlock(&context->k2, output, output);
144  //The third pass is a DES decryption of the second ciphertext result
145  desDecryptBlock(&context->k1, output, output);
146 }
147 
148 #endif
void(* CipherAlgoEncryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:1103
@ CIPHER_ALGO_TYPE_BLOCK
Definition: crypto.h:1069
DesContext k1
Definition: des3.h:55
#define DES3_BLOCK_SIZE
Definition: des3.h:39
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t
Error codes.
Definition: error.h:42
void(* CipherAlgoDecryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:1104
@ ERROR_INVALID_KEY_LENGTH
Definition: error.h:105
error_t des3Init(Des3Context *context, const uint8_t *key, size_t keyLen)
Initialize a Triple DES context using the supplied key.
Definition: des3.c:70
General definitions for cryptographic algorithms.
void des3EncryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output)
Encrypt a 8-byte block using Triple DES algorithm.
Definition: des3.c:120
const CipherAlgo des3CipherAlgo
Definition: des3.c:48
void desEncryptBlock(DesContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 8-byte block using DES algorithm.
Definition: des.c:347
DesContext k3
Definition: des3.h:57
DES (Data Encryption Standard)
error_t(* CipherAlgoInit)(void *context, const uint8_t *key, size_t keyLen)
Definition: crypto.h:1100
Triple DES algorithm context.
Definition: des3.h:53
void des3DecryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output)
Decrypt a 8-byte block using Triple DES algorithm.
Definition: des3.c:138
Common interface for encryption algorithms.
Definition: crypto.h:1150
DesContext k2
Definition: des3.h:56
error_t desInit(DesContext *context, const uint8_t *key, size_t keyLen)
Initialize a DES context using the supplied key.
Definition: des.c:271
void desDecryptBlock(DesContext *context, const uint8_t *input, uint8_t *output)
Decrypt a 8-byte block using DES algorithm.
Definition: des.c:386
Triple DES (Triple Data Encryption Algorithm)
@ NO_ERROR
Success.
Definition: error.h:44