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

Unified Diff: firmware/lib/cryptolib/rsa_utility.c

Issue 3136017: Add additional sanity checks to RSA verification code. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/vboot_reference.git
Patch Set: Created 10 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « firmware/lib/cryptolib/rsa.c ('k') | firmware/lib/vboot_common.c » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: firmware/lib/cryptolib/rsa_utility.c
diff --git a/firmware/lib/cryptolib/rsa_utility.c b/firmware/lib/cryptolib/rsa_utility.c
index c3cf50ef42d2a87af69f0c3d7a75460ed2d9f50a..3b82c061e4ce6fddf181c76b64602a007938eb2a 100644
--- a/firmware/lib/cryptolib/rsa_utility.c
+++ b/firmware/lib/cryptolib/rsa_utility.c
@@ -9,14 +9,18 @@
#include "stateful_util.h"
#include "utility.h"
-int RSAProcessedKeySize(int algorithm) {
- int key_len = siglen_map[algorithm]; /* Key length in
- * bytes. */
- /* Total size needed by a RSAPublicKey structure is =
- * 2 * key_len bytes for the n and rr arrays
- * + sizeof len + sizeof n0inv.
- */
- return (2 * key_len + sizeof(uint32_t) + sizeof(uint32_t));
+int RSAProcessedKeySize(unsigned int algorithm, int* out_size) {
+ int key_len; /* Key length in bytes. */
+ if (algorithm < kNumAlgorithms) {
+ key_len = siglen_map[algorithm];
+ /* Total size needed by a RSAPublicKey structure is =
+ * 2 * key_len bytes for the n and rr arrays
+ * + sizeof len + sizeof n0inv.
+ */
+ *out_size = (2 * key_len + sizeof(uint32_t) + sizeof(uint32_t));
+ return 1;
+ }
+ return 0;
}
RSAPublicKey* RSAPublicKeyNew(void) {
@@ -74,7 +78,7 @@ int RSAVerifyBinary_f(const uint8_t* key_blob,
const uint8_t* buf,
uint64_t len,
const uint8_t* sig,
- int algorithm) {
+ unsigned int algorithm) {
RSAPublicKey* verification_key = NULL;
uint8_t* digest = NULL;
int key_size;
@@ -83,7 +87,8 @@ int RSAVerifyBinary_f(const uint8_t* key_blob,
if (algorithm >= kNumAlgorithms)
return 0; /* Invalid algorithm. */
- key_size = RSAProcessedKeySize(algorithm);
+ if (!RSAProcessedKeySize(algorithm, &key_size))
+ return 0;
sig_size = siglen_map[algorithm];
if (key_blob && !key)
@@ -93,6 +98,10 @@ int RSAVerifyBinary_f(const uint8_t* key_blob,
else
return 0; /* Both can't be NULL or non-NULL. */
+ /* Ensure we have a valid key. */
+ if (!verification_key)
+ return 0;
+
digest = DigestBuf(buf, len, algorithm);
success = RSAVerify(verification_key, sig, (uint32_t)sig_size,
(uint8_t)algorithm, digest);
@@ -109,7 +118,7 @@ int RSAVerifyBinaryWithDigest_f(const uint8_t* key_blob,
const RSAPublicKey* key,
const uint8_t* digest,
const uint8_t* sig,
- int algorithm) {
+ unsigned int algorithm) {
RSAPublicKey* verification_key = NULL;
int key_size;
int sig_size;
@@ -117,7 +126,8 @@ int RSAVerifyBinaryWithDigest_f(const uint8_t* key_blob,
if (algorithm >= kNumAlgorithms)
return 0; /* Invalid algorithm. */
- key_size = RSAProcessedKeySize(algorithm);
+ if (!RSAProcessedKeySize(algorithm, &key_size))
+ return 0;
sig_size = siglen_map[algorithm];
if (key_blob && !key)
@@ -127,6 +137,10 @@ int RSAVerifyBinaryWithDigest_f(const uint8_t* key_blob,
else
return 0; /* Both can't be NULL or non-NULL. */
+ /* Ensure we have a valid key. */
+ if (!verification_key)
+ return 0;
+
success = RSAVerify(verification_key, sig, (uint32_t)sig_size,
(uint8_t)algorithm, digest);
« no previous file with comments | « firmware/lib/cryptolib/rsa.c ('k') | firmware/lib/vboot_common.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698