Files
OpenCellular/host/lib/signature_digest.c
Adam Langley 9978e0aa00 vboot: fix name-collision with OpenSSL.
vboot currently uses the |SHA256_CTX| name, which is claimed by OpenSSL.
To work around this, it defines OPENSSL_NO_SHA, but that can't be done
at compile time:

The OPENSSL_NO_* defines are set by OpenSSL to reflect the configuration
that it was built with so that users of OpenSSL can disable features as
needed. They can affect the contents of structures any thus the ABI of
the library.

If these defines are set outside of OpenSSL, then the library and the
code that uses it will have incompatible ABIs. At that point it's only
functioning by blind luck.

This change renames the name-collisions so that this hack isn't needed.
This is the same change as was made internally in cl/85758149.

BUG=none
BRANCH=none
TEST=emerge-samus coreboot; make runtests

Change-Id: I709da2507f341896d89d50129ce30ffb111a20d1
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/263506
Reviewed-by: Randall Spangler <rspangler@chromium.org>
2015-04-02 00:54:07 +00:00

74 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.
*/
#include <openssl/pem.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "cryptolib.h"
#include "host_common.h"
#include "signature_digest.h"
uint8_t* PrependDigestInfo(unsigned int algorithm, uint8_t* digest) {
const int digest_size = hash_size_map[algorithm];
const int digestinfo_size = digestinfo_size_map[algorithm];
const uint8_t* digestinfo = hash_digestinfo_map[algorithm];
uint8_t* p = malloc(digestinfo_size + digest_size);
Memcpy(p, digestinfo, digestinfo_size);
Memcpy(p + digestinfo_size, digest, digest_size);
return p;
}
uint8_t* SignatureDigest(const uint8_t* buf, uint64_t len,
unsigned int algorithm) {
uint8_t* info_digest = NULL;
uint8_t* digest = NULL;
if (algorithm >= kNumAlgorithms) {
VBDEBUG(("SignatureDigest() called with invalid algorithm!\n"));
} else if ((digest = DigestBuf(buf, len, algorithm))) {
info_digest = PrependDigestInfo(algorithm, digest);
}
free(digest);
return info_digest;
}
uint8_t* SignatureBuf(const uint8_t* buf, uint64_t len, const char* key_file,
unsigned int algorithm) {
FILE* key_fp = NULL;
RSA* key = NULL;
uint8_t* signature = NULL;
uint8_t* signature_digest = SignatureDigest(buf, len, algorithm);
int signature_digest_len = (hash_size_map[algorithm] +
digestinfo_size_map[algorithm]);
key_fp = fopen(key_file, "r");
if (!key_fp) {
VBDEBUG(("SignatureBuf(): Couldn't open key file: %s\n", key_file));
free(signature_digest);
return NULL;
}
if ((key = PEM_read_RSAPrivateKey(key_fp, NULL, NULL, NULL)))
signature = (uint8_t*) malloc(siglen_map[algorithm]);
else
VBDEBUG(("SignatureBuf(): Couldn't read private key from file: %s\n",
key_file));
if (signature) {
if (-1 == RSA_private_encrypt(signature_digest_len, /* Input length. */
signature_digest, /* Input data. */
signature, /* Output signature. */
key, /* Key to use. */
RSA_PKCS1_PADDING)) /* Padding to use. */
VBDEBUG(("SignatureBuf(): RSA_private_encrypt() failed.\n"));
}
fclose(key_fp);
if (key)
RSA_free(key);
free(signature_digest);
return signature;
}