|
| 1 | +/* libsodium: hmac_hmacsha256.c, v0.4.5 2014/04/16 */ |
| 2 | +/** |
| 3 | + * Copyright 2005,2007,2009 Colin Percival. All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions |
| 7 | + * are met: |
| 8 | + * 1. Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer in the |
| 12 | + * documentation and/or other materials provided with the distribution. |
| 13 | + * |
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 15 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 18 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 20 | + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 21 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 22 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 23 | + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 24 | + * SUCH DAMAGE. |
| 25 | + */ |
| 26 | +#include "hmac_sha256.h" |
| 27 | + |
| 28 | +#include <stdint.h> |
| 29 | +#include <string.h> |
| 30 | +#include "sha256.h" |
| 31 | +#include "zeroize.h" |
| 32 | + |
| 33 | +void HMACSHA256(const uint8_t* input, size_t length, const uint8_t* key, |
| 34 | + size_t key_length, uint8_t digest[HMACSHA256_DIGEST_LENGTH]) |
| 35 | +{ |
| 36 | + HMACSHA256CTX context; |
| 37 | + HMACSHA256Init(&context, key, key_length); |
| 38 | + HMACSHA256Update(&context, input, length); |
| 39 | + HMACSHA256Final(&context, digest); |
| 40 | +} |
| 41 | + |
| 42 | +void HMACSHA256Final(HMACSHA256CTX* context, |
| 43 | + uint8_t digest[HMACSHA256_DIGEST_LENGTH]) |
| 44 | +{ |
| 45 | + uint8_t hash[HMACSHA256_DIGEST_LENGTH]; |
| 46 | + |
| 47 | + SHA256Final(&context->ictx, hash); |
| 48 | + SHA256Update(&context->octx, hash, HMACSHA256_DIGEST_LENGTH); |
| 49 | + SHA256Final(&context->octx, digest); |
| 50 | + |
| 51 | + zeroize((void*)hash, sizeof hash); |
| 52 | +} |
| 53 | + |
| 54 | +void HMACSHA256Init(HMACSHA256CTX* context, const uint8_t* key, |
| 55 | + size_t key_length) |
| 56 | +{ |
| 57 | + size_t i; |
| 58 | + uint8_t pad[SHA256_BLOCK_LENGTH]; |
| 59 | + uint8_t key_hash[SHA256_DIGEST_LENGTH]; |
| 60 | + |
| 61 | + if (key_length > SHA256_BLOCK_LENGTH) |
| 62 | + { |
| 63 | + SHA256Init(&context->ictx); |
| 64 | + SHA256Update(&context->ictx, key, key_length); |
| 65 | + SHA256Final(&context->ictx, key_hash); |
| 66 | + key = key_hash; |
| 67 | + key_length = SHA256_DIGEST_LENGTH; |
| 68 | + } |
| 69 | + |
| 70 | + SHA256Init(&context->ictx); |
| 71 | + memset(pad, 0x36, SHA256_BLOCK_LENGTH); |
| 72 | + |
| 73 | + for (i = 0; i < key_length; i++) |
| 74 | + pad[i] ^= key[i]; |
| 75 | + |
| 76 | + SHA256Update(&context->ictx, pad, SHA256_BLOCK_LENGTH); |
| 77 | + SHA256Init(&context->octx); |
| 78 | + memset(pad, 0x5c, SHA256_BLOCK_LENGTH); |
| 79 | + |
| 80 | + for (i = 0; i < key_length; i++) |
| 81 | + pad[i] ^= key[i]; |
| 82 | + |
| 83 | + SHA256Update(&context->octx, pad, SHA256_BLOCK_LENGTH); |
| 84 | + zeroize((void*)key_hash, sizeof key_hash); |
| 85 | +} |
| 86 | + |
| 87 | +void HMACSHA256Update(HMACSHA256CTX* context, const uint8_t* input, |
| 88 | + size_t length) |
| 89 | +{ |
| 90 | + SHA256Update(&context->ictx, input, length); |
| 91 | +} |
0 commit comments