Webserver+3d print
cyclone_crypto/cipher_mode_cbc.c@0:8918a71cdbe9, 2017-02-04 (annotated)
- Committer:
- Sergunb
- Date:
- Sat Feb 04 18:15:49 2017 +0000
- Revision:
- 0:8918a71cdbe9
nothing else
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sergunb | 0:8918a71cdbe9 | 1 | /** |
Sergunb | 0:8918a71cdbe9 | 2 | * @file cipher_mode_cbc.c |
Sergunb | 0:8918a71cdbe9 | 3 | * @brief Cipher Block Chaining (CBC) mode |
Sergunb | 0:8918a71cdbe9 | 4 | * |
Sergunb | 0:8918a71cdbe9 | 5 | * @section License |
Sergunb | 0:8918a71cdbe9 | 6 | * |
Sergunb | 0:8918a71cdbe9 | 7 | * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved. |
Sergunb | 0:8918a71cdbe9 | 8 | * |
Sergunb | 0:8918a71cdbe9 | 9 | * This file is part of CycloneCrypto Open. |
Sergunb | 0:8918a71cdbe9 | 10 | * |
Sergunb | 0:8918a71cdbe9 | 11 | * This program is free software; you can redistribute it and/or |
Sergunb | 0:8918a71cdbe9 | 12 | * modify it under the terms of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 13 | * as published by the Free Software Foundation; either version 2 |
Sergunb | 0:8918a71cdbe9 | 14 | * of the License, or (at your option) any later version. |
Sergunb | 0:8918a71cdbe9 | 15 | * |
Sergunb | 0:8918a71cdbe9 | 16 | * This program is distributed in the hope that it will be useful, |
Sergunb | 0:8918a71cdbe9 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Sergunb | 0:8918a71cdbe9 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Sergunb | 0:8918a71cdbe9 | 19 | * GNU General Public License for more details. |
Sergunb | 0:8918a71cdbe9 | 20 | * |
Sergunb | 0:8918a71cdbe9 | 21 | * You should have received a copy of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 22 | * along with this program; if not, write to the Free Software Foundation, |
Sergunb | 0:8918a71cdbe9 | 23 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
Sergunb | 0:8918a71cdbe9 | 24 | * |
Sergunb | 0:8918a71cdbe9 | 25 | * @section Description |
Sergunb | 0:8918a71cdbe9 | 26 | * |
Sergunb | 0:8918a71cdbe9 | 27 | * The Cipher Block Chaining (CBC) mode is a confidentiality mode whose |
Sergunb | 0:8918a71cdbe9 | 28 | * encryption process features the combining of the plaintext blocks with |
Sergunb | 0:8918a71cdbe9 | 29 | * the previous ciphertext blocks. The CBC mode requires an IV to combine |
Sergunb | 0:8918a71cdbe9 | 30 | * with the first plaintext block. Refer to SP 800-38A for more details |
Sergunb | 0:8918a71cdbe9 | 31 | * |
Sergunb | 0:8918a71cdbe9 | 32 | * @author Oryx Embedded SARL (www.oryx-embedded.com) |
Sergunb | 0:8918a71cdbe9 | 33 | * @version 1.7.6 |
Sergunb | 0:8918a71cdbe9 | 34 | **/ |
Sergunb | 0:8918a71cdbe9 | 35 | |
Sergunb | 0:8918a71cdbe9 | 36 | //Switch to the appropriate trace level |
Sergunb | 0:8918a71cdbe9 | 37 | #define TRACE_LEVEL CRYPTO_TRACE_LEVEL |
Sergunb | 0:8918a71cdbe9 | 38 | |
Sergunb | 0:8918a71cdbe9 | 39 | //Dependencies |
Sergunb | 0:8918a71cdbe9 | 40 | #include <string.h> |
Sergunb | 0:8918a71cdbe9 | 41 | #include "crypto.h" |
Sergunb | 0:8918a71cdbe9 | 42 | #include "cipher_mode_cbc.h" |
Sergunb | 0:8918a71cdbe9 | 43 | #include "debug.h" |
Sergunb | 0:8918a71cdbe9 | 44 | |
Sergunb | 0:8918a71cdbe9 | 45 | //Check crypto library configuration |
Sergunb | 0:8918a71cdbe9 | 46 | #if (CBC_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 47 | |
Sergunb | 0:8918a71cdbe9 | 48 | |
Sergunb | 0:8918a71cdbe9 | 49 | /** |
Sergunb | 0:8918a71cdbe9 | 50 | * @brief CBC encryption |
Sergunb | 0:8918a71cdbe9 | 51 | * @param[in] cipher Cipher algorithm |
Sergunb | 0:8918a71cdbe9 | 52 | * @param[in] context Cipher algorithm context |
Sergunb | 0:8918a71cdbe9 | 53 | * @param[in,out] iv Initialization vector |
Sergunb | 0:8918a71cdbe9 | 54 | * @param[in] p Plaintext to be encrypted |
Sergunb | 0:8918a71cdbe9 | 55 | * @param[out] c Ciphertext resulting from the encryption |
Sergunb | 0:8918a71cdbe9 | 56 | * @param[in] length Total number of data bytes to be encrypted |
Sergunb | 0:8918a71cdbe9 | 57 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 58 | **/ |
Sergunb | 0:8918a71cdbe9 | 59 | |
Sergunb | 0:8918a71cdbe9 | 60 | error_t cbcEncrypt(const CipherAlgo *cipher, void *context, |
Sergunb | 0:8918a71cdbe9 | 61 | uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length) |
Sergunb | 0:8918a71cdbe9 | 62 | { |
Sergunb | 0:8918a71cdbe9 | 63 | size_t i; |
Sergunb | 0:8918a71cdbe9 | 64 | |
Sergunb | 0:8918a71cdbe9 | 65 | //CBC mode operates in a block-by-block fashion |
Sergunb | 0:8918a71cdbe9 | 66 | while(length >= cipher->blockSize) |
Sergunb | 0:8918a71cdbe9 | 67 | { |
Sergunb | 0:8918a71cdbe9 | 68 | //XOR input block with IV contents |
Sergunb | 0:8918a71cdbe9 | 69 | for(i = 0; i < cipher->blockSize; i++) |
Sergunb | 0:8918a71cdbe9 | 70 | c[i] = p[i] ^ iv[i]; |
Sergunb | 0:8918a71cdbe9 | 71 | |
Sergunb | 0:8918a71cdbe9 | 72 | //Encrypt the current block based upon the output |
Sergunb | 0:8918a71cdbe9 | 73 | //of the previous encryption |
Sergunb | 0:8918a71cdbe9 | 74 | cipher->encryptBlock(context, c, c); |
Sergunb | 0:8918a71cdbe9 | 75 | |
Sergunb | 0:8918a71cdbe9 | 76 | //Update IV with output block contents |
Sergunb | 0:8918a71cdbe9 | 77 | memcpy(iv, c, cipher->blockSize); |
Sergunb | 0:8918a71cdbe9 | 78 | |
Sergunb | 0:8918a71cdbe9 | 79 | //Next block |
Sergunb | 0:8918a71cdbe9 | 80 | p += cipher->blockSize; |
Sergunb | 0:8918a71cdbe9 | 81 | c += cipher->blockSize; |
Sergunb | 0:8918a71cdbe9 | 82 | length -= cipher->blockSize; |
Sergunb | 0:8918a71cdbe9 | 83 | } |
Sergunb | 0:8918a71cdbe9 | 84 | |
Sergunb | 0:8918a71cdbe9 | 85 | //The plaintext must be a multiple of the block size |
Sergunb | 0:8918a71cdbe9 | 86 | if(length != 0) |
Sergunb | 0:8918a71cdbe9 | 87 | return ERROR_INVALID_LENGTH; |
Sergunb | 0:8918a71cdbe9 | 88 | |
Sergunb | 0:8918a71cdbe9 | 89 | //Successful encryption |
Sergunb | 0:8918a71cdbe9 | 90 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 91 | } |
Sergunb | 0:8918a71cdbe9 | 92 | |
Sergunb | 0:8918a71cdbe9 | 93 | |
Sergunb | 0:8918a71cdbe9 | 94 | /** |
Sergunb | 0:8918a71cdbe9 | 95 | * @brief CBC decryption |
Sergunb | 0:8918a71cdbe9 | 96 | * @param[in] cipher Cipher algorithm |
Sergunb | 0:8918a71cdbe9 | 97 | * @param[in] context Cipher algorithm context |
Sergunb | 0:8918a71cdbe9 | 98 | * @param[in,out] iv Initialization vector |
Sergunb | 0:8918a71cdbe9 | 99 | * @param[in] c Ciphertext to be decrypted |
Sergunb | 0:8918a71cdbe9 | 100 | * @param[out] p Plaintext resulting from the decryption |
Sergunb | 0:8918a71cdbe9 | 101 | * @param[in] length Total number of data bytes to be decrypted |
Sergunb | 0:8918a71cdbe9 | 102 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 103 | **/ |
Sergunb | 0:8918a71cdbe9 | 104 | |
Sergunb | 0:8918a71cdbe9 | 105 | error_t cbcDecrypt(const CipherAlgo *cipher, void *context, |
Sergunb | 0:8918a71cdbe9 | 106 | uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length) |
Sergunb | 0:8918a71cdbe9 | 107 | { |
Sergunb | 0:8918a71cdbe9 | 108 | size_t i; |
Sergunb | 0:8918a71cdbe9 | 109 | uint8_t t[16]; |
Sergunb | 0:8918a71cdbe9 | 110 | |
Sergunb | 0:8918a71cdbe9 | 111 | //CBC mode operates in a block-by-block fashion |
Sergunb | 0:8918a71cdbe9 | 112 | while(length >= cipher->blockSize) |
Sergunb | 0:8918a71cdbe9 | 113 | { |
Sergunb | 0:8918a71cdbe9 | 114 | //Save input block |
Sergunb | 0:8918a71cdbe9 | 115 | memcpy(t, c, cipher->blockSize); |
Sergunb | 0:8918a71cdbe9 | 116 | |
Sergunb | 0:8918a71cdbe9 | 117 | //Decrypt the current block |
Sergunb | 0:8918a71cdbe9 | 118 | cipher->decryptBlock(context, c, p); |
Sergunb | 0:8918a71cdbe9 | 119 | |
Sergunb | 0:8918a71cdbe9 | 120 | //XOR output block with IV contents |
Sergunb | 0:8918a71cdbe9 | 121 | for(i = 0; i < cipher->blockSize; i++) |
Sergunb | 0:8918a71cdbe9 | 122 | p[i] ^= iv[i]; |
Sergunb | 0:8918a71cdbe9 | 123 | |
Sergunb | 0:8918a71cdbe9 | 124 | //Update IV with input block contents |
Sergunb | 0:8918a71cdbe9 | 125 | memcpy(iv, t, cipher->blockSize); |
Sergunb | 0:8918a71cdbe9 | 126 | |
Sergunb | 0:8918a71cdbe9 | 127 | //Next block |
Sergunb | 0:8918a71cdbe9 | 128 | c += cipher->blockSize; |
Sergunb | 0:8918a71cdbe9 | 129 | p += cipher->blockSize; |
Sergunb | 0:8918a71cdbe9 | 130 | length -= cipher->blockSize; |
Sergunb | 0:8918a71cdbe9 | 131 | } |
Sergunb | 0:8918a71cdbe9 | 132 | |
Sergunb | 0:8918a71cdbe9 | 133 | //The ciphertext must be a multiple of the block size |
Sergunb | 0:8918a71cdbe9 | 134 | if(length != 0) |
Sergunb | 0:8918a71cdbe9 | 135 | return ERROR_INVALID_LENGTH; |
Sergunb | 0:8918a71cdbe9 | 136 | |
Sergunb | 0:8918a71cdbe9 | 137 | //Successful encryption |
Sergunb | 0:8918a71cdbe9 | 138 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 139 | } |
Sergunb | 0:8918a71cdbe9 | 140 | |
Sergunb | 0:8918a71cdbe9 | 141 | #endif |
Sergunb | 0:8918a71cdbe9 | 142 |