OLD | NEW |
1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 * | 4 * |
5 * Utility functions for message digest functions. | 5 * Utility functions for message digest functions. |
6 */ | 6 */ |
7 | 7 |
8 #include "cryptolib.h" | 8 #include "sha_utility.h" |
| 9 |
| 10 #include <fcntl.h> |
| 11 #include <unistd.h> |
| 12 #include <stdio.h> |
| 13 #include <stdlib.h> |
| 14 #include <string.h> |
| 15 #include <sys/types.h> |
| 16 #include <sys/stat.h> |
| 17 |
| 18 #include "sha.h" |
9 #include "utility.h" | 19 #include "utility.h" |
10 | 20 |
| 21 int digest_type_map[] = { |
| 22 SHA1_DIGEST_ALGORITHM, /* RSA 1024 */ |
| 23 SHA256_DIGEST_ALGORITHM, |
| 24 SHA512_DIGEST_ALGORITHM, |
| 25 SHA1_DIGEST_ALGORITHM, /* RSA 2048 */ |
| 26 SHA256_DIGEST_ALGORITHM, |
| 27 SHA512_DIGEST_ALGORITHM, |
| 28 SHA1_DIGEST_ALGORITHM, /* RSA 4096 */ |
| 29 SHA256_DIGEST_ALGORITHM, |
| 30 SHA512_DIGEST_ALGORITHM, |
| 31 SHA1_DIGEST_ALGORITHM, /* RSA 8192 */ |
| 32 SHA256_DIGEST_ALGORITHM, |
| 33 SHA512_DIGEST_ALGORITHM, |
| 34 }; |
| 35 |
11 void DigestInit(DigestContext* ctx, int sig_algorithm) { | 36 void DigestInit(DigestContext* ctx, int sig_algorithm) { |
12 ctx->algorithm = hash_type_map[sig_algorithm]; | 37 ctx->algorithm = digest_type_map[sig_algorithm]; |
13 switch(ctx->algorithm) { | 38 switch(ctx->algorithm) { |
14 case SHA1_DIGEST_ALGORITHM: | 39 case SHA1_DIGEST_ALGORITHM: |
15 ctx->sha1_ctx = (SHA1_CTX*) Malloc(sizeof(SHA1_CTX)); | 40 ctx->sha1_ctx = (SHA1_CTX*) Malloc(sizeof(SHA1_CTX)); |
16 SHA1_init(ctx->sha1_ctx); | 41 SHA1_init(ctx->sha1_ctx); |
17 break; | 42 break; |
18 case SHA256_DIGEST_ALGORITHM: | 43 case SHA256_DIGEST_ALGORITHM: |
19 ctx->sha256_ctx = (SHA256_CTX*) Malloc(sizeof(SHA256_CTX)); | 44 ctx->sha256_ctx = (SHA256_CTX*) Malloc(sizeof(SHA256_CTX)); |
20 SHA256_init(ctx->sha256_ctx); | 45 SHA256_init(ctx->sha256_ctx); |
21 break; | 46 break; |
22 case SHA512_DIGEST_ALGORITHM: | 47 case SHA512_DIGEST_ALGORITHM: |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 break; | 80 break; |
56 case SHA512_DIGEST_ALGORITHM: | 81 case SHA512_DIGEST_ALGORITHM: |
57 digest = (uint8_t*) Malloc(SHA512_DIGEST_SIZE); | 82 digest = (uint8_t*) Malloc(SHA512_DIGEST_SIZE); |
58 Memcpy(digest, SHA512_final(ctx->sha512_ctx), SHA512_DIGEST_SIZE); | 83 Memcpy(digest, SHA512_final(ctx->sha512_ctx), SHA512_DIGEST_SIZE); |
59 Free(ctx->sha512_ctx); | 84 Free(ctx->sha512_ctx); |
60 break; | 85 break; |
61 }; | 86 }; |
62 return digest; | 87 return digest; |
63 } | 88 } |
64 | 89 |
| 90 uint8_t* DigestFile(char* input_file, int sig_algorithm) { |
| 91 int input_fd, len; |
| 92 uint8_t data[SHA1_BLOCK_SIZE]; |
| 93 uint8_t* digest = NULL; |
| 94 DigestContext ctx; |
| 95 |
| 96 if( (input_fd = open(input_file, O_RDONLY)) == -1 ) { |
| 97 fprintf(stderr, "Couldn't open input file.\n"); |
| 98 return NULL; |
| 99 } |
| 100 DigestInit(&ctx, sig_algorithm); |
| 101 while ( (len = read(input_fd, data, SHA1_BLOCK_SIZE)) == |
| 102 SHA1_BLOCK_SIZE) |
| 103 DigestUpdate(&ctx, data, len); |
| 104 if (len != -1) |
| 105 DigestUpdate(&ctx, data, len); |
| 106 digest = DigestFinal(&ctx); |
| 107 close(input_fd); |
| 108 return digest; |
| 109 } |
| 110 |
65 uint8_t* DigestBuf(const uint8_t* buf, uint64_t len, int sig_algorithm) { | 111 uint8_t* DigestBuf(const uint8_t* buf, uint64_t len, int sig_algorithm) { |
66 uint8_t* digest = (uint8_t*) Malloc(SHA512_DIGEST_SIZE); /* Use the max. */ | 112 uint8_t* digest = (uint8_t*) Malloc(SHA512_DIGEST_SIZE); /* Use the max. */ |
67 /* Define an array mapping [sig_algorithm] to function pointers to the | 113 /* Define an array mapping [sig_algorithm] to function pointers to the |
68 * SHA{1|256|512} functions. | 114 * SHA{1|256|512} functions. |
69 */ | 115 */ |
70 typedef uint8_t* (*Hash_ptr) (const uint8_t*, uint64_t, uint8_t*); | 116 typedef uint8_t* (*Hash_ptr) (const uint8_t*, uint64_t, uint8_t*); |
71 Hash_ptr hash[] = { | 117 Hash_ptr hash[] = { |
72 SHA1, /* RSA 1024 */ | 118 SHA1, /* RSA 1024 */ |
73 SHA256, | 119 SHA256, |
74 SHA512, | 120 SHA512, |
75 SHA1, /* RSA 2048 */ | 121 SHA1, /* RSA 2048 */ |
76 SHA256, | 122 SHA256, |
77 SHA512, | 123 SHA512, |
78 SHA1, /* RSA 4096 */ | 124 SHA1, /* RSA 4096 */ |
79 SHA256, | 125 SHA256, |
80 SHA512, | 126 SHA512, |
81 SHA1, /* RSA 8192 */ | 127 SHA1, /* RSA 8192 */ |
82 SHA256, | 128 SHA256, |
83 SHA512, | 129 SHA512, |
84 }; | 130 }; |
85 /* Call the appropriate hash function. */ | 131 /* Call the appropriate hash function. */ |
86 return hash[sig_algorithm](buf, len, digest); | 132 return hash[sig_algorithm](buf, len, digest); |
87 } | 133 } |
OLD | NEW |