cipher.cpp 4.21 KB
Newer Older
Mark Haines's avatar
Mark Haines committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* Copyright 2015 OpenMarket Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
15
#include "olm/cipher.h"
16
#include "olm/crypto.h"
17
#include "olm/memory.hh"
18
19
#include <cstring>

20
21
const std::size_t HMAC_KEY_LENGTH = 32;

22
23
24
namespace {

struct DerivedKeys {
25
    _olm_aes256_key aes_key;
26
    std::uint8_t mac_key[HMAC_KEY_LENGTH];
27
    _olm_aes256_iv aes_iv;
28
29
30
31
32
33
34
35
};


static void derive_keys(
    std::uint8_t const * kdf_info, std::size_t kdf_info_length,
    std::uint8_t const * key, std::size_t key_length,
    DerivedKeys & keys
) {
36
37
38
    std::uint8_t derived_secrets[
        AES256_KEY_LENGTH + HMAC_KEY_LENGTH + AES256_IV_LENGTH
    ];
39
    _olm_crypto_hkdf_sha256(
40
        key, key_length,
41
        nullptr, 0,
42
43
44
        kdf_info, kdf_info_length,
        derived_secrets, sizeof(derived_secrets)
    );
45
46
47
48
    std::uint8_t const * pos = derived_secrets;
    pos = olm::load_array(keys.aes_key.key, pos);
    pos = olm::load_array(keys.mac_key, pos);
    pos = olm::load_array(keys.aes_iv.iv, pos);
49
    olm::unset(derived_secrets);
50
51
52
53
}

static const std::size_t MAC_LENGTH = 8;

54
size_t aes_sha_256_cipher_mac_length(const struct _olm_cipher *cipher) {
55
56
57
    return MAC_LENGTH;
}

58
size_t aes_sha_256_cipher_encrypt_ciphertext_length(
59
        const struct _olm_cipher *cipher, size_t plaintext_length
60
) {
61
    return _olm_crypto_aes_encrypt_cbc_length(plaintext_length);
62
63
}

64
size_t aes_sha_256_cipher_encrypt(
65
    const struct _olm_cipher *cipher,
66
67
68
69
70
    uint8_t const * key, size_t key_length,
    uint8_t const * plaintext, size_t plaintext_length,
    uint8_t * ciphertext, size_t ciphertext_length,
    uint8_t * output, size_t output_length
) {
71
    auto *c = reinterpret_cast<const _olm_cipher_aes_sha_256 *>(cipher);
72

73
74
    if (aes_sha_256_cipher_encrypt_ciphertext_length(cipher, plaintext_length)
            < ciphertext_length) {
75
76
        return std::size_t(-1);
    }
77

78
    struct DerivedKeys keys;
79
    std::uint8_t mac[SHA256_OUTPUT_LENGTH];
80

81
    derive_keys(c->kdf_info, c->kdf_info_length, key, key_length, keys);
82

83
84
    _olm_crypto_aes_encrypt_cbc(
        &keys.aes_key, &keys.aes_iv, plaintext, plaintext_length, ciphertext
85
86
    );

87
    _olm_crypto_hmac_sha256(
88
        keys.mac_key, HMAC_KEY_LENGTH, output, output_length - MAC_LENGTH, mac
89
90
91
92
    );

    std::memcpy(output + output_length - MAC_LENGTH, mac, MAC_LENGTH);

93
    olm::unset(keys);
94
95
96
97
    return output_length;
}


98
size_t aes_sha_256_cipher_decrypt_max_plaintext_length(
99
    const struct _olm_cipher *cipher,
100
101
    size_t ciphertext_length
) {
102
103
104
    return ciphertext_length;
}

105
size_t aes_sha_256_cipher_decrypt(
106
    const struct _olm_cipher *cipher,
107
108
109
110
111
    uint8_t const * key, size_t key_length,
    uint8_t const * input, size_t input_length,
    uint8_t const * ciphertext, size_t ciphertext_length,
    uint8_t * plaintext, size_t max_plaintext_length
) {
112
    auto *c = reinterpret_cast<const _olm_cipher_aes_sha_256 *>(cipher);
113

114
    DerivedKeys keys;
115
    std::uint8_t mac[SHA256_OUTPUT_LENGTH];
116

117
    derive_keys(c->kdf_info, c->kdf_info_length, key, key_length, keys);
118

119
    _olm_crypto_hmac_sha256(
120
        keys.mac_key, HMAC_KEY_LENGTH, input, input_length - MAC_LENGTH, mac
121
122
123
    );

    std::uint8_t const * input_mac = input + input_length - MAC_LENGTH;
124
125
    if (!olm::is_equal(input_mac, mac, MAC_LENGTH)) {
        olm::unset(keys);
126
127
128
        return std::size_t(-1);
    }

129
130
    std::size_t plaintext_length = _olm_crypto_aes_decrypt_cbc(
        &keys.aes_key, &keys.aes_iv, ciphertext, ciphertext_length, plaintext
131
132
    );

133
    olm::unset(keys);
134
135
    return plaintext_length;
}
136

137
} // namespace
138

139
const struct _olm_cipher_ops _olm_cipher_aes_sha_256_ops = {
140
141
142
143
144
145
  aes_sha_256_cipher_mac_length,
  aes_sha_256_cipher_encrypt_ciphertext_length,
  aes_sha_256_cipher_encrypt,
  aes_sha_256_cipher_decrypt_max_plaintext_length,
  aes_sha_256_cipher_decrypt,
};