Home | History | Annotate | Download | only in 2lib
      1 /* Copyright (c) 2014 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 "2sysincludes.h"
      9 #include "2common.h"
     10 #include "2rsa.h"
     11 #include "2sha.h"
     12 
     13 #if VB2_SUPPORT_SHA1
     14 #define CTH_SHA1 VB2_HASH_SHA1
     15 #else
     16 #define CTH_SHA1 VB2_HASH_INVALID
     17 #endif
     18 
     19 #if VB2_SUPPORT_SHA256
     20 #define CTH_SHA256 VB2_HASH_SHA256
     21 #else
     22 #define CTH_SHA256 VB2_HASH_INVALID
     23 #endif
     24 
     25 #if VB2_SUPPORT_SHA512
     26 #define CTH_SHA512 VB2_HASH_SHA512
     27 #else
     28 #define CTH_SHA512 VB2_HASH_INVALID
     29 #endif
     30 
     31 static const uint8_t crypto_to_hash[] = {
     32 	CTH_SHA1,
     33 	CTH_SHA256,
     34 	CTH_SHA512,
     35 	CTH_SHA1,
     36 	CTH_SHA256,
     37 	CTH_SHA512,
     38 	CTH_SHA1,
     39 	CTH_SHA256,
     40 	CTH_SHA512,
     41 	CTH_SHA1,
     42 	CTH_SHA256,
     43 	CTH_SHA512,
     44 };
     45 
     46 /**
     47  * Convert vb2_crypto_algorithm to vb2_hash_algorithm.
     48  *
     49  * @param algorithm	Crypto algorithm (vb2_crypto_algorithm)
     50  *
     51  * @return The hash algorithm for that crypto algorithm, or VB2_HASH_INVALID if
     52  * the crypto algorithm or its corresponding hash algorithm is invalid or not
     53  * supported.
     54  */
     55 enum vb2_hash_algorithm vb2_crypto_to_hash(uint32_t algorithm)
     56 {
     57 	if (algorithm < ARRAY_SIZE(crypto_to_hash))
     58 		return crypto_to_hash[algorithm];
     59 	else
     60 		return VB2_HASH_INVALID;
     61 }
     62 
     63 int vb2_digest_size(enum vb2_hash_algorithm hash_alg)
     64 {
     65 	switch (hash_alg) {
     66 #if VB2_SUPPORT_SHA1
     67 	case VB2_HASH_SHA1:
     68 		return VB2_SHA1_DIGEST_SIZE;
     69 #endif
     70 #if VB2_SUPPORT_SHA256
     71 	case VB2_HASH_SHA256:
     72 		return VB2_SHA256_DIGEST_SIZE;
     73 #endif
     74 #if VB2_SUPPORT_SHA512
     75 	case VB2_HASH_SHA512:
     76 		return VB2_SHA512_DIGEST_SIZE;
     77 #endif
     78 	default:
     79 		return 0;
     80 	}
     81 }
     82 
     83 int vb2_digest_init(struct vb2_digest_context *dc,
     84 		    enum vb2_hash_algorithm hash_alg)
     85 {
     86 	dc->hash_alg = hash_alg;
     87 	dc->using_hwcrypto = 0;
     88 
     89 	switch (dc->hash_alg) {
     90 #if VB2_SUPPORT_SHA1
     91 	case VB2_HASH_SHA1:
     92 		vb2_sha1_init(&dc->sha1);
     93 		return VB2_SUCCESS;
     94 #endif
     95 #if VB2_SUPPORT_SHA256
     96 	case VB2_HASH_SHA256:
     97 		vb2_sha256_init(&dc->sha256);
     98 		return VB2_SUCCESS;
     99 #endif
    100 #if VB2_SUPPORT_SHA512
    101 	case VB2_HASH_SHA512:
    102 		vb2_sha512_init(&dc->sha512);
    103 		return VB2_SUCCESS;
    104 #endif
    105 	default:
    106 		return VB2_ERROR_SHA_INIT_ALGORITHM;
    107 	}
    108 }
    109 
    110 int vb2_digest_extend(struct vb2_digest_context *dc,
    111 		      const uint8_t *buf,
    112 		      uint32_t size)
    113 {
    114 	switch (dc->hash_alg) {
    115 #if VB2_SUPPORT_SHA1
    116 	case VB2_HASH_SHA1:
    117 		vb2_sha1_update(&dc->sha1, buf, size);
    118 		return VB2_SUCCESS;
    119 #endif
    120 #if VB2_SUPPORT_SHA256
    121 	case VB2_HASH_SHA256:
    122 		vb2_sha256_update(&dc->sha256, buf, size);
    123 		return VB2_SUCCESS;
    124 #endif
    125 #if VB2_SUPPORT_SHA512
    126 	case VB2_HASH_SHA512:
    127 		vb2_sha512_update(&dc->sha512, buf, size);
    128 		return VB2_SUCCESS;
    129 #endif
    130 	default:
    131 		return VB2_ERROR_SHA_EXTEND_ALGORITHM;
    132 	}
    133 }
    134 
    135 int vb2_digest_finalize(struct vb2_digest_context *dc,
    136 			uint8_t *digest,
    137 			uint32_t digest_size)
    138 {
    139 	if (digest_size < vb2_digest_size(dc->hash_alg))
    140 		return VB2_ERROR_SHA_FINALIZE_DIGEST_SIZE;
    141 
    142 	switch (dc->hash_alg) {
    143 #if VB2_SUPPORT_SHA1
    144 	case VB2_HASH_SHA1:
    145 		vb2_sha1_finalize(&dc->sha1, digest);
    146 		return VB2_SUCCESS;
    147 #endif
    148 #if VB2_SUPPORT_SHA256
    149 	case VB2_HASH_SHA256:
    150 		vb2_sha256_finalize(&dc->sha256, digest);
    151 		return VB2_SUCCESS;
    152 #endif
    153 #if VB2_SUPPORT_SHA512
    154 	case VB2_HASH_SHA512:
    155 		vb2_sha512_finalize(&dc->sha512, digest);
    156 		return VB2_SUCCESS;
    157 #endif
    158 	default:
    159 		return VB2_ERROR_SHA_FINALIZE_ALGORITHM;
    160 	}
    161 }
    162