Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(243)

Side by Side Diff: src/platform/vboot_reference/tests/verify_data.c

Issue 558025: Refactor SHA*_file functions into a separate file. Generate them using a C macro. (Closed)
Patch Set: Switched back from a macro to duplicate code. Created 10 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/platform/vboot_reference/tests/verify_data.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 5
6 /* Routines for verifying a file's signature. Useful in testing the core 6 /* Routines for verifying a file's signature. Useful in testing the core
7 * RSA verification implementation. 7 * RSA verification implementation.
8 */ 8 */
9 9
10 #include <fcntl.h> 10 #include <fcntl.h>
11 #include <stdio.h> 11 #include <stdio.h>
12 #include <stdlib.h> 12 #include <stdlib.h>
13 #include <string.h> 13 #include <string.h>
14 #include <sys/stat.h> 14 #include <sys/stat.h>
15 #include <sys/types.h> 15 #include <sys/types.h>
16 #include <unistd.h> 16 #include <unistd.h>
17 17
18 #include "digest_utility.h"
18 #include "padding.h" 19 #include "padding.h"
19 #include "rsa.h" 20 #include "rsa.h"
20 #include "sha.h"
21 #include "verify_data.h" 21 #include "verify_data.h"
22 22
23
24 RSAPublicKey* read_RSAkey(char *input_file, int len) { 23 RSAPublicKey* read_RSAkey(char *input_file, int len) {
25 int key_fd; 24 int key_fd;
26 RSAPublicKey *key = NULL; 25 RSAPublicKey *key = NULL;
27 26
28 if ((key_fd = open(input_file, O_RDONLY)) == -1) { 27 if ((key_fd = open(input_file, O_RDONLY)) == -1) {
29 fprintf(stderr, "Couldn't open pre-processed key file\n"); 28 fprintf(stderr, "Couldn't open pre-processed key file\n");
30 return NULL; 29 return NULL;
31 } 30 }
32 31
33 key = (RSAPublicKey *) malloc(sizeof(RSAPublicKey)); 32 key = (RSAPublicKey *) malloc(sizeof(RSAPublicKey));
(...skipping 29 matching lines...) Expand all
63 fprintf(stderr, "%d,", key->rr[i]); 62 fprintf(stderr, "%d,", key->rr[i]);
64 } 63 }
65 fprintf(stderr, "\n"); 64 fprintf(stderr, "\n");
66 } 65 }
67 #endif 66 #endif
68 67
69 close(key_fd); 68 close(key_fd);
70 return key; 69 return key;
71 } 70 }
72 71
73 uint8_t* SHA1_file(char *input_file) {
74 int i, input_fd, len;
75 uint8_t data[SHA1_BLOCK_SIZE], *digest = NULL, *p = NULL;
76 SHA1_CTX ctx;
77
78 if( (input_fd = open(input_file, O_RDONLY)) == -1 ) {
79 fprintf(stderr, "Couldn't open input file.\n");
80 return NULL;
81 }
82
83 /* Calculate SHA1 hash of input blocks, reading one block at a time. */
84 SHA1_init(&ctx);
85 while ( (len = read(input_fd, data, SHA1_BLOCK_SIZE)) == SHA1_BLOCK_SIZE)
86 SHA1_update(&ctx, data, len);
87 if (len != -1)
88 SHA1_update(&ctx, data, len);
89 p = SHA1_final(&ctx);
90 close(input_fd);
91
92 digest = (uint8_t*) malloc(SHA1_DIGEST_SIZE);
93 if (!digest)
94 return NULL;
95 for (i=0; i < SHA1_DIGEST_SIZE; i++)
96 digest[i] = *p++;
97
98 return digest;
99 }
100
101 uint8_t* SHA256_file(char *input_file) {
102 int i, input_fd, len;
103 uint8_t data[SHA256_BLOCK_SIZE], *digest = NULL, *p = NULL;
104 SHA256_CTX ctx;
105
106 if( (input_fd = open(input_file, O_RDONLY)) == -1 ) {
107 fprintf(stderr, "Couldn't open input file.\n");
108 return NULL;
109 }
110
111 /* Calculate SHA256 hash of file, reading one block at a time. */
112 SHA256_init(&ctx);
113 while ( (len = read(input_fd, data, SHA256_BLOCK_SIZE)) == SHA256_BLOCK_SIZE)
114 SHA256_update(&ctx, data, len);
115 if (len != -1)
116 SHA256_update(&ctx, data, len);
117 p = SHA256_final(&ctx);
118 close(input_fd);
119
120 digest = (uint8_t*) malloc(SHA256_DIGEST_SIZE);
121 if (!digest)
122 return NULL;
123 for (i=0; i < SHA256_DIGEST_SIZE; i++)
124 digest[i] = *p++;
125
126 return digest;
127 }
128
129 uint8_t* SHA512_file(char* input_file) {
130 int input_fd;
131 uint8_t data[SHA512_BLOCK_SIZE], *digest = NULL, *p = NULL;
132 int i, len;
133 SHA512_CTX ctx;
134
135 if( (input_fd = open(input_file, O_RDONLY)) == -1 ) {
136 fprintf(stderr, "Couldn't open input file.\n");
137 return NULL;
138 }
139
140 /* Calculate SHA512 hash of file, reading one block at a time. */
141 SHA512_init(&ctx);
142 while ( (len = read(input_fd, data, SHA512_BLOCK_SIZE)) == SHA512_BLOCK_SIZE)
143 SHA512_update(&ctx, data, len);
144 if (len != -1)
145 SHA512_update(&ctx, data, len);
146 p = SHA512_final(&ctx);
147 close(input_fd);
148
149 digest = (uint8_t*) malloc(SHA512_DIGEST_SIZE);
150 if (!digest)
151 return NULL;
152 for (i=0; i < SHA512_DIGEST_SIZE; i++)
153 digest[i] = *p++;
154
155 return digest;
156 }
157
158
159 uint8_t* calculate_digest(char *input_file, int algorithm) {
160 typedef uint8_t* (*Hash_file_ptr) (char*);
161 Hash_file_ptr hash_file[] = {
162 SHA1_file, /* RSA 1024 */
163 SHA256_file,
164 SHA512_file,
165 SHA1_file, /* RSA 2048 */
166 SHA256_file,
167 SHA512_file,
168 SHA1_file, /* RSA 4096 */
169 SHA256_file,
170 SHA512_file,
171 SHA1_file, /* RSA 8192 */
172 SHA256_file,
173 SHA512_file,
174 };
175 return hash_file[algorithm](input_file);
176 }
177
178 uint8_t* read_signature(char *input_file, int len) { 72 uint8_t* read_signature(char *input_file, int len) {
179 int i, sigfd; 73 int i, sigfd;
180 uint8_t *signature = NULL; 74 uint8_t *signature = NULL;
181 if ((sigfd = open(input_file, O_RDONLY)) == -1) { 75 if ((sigfd = open(input_file, O_RDONLY)) == -1) {
182 fprintf(stderr, "Couldn't open signature file\n"); 76 fprintf(stderr, "Couldn't open signature file\n");
183 return NULL; 77 return NULL;
184 } 78 }
185 79
186 /* Read the signature into a buffer*/ 80 /* Read the signature into a buffer*/
187 signature = (uint8_t*) malloc(len); 81 signature = (uint8_t*) malloc(len);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 else 128 else
235 fprintf(stderr, "Signature Verification FAILED!\n"); 129 fprintf(stderr, "Signature Verification FAILED!\n");
236 130
237 failure: 131 failure:
238 free(key); 132 free(key);
239 free(signature); 133 free(signature);
240 free(digest); 134 free(digest);
241 135
242 return 0; 136 return 0;
243 } 137 }
OLDNEW
« no previous file with comments | « src/platform/vboot_reference/tests/verify_data.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698