blob: 8cb6595ba9384c60fa471a87f677dc8dcd3791f4 [file] [log] [blame]
Ali Zhangef68dc62021-06-25 16:25:44 -07001// Copyright 2021 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// 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, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
Ali Zhang3f30a2e2022-04-01 11:23:03 -070014#define PW_LOG_MODULE_NAME "SHA256-MTLS"
15#define PW_LOG_LEVEL PW_LOG_LEVEL_WARN
Ali Zhangef68dc62021-06-25 16:25:44 -070016
17#include "pw_crypto/sha256.h"
18#include "pw_status/status.h"
19
Ali Zhang6ae057c2021-07-30 14:17:55 -070020namespace pw::crypto::sha256::backend {
Ali Zhangef68dc62021-06-25 16:25:44 -070021
Ali Zhang6ae057c2021-07-30 14:17:55 -070022Status DoInit(NativeSha256Context& ctx) {
Ali Zhang472287f2021-07-16 16:09:31 -070023 // mbedtsl_sha256_init() never fails (returns void).
Ali Zhang6ae057c2021-07-30 14:17:55 -070024 mbedtls_sha256_init(&ctx);
Ali Zhang472287f2021-07-16 16:09:31 -070025
Ali Zhang6ae057c2021-07-30 14:17:55 -070026 if (mbedtls_sha256_starts_ret(&ctx, /* is224 = */ 0)) {
Ali Zhangef68dc62021-06-25 16:25:44 -070027 return Status::Internal();
28 }
29
Ali Zhangef68dc62021-06-25 16:25:44 -070030 return OkStatus();
31}
32
Ali Zhang6ae057c2021-07-30 14:17:55 -070033Status DoUpdate(NativeSha256Context& ctx, ConstByteSpan data) {
34 if (mbedtls_sha256_update_ret(
35 &ctx,
36 reinterpret_cast<const unsigned char*>(data.data()),
37 data.size())) {
38 return Status::Internal();
39 }
40
41 return OkStatus();
42}
43
44Status DoFinal(NativeSha256Context& ctx, ByteSpan out_digest) {
45 if (mbedtls_sha256_finish_ret(
46 &ctx, reinterpret_cast<unsigned char*>(out_digest.data()))) {
47 return Status::Internal();
48 }
49
50 return OkStatus();
51}
52
53} // namespace pw::crypto::sha256::backend