1 /* Copyright 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.
16 #include "olm/pickle_encoding.h"
18 #include "olm/base64.h"
19 #include "olm/cipher.h"
22 static const struct _olm_cipher_aes_sha_256 PICKLE_CIPHER =
23 OLM_CIPHER_INIT_AES_SHA_256("Pickle");
25 size_t _olm_enc_output_length(
28 const struct _olm_cipher *cipher = OLM_CIPHER_BASE(&PICKLE_CIPHER);
29 size_t length = cipher->ops->encrypt_ciphertext_length(cipher, raw_length);
30 length += cipher->ops->mac_length(cipher);
31 return _olm_encode_base64_length(length);
34 uint8_t * _olm_enc_output_pos(
38 const struct _olm_cipher *cipher = OLM_CIPHER_BASE(&PICKLE_CIPHER);
39 size_t length = cipher->ops->encrypt_ciphertext_length(cipher, raw_length);
40 length += cipher->ops->mac_length(cipher);
41 return output + _olm_encode_base64_length(length) - length;
44 size_t _olm_enc_output(
45 uint8_t const * key, size_t key_length,
46 uint8_t * output, size_t raw_length
48 const struct _olm_cipher *cipher = OLM_CIPHER_BASE(&PICKLE_CIPHER);
49 size_t ciphertext_length = cipher->ops->encrypt_ciphertext_length(
52 size_t length = ciphertext_length + cipher->ops->mac_length(cipher);
53 size_t base64_length = _olm_encode_base64_length(length);
54 uint8_t * raw_output = output + base64_length - length;
58 raw_output, raw_length,
59 raw_output, ciphertext_length,
62 _olm_encode_base64(raw_output, length, output);
67 size_t _olm_enc_input(uint8_t const * key, size_t key_length,
68 uint8_t * input, size_t b64_length,
69 enum OlmErrorCode * last_error
71 size_t enc_length = _olm_decode_base64_length(b64_length);
72 if (enc_length == (size_t)-1) {
74 *last_error = OLM_INVALID_BASE64;
78 _olm_decode_base64(input, b64_length, input);
79 const struct _olm_cipher *cipher = OLM_CIPHER_BASE(&PICKLE_CIPHER);
80 size_t raw_length = enc_length - cipher->ops->mac_length(cipher);
81 size_t result = cipher->ops->decrypt(
88 if (result == (size_t)-1 && last_error) {
89 *last_error = OLM_BAD_ACCOUNT_KEY;