account.cpp 4.14 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
17
18
19
20
21
22
23
24
25
26
27
#include "axolotl/account.hh"
#include "axolotl/pickle.hh"


axolotl::LocalKey const * axolotl::Account::lookup_key(
    std::uint32_t id
) {
    for (axolotl::LocalKey const & key : one_time_keys) {
        if (key.id == id) return &key;
    }
    return 0;
}

28
29
30
31
32
33
34
35
36
37
38
39
std::size_t axolotl::Account::remove_key(
    std::uint32_t id
) {
    LocalKey * i;
    for (i = one_time_keys.begin(); i != one_time_keys.end(); ++i) {
        if (i->id == id) {
            one_time_keys.erase(i);
            return id;
        }
    }
    return std::size_t(-1);
}
Mark Haines's avatar
Mark Haines committed
40
41
42
43
44
45
46
47
48
49

std::size_t axolotl::Account::new_account_random_length() {
    return 103 * 32;
}

std::size_t axolotl::Account::new_account(
    uint8_t const * random, std::size_t random_length
) {
    if (random_length < new_account_random_length()) {
        last_error = axolotl::ErrorCode::NOT_ENOUGH_RANDOM;
50
        return std::size_t(-1);
Mark Haines's avatar
Mark Haines committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    }

    unsigned id = 0;

    identity_key.id = ++id;
    axolotl::generate_key(random, identity_key.key);
    random += 32;

    random += 32;

    last_resort_one_time_key.id = ++id;
    axolotl::generate_key(random, last_resort_one_time_key.key);
    random += 32;

65
    for (unsigned i = 0; i < 10; ++i) {
Mark Haines's avatar
Mark Haines committed
66
67
68
69
70
71
72
73
74
75
        LocalKey & key = *one_time_keys.insert(one_time_keys.end());
        key.id = ++id;
        axolotl::generate_key(random, key.key);
        random += 32;
    }

    return 0;
}


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
namespace axolotl {


static std::size_t pickle_length(
    axolotl::LocalKey const & value
) {
    return axolotl::pickle_length(value.id) + axolotl::pickle_length(value.key);
}


static std::uint8_t * pickle(
    std::uint8_t * pos,
    axolotl::LocalKey const & value
) {
    pos = axolotl::pickle(pos, value.id);
    pos = axolotl::pickle(pos, value.key);
    return pos;
}


static std::uint8_t const * unpickle(
    std::uint8_t const * pos, std::uint8_t const * end,
    axolotl::LocalKey & value
) {
    pos = axolotl::unpickle(pos, end, value.id);
    pos = axolotl::unpickle(pos, end, value.key);
    return pos;
}


static std::size_t pickle_length(
    axolotl::SignedKey const & value
) {
    return axolotl::pickle_length((axolotl::LocalKey const &) value) + 64;
}


static std::uint8_t * pickle(
    std::uint8_t * pos,
    axolotl::SignedKey const & value
) {
    pos = axolotl::pickle(pos, (axolotl::LocalKey const &) value);
    pos = axolotl::pickle_bytes(pos, value.signature, 64);
    return pos;
}


static std::uint8_t const * unpickle(
    std::uint8_t const * pos, std::uint8_t const * end,
    axolotl::SignedKey & value
) {
    pos = axolotl::unpickle(pos, end, (axolotl::LocalKey &) value);
    pos = axolotl::unpickle_bytes(pos, end, value.signature, 64);
    return pos;
}


} // namespace axolotl


Mark Haines's avatar
Mark Haines committed
136
std::size_t axolotl::pickle_length(
137
138
139
140
141
142
143
144
145
146
    axolotl::Account const & value
) {
    std::size_t length = 0;
    length += axolotl::pickle_length(value.identity_key);
    length += axolotl::pickle_length(value.last_resort_one_time_key);
    length += axolotl::pickle_length(value.one_time_keys);
    return length;
}


Mark Haines's avatar
Mark Haines committed
147
std::uint8_t * axolotl::pickle(
148
149
150
151
152
153
154
155
156
157
    std::uint8_t * pos,
    axolotl::Account const & value
) {
    pos = axolotl::pickle(pos, value.identity_key);
    pos = axolotl::pickle(pos, value.last_resort_one_time_key);
    pos = axolotl::pickle(pos, value.one_time_keys);
    return pos;
}


Mark Haines's avatar
Mark Haines committed
158
std::uint8_t const * axolotl::unpickle(
159
160
161
162
163
164
165
166
    std::uint8_t const * pos, std::uint8_t const * end,
    axolotl::Account & value
) {
    pos = axolotl::unpickle(pos, end, value.identity_key);
    pos = axolotl::unpickle(pos, end, value.last_resort_one_time_key);
    pos = axolotl::unpickle(pos, end, value.one_time_keys);
    return pos;
}