1 /* Copyright 2015-2016 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/message.hh"
17 #include "olm/memory.hh"
22 static std::size_t varint_length(
25 std::size_t result = 1;
26 while (value >= 128U) {
35 static std::uint8_t * varint_encode(
36 std::uint8_t * output,
39 while (value >= 128U) {
40 *(output++) = (0x7F & value) | 0x80;
49 static T varint_decode(
50 std::uint8_t const * varint_start,
51 std::uint8_t const * varint_end
54 if (varint_end == varint_start) {
59 value |= 0x7F & *(--varint_end);
60 } while (varint_end != varint_start);
65 static std::uint8_t const * varint_skip(
66 std::uint8_t const * input,
67 std::uint8_t const * input_end
69 while (input != input_end) {
70 std::uint8_t tmp = *(input++);
71 if ((tmp & 0x80) == 0) {
79 static std::size_t varstring_length(
80 std::size_t string_length
82 return varint_length(string_length) + string_length;
85 static std::size_t const VERSION_LENGTH = 1;
86 static std::uint8_t const RATCHET_KEY_TAG = 012;
87 static std::uint8_t const COUNTER_TAG = 020;
88 static std::uint8_t const CIPHERTEXT_TAG = 042;
90 static std::uint8_t * encode(
96 return varint_encode(pos, value);
99 static std::uint8_t * encode(
102 std::uint8_t * & value, std::size_t value_length
105 pos = varint_encode(pos, value_length);
107 return pos + value_length;
110 static std::uint8_t const * decode(
111 std::uint8_t const * pos, std::uint8_t const * end,
113 std::uint32_t & value, bool & has_value
115 if (pos != end && *pos == tag) {
117 std::uint8_t const * value_start = pos;
118 pos = varint_skip(pos, end);
119 value = varint_decode<std::uint32_t>(value_start, pos);
126 static std::uint8_t const * decode(
127 std::uint8_t const * pos, std::uint8_t const * end,
129 std::uint8_t const * & value, std::size_t & value_length
131 if (pos != end && *pos == tag) {
133 std::uint8_t const * len_start = pos;
134 pos = varint_skip(pos, end);
135 std::size_t len = varint_decode<std::size_t>(len_start, pos);
136 if (len > std::size_t(end - pos)) return end;
144 static std::uint8_t const * skip_unknown(
145 std::uint8_t const * pos, std::uint8_t const * end
149 if ((tag & 0x7) == 0) {
150 pos = varint_skip(pos, end);
151 pos = varint_skip(pos, end);
152 } else if ((tag & 0x7) == 2) {
153 pos = varint_skip(pos, end);
154 std::uint8_t const * len_start = pos;
155 pos = varint_skip(pos, end);
156 std::size_t len = varint_decode<std::size_t>(len_start, pos);
157 if (len > std::size_t(end - pos)) return end;
169 std::size_t olm::encode_message_length(
170 std::uint32_t counter,
171 std::size_t ratchet_key_length,
172 std::size_t ciphertext_length,
173 std::size_t mac_length
175 std::size_t length = VERSION_LENGTH;
176 length += 1 + varstring_length(ratchet_key_length);
177 length += 1 + varint_length(counter);
178 length += 1 + varstring_length(ciphertext_length);
179 length += mac_length;
184 void olm::encode_message(
185 olm::MessageWriter & writer,
186 std::uint8_t version,
187 std::uint32_t counter,
188 std::size_t ratchet_key_length,
189 std::size_t ciphertext_length,
190 std::uint8_t * output
192 std::uint8_t * pos = output;
194 pos = encode(pos, RATCHET_KEY_TAG, writer.ratchet_key, ratchet_key_length);
195 pos = encode(pos, COUNTER_TAG, counter);
196 pos = encode(pos, CIPHERTEXT_TAG, writer.ciphertext, ciphertext_length);
200 void olm::decode_message(
201 olm::MessageReader & reader,
202 std::uint8_t const * input, std::size_t input_length,
203 std::size_t mac_length
205 std::uint8_t const * pos = input;
206 std::uint8_t const * end = input + input_length - mac_length;
207 std::uint8_t const * unknown = nullptr;
210 reader.has_counter = false;
212 reader.input = input;
213 reader.input_length = input_length;
214 reader.ratchet_key = nullptr;
215 reader.ratchet_key_length = 0;
216 reader.ciphertext = nullptr;
217 reader.ciphertext_length = 0;
219 if (input_length < mac_length) return;
221 if (pos == end) return;
222 reader.version = *(pos++);
227 pos, end, RATCHET_KEY_TAG,
228 reader.ratchet_key, reader.ratchet_key_length
231 pos, end, COUNTER_TAG,
232 reader.counter, reader.has_counter
235 pos, end, CIPHERTEXT_TAG,
236 reader.ciphertext, reader.ciphertext_length
238 if (unknown == pos) {
239 pos = skip_unknown(pos, end);
247 static std::uint8_t const ONE_TIME_KEY_ID_TAG = 012;
248 static std::uint8_t const BASE_KEY_TAG = 022;
249 static std::uint8_t const IDENTITY_KEY_TAG = 032;
250 static std::uint8_t const MESSAGE_TAG = 042;
255 std::size_t olm::encode_one_time_key_message_length(
256 std::size_t one_time_key_length,
257 std::size_t identity_key_length,
258 std::size_t base_key_length,
259 std::size_t message_length
261 std::size_t length = VERSION_LENGTH;
262 length += 1 + varstring_length(one_time_key_length);
263 length += 1 + varstring_length(identity_key_length);
264 length += 1 + varstring_length(base_key_length);
265 length += 1 + varstring_length(message_length);
270 void olm::encode_one_time_key_message(
271 olm::PreKeyMessageWriter & writer,
272 std::uint8_t version,
273 std::size_t identity_key_length,
274 std::size_t base_key_length,
275 std::size_t one_time_key_length,
276 std::size_t message_length,
277 std::uint8_t * output
279 std::uint8_t * pos = output;
281 pos = encode(pos, ONE_TIME_KEY_ID_TAG, writer.one_time_key, one_time_key_length);
282 pos = encode(pos, BASE_KEY_TAG, writer.base_key, base_key_length);
283 pos = encode(pos, IDENTITY_KEY_TAG, writer.identity_key, identity_key_length);
284 pos = encode(pos, MESSAGE_TAG, writer.message, message_length);
288 void olm::decode_one_time_key_message(
289 PreKeyMessageReader & reader,
290 std::uint8_t const * input, std::size_t input_length
292 std::uint8_t const * pos = input;
293 std::uint8_t const * end = input + input_length;
294 std::uint8_t const * unknown = nullptr;
297 reader.one_time_key = nullptr;
298 reader.one_time_key_length = 0;
299 reader.identity_key = nullptr;
300 reader.identity_key_length = 0;
301 reader.base_key = nullptr;
302 reader.base_key_length = 0;
303 reader.message = nullptr;
304 reader.message_length = 0;
306 if (pos == end) return;
307 reader.version = *(pos++);
312 pos, end, ONE_TIME_KEY_ID_TAG,
313 reader.one_time_key, reader.one_time_key_length
316 pos, end, BASE_KEY_TAG,
317 reader.base_key, reader.base_key_length
320 pos, end, IDENTITY_KEY_TAG,
321 reader.identity_key, reader.identity_key_length
324 pos, end, MESSAGE_TAG,
325 reader.message, reader.message_length
327 if (unknown == pos) {
328 pos = skip_unknown(pos, end);
335 static const std::uint8_t GROUP_MESSAGE_INDEX_TAG = 010;
336 static const std::uint8_t GROUP_CIPHERTEXT_TAG = 022;
338 size_t _olm_encode_group_message_length(
339 uint32_t message_index,
340 size_t ciphertext_length,
342 size_t signature_length
344 size_t length = VERSION_LENGTH;
345 length += 1 + varint_length(message_index);
346 length += 1 + varstring_length(ciphertext_length);
347 length += mac_length;
348 length += signature_length;
353 size_t _olm_encode_group_message(
355 uint32_t message_index,
356 size_t ciphertext_length,
358 uint8_t **ciphertext_ptr
360 std::uint8_t * pos = output;
363 pos = encode(pos, GROUP_MESSAGE_INDEX_TAG, message_index);
364 pos = encode(pos, GROUP_CIPHERTEXT_TAG, *ciphertext_ptr, ciphertext_length);
368 void _olm_decode_group_message(
369 const uint8_t *input, size_t input_length,
370 size_t mac_length, size_t signature_length,
371 struct _OlmDecodeGroupMessageResults *results
373 std::uint8_t const * pos = input;
374 std::size_t trailer_length = mac_length + signature_length;
375 std::uint8_t const * end = input + input_length - trailer_length;
376 std::uint8_t const * unknown = nullptr;
378 bool has_message_index = false;
379 results->version = 0;
380 results->message_index = 0;
381 results->has_message_index = (int)has_message_index;
382 results->ciphertext = nullptr;
383 results->ciphertext_length = 0;
385 if (input_length < trailer_length) return;
387 if (pos == end) return;
388 results->version = *(pos++);
393 pos, end, GROUP_MESSAGE_INDEX_TAG,
394 results->message_index, has_message_index
397 pos, end, GROUP_CIPHERTEXT_TAG,
398 results->ciphertext, results->ciphertext_length
400 if (unknown == pos) {
401 pos = skip_unknown(pos, end);
405 results->has_message_index = (int)has_message_index;