MeterLogger
hmac-sha256.c
Go to the documentation of this file.
1 /**
2  * Copyright (c) 2013-2014 Tomas Dzetkulic
3  * Copyright (c) 2013-2014 Pavol Rusnak
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
19  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <esp8266.h>
25 #include <string.h>
26 #include "crypto/crypto.h"
27 #include "crypto/hmac-sha256.h"
28 
30 void hmac_sha256_init(hmac_sha256_ctx_t *hctx, uint8_t *key, uint32_t keylen) {
31  int i;
32  uint8_t i_key_pad[SHA256_BLOCK_LENGTH];
33  memset(i_key_pad, 0, SHA256_BLOCK_LENGTH);
34  if (keylen > SHA256_BLOCK_LENGTH) {
35  sha256_raw(key, keylen, i_key_pad);
36  } else {
37  memcpy(i_key_pad, key, keylen);
38  }
39  for (i = 0; i < SHA256_BLOCK_LENGTH; i++) {
40  hctx->o_key_pad[i] = i_key_pad[i] ^ 0x5c;
41  i_key_pad[i] ^= 0x36;
42  }
43  sha256_init(&(hctx->ctx));
44  sha256_update(&(hctx->ctx), i_key_pad, SHA256_BLOCK_LENGTH);
45  memset(i_key_pad, 0, sizeof(i_key_pad));
46 }
47 
49 void hmac_sha256_update(hmac_sha256_ctx_t *hctx, uint8_t *msg, uint32_t msglen) {
50  sha256_update(&(hctx->ctx), msg, msglen);
51 }
52 
54 void hmac_sha256_final(hmac_sha256_ctx_t *hctx, uint8_t *hmac) {
55  uint8_t hash[SHA256_DIGEST_LENGTH];
56  sha256_final(&(hctx->ctx), hash);
57  sha256_init(&(hctx->ctx));
59  sha256_update(&(hctx->ctx), hash, SHA256_DIGEST_LENGTH);
60  sha256_final(&(hctx->ctx), hmac);
61  memset(hash, 0, sizeof(hash));
62  memset(hctx, 0, sizeof(hmac_sha256_ctx_t));
63 }
64 
66 void hmac_sha256(uint8_t *key, uint32_t keylen, uint8_t *msg, uint32_t msglen, uint8_t *hmac) {
67  hmac_sha256_ctx_t hctx;
68  hmac_sha256_init(&hctx, key, keylen);
69  hmac_sha256_update(&hctx, msg, msglen);
70  hmac_sha256_final(&hctx, hmac);
71 }
#define memset(x, a, b)
Definition: platform.h:21
#define SHA256_BLOCK_LENGTH
Definition: sha256.h:38
ICACHE_FLASH_ATTR void sha256_raw(uint8_t *data, size_t len, uint8_t digest[SHA256_DIGEST_LENGTH])
Definition: sha256.c:421
#define ICACHE_FLASH_ATTR
Definition: c_types.h:99
ICACHE_FLASH_ATTR void hmac_sha256(uint8_t *key, uint32_t keylen, uint8_t *msg, uint32_t msglen, uint8_t *hmac)
Definition: hmac-sha256.c:66
ICACHE_FLASH_ATTR void hmac_sha256_update(hmac_sha256_ctx_t *hctx, uint8_t *msg, uint32_t msglen)
Definition: hmac-sha256.c:49
ICACHE_FLASH_ATTR void sha256_final(sha256_ctx_t *context, uint8_t digest[SHA256_DIGEST_LENGTH])
Definition: sha256.c:334
uint8_t o_key_pad[SHA256_BLOCK_LENGTH]
Definition: hmac-sha256.h:33
ICACHE_FLASH_ATTR void hmac_sha256_final(hmac_sha256_ctx_t *hctx, uint8_t *hmac)
Definition: hmac-sha256.c:54
static const char key[]
Definition: config.h:42
ICACHE_FLASH_ATTR void sha256_init(sha256_ctx_t *context)
Definition: sha256.c:196
#define memcpy(x, a, b)
Definition: platform.h:22
sha256_ctx_t ctx
Definition: hmac-sha256.h:34
#define SHA256_DIGEST_LENGTH
Definition: sha256.h:39
ICACHE_FLASH_ATTR void sha256_update(sha256_ctx_t *context, uint8_t *data, size_t len)
Definition: sha256.c:288
ICACHE_FLASH_ATTR void hmac_sha256_init(hmac_sha256_ctx_t *hctx, uint8_t *key, uint32_t keylen)
Definition: hmac-sha256.c:30