Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 1 | /* |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 2 | * Copyright (c) 2015-2022, Arm Limited and Contributors. All rights reserved. |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Isla Mitchell | 9930501 | 2017-07-11 14:54:08 +0100 | [diff] [blame] | 7 | #include <stdio.h> |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 8 | #include "debug.h" |
Qixiang Xu | 76a5a9b | 2017-11-09 13:51:58 +0800 | [diff] [blame] | 9 | #include "key.h" |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 10 | #if USING_OPENSSL3 |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 11 | #include <openssl/evp.h> |
| 12 | #include <openssl/obj_mac.h> |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 13 | #else |
| 14 | #include <openssl/sha.h> |
| 15 | #endif |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 16 | |
| 17 | #define BUFFER_SIZE 256 |
| 18 | |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 19 | #if USING_OPENSSL3 |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 20 | static 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 Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 28 | #endif |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 29 | |
Qixiang Xu | 76a5a9b | 2017-11-09 13:51:58 +0800 | [diff] [blame] | 30 | int sha_file(int md_alg, const char *filename, unsigned char *md) |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 31 | { |
| 32 | FILE *inFile; |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 33 | int bytes; |
| 34 | unsigned char data[BUFFER_SIZE]; |
| 35 | #if USING_OPENSSL3 |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 36 | EVP_MD_CTX *mdctx; |
| 37 | const EVP_MD *md_type; |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 38 | int alg_nid; |
| 39 | unsigned int total_bytes; |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 40 | #else |
| 41 | SHA256_CTX shaContext; |
| 42 | SHA512_CTX sha512Context; |
| 43 | #endif |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 44 | |
| 45 | if ((filename == NULL) || (md == NULL)) { |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 46 | ERROR("%s(): NULL argument\n", __func__); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 47 | 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 Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 56 | #if USING_OPENSSL3 |
| 57 | |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 58 | 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 Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 69 | } |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 70 | |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 71 | 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 Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 82 | fclose(inFile); |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 83 | EVP_MD_CTX_free(mdctx); |
Juan Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 84 | return 1; |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 85 | |
| 86 | err: |
| 87 | fclose(inFile); |
| 88 | EVP_MD_CTX_free(mdctx); |
| 89 | return 0; |
Juan Pablo Conde | 3539c74 | 2022-10-25 19:41:02 -0400 | [diff] [blame] | 90 | |
| 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 Castillo | 11abdcd | 2014-10-21 11:30:42 +0100 | [diff] [blame] | 117 | } |
Juan Pablo Conde | 7275a5a | 2022-03-02 18:10:08 -0500 | [diff] [blame] | 118 | |