blob: 04392dde1ecbb9240fab0208308ce4906232949c [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/* SHA-256 and SHA-512 implementation based on code by Oliver Gay
2 * <olivier.gay@a3.epfl.ch> under a BSD-style license. See below.
3 */
4
5/*
6 * FIPS 180-2 SHA-224/256/384/512 implementation
7 * Last update: 02/02/2007
8 * Issue date: 04/30/2005
9 *
10 * Copyright (C) 2005, 2007 Olivier Gay <olivier.gay@a3.epfl.ch>
11 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the project nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37#include "avb_sha.h"
38#include "avb_util.h"
39#include "sha256.h"
40/* SHA-256 implementation */
41void avb_sha256_init(AvbSHA256Ctx* ctx) {
42 struct sha256_context* s_ctx = (struct sha256_context*)ctx;
43 if(sizeof(struct sha256_context)>sizeof(AvbSHA256Ctx))
44 {
45 avb_print("ERROR:sizeof(struct sha256_context)>sizeof(AvbSHA256Ctx)\n");
46 while(1);
47 }
48 avb_memset(s_ctx, 0, sizeof(struct sha256_context));
49 sha256_start(s_ctx);
50}
51
52void avb_sha256_update(AvbSHA256Ctx* ctx, const uint8_t* data, uint32_t len) {
53 sha256_process((struct sha256_context*)ctx,data,len);
54}
55
56uint8_t* avb_sha256_final(AvbSHA256Ctx* ctx) {
57 uint8_t* output = avb_malloc(AVB_SHA256_DIGEST_SIZE);
58 sha256_end((struct sha256_context*)ctx,output);
59 return output;
60}
61