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.
15 #include "olm/cipher.h"
16 #include "olm/crypto.h"
17 #include "olm/memory.hh"
20 const std::size_t HMAC_KEY_LENGTH = 32;
25 _olm_aes256_key aes_key;
26 std::uint8_t mac_key[HMAC_KEY_LENGTH];
27 _olm_aes256_iv aes_iv;
31 static void derive_keys(
32 std::uint8_t const * kdf_info, std::size_t kdf_info_length,
33 std::uint8_t const * key, std::size_t key_length,
36 std::uint8_t derived_secrets[
37 AES256_KEY_LENGTH + HMAC_KEY_LENGTH + AES256_IV_LENGTH
39 _olm_crypto_hkdf_sha256(
42 kdf_info, kdf_info_length,
43 derived_secrets, sizeof(derived_secrets)
45 std::uint8_t const * pos = derived_secrets;
46 pos = olm::load_array(keys.aes_key.key, pos);
47 pos = olm::load_array(keys.mac_key, pos);
48 pos = olm::load_array(keys.aes_iv.iv, pos);
49 olm::unset(derived_secrets);
52 static const std::size_t MAC_LENGTH = 8;
54 size_t aes_sha_256_cipher_mac_length(const struct _olm_cipher *cipher) {
58 size_t aes_sha_256_cipher_encrypt_ciphertext_length(
59 const struct _olm_cipher *cipher, size_t plaintext_length
61 return _olm_crypto_aes_encrypt_cbc_length(plaintext_length);
64 size_t aes_sha_256_cipher_encrypt(
65 const struct _olm_cipher *cipher,
66 uint8_t const * key, size_t key_length,
67 uint8_t const * plaintext, size_t plaintext_length,
68 uint8_t * ciphertext, size_t ciphertext_length,
69 uint8_t * output, size_t output_length
71 auto *c = reinterpret_cast<const _olm_cipher_aes_sha_256 *>(cipher);
74 < aes_sha_256_cipher_encrypt_ciphertext_length(cipher, plaintext_length)
75 || output_length < MAC_LENGTH) {
76 return std::size_t(-1);
79 struct DerivedKeys keys;
80 std::uint8_t mac[SHA256_OUTPUT_LENGTH];
82 derive_keys(c->kdf_info, c->kdf_info_length, key, key_length, keys);
84 _olm_crypto_aes_encrypt_cbc(
85 &keys.aes_key, &keys.aes_iv, plaintext, plaintext_length, ciphertext
88 _olm_crypto_hmac_sha256(
89 keys.mac_key, HMAC_KEY_LENGTH, output, output_length - MAC_LENGTH, mac
92 std::memcpy(output + output_length - MAC_LENGTH, mac, MAC_LENGTH);
99 size_t aes_sha_256_cipher_decrypt_max_plaintext_length(
100 const struct _olm_cipher *cipher,
101 size_t ciphertext_length
103 return ciphertext_length;
106 size_t aes_sha_256_cipher_decrypt(
107 const struct _olm_cipher *cipher,
108 uint8_t const * key, size_t key_length,
109 uint8_t const * input, size_t input_length,
110 uint8_t const * ciphertext, size_t ciphertext_length,
111 uint8_t * plaintext, size_t max_plaintext_length
113 if (max_plaintext_length
114 < aes_sha_256_cipher_decrypt_max_plaintext_length(cipher, ciphertext_length)
115 || input_length < MAC_LENGTH) {
116 return std::size_t(-1);
119 auto *c = reinterpret_cast<const _olm_cipher_aes_sha_256 *>(cipher);
122 std::uint8_t mac[SHA256_OUTPUT_LENGTH];
124 derive_keys(c->kdf_info, c->kdf_info_length, key, key_length, keys);
126 _olm_crypto_hmac_sha256(
127 keys.mac_key, HMAC_KEY_LENGTH, input, input_length - MAC_LENGTH, mac
130 std::uint8_t const * input_mac = input + input_length - MAC_LENGTH;
131 if (!olm::is_equal(input_mac, mac, MAC_LENGTH)) {
133 return std::size_t(-1);
136 std::size_t plaintext_length = _olm_crypto_aes_decrypt_cbc(
137 &keys.aes_key, &keys.aes_iv, ciphertext, ciphertext_length, plaintext
141 return plaintext_length;
146 const struct _olm_cipher_ops _olm_cipher_aes_sha_256_ops = {
147 aes_sha_256_cipher_mac_length,
148 aes_sha_256_cipher_encrypt_ciphertext_length,
149 aes_sha_256_cipher_encrypt,
150 aes_sha_256_cipher_decrypt_max_plaintext_length,
151 aes_sha_256_cipher_decrypt,