xref: /rk3399_ARM-atf/tools/cert_create/src/sha.c (revision 2a4b4b71ba8a14148708719077d80889faa6f47b)
16f971622SJuan Castillo /*
26f971622SJuan Castillo  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
36f971622SJuan Castillo  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
56f971622SJuan Castillo  */
66f971622SJuan Castillo 
76f971622SJuan Castillo #include <openssl/sha.h>
8*2a4b4b71SIsla Mitchell #include <stdio.h>
96f971622SJuan Castillo 
106f971622SJuan Castillo #include "debug.h"
116f971622SJuan Castillo 
126f971622SJuan Castillo #define BUFFER_SIZE	256
136f971622SJuan Castillo 
146f971622SJuan Castillo int sha_file(const char *filename, unsigned char *md)
156f971622SJuan Castillo {
166f971622SJuan Castillo 	FILE *inFile;
176f971622SJuan Castillo 	SHA256_CTX shaContext;
186f971622SJuan Castillo 	int bytes;
196f971622SJuan Castillo 	unsigned char data[BUFFER_SIZE];
206f971622SJuan Castillo 
216f971622SJuan Castillo 	if ((filename == NULL) || (md == NULL)) {
226f971622SJuan Castillo 		ERROR("%s(): NULL argument\n", __FUNCTION__);
236f971622SJuan Castillo 		return 0;
246f971622SJuan Castillo 	}
256f971622SJuan Castillo 
266f971622SJuan Castillo 	inFile = fopen(filename, "rb");
276f971622SJuan Castillo 	if (inFile == NULL) {
286f971622SJuan Castillo 		ERROR("Cannot read %s\n", filename);
296f971622SJuan Castillo 		return 0;
306f971622SJuan Castillo 	}
316f971622SJuan Castillo 
326f971622SJuan Castillo 	SHA256_Init(&shaContext);
336f971622SJuan Castillo 	while ((bytes = fread(data, 1, BUFFER_SIZE, inFile)) != 0) {
346f971622SJuan Castillo 		SHA256_Update(&shaContext, data, bytes);
356f971622SJuan Castillo 	}
366f971622SJuan Castillo 	SHA256_Final(md, &shaContext);
376f971622SJuan Castillo 
386f971622SJuan Castillo 	fclose(inFile);
396f971622SJuan Castillo 	return 1;
406f971622SJuan Castillo }
41