Refactor ecryptfs structures to C++ style
[platform/core/security/krate.git] / volume / key-generator.h
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
17 #ifndef __VOLUME_MANAGER_KEY_GENERATOR_H__
18 #define __VOLUME_MANAGER_KEY_GENERATOR_H__
19
20 #include "ecryptfs.h"
21
22 #define SHA1_DIGEST_SIZE 20
23
24 typedef struct {
25         unsigned int state[5];
26         unsigned int count[2];
27         unsigned char buffer[64];
28 } SHA1_CTX;
29
30 class KeyGenerator final {
31 public:
32         KeyGenerator() = delete;
33         KeyGenerator(const KeyGenerator&) = delete;
34         KeyGenerator(KeyGenerator&&) = delete;
35
36         KeyGenerator& operator=(const KeyGenerator&) = delete;
37         KeyGenerator& operator=(KeyGenerator&&) = delete;
38
39         static std::string wrapKey(const std::string& decrypted, const std::string& salt, int len);
40         static std::string generateKey(int len);
41
42 private:
43         static void sha1Init(SHA1_CTX* context);
44         static void sha1Update(SHA1_CTX* context, const void* p, unsigned int len);
45         static void sha1Final(unsigned char digsest[SHA1_DIGEST_SIZE], SHA1_CTX* context);
46         static void sha1Transform(unsigned int state[5], const unsigned char buffer[64]);
47         static void sha1Hmac(const unsigned char* key, int keyLen, const unsigned char* data, int dataLen, unsigned char out[SHA1_DIGEST_SIZE]);
48
49         static int pbkdf2(const char* pass, int passLen, const unsigned char* salt, int saltLen, int iter, int keyLen, unsigned char* out);
50         static std::string hexConvert(unsigned char* src, int srcLen);
51 };
52
53 #endif //!__VOLUME_MANAGER_KEY_GENERATOR_H__