mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2025-11-25 18:55:24 +00:00
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>
163 lines
3.5 KiB
C
163 lines
3.5 KiB
C
/* Copyright (c) 2014 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.
|
|
*
|
|
* Miscellaneous functions for userspace vboot utilities.
|
|
*/
|
|
|
|
#include <openssl/bn.h>
|
|
#include <openssl/rsa.h>
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include "cryptolib.h"
|
|
#include "host_common.h"
|
|
#include "util_misc.h"
|
|
#include "vboot_common.h"
|
|
|
|
void PrintPubKeySha1Sum(VbPublicKey *key)
|
|
{
|
|
uint8_t *buf = ((uint8_t *)key) + key->key_offset;
|
|
uint64_t buflen = key->key_size;
|
|
uint8_t *digest = DigestBuf(buf, buflen, SHA1_DIGEST_ALGORITHM);
|
|
int i;
|
|
for (i = 0; i < SHA1_DIGEST_SIZE; i++)
|
|
printf("%02x", digest[i]);
|
|
free(digest);
|
|
}
|
|
|
|
void PrintPrivKeySha1Sum(VbPrivateKey *key)
|
|
{
|
|
uint8_t *buf, *digest;
|
|
uint32_t buflen;
|
|
int i;
|
|
|
|
if (vb_keyb_from_rsa(key->rsa_private_key, &buf, &buflen)) {
|
|
printf("<error>");
|
|
return;
|
|
}
|
|
|
|
digest = DigestBuf(buf, buflen, SHA1_DIGEST_ALGORITHM);
|
|
for (i = 0; i < SHA1_DIGEST_SIZE; i++)
|
|
printf("%02x", digest[i]);
|
|
|
|
free(digest);
|
|
free(buf);
|
|
}
|
|
|
|
int vb_keyb_from_rsa(struct rsa_st *rsa_private_key,
|
|
uint8_t **keyb_data, uint32_t *keyb_size)
|
|
{
|
|
uint32_t i, nwords;
|
|
BIGNUM *N = NULL;
|
|
BIGNUM *Big1 = NULL, *Big2 = NULL, *Big32 = NULL, *BigMinus1 = NULL;
|
|
BIGNUM *B = NULL;
|
|
BIGNUM *N0inv = NULL, *R = NULL, *RR = NULL;
|
|
BIGNUM *RRTemp = NULL, *NnumBits = NULL;
|
|
BIGNUM *n = NULL, *rr = NULL;
|
|
BN_CTX *bn_ctx = BN_CTX_new();
|
|
uint32_t n0invout;
|
|
uint32_t bufsize;
|
|
uint32_t *outbuf;
|
|
int retval = 1;
|
|
|
|
/* Size of RSA key in 32-bit words */
|
|
nwords = BN_num_bits(rsa_private_key->n) / 32;
|
|
|
|
bufsize = (2 + nwords + nwords) * sizeof(uint32_t);
|
|
outbuf = malloc(bufsize);
|
|
if (!outbuf)
|
|
goto done;
|
|
|
|
*keyb_data = (uint8_t *)outbuf;
|
|
*keyb_size = bufsize;
|
|
|
|
*outbuf++ = nwords;
|
|
|
|
/* Initialize BIGNUMs */
|
|
#define NEW_BIGNUM(x) do { x = BN_new(); if (!x) goto done; } while (0)
|
|
NEW_BIGNUM(N);
|
|
NEW_BIGNUM(Big1);
|
|
NEW_BIGNUM(Big2);
|
|
NEW_BIGNUM(Big32);
|
|
NEW_BIGNUM(BigMinus1);
|
|
NEW_BIGNUM(N0inv);
|
|
NEW_BIGNUM(R);
|
|
NEW_BIGNUM(RR);
|
|
NEW_BIGNUM(RRTemp);
|
|
NEW_BIGNUM(NnumBits);
|
|
NEW_BIGNUM(n);
|
|
NEW_BIGNUM(rr);
|
|
NEW_BIGNUM(B);
|
|
#undef NEW_BIGNUM
|
|
|
|
BN_copy(N, rsa_private_key->n);
|
|
BN_set_word(Big1, 1L);
|
|
BN_set_word(Big2, 2L);
|
|
BN_set_word(Big32, 32L);
|
|
BN_sub(BigMinus1, Big1, Big2);
|
|
|
|
BN_exp(B, Big2, Big32, bn_ctx); /* B = 2^32 */
|
|
|
|
/* Calculate and output N0inv = -1 / N[0] mod 2^32 */
|
|
BN_mod_inverse(N0inv, N, B, bn_ctx);
|
|
BN_sub(N0inv, B, N0inv);
|
|
n0invout = BN_get_word(N0inv);
|
|
|
|
*outbuf++ = n0invout;
|
|
|
|
/* Calculate R = 2^(# of key bits) */
|
|
BN_set_word(NnumBits, BN_num_bits(N));
|
|
BN_exp(R, Big2, NnumBits, bn_ctx);
|
|
|
|
/* Calculate RR = R^2 mod N */
|
|
BN_copy(RR, R);
|
|
BN_mul(RRTemp, RR, R, bn_ctx);
|
|
BN_mod(RR, RRTemp, N, bn_ctx);
|
|
|
|
|
|
/* Write out modulus as little endian array of integers. */
|
|
for (i = 0; i < nwords; ++i) {
|
|
uint32_t nout;
|
|
|
|
BN_mod(n, N, B, bn_ctx); /* n = N mod B */
|
|
nout = BN_get_word(n);
|
|
*outbuf++ = nout;
|
|
|
|
BN_rshift(N, N, 32); /* N = N/B */
|
|
}
|
|
|
|
/* Write R^2 as little endian array of integers. */
|
|
for (i = 0; i < nwords; ++i) {
|
|
uint32_t rrout;
|
|
|
|
BN_mod(rr, RR, B, bn_ctx); /* rr = RR mod B */
|
|
rrout = BN_get_word(rr);
|
|
*outbuf++ = rrout;
|
|
|
|
BN_rshift(RR, RR, 32); /* RR = RR/B */
|
|
}
|
|
|
|
outbuf = NULL;
|
|
retval = 0;
|
|
|
|
done:
|
|
free(outbuf);
|
|
/* Free BIGNUMs. */
|
|
BN_free(Big1);
|
|
BN_free(Big2);
|
|
BN_free(Big32);
|
|
BN_free(BigMinus1);
|
|
BN_free(N0inv);
|
|
BN_free(R);
|
|
BN_free(RRTemp);
|
|
BN_free(NnumBits);
|
|
BN_free(n);
|
|
BN_free(rr);
|
|
|
|
return retval;
|
|
}
|