aboutsummaryrefslogtreecommitdiff
path: root/pw_crypto/public
diff options
context:
space:
mode:
authorAli Zhang <alizhang@google.com>2021-06-25 16:25:44 -0700
committerCQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>2021-07-16 18:12:51 +0000
commitef68dc6a8e56db22044e29999465891c8f439cb5 (patch)
tree555120f5690c13b5373b88331ae6785380772962 /pw_crypto/public
parentef336f0a82855f5f65b4b475f9af8560f51fcf51 (diff)
downloadpigweed-ef68dc6a8e56db22044e29999465891c8f439cb5.tar.gz
pw_crypto: Add SHA256 backed by Mbed TLS
Change-Id: I9ae84c9b3fff41129d1f6f17c2ee92d95078fa30 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/51061 Reviewed-by: Keir Mierle <keir@google.com> Commit-Queue: Ali Zhang <alizhang@google.com>
Diffstat (limited to 'pw_crypto/public')
-rw-r--r--pw_crypto/public/pw_crypto/sha256.h8
-rw-r--r--pw_crypto/public/pw_crypto/sha256_mbedtls.h37
2 files changed, 45 insertions, 0 deletions
diff --git a/pw_crypto/public/pw_crypto/sha256.h b/pw_crypto/public/pw_crypto/sha256.h
index ef734a0df..9f1ccc9d2 100644
--- a/pw_crypto/public/pw_crypto/sha256.h
+++ b/pw_crypto/public/pw_crypto/sha256.h
@@ -17,6 +17,7 @@
#include <cstdint>
#include "pw_bytes/span.h"
+#include "pw_crypto/sha256_backend.h"
#include "pw_status/status.h"
namespace pw::crypto::sha256 {
@@ -28,6 +29,13 @@ constexpr uint32_t kDigestSizeBytes = 32;
// messages.
class Sha256 {
public:
+ Sha256();
+ // Remove copy/move ctors
+ Sha256(const Sha256& other) = delete;
+ Sha256(Sha256&& other) = delete;
+ Sha256& operator=(const Sha256& other) = delete;
+ Sha256& operator=(Sha256&& other) = delete;
+
// Update adds `data` to the running hasher.
void Update(ConstByteSpan data);
diff --git a/pw_crypto/public/pw_crypto/sha256_mbedtls.h b/pw_crypto/public/pw_crypto/sha256_mbedtls.h
new file mode 100644
index 000000000..f77548b79
--- /dev/null
+++ b/pw_crypto/public/pw_crypto/sha256_mbedtls.h
@@ -0,0 +1,37 @@
+// Copyright 2021 The Pigweed Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#pragma once
+
+#include "mbedtls/md.h"
+
+namespace pw::crypto::sha256::backend {
+
+enum Sha256State {
+ // --> kInitialized upon construction, without fail.
+ kInitialized,
+ // kInitialized --> kStarted upon the first Update() call.
+ kStarted,
+ // kStarted --> kFinalized upon the first Final() call.
+ kFinalized,
+ // --> kError upon any unexpected failure.
+ kError,
+};
+
+struct Sha256Context {
+ Sha256State state;
+ mbedtls_md_context_t native_context;
+};
+
+} // namespace pw::crypto::sha256::backend