dbd82d7d93fd4a0c56f9f235ec1ee5a80308dbe7
[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/md5.h>
17 #include <openssl/rand.h>
18 #include <openssl/crypto.h>
19 #include <openssl/hmac.h>
20 #include <openssl/aes.h>
21 #include <openssl/err.h>
22 #include <openssl/sha.h>
23
24 #include <klay/filesystem.h>
25 #include <klay/audit/logger.h>
26
27 #include "key-generator.h"
28
29 #define PBKDF_DEFAULT_ITERATION 1000
30 #define AES_256_CBC_IV "01234567890123456"
31 #define AES_256_KEY_LEN 32
32 #define AES_BLOCK_LEN 16
33
34 namespace ode {
35
36 KeyGenerator::KeyGenerator(int size) :
37         keySize(size)
38 {
39         ::OpenSSL_add_all_algorithms();
40 }
41
42 KeyGenerator::~KeyGenerator()
43 {
44 }
45
46 const KeyGenerator::data KeyGenerator::PBKDF(const KeyGenerator::data& pass, const KeyGenerator::data& salt)
47 {
48         data ret(keySize, 0);
49         std::string strPass(pass.begin(), pass.end());
50         std::string strSalt(salt.begin(), salt.end());
51
52         ::PKCS5_PBKDF2_HMAC((char*)strPass.c_str(), strPass.size(),
53                                                 (unsigned char*)strSalt.c_str(), strSalt.size(), PBKDF_DEFAULT_ITERATION,
54                                                 EVP_sha256(), keySize, reinterpret_cast<unsigned char*>(ret.data()));
55
56         return ret;
57 }
58
59 const KeyGenerator::data KeyGenerator::AESEncrypt(const KeyGenerator::data& key, const KeyGenerator::data& in)
60 {
61         data ret(keySize, 0);
62         std::string strKey(key.begin(), key.end());
63         std::string strIn(in.begin(), in.end());
64         EVP_CIPHER_CTX* ctx;
65         int outLen, len;
66
67         //if ((strKey.size() != AES_256_KEY_LEN) || (strIn.size() % AES_BLOCK_LEN != 0))
68
69         ctx = ::EVP_CIPHER_CTX_new();
70
71         ::EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (unsigned char*)strKey.c_str(), (unsigned char*)AES_256_CBC_IV);
72
73         ::EVP_CIPHER_CTX_set_padding(ctx, 0);
74         ::EVP_EncryptUpdate(ctx, reinterpret_cast<unsigned char*>(ret.data()), &len, (unsigned char*)strIn.c_str(), strIn.size());
75         outLen = len;
76
77         ::EVP_EncryptFinal_ex(ctx, &ret[len], &len);
78         outLen += len;
79
80         ::EVP_CIPHER_CTX_free(ctx);
81
82         return ret;
83 }
84
85 const KeyGenerator::data KeyGenerator::AESDecrypt(const KeyGenerator::data& key, const KeyGenerator::data& in)
86 {
87         data ret(keySize, 0);
88         std::string strKey(key.begin(), key.end());
89         std::string strIn(in.begin(), in.end());
90         EVP_CIPHER_CTX* ctx;
91
92         int len, len1;
93
94         //if ((strKey.size() != AES_256_KEY_LEN) || (strIn.size() % AES_BLOCK_LEN != 0))
95
96         ctx = ::EVP_CIPHER_CTX_new();
97
98         ::EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (unsigned char*)strKey.c_str(), (unsigned char*)AES_256_CBC_IV);
99         ::EVP_CIPHER_CTX_set_padding(ctx, 0);
100         ::EVP_DecryptUpdate(ctx, reinterpret_cast<unsigned char*>(ret.data()), &len, (unsigned char*)strIn.c_str(), keySize);
101
102         len1 = len;
103
104         ::EVP_DecryptFinal_ex(ctx, &ret[len], &len);
105         len1 += len;
106
107         ::EVP_CIPHER_CTX_free(ctx);
108
109         return ret;
110 }
111
112 const KeyGenerator::data KeyGenerator::HMAC(const KeyGenerator::data& original, const KeyGenerator::data& key)
113 {
114         data ret(keySize, 0);
115
116         unsigned int md_len;
117         std::string strOrigin(key.begin(), key.end());
118         std::string strKey(original.begin(), original.end());
119         std::string result;
120
121         ::HMAC(EVP_sha256(), (unsigned char*)strKey.c_str(), strKey.size(),
122                                                 (unsigned char*)strOrigin.c_str(), strOrigin.size(),
123                                                 reinterpret_cast<unsigned char*>(ret.data()), &md_len);
124
125         return ret;
126 }
127
128 const KeyGenerator::data KeyGenerator::RNG()
129 {
130         data ret(keySize);
131
132         ::RAND_bytes(reinterpret_cast<unsigned char*>(ret.data()), keySize);
133
134         return ret;
135 }
136
137 const KeyGenerator::data KeyGenerator::MD5(const KeyGenerator::data& in)
138 {
139         data ret(MD5_DIGEST_LENGTH);
140
141         ::MD5((unsigned char*)in.data(), in.size(), (unsigned char*)ret.data());
142
143         return ret;
144 }
145
146 } // namespace ode