cbc.c
Go to the documentation of this file.
1 /**
2  * @file cbc.c
3  * @brief Cipher Block Chaining (CBC) mode
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  * The Cipher Block Chaining (CBC) mode is a confidentiality mode whose
30  * encryption process features the combining of the plaintext blocks with
31  * the previous ciphertext blocks. The CBC mode requires an IV to combine
32  * with the first plaintext block. Refer to SP 800-38A for more details
33  *
34  * @author Oryx Embedded SARL (www.oryx-embedded.com)
35  * @version 2.4.0
36  **/
37 
38 //Switch to the appropriate trace level
39 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
40 
41 //Dependencies
42 #include "core/crypto.h"
43 #include "cipher_modes/cbc.h"
44 #include "debug.h"
45 
46 //Check crypto library configuration
47 #if (CBC_SUPPORT == ENABLED)
48 
49 
50 /**
51  * @brief CBC encryption
52  * @param[in] cipher Cipher algorithm
53  * @param[in] context Cipher algorithm context
54  * @param[in,out] iv Initialization vector
55  * @param[in] p Plaintext to be encrypted
56  * @param[out] c Ciphertext resulting from the encryption
57  * @param[in] length Total number of data bytes to be encrypted
58  * @return Error code
59  **/
60 
61 __weak_func error_t cbcEncrypt(const CipherAlgo *cipher, void *context,
62  uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
63 {
64  size_t i;
65 
66  //CBC mode operates in a block-by-block fashion
67  while(length >= cipher->blockSize)
68  {
69  //XOR input block with IV contents
70  for(i = 0; i < cipher->blockSize; i++)
71  {
72  c[i] = p[i] ^ iv[i];
73  }
74 
75  //Encrypt the current block based upon the output of the previous
76  //encryption
77  cipher->encryptBlock(context, c, c);
78 
79  //Update IV with output block contents
80  osMemcpy(iv, c, cipher->blockSize);
81 
82  //Next block
83  p += cipher->blockSize;
84  c += cipher->blockSize;
85  length -= cipher->blockSize;
86  }
87 
88  //The plaintext must be a multiple of the block size
89  if(length != 0)
90  return ERROR_INVALID_LENGTH;
91 
92  //Successful encryption
93  return NO_ERROR;
94 }
95 
96 
97 /**
98  * @brief CBC decryption
99  * @param[in] cipher Cipher algorithm
100  * @param[in] context Cipher algorithm context
101  * @param[in,out] iv Initialization vector
102  * @param[in] c Ciphertext to be decrypted
103  * @param[out] p Plaintext resulting from the decryption
104  * @param[in] length Total number of data bytes to be decrypted
105  * @return Error code
106  **/
107 
108 __weak_func error_t cbcDecrypt(const CipherAlgo *cipher, void *context,
109  uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
110 {
111  size_t i;
112  uint8_t t[16];
113 
114  //CBC mode operates in a block-by-block fashion
115  while(length >= cipher->blockSize)
116  {
117  //Save input block
118  osMemcpy(t, c, cipher->blockSize);
119 
120  //Decrypt the current block
121  cipher->decryptBlock(context, c, p);
122 
123  //XOR output block with IV contents
124  for(i = 0; i < cipher->blockSize; i++)
125  {
126  p[i] ^= iv[i];
127  }
128 
129  //Update IV with input block contents
130  osMemcpy(iv, t, cipher->blockSize);
131 
132  //Next block
133  c += cipher->blockSize;
134  p += cipher->blockSize;
135  length -= cipher->blockSize;
136  }
137 
138  //The ciphertext must be a multiple of the block size
139  if(length != 0)
140  return ERROR_INVALID_LENGTH;
141 
142  //Successful encryption
143  return NO_ERROR;
144 }
145 
146 #endif
__weak_func error_t cbcDecrypt(const CipherAlgo *cipher, void *context, uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
CBC decryption.
Definition: cbc.c:108
__weak_func error_t cbcEncrypt(const CipherAlgo *cipher, void *context, uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
CBC encryption.
Definition: cbc.c:61
Cipher Block Chaining (CBC) mode.
General definitions for cryptographic algorithms.
Debugging facilities.
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_INVALID_LENGTH
Definition: error.h:111
uint8_t iv[]
Definition: ike.h:1502
uint8_t t
Definition: lldp_ext_med.h:212
uint8_t c
Definition: ndp.h:514
uint8_t p
Definition: ndp.h:300
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
Common interface for encryption algorithms.
Definition: crypto.h:1036
CipherAlgoEncryptBlock encryptBlock
Definition: crypto.h:1044
CipherAlgoDecryptBlock decryptBlock
Definition: crypto.h:1045
size_t blockSize
Definition: crypto.h:1040
uint8_t length
Definition: tcp.h:368