1 /* Copyright 2015 OpenMarket Ltd
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "olm/utility.hh"
17 #include "olm/crypto.h"
20 olm::Utility::Utility(
21 ) : last_error(OlmErrorCode::OLM_SUCCESS) {
25 size_t olm::Utility::sha256_length() const {
26 return SHA256_OUTPUT_LENGTH;
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
34 if (output_length < sha256_length()) {
35 last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
36 return std::size_t(-1);
38 _olm_crypto_sha256(input, input_length, output);
39 return SHA256_OUTPUT_LENGTH;
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
48 if (signature_length < ED25519_SIGNATURE_LENGTH) {
49 last_error = OlmErrorCode::OLM_BAD_MESSAGE_MAC;
50 return std::size_t(-1);
52 if (!_olm_crypto_ed25519_verify(&key, message, message_length, signature)) {
53 last_error = OlmErrorCode::OLM_BAD_MESSAGE_MAC;
54 return std::size_t(-1);
56 return std::size_t(0);