mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2025-11-25 18:55:24 +00:00
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:
@@ -2,7 +2,7 @@
|
|||||||
# Use of this source code is governed by a BSD-style license that can be
|
# Use of this source code is governed by a BSD-style license that can be
|
||||||
# found in the LICENSE file.
|
# 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)
|
OBJS=$(SRCS:.c=.o)
|
||||||
|
|
||||||
all: libcrypto.a
|
all: libcrypto.a
|
||||||
|
|||||||
46
crypto/rsa_utility.c
Normal file
46
crypto/rsa_utility.c
Normal 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
21
include/rsa_utility.h
Normal 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_ */
|
||||||
@@ -18,60 +18,43 @@
|
|||||||
#include "digest_utility.h"
|
#include "digest_utility.h"
|
||||||
#include "padding.h"
|
#include "padding.h"
|
||||||
#include "rsa.h"
|
#include "rsa.h"
|
||||||
|
#include "rsa_utility.h"
|
||||||
#include "verify_data.h"
|
#include "verify_data.h"
|
||||||
|
|
||||||
RSAPublicKey* read_RSAkey(char *input_file, int len) {
|
RSAPublicKey* read_RSAkey(char* input_file, int len) {
|
||||||
int key_fd;
|
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) {
|
if ((key_fd = open(input_file, O_RDONLY)) == -1) {
|
||||||
fprintf(stderr, "Couldn't open pre-processed key file\n");
|
fprintf(stderr, "Couldn't open pre-processed key file\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
key = (RSAPublicKey *) malloc(sizeof(RSAPublicKey));
|
if (-1 == fstat(key_fd, &stat_fd)) {
|
||||||
if (!key)
|
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;
|
return NULL;
|
||||||
|
|
||||||
/* Read the pre-processed RSA key into a RSAPublicKey structure */
|
if (buf_len != read(key_fd, buf, buf_len)) {
|
||||||
/* TODO(gauravsh): Add error checking here? */
|
fprintf(stderr, "Couldn't read key into a buffer.\n");
|
||||||
|
return NULL;
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
close(key_fd);
|
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;
|
int i, sigfd;
|
||||||
uint8_t *signature = NULL;
|
uint8_t* signature = NULL;
|
||||||
if ((sigfd = open(input_file, O_RDONLY)) == -1) {
|
if ((sigfd = open(input_file, O_RDONLY)) == -1) {
|
||||||
fprintf(stderr, "Couldn't open signature file\n");
|
fprintf(stderr, "Couldn't open signature file\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -96,7 +79,8 @@ uint8_t* read_signature(char *input_file, int len) {
|
|||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
int i, algorithm, sig_len;
|
int i, algorithm, sig_len;
|
||||||
uint8_t *digest = NULL, *signature = NULL;
|
uint8_t* digest = NULL;
|
||||||
|
uint8_t* signature = NULL;
|
||||||
RSAPublicKey* key = NULL;
|
RSAPublicKey* key = NULL;
|
||||||
|
|
||||||
if (argc!=5) {
|
if (argc!=5) {
|
||||||
|
|||||||
Reference in New Issue
Block a user