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-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 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.4.4
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  //Compute I(j+1) = LSB(I(j)) | C(j)
98  osMemmove(iv, iv + s, cipher->blockSize - s);
99  osMemcpy(iv + cipher->blockSize - s, c, s);
100 
101  //Next block
102  p += n;
103  c += n;
104  length -= n;
105  }
106 
107  //Successful encryption
108  return NO_ERROR;
109 }
110 
111 
112 /**
113  * @brief CFB decryption
114  * @param[in] cipher Cipher algorithm
115  * @param[in] context Cipher algorithm context
116  * @param[in] s Size of the plaintext and ciphertext segments
117  * @param[in,out] iv Initialization vector
118  * @param[in] c Ciphertext to be decrypted
119  * @param[out] p Plaintext resulting from the decryption
120  * @param[in] length Total number of data bytes to be decrypted
121  * @return Error code
122  **/
123 
124 __weak_func error_t cfbDecrypt(const CipherAlgo *cipher, void *context, uint_t s,
125  uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
126 {
127  size_t i;
128  size_t n;
129  uint8_t o[16];
130 
131  //The parameter must be a multiple of 8
132  if((s % 8) != 0)
134 
135  //Determine the size, in bytes, of the plaintext and ciphertext segments
136  s = s / 8;
137 
138  //Check the resulting value
139  if(s < 1 || s > cipher->blockSize)
141 
142  //Process each ciphertext segment
143  while(length > 0)
144  {
145  //Compute the number of bytes to process at a time
146  n = MIN(length, s);
147 
148  //Compute O(j) = CIPH(I(j))
149  cipher->encryptBlock(context, iv, o);
150 
151  //Compute I(j+1) = LSB(I(j)) | C(j)
152  osMemmove(iv, iv + s, cipher->blockSize - s);
153  osMemcpy(iv + cipher->blockSize - s, c, s);
154 
155  //Compute P(j) = C(j) XOR MSB(O(j))
156  for(i = 0; i < n; i++)
157  {
158  p[i] = c[i] ^ o[i];
159  }
160 
161  //Next block
162  c += n;
163  p += n;
164  length -= n;
165  }
166 
167  //Successful encryption
168  return NO_ERROR;
169 }
170 
171 #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:1072
CipherAlgoEncryptBlock encryptBlock
Definition: crypto.h:1076
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
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:124
General definitions for cryptographic algorithms.
uint8_t iv[]
Definition: ike.h:1502
uint8_t length
Definition: tcp.h:368
#define MIN(a, b)
Definition: os_port.h:63
uint8_t n
Common interface for encryption algorithms.
Definition: crypto.h:1068
uint8_t s
Definition: igmp_common.h:234
unsigned int uint_t
Definition: compiler_port.h:50
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:147