mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2025-11-26 19:25:02 +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>
119 lines
3.1 KiB
C
119 lines
3.1 KiB
C
/* Copyright (c) 2011 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 "sysincludes.h"
|
|
|
|
#include "cryptolib.h"
|
|
#include "utility.h"
|
|
#include "vboot_api.h"
|
|
|
|
void DigestInit(DigestContext* ctx, int sig_algorithm) {
|
|
ctx->algorithm = hash_type_map[sig_algorithm];
|
|
switch(ctx->algorithm) {
|
|
#ifndef CHROMEOS_EC
|
|
case SHA1_DIGEST_ALGORITHM:
|
|
ctx->sha1_ctx = (SHA1_CTX*) VbExMalloc(sizeof(SHA1_CTX));
|
|
SHA1_init(ctx->sha1_ctx);
|
|
break;
|
|
#endif
|
|
case SHA256_DIGEST_ALGORITHM:
|
|
ctx->sha256_ctx = (VB_SHA256_CTX*) VbExMalloc(sizeof(VB_SHA256_CTX));
|
|
SHA256_init(ctx->sha256_ctx);
|
|
break;
|
|
#ifndef CHROMEOS_EC
|
|
case SHA512_DIGEST_ALGORITHM:
|
|
ctx->sha512_ctx = (VB_SHA512_CTX*) VbExMalloc(sizeof(VB_SHA512_CTX));
|
|
SHA512_init(ctx->sha512_ctx);
|
|
break;
|
|
#endif
|
|
};
|
|
}
|
|
|
|
void DigestUpdate(DigestContext* ctx, const uint8_t* data, uint32_t len) {
|
|
switch(ctx->algorithm) {
|
|
#ifndef CHROMEOS_EC
|
|
case SHA1_DIGEST_ALGORITHM:
|
|
SHA1_update(ctx->sha1_ctx, data, len);
|
|
break;
|
|
#endif
|
|
case SHA256_DIGEST_ALGORITHM:
|
|
SHA256_update(ctx->sha256_ctx, data, len);
|
|
break;
|
|
#ifndef CHROMEOS_EC
|
|
case SHA512_DIGEST_ALGORITHM:
|
|
SHA512_update(ctx->sha512_ctx, data, len);
|
|
break;
|
|
#endif
|
|
};
|
|
}
|
|
|
|
uint8_t* DigestFinal(DigestContext* ctx) {
|
|
uint8_t* digest = NULL;
|
|
switch(ctx->algorithm) {
|
|
#ifndef CHROMEOS_EC
|
|
case SHA1_DIGEST_ALGORITHM:
|
|
digest = (uint8_t*) VbExMalloc(SHA1_DIGEST_SIZE);
|
|
Memcpy(digest, SHA1_final(ctx->sha1_ctx), SHA1_DIGEST_SIZE);
|
|
VbExFree(ctx->sha1_ctx);
|
|
break;
|
|
#endif
|
|
case SHA256_DIGEST_ALGORITHM:
|
|
digest = (uint8_t*) VbExMalloc(SHA256_DIGEST_SIZE);
|
|
Memcpy(digest, SHA256_final(ctx->sha256_ctx), SHA256_DIGEST_SIZE);
|
|
VbExFree(ctx->sha256_ctx);
|
|
break;
|
|
#ifndef CHROMEOS_EC
|
|
case SHA512_DIGEST_ALGORITHM:
|
|
digest = (uint8_t*) VbExMalloc(SHA512_DIGEST_SIZE);
|
|
Memcpy(digest, SHA512_final(ctx->sha512_ctx), SHA512_DIGEST_SIZE);
|
|
VbExFree(ctx->sha512_ctx);
|
|
break;
|
|
#endif
|
|
};
|
|
return digest;
|
|
}
|
|
|
|
uint8_t* DigestBuf(const uint8_t* buf, uint64_t len, int sig_algorithm) {
|
|
/* Allocate enough space for the largest digest */
|
|
uint8_t* digest = (uint8_t*) VbExMalloc(SHA512_DIGEST_SIZE);
|
|
/* Define an array mapping [sig_algorithm] to function pointers to the
|
|
* SHA{1|256|512} functions.
|
|
*/
|
|
typedef uint8_t* (*Hash_ptr) (const uint8_t*, uint64_t, uint8_t*);
|
|
Hash_ptr hash[] = {
|
|
#ifdef CHROMEOS_EC
|
|
0, /* RSA 1024 */
|
|
0,
|
|
0,
|
|
0, /* RSA 2048 */
|
|
0,
|
|
0,
|
|
0, /* RSA 4096 */
|
|
internal_SHA256,
|
|
0,
|
|
0, /* RSA 8192 */
|
|
0,
|
|
0,
|
|
#else
|
|
internal_SHA1, /* RSA 1024 */
|
|
internal_SHA256,
|
|
internal_SHA512,
|
|
internal_SHA1, /* RSA 2048 */
|
|
internal_SHA256,
|
|
internal_SHA512,
|
|
internal_SHA1, /* RSA 4096 */
|
|
internal_SHA256,
|
|
internal_SHA512,
|
|
internal_SHA1, /* RSA 8192 */
|
|
internal_SHA256,
|
|
internal_SHA512,
|
|
#endif
|
|
};
|
|
/* Call the appropriate hash function. */
|
|
return hash[sig_algorithm](buf, len, digest);
|
|
}
|