blob: 2a52f810d1f7d5c91e416ee850b0777163157e3c [file] [log] [blame]
Shawn Willden6507c272016-01-05 22:51:48 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
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 KEYSTORE_USER_STATE_H_
18#define KEYSTORE_USER_STATE_H_
19
20#include <sys/types.h>
21
22#include <openssl/aes.h>
23
24#include <utils/String8.h>
25
26#include <keystore/keystore.h>
27
28#include "entropy.h"
29
30class UserState {
31 public:
32 UserState(uid_t userId);
33 ~UserState();
34
35 bool initialize();
36
37 uid_t getUserId() const { return mUserId; }
38 const char* getUserDirName() const { return mUserDir; }
39
40 const char* getMasterKeyFileName() const { return mMasterKeyFile; }
41
42 void setState(State state);
43 State getState() const { return mState; }
44
45 int8_t getRetry() const { return mRetry; }
46
47 void zeroizeMasterKeysInMemory();
48 bool deleteMasterKey();
49
50 ResponseCode initialize(const android::String8& pw, Entropy* entropy);
51
52 ResponseCode copyMasterKey(UserState* src);
53 ResponseCode copyMasterKeyFile(UserState* src);
54 ResponseCode writeMasterKey(const android::String8& pw, Entropy* entropy);
55 ResponseCode readMasterKey(const android::String8& pw, Entropy* entropy);
56
57 AES_KEY* getEncryptionKey() { return &mMasterKeyEncryption; }
58 AES_KEY* getDecryptionKey() { return &mMasterKeyDecryption; }
59
60 bool reset();
61
62 private:
63 static const int MASTER_KEY_SIZE_BYTES = 16;
64 static const int MASTER_KEY_SIZE_BITS = MASTER_KEY_SIZE_BYTES * 8;
65
66 static const int MAX_RETRY = 4;
67 static const size_t SALT_SIZE = 16;
68
69 void generateKeyFromPassword(uint8_t* key, ssize_t keySize, const android::String8& pw,
70 uint8_t* salt);
71 bool generateSalt(Entropy* entropy);
72 bool generateMasterKey(Entropy* entropy);
73 void setupMasterKeys();
74
75 uid_t mUserId;
76
77 char* mUserDir;
78 char* mMasterKeyFile;
79
80 State mState;
81 int8_t mRetry;
82
83 uint8_t mMasterKey[MASTER_KEY_SIZE_BYTES];
84 uint8_t mSalt[SALT_SIZE];
85
86 AES_KEY mMasterKeyEncryption;
87 AES_KEY mMasterKeyDecryption;
88};
89
90#endif // KEYSTORE_USER_STATE_H_