mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2025-11-24 02:05:01 +00:00
This CL does the following: 1) It adds a SignatureBuf function which uses the OpenSSL library to generate RSA signature. This is more robust than the previous way of invoking the command line "openssl" utility and capturing its output. No more unnecessary temporary files for signature operations. 2) It adds functions that allow direct manipulation of binary verified Firmware and Kernel Image blobs in memory. 3) It changes the structure field members for FirmwareImage to make it consistent with KernelImage. Now it's clearer which key is used when. 4) Minor bug fixes and slightly improved API for dealing verified boot firmware and kernel images. 5) Renames the RSA_verify function to prevent conflicts with OpenSSL since it's linked into the firmware utility binary. Review URL: http://codereview.chromium.org/661353
89 lines
2.5 KiB
C
89 lines
2.5 KiB
C
/* 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 "sha_utility.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(int) + sizeof(uint32_t));
|
|
}
|
|
|
|
void RSAPublicKeyFree(RSAPublicKey* key) {
|
|
if (key) {
|
|
Free(key->n);
|
|
Free(key->rr);
|
|
Free(key);
|
|
}
|
|
}
|
|
|
|
RSAPublicKey* RSAPublicKeyFromBuf(const uint8_t* buf, int len) {
|
|
RSAPublicKey* key = (RSAPublicKey*) Malloc(sizeof(RSAPublicKey));
|
|
MemcpyState st;
|
|
int key_len;
|
|
|
|
st.remaining_buf = (uint8_t*) 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;
|
|
}
|
|
|
|
int RSAVerifyBinary_f(const uint8_t* key_blob,
|
|
const RSAPublicKey* key,
|
|
const uint8_t* buf,
|
|
int len,
|
|
const uint8_t* sig,
|
|
int algorithm) {
|
|
RSAPublicKey* verification_key = NULL;
|
|
uint8_t* digest = NULL;
|
|
int key_size;
|
|
int sig_size;
|
|
int success;
|
|
|
|
if (algorithm >= kNumAlgorithms)
|
|
return 0; /* Invalid algorithm. */
|
|
key_size = RSAProcessedKeySize(algorithm);
|
|
sig_size = siglen_map[algorithm];
|
|
|
|
if (key_blob && !key)
|
|
verification_key = RSAPublicKeyFromBuf(key_blob, key_size);
|
|
else if (!key_blob && key)
|
|
verification_key = (RSAPublicKey*) key; /* Supress const warning. */
|
|
else
|
|
return 0; /* Both can't be NULL or non-NULL. */
|
|
|
|
digest = DigestBuf(buf, len, algorithm);
|
|
success = RSAVerify(verification_key, sig, sig_size, algorithm, digest);
|
|
|
|
Free(digest);
|
|
if (!key)
|
|
RSAPublicKeyFree(verification_key); /* Only free if we allocated it. */
|
|
return success;
|
|
}
|