1 /* Copyright (c) 2024, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #include <openssl/sha.h>
16
17 #include <openssl/mem.h>
18
19 #include "../fipsmodule/bcm_interface.h"
20
21
SHA224_Init(SHA256_CTX * sha)22 int SHA224_Init(SHA256_CTX *sha) {
23 BCM_sha224_init(sha);
24 return 1;
25 }
26
SHA224_Update(SHA256_CTX * sha,const void * data,size_t len)27 int SHA224_Update(SHA256_CTX *sha, const void *data, size_t len) {
28 BCM_sha224_update(sha, data, len);
29 return 1;
30 }
31
SHA224_Final(uint8_t out[SHA224_DIGEST_LENGTH],SHA256_CTX * sha)32 int SHA224_Final(uint8_t out[SHA224_DIGEST_LENGTH], SHA256_CTX *sha) {
33 BCM_sha224_final(out, sha);
34 return 1;
35 }
36
SHA224(const uint8_t * data,size_t len,uint8_t out[SHA224_DIGEST_LENGTH])37 uint8_t *SHA224(const uint8_t *data, size_t len,
38 uint8_t out[SHA224_DIGEST_LENGTH]) {
39 SHA256_CTX ctx;
40 BCM_sha224_init(&ctx);
41 BCM_sha224_update(&ctx, data, len);
42 BCM_sha224_final(out, &ctx);
43 OPENSSL_cleanse(&ctx, sizeof(ctx));
44 return out;
45 }
46
SHA256_Init(SHA256_CTX * sha)47 int SHA256_Init(SHA256_CTX *sha) {
48 BCM_sha256_init(sha);
49 return 1;
50 }
51
SHA256_Update(SHA256_CTX * sha,const void * data,size_t len)52 int SHA256_Update(SHA256_CTX *sha, const void *data, size_t len) {
53 BCM_sha256_update(sha, data, len);
54 return 1;
55 }
56
SHA256_Final(uint8_t out[SHA256_DIGEST_LENGTH],SHA256_CTX * sha)57 int SHA256_Final(uint8_t out[SHA256_DIGEST_LENGTH], SHA256_CTX *sha) {
58 // TODO(bbe): This overflow check one of the few places a low-level hash
59 // 'final' function can fail. SHA-512 does not have a corresponding check.
60 // The BCM function is infallible and will abort if this is done incorrectly.
61 // we should verify nothing crashes with this removed and eliminate the 0
62 // return.
63 if (sha->md_len > SHA256_DIGEST_LENGTH) {
64 return 0;
65 }
66 BCM_sha256_final(out, sha);
67 return 1;
68 }
69
SHA256(const uint8_t * data,size_t len,uint8_t out[SHA256_DIGEST_LENGTH])70 uint8_t *SHA256(const uint8_t *data, size_t len,
71 uint8_t out[SHA256_DIGEST_LENGTH]) {
72 SHA256_CTX ctx;
73 BCM_sha256_init(&ctx);
74 BCM_sha256_update(&ctx, data, len);
75 BCM_sha256_final(out, &ctx);
76 OPENSSL_cleanse(&ctx, sizeof(ctx));
77 return out;
78 }
79
SHA256_Transform(SHA256_CTX * sha,const uint8_t block[SHA256_CBLOCK])80 void SHA256_Transform(SHA256_CTX *sha, const uint8_t block[SHA256_CBLOCK]) {
81 BCM_sha256_transform(sha, block);
82 }
83
SHA256_TransformBlocks(uint32_t state[8],const uint8_t * data,size_t num_blocks)84 void SHA256_TransformBlocks(uint32_t state[8], const uint8_t *data,
85 size_t num_blocks) {
86 BCM_sha256_transform_blocks(state, data, num_blocks);
87 }
88