Gaurav Shah | 321f310 | 2010-01-28 16:59:42 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
| 4 | * |
| 5 | * Utility functions for message digest functions. |
| 6 | */ |
| 7 | |
| 8 | #include "digest_utility.h" |
| 9 | #include "sha.h" |
| 10 | |
| 11 | #include <fcntl.h> |
| 12 | #include <unistd.h> |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | #include <sys/types.h> |
| 17 | #include <sys/stat.h> |
| 18 | |
| 19 | uint8_t* SHA1_file(char* input_file) { |
| 20 | int input_fd, len; |
| 21 | uint8_t data[ SHA1_BLOCK_SIZE], *digest = NULL, *p = NULL; |
| 22 | SHA1_CTX ctx; |
| 23 | if( (input_fd = open(input_file, O_RDONLY)) == -1 ) { |
| 24 | fprintf(stderr, "Couldn't open input file.\n"); |
| 25 | return NULL; |
| 26 | } |
| 27 | SHA1_init(&ctx); |
| 28 | while ( (len = read(input_fd, data, SHA1_BLOCK_SIZE)) == |
| 29 | SHA1_BLOCK_SIZE) |
| 30 | SHA1_update(&ctx, data, len); |
| 31 | if (len != -1) |
| 32 | SHA1_update(&ctx, data, len); |
| 33 | p = SHA1_final(&ctx); |
| 34 | close(input_fd); |
| 35 | digest = (uint8_t*) malloc(SHA1_DIGEST_SIZE); |
| 36 | if (!digest) |
| 37 | return NULL; |
| 38 | memcpy(digest, p, SHA1_DIGEST_SIZE); |
| 39 | return digest; |
| 40 | } |
| 41 | |
| 42 | uint8_t* SHA256_file(char* input_file) { |
| 43 | int input_fd, len; |
| 44 | uint8_t data[ SHA256_BLOCK_SIZE], *digest = NULL, *p = NULL; |
| 45 | SHA256_CTX ctx; |
| 46 | if( (input_fd = open(input_file, O_RDONLY)) == -1 ) { |
| 47 | fprintf(stderr, "Couldn't open input file.\n"); |
| 48 | return NULL; |
| 49 | } |
| 50 | SHA256_init(&ctx); |
| 51 | while ( (len = read(input_fd, data, SHA256_BLOCK_SIZE)) == |
| 52 | SHA256_BLOCK_SIZE) |
| 53 | SHA256_update(&ctx, data, len); |
| 54 | if (len != -1) |
| 55 | SHA256_update(&ctx, data, len); |
| 56 | p = SHA256_final(&ctx); |
| 57 | close(input_fd); |
| 58 | digest = (uint8_t*) malloc(SHA256_DIGEST_SIZE); |
| 59 | if (!digest) |
| 60 | return NULL; |
| 61 | memcpy(digest, p, SHA256_DIGEST_SIZE); |
| 62 | return digest; |
| 63 | } |
| 64 | |
| 65 | uint8_t* SHA512_file(char* input_file) { |
| 66 | int input_fd, len; |
| 67 | uint8_t data[ SHA512_BLOCK_SIZE], *digest = NULL, *p = NULL; |
| 68 | SHA512_CTX ctx; |
| 69 | if( (input_fd = open(input_file, O_RDONLY)) == -1 ) { |
| 70 | fprintf(stderr, "Couldn't open input file.\n"); |
| 71 | return NULL; |
| 72 | } |
| 73 | SHA512_init(&ctx); |
| 74 | while ( (len = read(input_fd, data, SHA512_BLOCK_SIZE)) == |
| 75 | SHA512_BLOCK_SIZE) |
| 76 | SHA512_update(&ctx, data, len); |
| 77 | if (len != -1) |
| 78 | SHA512_update(&ctx, data, len); |
| 79 | p = SHA512_final(&ctx); |
| 80 | close(input_fd); |
| 81 | digest = (uint8_t*) malloc(SHA512_DIGEST_SIZE); |
| 82 | if (!digest) |
| 83 | return NULL; |
| 84 | memcpy(digest, p, SHA512_DIGEST_SIZE); |
| 85 | return digest; |
| 86 | } |
| 87 | |
| 88 | uint8_t* calculate_digest(char *input_file, int algorithm) { |
| 89 | typedef uint8_t* (*Hash_file_ptr) (char*); |
| 90 | Hash_file_ptr hash_file[] = { |
| 91 | SHA1_file, /* RSA 1024 */ |
| 92 | SHA256_file, |
| 93 | SHA512_file, |
| 94 | SHA1_file, /* RSA 2048 */ |
| 95 | SHA256_file, |
| 96 | SHA512_file, |
| 97 | SHA1_file, /* RSA 4096 */ |
| 98 | SHA256_file, |
| 99 | SHA512_file, |
| 100 | SHA1_file, /* RSA 8192 */ |
| 101 | SHA256_file, |
| 102 | SHA512_file, |
| 103 | }; |
| 104 | return hash_file[algorithm](input_file); |
| 105 | } |