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  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 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 Feedback (CFB) mode is a confidentiality mode that features the
30  * feedback of successive ciphertext segments into the input blocks of the
31  * forward cipher to generate output blocks that are exclusive-ORed with the
32  * plaintext to produce the ciphertext, and vice versa. The CFB mode requires
33  * an IV as the initial input block. The IV need not be secret, but it must be
34  * unpredictable. Refer to SP 800-38A for more details
35  *
36  * @author Oryx Embedded SARL (www.oryx-embedded.com)
37  * @version 2.6.6
38  **/
39 
40 //Switch to the appropriate trace level
41 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
42 
43 //Dependencies
44 #include "core/crypto.h"
45 #include "cipher_modes/cfb.h"
46 #include "debug.h"
47 
48 //Check crypto library configuration
49 #if (CFB_SUPPORT == ENABLED)
50 
51 
52 /**
53  * @brief CFB encryption
54  * @param[in] cipher Cipher algorithm
55  * @param[in] context Cipher algorithm context
56  * @param[in] s Size of the plaintext and ciphertext segments
57  * @param[in,out] iv Initialization vector
58  * @param[in] p Plaintext to be encrypted
59  * @param[out] c Ciphertext resulting from the encryption
60  * @param[in] length Total number of data bytes to be encrypted
61  * @return Error code
62  **/
63 
64 __weak_func error_t cfbEncrypt(const CipherAlgo *cipher, void *context, uint_t s,
65  uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
66 {
67  size_t i;
68  size_t n;
69  uint8_t o[16];
70 
71  //The parameter must be a multiple of 8
72  if((s % 8) != 0)
74 
75  //Determine the size, in bytes, of the plaintext and ciphertext segments
76  s = s / 8;
77 
78  //Check the resulting value
79  if(s < 1 || s > cipher->blockSize)
81 
82  //Process each plaintext segment
83  while(length > 0)
84  {
85  //Compute the number of bytes to process at a time
86  n = MIN(length, s);
87 
88  //Compute O(j) = CIPH(I(j))
89  cipher->encryptBlock(context, iv, o);
90 
91  //Compute C(j) = P(j) XOR MSB(O(j))
92  for(i = 0; i < n; i++)
93  {
94  c[i] = p[i] ^ o[i];
95  }
96 
97  //Complete block?
98  if(n == s)
99  {
100  //Compute I(j+1) = LSB(I(j)) | C(j)
101  osMemmove(iv, iv + s, cipher->blockSize - s);
102  osMemcpy(iv + cipher->blockSize - s, c, s);
103  }
104 
105  //Next block
106  p += n;
107  c += n;
108  length -= n;
109  }
110 
111  //Successful encryption
112  return NO_ERROR;
113 }
114 
115 
116 /**
117  * @brief CFB decryption
118  * @param[in] cipher Cipher algorithm
119  * @param[in] context Cipher algorithm context
120  * @param[in] s Size of the plaintext and ciphertext segments
121  * @param[in,out] iv Initialization vector
122  * @param[in] c Ciphertext to be decrypted
123  * @param[out] p Plaintext resulting from the decryption
124  * @param[in] length Total number of data bytes to be decrypted
125  * @return Error code
126  **/
127 
128 __weak_func error_t cfbDecrypt(const CipherAlgo *cipher, void *context, uint_t s,
129  uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
130 {
131  size_t i;
132  size_t n;
133  uint8_t o[16];
134 
135  //The parameter must be a multiple of 8
136  if((s % 8) != 0)
138 
139  //Determine the size, in bytes, of the plaintext and ciphertext segments
140  s = s / 8;
141 
142  //Check the resulting value
143  if(s < 1 || s > cipher->blockSize)
145 
146  //Process each ciphertext segment
147  while(length > 0)
148  {
149  //Compute the number of bytes to process at a time
150  n = MIN(length, s);
151 
152  //Compute O(j) = CIPH(I(j))
153  cipher->encryptBlock(context, iv, o);
154 
155  //Complete block?
156  if(n == s)
157  {
158  //Compute I(j+1) = LSB(I(j)) | C(j)
159  osMemmove(iv, iv + s, cipher->blockSize - s);
160  osMemcpy(iv + cipher->blockSize - s, c, s);
161  }
162 
163  //Compute P(j) = C(j) XOR MSB(O(j))
164  for(i = 0; i < n; i++)
165  {
166  p[i] = c[i] ^ o[i];
167  }
168 
169  //Next block
170  c += n;
171  p += n;
172  length -= n;
173  }
174 
175  //Successful encryption
176  return NO_ERROR;
177 }
178 
179 #endif
__weak_func 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:64
uint8_t p
Definition: ndp.h:300
uint8_t o
size_t blockSize
Definition: crypto.h:1289
CipherAlgoEncryptBlock encryptBlock
Definition: crypto.h:1293
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define osMemcpy(dest, src, length)
Definition: os_port.h:147
error_t
Error codes.
Definition: error.h:43
__weak_func 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:128
General definitions for cryptographic algorithms.
uint8_t iv[]
Definition: ike.h:1695
uint8_t length
Definition: tcp.h:375
#define MIN(a, b)
Definition: os_port.h:63
uint8_t n
Common interface for encryption algorithms.
Definition: crypto.h:1285
uint8_t s
Definition: igmp_common.h:234
unsigned int uint_t
Definition: compiler_port.h:57
Cipher Feedback (CFB) mode.
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t c
Definition: ndp.h:514
Debugging facilities.
#define osMemmove(dest, src, length)
Definition: os_port.h:153