axolotl.cpp 14.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#include "axolotl/axolotl.hh"
#include "axolotl/session.hh"
#include "axolotl/account.hh"
#include "axolotl/base64.hh"
#include "axolotl/cipher.hh"

#include <new>
#include <cstring>

namespace {

static AxolotlAccount * to_c(axolotl::Account * account) {
    return reinterpret_cast<AxolotlAccount *>(account);
}

static AxolotlSession * to_c(axolotl::Session * account) {
    return reinterpret_cast<AxolotlSession *>(account);
}

static axolotl::Account * from_c(AxolotlAccount * account) {
    return reinterpret_cast<axolotl::Account *>(account);
}

static axolotl::Session * from_c(AxolotlSession * account) {
    return reinterpret_cast<axolotl::Session *>(account);
}

static std::uint8_t * from_c(void * bytes) {
    return reinterpret_cast<std::uint8_t *>(bytes);
}

static std::uint8_t const * from_c(void const * bytes) {
    return reinterpret_cast<std::uint8_t const *>(bytes);
}

static const std::uint8_t CIPHER_KDF_INFO[] = "Pickle";

static const axolotl::CipherAesSha256 PICKLE_CIPHER(
    CIPHER_KDF_INFO, sizeof(CIPHER_KDF_INFO) -1
);

std::size_t enc_output_length(
    size_t raw_length
) {
    std::size_t length = PICKLE_CIPHER.encrypt_ciphertext_length(raw_length);
    length += PICKLE_CIPHER.mac_length();
    return axolotl::encode_base64_length(length);
}


std::uint8_t * enc_output_pos(
    std::uint8_t * output,
    size_t raw_length
) {
    std::size_t length = PICKLE_CIPHER.encrypt_ciphertext_length(raw_length);
    length += PICKLE_CIPHER.mac_length();
    return output + axolotl::encode_base64_length(length) - length;
}

std::size_t enc_output(
    std::uint8_t const * key, std::size_t key_length,
    std::uint8_t * output, size_t raw_length
) {
    std::size_t ciphertext_length = PICKLE_CIPHER.encrypt_ciphertext_length(
        raw_length
    );
    std::size_t length = ciphertext_length + PICKLE_CIPHER.mac_length();
    std::size_t base64_length = axolotl::encode_base64_length(length);
    std::uint8_t * raw_output = output + base64_length - length;
    length -= PICKLE_CIPHER.mac_length();
    PICKLE_CIPHER.encrypt(
        key, key_length,
        raw_output, raw_length,
        raw_output, ciphertext_length,
        raw_output, length
    );
    axolotl::encode_base64(raw_output, length, output);
    return raw_length;
}

std::size_t enc_input(
    std::uint8_t const * key, std::size_t key_length,
    std::uint8_t * input, size_t b64_length,
    axolotl::ErrorCode & last_error
) {
    std::size_t enc_length = axolotl::decode_base64_length(b64_length);
    if (enc_length == std::size_t(-1)) {
        last_error = axolotl::ErrorCode::INVALID_BASE64;
        return std::size_t(-1);
    }
    axolotl::decode_base64(input, b64_length, input);
    std::size_t raw_length = enc_length - PICKLE_CIPHER.mac_length();
    std::size_t result = PICKLE_CIPHER.decrypt(
        key, key_length,
        input, enc_length,
        input, raw_length,
        input, raw_length
    );
    if (result == std::size_t(-1)) {
        last_error = axolotl::ErrorCode::BAD_ACCOUNT_KEY;
    }
    return result;
}


std::size_t b64_output_length(
    size_t raw_length
) {
    return axolotl::encode_base64_length(raw_length);
}

std::uint8_t * b64_output_pos(
    std::uint8_t * output,
    size_t raw_length
) {
    return output + axolotl::encode_base64_length(raw_length) - raw_length;
}

std::size_t b64_output(
    std::uint8_t * output, size_t raw_length
) {
    std::size_t base64_length = axolotl::encode_base64_length(raw_length);
    std::uint8_t * raw_output = output + base64_length - raw_length;
    axolotl::encode_base64(raw_output, raw_length, output);
    return base64_length;
}

std::size_t b64_input(
    std::uint8_t * input, size_t b64_length,
    axolotl::ErrorCode & last_error
) {
    std::size_t raw_length = axolotl::decode_base64_length(b64_length);
    if (raw_length == std::size_t(-1)) {
        last_error = axolotl::ErrorCode::INVALID_BASE64;
        return std::size_t(-1);
    }
    axolotl::decode_base64(input, b64_length, input);
    return raw_length;
}

const char * errors[9] {
    "SUCCESS",
    "NOT_ENOUGH_RANDOM",
    "OUTPUT_BUFFER_TOO_SMALL",
    "BAD_MESSAGE_VERSION",
    "BAD_MESSAGE_FORMAT",
    "BAD_MESSAGE_MAC",
    "BAD_MESSAGE_KEY_ID",
    "INVALID_BASE64",
    "BAD_ACCOUNT_KEY",
};

} // namespace


