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 md2.c
Sergunb 0:8918a71cdbe9 3 * @brief MD2 (Message-Digest Algorithm)
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 MD2 algorithm takes as input a message of arbitrary length and produces
Sergunb 0:8918a71cdbe9 28 * as output a 128-bit message digest of the input. Refer to RFC 1319
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 "md2.h"
Sergunb 0:8918a71cdbe9 41
Sergunb 0:8918a71cdbe9 42 //Check crypto library configuration
Sergunb 0:8918a71cdbe9 43 #if (MD2_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 44
Sergunb 0:8918a71cdbe9 45 //MD2 constants
Sergunb 0:8918a71cdbe9 46 static const uint8_t s[256] =
Sergunb 0:8918a71cdbe9 47 {
Sergunb 0:8918a71cdbe9 48 0x29, 0x2E, 0x43, 0xC9, 0xA2, 0xD8, 0x7C, 0x01, 0x3D, 0x36, 0x54, 0xA1, 0xEC, 0xF0, 0x06, 0x13,
Sergunb 0:8918a71cdbe9 49 0x62, 0xA7, 0x05, 0xF3, 0xC0, 0xC7, 0x73, 0x8C, 0x98, 0x93, 0x2B, 0xD9, 0xBC, 0x4C, 0x82, 0xCA,
Sergunb 0:8918a71cdbe9 50 0x1E, 0x9B, 0x57, 0x3C, 0xFD, 0xD4, 0xE0, 0x16, 0x67, 0x42, 0x6F, 0x18, 0x8A, 0x17, 0xE5, 0x12,
Sergunb 0:8918a71cdbe9 51 0xBE, 0x4E, 0xC4, 0xD6, 0xDA, 0x9E, 0xDE, 0x49, 0xA0, 0xFB, 0xF5, 0x8E, 0xBB, 0x2F, 0xEE, 0x7A,
Sergunb 0:8918a71cdbe9 52 0xA9, 0x68, 0x79, 0x91, 0x15, 0xB2, 0x07, 0x3F, 0x94, 0xC2, 0x10, 0x89, 0x0B, 0x22, 0x5F, 0x21,
Sergunb 0:8918a71cdbe9 53 0x80, 0x7F, 0x5D, 0x9A, 0x5A, 0x90, 0x32, 0x27, 0x35, 0x3E, 0xCC, 0xE7, 0xBF, 0xF7, 0x97, 0x03,
Sergunb 0:8918a71cdbe9 54 0xFF, 0x19, 0x30, 0xB3, 0x48, 0xA5, 0xB5, 0xD1, 0xD7, 0x5E, 0x92, 0x2A, 0xAC, 0x56, 0xAA, 0xC6,
Sergunb 0:8918a71cdbe9 55 0x4F, 0xB8, 0x38, 0xD2, 0x96, 0xA4, 0x7D, 0xB6, 0x76, 0xFC, 0x6B, 0xE2, 0x9C, 0x74, 0x04, 0xF1,
Sergunb 0:8918a71cdbe9 56 0x45, 0x9D, 0x70, 0x59, 0x64, 0x71, 0x87, 0x20, 0x86, 0x5B, 0xCF, 0x65, 0xE6, 0x2D, 0xA8, 0x02,
Sergunb 0:8918a71cdbe9 57 0x1B, 0x60, 0x25, 0xAD, 0xAE, 0xB0, 0xB9, 0xF6, 0x1C, 0x46, 0x61, 0x69, 0x34, 0x40, 0x7E, 0x0F,
Sergunb 0:8918a71cdbe9 58 0x55, 0x47, 0xA3, 0x23, 0xDD, 0x51, 0xAF, 0x3A, 0xC3, 0x5C, 0xF9, 0xCE, 0xBA, 0xC5, 0xEA, 0x26,
Sergunb 0:8918a71cdbe9 59 0x2C, 0x53, 0x0D, 0x6E, 0x85, 0x28, 0x84, 0x09, 0xD3, 0xDF, 0xCD, 0xF4, 0x41, 0x81, 0x4D, 0x52,
Sergunb 0:8918a71cdbe9 60 0x6A, 0xDC, 0x37, 0xC8, 0x6C, 0xC1, 0xAB, 0xFA, 0x24, 0xE1, 0x7B, 0x08, 0x0C, 0xBD, 0xB1, 0x4A,
Sergunb 0:8918a71cdbe9 61 0x78, 0x88, 0x95, 0x8B, 0xE3, 0x63, 0xE8, 0x6D, 0xE9, 0xCB, 0xD5, 0xFE, 0x3B, 0x00, 0x1D, 0x39,
Sergunb 0:8918a71cdbe9 62 0xF2, 0xEF, 0xB7, 0x0E, 0x66, 0x58, 0xD0, 0xE4, 0xA6, 0x77, 0x72, 0xF8, 0xEB, 0x75, 0x4B, 0x0A,
Sergunb 0:8918a71cdbe9 63 0x31, 0x44, 0x50, 0xB4, 0x8F, 0xED, 0x1F, 0x1A, 0xDB, 0x99, 0x8D, 0x33, 0x9F, 0x11, 0x83, 0x14,
Sergunb 0:8918a71cdbe9 64 };
Sergunb 0:8918a71cdbe9 65
Sergunb 0:8918a71cdbe9 66 //MD2 object identifier (1.2.840.113549.2.2)
Sergunb 0:8918a71cdbe9 67 static const uint8_t md2Oid[] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x02};
Sergunb 0:8918a71cdbe9 68
Sergunb 0:8918a71cdbe9 69 //Common interface for hash algorithms
Sergunb 0:8918a71cdbe9 70 const HashAlgo md2HashAlgo =
Sergunb 0:8918a71cdbe9 71 {
Sergunb 0:8918a71cdbe9 72 "MD2",
Sergunb 0:8918a71cdbe9 73 md2Oid,
Sergunb 0:8918a71cdbe9 74 sizeof(md2Oid),
Sergunb 0:8918a71cdbe9 75 sizeof(Md2Context),
Sergunb 0:8918a71cdbe9 76 MD2_BLOCK_SIZE,
Sergunb 0:8918a71cdbe9 77 MD2_DIGEST_SIZE,
Sergunb 0:8918a71cdbe9 78 (HashAlgoCompute) md2Compute,
Sergunb 0:8918a71cdbe9 79 (HashAlgoInit) md2Init,
Sergunb 0:8918a71cdbe9 80 (HashAlgoUpdate) md2Update,
Sergunb 0:8918a71cdbe9 81 (HashAlgoFinal) md2Final
Sergunb 0:8918a71cdbe9 82 };
Sergunb 0:8918a71cdbe9 83
Sergunb 0:8918a71cdbe9 84
Sergunb 0:8918a71cdbe9 85 /**
Sergunb 0:8918a71cdbe9 86 * @brief Digest a message using MD2
Sergunb 0:8918a71cdbe9 87 * @param[in] data Pointer to the message being hashed
Sergunb 0:8918a71cdbe9 88 * @param[in] length Length of the message
Sergunb 0:8918a71cdbe9 89 * @param[out] digest Pointer to the calculated digest
Sergunb 0:8918a71cdbe9 90 * @return Error code
Sergunb 0:8918a71cdbe9 91 **/
Sergunb 0:8918a71cdbe9 92
Sergunb 0:8918a71cdbe9 93 error_t md2Compute(const void *data, size_t length, uint8_t *digest)
Sergunb 0:8918a71cdbe9 94 {
Sergunb 0:8918a71cdbe9 95 //Allocate a memory buffer to hold the MD2 context
Sergunb 0:8918a71cdbe9 96 Md2Context *context = cryptoAllocMem(sizeof(Md2Context));
Sergunb 0:8918a71cdbe9 97 //Failed to allocate memory?
Sergunb 0:8918a71cdbe9 98 if(context == NULL)
Sergunb 0:8918a71cdbe9 99 return ERROR_OUT_OF_MEMORY;
Sergunb 0:8918a71cdbe9 100
Sergunb 0:8918a71cdbe9 101 //Initialize the MD2 context
Sergunb 0:8918a71cdbe9 102 md2Init(context);
Sergunb 0:8918a71cdbe9 103 //Digest the message
Sergunb 0:8918a71cdbe9 104 md2Update(context, data, length);
Sergunb 0:8918a71cdbe9 105 //Finalize the MD2 message digest
Sergunb 0:8918a71cdbe9 106 md2Final(context, digest);
Sergunb 0:8918a71cdbe9 107
Sergunb 0:8918a71cdbe9 108 //Free previously allocated memory
Sergunb 0:8918a71cdbe9 109 cryptoFreeMem(context);
Sergunb 0:8918a71cdbe9 110 //Successful processing
Sergunb 0:8918a71cdbe9 111 return NO_ERROR;
Sergunb 0:8918a71cdbe9 112 }
Sergunb 0:8918a71cdbe9 113
Sergunb 0:8918a71cdbe9 114
Sergunb 0:8918a71cdbe9 115 /**
Sergunb 0:8918a71cdbe9 116 * @brief Initialize MD2 message digest context
Sergunb 0:8918a71cdbe9 117 * @param[in] context Pointer to the MD2 context to initialize
Sergunb 0:8918a71cdbe9 118 **/
Sergunb 0:8918a71cdbe9 119
Sergunb 0:8918a71cdbe9 120 void md2Init(Md2Context *context)
Sergunb 0:8918a71cdbe9 121 {
Sergunb 0:8918a71cdbe9 122 //Initialize the 48-byte buffer X
Sergunb 0:8918a71cdbe9 123 memset(context->x, 0, 48);
Sergunb 0:8918a71cdbe9 124 //Clear checksum
Sergunb 0:8918a71cdbe9 125 memset(context->c, 0, 16);
Sergunb 0:8918a71cdbe9 126 //Number of bytes in the buffer
Sergunb 0:8918a71cdbe9 127 context->size = 0;
Sergunb 0:8918a71cdbe9 128 }
Sergunb 0:8918a71cdbe9 129
Sergunb 0:8918a71cdbe9 130
Sergunb 0:8918a71cdbe9 131 /**
Sergunb 0:8918a71cdbe9 132 * @brief Update the MD2 context with a portion of the message being hashed
Sergunb 0:8918a71cdbe9 133 * @param[in] context Pointer to the MD2 context
Sergunb 0:8918a71cdbe9 134 * @param[in] data Pointer to the buffer being hashed
Sergunb 0:8918a71cdbe9 135 * @param[in] length Length of the buffer
Sergunb 0:8918a71cdbe9 136 **/
Sergunb 0:8918a71cdbe9 137
Sergunb 0:8918a71cdbe9 138 void md2Update(Md2Context *context, const void *data, size_t length)
Sergunb 0:8918a71cdbe9 139 {
Sergunb 0:8918a71cdbe9 140 size_t n;
Sergunb 0:8918a71cdbe9 141
Sergunb 0:8918a71cdbe9 142 //Process the incoming data
Sergunb 0:8918a71cdbe9 143 while(length > 0)
Sergunb 0:8918a71cdbe9 144 {
Sergunb 0:8918a71cdbe9 145 //The buffer can hold at most 16 bytes
Sergunb 0:8918a71cdbe9 146 n = MIN(length, 16 - context->size);
Sergunb 0:8918a71cdbe9 147
Sergunb 0:8918a71cdbe9 148 //Copy the data to the buffer
Sergunb 0:8918a71cdbe9 149 memcpy(context->m + context->size, data, n);
Sergunb 0:8918a71cdbe9 150
Sergunb 0:8918a71cdbe9 151 //Update the MD2 context
Sergunb 0:8918a71cdbe9 152 context->size += n;
Sergunb 0:8918a71cdbe9 153 //Advance the data pointer
Sergunb 0:8918a71cdbe9 154 data = (uint8_t *) data + n;
Sergunb 0:8918a71cdbe9 155 //Remaining bytes to process
Sergunb 0:8918a71cdbe9 156 length -= n;
Sergunb 0:8918a71cdbe9 157
Sergunb 0:8918a71cdbe9 158 //Process message in 16-word blocks
Sergunb 0:8918a71cdbe9 159 if(context->size == 16)
Sergunb 0:8918a71cdbe9 160 {
Sergunb 0:8918a71cdbe9 161 //Transform the 16-word block
Sergunb 0:8918a71cdbe9 162 md2ProcessBlock(context->m, context->x, context->c);
Sergunb 0:8918a71cdbe9 163 //Empty the buffer
Sergunb 0:8918a71cdbe9 164 context->size = 0;
Sergunb 0:8918a71cdbe9 165 }
Sergunb 0:8918a71cdbe9 166 }
Sergunb 0:8918a71cdbe9 167 }
Sergunb 0:8918a71cdbe9 168
Sergunb 0:8918a71cdbe9 169
Sergunb 0:8918a71cdbe9 170 /**
Sergunb 0:8918a71cdbe9 171 * @brief Finish the MD2 message digest
Sergunb 0:8918a71cdbe9 172 * @param[in] context Pointer to the MD2 context
Sergunb 0:8918a71cdbe9 173 * @param[out] digest Calculated digest (optional parameter)
Sergunb 0:8918a71cdbe9 174 **/
Sergunb 0:8918a71cdbe9 175
Sergunb 0:8918a71cdbe9 176 void md2Final(Md2Context *context, uint8_t *digest)
Sergunb 0:8918a71cdbe9 177 {
Sergunb 0:8918a71cdbe9 178 uint_t n;
Sergunb 0:8918a71cdbe9 179
Sergunb 0:8918a71cdbe9 180 //Pad the message so that its length is congruent to 0 modulo 16
Sergunb 0:8918a71cdbe9 181 n = 16 - context->size;
Sergunb 0:8918a71cdbe9 182
Sergunb 0:8918a71cdbe9 183 //Append padding bytes
Sergunb 0:8918a71cdbe9 184 memset(context->m + context->size, n, n);
Sergunb 0:8918a71cdbe9 185 //Transform the 16-word block
Sergunb 0:8918a71cdbe9 186 md2ProcessBlock(context->m, context->x, context->c);
Sergunb 0:8918a71cdbe9 187
Sergunb 0:8918a71cdbe9 188 //Append the checksum
Sergunb 0:8918a71cdbe9 189 memcpy(context->m, context->c, 16);
Sergunb 0:8918a71cdbe9 190 //Transform the 16-word block
Sergunb 0:8918a71cdbe9 191 md2ProcessBlock(context->m, context->x, context->c);
Sergunb 0:8918a71cdbe9 192
Sergunb 0:8918a71cdbe9 193 //Copy the resulting digest
Sergunb 0:8918a71cdbe9 194 if(digest != NULL)
Sergunb 0:8918a71cdbe9 195 memcpy(digest, context->digest, MD2_DIGEST_SIZE);
Sergunb 0:8918a71cdbe9 196 }
Sergunb 0:8918a71cdbe9 197
Sergunb 0:8918a71cdbe9 198
Sergunb 0:8918a71cdbe9 199 /**
Sergunb 0:8918a71cdbe9 200 * @brief Process message in 16-word blocks
Sergunb 0:8918a71cdbe9 201 * @param[in] m 16-byte data block to process
Sergunb 0:8918a71cdbe9 202 * @param[in,out] x 48-byte buffer
Sergunb 0:8918a71cdbe9 203 * @param[in,out] c 16-byte checksum
Sergunb 0:8918a71cdbe9 204 **/
Sergunb 0:8918a71cdbe9 205
Sergunb 0:8918a71cdbe9 206 void md2ProcessBlock(const uint8_t *m, uint8_t *x, uint8_t *c)
Sergunb 0:8918a71cdbe9 207 {
Sergunb 0:8918a71cdbe9 208 uint_t j;
Sergunb 0:8918a71cdbe9 209 uint_t k;
Sergunb 0:8918a71cdbe9 210 uint8_t t;
Sergunb 0:8918a71cdbe9 211
Sergunb 0:8918a71cdbe9 212 //Update checksum
Sergunb 0:8918a71cdbe9 213 for(t = c[15], j = 0; j < 16; j++)
Sergunb 0:8918a71cdbe9 214 {
Sergunb 0:8918a71cdbe9 215 c[j] ^= s[m[j] ^ t];
Sergunb 0:8918a71cdbe9 216 t = c[j];
Sergunb 0:8918a71cdbe9 217 }
Sergunb 0:8918a71cdbe9 218
Sergunb 0:8918a71cdbe9 219 //Copy current block into X
Sergunb 0:8918a71cdbe9 220 for(j = 0; j < 16; j++)
Sergunb 0:8918a71cdbe9 221 {
Sergunb 0:8918a71cdbe9 222 x[16 + j] = m[j];
Sergunb 0:8918a71cdbe9 223 x[32 + j] = x[16 + j] ^ x[j];
Sergunb 0:8918a71cdbe9 224 }
Sergunb 0:8918a71cdbe9 225
Sergunb 0:8918a71cdbe9 226 //Encrypt block (18 rounds)
Sergunb 0:8918a71cdbe9 227 for(t = 0, j = 0; j < 18; j++)
Sergunb 0:8918a71cdbe9 228 {
Sergunb 0:8918a71cdbe9 229 //Round j
Sergunb 0:8918a71cdbe9 230 for(k = 0; k < 48; k++)
Sergunb 0:8918a71cdbe9 231 {
Sergunb 0:8918a71cdbe9 232 x[k] ^= s[t];
Sergunb 0:8918a71cdbe9 233 t = x[k];
Sergunb 0:8918a71cdbe9 234 }
Sergunb 0:8918a71cdbe9 235
Sergunb 0:8918a71cdbe9 236 //Set t to (t + j) modulo 256
Sergunb 0:8918a71cdbe9 237 t = (t + j) & 0xFF;
Sergunb 0:8918a71cdbe9 238 }
Sergunb 0:8918a71cdbe9 239 }
Sergunb 0:8918a71cdbe9 240
Sergunb 0:8918a71cdbe9 241 #endif
Sergunb 0:8918a71cdbe9 242