blob: 0e928db494fdb99e0bd03aff0271613bc186a508 [file] [log] [blame]
Chung-yih Wang70246eb2009-06-29 03:12:56 +08001/*
2** Copyright 2009, 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 __KEYMGMT_H__
18#define __KEYMGMT_H__
19
20#define MASTER_KEY_TAG "master_key"
21#define MASTER_KEY ".keymaster"
22#define MAX_PATH_LEN 128
23#define SALT "Android Keystore 0.1"
24#define NAME_DELIMITER "_"
25#define KEYFILE_NAME "%s"NAME_DELIMITER"%s"
26#define KEYGEN_ITER 1024
27#define AES_KEY_LEN 128
28#define USER_KEY_LEN (AES_KEY_LEN/8)
29#define IV_LEN USER_KEY_LEN
Chung-yih Wanga7342072009-07-03 12:09:52 +080030#define MAX_RETRY_COUNT 6
31#define MIN_PASSWD_LENGTH 8
Chung-yih Wang70246eb2009-06-29 03:12:56 +080032
33#define gen_key(passwd, key, len) \
34 PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), \
35 (unsigned char*)SALT, \
36 strlen(SALT), KEYGEN_ITER, \
37 len, key)
38
39#define KEYFILE_LEN MAX_NAMESPACE_LENGTH + MAX_KEY_NAME_LENGTH + 6
40
41#define get_blob_size(blob) \
42 (((blob->value_size + sizeof(uint32_t) + MAX_KEY_NAME_LENGTH \
43 + USER_KEY_LEN - 1) / USER_KEY_LEN) * USER_KEY_LEN)
44
45#define MAX_BLOB_LEN ((MAX_KEY_VALUE_LENGTH + MAX_KEY_NAME_LENGTH + \
46 sizeof(uint32_t) + USER_KEY_LEN - 1) / USER_KEY_LEN)\
47 * USER_KEY_LEN
48
49#define data_blob_size(blob) USER_KEY_LEN + sizeof(uint32_t) + blob->blob_size
50
51typedef struct {
52 unsigned char iv[USER_KEY_LEN];
53 uint32_t blob_size;
54 union {
55 unsigned char blob[1];
56 struct {
57 uint32_t value_size;
58 char keyname[MAX_KEY_NAME_LENGTH];
59 unsigned char value[MAX_KEY_VALUE_LENGTH];
60 } __attribute__((packed));
61 };
62} DATA_BLOB;
63
64typedef struct {
65 char tag[USER_KEY_LEN];
66 unsigned char master_key[USER_KEY_LEN];
67} MASTER_BLOB;
68
69int put_key(const char *namespace, const char *keyname,
70 unsigned char *data, int size);
71int get_key(const char *namespace, const char *keyname,
72 unsigned char *data, int *size);
73int remove_key(const char *namespace, const char *keyname);
74int list_keys(const char *namespace, char reply[BUFFER_MAX]);
75int passwd(char *data);
76int lock();
77int unlock(char *passwd);
78KEYSTORE_STATE get_state();
79int reset_keystore();
80int init_keystore(const char *dir);
81
82#endif