Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: MiniTLS-HTTPS-Example
crypto_hmac_sha1.c
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_sha1.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_sha1.c" 00028 #endif 00029 00030 #include "core/fwk.h" 00031 #include "inc/minitls_errors.h" 00032 00033 #include "crypto_hmac_sha1.h" 00034 00035 void crypto_hmac_sha1_init(crypto_hmac_sha1_t* mac, const uint8_t* key, size_t size) //FIXME add crypto_err_t 00036 { 00037 if(size > SHA1_BLOCK_SIZE) 00038 { 00039 //If key is longer than block size, hash the key first 00040 crypto_sha1_init(&mac->hash); 00041 crypto_sha1_update(&mac->hash, key, size); 00042 crypto_sha1_end(&mac->hash, mac->key_pad); 00043 size = SHA1_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, SHA1_BLOCK_SIZE - size); 00053 00054 //XOR with 0x36363636... 00055 for( int i = 0; i < SHA1_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_sha1_init(&mac->hash); 00063 crypto_sha1_update(&mac->hash, mac->key_pad, SHA1_BLOCK_SIZE); 00064 } 00065 00066 void crypto_hmac_sha1_update(crypto_hmac_sha1_t* mac, const uint8_t* data, size_t size) 00067 { 00068 crypto_sha1_update(&mac->hash, data, size); 00069 } 00070 00071 void crypto_hmac_sha1_end(crypto_hmac_sha1_t* mac, uint8_t* out) 00072 { 00073 //Complete hash 00074 uint8_t* hash1 = out; //Save memory 00075 crypto_sha1_end(&mac->hash, hash1); 00076 00077 //Compute second key 00078 //XOR initial padded key with 0x5c5c5c5c... 00079 for( int i = 0; i < SHA1_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_sha1_init(&mac->hash); 00087 crypto_sha1_update(&mac->hash, mac->key_pad, SHA1_BLOCK_SIZE); 00088 crypto_sha1_update(&mac->hash, hash1, SHA1_SIZE); 00089 crypto_sha1_end(&mac->hash, out); 00090 } 00091 00092
Generated on Wed Jul 13 2022 00:22:54 by
1.7.2