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