Add some convenience/helper functions for RSA. Modify test utility to use the new function.

BUG=670
TEST=RSA verification test using the convenience function is passes.

Review URL: http://codereview.chromium.org/575019
diff --git a/crypto/Makefile b/crypto/Makefile
index 201085e..ff4326d 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -2,7 +2,7 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-SRCS=rsa.c sha1.c sha2.c padding.c
+SRCS=rsa.c sha1.c sha2.c padding.c rsa_utility.c
 OBJS=$(SRCS:.c=.o)
 
 all:	libcrypto.a
diff --git a/crypto/rsa_utility.c b/crypto/rsa_utility.c
new file mode 100644
index 0000000..2215b7c
--- /dev/null
+++ b/crypto/rsa_utility.c
@@ -0,0 +1,46 @@
+/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ * Utility functions for message digest functions.
+ */
+
+#include "padding.h"
+#include "rsa_utility.h"
+#include "utility.h"
+
+int RSAProcessedKeySize(int algorithm) {
+  int key_len = siglen_map[algorithm] * sizeof(uint32_t);  /* 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(int) + sizeof(uint32_t));
+}
+
+RSAPublicKey* RSAPublicKeyFromBuf(uint8_t* buf, int len) {
+  RSAPublicKey* key = (RSAPublicKey*) Malloc(sizeof(RSAPublicKey));
+  MemcpyState st;
+  int key_len;
+
+  st.remaining_buf = buf;
+  st.remaining_len = len;
+
+  StatefulMemcpy(&st, &key->len, sizeof(key->len));
+  key_len = key->len * sizeof(uint32_t);  /* key length in bytes. */
+  key->n = (uint32_t*) Malloc(key_len);
+  key->rr = (uint32_t*) Malloc(key_len);
+
+  StatefulMemcpy(&st, &key->n0inv, sizeof(key->n0inv));
+  StatefulMemcpy(&st, key->n, key_len);
+  StatefulMemcpy(&st, key->rr, key_len);
+  if (st.remaining_len != 0) {  /* Underrun or overrun. */
+    Free(key->n);
+    Free(key->rr);
+    Free(key);
+    return NULL;
+  }
+
+  return key;
+}
diff --git a/include/rsa_utility.h b/include/rsa_utility.h
new file mode 100644
index 0000000..808d17a
--- /dev/null
+++ b/include/rsa_utility.h
@@ -0,0 +1,21 @@
+/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ * Some utility functions for use with RSA signature verification.
+ */
+
+#ifndef VBOOT_REFERENCE_RSA_UTILITY_H_
+#define VBOOT_REFERENCE_RSA_UTILITY_H_
+
+#include "rsa.h"
+
+/* Returns the size of a pre-processed RSA public key in bytes with algorithm
+ * [algorithm]. */
+int RSAProcessedKeySize(int algorithm);
+
+/* Create a RSAPublic key structure from binary blob [buf] of length
+ * [len]. */
+RSAPublicKey* RSAPublicKeyFromBuf(uint8_t* buf, int len);
+
+#endif  /* VBOOT_REFERENCE_RSA_UTILITY_H_ */
diff --git a/tests/verify_data.c b/tests/verify_data.c
index c183b3b..ac51daf 100644
--- a/tests/verify_data.c
+++ b/tests/verify_data.c
@@ -18,60 +18,43 @@
 #include "digest_utility.h"
 #include "padding.h"
 #include "rsa.h"
+#include "rsa_utility.h"
 #include "verify_data.h"
 
-RSAPublicKey* read_RSAkey(char *input_file, int len) {
+RSAPublicKey* read_RSAkey(char* input_file, int len) {
   int key_fd;
-  RSAPublicKey *key = NULL;
+  int buf_len;
+  struct stat stat_fd;
+  uint8_t* buf = NULL;
 
   if ((key_fd = open(input_file, O_RDONLY)) == -1) {
     fprintf(stderr, "Couldn't open pre-processed key file\n");
     return NULL;
   }
 
-  key = (RSAPublicKey *) malloc(sizeof(RSAPublicKey));
-  if (!key)
+  if (-1 == fstat(key_fd, &stat_fd)) {
+    fprintf(stderr, "Couldn't stat key file\n");
+    return NULL;
+  }
+  buf_len = stat_fd.st_size;
+
+  /* Read entire key binary blob into a buffer. */
+  buf = (uint8_t*) malloc(buf_len);
+  if (!buf)
     return NULL;
 
-  /* Read the pre-processed RSA key into a RSAPublicKey structure */
-  /* TODO(gauravsh): Add error checking here? */
-
-  read(key_fd, &key->len, sizeof(key->len));
-  read(key_fd, &key->n0inv, sizeof(key->n0inv));
-
-#ifndef NDEBUG
-  fprintf(stderr, "%d\n", key->len);
-  fprintf(stderr, "%d\n", key->n0inv);
-#endif
-
-  key->n = (uint32_t *) malloc(len);
-  read(key_fd, key->n, len);
-
-  key->rr = (uint32_t *) malloc(len);
-  read(key_fd, key->rr, len);
-
-#ifndef NDEBUG
-  {
-    int i;
-    for(i=0; i<key->len; i++) {
-      fprintf(stderr, "%d,", key->n[i]);
-    }
-    fprintf(stderr, "\n");
-
-    for(i=0; i<key->len; i++) {
-      fprintf(stderr, "%d,", key->rr[i]);
-    }
-    fprintf(stderr, "\n");
+  if (buf_len != read(key_fd, buf, buf_len)) {
+    fprintf(stderr, "Couldn't read key into a buffer.\n");
+    return NULL;
   }
-#endif
 
   close(key_fd);
-  return key;
+  return RSAPublicKeyFromBuf(buf, buf_len);
 }
 
-uint8_t* read_signature(char *input_file, int len) {
+uint8_t* read_signature(char* input_file, int len) {
   int i, sigfd;
-  uint8_t *signature = NULL;
+  uint8_t* signature = NULL;
   if ((sigfd = open(input_file, O_RDONLY)) == -1) {
     fprintf(stderr, "Couldn't open signature file\n");
     return NULL;
@@ -96,7 +79,8 @@
 
 int main(int argc, char* argv[]) {
   int i, algorithm, sig_len;
-  uint8_t *digest = NULL, *signature = NULL;
+  uint8_t* digest = NULL;
+  uint8_t* signature = NULL;
   RSAPublicKey* key = NULL;
 
   if (argc!=5) {