Ali Zhang | dd1cbe7 | 2021-07-20 15:48:55 -0700 | [diff] [blame] | 1 | // 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 Zhang | 3f30a2e | 2022-04-01 11:23:03 -0700 | [diff] [blame] | 14 | #define PW_LOG_MODULE_NAME "SHA256-BSSL" |
| 15 | #define PW_LOG_LEVEL PW_LOG_LEVEL_WARN |
Ali Zhang | dd1cbe7 | 2021-07-20 15:48:55 -0700 | [diff] [blame] | 16 | |
| 17 | #include "pw_crypto/sha256.h" |
| 18 | #include "pw_status/status.h" |
| 19 | |
Ali Zhang | 6ae057c | 2021-07-30 14:17:55 -0700 | [diff] [blame] | 20 | namespace pw::crypto::sha256::backend { |
Ali Zhang | dd1cbe7 | 2021-07-20 15:48:55 -0700 | [diff] [blame] | 21 | |
Ali Zhang | 6ae057c | 2021-07-30 14:17:55 -0700 | [diff] [blame] | 22 | Status DoInit(NativeSha256Context& ctx) { |
| 23 | if (!SHA256_Init(&ctx)) { |
Ali Zhang | dd1cbe7 | 2021-07-20 15:48:55 -0700 | [diff] [blame] | 24 | return Status::Internal(); |
| 25 | } |
Ali Zhang | 6ae057c | 2021-07-30 14:17:55 -0700 | [diff] [blame] | 26 | return OkStatus(); |
| 27 | } |
Ali Zhang | dd1cbe7 | 2021-07-20 15:48:55 -0700 | [diff] [blame] | 28 | |
Ali Zhang | 6ae057c | 2021-07-30 14:17:55 -0700 | [diff] [blame] | 29 | Status DoUpdate(NativeSha256Context& ctx, ConstByteSpan data) { |
| 30 | if (!SHA256_Update(&ctx, |
| 31 | reinterpret_cast<const unsigned char*>(data.data()), |
| 32 | data.size())) { |
| 33 | return Status::Internal(); |
| 34 | } |
Ali Zhang | dd1cbe7 | 2021-07-20 15:48:55 -0700 | [diff] [blame] | 35 | |
| 36 | return OkStatus(); |
| 37 | } |
| 38 | |
Ali Zhang | 6ae057c | 2021-07-30 14:17:55 -0700 | [diff] [blame] | 39 | Status DoFinal(NativeSha256Context& ctx, ByteSpan out_digest) { |
| 40 | if (!SHA256_Final(reinterpret_cast<unsigned char*>(out_digest.data()), |
| 41 | &ctx)) { |
| 42 | return Status::Internal(); |
| 43 | } |
| 44 | |
| 45 | return OkStatus(); |
| 46 | } |
| 47 | |
| 48 | } // namespace pw::crypto::sha256::backend |