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

Side by Side Diff: firmware/lib/cryptolib/rsa_utility.c

Issue 6733018: Use uint64_t and avoid down casting as much as possible. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/vboot_reference.git@master
Patch Set: Created 9 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « firmware/lib/cryptolib/include/rsa.h ('k') | firmware/lib/vboot_common.c » ('j') | 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 * Implementation of RSA utility functions. 5 * Implementation of RSA utility functions.
6 */ 6 */
7 7
8 #include "cryptolib.h" 8 #include "cryptolib.h"
9 #include "stateful_util.h" 9 #include "stateful_util.h"
10 #include "utility.h" 10 #include "utility.h"
11 11
12 int RSAProcessedKeySize(unsigned int algorithm, int* out_size) { 12 uint64_t RSAProcessedKeySize(uint64_t algorithm, uint64_t* out_size) {
13 int key_len; /* Key length in bytes. */ 13 uint64_t key_len; /* Key length in bytes. */
14 if (algorithm < (unsigned int)kNumAlgorithms) { 14 if (algorithm < kNumAlgorithms) {
15 key_len = siglen_map[algorithm]; 15 key_len = siglen_map[algorithm];
16 /* Total size needed by a RSAPublicKey structure is = 16 /* Total size needed by a RSAPublicKey structure is =
17 * 2 * key_len bytes for the n and rr arrays 17 * 2 * key_len bytes for the n and rr arrays
18 * + sizeof len + sizeof n0inv. 18 * + sizeof len + sizeof n0inv.
19 */ 19 */
20 *out_size = (2 * key_len + sizeof(uint32_t) + sizeof(uint32_t)); 20 *out_size = (2 * key_len + sizeof(uint32_t) + sizeof(uint32_t));
21 return 1; 21 return 1;
22 } 22 }
23 return 0; 23 return 0;
24 } 24 }
25 25
26 RSAPublicKey* RSAPublicKeyNew(void) { 26 RSAPublicKey* RSAPublicKeyNew(void) {
27 RSAPublicKey* key = (RSAPublicKey*) Malloc(sizeof(RSAPublicKey)); 27 RSAPublicKey* key = (RSAPublicKey*) Malloc(sizeof(RSAPublicKey));
28 key->n = NULL; 28 key->n = NULL;
29 key->rr = NULL; 29 key->rr = NULL;
30 return key; 30 return key;
31 } 31 }
32 32
33 void RSAPublicKeyFree(RSAPublicKey* key) { 33 void RSAPublicKeyFree(RSAPublicKey* key) {
34 if (key) { 34 if (key) {
35 Free(key->n); 35 Free(key->n);
36 Free(key->rr); 36 Free(key->rr);
37 Free(key); 37 Free(key);
38 } 38 }
39 } 39 }
40 40
41 RSAPublicKey* RSAPublicKeyFromBuf(const uint8_t* buf, int len) { 41 RSAPublicKey* RSAPublicKeyFromBuf(const uint8_t* buf, uint64_t len) {
42 RSAPublicKey* key = RSAPublicKeyNew(); 42 RSAPublicKey* key = RSAPublicKeyNew();
43 MemcpyState st; 43 MemcpyState st;
44 int key_len; 44 uint64_t key_len;
45 45
46 st.remaining_buf = (uint8_t*) buf; 46 st.remaining_buf = (uint8_t*) buf;
47 st.remaining_len = len; 47 st.remaining_len = len;
48 st.overrun = 0; 48 st.overrun = 0;
49 49
50 StatefulMemcpy(&st, &key->len, sizeof(key->len)); 50 StatefulMemcpy(&st, &key->len, sizeof(key->len));
51 key_len = key->len * sizeof(uint32_t); /* key length in bytes. */ 51 key_len = key->len * sizeof(uint32_t); /* key length in bytes. */
52 52
53 /* Sanity Check the key length. */ 53 /* Sanity Check the key length. */
54 if (RSA1024NUMBYTES != key_len && 54 if (RSA1024NUMBYTES != key_len &&
(...skipping 19 matching lines...) Expand all
74 } 74 }
75 75
76 int RSAVerifyBinary_f(const uint8_t* key_blob, 76 int RSAVerifyBinary_f(const uint8_t* key_blob,
77 const RSAPublicKey* key, 77 const RSAPublicKey* key,
78 const uint8_t* buf, 78 const uint8_t* buf,
79 uint64_t len, 79 uint64_t len,
80 const uint8_t* sig, 80 const uint8_t* sig,
81 unsigned int algorithm) { 81 unsigned int algorithm) {
82 RSAPublicKey* verification_key = NULL; 82 RSAPublicKey* verification_key = NULL;
83 uint8_t* digest = NULL; 83 uint8_t* digest = NULL;
84 int key_size; 84 uint64_t key_size;
85 int sig_size; 85 int sig_size;
86 int success; 86 int success;
87 87
88 if (algorithm >= (unsigned int)kNumAlgorithms) 88 if (algorithm >= (unsigned int)kNumAlgorithms)
89 return 0; /* Invalid algorithm. */ 89 return 0; /* Invalid algorithm. */
90 if (!RSAProcessedKeySize(algorithm, &key_size)) 90 if (!RSAProcessedKeySize(algorithm, &key_size))
91 return 0; 91 return 0;
92 sig_size = siglen_map[algorithm]; 92 sig_size = siglen_map[algorithm];
93 93
94 if (key_blob && !key) 94 if (key_blob && !key)
(...skipping 18 matching lines...) Expand all
113 } 113 }
114 114
115 /* Version of RSAVerifyBinary_f() where instead of the raw binary blob 115 /* Version of RSAVerifyBinary_f() where instead of the raw binary blob
116 * of data, its digest is passed as the argument. */ 116 * of data, its digest is passed as the argument. */
117 int RSAVerifyBinaryWithDigest_f(const uint8_t* key_blob, 117 int RSAVerifyBinaryWithDigest_f(const uint8_t* key_blob,
118 const RSAPublicKey* key, 118 const RSAPublicKey* key,
119 const uint8_t* digest, 119 const uint8_t* digest,
120 const uint8_t* sig, 120 const uint8_t* sig,
121 unsigned int algorithm) { 121 unsigned int algorithm) {
122 RSAPublicKey* verification_key = NULL; 122 RSAPublicKey* verification_key = NULL;
123 int key_size; 123 uint64_t key_size;
124 int sig_size; 124 int sig_size;
125 int success; 125 int success;
126 126
127 if (algorithm >= (unsigned int)kNumAlgorithms) 127 if (algorithm >= (unsigned int)kNumAlgorithms)
128 return 0; /* Invalid algorithm. */ 128 return 0; /* Invalid algorithm. */
129 if (!RSAProcessedKeySize(algorithm, &key_size)) 129 if (!RSAProcessedKeySize(algorithm, &key_size))
130 return 0; 130 return 0;
131 sig_size = siglen_map[algorithm]; 131 sig_size = siglen_map[algorithm];
132 132
133 if (key_blob && !key) 133 if (key_blob && !key)
134 verification_key = RSAPublicKeyFromBuf(key_blob, key_size); 134 verification_key = RSAPublicKeyFromBuf(key_blob, key_size);
135 else if (!key_blob && key) 135 else if (!key_blob && key)
136 verification_key = (RSAPublicKey*) key; /* Supress const warning. */ 136 verification_key = (RSAPublicKey*) key; /* Supress const warning. */
137 else 137 else
138 return 0; /* Both can't be NULL or non-NULL. */ 138 return 0; /* Both can't be NULL or non-NULL. */
139 139
140 /* Ensure we have a valid key. */ 140 /* Ensure we have a valid key. */
141 if (!verification_key) 141 if (!verification_key)
142 return 0; 142 return 0;
143 143
144 success = RSAVerify(verification_key, sig, (uint32_t)sig_size, 144 success = RSAVerify(verification_key, sig, (uint32_t)sig_size,
145 (uint8_t)algorithm, digest); 145 (uint8_t)algorithm, digest);
146 146
147 if (!key) 147 if (!key)
148 RSAPublicKeyFree(verification_key); /* Only free if we allocated it. */ 148 RSAPublicKeyFree(verification_key); /* Only free if we allocated it. */
149 return success; 149 return success;
150 } 150 }
OLDNEW
« no previous file with comments | « firmware/lib/cryptolib/include/rsa.h ('k') | firmware/lib/vboot_common.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698