cfb.c
Go to the documentation of this file.
1 /**
2  * @file cfb.c
3  * @brief Cipher Feedback (CFB) 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 Feedback (CFB) mode is a confidentiality mode that features the
28  * feedback of successive ciphertext segments into the input blocks of the
29  * forward cipher to generate output blocks that are exclusive-ORed with the
30  * plaintext to produce the ciphertext, and vice versa. The CFB mode requires
31  * an IV as the initial input block. The IV need not be secret, but it must be
32  * unpredictable. Refer to SP 800-38A for more details
33  *
34  * @author Oryx Embedded SARL (www.oryx-embedded.com)
35  * @version 1.9.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_mode/cfb.h"
44 #include "debug.h"
45 
46 //Check crypto library configuration
47 #if (CFB_SUPPORT == ENABLED)
48 
49 
50 /**
51  * @brief CFB encryption
52  * @param[in] cipher Cipher algorithm
53  * @param[in] context Cipher algorithm context
54  * @param[in] s Size of the plaintext and ciphertext segments
55  * @param[in,out] iv Initialization vector
56  * @param[in] p Plaintext to be encrypted
57  * @param[out] c Ciphertext resulting from the encryption
58  * @param[in] length Total number of data bytes to be encrypted
59  * @return Error code
60  **/
61 
62 error_t cfbEncrypt(const CipherAlgo *cipher, void *context, uint_t s,
63  uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
64 {
65  size_t i;
66  size_t n;
67  uint8_t o[16];
68 
69  //The parameter must be a multiple of 8
70  if((s % 8) != 0)
72 
73  //Determine the size, in bytes, of the plaintext and ciphertext segments
74  s = s / 8;
75 
76  //Check the resulting value
77  if(s < 1 || s > cipher->blockSize)
79 
80  //Process each plaintext segment
81  while(length > 0)
82  {
83  //Compute the number of bytes to process at a time
84  n = MIN(length, s);
85 
86  //Compute O(j) = CIPH(I(j))
87  cipher->encryptBlock(context, iv, o);
88 
89  //Compute C(j) = P(j) XOR MSB(O(j))
90  for(i = 0; i < n; i++)
91  c[i] = p[i] ^ o[i];
92 
93  //Compute I(j+1) = LSB(I(j)) | C(j)
94  cryptoMemmove(iv, iv + s, cipher->blockSize - s);
95  cryptoMemcpy(iv + cipher->blockSize - s, c, s);
96 
97  //Next block
98  p += n;
99  c += n;
100  length -= n;
101  }
102 
103  //Successful encryption
104  return NO_ERROR;
105 }
106 
107 
108 /**
109  * @brief CFB decryption
110  * @param[in] cipher Cipher algorithm
111  * @param[in] context Cipher algorithm context
112  * @param[in] s Size of the plaintext and ciphertext segments
113  * @param[in,out] iv Initialization vector
114  * @param[in] c Ciphertext to be decrypted
115  * @param[out] p Plaintext resulting from the decryption
116  * @param[in] length Total number of data bytes to be decrypted
117  * @return Error code
118  **/
119 
120 error_t cfbDecrypt(const CipherAlgo *cipher, void *context, uint_t s,
121  uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
122 {
123  size_t i;
124  size_t n;
125  uint8_t o[16];
126 
127  //The parameter must be a multiple of 8
128  if((s % 8) != 0)
130 
131  //Determine the size, in bytes, of the plaintext and ciphertext segments
132  s = s / 8;
133 
134  //Check the resulting value
135  if(s < 1 || s > cipher->blockSize)
137 
138  //Process each ciphertext segment
139  while(length > 0)
140  {
141  //Compute the number of bytes to process at a time
142  n = MIN(length, s);
143 
144  //Compute O(j) = CIPH(I(j))
145  cipher->encryptBlock(context, iv, o);
146 
147  //Compute I(j+1) = LSB(I(j)) | C(j)
148  cryptoMemmove(iv, iv + s, cipher->blockSize - s);
149  cryptoMemcpy(iv + cipher->blockSize - s, c, s);
150 
151  //Compute P(j) = C(j) XOR MSB(O(j))
152  for(i = 0; i < n; i++)
153  p[i] = c[i] ^ o[i];
154 
155  //Next block
156  c += n;
157  p += n;
158  length -= n;
159  }
160 
161  //Successful encryption
162  return NO_ERROR;
163 }
164 
165 #endif
uint8_t c
Definition: ndp.h:510
#define cryptoMemcpy(dest, src, length)
Definition: crypto.h:590
Debugging facilities.
uint8_t p
Definition: ndp.h:295
CipherAlgoEncryptBlock encryptBlock
Definition: crypto.h:1082
General definitions for cryptographic algorithms.
Invalid parameter.
Definition: error.h:45
Common interface for encryption algorithms.
Definition: crypto.h:1073
#define cryptoMemmove(dest, src, length)
Definition: crypto.h:596
size_t blockSize
Definition: crypto.h:1078
#define MIN(a, b)
Definition: os_port.h:60
uint8_t s
Success.
Definition: error.h:42
error_t
Error codes.
Definition: error.h:40
unsigned int uint_t
Definition: compiler_port.h:43
error_t cfbEncrypt(const CipherAlgo *cipher, void *context, uint_t s, uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
CFB encryption.
Definition: cfb.c:62
uint8_t length
Definition: dtls_misc.h:140
uint8_t n
error_t cfbDecrypt(const CipherAlgo *cipher, void *context, uint_t s, uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
CFB decryption.
Definition: cfb.c:120
uint8_t o
Cipher Feedback (CFB) mode.