blob: b7041c994b9456cb7068ac14c939a85ad0516164 [file] [log] [blame]
Daisuke Nojirie1741c52015-02-09 18:15:17 -08001/*
2 * Copyright (C) 2015 Broadcom Corporation
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
7 *
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
Daisuke Nojirie1741c52015-02-09 18:15:17 -080014#include <stdio.h>
15#include <string.h>
16#include <stdint.h>
17#include "secimage.h"
18#include <openssl/hmac.h>
19
Daisuke Nojirie1741c52015-02-09 18:15:17 -080020/*----------------------------------------------------------------------
21 * Name : HmacSha256Hash
22 * Purpose :
23 * Input : none
24 * Output : none
25 *---------------------------------------------------------------------*/
26int HmacSha256Hash(uint8_t *data, uint32_t len, uint8_t *hash, uint8_t *key)
27{
28 HMAC_CTX hctx;
29
30 HMAC_CTX_init(&hctx);
31 HMAC_Init_ex(&hctx, key, 32, EVP_sha256(), NULL);
32
Stefan Reinauer9dd8f882015-08-06 16:55:09 -070033 /* FIXME: why we need this? NULL means to use whatever there is?
Daisuke Nojirie1741c52015-02-09 18:15:17 -080034 * if removed, result is different
35 */
36 HMAC_Init_ex(&hctx, NULL, 0, NULL, NULL);
37 HMAC_Update(&hctx, data, len);
38 HMAC_Final(&hctx, hash, NULL);
39
40 HMAC_CTX_cleanup(&hctx);
41 return 0;
42}
43
Daisuke Nojirie1741c52015-02-09 18:15:17 -080044/*----------------------------------------------------------------------
45 * Name : AppendHMACSignature
46 * Purpose : Appends HMAC signature at the end of the data
47 *---------------------------------------------------------------------*/
48int AppendHMACSignature(uint8_t *data, uint32_t length, char *filename,
49 uint32_t offset)
50{
51 uint8_t hmackey[32];
52 uint32_t len;
53 uint32_t status;
54 uint8_t *digest = data + length;
55
56 len = ReadBinaryFile(filename, hmackey, 32);
57 if (len != 32) {
58 printf("Error reading hmac key file\n");
59 return 0;
60 }
61
62 status = HmacSha256Hash(&data[offset], length - offset, digest,
63 hmackey);
64
65 if (status) {
66 printf("HMAC-SHA256 hash error\n");
67 return 0;
68 }
69
70 return 32;
71}