blob: 06ef3601b1d5f876396fe12798aa0c958305bc33 [file] [log] [blame]
Juan Castillo11abdcd2014-10-21 11:30:42 +01001/*
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -05002 * Copyright (c) 2015-2022, ARM Limited and Contributors. All rights reserved.
Juan Castillo11abdcd2014-10-21 11:30:42 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Juan Castillo11abdcd2014-10-21 11:30:42 +01005 */
6
Isla Mitchell99305012017-07-11 14:54:08 +01007#include <stdio.h>
Juan Castillo11abdcd2014-10-21 11:30:42 +01008#include "debug.h"
Qixiang Xu76a5a9b2017-11-09 13:51:58 +08009#include "key.h"
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050010#include <openssl/evp.h>
11#include <openssl/obj_mac.h>
Juan Castillo11abdcd2014-10-21 11:30:42 +010012
13#define BUFFER_SIZE 256
14
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050015static int get_algorithm_nid(int hash_alg)
16{
17 int nids[] = {NID_sha256, NID_sha384, NID_sha512};
18 if (hash_alg < 0 || hash_alg >= sizeof(nids) / sizeof(*nids)) {
19 return NID_undef;
20 }
21 return nids[hash_alg];
22}
23
Qixiang Xu76a5a9b2017-11-09 13:51:58 +080024int sha_file(int md_alg, const char *filename, unsigned char *md)
Juan Castillo11abdcd2014-10-21 11:30:42 +010025{
26 FILE *inFile;
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050027 EVP_MD_CTX *mdctx;
28 const EVP_MD *md_type;
Juan Castillo11abdcd2014-10-21 11:30:42 +010029 int bytes;
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050030 int alg_nid;
31 unsigned int total_bytes;
Juan Castillo11abdcd2014-10-21 11:30:42 +010032 unsigned char data[BUFFER_SIZE];
33
34 if ((filename == NULL) || (md == NULL)) {
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050035 ERROR("%s(): NULL argument\n", __func__);
Juan Castillo11abdcd2014-10-21 11:30:42 +010036 return 0;
37 }
38
39 inFile = fopen(filename, "rb");
40 if (inFile == NULL) {
41 ERROR("Cannot read %s\n", filename);
42 return 0;
43 }
44
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050045 mdctx = EVP_MD_CTX_new();
46 if (mdctx == NULL) {
47 fclose(inFile);
48 ERROR("%s(): Could not create EVP MD context\n", __func__);
49 return 0;
50 }
51
52 alg_nid = get_algorithm_nid(md_alg);
53 if (alg_nid == NID_undef) {
54 ERROR("%s(): Invalid hash algorithm\n", __func__);
55 goto err;
Juan Castillo11abdcd2014-10-21 11:30:42 +010056 }
Juan Castillo11abdcd2014-10-21 11:30:42 +010057
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050058 md_type = EVP_get_digestbynid(alg_nid);
59 if (EVP_DigestInit_ex(mdctx, md_type, NULL) == 0) {
60 ERROR("%s(): Could not initialize EVP MD digest\n", __func__);
61 goto err;
62 }
63
64 while ((bytes = fread(data, 1, BUFFER_SIZE, inFile)) != 0) {
65 EVP_DigestUpdate(mdctx, data, bytes);
66 }
67 EVP_DigestFinal_ex(mdctx, md, &total_bytes);
68
Juan Castillo11abdcd2014-10-21 11:30:42 +010069 fclose(inFile);
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050070 EVP_MD_CTX_free(mdctx);
Juan Castillo11abdcd2014-10-21 11:30:42 +010071 return 1;
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050072
73err:
74 fclose(inFile);
75 EVP_MD_CTX_free(mdctx);
76 return 0;
Juan Castillo11abdcd2014-10-21 11:30:42 +010077}
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050078