1 /* Copyright 2018-2019 New Vector 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.
17 #include "olm/base64.h"
18 #include "olm/crypto.h"
19 #include "olm/error.h"
20 #include "olm/memory.h"
23 enum OlmErrorCode last_error;
24 struct _olm_curve25519_key_pair curve25519_key;
25 uint8_t secret[CURVE25519_SHARED_SECRET_LENGTH];
29 const char * olm_sas_last_error(
32 return _olm_error_to_string(sas->last_error);
35 enum OlmErrorCode olm_sas_last_error_code(
38 return sas->last_error;
41 size_t olm_sas_size(void) {
42 return sizeof(OlmSAS);
48 _olm_unset(memory, sizeof(OlmSAS));
49 return (OlmSAS *) memory;
55 _olm_unset(sas, sizeof(OlmSAS));
56 return sizeof(OlmSAS);
59 size_t olm_create_sas_random_length(const OlmSAS * sas) {
60 return CURVE25519_KEY_LENGTH;
63 size_t olm_create_sas(
65 void * random, size_t random_length
67 if (random_length < olm_create_sas_random_length(sas)) {
68 sas->last_error = OLM_NOT_ENOUGH_RANDOM;
71 _olm_crypto_curve25519_generate_key((uint8_t *) random, &sas->curve25519_key);
72 sas->their_key_set = 0;
76 size_t olm_sas_pubkey_length(const OlmSAS * sas) {
77 return _olm_encode_base64_length(CURVE25519_KEY_LENGTH);
80 size_t olm_sas_get_pubkey(
82 void * pubkey, size_t pubkey_length
84 if (pubkey_length < olm_sas_pubkey_length(sas)) {
85 sas->last_error = OLM_OUTPUT_BUFFER_TOO_SMALL;
89 (const uint8_t *)sas->curve25519_key.public_key.public_key,
90 CURVE25519_KEY_LENGTH,
96 size_t olm_sas_set_their_key(
98 void * their_key, size_t their_key_length
100 if (their_key_length < olm_sas_pubkey_length(sas)) {
101 sas->last_error = OLM_INPUT_BUFFER_TOO_SMALL;
105 size_t ret = _olm_decode_base64(their_key, their_key_length, their_key);
106 if (ret == (size_t)-1) {
107 sas->last_error = OLM_INVALID_BASE64;
111 _olm_crypto_curve25519_shared_secret(&sas->curve25519_key, their_key, sas->secret);
112 sas->their_key_set = 1;
116 int olm_sas_is_their_key_set(
119 return sas->their_key_set;
122 size_t olm_sas_generate_bytes(
124 const void * info, size_t info_length,
125 void * output, size_t output_length
127 if (!sas->their_key_set) {
128 sas->last_error = OLM_SAS_THEIR_KEY_NOT_SET;
131 _olm_crypto_hkdf_sha256(
132 sas->secret, sizeof(sas->secret),
134 (const uint8_t *) info, info_length,
135 output, output_length
140 size_t olm_sas_mac_length(
143 return _olm_encode_base64_length(SHA256_OUTPUT_LENGTH);
146 // A version of the calculate mac function that produces base64 strings that are
147 // compatible with other base64 implementations.
148 size_t olm_sas_calculate_mac_fixed_base64(
150 const void * input, size_t input_length,
151 const void * info, size_t info_length,
152 void * mac, size_t mac_length
154 if (mac_length < olm_sas_mac_length(sas)) {
155 sas->last_error = OLM_OUTPUT_BUFFER_TOO_SMALL;
158 if (!sas->their_key_set) {
159 sas->last_error = OLM_SAS_THEIR_KEY_NOT_SET;
163 _olm_crypto_hkdf_sha256(
164 sas->secret, sizeof(sas->secret),
166 (const uint8_t *) info, info_length,
170 uint8_t temp_mac[32];
171 _olm_crypto_hmac_sha256(key, 32, input, input_length, temp_mac);
172 _olm_encode_base64((const uint8_t *)temp_mac, SHA256_OUTPUT_LENGTH, (uint8_t *)mac);
178 size_t olm_sas_calculate_mac(
180 const void * input, size_t input_length,
181 const void * info, size_t info_length,
182 void * mac, size_t mac_length
184 if (mac_length < olm_sas_mac_length(sas)) {
185 sas->last_error = OLM_OUTPUT_BUFFER_TOO_SMALL;
188 if (!sas->their_key_set) {
189 sas->last_error = OLM_SAS_THEIR_KEY_NOT_SET;
193 _olm_crypto_hkdf_sha256(
194 sas->secret, sizeof(sas->secret),
196 (const uint8_t *) info, info_length,
199 _olm_crypto_hmac_sha256(key, 32, input, input_length, mac);
200 _olm_encode_base64((const uint8_t *)mac, SHA256_OUTPUT_LENGTH, (uint8_t *)mac);
204 // for compatibility with an old version of Riot
205 size_t olm_sas_calculate_mac_long_kdf(
207 const void * input, size_t input_length,
208 const void * info, size_t info_length,
209 void * mac, size_t mac_length
211 if (mac_length < olm_sas_mac_length(sas)) {
212 sas->last_error = OLM_OUTPUT_BUFFER_TOO_SMALL;
215 if (!sas->their_key_set) {
216 sas->last_error = OLM_SAS_THEIR_KEY_NOT_SET;
220 _olm_crypto_hkdf_sha256(
221 sas->secret, sizeof(sas->secret),
223 (const uint8_t *) info, info_length,
226 _olm_crypto_hmac_sha256(key, 256, input, input_length, mac);
227 _olm_encode_base64((const uint8_t *)mac, SHA256_OUTPUT_LENGTH, (uint8_t *)mac);