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 sha256.c
Sergunb 0:8918a71cdbe9 3 * @brief SHA-256 (Secure Hash Algorithm 256)
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 * SHA-256 is a secure hash algorithm for computing a condensed representation
Sergunb 0:8918a71cdbe9 28 * of an electronic message. Refer to FIPS 180-4 for more details
Sergunb 0:8918a71cdbe9 29 *
Sergunb 0:8918a71cdbe9 30 * @author Oryx Embedded SARL (www.oryx-embedded.com)
Sergunb 0:8918a71cdbe9 31 * @version 1.7.6
Sergunb 0:8918a71cdbe9 32 **/
Sergunb 0:8918a71cdbe9 33
Sergunb 0:8918a71cdbe9 34 //Switch to the appropriate trace level
Sergunb 0:8918a71cdbe9 35 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
Sergunb 0:8918a71cdbe9 36
Sergunb 0:8918a71cdbe9 37 //Dependencies
Sergunb 0:8918a71cdbe9 38 #include <string.h>
Sergunb 0:8918a71cdbe9 39 #include "crypto.h"
Sergunb 0:8918a71cdbe9 40 #include "sha256.h"
Sergunb 0:8918a71cdbe9 41
Sergunb 0:8918a71cdbe9 42 //Check crypto library configuration
Sergunb 0:8918a71cdbe9 43 #if (SHA224_SUPPORT == ENABLED || SHA256_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 44
Sergunb 0:8918a71cdbe9 45 //Macro to access the workspace as a circular buffer
Sergunb 0:8918a71cdbe9 46 #define W(t) w[(t) & 0x0F]
Sergunb 0:8918a71cdbe9 47
Sergunb 0:8918a71cdbe9 48 //SHA-256 auxiliary functions
Sergunb 0:8918a71cdbe9 49 #define CH(x, y, z) (((x) & (y)) | (~(x) & (z)))
Sergunb 0:8918a71cdbe9 50 #define MAJ(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
Sergunb 0:8918a71cdbe9 51 #define SIGMA1(x) (ROR32(x, 2) ^ ROR32(x, 13) ^ ROR32(x, 22))
Sergunb 0:8918a71cdbe9 52 #define SIGMA2(x) (ROR32(x, 6) ^ ROR32(x, 11) ^ ROR32(x, 25))
Sergunb 0:8918a71cdbe9 53 #define SIGMA3(x) (ROR32(x, 7) ^ ROR32(x, 18) ^ SHR32(x, 3))
Sergunb 0:8918a71cdbe9 54 #define SIGMA4(x) (ROR32(x, 17) ^ ROR32(x, 19) ^ SHR32(x, 10))
Sergunb 0:8918a71cdbe9 55
Sergunb 0:8918a71cdbe9 56 //SHA-256 padding
Sergunb 0:8918a71cdbe9 57 static const uint8_t padding[64] =
Sergunb 0:8918a71cdbe9 58 {
Sergunb 0:8918a71cdbe9 59 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Sergunb 0:8918a71cdbe9 60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Sergunb 0:8918a71cdbe9 61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Sergunb 0:8918a71cdbe9 62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
Sergunb 0:8918a71cdbe9 63 };
Sergunb 0:8918a71cdbe9 64
Sergunb 0:8918a71cdbe9 65 //SHA-256 constants
Sergunb 0:8918a71cdbe9 66 static const uint32_t k[64] =
Sergunb 0:8918a71cdbe9 67 {
Sergunb 0:8918a71cdbe9 68 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
Sergunb 0:8918a71cdbe9 69 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
Sergunb 0:8918a71cdbe9 70 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
Sergunb 0:8918a71cdbe9 71 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
Sergunb 0:8918a71cdbe9 72 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
Sergunb 0:8918a71cdbe9 73 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
Sergunb 0:8918a71cdbe9 74 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
Sergunb 0:8918a71cdbe9 75 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
Sergunb 0:8918a71cdbe9 76 };
Sergunb 0:8918a71cdbe9 77
Sergunb 0:8918a71cdbe9 78 //SHA-256 object identifier (2.16.840.1.101.3.4.2.1)
Sergunb 0:8918a71cdbe9 79 static const uint8_t sha256Oid[] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01};
Sergunb 0:8918a71cdbe9 80
Sergunb 0:8918a71cdbe9 81 //Common interface for hash algorithms
Sergunb 0:8918a71cdbe9 82 const HashAlgo sha256HashAlgo =
Sergunb 0:8918a71cdbe9 83 {
Sergunb 0:8918a71cdbe9 84 "SHA-256",
Sergunb 0:8918a71cdbe9 85 sha256Oid,
Sergunb 0:8918a71cdbe9 86 sizeof(sha256Oid),
Sergunb 0:8918a71cdbe9 87 sizeof(Sha256Context),
Sergunb 0:8918a71cdbe9 88 SHA256_BLOCK_SIZE,
Sergunb 0:8918a71cdbe9 89 SHA256_DIGEST_SIZE,
Sergunb 0:8918a71cdbe9 90 (HashAlgoCompute) sha256Compute,
Sergunb 0:8918a71cdbe9 91 (HashAlgoInit) sha256Init,
Sergunb 0:8918a71cdbe9 92 (HashAlgoUpdate) sha256Update,
Sergunb 0:8918a71cdbe9 93 (HashAlgoFinal) sha256Final
Sergunb 0:8918a71cdbe9 94 };
Sergunb 0:8918a71cdbe9 95
Sergunb 0:8918a71cdbe9 96
Sergunb 0:8918a71cdbe9 97 /**
Sergunb 0:8918a71cdbe9 98 * @brief Digest a message using SHA-256
Sergunb 0:8918a71cdbe9 99 * @param[in] data Pointer to the message being hashed
Sergunb 0:8918a71cdbe9 100 * @param[in] length Length of the message
Sergunb 0:8918a71cdbe9 101 * @param[out] digest Pointer to the calculated digest
Sergunb 0:8918a71cdbe9 102 * @return Error code
Sergunb 0:8918a71cdbe9 103 **/
Sergunb 0:8918a71cdbe9 104
Sergunb 0:8918a71cdbe9 105 error_t sha256Compute(const void *data, size_t length, uint8_t *digest)
Sergunb 0:8918a71cdbe9 106 {
Sergunb 0:8918a71cdbe9 107 //Allocate a memory buffer to hold the SHA-256 context
Sergunb 0:8918a71cdbe9 108 Sha256Context *context = cryptoAllocMem(sizeof(Sha256Context));
Sergunb 0:8918a71cdbe9 109 //Failed to allocate memory?
Sergunb 0:8918a71cdbe9 110 if(context == NULL)
Sergunb 0:8918a71cdbe9 111 return ERROR_OUT_OF_MEMORY;
Sergunb 0:8918a71cdbe9 112
Sergunb 0:8918a71cdbe9 113 //Initialize the SHA-256 context
Sergunb 0:8918a71cdbe9 114 sha256Init(context);
Sergunb 0:8918a71cdbe9 115 //Digest the message
Sergunb 0:8918a71cdbe9 116 sha256Update(context, data, length);
Sergunb 0:8918a71cdbe9 117 //Finalize the SHA-256 message digest
Sergunb 0:8918a71cdbe9 118 sha256Final(context, digest);
Sergunb 0:8918a71cdbe9 119
Sergunb 0:8918a71cdbe9 120 //Free previously allocated memory
Sergunb 0:8918a71cdbe9 121 cryptoFreeMem(context);
Sergunb 0:8918a71cdbe9 122 //Successful processing
Sergunb 0:8918a71cdbe9 123 return NO_ERROR;
Sergunb 0:8918a71cdbe9 124 }
Sergunb 0:8918a71cdbe9 125
Sergunb 0:8918a71cdbe9 126
Sergunb 0:8918a71cdbe9 127 /**
Sergunb 0:8918a71cdbe9 128 * @brief Initialize SHA-256 message digest context
Sergunb 0:8918a71cdbe9 129 * @param[in] context Pointer to the SHA-256 context to initialize
Sergunb 0:8918a71cdbe9 130 **/
Sergunb 0:8918a71cdbe9 131
Sergunb 0:8918a71cdbe9 132 void sha256Init(Sha256Context *context)
Sergunb 0:8918a71cdbe9 133 {
Sergunb 0:8918a71cdbe9 134 //Set initial hash value
Sergunb 0:8918a71cdbe9 135 context->h[0] = 0x6A09E667;
Sergunb 0:8918a71cdbe9 136 context->h[1] = 0xBB67AE85;
Sergunb 0:8918a71cdbe9 137 context->h[2] = 0x3C6EF372;
Sergunb 0:8918a71cdbe9 138 context->h[3] = 0xA54FF53A;
Sergunb 0:8918a71cdbe9 139 context->h[4] = 0x510E527F;
Sergunb 0:8918a71cdbe9 140 context->h[5] = 0x9B05688C;
Sergunb 0:8918a71cdbe9 141 context->h[6] = 0x1F83D9AB;
Sergunb 0:8918a71cdbe9 142 context->h[7] = 0x5BE0CD19;
Sergunb 0:8918a71cdbe9 143
Sergunb 0:8918a71cdbe9 144 //Number of bytes in the buffer
Sergunb 0:8918a71cdbe9 145 context->size = 0;
Sergunb 0:8918a71cdbe9 146 //Total length of the message
Sergunb 0:8918a71cdbe9 147 context->totalSize = 0;
Sergunb 0:8918a71cdbe9 148 }
Sergunb 0:8918a71cdbe9 149
Sergunb 0:8918a71cdbe9 150
Sergunb 0:8918a71cdbe9 151 /**
Sergunb 0:8918a71cdbe9 152 * @brief Update the SHA-256 context with a portion of the message being hashed
Sergunb 0:8918a71cdbe9 153 * @param[in] context Pointer to the SHA-256 context
Sergunb 0:8918a71cdbe9 154 * @param[in] data Pointer to the buffer being hashed
Sergunb 0:8918a71cdbe9 155 * @param[in] length Length of the buffer
Sergunb 0:8918a71cdbe9 156 **/
Sergunb 0:8918a71cdbe9 157
Sergunb 0:8918a71cdbe9 158 void sha256Update(Sha256Context *context, const void *data, size_t length)
Sergunb 0:8918a71cdbe9 159 {
Sergunb 0:8918a71cdbe9 160 size_t n;
Sergunb 0:8918a71cdbe9 161
Sergunb 0:8918a71cdbe9 162 //Process the incoming data
Sergunb 0:8918a71cdbe9 163 while(length > 0)
Sergunb 0:8918a71cdbe9 164 {
Sergunb 0:8918a71cdbe9 165 //The buffer can hold at most 64 bytes
Sergunb 0:8918a71cdbe9 166 n = MIN(length, 64 - context->size);
Sergunb 0:8918a71cdbe9 167
Sergunb 0:8918a71cdbe9 168 //Copy the data to the buffer
Sergunb 0:8918a71cdbe9 169 memcpy(context->buffer + context->size, data, n);
Sergunb 0:8918a71cdbe9 170
Sergunb 0:8918a71cdbe9 171 //Update the SHA-256 context
Sergunb 0:8918a71cdbe9 172 context->size += n;
Sergunb 0:8918a71cdbe9 173 context->totalSize += n;
Sergunb 0:8918a71cdbe9 174 //Advance the data pointer
Sergunb 0:8918a71cdbe9 175 data = (uint8_t *) data + n;
Sergunb 0:8918a71cdbe9 176 //Remaining bytes to process
Sergunb 0:8918a71cdbe9 177 length -= n;
Sergunb 0:8918a71cdbe9 178
Sergunb 0:8918a71cdbe9 179 //Process message in 16-word blocks
Sergunb 0:8918a71cdbe9 180 if(context->size == 64)
Sergunb 0:8918a71cdbe9 181 {
Sergunb 0:8918a71cdbe9 182 //Transform the 16-word block
Sergunb 0:8918a71cdbe9 183 sha256ProcessBlock(context);
Sergunb 0:8918a71cdbe9 184 //Empty the buffer
Sergunb 0:8918a71cdbe9 185 context->size = 0;
Sergunb 0:8918a71cdbe9 186 }
Sergunb 0:8918a71cdbe9 187 }
Sergunb 0:8918a71cdbe9 188 }
Sergunb 0:8918a71cdbe9 189
Sergunb 0:8918a71cdbe9 190
Sergunb 0:8918a71cdbe9 191 /**
Sergunb 0:8918a71cdbe9 192 * @brief Finish the SHA-256 message digest
Sergunb 0:8918a71cdbe9 193 * @param[in] context Pointer to the SHA-256 context
Sergunb 0:8918a71cdbe9 194 * @param[out] digest Calculated digest (optional parameter)
Sergunb 0:8918a71cdbe9 195 **/
Sergunb 0:8918a71cdbe9 196
Sergunb 0:8918a71cdbe9 197 void sha256Final(Sha256Context *context, uint8_t *digest)
Sergunb 0:8918a71cdbe9 198 {
Sergunb 0:8918a71cdbe9 199 uint_t i;
Sergunb 0:8918a71cdbe9 200 size_t paddingSize;
Sergunb 0:8918a71cdbe9 201 uint64_t totalSize;
Sergunb 0:8918a71cdbe9 202
Sergunb 0:8918a71cdbe9 203 //Length of the original message (before padding)
Sergunb 0:8918a71cdbe9 204 totalSize = context->totalSize * 8;
Sergunb 0:8918a71cdbe9 205
Sergunb 0:8918a71cdbe9 206 //Pad the message so that its length is congruent to 56 modulo 64
Sergunb 0:8918a71cdbe9 207 if(context->size < 56)
Sergunb 0:8918a71cdbe9 208 paddingSize = 56 - context->size;
Sergunb 0:8918a71cdbe9 209 else
Sergunb 0:8918a71cdbe9 210 paddingSize = 64 + 56 - context->size;
Sergunb 0:8918a71cdbe9 211
Sergunb 0:8918a71cdbe9 212 //Append padding
Sergunb 0:8918a71cdbe9 213 sha256Update(context, padding, paddingSize);
Sergunb 0:8918a71cdbe9 214
Sergunb 0:8918a71cdbe9 215 //Append the length of the original message
Sergunb 0:8918a71cdbe9 216 context->w[14] = htobe32((uint32_t) (totalSize >> 32));
Sergunb 0:8918a71cdbe9 217 context->w[15] = htobe32((uint32_t) totalSize);
Sergunb 0:8918a71cdbe9 218
Sergunb 0:8918a71cdbe9 219 //Calculate the message digest
Sergunb 0:8918a71cdbe9 220 sha256ProcessBlock(context);
Sergunb 0:8918a71cdbe9 221
Sergunb 0:8918a71cdbe9 222 //Convert from host byte order to big-endian byte order
Sergunb 0:8918a71cdbe9 223 for(i = 0; i < 8; i++)
Sergunb 0:8918a71cdbe9 224 context->h[i] = htobe32(context->h[i]);
Sergunb 0:8918a71cdbe9 225
Sergunb 0:8918a71cdbe9 226 //Copy the resulting digest
Sergunb 0:8918a71cdbe9 227 if(digest != NULL)
Sergunb 0:8918a71cdbe9 228 memcpy(digest, context->digest, SHA256_DIGEST_SIZE);
Sergunb 0:8918a71cdbe9 229 }
Sergunb 0:8918a71cdbe9 230
Sergunb 0:8918a71cdbe9 231
Sergunb 0:8918a71cdbe9 232 /**
Sergunb 0:8918a71cdbe9 233 * @brief Process message in 16-word blocks
Sergunb 0:8918a71cdbe9 234 * @param[in] context Pointer to the SHA-256 context
Sergunb 0:8918a71cdbe9 235 **/
Sergunb 0:8918a71cdbe9 236
Sergunb 0:8918a71cdbe9 237 void sha256ProcessBlock(Sha256Context *context)
Sergunb 0:8918a71cdbe9 238 {
Sergunb 0:8918a71cdbe9 239 uint_t t;
Sergunb 0:8918a71cdbe9 240 uint32_t temp1;
Sergunb 0:8918a71cdbe9 241 uint32_t temp2;
Sergunb 0:8918a71cdbe9 242
Sergunb 0:8918a71cdbe9 243 //Initialize the 8 working registers
Sergunb 0:8918a71cdbe9 244 uint32_t a = context->h[0];
Sergunb 0:8918a71cdbe9 245 uint32_t b = context->h[1];
Sergunb 0:8918a71cdbe9 246 uint32_t c = context->h[2];
Sergunb 0:8918a71cdbe9 247 uint32_t d = context->h[3];
Sergunb 0:8918a71cdbe9 248 uint32_t e = context->h[4];
Sergunb 0:8918a71cdbe9 249 uint32_t f = context->h[5];
Sergunb 0:8918a71cdbe9 250 uint32_t g = context->h[6];
Sergunb 0:8918a71cdbe9 251 uint32_t h = context->h[7];
Sergunb 0:8918a71cdbe9 252
Sergunb 0:8918a71cdbe9 253 //Process message in 16-word blocks
Sergunb 0:8918a71cdbe9 254 uint32_t *w = context->w;
Sergunb 0:8918a71cdbe9 255
Sergunb 0:8918a71cdbe9 256 //Convert from big-endian byte order to host byte order
Sergunb 0:8918a71cdbe9 257 for(t = 0; t < 16; t++)
Sergunb 0:8918a71cdbe9 258 w[t] = betoh32(w[t]);
Sergunb 0:8918a71cdbe9 259
Sergunb 0:8918a71cdbe9 260 //SHA-256 hash computation (alternate method)
Sergunb 0:8918a71cdbe9 261 for(t = 0; t < 64; t++)
Sergunb 0:8918a71cdbe9 262 {
Sergunb 0:8918a71cdbe9 263 //Prepare the message schedule
Sergunb 0:8918a71cdbe9 264 if(t >= 16)
Sergunb 0:8918a71cdbe9 265 W(t) += SIGMA4(W(t + 14)) + W(t + 9) + SIGMA3(W(t + 1));
Sergunb 0:8918a71cdbe9 266
Sergunb 0:8918a71cdbe9 267 //Calculate T1 and T2
Sergunb 0:8918a71cdbe9 268 temp1 = h + SIGMA2(e) + CH(e, f, g) + k[t] + W(t);
Sergunb 0:8918a71cdbe9 269 temp2 = SIGMA1(a) + MAJ(a, b, c);
Sergunb 0:8918a71cdbe9 270
Sergunb 0:8918a71cdbe9 271 //Update the working registers
Sergunb 0:8918a71cdbe9 272 h = g;
Sergunb 0:8918a71cdbe9 273 g = f;
Sergunb 0:8918a71cdbe9 274 f = e;
Sergunb 0:8918a71cdbe9 275 e = d + temp1;
Sergunb 0:8918a71cdbe9 276 d = c;
Sergunb 0:8918a71cdbe9 277 c = b;
Sergunb 0:8918a71cdbe9 278 b = a;
Sergunb 0:8918a71cdbe9 279 a = temp1 + temp2;
Sergunb 0:8918a71cdbe9 280 }
Sergunb 0:8918a71cdbe9 281
Sergunb 0:8918a71cdbe9 282 //Update the hash value
Sergunb 0:8918a71cdbe9 283 context->h[0] += a;
Sergunb 0:8918a71cdbe9 284 context->h[1] += b;
Sergunb 0:8918a71cdbe9 285 context->h[2] += c;
Sergunb 0:8918a71cdbe9 286 context->h[3] += d;
Sergunb 0:8918a71cdbe9 287 context->h[4] += e;
Sergunb 0:8918a71cdbe9 288 context->h[5] += f;
Sergunb 0:8918a71cdbe9 289 context->h[6] += g;
Sergunb 0:8918a71cdbe9 290 context->h[7] += h;
Sergunb 0:8918a71cdbe9 291 }
Sergunb 0:8918a71cdbe9 292
Sergunb 0:8918a71cdbe9 293 #endif
Sergunb 0:8918a71cdbe9 294