Change fallocate() to posix_fallocate()
[platform/core/security/ode.git] / server / key-manager / key-generator.cpp
1 /*
2  *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16 #include <openssl/rand.h>
17 #include <openssl/crypto.h>
18 #include <openssl/hmac.h>
19 #include <openssl/aes.h>
20 #include <openssl/err.h>
21 #include <openssl/sha.h>
22
23 #include <klay/filesystem.h>
24 #include <klay/exception.h>
25
26 #include "../logger.h"
27 #include "key-generator.h"
28
29 #define PBKDF_DEFAULT_ITERATION 1000
30
31 #ifdef OPENSSL_NO_AES
32 #error This requires AES
33 #endif
34
35 #ifdef OPENSSL_NO_SHA256
36 #error This requires SHA256
37 #endif
38
39 #ifdef OPENSSL_NO_SHA512
40 #error This requires SHA512
41 #endif
42
43 namespace ode {
44
45 namespace KeyGenerator {
46
47 void init()
48 {
49         EVP_add_cipher(EVP_aes_256_cbc());
50         EVP_add_digest(EVP_sha256());
51         EVP_add_digest(EVP_sha512());
52 }
53
54 void cleanup()
55 {
56         EVP_cleanup();
57 }
58
59
60 BinaryData PBKDF(const BinaryData& pass,
61                                  const BinaryData& salt,
62                                  size_t iteration,
63                                  size_t resultSize)
64 {
65         BinaryData ret(resultSize, 0);
66
67         ::PKCS5_PBKDF2_HMAC((char *)pass.data(), pass.size(),
68                                                 salt.data(), salt.size(), iteration,
69                                                 EVP_sha256(), resultSize, ret.data());
70
71         return ret;
72 }
73
74 BinaryData AESEncrypt(const BinaryData& in,
75                                           const BinaryData& key,
76                                           const BinaryData& iv)
77 {
78         BinaryData ret(in.size(), 0);
79         EVP_CIPHER_CTX* ctx = nullptr;
80         int outLen, len;
81
82         ctx = ::EVP_CIPHER_CTX_new();
83         if (ctx == nullptr)
84                 throw runtime::Exception("Failed to allocate memory for chipher context");
85
86         ::EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key.data(), iv.data());
87
88         ::EVP_CIPHER_CTX_set_padding(ctx, 0);
89         ::EVP_EncryptUpdate(ctx, ret.data(), &len, in.data(), in.size());
90         outLen = len;
91
92         ::EVP_EncryptFinal_ex(ctx, &ret[len], &len);
93         outLen += len;
94
95         ::EVP_CIPHER_CTX_free(ctx);
96
97         return ret;
98 }
99
100 BinaryData AESDecrypt(const BinaryData& in,
101                                           const BinaryData& key,
102                                           const BinaryData& iv)
103 {
104         BinaryData ret(in.size(), 0);
105         EVP_CIPHER_CTX* ctx = nullptr;
106
107         int len, len1;
108
109         ctx = ::EVP_CIPHER_CTX_new();
110         if (ctx == nullptr)
111                 throw runtime::Exception("Failed to allocate memory for chipher context");
112
113         ::EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key.data(), iv.data());
114         ::EVP_CIPHER_CTX_set_padding(ctx, 0);
115         ::EVP_DecryptUpdate(ctx, ret.data(), &len, in.data(), in.size());
116
117         len1 = len;
118
119         ::EVP_DecryptFinal_ex(ctx, &ret[len], &len);
120         len1 += len;
121
122         ::EVP_CIPHER_CTX_free(ctx);
123
124         return ret;
125 }
126
127 BinaryData HMAC(const BinaryData& key, const BinaryData& in)
128 {
129         BinaryData ret(256 / 8);
130         unsigned int md_len;
131
132         ::HMAC(EVP_sha256(), key.data(), key.size(), in.data(), in.size(),
133                                                 ret.data(), &md_len);
134
135         return ret;
136 }
137
138 BinaryData RNG(size_t resultSize)
139 {
140         BinaryData ret(resultSize);
141
142         if(::RAND_bytes(ret.data(), resultSize) != 1)
143                 throw runtime::Exception("RAND_bytes() failed");
144
145         return ret;
146 }
147
148 BinaryData SHA256(const BinaryData& in)
149 {
150         BinaryData ret(256 / 8);
151
152         ::SHA256(in.data(), in.size(), ret.data());
153
154         return ret;
155 }
156
157 BinaryData SHA512(const BinaryData& in)
158 {
159         BinaryData ret(512 / 8);
160
161         ::SHA512(in.data(), in.size(), ret.data());
162
163         return ret;
164 }
165
166 } // namespace KeyManager
167
168 } // namespace ode