OLD | NEW |
1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 */ | 4 */ |
5 | 5 |
6 /* SHA-1, 256 and 512 functions. */ | 6 /* SHA-1, 256 and 512 functions. */ |
7 | 7 |
8 #ifndef VBOOT_REFERENCE_SHA_H_ | 8 #ifndef VBOOT_REFERENCE_SHA_H_ |
9 #define VBOOT_REFERENCE_SHA_H_ | 9 #define VBOOT_REFERENCE_SHA_H_ |
10 | 10 |
11 #include <inttypes.h> | 11 #ifndef VBOOT_REFERENCE_CRYPTOLIB_H_ |
12 #include <string.h> | 12 #error "Do not include this file directly. Use cryptolib.h instead." |
| 13 #endif |
| 14 |
| 15 #include <stdint.h> |
13 | 16 |
14 #define SHA1_DIGEST_SIZE 20 | 17 #define SHA1_DIGEST_SIZE 20 |
15 #define SHA1_BLOCK_SIZE 64 | 18 #define SHA1_BLOCK_SIZE 64 |
16 | 19 |
17 #define SHA256_DIGEST_SIZE 32 | 20 #define SHA256_DIGEST_SIZE 32 |
18 #define SHA256_BLOCK_SIZE 64 | 21 #define SHA256_BLOCK_SIZE 64 |
19 | 22 |
20 #define SHA512_DIGEST_SIZE 64 | 23 #define SHA512_DIGEST_SIZE 64 |
21 #define SHA512_BLOCK_SIZE 128 | 24 #define SHA512_BLOCK_SIZE 128 |
22 | 25 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 */ | 77 */ |
75 uint8_t* SHA256(const uint8_t* data, uint64_t len, uint8_t* digest); | 78 uint8_t* SHA256(const uint8_t* data, uint64_t len, uint8_t* digest); |
76 | 79 |
77 /* Convenience function for SHA-512. Computes hash on [data] of length [len]. | 80 /* Convenience function for SHA-512. Computes hash on [data] of length [len]. |
78 * and stores it into [digest]. [digest] should be pre-allocated to | 81 * and stores it into [digest]. [digest] should be pre-allocated to |
79 * SHA512_DIGEST_SIZE bytes. | 82 * SHA512_DIGEST_SIZE bytes. |
80 */ | 83 */ |
81 uint8_t* SHA512(const uint8_t* data, uint64_t len, uint8_t* digest); | 84 uint8_t* SHA512(const uint8_t* data, uint64_t len, uint8_t* digest); |
82 | 85 |
83 | 86 |
| 87 /*---- Utility functions/wrappers for message digests. */ |
| 88 |
| 89 #define SHA1_DIGEST_ALGORITHM 0 |
| 90 #define SHA256_DIGEST_ALGORITHM 1 |
| 91 #define SHA512_DIGEST_ALGORITHM 2 |
| 92 |
| 93 /* A generic digest context structure which can be used to represent |
| 94 * the SHA*_CTX for multiple digest algorithms. |
| 95 */ |
| 96 typedef struct DigestContext { |
| 97 SHA1_CTX* sha1_ctx; |
| 98 SHA256_CTX* sha256_ctx; |
| 99 SHA512_CTX* sha512_ctx; |
| 100 int algorithm; /* Hashing algorithm to use. */ |
| 101 } DigestContext; |
| 102 |
| 103 /* Wrappers for message digest algorithms. These are useful when the hashing |
| 104 * operation is being done in parallel with something else. DigestContext tracks |
| 105 * and stores the state of any digest algorithm (one at any given time). |
| 106 */ |
| 107 |
| 108 /* Initialize a digest context for use with signature algorithm [algorithm]. */ |
| 109 void DigestInit(DigestContext* ctx, int sig_algorithm); |
| 110 void DigestUpdate(DigestContext* ctx, const uint8_t* data, uint64_t len); |
| 111 |
| 112 /* Caller owns the returned digest and must free it. */ |
| 113 uint8_t* DigestFinal(DigestContext* ctx); |
| 114 |
| 115 /* Returns the appropriate digest for the data in [input_file] |
| 116 * based on the signature [algorithm]. |
| 117 * Caller owns the returned digest and must free it. |
| 118 */ |
| 119 uint8_t* DigestFile(char* input_file, int sig_algorithm); |
| 120 |
| 121 /* Returns the appropriate digest of [buf] of length |
| 122 * [len] based on the signature [algorithm]. |
| 123 * Caller owns the returned digest and must free it. |
| 124 */ |
| 125 uint8_t* DigestBuf(const uint8_t* buf, uint64_t len, int sig_algorithm); |
| 126 |
| 127 |
84 #endif /* VBOOT_REFERENCE_SHA_H_ */ | 128 #endif /* VBOOT_REFERENCE_SHA_H_ */ |
OLD | NEW |