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
SHA384_Init(SHA512_CTX * sha)22 int SHA384_Init(SHA512_CTX *sha) {
23 BCM_sha384_init(sha);
24 return 1;
25 }
26
SHA384_Update(SHA512_CTX * sha,const void * data,size_t len)27 int SHA384_Update(SHA512_CTX *sha, const void *data, size_t len) {
28 BCM_sha384_update(sha, data, len);
29 return 1;
30 }
31
SHA384_Final(uint8_t out[SHA384_DIGEST_LENGTH],SHA512_CTX * sha)32 int SHA384_Final(uint8_t out[SHA384_DIGEST_LENGTH], SHA512_CTX *sha) {
33 BCM_sha384_final(out, sha);
34 return 1;
35 }
36
SHA384(const uint8_t * data,size_t len,uint8_t out[SHA384_DIGEST_LENGTH])37 uint8_t *SHA384(const uint8_t *data, size_t len,
38 uint8_t out[SHA384_DIGEST_LENGTH]) {
39 SHA512_CTX ctx;
40 BCM_sha384_init(&ctx);
41 BCM_sha384_update(&ctx, data, len);
42 BCM_sha384_final(out, &ctx);
43 OPENSSL_cleanse(&ctx, sizeof(ctx));
44 return out;
45 }
46
SHA512_256_Init(SHA512_CTX * sha)47 int SHA512_256_Init(SHA512_CTX *sha) {
48 BCM_sha512_256_init(sha);
49 return 1;
50 }
51
SHA512_256_Update(SHA512_CTX * sha,const void * data,size_t len)52 int SHA512_256_Update(SHA512_CTX *sha, const void *data, size_t len) {
53 BCM_sha512_256_update(sha, data, len);
54 return 1;
55 }
56
SHA512_256_Final(uint8_t out[SHA512_256_DIGEST_LENGTH],SHA512_CTX * sha)57 int SHA512_256_Final(uint8_t out[SHA512_256_DIGEST_LENGTH], SHA512_CTX *sha) {
58 BCM_sha512_256_final(out, sha);
59 return 1;
60 }
61
SHA512_256(const uint8_t * data,size_t len,uint8_t out[SHA512_256_DIGEST_LENGTH])62 uint8_t *SHA512_256(const uint8_t *data, size_t len,
63 uint8_t out[SHA512_256_DIGEST_LENGTH]) {
64 SHA512_CTX ctx;
65 BCM_sha512_256_init(&ctx);
66 BCM_sha512_256_update(&ctx, data, len);
67 BCM_sha512_256_final(out, &ctx);
68 OPENSSL_cleanse(&ctx, sizeof(ctx));
69 return out;
70 }
71
SHA512_Init(SHA512_CTX * sha)72 int SHA512_Init(SHA512_CTX *sha) {
73 BCM_sha512_init(sha);
74 return 1;
75 }
76
SHA512_Update(SHA512_CTX * sha,const void * data,size_t len)77 int SHA512_Update(SHA512_CTX *sha, const void *data, size_t len) {
78 BCM_sha512_update(sha, data, len);
79 return 1;
80 }
81
SHA512_Final(uint8_t out[SHA512_DIGEST_LENGTH],SHA512_CTX * sha)82 int SHA512_Final(uint8_t out[SHA512_DIGEST_LENGTH], SHA512_CTX *sha) {
83 // Historically this function retured failure if passed NULL, even
84 // though other final functions do not.
85 if (out == NULL) {
86 return 0;
87 }
88 BCM_sha512_final(out, sha);
89 return 1;
90 }
91
SHA512(const uint8_t * data,size_t len,uint8_t out[SHA512_DIGEST_LENGTH])92 uint8_t *SHA512(const uint8_t *data, size_t len,
93 uint8_t out[SHA512_DIGEST_LENGTH]) {
94 SHA512_CTX ctx;
95 BCM_sha512_init(&ctx);
96 BCM_sha512_update(&ctx, data, len);
97 BCM_sha512_final(out, &ctx);
98 OPENSSL_cleanse(&ctx, sizeof(ctx));
99 return out;
100 }
101
SHA512_Transform(SHA512_CTX * sha,const uint8_t block[SHA512_CBLOCK])102 void SHA512_Transform(SHA512_CTX *sha, const uint8_t block[SHA512_CBLOCK]) {
103 BCM_sha512_transform(sha, block);
104 }
105