ARM Shanghai IoT Team (Internal) / newMiniTLS-GPL

Fork of MiniTLS-GPL by Donatien Garnier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers crypto_hmac_sha256.c Source File

crypto_hmac_sha256.c

Go to the documentation of this file.
00001 /*
00002 MiniTLS - A super trimmed down TLS/SSL Library for embedded devices
00003 Author: Donatien Garnier
00004 Copyright (C) 2013-2014 AppNearMe Ltd
00005 
00006 This program is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU General Public License
00008 as published by the Free Software Foundation; either version 2
00009 of the License, or (at your option) any later version.
00010 
00011 This program is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with this program; if not, write to the Free Software
00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019 *//**
00020  * \file crypto_hmac_sha256.c
00021  * \copyright Copyright (c) AppNearMe Ltd 2013
00022  * \author Donatien Garnier
00023  */
00024 
00025 #define __DEBUG__ 0
00026 #ifndef __MODULE__
00027 #define __MODULE__ "crypto_hmac_sha256.c"
00028 #endif
00029 
00030 #include "core/fwk.h"
00031 #include "inc/minitls_errors.h"
00032 
00033 #include "crypto_hmac_sha256.h"
00034 
00035 void crypto_hmac_sha256_init(crypto_hmac_sha256_t* mac, const uint8_t* key, size_t size) //FIXME add crypto_err_t
00036 {
00037   if(size > SHA256_BLOCK_SIZE)
00038   {
00039     //If key is longer than block size, hash the key first
00040     crypto_sha256_init(&mac->hash);
00041     crypto_sha256_update(&mac->hash, key, size);
00042     crypto_sha256_end(&mac->hash, mac->key_pad);
00043     size = SHA256_SIZE;
00044   }
00045   else
00046   {
00047     //Copy key
00048     memcpy(mac->key_pad, key, size);
00049   }
00050 
00051   //Pad key with 0s
00052   memset(mac->key_pad + size, 0, SHA256_BLOCK_SIZE - size);
00053 
00054   //XOR with 0x36363636...
00055   for( int i = 0; i < SHA256_BLOCK_SIZE / sizeof(uint32_t); i++)
00056   {
00057     //XOR by blocks
00058     *(((uint32_t*)mac->key_pad) + i) ^= 0x36363636UL;
00059   }
00060 
00061   //Init hash
00062   crypto_sha256_init(&mac->hash);
00063   crypto_sha256_update(&mac->hash, mac->key_pad, SHA256_BLOCK_SIZE);
00064 }
00065 
00066 void crypto_hmac_sha256_update(crypto_hmac_sha256_t* mac, const uint8_t* data, size_t size)
00067 {
00068   crypto_sha256_update(&mac->hash, data, size);
00069 }
00070 
00071 void crypto_hmac_sha256_end(crypto_hmac_sha256_t* mac, uint8_t* out)
00072 {
00073   //Complete hash
00074   uint8_t* hash1 = out; //Save memory
00075   crypto_sha256_end(&mac->hash, hash1);
00076 
00077   //Compute second key
00078   //XOR initial padded key with 0x5c5c5c5c...
00079   for( int i = 0; i < SHA256_BLOCK_SIZE / sizeof(uint32_t); i++)
00080   {
00081     //XOR by blocks
00082     *(((uint32_t*)mac->key_pad) + i) ^= (0x5c5c5c5cUL ^ 0x36363636UL);
00083   }
00084 
00085   //Compute final hash
00086   crypto_sha256_init(&mac->hash);
00087   crypto_sha256_update(&mac->hash, mac->key_pad, SHA256_BLOCK_SIZE);
00088   crypto_sha256_update(&mac->hash, hash1, SHA256_SIZE);
00089   crypto_sha256_end(&mac->hash, out);
00090 }
00091 
00092