Commit f9139dfa authored by Richard van der Hoff's avatar Richard van der Hoff
Browse files

Convert error.hh to plain C

parent e533b0dc
......@@ -17,7 +17,7 @@
#include "olm/list.hh"
#include "olm/crypto.hh"
#include "olm/error.hh"
#include "olm/error.h"
#include <cstdint>
......@@ -44,7 +44,7 @@ struct Account {
IdentityKeys identity_keys;
List<OneTimeKey, MAX_ONE_TIME_KEYS> one_time_keys;
std::uint32_t next_one_time_key_id;
ErrorCode last_error;
OlmErrorCode last_error;
/** Number of random bytes needed to create a new account */
std::size_t new_account_random_length();
......
/* Copyright 2015 OpenMarket Ltd
/* Copyright 2015-2016 OpenMarket Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -12,25 +12,29 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ERROR_HH_
#define ERROR_HH_
#ifndef OLM_ERROR_H_
#define OLM_ERROR_H_
namespace olm {
#ifdef __cplusplus
extern "C" {
#endif
enum struct ErrorCode {
SUCCESS = 0, /*!< There wasn't an error */
NOT_ENOUGH_RANDOM = 1, /*!< Not enough entropy was supplied */
OUTPUT_BUFFER_TOO_SMALL = 2, /*!< Supplied output buffer is too small */
BAD_MESSAGE_VERSION = 3, /*!< The message version is unsupported */
BAD_MESSAGE_FORMAT = 4, /*!< The message couldn't be decoded */
BAD_MESSAGE_MAC = 5, /*!< The message couldn't be decrypted */
BAD_MESSAGE_KEY_ID = 6, /*!< The message references an unknown key id */
INVALID_BASE64 = 7, /*!< The input base64 was invalid */
BAD_ACCOUNT_KEY = 8, /*!< The supplied account key is invalid */
UNKNOWN_PICKLE_VERSION = 9, /*!< The pickled object is too new */
CORRUPTED_PICKLE = 10, /*!< The pickled object couldn't be decoded */
enum OlmErrorCode {
OLM_SUCCESS = 0, /*!< There wasn't an error */
OLM_NOT_ENOUGH_RANDOM = 1, /*!< Not enough entropy was supplied */
OLM_OUTPUT_BUFFER_TOO_SMALL = 2, /*!< Supplied output buffer is too small */
OLM_BAD_MESSAGE_VERSION = 3, /*!< The message version is unsupported */
OLM_BAD_MESSAGE_FORMAT = 4, /*!< The message couldn't be decoded */
OLM_BAD_MESSAGE_MAC = 5, /*!< The message couldn't be decrypted */
OLM_BAD_MESSAGE_KEY_ID = 6, /*!< The message references an unknown key id */
OLM_INVALID_BASE64 = 7, /*!< The input base64 was invalid */
OLM_BAD_ACCOUNT_KEY = 8, /*!< The supplied account key is invalid */
OLM_UNKNOWN_PICKLE_VERSION = 9, /*!< The pickled object is too new */
OLM_CORRUPTED_PICKLE = 10, /*!< The pickled object couldn't be decoded */
};
} // namespace olm
#ifdef __cplusplus
} // extern "C"
#endif
#endif /* ERROR_HH_ */
#endif /* OLM_ERROR_H_ */
......@@ -15,7 +15,7 @@
#include "olm/crypto.hh"
#include "olm/list.hh"
#include "olm/error.hh"
#include "olm/error.h"
namespace olm {
......@@ -79,7 +79,7 @@ struct Ratchet {
Cipher const & ratchet_cipher;
/** The last error that happened encrypting or decrypting a message. */
ErrorCode last_error;
OlmErrorCode last_error;
/**
* A count of the number of times the root key has been advanced; this is
......
......@@ -31,7 +31,7 @@ struct Session {
Session();
Ratchet ratchet;
ErrorCode last_error;
OlmErrorCode last_error;
bool received_message;
......
......@@ -16,7 +16,7 @@
#ifndef UTILITY_HH_
#define UTILITY_HH_
#include "olm/error.hh"
#include "olm/error.h"
#include <cstddef>
#include <cstdint>
......@@ -29,7 +29,7 @@ struct Utility {
Utility();
ErrorCode last_error;
OlmErrorCode last_error;
/** The length of a SHA-256 hash in bytes. */
std::size_t sha256_length();
......
......@@ -19,7 +19,7 @@
olm::Account::Account(
) : next_one_time_key_id(0),
last_error(olm::ErrorCode::SUCCESS) {
last_error(OlmErrorCode::OLM_SUCCESS) {
}
......@@ -56,7 +56,7 @@ std::size_t olm::Account::new_account(
uint8_t const * random, std::size_t random_length
) {
if (random_length < new_account_random_length()) {
last_error = olm::ErrorCode::NOT_ENOUGH_RANDOM;
last_error = OlmErrorCode::OLM_NOT_ENOUGH_RANDOM;
return std::size_t(-1);
}
......@@ -110,7 +110,7 @@ std::size_t olm::Account::get_identity_json(
size_t expected_length = get_identity_json_length();
if (identity_json_length < expected_length) {
last_error = olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
return std::size_t(-1);
}
......@@ -146,7 +146,7 @@ std::size_t olm::Account::sign(
std::uint8_t * signature, std::size_t signature_length
) {
if (signature_length < this->signature_length()) {
last_error = olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
return std::size_t(-1);
}
olm::ed25519_sign(
......@@ -185,7 +185,7 @@ std::size_t olm::Account::get_one_time_keys_json(
) {
std::uint8_t * pos = one_time_json;
if (one_time_json_length < get_one_time_keys_json_length()) {
last_error = olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
return std::size_t(-1);
}
*(pos++) = '{';
......@@ -246,7 +246,7 @@ std::size_t olm::Account::generate_one_time_keys(
std::uint8_t const * random, std::size_t random_length
) {
if (random_length < generate_one_time_keys_random_length(number_of_keys)) {
last_error = olm::ErrorCode::NOT_ENOUGH_RANDOM;
last_error = OlmErrorCode::OLM_NOT_ENOUGH_RANDOM;
return std::size_t(-1);
}
for (unsigned i = 0; i < number_of_keys; ++i) {
......@@ -361,7 +361,7 @@ std::uint8_t const * olm::unpickle(
uint32_t pickle_version;
pos = olm::unpickle(pos, end, pickle_version);
if (pickle_version != ACCOUNT_PICKLE_VERSION) {
value.last_error = olm::ErrorCode::UNKNOWN_PICKLE_VERSION;
value.last_error = OlmErrorCode::OLM_UNKNOWN_PICKLE_VERSION;
return end;
}
pos = olm::unpickle(pos, end, value.identity_keys);
......
......@@ -104,11 +104,11 @@ std::size_t enc_output(
std::size_t enc_input(
std::uint8_t const * key, std::size_t key_length,
std::uint8_t * input, size_t b64_length,
olm::ErrorCode & last_error
OlmErrorCode & last_error
) {
std::size_t enc_length = olm::decode_base64_length(b64_length);
if (enc_length == std::size_t(-1)) {
last_error = olm::ErrorCode::INVALID_BASE64;
last_error = OlmErrorCode::OLM_INVALID_BASE64;
return std::size_t(-1);
}
olm::decode_base64(input, b64_length, input);
......@@ -120,7 +120,7 @@ std::size_t enc_input(
input, raw_length
);
if (result == std::size_t(-1)) {
last_error = olm::ErrorCode::BAD_ACCOUNT_KEY;
last_error = OlmErrorCode::OLM_BAD_ACCOUNT_KEY;
}
return result;
}
......@@ -150,11 +150,11 @@ std::size_t b64_output(
std::size_t b64_input(
std::uint8_t * input, size_t b64_length,
olm::ErrorCode & last_error
OlmErrorCode & last_error
) {
std::size_t raw_length = olm::decode_base64_length(b64_length);
if (raw_length == std::size_t(-1)) {
last_error = olm::ErrorCode::INVALID_BASE64;
last_error = OlmErrorCode::OLM_INVALID_BASE64;
return std::size_t(-1);
}
olm::decode_base64(input, b64_length, input);
......@@ -312,7 +312,7 @@ size_t olm_pickle_account(
olm::Account & object = *from_c(account);
std::size_t raw_length = pickle_length(object);
if (pickled_length < enc_output_length(raw_length)) {
object.last_error = olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
object.last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
return size_t(-1);
}
pickle(enc_output_pos(from_c(pickled), raw_length), object);
......@@ -328,7 +328,7 @@ size_t olm_pickle_session(
olm::Session & object = *from_c(session);
std::size_t raw_length = pickle_length(object);
if (pickled_length < enc_output_length(raw_length)) {
object.last_error = olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
object.last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
return size_t(-1);
}
pickle(enc_output_pos(from_c(pickled), raw_length), object);
......@@ -355,8 +355,8 @@ size_t olm_unpickle_account(
* (pos + raw_length). On error unpickle will return (pos + raw_length + 1).
*/
if (end != unpickle(pos, end + 1, object)) {
if (object.last_error == olm::ErrorCode::SUCCESS) {
object.last_error = olm::ErrorCode::CORRUPTED_PICKLE;
if (object.last_error == OlmErrorCode::OLM_SUCCESS) {
object.last_error = OlmErrorCode::OLM_CORRUPTED_PICKLE;
}
return std::size_t(-1);
}
......@@ -384,8 +384,8 @@ size_t olm_unpickle_session(
* (pos + raw_length). On error unpickle will return (pos + raw_length + 1).
*/
if (end != unpickle(pos, end + 1, object)) {
if (object.last_error == olm::ErrorCode::SUCCESS) {
object.last_error = olm::ErrorCode::CORRUPTED_PICKLE;
if (object.last_error == OlmErrorCode::OLM_SUCCESS) {
object.last_error = OlmErrorCode::OLM_CORRUPTED_PICKLE;
}
return std::size_t(-1);
}
......@@ -442,7 +442,7 @@ size_t olm_account_sign(
std::size_t raw_length = from_c(account)->signature_length();
if (signature_length < b64_output_length(raw_length)) {
from_c(account)->last_error =
olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
return std::size_t(-1);
}
from_c(account)->sign(
......@@ -528,7 +528,7 @@ size_t olm_create_outbound_session(
if (olm::decode_base64_length(id_key_length) != olm::KEY_LENGTH
|| olm::decode_base64_length(ot_key_length) != olm::KEY_LENGTH
) {
from_c(session)->last_error = olm::ErrorCode::INVALID_BASE64;
from_c(session)->last_error = OlmErrorCode::OLM_INVALID_BASE64;
return std::size_t(-1);
}
olm::Curve25519PublicKey identity_key;
......@@ -573,7 +573,7 @@ size_t olm_create_inbound_session_from(
std::size_t id_key_length = their_identity_key_length;
if (olm::decode_base64_length(id_key_length) != olm::KEY_LENGTH) {
from_c(session)->last_error = olm::ErrorCode::INVALID_BASE64;
from_c(session)->last_error = OlmErrorCode::OLM_INVALID_BASE64;
return std::size_t(-1);
}
olm::Curve25519PublicKey identity_key;
......@@ -605,7 +605,7 @@ size_t olm_session_id(
std::size_t raw_length = from_c(session)->session_id_length();
if (id_length < b64_output_length(raw_length)) {
from_c(session)->last_error =
olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
return std::size_t(-1);
}
std::size_t result = from_c(session)->session_id(
......@@ -644,7 +644,7 @@ size_t olm_matches_inbound_session_from(
std::size_t id_key_length = their_identity_key_length;
if (olm::decode_base64_length(id_key_length) != olm::KEY_LENGTH) {
from_c(session)->last_error = olm::ErrorCode::INVALID_BASE64;
from_c(session)->last_error = OlmErrorCode::OLM_INVALID_BASE64;
return std::size_t(-1);
}
olm::Curve25519PublicKey identity_key;
......@@ -671,7 +671,7 @@ size_t olm_remove_one_time_keys(
from_c(session)->bob_one_time_key
);
if (result == std::size_t(-1)) {
from_c(account)->last_error = olm::ErrorCode::BAD_MESSAGE_KEY_ID;
from_c(account)->last_error = OlmErrorCode::OLM_BAD_MESSAGE_KEY_ID;
}
return result;
}
......@@ -712,7 +712,7 @@ size_t olm_encrypt(
);
if (message_length < b64_output_length(raw_length)) {
from_c(session)->last_error =
olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
return std::size_t(-1);
}
std::size_t result = from_c(session)->encrypt(
......@@ -779,7 +779,7 @@ size_t olm_sha256(
std::size_t raw_length = from_c(utility)->sha256_length();
if (output_length < b64_output_length(raw_length)) {
from_c(utility)->last_error =
olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
return std::size_t(-1);
}
std::size_t result = from_c(utility)->sha256(
......@@ -800,7 +800,7 @@ size_t olm_ed25519_verify(
void * signature, size_t signature_length
) {
if (olm::decode_base64_length(key_length) != olm::KEY_LENGTH) {
from_c(utility)->last_error = olm::ErrorCode::INVALID_BASE64;
from_c(utility)->last_error = OlmErrorCode::OLM_INVALID_BASE64;
return std::size_t(-1);
}
olm::Ed25519PublicKey verify_key;
......
......@@ -186,7 +186,7 @@ olm::Ratchet::Ratchet(
Cipher const & ratchet_cipher
) : kdf_info(kdf_info),
ratchet_cipher(ratchet_cipher),
last_error(olm::ErrorCode::SUCCESS) {
last_error(OlmErrorCode::OLM_SUCCESS) {
}
......@@ -427,11 +427,11 @@ std::size_t olm::Ratchet::encrypt(
std::size_t output_length = encrypt_output_length(plaintext_length);
if (random_length < encrypt_random_length()) {
last_error = olm::ErrorCode::NOT_ENOUGH_RANDOM;
last_error = OlmErrorCode::OLM_NOT_ENOUGH_RANDOM;
return std::size_t(-1);
}
if (max_output_length < output_length) {
last_error = olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
return std::size_t(-1);
}
......@@ -488,7 +488,7 @@ std::size_t olm::Ratchet::decrypt_max_plaintext_length(
);
if (!reader.ciphertext) {
last_error = olm::ErrorCode::BAD_MESSAGE_FORMAT;
last_error = OlmErrorCode::OLM_BAD_MESSAGE_FORMAT;
return std::size_t(-1);
}
......@@ -506,12 +506,12 @@ std::size_t olm::Ratchet::decrypt(
);
if (reader.version != PROTOCOL_VERSION) {
last_error = olm::ErrorCode::BAD_MESSAGE_VERSION;
last_error = OlmErrorCode::OLM_BAD_MESSAGE_VERSION;
return std::size_t(-1);
}
if (!reader.has_counter || !reader.ratchet_key || !reader.ciphertext) {
last_error = olm::ErrorCode::BAD_MESSAGE_FORMAT;
last_error = OlmErrorCode::OLM_BAD_MESSAGE_FORMAT;
return std::size_t(-1);
}
......@@ -520,12 +520,12 @@ std::size_t olm::Ratchet::decrypt(
);
if (max_plaintext_length < max_length) {
last_error = olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
return std::size_t(-1);
}
if (reader.ratchet_key_length != olm::KEY_LENGTH) {
last_error = olm::ErrorCode::BAD_MESSAGE_FORMAT;
last_error = OlmErrorCode::OLM_BAD_MESSAGE_FORMAT;
return std::size_t(-1);
}
......@@ -588,7 +588,7 @@ std::size_t olm::Ratchet::decrypt(
}
if (result == std::size_t(-1)) {
last_error = olm::ErrorCode::BAD_MESSAGE_MAC;
last_error = OlmErrorCode::OLM_BAD_MESSAGE_MAC;
return std::size_t(-1);
}
......
......@@ -43,7 +43,7 @@ static const olm::KdfInfo OLM_KDF_INFO = {
olm::Session::Session(
) : ratchet(OLM_KDF_INFO, OLM_CIPHER),
last_error(olm::ErrorCode::SUCCESS),
last_error(OlmErrorCode::OLM_SUCCESS),
received_message(false) {
}
......@@ -61,7 +61,7 @@ std::size_t olm::Session::new_outbound_session(
std::uint8_t const * random, std::size_t random_length
) {
if (random_length < new_outbound_session_random_length()) {
last_error = olm::ErrorCode::NOT_ENOUGH_RANDOM;
last_error = OlmErrorCode::OLM_NOT_ENOUGH_RANDOM;
return std::size_t(-1);
}
......@@ -128,7 +128,7 @@ std::size_t olm::Session::new_inbound_session(
decode_one_time_key_message(reader, one_time_key_message, message_length);
if (!check_message_fields(reader, their_identity_key)) {
last_error = olm::ErrorCode::BAD_MESSAGE_FORMAT;
last_error = OlmErrorCode::OLM_BAD_MESSAGE_FORMAT;
return std::size_t(-1);
}
......@@ -137,7 +137,7 @@ std::size_t olm::Session::new_inbound_session(
their_identity_key->public_key, reader.identity_key, olm::KEY_LENGTH
);
if (!same) {
last_error = olm::ErrorCode::BAD_MESSAGE_KEY_ID;
last_error = OlmErrorCode::OLM_BAD_MESSAGE_KEY_ID;
return std::size_t(-1);
}
}
......@@ -154,7 +154,7 @@ std::size_t olm::Session::new_inbound_session(
if (!message_reader.ratchet_key
|| message_reader.ratchet_key_length != olm::KEY_LENGTH) {
last_error = olm::ErrorCode::BAD_MESSAGE_FORMAT;
last_error = OlmErrorCode::OLM_BAD_MESSAGE_FORMAT;
return std::size_t(-1);
}
......@@ -166,7 +166,7 @@ std::size_t olm::Session::new_inbound_session(
);
if (!our_one_time_key) {
last_error = olm::ErrorCode::BAD_MESSAGE_KEY_ID;
last_error = OlmErrorCode::OLM_BAD_MESSAGE_KEY_ID;
return std::size_t(-1);
}
......@@ -200,7 +200,7 @@ std::size_t olm::Session::session_id(
std::uint8_t * id, std::size_t id_length
) {
if (id_length < session_id_length()) {
last_error = olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
return std::size_t(-1);
}
std::uint8_t tmp[olm::KEY_LENGTH * 3];
......@@ -286,7 +286,7 @@ std::size_t olm::Session::encrypt(
std::uint8_t * message, std::size_t message_length
) {
if (message_length < encrypt_message_length(plaintext_length)) {
last_error = olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
return std::size_t(-1);
}
std::uint8_t * message_body;
......@@ -321,7 +321,7 @@ std::size_t olm::Session::encrypt(
if (result == std::size_t(-1)) {
last_error = ratchet.last_error;
ratchet.last_error = olm::ErrorCode::SUCCESS;
ratchet.last_error = OlmErrorCode::OLM_SUCCESS;
return result;
}
......@@ -342,7 +342,7 @@ std::size_t olm::Session::decrypt_max_plaintext_length(
olm::PreKeyMessageReader reader;
decode_one_time_key_message(reader, message, message_length);
if (!reader.message) {
last_error = olm::ErrorCode::BAD_MESSAGE_FORMAT;
last_error = OlmErrorCode::OLM_BAD_MESSAGE_FORMAT;
return std::size_t(-1);
}
message_body = reader.message;
......@@ -355,7 +355,7 @@ std::size_t olm::Session::decrypt_max_plaintext_length(
if (result == std::size_t(-1)) {
last_error = ratchet.last_error;
ratchet.last_error = olm::ErrorCode::SUCCESS;
ratchet.last_error = OlmErrorCode::OLM_SUCCESS;
}
return result;
}
......@@ -375,7 +375,7 @@ std::size_t olm::Session::decrypt(
olm::PreKeyMessageReader reader;
decode_one_time_key_message(reader, message, message_length);
if (!reader.message) {
last_error = olm::ErrorCode::BAD_MESSAGE_FORMAT;
last_error = OlmErrorCode::OLM_BAD_MESSAGE_FORMAT;
return std::size_t(-1);
}
message_body = reader.message;
......@@ -388,7 +388,7 @@ std::size_t olm::Session::decrypt(
if (result == std::size_t(-1)) {
last_error = ratchet.last_error;
ratchet.last_error = olm::ErrorCode::SUCCESS;
ratchet.last_error = OlmErrorCode::OLM_SUCCESS;
return result;
}
......@@ -435,7 +435,7 @@ std::uint8_t const * olm::unpickle(
uint32_t pickle_version;
pos = olm::unpickle(pos, end, pickle_version);
if (pickle_version != SESSION_PICKLE_VERSION) {
value.last_error = olm::ErrorCode::UNKNOWN_PICKLE_VERSION;
value.last_error = OlmErrorCode::OLM_UNKNOWN_PICKLE_VERSION;
return end;
}
pos = olm::unpickle(pos, end, value.received_message);
......
......@@ -18,7 +18,7 @@
olm::Utility::Utility(
) : last_error(olm::ErrorCode::SUCCESS) {
) : last_error(OlmErrorCode::OLM_SUCCESS) {
}
......@@ -32,7 +32,7 @@ size_t olm::Utility::sha256(
std::uint8_t * output, std::size_t output_length
) {
if (output_length < sha256_length()) {
last_error = olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL;
return std::size_t(-1);
}
crypto_sha256(input, input_length, output);
......@@ -46,11 +46,11 @@ size_t olm::Utility::ed25519_verify(
std::uint8_t const * signature, std::size_t signature_length
) {
if (signature_length < olm::SIGNATURE_LENGTH) {
last_error = olm::ErrorCode::BAD_MESSAGE_MAC;
last_error = OlmErrorCode::OLM_BAD_MESSAGE_MAC;
return std::size_t(-1);
}
if (!olm::ed25519_verify(key, message, message_length, signature)) {
last_error = olm::ErrorCode::BAD_MESSAGE_MAC;
last_error = OlmErrorCode::OLM_BAD_MESSAGE_MAC;
return std::size_t(-1);
}
return std::size_t(0);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment