| 1 | /* Copyright 2015 OpenMarket Ltd |
| 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 | * http://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 | |
| 16 | #include "olm/utility.hh" |
| 17 | #include "olm/crypto.h" |
| 18 | |
| 19 | |
| 20 | olm::Utility::Utility( |
| 21 | ) : last_error(OlmErrorCode::OLM_SUCCESS) { |
| 22 | } |
| 23 | |
| 24 | |
| 25 | size_t olm::Utility::sha256_length() const { |
| 26 | return SHA256_OUTPUT_LENGTH; |
| 27 | } |
| 28 | |
| 29 | |
| 30 | size_t olm::Utility::sha256( |
| 31 | std::uint8_t const * input, std::size_t input_length, |
| 32 | std::uint8_t * output, std::size_t output_length |
| 33 | ) { |
| 34 | if (output_length < sha256_length()) { |
| 35 | last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL; |
| 36 | return std::size_t(-1); |
| 37 | } |
| 38 | _olm_crypto_sha256(input, input_length, output); |
| 39 | return SHA256_OUTPUT_LENGTH; |
| 40 | } |
| 41 | |
| 42 | |
| 43 | size_t olm::Utility::ed25519_verify( |
| 44 | _olm_ed25519_public_key const & key, |
| 45 | std::uint8_t const * message, std::size_t message_length, |
| 46 | std::uint8_t const * signature, std::size_t signature_length |
| 47 | ) { |
| 48 | if (signature_length < ED25519_SIGNATURE_LENGTH) { |
| 49 | last_error = OlmErrorCode::OLM_BAD_MESSAGE_MAC; |
| 50 | return std::size_t(-1); |
| 51 | } |
| 52 | if (!_olm_crypto_ed25519_verify(their_key: &key, message, message_length, signature)) { |
| 53 | last_error = OlmErrorCode::OLM_BAD_MESSAGE_MAC; |
| 54 | return std::size_t(-1); |
| 55 | } |
| 56 | return std::size_t(0); |
| 57 | } |
| 58 | |