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