aboutsummaryrefslogtreecommitdiff
path: root/src/crypto.cpp
diff options
context:
space:
mode:
authorRichard van der Hoff <github@rvanderhoff.org.uk>2016-09-05 10:42:09 +0100
committerGitHub <noreply@github.com>2016-09-05 10:42:09 +0100
commit057ab15c1a9bd6e09b31955e840861151b5ad69e (patch)
tree140b55571972ddf76c28515ec767ac01a5e21712 /src/crypto.cpp
parent2aad4cfa860e33228372d525b4bc6a8bcdfbb8f6 (diff)
parent69f269ffaf88515f6d5c0b34178bf0096cf5773b (diff)
Merge pull request #14 from matrix-org/rav/convert_crypto_to_c
Convert crypto.hh into C-compatible interface
Diffstat (limited to 'src/crypto.cpp')
-rw-r--r--src/crypto.cpp66
1 files changed, 36 insertions, 30 deletions
diff --git a/src/crypto.cpp b/src/crypto.cpp
index 0b08c54..5095c79 100644
--- a/src/crypto.cpp
+++ b/src/crypto.cpp
@@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-#include "olm/crypto.hh"
+#include "olm/crypto.h"
#include "olm/memory.hh"
#include <cstring>
@@ -100,80 +100,86 @@ inline static void hmac_sha256_final(
} // namespace
-void olm::curve25519_generate_key(
- std::uint8_t const * random_32_bytes,
- olm::Curve25519KeyPair & key_pair
+void _olm_crypto_curve25519_generate_key(
+ uint8_t const * random_32_bytes,
+ struct _olm_curve25519_key_pair *key_pair
) {
- std::memcpy(key_pair.private_key, random_32_bytes, CURVE25519_KEY_LENGTH);
+ std::memcpy(
+ key_pair->private_key.private_key, random_32_bytes,
+ CURVE25519_KEY_LENGTH
+ );
::curve25519_donna(
- key_pair.public_key, key_pair.private_key, CURVE25519_BASEPOINT
+ key_pair->public_key.public_key,
+ key_pair->private_key.private_key,
+ CURVE25519_BASEPOINT
);
}
-void olm::curve25519_shared_secret(
- olm::Curve25519KeyPair const & our_key,
- olm::Curve25519PublicKey const & their_key,
+void _olm_crypto_curve25519_shared_secret(
+ const struct _olm_curve25519_key_pair *our_key,
+ const struct _olm_curve25519_public_key * their_key,
std::uint8_t * output
) {
- ::curve25519_donna(output, our_key.private_key, their_key.public_key);
+ ::curve25519_donna(output, our_key->private_key.private_key, their_key->public_key);
}
-void olm::ed25519_generate_key(
+void _olm_crypto_ed25519_generate_key(
std::uint8_t const * random_32_bytes,
- olm::Ed25519KeyPair & key_pair
+ struct _olm_ed25519_key_pair *key_pair
) {
::ed25519_create_keypair(
- key_pair.public_key, key_pair.private_key,
+ key_pair->public_key.public_key, key_pair->private_key.private_key,
random_32_bytes
);
}
-void olm::ed25519_sign(
- olm::Ed25519KeyPair const & our_key,
+void _olm_crypto_ed25519_sign(
+ const struct _olm_ed25519_key_pair *our_key,
std::uint8_t const * message, std::size_t message_length,
std::uint8_t * output
) {
::ed25519_sign(
output,
message, message_length,
- our_key.public_key, our_key.private_key
+ our_key->public_key.public_key,
+ our_key->private_key.private_key
);
}
-bool olm::ed25519_verify(
- olm::Ed25519PublicKey const & their_key,
+int _olm_crypto_ed25519_verify(
+ const struct _olm_ed25519_public_key *their_key,
std::uint8_t const * message, std::size_t message_length,
std::uint8_t const * signature
) {
return 0 != ::ed25519_verify(
signature,
message, message_length,
- their_key.public_key
+ their_key->public_key
);
}
-std::size_t olm::aes_encrypt_cbc_length(
+std::size_t _olm_crypto_aes_encrypt_cbc_length(
std::size_t input_length
) {
return input_length + AES_BLOCK_LENGTH - input_length % AES_BLOCK_LENGTH;
}
-void olm::aes_encrypt_cbc(
- olm::Aes256Key const & key,
- olm::Aes256Iv const & iv,
+void _olm_crypto_aes_encrypt_cbc(
+ _olm_aes256_key const *key,
+ _olm_aes256_iv const *iv,
std::uint8_t const * input, std::size_t input_length,
std::uint8_t * output
) {
std::uint32_t key_schedule[AES_KEY_SCHEDULE_LENGTH];
- ::aes_key_setup(key.key, key_schedule, AES_KEY_BITS);
+ ::aes_key_setup(key->key, key_schedule, AES_KEY_BITS);
std::uint8_t input_block[AES_BLOCK_LENGTH];
- std::memcpy(input_block, iv.iv, AES_BLOCK_LENGTH);
+ std::memcpy(input_block, iv->iv, AES_BLOCK_LENGTH);
while (input_length >= AES_BLOCK_LENGTH) {
xor_block<AES_BLOCK_LENGTH>(input_block, input);
::aes_encrypt(input_block, output, key_schedule, AES_KEY_BITS);
@@ -195,17 +201,17 @@ void olm::aes_encrypt_cbc(
}
-std::size_t olm::aes_decrypt_cbc(
- olm::Aes256Key const & key,
- olm::Aes256Iv const & iv,
+std::size_t _olm_crypto_aes_decrypt_cbc(
+ _olm_aes256_key const *key,
+ _olm_aes256_iv const *iv,
std::uint8_t const * input, std::size_t input_length,
std::uint8_t * output
) {
std::uint32_t key_schedule[AES_KEY_SCHEDULE_LENGTH];
- ::aes_key_setup(key.key, key_schedule, AES_KEY_BITS);
+ ::aes_key_setup(key->key, key_schedule, AES_KEY_BITS);
std::uint8_t block1[AES_BLOCK_LENGTH];
std::uint8_t block2[AES_BLOCK_LENGTH];
- std::memcpy(block1, iv.iv, AES_BLOCK_LENGTH);
+ std::memcpy(block1, iv->iv, AES_BLOCK_LENGTH);
for (std::size_t i = 0; i < input_length; i += AES_BLOCK_LENGTH) {
std::memcpy(block2, &input[i], AES_BLOCK_LENGTH);
::aes_decrypt(&input[i], &output[i], key_schedule, AES_KEY_BITS);