summaryrefslogtreecommitdiff
path: root/src/include/ckm/ckm-type.h
blob: 7b91d99eb872bb51ff3974d43d6b1bc0ff2084af (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
124
125
126
127
128
129
130
131
132
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 *  Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  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
 *
 *
 * @file        ckm-type.h
 * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
 * @version     1.0
 * @brief       Sample service implementation.
 */
#pragma once

#include <stdint.h>
#include <cassert>

#include <string>
#include <vector>
#include <map>
#include <memory>

#include <ckm/ckm-raw-buffer.h>
#include <ckm/ckm-password.h>

#define KEY_MANAGER_API __attribute__((visibility("default")))

namespace CKM {

// used to pass password and raw key data
typedef std::vector<RawBuffer> RawBufferVector;
typedef std::string Alias;
typedef std::string Label;
typedef std::vector<Alias> AliasVector;

enum class KeyType : int {
    KEY_NONE = 0,
    KEY_RSA_PUBLIC,
    KEY_RSA_PRIVATE,
    KEY_ECDSA_PUBLIC,
    KEY_ECDSA_PRIVATE,
    KEY_DSA_PUBLIC,
    KEY_DSA_PRIVATE,
    KEY_AES
};

enum class DataFormat : int {
    FORM_DER_BASE64 = 0,
    FORM_DER,
    FORM_PEM
};

enum class ElipticCurve : int {
    prime192v1 = 0,
    prime256v1,
    secp384r1
};

enum class CertificateFieldId : int {
    ISSUER = 0,
    SUBJECT
};

struct Policy {
    Policy(const Password &pass = Password(), bool extract = true)
      : password(pass)
      , extractable(extract)
    {}
    virtual ~Policy() {}
    Password password;  // byte array used to encrypt data inside CKM
    bool extractable;   // if true key may be extracted from storage
};

enum class HashAlgorithm : int {
    NONE = 0,
    SHA1,
    SHA256,
    SHA384,
    SHA512
};

enum class RSAPaddingAlgorithm : int {
    NONE = 0,
    PKCS1,
    X931
};

enum class DBCMAlgType : int {
    NONE = 0,
    AES_GCM_256,
    COUNT
};

typedef int PermissionMask;
enum Permission: int {
    NONE            = 0x00,
    READ            = 0x01,
    REMOVE          = 0x02
    // keep in sync with ckmc_permission_e !
};

// algorithm parameters
enum class ParamName : int {
    ALGO_TYPE = 1,      // If there's no such param, the service will try to deduce the algorithm
                        // type from the key.

    // encryption & decryption
    ED_IV = 101,
    ED_CTR_LEN,
    ED_AAD,
    ED_TAG_LEN,
    ED_LABEL,

    // key generation
    GEN_KEY_LEN = 201,
    GEN_EC,             // elliptic curve (ElipticCurve)

    // sign & verify
    SV_HASH_ALGO = 301, // hash algorithm (HashAlgorithm)
    SV_RSA_PADDING,     // RSA padding (RSAPaddingAlgorithm)

    // special values marking valid values range
    FIRST = ALGO_TYPE,
    LAST = SV_RSA_PADDING
};

// algorithm types (ALGO_TYPE param)
enum class AlgoType : int {
    AES_CTR = 1,
    AES_CBC,
    AES_GCM,
    AES_CFB,
    RSA_OAEP,
    RSA_SV,
    DSA_SV,
    ECDSA_SV,
    RSA_GEN,
    DSA_GEN,
    ECDSA_GEN,
    AES_GEN,
};

// cryptographic algorithm description
class KEY_MANAGER_API CryptoAlgorithm {
public:
    template <typename T>
    bool getParam(ParamName name, T& value) const;

    // returns false if param 'name' is invalid
    template <typename T>
    bool setParam(ParamName name, const T& value);

protected:
    class BaseParam {
    public:
        virtual bool getBuffer(RawBuffer&) const { return false; }
        virtual bool getInt(uint64_t&) const { return false; }
        virtual ~BaseParam() {}

    protected:
        BaseParam() {}
    };
    typedef std::shared_ptr<BaseParam> BaseParamPtr;

    class BufferParam : public BaseParam {
    public:
        bool getBuffer(RawBuffer& buffer) const;
        static BaseParamPtr create(const RawBuffer& buffer);
    private:
        explicit BufferParam(const RawBuffer& value) : m_buffer(value) {}

        RawBuffer m_buffer;
    };

    class IntParam : public BaseParam {
    public:
        static BaseParamPtr create(uint64_t value);
        bool getInt(uint64_t& value) const;
    private:
        explicit IntParam(uint64_t value) : m_int(value) {}

        uint64_t m_int;
    };

    std::map<ParamName, BaseParamPtr> m_params;
};

template <typename T>
bool CryptoAlgorithm::getParam(ParamName name, T& value) const
{
    auto param = m_params.find(name);
    if (param == m_params.end())
        return false;

    assert(param->second);

    uint64_t valueTmp;
    if (param->second->getInt(valueTmp)) {
        value = static_cast<T>(valueTmp);
        return true;
    }
    return false;
}

template <>
bool CryptoAlgorithm::getParam(ParamName name, RawBuffer& value) const;

template <typename T>
bool CryptoAlgorithm::setParam(ParamName name, const T& value)
{
    if (name < ParamName::FIRST || name > ParamName::LAST)
        return false;
    m_params[name] = IntParam::create(static_cast<uint64_t>(value));
    return true;
}

template <>
bool CryptoAlgorithm::setParam(ParamName name, const RawBuffer& value);

} // namespace CKM