account.hh 5.71 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
16
#ifndef OLM_ACCOUNT_HH_
#define OLM_ACCOUNT_HH_
17

18
#include "olm/list.hh"
19
#include "olm/crypto.h"
20
#include "olm/error.h"
21
22
23

#include <cstdint>

24
namespace olm {
25
26


27
struct IdentityKeys {
28
29
    _olm_ed25519_key_pair ed25519_key;
    _olm_curve25519_key_pair curve25519_key;
30
31
32
};

struct OneTimeKey {
33
    std::uint32_t id;
34
    bool published;
35
    _olm_curve25519_key_pair key;
36
37
38
39
40
};


static std::size_t const MAX_ONE_TIME_KEYS = 100;

41

42
struct Account {
43
    Account();
44
45
    IdentityKeys identity_keys;
    List<OneTimeKey, MAX_ONE_TIME_KEYS> one_time_keys;
Hubert Chathi's avatar
Hubert Chathi committed
46
47
    OneTimeKey current_fallback_key;
    OneTimeKey prev_fallback_key;
48
    std::uint32_t next_one_time_key_id;
49
    OlmErrorCode last_error;
50
51
52
53

    /** Number of random bytes needed to create a new account */
    std::size_t new_account_random_length();

54
55
    /** Create a new account. Returns std::size_t(-1) on error. If the number of
     * random bytes is too small then last_error will be NOT_ENOUGH_RANDOM */
56
    std::size_t new_account(
57
58
59
        uint8_t const * random, std::size_t random_length
    );

60
    /** Number of bytes needed to output the identity keys for this account */
61
    std::size_t get_identity_json_length();
62
63

    /** Output the identity keys for this account as JSON in the following
64
     * format:
65
     *
66
67
     *    {"curve25519":"<43 base64 characters>"
     *    ,"ed25519":"<43 base64 characters>"
68
69
     *    }
     *
70
71
72
     *
     * Returns the size of the JSON written or std::size_t(-1) on error.
     * If the buffer is too small last_error will be OUTPUT_BUFFER_TOO_SMALL. */
73
    std::size_t get_identity_json(
74
        std::uint8_t * identity_json, std::size_t identity_json_length
75
76
    );

77
78
79
80
81
82
83
84
85
86
87
88
89
    /**
     * The length of an ed25519 signature in bytes.
     */
    std::size_t signature_length();

    /**
     * Signs a message with the ed25519 key for this account.
     */
    std::size_t sign(
        std::uint8_t const * message, std::size_t message_length,
        std::uint8_t * signature, std::size_t signature_length
    );

90
91
92
    /** Number of bytes needed to output the one time keys for this account */
    std::size_t get_one_time_keys_json_length();

93
94
    /** Output the one time keys that haven't been published yet as JSON:
     *
95
96
97
     *  {"curve25519":
     *  ["<6 byte key id>":"<43 base64 characters>"
     *  ,"<6 byte key id>":"<43 base64 characters>"
98
     *  ...
99
     *  ]
100
101
     *  }
     *
102
103
104
105
106
107
108
     * Returns the size of the JSON written or std::size_t(-1) on error.
     * If the buffer is too small last_error will be OUTPUT_BUFFER_TOO_SMALL.
     */
    std::size_t get_one_time_keys_json(
        std::uint8_t * one_time_json, std::size_t one_time_json_length
    );

109
110
111
112
113
114
115
    /** Mark the current list of one_time_keys as being published. They
     * will no longer be returned by get_one_time_keys_json_length(). */
    std::size_t mark_keys_as_published();

    /** The largest number of one time keys this account can store. */
    std::size_t max_number_of_one_time_keys();

116
117
    /** The number of random bytes needed to generate a given number of new one
     * time keys. */
118
119
120
121
122
123
124
125
126
127
128
129
130
    std::size_t generate_one_time_keys_random_length(
        std::size_t number_of_keys
    );

    /** Generates a number of new one time keys. If the total number of keys
     * stored by this account exceeds max_number_of_one_time_keys() then the
     * old keys are discarded. Returns std::size_t(-1) on error. If the number
     * of random bytes is too small then last_error will be NOT_ENOUGH_RANDOM */
    std::size_t generate_one_time_keys(
        std::size_t number_of_keys,
        std::uint8_t const * random, std::size_t random_length
    );

Hubert Chathi's avatar
Hubert Chathi committed
131
    /** The number of random bytes needed to generate a fallback key. */
Hubert Chathi's avatar
Hubert Chathi committed
132
    std::size_t generate_fallback_key_random_length();
Hubert Chathi's avatar
Hubert Chathi committed
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

    /** Generates a new fallback key. Returns std::size_t(-1) on error. If the
     * number of random bytes is too small then last_error will be
     * NOT_ENOUGH_RANDOM */
    std::size_t generate_fallback_key(
        std::uint8_t const * random, std::size_t random_length
    );

    /** Number of bytes needed to output the one time keys for this account */
    std::size_t get_fallback_key_json_length();

    /** Output the fallback key as JSON:
     *
     *  {"curve25519":
     *  ["<6 byte key id>":"<43 base64 characters>"
     *  ,"<6 byte key id>":"<43 base64 characters>"
     *  ...
     *  ]
     *  }
     *
     * Returns the size of the JSON written or std::size_t(-1) on error.
     * If the buffer is too small last_error will be OUTPUT_BUFFER_TOO_SMALL.
     */
    std::size_t get_fallback_key_json(
        std::uint8_t * fallback_json, std::size_t fallback_json_length
    );

160
    /** Lookup a one time key with the given public key */
161
    OneTimeKey const * lookup_key(
162
        _olm_curve25519_public_key const & public_key
163
    );
164

165
    /** Remove a one time key with the given public key */
166
    std::size_t remove_key(
167
        _olm_curve25519_public_key const & public_key
168
    );
169
};
170

171

172
173
174
std::size_t pickle_length(
    Account const & value
);
175

176
177
178
179
180
181
182
183
184
185
186

std::uint8_t * pickle(
    std::uint8_t * pos,
    Account const & value
);


std::uint8_t const * unpickle(
    std::uint8_t const * pos, std::uint8_t const * end,
    Account & value
);
187
188


189
} // namespace olm
190

191
#endif /* OLM_ACCOUNT_HH_ */