Coverage Report

Created: 2026-02-16 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
using namespace bssl;
23
24
0
int SHA224_Init(SHA256_CTX *sha) {
25
0
  BCM_sha224_init(sha);
26
0
  return 1;
27
0
}
28
29
0
int SHA224_Update(SHA256_CTX *sha, const void *data, size_t len) {
30
0
  BCM_sha224_update(sha, data, len);
31
0
  return 1;
32
0
}
33
34
0
int SHA224_Final(uint8_t out[SHA224_DIGEST_LENGTH], SHA256_CTX *sha) {
35
0
  BCM_sha224_final(out, sha);
36
0
  return 1;
37
0
}
38
39
uint8_t *SHA224(const uint8_t *data, size_t len,
40
0
                uint8_t out[SHA224_DIGEST_LENGTH]) {
41
0
  SHA256_CTX ctx;
42
0
  BCM_sha224_init(&ctx);
43
0
  BCM_sha224_update(&ctx, data, len);
44
0
  BCM_sha224_final(out, &ctx);
45
0
  OPENSSL_cleanse(&ctx, sizeof(ctx));
46
0
  return out;
47
0
}
48
49
716
int SHA256_Init(SHA256_CTX *sha) {
50
716
  BCM_sha256_init(sha);
51
716
  return 1;
52
716
}
53
54
1.49k
int SHA256_Update(SHA256_CTX *sha, const void *data, size_t len) {
55
1.49k
  BCM_sha256_update(sha, data, len);
56
1.49k
  return 1;
57
1.49k
}
58
59
716
int SHA256_Final(uint8_t out[SHA256_DIGEST_LENGTH], SHA256_CTX *sha) {
60
  // TODO(bbe): This overflow check one of the few places a low-level hash
61
  // 'final' function can fail. SHA-512 does not have a corresponding check.
62
  // The BCM function is infallible and will abort if this is done incorrectly.
63
  // we should verify nothing crashes with this removed and eliminate the 0
64
  // return.
65
716
  if (sha->md_len > SHA256_DIGEST_LENGTH) {
66
0
    return 0;
67
0
  }
68
716
  BCM_sha256_final(out, sha);
69
716
  return 1;
70
716
}
71
72
uint8_t *SHA256(const uint8_t *data, size_t len,
73
41.2k
                uint8_t out[SHA256_DIGEST_LENGTH]) {
74
41.2k
  SHA256_CTX ctx;
75
41.2k
  BCM_sha256_init(&ctx);
76
41.2k
  BCM_sha256_update(&ctx, data, len);
77
41.2k
  BCM_sha256_final(out, &ctx);
78
41.2k
  OPENSSL_cleanse(&ctx, sizeof(ctx));
79
41.2k
  return out;
80
41.2k
}
81
82
0
void SHA256_Transform(SHA256_CTX *sha, const uint8_t block[SHA256_CBLOCK]) {
83
0
  BCM_sha256_transform(sha, block);
84
0
}
85
86
void SHA256_TransformBlocks(uint32_t state[8], const uint8_t *data,
87
0
                            size_t num_blocks) {
88
0
  BCM_sha256_transform_blocks(state, data, num_blocks);
89
0
}