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

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

Issue 575019: Add some convenience/helper functions for RSA. (Closed)
Patch Set: Add comment to size calculation. 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/include/rsa_utility.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 "digest_utility.h"
19 #include "padding.h" 19 #include "padding.h"
20 #include "rsa.h" 20 #include "rsa.h"
21 #include "rsa_utility.h"
21 #include "verify_data.h" 22 #include "verify_data.h"
22 23
23 RSAPublicKey* read_RSAkey(char *input_file, int len) { 24 RSAPublicKey* read_RSAkey(char* input_file, int len) {
24 int key_fd; 25 int key_fd;
25 RSAPublicKey *key = NULL; 26 int buf_len;
27 struct stat stat_fd;
28 uint8_t* buf = NULL;
26 29
27 if ((key_fd = open(input_file, O_RDONLY)) == -1) { 30 if ((key_fd = open(input_file, O_RDONLY)) == -1) {
28 fprintf(stderr, "Couldn't open pre-processed key file\n"); 31 fprintf(stderr, "Couldn't open pre-processed key file\n");
29 return NULL; 32 return NULL;
30 } 33 }
31 34
32 key = (RSAPublicKey *) malloc(sizeof(RSAPublicKey)); 35 if (-1 == fstat(key_fd, &stat_fd)) {
33 if (!key) 36 fprintf(stderr, "Couldn't stat key file\n");
37 return NULL;
38 }
39 buf_len = stat_fd.st_size;
40
41 /* Read entire key binary blob into a buffer. */
42 buf = (uint8_t*) malloc(buf_len);
43 if (!buf)
34 return NULL; 44 return NULL;
35 45
36 /* Read the pre-processed RSA key into a RSAPublicKey structure */ 46 if (buf_len != read(key_fd, buf, buf_len)) {
37 /* TODO(gauravsh): Add error checking here? */ 47 fprintf(stderr, "Couldn't read key into a buffer.\n");
38 48 return NULL;
39 read(key_fd, &key->len, sizeof(key->len));
40 read(key_fd, &key->n0inv, sizeof(key->n0inv));
41
42 #ifndef NDEBUG
43 fprintf(stderr, "%d\n", key->len);
44 fprintf(stderr, "%d\n", key->n0inv);
45 #endif
46
47 key->n = (uint32_t *) malloc(len);
48 read(key_fd, key->n, len);
49
50 key->rr = (uint32_t *) malloc(len);
51 read(key_fd, key->rr, len);
52
53 #ifndef NDEBUG
54 {
55 int i;
56 for(i=0; i<key->len; i++) {
57 fprintf(stderr, "%d,", key->n[i]);
58 }
59 fprintf(stderr, "\n");
60
61 for(i=0; i<key->len; i++) {
62 fprintf(stderr, "%d,", key->rr[i]);
63 }
64 fprintf(stderr, "\n");
65 } 49 }
66 #endif
67 50
68 close(key_fd); 51 close(key_fd);
69 return key; 52 return RSAPublicKeyFromBuf(buf, buf_len);
70 } 53 }
71 54
72 uint8_t* read_signature(char *input_file, int len) { 55 uint8_t* read_signature(char* input_file, int len) {
73 int i, sigfd; 56 int i, sigfd;
74 uint8_t *signature = NULL; 57 uint8_t* signature = NULL;
75 if ((sigfd = open(input_file, O_RDONLY)) == -1) { 58 if ((sigfd = open(input_file, O_RDONLY)) == -1) {
76 fprintf(stderr, "Couldn't open signature file\n"); 59 fprintf(stderr, "Couldn't open signature file\n");
77 return NULL; 60 return NULL;
78 } 61 }
79 62
80 /* Read the signature into a buffer*/ 63 /* Read the signature into a buffer*/
81 signature = (uint8_t*) malloc(len); 64 signature = (uint8_t*) malloc(len);
82 if (!signature) 65 if (!signature)
83 return NULL; 66 return NULL;
84 67
85 if( (i = read(sigfd, signature, len)) != len ) { 68 if( (i = read(sigfd, signature, len)) != len ) {
86 fprintf(stderr, "Wrong signature length - Expected = %d, Received = %d\n", 69 fprintf(stderr, "Wrong signature length - Expected = %d, Received = %d\n",
87 len, i); 70 len, i);
88 close(sigfd); 71 close(sigfd);
89 return NULL; 72 return NULL;
90 } 73 }
91 74
92 close(sigfd); 75 close(sigfd);
93 return signature; 76 return signature;
94 } 77 }
95 78
96 79
97 int main(int argc, char* argv[]) { 80 int main(int argc, char* argv[]) {
98 int i, algorithm, sig_len; 81 int i, algorithm, sig_len;
99 uint8_t *digest = NULL, *signature = NULL; 82 uint8_t* digest = NULL;
sosa 2010/02/05 19:37:59 Is this extra whitespace?
gauravsh 2010/02/05 19:40:45 No, I think it's just the diff, as I split the dec
83 uint8_t* signature = NULL;
100 RSAPublicKey* key = NULL; 84 RSAPublicKey* key = NULL;
101 85
102 if (argc!=5) { 86 if (argc!=5) {
103 fprintf(stderr, "Usage: %s <algorithm> <key file> <signature file>" 87 fprintf(stderr, "Usage: %s <algorithm> <key file> <signature file>"
104 " <input file>\n\n", argv[0]); 88 " <input file>\n\n", argv[0]);
105 fprintf(stderr, "where <algorithm> depends on the signature algorithm" 89 fprintf(stderr, "where <algorithm> depends on the signature algorithm"
106 " used:\n"); 90 " used:\n");
107 for(i = 0; i<kNumAlgorithms; i++) 91 for(i = 0; i<kNumAlgorithms; i++)
108 fprintf(stderr, "\t%d for %s\n", i, algo_strings[i]); 92 fprintf(stderr, "\t%d for %s\n", i, algo_strings[i]);
109 return -1; 93 return -1;
(...skipping 18 matching lines...) Expand all
128 else 112 else
129 fprintf(stderr, "Signature Verification FAILED!\n"); 113 fprintf(stderr, "Signature Verification FAILED!\n");
130 114
131 failure: 115 failure:
132 free(key); 116 free(key);
133 free(signature); 117 free(signature);
134 free(digest); 118 free(digest);
135 119
136 return 0; 120 return 0;
137 } 121 }
OLDNEW
« no previous file with comments | « src/platform/vboot_reference/include/rsa_utility.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698