blob: bb750d4e319a875efaee23f2eca217182cda3f6a [file] [log] [blame]
Juan Castillo11abdcd2014-10-21 11:30:42 +01001/*
Juan Pablo Conde3539c742022-10-25 19:41:02 -04002 * 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 Conde3539c742022-10-25 19:41:02 -040010#if USING_OPENSSL3
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050011#include <openssl/evp.h>
12#include <openssl/obj_mac.h>
Juan Pablo Conde3539c742022-10-25 19:41:02 -040013#else
14#include <openssl/sha.h>
15#endif
Juan Castillo11abdcd2014-10-21 11:30:42 +010016
17#define BUFFER_SIZE 256
18
Juan Pablo Conde3539c742022-10-25 19:41:02 -040019#if USING_OPENSSL3
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050020static int get_algorithm_nid(int hash_alg)
21{
22 int nids[] = {NID_sha256, NID_sha384, NID_sha512};
23 if (hash_alg < 0 || hash_alg >= sizeof(nids) / sizeof(*nids)) {
24 return NID_undef;
25 }
26 return nids[hash_alg];
27}
Juan Pablo Conde3539c742022-10-25 19:41:02 -040028#endif
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050029
Qixiang Xu76a5a9b2017-11-09 13:51:58 +080030int sha_file(int md_alg, const char *filename, unsigned char *md)
Juan Castillo11abdcd2014-10-21 11:30:42 +010031{
32 FILE *inFile;
Juan Pablo Conde3539c742022-10-25 19:41:02 -040033 int bytes;
34 unsigned char data[BUFFER_SIZE];
35#if USING_OPENSSL3
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050036 EVP_MD_CTX *mdctx;
37 const EVP_MD *md_type;
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050038 int alg_nid;
39 unsigned int total_bytes;
Juan Pablo Conde3539c742022-10-25 19:41:02 -040040#else
41 SHA256_CTX shaContext;
42 SHA512_CTX sha512Context;
43#endif
Juan Castillo11abdcd2014-10-21 11:30:42 +010044
45 if ((filename == NULL) || (md == NULL)) {
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050046 ERROR("%s(): NULL argument\n", __func__);
Juan Castillo11abdcd2014-10-21 11:30:42 +010047 return 0;
48 }
49
50 inFile = fopen(filename, "rb");
51 if (inFile == NULL) {
52 ERROR("Cannot read %s\n", filename);
53 return 0;
54 }
55
Juan Pablo Conde3539c742022-10-25 19:41:02 -040056#if USING_OPENSSL3
57
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050058 mdctx = EVP_MD_CTX_new();
59 if (mdctx == NULL) {
60 fclose(inFile);
61 ERROR("%s(): Could not create EVP MD context\n", __func__);
62 return 0;
63 }
64
65 alg_nid = get_algorithm_nid(md_alg);
66 if (alg_nid == NID_undef) {
67 ERROR("%s(): Invalid hash algorithm\n", __func__);
68 goto err;
Juan Castillo11abdcd2014-10-21 11:30:42 +010069 }
Juan Castillo11abdcd2014-10-21 11:30:42 +010070
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050071 md_type = EVP_get_digestbynid(alg_nid);
72 if (EVP_DigestInit_ex(mdctx, md_type, NULL) == 0) {
73 ERROR("%s(): Could not initialize EVP MD digest\n", __func__);
74 goto err;
75 }
76
77 while ((bytes = fread(data, 1, BUFFER_SIZE, inFile)) != 0) {
78 EVP_DigestUpdate(mdctx, data, bytes);
79 }
80 EVP_DigestFinal_ex(mdctx, md, &total_bytes);
81
Juan Castillo11abdcd2014-10-21 11:30:42 +010082 fclose(inFile);
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050083 EVP_MD_CTX_free(mdctx);
Juan Castillo11abdcd2014-10-21 11:30:42 +010084 return 1;
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -050085
86err:
87 fclose(inFile);
88 EVP_MD_CTX_free(mdctx);
89 return 0;
Juan Pablo Conde3539c742022-10-25 19:41:02 -040090
91#else
92
93 if (md_alg == HASH_ALG_SHA384) {
94 SHA384_Init(&sha512Context);
95 while ((bytes = fread(data, 1, BUFFER_SIZE, inFile)) != 0) {
96 SHA384_Update(&sha512Context, data, bytes);
97 }
98 SHA384_Final(md, &sha512Context);
99 } else if (md_alg == HASH_ALG_SHA512) {
100 SHA512_Init(&sha512Context);
101 while ((bytes = fread(data, 1, BUFFER_SIZE, inFile)) != 0) {
102 SHA512_Update(&sha512Context, data, bytes);
103 }
104 SHA512_Final(md, &sha512Context);
105 } else {
106 SHA256_Init(&shaContext);
107 while ((bytes = fread(data, 1, BUFFER_SIZE, inFile)) != 0) {
108 SHA256_Update(&shaContext, data, bytes);
109 }
110 SHA256_Final(md, &shaContext);
111 }
112
113 fclose(inFile);
114 return 1;
115
116#endif
Juan Castillo11abdcd2014-10-21 11:30:42 +0100117}
Juan Pablo Conde7275a5a2022-03-02 18:10:08 -0500118