Index: src/platform/vboot_reference/crypto/sha_utility.c |
diff --git a/src/platform/vboot_reference/crypto/sha_utility.c b/src/platform/vboot_reference/crypto/sha_utility.c |
index 4e266f7cf7c6094b38d41436c203c9d93899235d..1478a7a4fa3406c25a01d921955477ef9e7bc9c0 100644 |
--- a/src/platform/vboot_reference/crypto/sha_utility.c |
+++ b/src/platform/vboot_reference/crypto/sha_utility.c |
@@ -5,11 +5,36 @@ |
* Utility functions for message digest functions. |
*/ |
-#include "cryptolib.h" |
+#include "sha_utility.h" |
+ |
+#include <fcntl.h> |
+#include <unistd.h> |
+#include <stdio.h> |
+#include <stdlib.h> |
+#include <string.h> |
+#include <sys/types.h> |
+#include <sys/stat.h> |
+ |
+#include "sha.h" |
#include "utility.h" |
+int digest_type_map[] = { |
+ SHA1_DIGEST_ALGORITHM, /* RSA 1024 */ |
+ SHA256_DIGEST_ALGORITHM, |
+ SHA512_DIGEST_ALGORITHM, |
+ SHA1_DIGEST_ALGORITHM, /* RSA 2048 */ |
+ SHA256_DIGEST_ALGORITHM, |
+ SHA512_DIGEST_ALGORITHM, |
+ SHA1_DIGEST_ALGORITHM, /* RSA 4096 */ |
+ SHA256_DIGEST_ALGORITHM, |
+ SHA512_DIGEST_ALGORITHM, |
+ SHA1_DIGEST_ALGORITHM, /* RSA 8192 */ |
+ SHA256_DIGEST_ALGORITHM, |
+ SHA512_DIGEST_ALGORITHM, |
+}; |
+ |
void DigestInit(DigestContext* ctx, int sig_algorithm) { |
- ctx->algorithm = hash_type_map[sig_algorithm]; |
+ ctx->algorithm = digest_type_map[sig_algorithm]; |
switch(ctx->algorithm) { |
case SHA1_DIGEST_ALGORITHM: |
ctx->sha1_ctx = (SHA1_CTX*) Malloc(sizeof(SHA1_CTX)); |
@@ -62,6 +87,27 @@ uint8_t* DigestFinal(DigestContext* ctx) { |
return digest; |
} |
+uint8_t* DigestFile(char* input_file, int sig_algorithm) { |
+ int input_fd, len; |
+ uint8_t data[SHA1_BLOCK_SIZE]; |
+ uint8_t* digest = NULL; |
+ DigestContext ctx; |
+ |
+ if( (input_fd = open(input_file, O_RDONLY)) == -1 ) { |
+ fprintf(stderr, "Couldn't open input file.\n"); |
+ return NULL; |
+ } |
+ DigestInit(&ctx, sig_algorithm); |
+ while ( (len = read(input_fd, data, SHA1_BLOCK_SIZE)) == |
+ SHA1_BLOCK_SIZE) |
+ DigestUpdate(&ctx, data, len); |
+ if (len != -1) |
+ DigestUpdate(&ctx, data, len); |
+ digest = DigestFinal(&ctx); |
+ close(input_fd); |
+ return digest; |
+} |
+ |
uint8_t* DigestBuf(const uint8_t* buf, uint64_t len, int sig_algorithm) { |
uint8_t* digest = (uint8_t*) Malloc(SHA512_DIGEST_SIZE); /* Use the max. */ |
/* Define an array mapping [sig_algorithm] to function pointers to the |