summaryrefslogtreecommitdiff
path: root/include/gatekeeper/soft_gatekeeper.h
blob: 2927cd7360e3bd9f9450c744dd0772a7630a8070 (plain)
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
/*
 * Copyright 2015 The Android Open Source Project
 *
 * 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.
 *
 */

#ifndef SOFT_GATEKEEPER_H_
#define SOFT_GATEKEEPER_H_

extern "C" {
#include <openssl/rand.h>
#include <crypto_scrypt.h>
}

#include <UniquePtr.h>
#include <gatekeeper/gatekeeper.h>
#include <iostream>

namespace gatekeeper {

/**
 * Convenience class for easily switching out a testing implementation
 */
class GateKeeperFileIo {
public:
    virtual ~GateKeeperFileIo() {}
    virtual void Write(const char *filename, const uint8_t *bytes, uint32_t length) = 0;
    virtual uint32_t Read(const char *filename, UniquePtr<uint8_t> *bytes) const = 0;
};

class SoftGateKeeper : public GateKeeper {
public:
    static const uint32_t SIGNATURE_LENGTH_BYTES = 32;

    // scrypt params
    static const uint64_t N = 16384;
    static const uint32_t r = 8;
    static const uint32_t p = 1;

    static const int MAX_UINT_32_CHARS = 11;

    SoftGateKeeper(GateKeeperFileIo *file_io) {
        file_io_ = file_io;
        key_.reset(new uint8_t[SIGNATURE_LENGTH_BYTES]);
        memset(key_.get(), 0, SIGNATURE_LENGTH_BYTES);
    }

    virtual ~SoftGateKeeper() {
        delete file_io_;
    }

    virtual void GetAuthTokenKey(const uint8_t **auth_token_key,
            uint32_t *length) const {
        if (auth_token_key == NULL || length == NULL) return;
        *auth_token_key = const_cast<const uint8_t *>(key_.get());
        *length = SIGNATURE_LENGTH_BYTES;
    }

    virtual void GetPasswordKey(const uint8_t **password_key, uint32_t *length) {
        if (password_key == NULL || length == NULL) return;
        *password_key = const_cast<const uint8_t *>(key_.get());
        *length = SIGNATURE_LENGTH_BYTES;
    }

    virtual void ComputePasswordSignature(uint8_t *signature, uint32_t signature_length,
            const uint8_t *, uint32_t, const uint8_t *password,
            uint32_t password_length, salt_t salt) const {
        if (signature == NULL) return;
        crypto_scrypt(password, password_length, reinterpret_cast<uint8_t *>(&salt),
                sizeof(salt), N, r, p, signature, signature_length);
    }

    virtual void GetRandom(void *random, uint32_t requested_length) const {
        if (random == NULL) return;
        RAND_pseudo_bytes((uint8_t *) random, requested_length);
    }

    virtual void ComputeSignature(uint8_t *signature, uint32_t signature_length,
            const uint8_t *, uint32_t, const uint8_t *, const uint32_t) const {
        if (signature == NULL) return;
        memset(signature, 0, signature_length);
    }

    virtual void ReadPasswordFile(uint32_t uid, SizedBuffer *password_file) const {
        char buf[MAX_UINT_32_CHARS];
        sprintf(buf, "%u", uid);
        UniquePtr<uint8_t> password_buffer;
        uint32_t length = file_io_->Read(buf, &password_buffer);
        password_file->buffer.reset(password_buffer.release());
        password_file->length = length;
    }

    virtual void WritePasswordFile(uint32_t uid, const SizedBuffer &password_file) const {
        char buf[MAX_UINT_32_CHARS];
        sprintf(buf, "%u", uid);
        file_io_->Write(buf, password_file.buffer.get(), password_file.length);
    }

    virtual uint64_t GetNanosecondsSinceBoot() const {
        struct timespec time;
        int res = clock_gettime(CLOCK_MONOTONIC_RAW, &time);
        if (res < 0) return 0;
        return time.tv_nsec;
    }
private:
    GateKeeperFileIo *file_io_;
    UniquePtr<uint8_t> key_;
};
}

#endif // SOFT_GATEKEEPER_H_