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 "sha_utility.h" | 8 #include "cryptolib.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" | |
19 #include "utility.h" | 9 #include "utility.h" |
20 | 10 |
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 | |
36 void DigestInit(DigestContext* ctx, int sig_algorithm) { | 11 void DigestInit(DigestContext* ctx, int sig_algorithm) { |
37 ctx->algorithm = digest_type_map[sig_algorithm]; | 12 ctx->algorithm = hash_type_map[sig_algorithm]; |
38 switch(ctx->algorithm) { | 13 switch(ctx->algorithm) { |
39 case SHA1_DIGEST_ALGORITHM: | 14 case SHA1_DIGEST_ALGORITHM: |
40 ctx->sha1_ctx = (SHA1_CTX*) Malloc(sizeof(SHA1_CTX)); | 15 ctx->sha1_ctx = (SHA1_CTX*) Malloc(sizeof(SHA1_CTX)); |
41 SHA1_init(ctx->sha1_ctx); | 16 SHA1_init(ctx->sha1_ctx); |
42 break; | 17 break; |
43 case SHA256_DIGEST_ALGORITHM: | 18 case SHA256_DIGEST_ALGORITHM: |
44 ctx->sha256_ctx = (SHA256_CTX*) Malloc(sizeof(SHA256_CTX)); | 19 ctx->sha256_ctx = (SHA256_CTX*) Malloc(sizeof(SHA256_CTX)); |
45 SHA256_init(ctx->sha256_ctx); | 20 SHA256_init(ctx->sha256_ctx); |
46 break; | 21 break; |
47 case SHA512_DIGEST_ALGORITHM: | 22 case SHA512_DIGEST_ALGORITHM: |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 break; | 55 break; |
81 case SHA512_DIGEST_ALGORITHM: | 56 case SHA512_DIGEST_ALGORITHM: |
82 digest = (uint8_t*) Malloc(SHA512_DIGEST_SIZE); | 57 digest = (uint8_t*) Malloc(SHA512_DIGEST_SIZE); |
83 Memcpy(digest, SHA512_final(ctx->sha512_ctx), SHA512_DIGEST_SIZE); | 58 Memcpy(digest, SHA512_final(ctx->sha512_ctx), SHA512_DIGEST_SIZE); |
84 Free(ctx->sha512_ctx); | 59 Free(ctx->sha512_ctx); |
85 break; | 60 break; |
86 }; | 61 }; |
87 return digest; | 62 return digest; |
88 } | 63 } |
89 | 64 |
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 | |
111 uint8_t* DigestBuf(const uint8_t* buf, uint64_t len, int sig_algorithm) { | 65 uint8_t* DigestBuf(const uint8_t* buf, uint64_t len, int sig_algorithm) { |
112 uint8_t* digest = (uint8_t*) Malloc(SHA512_DIGEST_SIZE); /* Use the max. */ | 66 uint8_t* digest = (uint8_t*) Malloc(SHA512_DIGEST_SIZE); /* Use the max. */ |
113 /* Define an array mapping [sig_algorithm] to function pointers to the | 67 /* Define an array mapping [sig_algorithm] to function pointers to the |
114 * SHA{1|256|512} functions. | 68 * SHA{1|256|512} functions. |
115 */ | 69 */ |
116 typedef uint8_t* (*Hash_ptr) (const uint8_t*, uint64_t, uint8_t*); | 70 typedef uint8_t* (*Hash_ptr) (const uint8_t*, uint64_t, uint8_t*); |
117 Hash_ptr hash[] = { | 71 Hash_ptr hash[] = { |
118 SHA1, /* RSA 1024 */ | 72 SHA1, /* RSA 1024 */ |
119 SHA256, | 73 SHA256, |
120 SHA512, | 74 SHA512, |
121 SHA1, /* RSA 2048 */ | 75 SHA1, /* RSA 2048 */ |
122 SHA256, | 76 SHA256, |
123 SHA512, | 77 SHA512, |
124 SHA1, /* RSA 4096 */ | 78 SHA1, /* RSA 4096 */ |
125 SHA256, | 79 SHA256, |
126 SHA512, | 80 SHA512, |
127 SHA1, /* RSA 8192 */ | 81 SHA1, /* RSA 8192 */ |
128 SHA256, | 82 SHA256, |
129 SHA512, | 83 SHA512, |
130 }; | 84 }; |
131 /* Call the appropriate hash function. */ | 85 /* Call the appropriate hash function. */ |
132 return hash[sig_algorithm](buf, len, digest); | 86 return hash[sig_algorithm](buf, len, digest); |
133 } | 87 } |
OLD | NEW |