account.cpp 3.33 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
#include "olm/account.hh"
#include "olm/pickle.hh"
17
18


19
olm::LocalKey const * olm::Account::lookup_key(
20
21
    std::uint32_t id
) {
22
    for (olm::LocalKey const & key : one_time_keys) {
23
24
25
26
27
        if (key.id == id) return &key;
    }
    return 0;
}

28
std::size_t olm::Account::remove_key(
29
30
31
32
33
34
35
36
37
38
39
    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
std::size_t olm::Account::new_account_random_length() {
Mark Haines's avatar
Mark Haines committed
42
43
44
    return 103 * 32;
}

45
std::size_t olm::Account::new_account(
Mark Haines's avatar
Mark Haines committed
46
47
48
    uint8_t const * random, std::size_t random_length
) {
    if (random_length < new_account_random_length()) {
49
        last_error = olm::ErrorCode::NOT_ENOUGH_RANDOM;
50
        return std::size_t(-1);
Mark Haines's avatar
Mark Haines committed
51
52
53
54
55
    }

    unsigned id = 0;

    identity_key.id = ++id;
56
    olm::generate_key(random, identity_key.key);
Mark Haines's avatar
Mark Haines committed
57
58
59
60
61
    random += 32;

    random += 32;

    last_resort_one_time_key.id = ++id;
62
    olm::generate_key(random, last_resort_one_time_key.key);
Mark Haines's avatar
Mark Haines committed
63
64
    random += 32;

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

    return 0;
}


76
namespace olm {
77
78
79


static std::size_t pickle_length(
80
    olm::LocalKey const & value
81
) {
82
    return olm::pickle_length(value.id) + olm::pickle_length(value.key);
83
84
85
86
87
}


static std::uint8_t * pickle(
    std::uint8_t * pos,
88
    olm::LocalKey const & value
89
) {
90
91
    pos = olm::pickle(pos, value.id);
    pos = olm::pickle(pos, value.key);
92
93
94
95
96
97
    return pos;
}


static std::uint8_t const * unpickle(
    std::uint8_t const * pos, std::uint8_t const * end,
98
    olm::LocalKey & value
99
) {
100
101
    pos = olm::unpickle(pos, end, value.id);
    pos = olm::unpickle(pos, end, value.key);
102
103
104
    return pos;
}

105
} // namespace olm
106
107


108
109
std::size_t olm::pickle_length(
    olm::Account const & value
110
111
) {
    std::size_t length = 0;
112
113
114
    length += olm::pickle_length(value.identity_key);
    length += olm::pickle_length(value.last_resort_one_time_key);
    length += olm::pickle_length(value.one_time_keys);
115
116
117
118
    return length;
}


119
std::uint8_t * olm::pickle(
120
    std::uint8_t * pos,
121
    olm::Account const & value
122
) {
123
124
125
    pos = olm::pickle(pos, value.identity_key);
    pos = olm::pickle(pos, value.last_resort_one_time_key);
    pos = olm::pickle(pos, value.one_time_keys);
126
127
128
129
    return pos;
}


130
std::uint8_t const * olm::unpickle(
131
    std::uint8_t const * pos, std::uint8_t const * end,
132
    olm::Account & value
133
) {
134
135
136
    pos = olm::unpickle(pos, end, value.identity_key);
    pos = olm::unpickle(pos, end, value.last_resort_one_time_key);
    pos = olm::unpickle(pos, end, value.one_time_keys);
137
138
    return pos;
}