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-2020 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 1.9.8
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_mode/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 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  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 P(j) = C(j) XOR MSB(O(j))
152  for(i = 0; i < n; i++)
153  {
154  p[i] = c[i] ^ o[i];
155  }
156 
157  //Compute I(j+1) = LSB(I(j)) | O(j)
158  osMemmove(iv, iv + s, cipher->blockSize - s);
159  osMemcpy(iv + cipher->blockSize - s, o, s);
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
uint8_t length
Definition: coap_common.h:190
uint8_t p
Definition: ndp.h:298
uint8_t o
size_t blockSize
Definition: crypto.h:1089
CipherAlgoEncryptBlock encryptBlock
Definition: crypto.h:1093
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
Invalid parameter.
Definition: error.h:47
#define osMemcpy(dest, src, length)
Definition: os_port.h:134
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
error_t
Error codes.
Definition: error.h:42
Output Feedback (OFB) mode.
General definitions for cryptographic algorithms.
#define MIN(a, b)
Definition: os_port.h:62
uint8_t n
uint8_t s
Common interface for encryption algorithms.
Definition: crypto.h:1084
unsigned int uint_t
Definition: compiler_port.h:45
Success.
Definition: error.h:44
uint8_t c
Definition: ndp.h:513
Debugging facilities.
#define osMemmove(dest, src, length)
Definition: os_port.h:140