1 /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. 2 * Use of this source code is governed by a BSD-style license that can be 3 * found in the LICENSE file. 4 * 5 * Utility functions for message digest functions. 6 */ 7 8 #include "sysincludes.h" 9 10 #include "cryptolib.h" 11 #include "utility.h" 12 #include "vboot_api.h" 13 14 void DigestInit(DigestContext* ctx, int sig_algorithm) { 15 ctx->algorithm = hash_type_map[sig_algorithm]; 16 switch(ctx->algorithm) { 17 #ifndef CHROMEOS_EC 18 case SHA1_DIGEST_ALGORITHM: 19 ctx->sha1_ctx = (SHA1_CTX*) VbExMalloc(sizeof(SHA1_CTX)); 20 SHA1_init(ctx->sha1_ctx); 21 break; 22 #endif 23 case SHA256_DIGEST_ALGORITHM: 24 ctx->sha256_ctx = (VB_SHA256_CTX*) VbExMalloc(sizeof(VB_SHA256_CTX)); 25 SHA256_init(ctx->sha256_ctx); 26 break; 27 #ifndef CHROMEOS_EC 28 case SHA512_DIGEST_ALGORITHM: 29 ctx->sha512_ctx = (VB_SHA512_CTX*) VbExMalloc(sizeof(VB_SHA512_CTX)); 30 SHA512_init(ctx->sha512_ctx); 31 break; 32 #endif 33 }; 34 } 35 36 void DigestUpdate(DigestContext* ctx, const uint8_t* data, uint32_t len) { 37 switch(ctx->algorithm) { 38 #ifndef CHROMEOS_EC 39 case SHA1_DIGEST_ALGORITHM: 40 SHA1_update(ctx->sha1_ctx, data, len); 41 break; 42 #endif 43 case SHA256_DIGEST_ALGORITHM: 44 SHA256_update(ctx->sha256_ctx, data, len); 45 break; 46 #ifndef CHROMEOS_EC 47 case SHA512_DIGEST_ALGORITHM: 48 SHA512_update(ctx->sha512_ctx, data, len); 49 break; 50 #endif 51 }; 52 } 53 54 uint8_t* DigestFinal(DigestContext* ctx) { 55 uint8_t* digest = NULL; 56 switch(ctx->algorithm) { 57 #ifndef CHROMEOS_EC 58 case SHA1_DIGEST_ALGORITHM: 59 digest = (uint8_t*) VbExMalloc(SHA1_DIGEST_SIZE); 60 Memcpy(digest, SHA1_final(ctx->sha1_ctx), SHA1_DIGEST_SIZE); 61 VbExFree(ctx->sha1_ctx); 62 break; 63 #endif 64 case SHA256_DIGEST_ALGORITHM: 65 digest = (uint8_t*) VbExMalloc(SHA256_DIGEST_SIZE); 66 Memcpy(digest, SHA256_final(ctx->sha256_ctx), SHA256_DIGEST_SIZE); 67 VbExFree(ctx->sha256_ctx); 68 break; 69 #ifndef CHROMEOS_EC 70 case SHA512_DIGEST_ALGORITHM: 71 digest = (uint8_t*) VbExMalloc(SHA512_DIGEST_SIZE); 72 Memcpy(digest, SHA512_final(ctx->sha512_ctx), SHA512_DIGEST_SIZE); 73 VbExFree(ctx->sha512_ctx); 74 break; 75 #endif 76 }; 77 return digest; 78 } 79 80 uint8_t* DigestBuf(const uint8_t* buf, uint64_t len, int sig_algorithm) { 81 /* Allocate enough space for the largest digest */ 82 uint8_t* digest = (uint8_t*) VbExMalloc(SHA512_DIGEST_SIZE); 83 /* Define an array mapping [sig_algorithm] to function pointers to the 84 * SHA{1|256|512} functions. 85 */ 86 typedef uint8_t* (*Hash_ptr) (const uint8_t*, uint64_t, uint8_t*); 87 Hash_ptr hash[] = { 88 #ifdef CHROMEOS_EC 89 0, /* RSA 1024 */ 90 0, 91 0, 92 0, /* RSA 2048 */ 93 0, 94 0, 95 0, /* RSA 4096 */ 96 internal_SHA256, 97 0, 98 0, /* RSA 8192 */ 99 0, 100 0, 101 #else 102 internal_SHA1, /* RSA 1024 */ 103 internal_SHA256, 104 internal_SHA512, 105 internal_SHA1, /* RSA 2048 */ 106 internal_SHA256, 107 internal_SHA512, 108 internal_SHA1, /* RSA 4096 */ 109 internal_SHA256, 110 internal_SHA512, 111 internal_SHA1, /* RSA 8192 */ 112 internal_SHA256, 113 internal_SHA512, 114 #endif 115 }; 116 /* Call the appropriate hash function. */ 117 return hash[sig_algorithm](buf, len, digest); 118 } 119