ecb.c
Go to the documentation of this file.
1 /**
2  * @file ecb.c
3  * @brief Electronic Codebook (ECB) 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 Electronic Codebook (ECB) mode is a confidentiality mode that features,
30  * for a given key, the assignment of a fixed ciphertext block to each
31  * plaintext block, analogous to the assignment of code words in a codebook.
32  * Refer to SP 800-38A for more details
33  *
34  * @author Oryx Embedded SARL (www.oryx-embedded.com)
35  * @version 2.4.4
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_modes/ecb.h"
44 #include "debug.h"
45 
46 //Check crypto library configuration
47 #if (ECB_SUPPORT == ENABLED)
48 
49 
50 /**
51  * @brief ECB encryption
52  * @param[in] cipher Cipher algorithm
53  * @param[in] context Cipher algorithm context
54  * @param[in] p Plaintext to be encrypted
55  * @param[out] c Ciphertext resulting from the encryption
56  * @param[in] length Total number of data bytes to be encrypted
57  * @return Error code
58  **/
59 
60 __weak_func error_t ecbEncrypt(const CipherAlgo *cipher, void *context,
61  const uint8_t *p, uint8_t *c, size_t length)
62 {
63  //ECB mode operates in a block-by-block fashion
64  while(length >= cipher->blockSize)
65  {
66  //Encrypt current block
67  cipher->encryptBlock(context, p, c);
68 
69  //Next block
70  p += cipher->blockSize;
71  c += cipher->blockSize;
72  length -= cipher->blockSize;
73  }
74 
75  //The plaintext must be a multiple of the block size
76  if(length != 0)
77  return ERROR_INVALID_LENGTH;
78 
79  //Successful encryption
80  return NO_ERROR;
81 }
82 
83 
84 /**
85  * @brief ECB decryption
86  * @param[in] cipher Cipher algorithm
87  * @param[in] context Cipher algorithm context
88  * @param[in] c Ciphertext to be decrypted
89  * @param[out] p Plaintext resulting from the decryption
90  * @param[in] length Total number of data bytes to be decrypted
91  * @return Error code
92  **/
93 
94 __weak_func error_t ecbDecrypt(const CipherAlgo *cipher, void *context,
95  const uint8_t *c, uint8_t *p, size_t length)
96 {
97  //ECB mode operates in a block-by-block fashion
98  while(length >= cipher->blockSize)
99  {
100  //Decrypt current block
101  cipher->decryptBlock(context, c, p);
102 
103  //Next block
104  c += cipher->blockSize;
105  p += cipher->blockSize;
106  length -= cipher->blockSize;
107  }
108 
109  //The ciphertext must be a multiple of the block size
110  if(length != 0)
111  return ERROR_INVALID_LENGTH;
112 
113  //Successful encryption
114  return NO_ERROR;
115 }
116 
117 #endif
CipherAlgoDecryptBlock decryptBlock
Definition: crypto.h:1077
uint8_t p
Definition: ndp.h:300
size_t blockSize
Definition: crypto.h:1072
Electronic Codebook (ECB) mode.
CipherAlgoEncryptBlock encryptBlock
Definition: crypto.h:1076
error_t
Error codes.
Definition: error.h:43
@ ERROR_INVALID_LENGTH
Definition: error.h:111
__weak_func error_t ecbDecrypt(const CipherAlgo *cipher, void *context, const uint8_t *c, uint8_t *p, size_t length)
ECB decryption.
Definition: ecb.c:94
General definitions for cryptographic algorithms.
uint8_t length
Definition: tcp.h:368
Common interface for encryption algorithms.
Definition: crypto.h:1068
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t c
Definition: ndp.h:514
Debugging facilities.
__weak_func error_t ecbEncrypt(const CipherAlgo *cipher, void *context, const uint8_t *p, uint8_t *c, size_t length)
ECB encryption.
Definition: ecb.c:60