ofb.c
Go to the documentation of this file.
1 /**
2  * @file ofb.c
3  * @brief Output Feedback (OFB) 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 Output Feedback (OFB) mode is a confidentiality mode that features the
30  * iteration of the forward cipher on an IV to generate a sequence of output
31  * blocks that are exclusive-ORed with the plaintext to produce the ciphertext,
32  * and vice versa. The OFB mode requires that the IV is a nonce, i.e., the IV
33  * must be unique for each execution of the mode under the given key.
34  * Refer to SP 800-38A for more details
35  *
36  * @author Oryx Embedded SARL (www.oryx-embedded.com)
37  * @version 2.4.0
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/ofb.h"
46 #include "debug.h"
47 
48 //Check crypto library configuration
49 #if (OFB_SUPPORT == ENABLED)
50 
51 
52 /**
53  * @brief OFB 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 ofbEncrypt(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)) | O(j)
98  osMemmove(iv, iv + s, cipher->blockSize - s);
99  osMemcpy(iv + cipher->blockSize - s, o, 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 OFB 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 error_t ofbDecrypt(const CipherAlgo *cipher, void *context, uint_t s,
125  uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
126 {
127  //Decryption is the same the as encryption with P and C interchanged
128  return ofbEncrypt(cipher, context, s, iv, c, p, length);
129 }
130 
131 #endif
unsigned int uint_t
Definition: compiler_port.h:50
General definitions for cryptographic algorithms.
Debugging facilities.
uint8_t n
uint8_t o
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
uint8_t iv[]
Definition: ike.h:1502
uint8_t c
Definition: ndp.h:514
uint8_t s
Definition: ndp.h:345
uint8_t p
Definition: ndp.h:300
error_t ofbDecrypt(const CipherAlgo *cipher, void *context, uint_t s, uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
OFB decryption.
Definition: ofb.c:124
__weak_func error_t ofbEncrypt(const CipherAlgo *cipher, void *context, uint_t s, uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
OFB encryption.
Definition: ofb.c:64
Output Feedback (OFB) mode.
#define osMemmove(dest, src, length)
Definition: os_port.h:147
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
#define MIN(a, b)
Definition: os_port.h:63
Common interface for encryption algorithms.
Definition: crypto.h:1036
CipherAlgoEncryptBlock encryptBlock
Definition: crypto.h:1044
size_t blockSize
Definition: crypto.h:1040
uint8_t length
Definition: tcp.h:368