1 /* hmac.c - TinyCrypt implementation of the HMAC algorithm */
2 
3 /*
4  *  Copyright (C) 2017 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 
rekey(uint8_t * key,const uint8_t * new_key,unsigned int key_size)37 static void rekey(uint8_t *key, const uint8_t *new_key, unsigned int key_size)
38 {
39 	const uint8_t inner_pad = (uint8_t) 0x36;
40 	const uint8_t outer_pad = (uint8_t) 0x5c;
41 	unsigned int 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 
tc_hmac_set_key(TCHmacState_t ctx,const uint8_t * key,unsigned int key_size)52 int tc_hmac_set_key(TCHmacState_t ctx, const uint8_t *key,
53 		    unsigned int key_size)
54 {
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 
tc_hmac_init(TCHmacState_t ctx)96 int tc_hmac_init(TCHmacState_t ctx)
97 {
98 
99 	/* input sanity check: */
100 	if (ctx == (TCHmacState_t) 0) {
101 		return TC_CRYPTO_FAIL;
102 	}
103 
104   (void) tc_sha256_init(&ctx->hash_state);
105   (void) tc_sha256_update(&ctx->hash_state, ctx->key, TC_SHA256_BLOCK_SIZE);
106 
107 	return TC_CRYPTO_SUCCESS;
108 }
109 
tc_hmac_update(TCHmacState_t ctx,const void * data,unsigned int data_length)110 int tc_hmac_update(TCHmacState_t ctx,
111 		   const void *data,
112 		   unsigned int data_length)
113 {
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 
tc_hmac_final(uint8_t * tag,unsigned int taglen,TCHmacState_t ctx)125 int tc_hmac_final(uint8_t *tag, unsigned int taglen, TCHmacState_t ctx)
126 {
127 
128 	/* input sanity check: */
129 	if (tag == (uint8_t *) 0 ||
130 	    taglen != TC_SHA256_DIGEST_SIZE ||
131 	    ctx == (TCHmacState_t) 0) {
132 		return TC_CRYPTO_FAIL;
133 	}
134 
135 	(void) tc_sha256_final(tag, &ctx->hash_state);
136 
137 	(void)tc_sha256_init(&ctx->hash_state);
138 	(void)tc_sha256_update(&ctx->hash_state,
139 			       &ctx->key[TC_SHA256_BLOCK_SIZE],
140 				TC_SHA256_BLOCK_SIZE);
141 	(void)tc_sha256_update(&ctx->hash_state, tag, TC_SHA256_DIGEST_SIZE);
142 	(void)tc_sha256_final(tag, &ctx->hash_state);
143 
144 	/* destroy the current state */
145 	_set(ctx, 0, sizeof(*ctx));
146 
147 	return TC_CRYPTO_SUCCESS;
148 }
149