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  * 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 Output Feedback (OFB) mode is a confidentiality mode that features the
28  * iteration of the forward cipher on an IV to generate a sequence of output
29  * blocks that are exclusive-ORed with the plaintext to produce the ciphertext,
30  * and vice versa. The OFB mode requires that the IV is a nonce, i.e., the IV
31  * must be unique for each execution of the mode under the given key.
32  * 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/ofb.h"
44 #include "debug.h"
45 
46 //Check crypto library configuration
47 #if (OFB_SUPPORT == ENABLED)
48 
49 
50 /**
51  * @brief OFB 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 ofbEncrypt(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  uint8_t o[16];
67 
68  //The parameter must be a multiple of 8
69  if((s % 8) != 0)
71 
72  //Determine the size, in bytes, of the plaintext and ciphertext segments
73  s = s / 8;
74 
75  //Check the resulting value
76  if(s < 1 || s > cipher->blockSize)
78 
79  //Process each plaintext segment
80  while(length >= s)
81  {
82  //Compute O(j) = CIPH(I(j))
83  cipher->encryptBlock(context, iv, o);
84 
85  //Compute C(j) = P(j) XOR MSB(O(j))
86  for(i = 0; i < s; i++)
87  c[i] = p[i] ^ o[i];
88 
89  //Compute I(j+1) = LSB(I(j)) | O(j)
90  cryptoMemmove(iv, iv + s, cipher->blockSize - s);
91  cryptoMemcpy(iv + cipher->blockSize - s, o, s);
92 
93  //Next block
94  p += s;
95  c += s;
96  length -= s;
97  }
98 
99  //The plaintext must be a multiple of the segment size
100  if(length != 0)
101  return ERROR_INVALID_LENGTH;
102 
103  //Successful encryption
104  return NO_ERROR;
105 }
106 
107 
108 /**
109  * @brief OFB 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 ofbDecrypt(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  uint8_t o[16];
125 
126  //The parameter must be a multiple of 8
127  if((s % 8) != 0)
129 
130  //Determine the size, in bytes, of the plaintext and ciphertext segments
131  s = s / 8;
132 
133  //Check the resulting value
134  if(s < 1 || s > cipher->blockSize)
136 
137  //Process each ciphertext segment
138  while(length >= s)
139  {
140  //Compute O(j) = CIPH(I(j))
141  cipher->encryptBlock(context, iv, o);
142 
143  //Compute P(j) = C(j) XOR MSB(O(j))
144  for(i = 0; i < s; i++)
145  p[i] = c[i] ^ o[i];
146 
147  //Compute I(j+1) = LSB(I(j)) | O(j)
148  cryptoMemmove(iv, iv + s, cipher->blockSize - s);
149  cryptoMemcpy(iv + cipher->blockSize - s, o, s);
150 
151  //Next block
152  c += s;
153  p += s;
154  length -= s;
155  }
156 
157  //The plaintext must be a multiple of the segment size
158  if(length != 0)
159  return ERROR_INVALID_LENGTH;
160 
161  //Successful encryption
162  return NO_ERROR;
163 }
164 
165 #endif
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:62
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
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:120
Output Feedback (OFB) mode.
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
uint8_t length
Definition: dtls_misc.h:140
uint8_t o