Rough and ready port of axTLS
crypto/hmac.c@0:5a29fd060ac8, 2013-05-13 (annotated)
- Committer:
- ashleymills
- Date:
- Mon May 13 18:15:18 2013 +0000
- Revision:
- 0:5a29fd060ac8
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ashleymills | 0:5a29fd060ac8 | 1 | /* |
ashleymills | 0:5a29fd060ac8 | 2 | * Copyright (c) 2007, Cameron Rich |
ashleymills | 0:5a29fd060ac8 | 3 | * |
ashleymills | 0:5a29fd060ac8 | 4 | * All rights reserved. |
ashleymills | 0:5a29fd060ac8 | 5 | * |
ashleymills | 0:5a29fd060ac8 | 6 | * Redistribution and use in source and binary forms, with or without |
ashleymills | 0:5a29fd060ac8 | 7 | * modification, are permitted provided that the following conditions are met: |
ashleymills | 0:5a29fd060ac8 | 8 | * |
ashleymills | 0:5a29fd060ac8 | 9 | * * Redistributions of source code must retain the above copyright notice, |
ashleymills | 0:5a29fd060ac8 | 10 | * this list of conditions and the following disclaimer. |
ashleymills | 0:5a29fd060ac8 | 11 | * * Redistributions in binary form must reproduce the above copyright notice, |
ashleymills | 0:5a29fd060ac8 | 12 | * this list of conditions and the following disclaimer in the documentation |
ashleymills | 0:5a29fd060ac8 | 13 | * and/or other materials provided with the distribution. |
ashleymills | 0:5a29fd060ac8 | 14 | * * Neither the name of the axTLS project nor the names of its contributors |
ashleymills | 0:5a29fd060ac8 | 15 | * may be used to endorse or promote products derived from this software |
ashleymills | 0:5a29fd060ac8 | 16 | * without specific prior written permission. |
ashleymills | 0:5a29fd060ac8 | 17 | * |
ashleymills | 0:5a29fd060ac8 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
ashleymills | 0:5a29fd060ac8 | 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
ashleymills | 0:5a29fd060ac8 | 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
ashleymills | 0:5a29fd060ac8 | 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
ashleymills | 0:5a29fd060ac8 | 22 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
ashleymills | 0:5a29fd060ac8 | 23 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
ashleymills | 0:5a29fd060ac8 | 24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
ashleymills | 0:5a29fd060ac8 | 25 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
ashleymills | 0:5a29fd060ac8 | 26 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
ashleymills | 0:5a29fd060ac8 | 27 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
ashleymills | 0:5a29fd060ac8 | 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
ashleymills | 0:5a29fd060ac8 | 29 | */ |
ashleymills | 0:5a29fd060ac8 | 30 | |
ashleymills | 0:5a29fd060ac8 | 31 | /** |
ashleymills | 0:5a29fd060ac8 | 32 | * HMAC implementation - This code was originally taken from RFC2104 |
ashleymills | 0:5a29fd060ac8 | 33 | * See http://www.ietf.org/rfc/rfc2104.txt and |
ashleymills | 0:5a29fd060ac8 | 34 | * http://www.faqs.org/rfcs/rfc2202.html |
ashleymills | 0:5a29fd060ac8 | 35 | */ |
ashleymills | 0:5a29fd060ac8 | 36 | |
ashleymills | 0:5a29fd060ac8 | 37 | #include <string.h> |
ashleymills | 0:5a29fd060ac8 | 38 | #include "os_port.h" |
ashleymills | 0:5a29fd060ac8 | 39 | #include "crypto.h" |
ashleymills | 0:5a29fd060ac8 | 40 | |
ashleymills | 0:5a29fd060ac8 | 41 | /** |
ashleymills | 0:5a29fd060ac8 | 42 | * Perform HMAC-MD5 |
ashleymills | 0:5a29fd060ac8 | 43 | * NOTE: does not handle keys larger than the block size. |
ashleymills | 0:5a29fd060ac8 | 44 | */ |
ashleymills | 0:5a29fd060ac8 | 45 | void hmac_md5(const uint8_t *msg, int length, const uint8_t *key, |
ashleymills | 0:5a29fd060ac8 | 46 | int key_len, uint8_t *digest) |
ashleymills | 0:5a29fd060ac8 | 47 | { |
ashleymills | 0:5a29fd060ac8 | 48 | MD5_CTX context; |
ashleymills | 0:5a29fd060ac8 | 49 | uint8_t k_ipad[64]; |
ashleymills | 0:5a29fd060ac8 | 50 | uint8_t k_opad[64]; |
ashleymills | 0:5a29fd060ac8 | 51 | int i; |
ashleymills | 0:5a29fd060ac8 | 52 | |
ashleymills | 0:5a29fd060ac8 | 53 | memset(k_ipad, 0, sizeof k_ipad); |
ashleymills | 0:5a29fd060ac8 | 54 | memset(k_opad, 0, sizeof k_opad); |
ashleymills | 0:5a29fd060ac8 | 55 | memcpy(k_ipad, key, key_len); |
ashleymills | 0:5a29fd060ac8 | 56 | memcpy(k_opad, key, key_len); |
ashleymills | 0:5a29fd060ac8 | 57 | |
ashleymills | 0:5a29fd060ac8 | 58 | for (i = 0; i < 64; i++) |
ashleymills | 0:5a29fd060ac8 | 59 | { |
ashleymills | 0:5a29fd060ac8 | 60 | k_ipad[i] ^= 0x36; |
ashleymills | 0:5a29fd060ac8 | 61 | k_opad[i] ^= 0x5c; |
ashleymills | 0:5a29fd060ac8 | 62 | } |
ashleymills | 0:5a29fd060ac8 | 63 | |
ashleymills | 0:5a29fd060ac8 | 64 | MD5_Init(&context); |
ashleymills | 0:5a29fd060ac8 | 65 | MD5_Update(&context, k_ipad, 64); |
ashleymills | 0:5a29fd060ac8 | 66 | MD5_Update(&context, msg, length); |
ashleymills | 0:5a29fd060ac8 | 67 | MD5_Final(digest, &context); |
ashleymills | 0:5a29fd060ac8 | 68 | MD5_Init(&context); |
ashleymills | 0:5a29fd060ac8 | 69 | MD5_Update(&context, k_opad, 64); |
ashleymills | 0:5a29fd060ac8 | 70 | MD5_Update(&context, digest, MD5_SIZE); |
ashleymills | 0:5a29fd060ac8 | 71 | MD5_Final(digest, &context); |
ashleymills | 0:5a29fd060ac8 | 72 | } |
ashleymills | 0:5a29fd060ac8 | 73 | |
ashleymills | 0:5a29fd060ac8 | 74 | /** |
ashleymills | 0:5a29fd060ac8 | 75 | * Perform HMAC-SHA1 |
ashleymills | 0:5a29fd060ac8 | 76 | * NOTE: does not handle keys larger than the block size. |
ashleymills | 0:5a29fd060ac8 | 77 | */ |
ashleymills | 0:5a29fd060ac8 | 78 | void hmac_sha1(const uint8_t *msg, int length, const uint8_t *key, |
ashleymills | 0:5a29fd060ac8 | 79 | int key_len, uint8_t *digest) |
ashleymills | 0:5a29fd060ac8 | 80 | { |
ashleymills | 0:5a29fd060ac8 | 81 | SHA1_CTX context; |
ashleymills | 0:5a29fd060ac8 | 82 | uint8_t k_ipad[64]; |
ashleymills | 0:5a29fd060ac8 | 83 | uint8_t k_opad[64]; |
ashleymills | 0:5a29fd060ac8 | 84 | int i; |
ashleymills | 0:5a29fd060ac8 | 85 | |
ashleymills | 0:5a29fd060ac8 | 86 | memset(k_ipad, 0, sizeof k_ipad); |
ashleymills | 0:5a29fd060ac8 | 87 | memset(k_opad, 0, sizeof k_opad); |
ashleymills | 0:5a29fd060ac8 | 88 | memcpy(k_ipad, key, key_len); |
ashleymills | 0:5a29fd060ac8 | 89 | memcpy(k_opad, key, key_len); |
ashleymills | 0:5a29fd060ac8 | 90 | |
ashleymills | 0:5a29fd060ac8 | 91 | for (i = 0; i < 64; i++) |
ashleymills | 0:5a29fd060ac8 | 92 | { |
ashleymills | 0:5a29fd060ac8 | 93 | k_ipad[i] ^= 0x36; |
ashleymills | 0:5a29fd060ac8 | 94 | k_opad[i] ^= 0x5c; |
ashleymills | 0:5a29fd060ac8 | 95 | } |
ashleymills | 0:5a29fd060ac8 | 96 | |
ashleymills | 0:5a29fd060ac8 | 97 | SHA1_Init(&context); |
ashleymills | 0:5a29fd060ac8 | 98 | SHA1_Update(&context, k_ipad, 64); |
ashleymills | 0:5a29fd060ac8 | 99 | SHA1_Update(&context, msg, length); |
ashleymills | 0:5a29fd060ac8 | 100 | SHA1_Final(digest, &context); |
ashleymills | 0:5a29fd060ac8 | 101 | SHA1_Init(&context); |
ashleymills | 0:5a29fd060ac8 | 102 | SHA1_Update(&context, k_opad, 64); |
ashleymills | 0:5a29fd060ac8 | 103 | SHA1_Update(&context, digest, SHA1_SIZE); |
ashleymills | 0:5a29fd060ac8 | 104 | SHA1_Final(digest, &context); |
ashleymills | 0:5a29fd060ac8 | 105 | } |