1e49e8af65fce38da7a308305566f8a14f102254aRandall Spangler/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * Use of this source code is governed by a BSD-style license that can be
3322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * found in the LICENSE file.
4322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */
5322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
6322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* SHA-1, 256 and 512 functions. */
7322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
8322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#ifndef VBOOT_REFERENCE_SHA_H_
9322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#define VBOOT_REFERENCE_SHA_H_
10322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
115411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah#ifndef VBOOT_REFERENCE_CRYPTOLIB_H_
125411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah#error "Do not include this file directly. Use cryptolib.h instead."
135411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah#endif
145411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah
15f302905224a346718910e56f5f1593d4b19253f1Randall Spangler#include "sysincludes.h"
16322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
17322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#define SHA1_DIGEST_SIZE 20
18322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#define SHA1_BLOCK_SIZE 64
19322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
20322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#define SHA256_DIGEST_SIZE 32
21322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#define SHA256_BLOCK_SIZE 64
22322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
23322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#define SHA512_DIGEST_SIZE 64
24322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#define SHA512_BLOCK_SIZE 128
25322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
26322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahtypedef struct SHA1_CTX {
27322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah  uint64_t count;
28322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah  uint32_t state[5];
29322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#if defined(HAVE_ENDIAN_H) && defined(HAVE_LITTLE_ENDIAN)
30322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah  union {
31322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah    uint8_t b[64];
32322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah    uint32_t w[16];
33322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah  } buf;
34322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#else
35322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah  uint8_t buf[64];
36322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#endif
37322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah} SHA1_CTX;
38322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
39322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahtypedef struct {
40322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah  uint32_t h[8];
41322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah  uint32_t tot_len;
42322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah  uint32_t len;
43322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah  uint8_t block[2 * SHA256_BLOCK_SIZE];
44322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah  uint8_t buf[SHA256_DIGEST_SIZE];  /* Used for storing the final digest. */
45d6759e4ce635fabf01e7919ef070e114d54b455bAdam Langley} VB_SHA256_CTX;
46322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
47322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahtypedef struct {
48322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah  uint64_t h[8];
49322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah  uint32_t tot_len;
50322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah  uint32_t len;
51322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah  uint8_t block[2 * SHA512_BLOCK_SIZE];
52322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah  uint8_t buf[SHA512_DIGEST_SIZE];  /* Used for storing the final digest. */
53d6759e4ce635fabf01e7919ef070e114d54b455bAdam Langley} VB_SHA512_CTX;
54322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
55322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
56322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahvoid SHA1_init(SHA1_CTX* ctx);
57456678b0c45d9fa6ad45d5dc6769051a731207f3Gaurav Shahvoid SHA1_update(SHA1_CTX* ctx, const uint8_t* data, uint64_t len);
58322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahuint8_t* SHA1_final(SHA1_CTX* ctx);
59322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
60d6759e4ce635fabf01e7919ef070e114d54b455bAdam Langleyvoid SHA256_init(VB_SHA256_CTX* ctx);
61d6759e4ce635fabf01e7919ef070e114d54b455bAdam Langleyvoid SHA256_update(VB_SHA256_CTX* ctx, const uint8_t* data, uint32_t len);
62d6759e4ce635fabf01e7919ef070e114d54b455bAdam Langleyuint8_t* SHA256_final(VB_SHA256_CTX* ctx);
63322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
64d6759e4ce635fabf01e7919ef070e114d54b455bAdam Langleyvoid SHA512_init(VB_SHA512_CTX* ctx);
65d6759e4ce635fabf01e7919ef070e114d54b455bAdam Langleyvoid SHA512_update(VB_SHA512_CTX* ctx, const uint8_t* data, uint32_t len);
66d6759e4ce635fabf01e7919ef070e114d54b455bAdam Langleyuint8_t* SHA512_final(VB_SHA512_CTX* ctx);
67321f310040b6ad281c0e4930defe551c5670859cGaurav Shah
68321f310040b6ad281c0e4930defe551c5670859cGaurav Shah/* Convenience function for SHA-1.  Computes hash on [data] of length [len].
69321f310040b6ad281c0e4930defe551c5670859cGaurav Shah * and stores it into [digest]. [digest] should be pre-allocated to
70321f310040b6ad281c0e4930defe551c5670859cGaurav Shah * SHA1_DIGEST_SIZE bytes.
71321f310040b6ad281c0e4930defe551c5670859cGaurav Shah */
72201fe0bb55ddb5a7270c45aa58bcb91258c0a9ceKees Cookuint8_t* internal_SHA1(const uint8_t* data, uint64_t len, uint8_t* digest);
73321f310040b6ad281c0e4930defe551c5670859cGaurav Shah
74321f310040b6ad281c0e4930defe551c5670859cGaurav Shah/* Convenience function for SHA-256.  Computes hash on [data] of length [len].
75321f310040b6ad281c0e4930defe551c5670859cGaurav Shah * and stores it into [digest]. [digest] should be pre-allocated to
76321f310040b6ad281c0e4930defe551c5670859cGaurav Shah * SHA256_DIGEST_SIZE bytes.
77321f310040b6ad281c0e4930defe551c5670859cGaurav Shah */
78201fe0bb55ddb5a7270c45aa58bcb91258c0a9ceKees Cookuint8_t* internal_SHA256(const uint8_t* data, uint64_t len, uint8_t* digest);
79321f310040b6ad281c0e4930defe551c5670859cGaurav Shah
80321f310040b6ad281c0e4930defe551c5670859cGaurav Shah/* Convenience function for SHA-512.  Computes hash on [data] of length [len].
81321f310040b6ad281c0e4930defe551c5670859cGaurav Shah * and stores it into [digest]. [digest] should be pre-allocated to
82321f310040b6ad281c0e4930defe551c5670859cGaurav Shah * SHA512_DIGEST_SIZE bytes.
83321f310040b6ad281c0e4930defe551c5670859cGaurav Shah */
84201fe0bb55ddb5a7270c45aa58bcb91258c0a9ceKees Cookuint8_t* internal_SHA512(const uint8_t* data, uint64_t len, uint8_t* digest);
85322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
86322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
875411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah/*---- Utility functions/wrappers for message digests. */
885411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah
895411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah#define SHA1_DIGEST_ALGORITHM 0
905411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah#define SHA256_DIGEST_ALGORITHM 1
915411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah#define SHA512_DIGEST_ALGORITHM 2
925411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah
935411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah/* A generic digest context structure which can be used to represent
945411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah * the SHA*_CTX for multiple digest algorithms.
955411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah */
965411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shahtypedef struct DigestContext {
975411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah  SHA1_CTX* sha1_ctx;
98d6759e4ce635fabf01e7919ef070e114d54b455bAdam Langley  VB_SHA256_CTX* sha256_ctx;
99d6759e4ce635fabf01e7919ef070e114d54b455bAdam Langley  VB_SHA512_CTX* sha512_ctx;
1005411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah  int algorithm;  /* Hashing algorithm to use. */
1015411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah} DigestContext;
1025411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah
1035411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah/* Wrappers for message digest algorithms. These are useful when the hashing
1045411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah * operation is being done in parallel with something else. DigestContext tracks
1055411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah * and stores the state of any digest algorithm (one at any given time).
1065411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah */
1075411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah
1085411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah/* Initialize a digest context for use with signature algorithm [algorithm]. */
1095411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shahvoid DigestInit(DigestContext* ctx, int sig_algorithm);
110e49e8af65fce38da7a308305566f8a14f102254aRandall Spanglervoid DigestUpdate(DigestContext* ctx, const uint8_t* data, uint32_t len);
1115411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah
1125411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah/* Caller owns the returned digest and must free it. */
1135411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shahuint8_t* DigestFinal(DigestContext* ctx);
1145411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah
1155411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah/* Returns the appropriate digest for the data in [input_file]
1165411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah * based on the signature [algorithm].
1175411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah * Caller owns the returned digest and must free it.
1185411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah */
1195411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shahuint8_t* DigestFile(char* input_file, int sig_algorithm);
1205411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah
1215411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah/* Returns the appropriate digest of [buf] of length
1225411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah * [len] based on the signature [algorithm].
1235411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah * Caller owns the returned digest and must free it.
1245411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah */
1255411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shahuint8_t* DigestBuf(const uint8_t* buf, uint64_t len, int sig_algorithm);
1265411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah
1275411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah
128322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#endif  /* VBOOT_REFERENCE_SHA_H_ */
129