/src/boringssl/crypto/sha/sha256.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
22 | 1.43k | int SHA224_Init(SHA256_CTX *sha) { |
23 | 1.43k | BCM_sha224_init(sha); |
24 | 1.43k | return 1; |
25 | 1.43k | } |
26 | | |
27 | 11.7k | int SHA224_Update(SHA256_CTX *sha, const void *data, size_t len) { |
28 | 11.7k | BCM_sha224_update(sha, data, len); |
29 | 11.7k | return 1; |
30 | 11.7k | } |
31 | | |
32 | 5.31k | int SHA224_Final(uint8_t out[SHA224_DIGEST_LENGTH], SHA256_CTX *sha) { |
33 | 5.31k | BCM_sha224_final(out, sha); |
34 | 5.31k | return 1; |
35 | 5.31k | } |
36 | | |
37 | | uint8_t *SHA224(const uint8_t *data, size_t len, |
38 | 0 | uint8_t out[SHA224_DIGEST_LENGTH]) { |
39 | 0 | SHA256_CTX ctx; |
40 | 0 | BCM_sha224_init(&ctx); |
41 | 0 | BCM_sha224_update(&ctx, data, len); |
42 | 0 | BCM_sha224_final(out, &ctx); |
43 | 0 | OPENSSL_cleanse(&ctx, sizeof(ctx)); |
44 | 0 | return out; |
45 | 0 | } |
46 | | |
47 | 1.16k | int SHA256_Init(SHA256_CTX *sha) { |
48 | 1.16k | BCM_sha256_init(sha); |
49 | 1.16k | return 1; |
50 | 1.16k | } |
51 | | |
52 | 2 | int SHA256_Update(SHA256_CTX *sha, const void *data, size_t len) { |
53 | 2 | BCM_sha256_update(sha, data, len); |
54 | 2 | return 1; |
55 | 2 | } |
56 | | |
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 | | |
70 | | uint8_t *SHA256(const uint8_t *data, size_t len, |
71 | 9 | uint8_t out[SHA256_DIGEST_LENGTH]) { |
72 | 9 | SHA256_CTX ctx; |
73 | 9 | BCM_sha256_init(&ctx); |
74 | 9 | BCM_sha256_update(&ctx, data, len); |
75 | 9 | BCM_sha256_final(out, &ctx); |
76 | 9 | OPENSSL_cleanse(&ctx, sizeof(ctx)); |
77 | 9 | return out; |
78 | 9 | } |
79 | | |
80 | 0 | void SHA256_Transform(SHA256_CTX *sha, const uint8_t block[SHA256_CBLOCK]) { |
81 | 0 | BCM_sha256_transform(sha, block); |
82 | 0 | } |
83 | | |
84 | | void SHA256_TransformBlocks(uint32_t state[8], const uint8_t *data, |
85 | 0 | size_t num_blocks) { |
86 | 0 | BCM_sha256_transform_blocks(state, data, num_blocks); |
87 | 0 | } |