Home | History | Annotate | Download | only in include
      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 
      6 /* SHA-1, 256 and 512 functions. */
      7 
      8 #ifndef VBOOT_REFERENCE_SHA_H_
      9 #define VBOOT_REFERENCE_SHA_H_
     10 
     11 #ifndef VBOOT_REFERENCE_CRYPTOLIB_H_
     12 #error "Do not include this file directly. Use cryptolib.h instead."
     13 #endif
     14 
     15 #include "sysincludes.h"
     16 
     17 #define SHA1_DIGEST_SIZE 20
     18 #define SHA1_BLOCK_SIZE 64
     19 
     20 #define SHA256_DIGEST_SIZE 32
     21 #define SHA256_BLOCK_SIZE 64
     22 
     23 #define SHA512_DIGEST_SIZE 64
     24 #define SHA512_BLOCK_SIZE 128
     25 
     26 typedef struct SHA1_CTX {
     27   uint64_t count;
     28   uint32_t state[5];
     29 #if defined(HAVE_ENDIAN_H) && defined(HAVE_LITTLE_ENDIAN)
     30   union {
     31     uint8_t b[64];
     32     uint32_t w[16];
     33   } buf;
     34 #else
     35   uint8_t buf[64];
     36 #endif
     37 } SHA1_CTX;
     38 
     39 typedef struct {
     40   uint32_t h[8];
     41   uint32_t tot_len;
     42   uint32_t len;
     43   uint8_t block[2 * SHA256_BLOCK_SIZE];
     44   uint8_t buf[SHA256_DIGEST_SIZE];  /* Used for storing the final digest. */
     45 } VB_SHA256_CTX;
     46 
     47 typedef struct {
     48   uint64_t h[8];
     49   uint32_t tot_len;
     50   uint32_t len;
     51   uint8_t block[2 * SHA512_BLOCK_SIZE];
     52   uint8_t buf[SHA512_DIGEST_SIZE];  /* Used for storing the final digest. */
     53 } VB_SHA512_CTX;
     54 
     55 
     56 void SHA1_init(SHA1_CTX* ctx);
     57 void SHA1_update(SHA1_CTX* ctx, const uint8_t* data, uint64_t len);
     58 uint8_t* SHA1_final(SHA1_CTX* ctx);
     59 
     60 void SHA256_init(VB_SHA256_CTX* ctx);
     61 void SHA256_update(VB_SHA256_CTX* ctx, const uint8_t* data, uint32_t len);
     62 uint8_t* SHA256_final(VB_SHA256_CTX* ctx);
     63 
     64 void SHA512_init(VB_SHA512_CTX* ctx);
     65 void SHA512_update(VB_SHA512_CTX* ctx, const uint8_t* data, uint32_t len);
     66 uint8_t* SHA512_final(VB_SHA512_CTX* ctx);
     67 
     68 /* Convenience function for SHA-1.  Computes hash on [data] of length [len].
     69  * and stores it into [digest]. [digest] should be pre-allocated to
     70  * SHA1_DIGEST_SIZE bytes.
     71  */
     72 uint8_t* internal_SHA1(const uint8_t* data, uint64_t len, uint8_t* digest);
     73 
     74 /* Convenience function for SHA-256.  Computes hash on [data] of length [len].
     75  * and stores it into [digest]. [digest] should be pre-allocated to
     76  * SHA256_DIGEST_SIZE bytes.
     77  */
     78 uint8_t* internal_SHA256(const uint8_t* data, uint64_t len, uint8_t* digest);
     79 
     80 /* Convenience function for SHA-512.  Computes hash on [data] of length [len].
     81  * and stores it into [digest]. [digest] should be pre-allocated to
     82  * SHA512_DIGEST_SIZE bytes.
     83  */
     84 uint8_t* internal_SHA512(const uint8_t* data, uint64_t len, uint8_t* digest);
     85 
     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   VB_SHA256_CTX* sha256_ctx;
     99   VB_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, uint32_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 
    128 #endif  /* VBOOT_REFERENCE_SHA_H_ */
    129