Webserver+3d print

Dependents:   Nucleo

Committer:
Sergunb
Date:
Sat Feb 04 18:15:49 2017 +0000
Revision:
0:8918a71cdbe9
nothing else

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sergunb 0:8918a71cdbe9 1 /**
Sergunb 0:8918a71cdbe9 2 * @file cipher_mode_cfb.c
Sergunb 0:8918a71cdbe9 3 * @brief Cipher Feedback (CFB) 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 Feedback (CFB) mode is a confidentiality mode that features the
Sergunb 0:8918a71cdbe9 28 * feedback of successive ciphertext segments into the input blocks of the
Sergunb 0:8918a71cdbe9 29 * forward cipher to generate output blocks that are exclusive-ORed with the
Sergunb 0:8918a71cdbe9 30 * plaintext to produce the ciphertext, and vice versa. The CFB mode requires
Sergunb 0:8918a71cdbe9 31 * an IV as the initial input block. The IV need not be secret, but it must be
Sergunb 0:8918a71cdbe9 32 * unpredictable. Refer to SP 800-38A for more details
Sergunb 0:8918a71cdbe9 33 *
Sergunb 0:8918a71cdbe9 34 * @author Oryx Embedded SARL (www.oryx-embedded.com)
Sergunb 0:8918a71cdbe9 35 * @version 1.7.6
Sergunb 0:8918a71cdbe9 36 **/
Sergunb 0:8918a71cdbe9 37
Sergunb 0:8918a71cdbe9 38 //Switch to the appropriate trace level
Sergunb 0:8918a71cdbe9 39 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
Sergunb 0:8918a71cdbe9 40
Sergunb 0:8918a71cdbe9 41 //Dependencies
Sergunb 0:8918a71cdbe9 42 #include <string.h>
Sergunb 0:8918a71cdbe9 43 #include "crypto.h"
Sergunb 0:8918a71cdbe9 44 #include "cipher_mode_cfb.h"
Sergunb 0:8918a71cdbe9 45 #include "debug.h"
Sergunb 0:8918a71cdbe9 46
Sergunb 0:8918a71cdbe9 47 //Check crypto library configuration
Sergunb 0:8918a71cdbe9 48 #if (CFB_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 49
Sergunb 0:8918a71cdbe9 50
Sergunb 0:8918a71cdbe9 51 /**
Sergunb 0:8918a71cdbe9 52 * @brief CFB encryption
Sergunb 0:8918a71cdbe9 53 * @param[in] cipher Cipher algorithm
Sergunb 0:8918a71cdbe9 54 * @param[in] context Cipher algorithm context
Sergunb 0:8918a71cdbe9 55 * @param[in] s Size of the plaintext and ciphertext segments
Sergunb 0:8918a71cdbe9 56 * @param[in,out] iv Initialization vector
Sergunb 0:8918a71cdbe9 57 * @param[in] p Plaintext to be encrypted
Sergunb 0:8918a71cdbe9 58 * @param[out] c Ciphertext resulting from the encryption
Sergunb 0:8918a71cdbe9 59 * @param[in] length Total number of data bytes to be encrypted
Sergunb 0:8918a71cdbe9 60 * @return Error code
Sergunb 0:8918a71cdbe9 61 **/
Sergunb 0:8918a71cdbe9 62
Sergunb 0:8918a71cdbe9 63 error_t cfbEncrypt(const CipherAlgo *cipher, void *context, uint_t s,
Sergunb 0:8918a71cdbe9 64 uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
Sergunb 0:8918a71cdbe9 65 {
Sergunb 0:8918a71cdbe9 66 size_t i;
Sergunb 0:8918a71cdbe9 67 size_t n;
Sergunb 0:8918a71cdbe9 68 uint8_t o[16];
Sergunb 0:8918a71cdbe9 69
Sergunb 0:8918a71cdbe9 70 //The parameter must be a multiple of 8
Sergunb 0:8918a71cdbe9 71 if(s % 8)
Sergunb 0:8918a71cdbe9 72 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 73
Sergunb 0:8918a71cdbe9 74 //Determine the size, in bytes, of the plaintext and ciphertext segments
Sergunb 0:8918a71cdbe9 75 s = s / 8;
Sergunb 0:8918a71cdbe9 76
Sergunb 0:8918a71cdbe9 77 //Check the resulting value
Sergunb 0:8918a71cdbe9 78 if(s < 1 || s > cipher->blockSize)
Sergunb 0:8918a71cdbe9 79 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 80
Sergunb 0:8918a71cdbe9 81 //Process each plaintext segment
Sergunb 0:8918a71cdbe9 82 while(length > 0)
Sergunb 0:8918a71cdbe9 83 {
Sergunb 0:8918a71cdbe9 84 //Compute the number of bytes to process at a time
Sergunb 0:8918a71cdbe9 85 n = MIN(length, s);
Sergunb 0:8918a71cdbe9 86
Sergunb 0:8918a71cdbe9 87 //Compute O(j) = CIPH(I(j))
Sergunb 0:8918a71cdbe9 88 cipher->encryptBlock(context, iv, o);
Sergunb 0:8918a71cdbe9 89
Sergunb 0:8918a71cdbe9 90 //Compute C(j) = P(j) XOR MSB(O(j))
Sergunb 0:8918a71cdbe9 91 for(i = 0; i < n; i++)
Sergunb 0:8918a71cdbe9 92 c[i] = p[i] ^ o[i];
Sergunb 0:8918a71cdbe9 93
Sergunb 0:8918a71cdbe9 94 //Compute I(j+1) = LSB(I(j)) | C(j)
Sergunb 0:8918a71cdbe9 95 memmove(iv, iv + s, cipher->blockSize - s);
Sergunb 0:8918a71cdbe9 96 memcpy(iv + cipher->blockSize - s, c, s);
Sergunb 0:8918a71cdbe9 97
Sergunb 0:8918a71cdbe9 98 //Next block
Sergunb 0:8918a71cdbe9 99 p += n;
Sergunb 0:8918a71cdbe9 100 c += n;
Sergunb 0:8918a71cdbe9 101 length -= n;
Sergunb 0:8918a71cdbe9 102 }
Sergunb 0:8918a71cdbe9 103
Sergunb 0:8918a71cdbe9 104 //Successful encryption
Sergunb 0:8918a71cdbe9 105 return NO_ERROR;
Sergunb 0:8918a71cdbe9 106 }
Sergunb 0:8918a71cdbe9 107
Sergunb 0:8918a71cdbe9 108
Sergunb 0:8918a71cdbe9 109 /**
Sergunb 0:8918a71cdbe9 110 * @brief CFB decryption
Sergunb 0:8918a71cdbe9 111 * @param[in] cipher Cipher algorithm
Sergunb 0:8918a71cdbe9 112 * @param[in] context Cipher algorithm context
Sergunb 0:8918a71cdbe9 113 * @param[in] s Size of the plaintext and ciphertext segments
Sergunb 0:8918a71cdbe9 114 * @param[in,out] iv Initialization vector
Sergunb 0:8918a71cdbe9 115 * @param[in] c Ciphertext to be decrypted
Sergunb 0:8918a71cdbe9 116 * @param[out] p Plaintext resulting from the decryption
Sergunb 0:8918a71cdbe9 117 * @param[in] length Total number of data bytes to be decrypted
Sergunb 0:8918a71cdbe9 118 * @return Error code
Sergunb 0:8918a71cdbe9 119 **/
Sergunb 0:8918a71cdbe9 120
Sergunb 0:8918a71cdbe9 121 error_t cfbDecrypt(const CipherAlgo *cipher, void *context, uint_t s,
Sergunb 0:8918a71cdbe9 122 uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
Sergunb 0:8918a71cdbe9 123 {
Sergunb 0:8918a71cdbe9 124 size_t i;
Sergunb 0:8918a71cdbe9 125 size_t n;
Sergunb 0:8918a71cdbe9 126 uint8_t o[16];
Sergunb 0:8918a71cdbe9 127
Sergunb 0:8918a71cdbe9 128 //The parameter must be a multiple of 8
Sergunb 0:8918a71cdbe9 129 if(s % 8)
Sergunb 0:8918a71cdbe9 130 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 131
Sergunb 0:8918a71cdbe9 132 //Determine the size, in bytes, of the plaintext and ciphertext segments
Sergunb 0:8918a71cdbe9 133 s = s / 8;
Sergunb 0:8918a71cdbe9 134
Sergunb 0:8918a71cdbe9 135 //Check the resulting value
Sergunb 0:8918a71cdbe9 136 if(s < 1 || s > cipher->blockSize)
Sergunb 0:8918a71cdbe9 137 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 138
Sergunb 0:8918a71cdbe9 139 //Process each ciphertext segment
Sergunb 0:8918a71cdbe9 140 while(length > 0)
Sergunb 0:8918a71cdbe9 141 {
Sergunb 0:8918a71cdbe9 142 //Compute the number of bytes to process at a time
Sergunb 0:8918a71cdbe9 143 n = MIN(length, s);
Sergunb 0:8918a71cdbe9 144
Sergunb 0:8918a71cdbe9 145 //Compute O(j) = CIPH(I(j))
Sergunb 0:8918a71cdbe9 146 cipher->encryptBlock(context, iv, o);
Sergunb 0:8918a71cdbe9 147
Sergunb 0:8918a71cdbe9 148 //Compute I(j+1) = LSB(I(j)) | C(j)
Sergunb 0:8918a71cdbe9 149 memmove(iv, iv + s, cipher->blockSize - s);
Sergunb 0:8918a71cdbe9 150 memcpy(iv + cipher->blockSize - s, c, s);
Sergunb 0:8918a71cdbe9 151
Sergunb 0:8918a71cdbe9 152 //Compute P(j) = C(j) XOR MSB(O(j))
Sergunb 0:8918a71cdbe9 153 for(i = 0; i < n; i++)
Sergunb 0:8918a71cdbe9 154 p[i] = c[i] ^ o[i];
Sergunb 0:8918a71cdbe9 155
Sergunb 0:8918a71cdbe9 156 //Next block
Sergunb 0:8918a71cdbe9 157 c += n;
Sergunb 0:8918a71cdbe9 158 p += n;
Sergunb 0:8918a71cdbe9 159 length -= n;
Sergunb 0:8918a71cdbe9 160 }
Sergunb 0:8918a71cdbe9 161
Sergunb 0:8918a71cdbe9 162 //Successful encryption
Sergunb 0:8918a71cdbe9 163 return NO_ERROR;
Sergunb 0:8918a71cdbe9 164 }
Sergunb 0:8918a71cdbe9 165
Sergunb 0:8918a71cdbe9 166 #endif
Sergunb 0:8918a71cdbe9 167