blob: e256846edfa5779325bdc14e7609f83d4d61e076 [file] [log] [blame]
David Brownfecda2d2017-09-07 10:20:34 -06001/* hmac.c - TinyCrypt implementation of the HMAC algorithm */
2
3/*
4 * Copyright (C) 2015 by Intel Corporation, All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * - Neither the name of Intel Corporation nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <tinycrypt/hmac.h>
34#include <tinycrypt/constants.h>
35#include <tinycrypt/utils.h>
36
37static void rekey(uint8_t *key, const uint8_t *new_key, uint32_t key_size)
38{
39 const uint8_t inner_pad = (uint8_t) 0x36;
40 const uint8_t outer_pad = (uint8_t) 0x5c;
41 uint32_t i;
42
43 for (i = 0; i < key_size; ++i) {
44 key[i] = inner_pad ^ new_key[i];
45 key[i + TC_SHA256_BLOCK_SIZE] = outer_pad ^ new_key[i];
46 }
47 for (; i < TC_SHA256_BLOCK_SIZE; ++i) {
48 key[i] = inner_pad; key[i + TC_SHA256_BLOCK_SIZE] = outer_pad;
49 }
50}
51
52int32_t tc_hmac_set_key(TCHmacState_t ctx,
53 const uint8_t *key,
54 uint32_t key_size)
55{
56 /* input sanity check: */
57 if (ctx == (TCHmacState_t) 0 ||
58 key == (const uint8_t *) 0 ||
59 key_size == 0) {
60 return TC_CRYPTO_FAIL;
61 }
62
63 const uint8_t dummy_key[key_size];
64 struct tc_hmac_state_struct dummy_state;
65
66 if (key_size <= TC_SHA256_BLOCK_SIZE) {
67 /*
68 * The next three lines consist of dummy calls just to avoid
69 * certain timing attacks. Without these dummy calls,
70 * adversaries would be able to learn whether the key_size is
71 * greater than TC_SHA256_BLOCK_SIZE by measuring the time
72 * consumed in this process.
73 */
74 (void)tc_sha256_init(&dummy_state.hash_state);
75 (void)tc_sha256_update(&dummy_state.hash_state,
76 dummy_key,
77 key_size);
78 (void)tc_sha256_final(&dummy_state.key[TC_SHA256_DIGEST_SIZE],
79 &dummy_state.hash_state);
80
81 /* Actual code for when key_size <= TC_SHA256_BLOCK_SIZE: */
82 rekey(ctx->key, key, key_size);
83 } else {
84 (void)tc_sha256_init(&ctx->hash_state);
85 (void)tc_sha256_update(&ctx->hash_state, key, key_size);
86 (void)tc_sha256_final(&ctx->key[TC_SHA256_DIGEST_SIZE],
87 &ctx->hash_state);
88 rekey(ctx->key,
89 &ctx->key[TC_SHA256_DIGEST_SIZE],
90 TC_SHA256_DIGEST_SIZE);
91 }
92
93 return TC_CRYPTO_SUCCESS;
94}
95
96int32_t tc_hmac_init(TCHmacState_t ctx)
97{
98 /* input sanity check: */
99 if (ctx == (TCHmacState_t) 0) {
100 return TC_CRYPTO_FAIL;
101 }
102
103 (void)tc_sha256_init(&ctx->hash_state);
104 (void)tc_sha256_update(&ctx->hash_state,
105 ctx->key,
106 TC_SHA256_BLOCK_SIZE);
107
108 return TC_CRYPTO_SUCCESS;
109}
110
111int32_t tc_hmac_update(TCHmacState_t ctx,
112 const void *data,
113 uint32_t data_length)
114{
115 /* input sanity check: */
116 if (ctx == (TCHmacState_t) 0) {
117 return TC_CRYPTO_FAIL;
118 }
119
120 (void)tc_sha256_update(&ctx->hash_state, data, data_length);
121
122 return TC_CRYPTO_SUCCESS;
123}
124
125int32_t tc_hmac_final(uint8_t *tag, uint32_t taglen, TCHmacState_t ctx)
126{
127 /* input sanity check: */
128 if (tag == (uint8_t *) 0 ||
129 taglen != TC_SHA256_DIGEST_SIZE ||
130 ctx == (TCHmacState_t) 0) {
131 return TC_CRYPTO_FAIL;
132 }
133
134 (void) tc_sha256_final(tag, &ctx->hash_state);
135
136 (void)tc_sha256_init(&ctx->hash_state);
137 (void)tc_sha256_update(&ctx->hash_state,
138 &ctx->key[TC_SHA256_BLOCK_SIZE],
139 TC_SHA256_BLOCK_SIZE);
140 (void)tc_sha256_update(&ctx->hash_state, tag, TC_SHA256_DIGEST_SIZE);
141 (void)tc_sha256_final(tag, &ctx->hash_state);
142
143 /* destroy the current state */
144 _set(ctx, 0, sizeof(*ctx));
145
146 return TC_CRYPTO_SUCCESS;
147}