Refactor ecryptfs structures to C++ style
[platform/core/security/krate.git] / volume / ecryptfs.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 __ECRYPTFS_H__
18 #define __ECRYPTFS_H__
19
20 #include <sys/types.h>
21 #include <errno.h>
22
23 // this is versions of ecryptfs module
24 #define ECRYPTFS_MAJOR_VERSION 0x00
25 #define ECRYPTFS_MINOR_VERSION 0x04
26 #define ECRYPTFS_VERSION ((ECRYPTFS_MAJOR_VERSION << 8) | ECRYPTFS_MINOR_VERSION)
27
28 #define ECRYPTFS_SALT_SIZE 8
29 #define ECRYPTFS_SIGNATURE_SIZE 16
30 #define ECRYPTFS_MAX_KEY_SIZE 64
31 #define ECRYPTFS_MAX_KEY_MOD_NAME_SIZE 16
32 #define ECRYPTFS_MAX_ENCRYPTED_KEY_SIZE 512
33
34 struct EcryptfsPassword {
35         enum Flag {
36                 PersistentPassword                      = 0x01,
37                 SessionKeyEncryptionKeySet      = 0x02
38         };
39
40         int32_t passwordSize;
41         int32_t hashAlgorithm;
42         int32_t hashIterations;
43         int32_t sessionKeyEncryptionKeySize;
44         uint32_t flags;
45         uint8_t sessionKeyEncryptionKey[ECRYPTFS_MAX_KEY_SIZE];
46         uint8_t signature[ECRYPTFS_SIGNATURE_SIZE + 1];
47         uint8_t salt[ECRYPTFS_SALT_SIZE];
48 };
49
50 struct EcryptfsPrivateKey {
51         uint32_t keySize;
52         uint32_t dataSize;
53         uint8_t signature[ECRYPTFS_SIGNATURE_SIZE + 1];
54         char keyModAlias[ECRYPTFS_MAX_KEY_MOD_NAME_SIZE + 1];
55         uint8_t data[];
56 };
57
58 struct EcryptfsSessionKey {
59         enum Flag {
60                 UserspaceShouldTryToDecrypt =   0x00000001,
61                 UserspaceShouldTryToEncrypt =   0x00000002,
62                 ContainsDecryptedKey =                  0x00000004,
63                 ContainsEncryptedKey =                  0x00000008
64         };
65
66         int32_t flags;
67         int32_t encryptedKeySize;
68         int32_t decryptedKeySize;
69         uint8_t encryptedKey[ECRYPTFS_MAX_ENCRYPTED_KEY_SIZE];
70         uint8_t decryptedKey[ECRYPTFS_MAX_KEY_SIZE];
71
72         EcryptfsSessionKey()
73                 : flags(0), encryptedKeySize(0), decryptedKeySize(0),
74                         encryptedKey{0, }, decryptedKey{0, }
75         {};
76 } __attribute__((packed));
77
78 struct EcryptfsPayload {
79         enum Type {
80                 PasswordToken,
81                 PrivateKeyToken
82         };
83         enum Flag {
84                 EncryptOnly
85         };
86
87         uint16_t version;
88         uint16_t type;
89         uint32_t flags;
90         EcryptfsSessionKey sessionKey;
91         uint8_t reserved[32];
92         union {
93                 EcryptfsPassword password;
94                 EcryptfsPrivateKey privateKey;
95         } token;
96
97         EcryptfsPayload(Type type)
98                 : version(ECRYPTFS_VERSION), type(type), flags(0), reserved{0, }
99         {
100                 ::memset(&token, 0, sizeof(token));
101         };
102 } __attribute__((packed));
103
104 #endif