extern "C" {


size_t axolotl_error() {
    return std::size_t(-1);
}


const char * axolotl_account_last_error(
    AxolotlSession * account
) {
    unsigned error = unsigned(from_c(account)->last_error);
    if (error < 9) {
        return errors[error];
    } else {
        return "UNKNOWN_ERROR";
    }
}


const char * axolotl_session_last_error(
    AxolotlSession * session
) {
    unsigned error = unsigned(from_c(session)->last_error);
    if (error < 9) {
        return errors[error];
    } else {
        return "UNKNOWN_ERROR";
    }
}


size_t axolotl_account_size() {
    return sizeof(axolotl::Account);
}


size_t axolotl_session_size() {
    return sizeof(axolotl::Session);
}


AxolotlAccount * axolotl_account(
    void * memory
) {
    return to_c(new(memory) axolotl::Account());
}


AxolotlSession * axolotl_session(
    void * memory
) {
    return to_c(new(memory) axolotl::Session());
}


size_t axolotl_pickle_account_length(
    AxolotlAccount * account
) {
    return enc_output_length(pickle_length(*from_c(account)));
}


size_t axolotl_pickle_session_length(
    AxolotlSession * session
) {
    return enc_output_length(pickle_length(*from_c(session)));
}


size_t axolotl_pickle_account(
    AxolotlAccount * account,
    void const * key, size_t key_length,
    void * pickled, size_t pickled_length
) {
    axolotl::Account & object = *from_c(account);
    std::size_t raw_length = pickle_length(object);
    if (pickled_length < enc_output_length(raw_length)) {
        object.last_error = axolotl::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
        return size_t(-1);
    }
    pickle(enc_output_pos(from_c(pickled), raw_length), object);
    return enc_output(from_c(key), key_length, from_c(pickled), raw_length);
}


size_t axolotl_pickle_session(
    AxolotlSession * session,
    void const * key, size_t key_length,
    void * pickled, size_t pickled_length
) {
    axolotl::Session & object = *from_c(session);
    std::size_t raw_length = pickle_length(object);
    if (pickled_length < enc_output_length(raw_length)) {
        object.last_error = axolotl::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
        return size_t(-1);
    }
    pickle(enc_output_pos(from_c(pickled), raw_length), object);
    return enc_output(from_c(key), key_length, from_c(pickled), raw_length);
}


size_t axolotl_unpickle_account(
    AxolotlAccount * account,
    void const * key, size_t key_length,
    void * pickled, size_t pickled_length
) {
    axolotl::Account & object = *from_c(account);
    std::uint8_t * const pos = from_c(pickled);
    std::size_t raw_length = enc_input(
        from_c(key), key_length, pos, pickled_length, object.last_error
    );
    if (raw_length == std::size_t(-1)) {
        return std::size_t(-1);
    }
    std::uint8_t * const end = pos + raw_length;
    unpickle(pos, end, object);
    return pickled_length;
}


size_t axolotl_unpickle_session(
    AxolotlSession * session,
    void const * key, size_t key_length,
    void * pickled, size_t pickled_length
) {
    axolotl::Session & object = *from_c(session);
    std::uint8_t * const pos = from_c(pickled);
    std::size_t raw_length = enc_input(
        from_c(key), key_length, pos, pickled_length, object.last_error
    );
    if (raw_length == std::size_t(-1)) {
        return std::size_t(-1);
    }
    std::uint8_t * const end = pos + raw_length;
    unpickle(pos, end, object);
    return pickled_length;
}


size_t axolotl_create_account_random_length(
    AxolotlAccount * account
) {
    return from_c(account)->new_account_random_length();
}


size_t axolotl_create_account(
    AxolotlAccount * account,
    void const * random, size_t random_length
) {
    return from_c(account)->new_account(from_c(random), random_length);
}

namespace {

static const std::size_t OUTPUT_KEY_LENGTH = 2 + 10 + 2 +
        axolotl::encode_base64_length(32) + 3;

void output_key(
    axolotl::LocalKey const & key,
    std::uint8_t sep,
    std::uint8_t * output
) {
    output[0] = sep;
    output[1] = '[';
    std::memset(output + 2, ' ', 10);
    uint32_t value = key.id;
    uint8_t * number = output + 11;
    *number = '0' + value % 10;
    value /= 10;
    while (value) {
        *(--number) = '0' + value % 10;
        value /= 10;
    }
    output[12] = ',';
    output[13] = '"';
    axolotl::encode_base64(key.key.public_key, 32, output + 14);
    output[OUTPUT_KEY_LENGTH - 3] = '"';
    output[OUTPUT_KEY_LENGTH - 2] = ']';
    output[OUTPUT_KEY_LENGTH - 1] = '\n';
}

} // namespace


size_t axolotl_account_identity_keys_length(
    AxolotlAccount * account
) {
    return OUTPUT_KEY_LENGTH * 2 + 1;
}


size_t axolotl_account_identity_keys(
    AxolotlAccount * account,
    void * identity_keys, size_t identity_key_length
) {
    std::size_t length = OUTPUT_KEY_LENGTH * 2 + 1;
    if (identity_key_length < length) {
        from_c(account)->last_error =
            axolotl::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
        return size_t(-1);
    }
    std::uint8_t * output = from_c(identity_keys);
    output_key(from_c(account)->identity_key, '[', output);
    output += OUTPUT_KEY_LENGTH;
    output += OUTPUT_KEY_LENGTH;
    output[0] = ']';
    return length;
}


size_t axolotl_account_one_time_keys_length(
    AxolotlAccount * account
) {
    size_t count = from_c(account)->one_time_keys.size();
    return OUTPUT_KEY_LENGTH * (count + 1) + 1;
}


size_t axolotl_account_one_time_keys(
    AxolotlAccount * account,
    void * identity_keys, size_t identity_key_length
) {
    std::size_t length = axolotl_account_one_time_keys_length(account);
    if (identity_key_length < length) {
        from_c(account)->last_error =
            axolotl::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
        return size_t(-1);
    }
    std::uint8_t * output = from_c(identity_keys);
    output_key(from_c(account)->last_resort_one_time_key, '[', output);
    output += OUTPUT_KEY_LENGTH;
    for (auto const & key : from_c(account)->one_time_keys) {
        output_key(key, ',', output);
        output += OUTPUT_KEY_LENGTH;
    }
    output[0] = ']';
    return length;
}


size_t axolotl_create_outbound_session(
    AxolotlSession * session,
    AxolotlAccount * account,
    void const * their_identity_key, size_t their_identity_key_length,
    unsigned their_one_time_key_id,
    void const * their_one_time_key, size_t their_one_time_key_length,
    void const * random, size_t random_length
) {
    if (axolotl::decode_base64_length(their_identity_key_length) != 32
            || axolotl::decode_base64_length(their_one_time_key_length) != 32
    ) {
        from_c(session)->last_error = axolotl::ErrorCode::INVALID_BASE64;
        return std::size_t(-1);
    }
    axolotl::Curve25519PublicKey identity_key;
    axolotl::RemoteKey one_time_key;

    axolotl::decode_base64(
        from_c(their_identity_key), their_identity_key_length,
        identity_key.public_key
    );
    one_time_key.id = their_one_time_key_id;
    axolotl::decode_base64(
        from_c(their_one_time_key), their_one_time_key_length,
        one_time_key.key.public_key
    );

    return from_c(session)->new_outbound_session(
        *from_c(account), identity_key, one_time_key,
        from_c(random), random_length
    );
}


size_t axolotl_create_inbound_session(
    AxolotlSession * session,
    AxolotlAccount * account,
    void * one_time_key_message, size_t message_length
) {
    std::size_t raw_length = b64_input(
        from_c(one_time_key_message), message_length, from_c(session)->last_error
    );
    if (raw_length == std::size_t(-1)) {
        return std::size_t(-1);
    }
    return from_c(session)->new_inbound_session(
        *from_c(account), from_c(one_time_key_message), raw_length
    );
}


size_t axolotl_matches_inbound_session(
    AxolotlSession * session,
    void * one_time_key_message, size_t message_length
) {
    std::size_t raw_length = b64_input(
        from_c(one_time_key_message), message_length, from_c(session)->last_error
    );
    if (raw_length == std::size_t(-1)) {
        return std::size_t(-1);
    }
    bool matches = from_c(session)->matches_inbound_session(
        from_c(one_time_key_message), raw_length
    );
    return matches ? 1 : 0;
}


size_t axolotl_encrypt_message_type(
    AxolotlSession * session
) {
    return size_t(from_c(session)->encrypt_message_type());
}


size_t axolotl_encrypt_random_length(
    AxolotlSession * session
) {
    return from_c(session)->encrypt_random_length();
}


size_t axolotl_encrypt_message_length(
    AxolotlSession * session,
    size_t plaintext_length
) {
    return b64_output_length(
        from_c(session)->encrypt_message_length(plaintext_length)
    );
}


size_t axolotl_encrypt(
    AxolotlSession * session,
    void const * plaintext, size_t plaintext_length,
    void const * random, size_t random_length,
    void * message, size_t message_length
) {
    std::size_t raw_length = from_c(session)->encrypt_message_length(
        plaintext_length
    );
    if (message_length < raw_length) {
        from_c(session)->last_error =
            axolotl::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
        return std::size_t(-1);
    }
    from_c(session)->encrypt(
        from_c(plaintext), plaintext_length,
        from_c(random), random_length,
        b64_output_pos(from_c(message), raw_length), raw_length
    );
    return b64_output(from_c(message), raw_length);
}


size_t axolotl_decrypt_max_plaintext_length(
    AxolotlSession * session,
    size_t message_type,
    void * message, size_t message_length
) {
    std::size_t raw_length = b64_input(
        from_c(message), message_length, from_c(session)->last_error
    );
    if (raw_length == std::size_t(-1)) {
        return std::size_t(-1);
    }
    return from_c(session)->decrypt_max_plaintext_length(
        axolotl::MessageType(message_type), from_c(message), raw_length
    );
}


size_t axolotl_decrypt(
    AxolotlSession * session,
    size_t message_type,
    void * message, size_t message_length,
    void * plaintext, size_t max_plaintext_length
) {
    std::size_t raw_length = b64_input(
        from_c(message), message_length, from_c(session)->last_error
    );
    if (raw_length == std::size_t(-1)) {
        return std::size_t(-1);
    }
    return from_c(session)->decrypt(
        axolotl::MessageType(message_type), from_c(message), raw_length,
        from_c(plaintext), max_plaintext_length
    );
}

}