Sergey Pastor / 1

Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cipher_mode_ecb.c Source File

cipher_mode_ecb.c

Go to the documentation of this file.
00001 /**
00002  * @file cipher_mode_ecb.c
00003  * @brief Electronic Codebook (ECB) mode
00004  *
00005  * @section License
00006  *
00007  * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
00008  *
00009  * This file is part of CycloneCrypto Open.
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License
00013  * as published by the Free Software Foundation; either version 2
00014  * of the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software Foundation,
00023  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024  *
00025  * @section Description
00026  *
00027  * The Electronic Codebook (ECB) mode is a confidentiality mode that features,
00028  * for a given key, the assignment of a fixed ciphertext block to each
00029  * plaintext block, analogous to the assignment of code words in a codebook.
00030  * Refer to SP 800-38A for more details
00031  *
00032  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00033  * @version 1.7.6
00034  **/
00035 
00036 //Switch to the appropriate trace level
00037 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
00038 
00039 //Dependencies
00040 #include <string.h>
00041 #include "crypto.h"
00042 #include "cipher_mode_ecb.h"
00043 #include "debug.h"
00044 
00045 //Check crypto library configuration
00046 #if (ECB_SUPPORT == ENABLED)
00047 
00048 
00049 /**
00050  * @brief ECB encryption
00051  * @param[in] cipher Cipher algorithm
00052  * @param[in] context Cipher algorithm context
00053  * @param[in] p Plaintext to be encrypted
00054  * @param[out] c Ciphertext resulting from the encryption
00055  * @param[in] length Total number of data bytes to be encrypted
00056  * @return Error code
00057  **/
00058 
00059 error_t ecbEncrypt(const CipherAlgo *cipher, void *context,
00060    const uint8_t *p, uint8_t *c, size_t length)
00061 {
00062    //ECB mode operates in a block-by-block fashion
00063    while(length >= cipher->blockSize)
00064    {
00065       //Encrypt current block
00066       cipher->encryptBlock(context, p, c);
00067 
00068       //Next block
00069       p += cipher->blockSize;
00070       c += cipher->blockSize;
00071       length -= cipher->blockSize;
00072    }
00073 
00074    //The plaintext must be a multiple of the block size
00075    if(length != 0)
00076       return ERROR_INVALID_LENGTH;
00077 
00078    //Successful encryption
00079    return NO_ERROR;
00080 }
00081 
00082 
00083 /**
00084  * @brief ECB decryption
00085  * @param[in] cipher Cipher algorithm
00086  * @param[in] context Cipher algorithm context
00087  * @param[in] c Ciphertext to be decrypted
00088  * @param[out] p Plaintext resulting from the decryption
00089  * @param[in] length Total number of data bytes to be decrypted
00090  * @return Error code
00091  **/
00092 
00093 error_t ecbDecrypt(const CipherAlgo *cipher, void *context,
00094    const uint8_t *c, uint8_t *p, size_t length)
00095 {
00096    //ECB mode operates in a block-by-block fashion
00097    while(length >= cipher->blockSize)
00098    {
00099       //Decrypt current block
00100       cipher->decryptBlock(context, c, p);
00101 
00102       //Next block
00103       c += cipher->blockSize;
00104       p += cipher->blockSize;
00105       length -= cipher->blockSize;
00106    }
00107 
00108    //The ciphertext must be a multiple of the block size
00109    if(length != 0)
00110       return ERROR_INVALID_LENGTH;
00111 
00112    //Successful encryption
00113    return NO_ERROR;
00114 }
00115 
00116 #endif
00117