Fix coverity issues
[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;
80         int outLen, len;
81
82         ctx = ::EVP_CIPHER_CTX_new();
83
84         ::EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key.data(), iv.data());
85
86         ::EVP_CIPHER_CTX_set_padding(ctx, 0);
87         ::EVP_EncryptUpdate(ctx, ret.data(), &len, in.data(), in.size());
88         outLen = len;
89
90         ::EVP_EncryptFinal_ex(ctx, &ret[len], &len);
91         outLen += len;
92
93         ::EVP_CIPHER_CTX_free(ctx);
94
95         return ret;
96 }
97
98 BinaryData AESDecrypt(const BinaryData& in,
99                                           const BinaryData& key,
100                                           const BinaryData& iv)
101 {
102         BinaryData ret(in.size(), 0);
103         EVP_CIPHER_CTX* ctx;
104
105         int len, len1;
106
107         ctx = ::EVP_CIPHER_CTX_new();
108
109         ::EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key.data(), iv.data());
110         ::EVP_CIPHER_CTX_set_padding(ctx, 0);
111         ::EVP_DecryptUpdate(ctx, ret.data(), &len, in.data(), in.size());
112
113         len1 = len;
114
115         ::EVP_DecryptFinal_ex(ctx, &ret[len], &len);
116         len1 += len;
117
118         ::EVP_CIPHER_CTX_free(ctx);
119
120         return ret;
121 }
122
123 BinaryData HMAC(const BinaryData& key, const BinaryData& in)
124 {
125         BinaryData ret(256 / 8);
126         unsigned int md_len;
127
128         ::HMAC(EVP_sha256(), key.data(), key.size(), in.data(), in.size(),
129                                                 ret.data(), &md_len);
130
131         return ret;
132 }
133
134 BinaryData RNG(size_t resultSize)
135 {
136         BinaryData ret(resultSize);
137
138         if(::RAND_bytes(ret.data(), resultSize) != 1)
139                 throw runtime::Exception("RAND_bytes() failed");
140
141         return ret;
142 }
143
144 BinaryData SHA256(const BinaryData& in)
145 {
146         BinaryData ret(256 / 8);
147
148         ::SHA256(in.data(), in.size(), ret.data());
149
150         return ret;
151 }
152
153 BinaryData SHA512(const BinaryData& in)
154 {
155         BinaryData ret(512 / 8);
156
157         ::SHA512(in.data(), in.size(), ret.data());
158
159         return ret;
160 }
161
162 } // namespace KeyManager
163
164 } // namespace ode