This is a fork of the mbed port of axTLS

Dependents:   TLS_axTLS-Example HTTPSClientExample

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hmac.c Source File

hmac.c

00001 /*
00002  * Copyright (c) 2007, Cameron Rich
00003  * 
00004  * All rights reserved.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without 
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  * * Redistributions of source code must retain the above copyright notice, 
00010  *   this list of conditions and the following disclaimer.
00011  * * Redistributions in binary form must reproduce the above copyright notice, 
00012  *   this list of conditions and the following disclaimer in the documentation 
00013  *   and/or other materials provided with the distribution.
00014  * * Neither the name of the axTLS project nor the names of its contributors 
00015  *   may be used to endorse or promote products derived from this software 
00016  *   without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00022  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00025  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00026  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  */
00030 
00031 /**
00032  * HMAC implementation - This code was originally taken from RFC2104
00033  * See http://www.ietf.org/rfc/rfc2104.txt and
00034  * http://www.faqs.org/rfcs/rfc2202.html
00035  */
00036 
00037 #include <string.h>
00038 #include "os_port.h"
00039 #include "crypto.h "
00040 
00041 /**
00042  * Perform HMAC-MD5
00043  * NOTE: does not handle keys larger than the block size.
00044  */
00045 void hmac_md5(const uint8_t *msg, int length, const uint8_t *key, 
00046         int key_len, uint8_t *digest)
00047 {
00048     MD5_CTX context;
00049     uint8_t k_ipad[64];
00050     uint8_t k_opad[64];
00051     int i;
00052 
00053     memset(k_ipad, 0, sizeof k_ipad);
00054     memset(k_opad, 0, sizeof k_opad);
00055     memcpy(k_ipad, key, key_len);
00056     memcpy(k_opad, key, key_len);
00057 
00058     for (i = 0; i < 64; i++) 
00059     {
00060         k_ipad[i] ^= 0x36;
00061         k_opad[i] ^= 0x5c;
00062     }
00063 
00064     MD5_Init(&context);
00065     MD5_Update(&context, k_ipad, 64);
00066     MD5_Update(&context, msg, length);
00067     MD5_Final(digest, &context);
00068     MD5_Init(&context);
00069     MD5_Update(&context, k_opad, 64);
00070     MD5_Update(&context, digest, MD5_SIZE);
00071     MD5_Final(digest, &context);
00072 }
00073 
00074 /**
00075  * Perform HMAC-SHA1
00076  * NOTE: does not handle keys larger than the block size.
00077  */
00078 void hmac_sha1(const uint8_t *msg, int length, const uint8_t *key, 
00079         int key_len, uint8_t *digest)
00080 {
00081     SHA1_CTX context;
00082     uint8_t k_ipad[64];
00083     uint8_t k_opad[64];
00084     int i;
00085 
00086     memset(k_ipad, 0, sizeof k_ipad);
00087     memset(k_opad, 0, sizeof k_opad);
00088     memcpy(k_ipad, key, key_len);
00089     memcpy(k_opad, key, key_len);
00090 
00091     for (i = 0; i < 64; i++) 
00092     {
00093         k_ipad[i] ^= 0x36;
00094         k_opad[i] ^= 0x5c;
00095     }
00096 
00097     SHA1_Init(&context);
00098     SHA1_Update(&context, k_ipad, 64);
00099     SHA1_Update(&context, msg, length);
00100     SHA1_Final(digest, &context);
00101     SHA1_Init(&context);
00102     SHA1_Update(&context, k_opad, 64);
00103     SHA1_Update(&context, digest, SHA1_SIZE);
00104     SHA1_Final(digest, &context);
00105 }
00106 
00107