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
This commit is contained in:
Gaurav Shah
2010-02-05 11:44:58 -08:00
parent d067712ff9
commit e178fd9e34
4 changed files with 90 additions and 39 deletions

View File

@@ -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

46
crypto/rsa_utility.c Normal file
View File

@@ -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;
}

21
include/rsa_utility.h Normal file
View File

@@ -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_ */

View File

@@ -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 @@ uint8_t* read_signature(char *input_file, int len) {
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) {