Daisuke Nojiri | e1741c5 | 2015-02-09 18:15:17 -0800 | [diff] [blame] | 1 | /* |
| 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 Nojiri | e1741c5 | 2015-02-09 18:15:17 -0800 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | #include <stdint.h> |
| 17 | #include "secimage.h" |
| 18 | #include <openssl/hmac.h> |
| 19 | |
Daisuke Nojiri | e1741c5 | 2015-02-09 18:15:17 -0800 | [diff] [blame] | 20 | /*---------------------------------------------------------------------- |
| 21 | * Name : HmacSha256Hash |
| 22 | * Purpose : |
| 23 | * Input : none |
| 24 | * Output : none |
| 25 | *---------------------------------------------------------------------*/ |
| 26 | int 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 Reinauer | 9dd8f88 | 2015-08-06 16:55:09 -0700 | [diff] [blame] | 33 | /* FIXME: why we need this? NULL means to use whatever there is? |
Daisuke Nojiri | e1741c5 | 2015-02-09 18:15:17 -0800 | [diff] [blame] | 34 | * 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 Nojiri | e1741c5 | 2015-02-09 18:15:17 -0800 | [diff] [blame] | 44 | /*---------------------------------------------------------------------- |
| 45 | * Name : AppendHMACSignature |
| 46 | * Purpose : Appends HMAC signature at the end of the data |
| 47 | *---------------------------------------------------------------------*/ |
| 48 | int 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 | } |