/src/boringssl/crypto/sha/sha256.cc
Line | Count | Source |
1 | | // Copyright 2024 The BoringSSL Authors |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <openssl/sha2.h> |
16 | | |
17 | | #include <openssl/mem.h> |
18 | | |
19 | | #include "../fipsmodule/bcm_interface.h" |
20 | | |
21 | | |
22 | 0 | int SHA224_Init(SHA256_CTX *sha) { |
23 | 0 | BCM_sha224_init(sha); |
24 | 0 | return 1; |
25 | 0 | } |
26 | | |
27 | 0 | int SHA224_Update(SHA256_CTX *sha, const void *data, size_t len) { |
28 | 0 | BCM_sha224_update(sha, data, len); |
29 | 0 | return 1; |
30 | 0 | } |
31 | | |
32 | 0 | int SHA224_Final(uint8_t out[SHA224_DIGEST_LENGTH], SHA256_CTX *sha) { |
33 | 0 | BCM_sha224_final(out, sha); |
34 | 0 | return 1; |
35 | 0 | } |
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 | 267 | int SHA256_Init(SHA256_CTX *sha) { |
48 | 267 | BCM_sha256_init(sha); |
49 | 267 | return 1; |
50 | 267 | } |
51 | | |
52 | 588 | int SHA256_Update(SHA256_CTX *sha, const void *data, size_t len) { |
53 | 588 | BCM_sha256_update(sha, data, len); |
54 | 588 | return 1; |
55 | 588 | } |
56 | | |
57 | 267 | 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 | 267 | if (sha->md_len > SHA256_DIGEST_LENGTH) { |
64 | 0 | return 0; |
65 | 0 | } |
66 | 267 | BCM_sha256_final(out, sha); |
67 | 267 | return 1; |
68 | 267 | } |
69 | | |
70 | | uint8_t *SHA256(const uint8_t *data, size_t len, |
71 | 54.0k | uint8_t out[SHA256_DIGEST_LENGTH]) { |
72 | 54.0k | SHA256_CTX ctx; |
73 | 54.0k | BCM_sha256_init(&ctx); |
74 | 54.0k | BCM_sha256_update(&ctx, data, len); |
75 | 54.0k | BCM_sha256_final(out, &ctx); |
76 | 54.0k | OPENSSL_cleanse(&ctx, sizeof(ctx)); |
77 | 54.0k | return out; |
78 | 54.0k | } |
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 | } |