blob: 2047f71bfcc934e27f0857ebdb3618ec77656d7e [file] [log] [blame]
/*
* Copyright (c) 2015 Samsung Electronics Co., Ltd.
*
* Sensitive Data Protection
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef DEK_COMMON_H__
#define DEK_COMMON_H__
// ==== kernel configs
#include <linux/limits.h>
#include <linux/ioctl.h>
#include <linux/device.h>
#include <sdp/common.h>
#include "../../fs/crypto/sdp/sdp_crypto.h"
// ==== common configs
#define SDPK_DEFAULT_ALGOTYPE (SDPK_ALGOTYPE_ASYMM_ECDH)
#define SDPK_ALGOTYPE_ASYMM_RSA 0
#define SDPK_ALGOTYPE_ASYMM_DH 1
#define SDPK_ALGOTYPE_ASYMM_ECDH 2
//#define DEK_ENGINE_LOCAL_KEK
#define KEK_RSA_KEY_BITS 2048
#define KEK_MK_BITS 256
#define KEK_SS_BITS 256
#define DH_DEFAULT_GENERATOR DH_GENERATOR_2
#define DH_MAXLEN 256
#define DEK_NAME_LEN 256
#define DEK_LEN 32
#define DEK_MAXLEN 400 // TODO : need to optimize the length of EDEK DEK_RSA_KEY_BITS/8 : 256 bytes , DH2236 : 280 bytes
#define DEK_PW_LEN 32
#define KEK_MAXLEN (KEK_RSA_KEY_BITS/4+4)
#define KEK_MK_LEN (KEK_MK_BITS/8)
#define KEK_SS_LEN (KEK_SS_BITS/8)
#define DEK_AES_HEADER 44
#define FEK_MAXLEN 32
#define EFEK_MAXLEN (FEK_MAXLEN+16)
#define AES_BLOCK_SIZE 16
// DEK types
#define DEK_TYPE_PLAIN 0
#define DEK_TYPE_RSA_ENC 1
#define DEK_TYPE_AES_ENC 2
//#define DEK_TYPE_DH_PUB 4
#define DEK_TYPE_DH_ENC 5
#define DEK_TYPE_ECDH256_ENC 6
#define DEK_TYPE_DUMMY 7
#define DEK_TYPE_SS 8
#define DEK_TYPE_SS_PAYLOAD 9
// KEK types
#define KEK_TYPE_SYM 10
#define KEK_TYPE_RSA_PUB 11
#define KEK_TYPE_RSA_PRIV 12
#define KEK_TYPE_DH_PUB 13
#define KEK_TYPE_DH_PRIV 14
#define KEK_TYPE_ECDH256_PUB 15
#define KEK_TYPE_ECDH256_PRIV 16
#define KEK_TYPE_SS 17
#define SDPK_PATH_MAX 256
#define SDPK_PATH_FMT "/data/system/users/%d/SDPK_%s"
#define SDPK_RPRI_NAME "Rpri"
#define SDPK_RPUB_NAME "Rpub"
#define SDPK_DPRI_NAME "Dpri"
#define SDPK_DPUB_NAME "Dpub"
#define SDPK_EDPRI_NAME "EDpri"
#define SDPK_EDPUB_NAME "EDpub"
#define SDPK_SYM_NAME "sym"
typedef struct _password {
unsigned int len;
unsigned char buf[DEK_MAXLEN];
} password_t;
typedef struct _key {
unsigned int type;
unsigned int len;
unsigned char buf[DEK_MAXLEN];
} dek_t;
typedef struct _kek {
unsigned int type;
unsigned int len;
unsigned char buf[KEK_MAXLEN];
} kek_t;
typedef struct _payload {
unsigned int efek_len;
unsigned int dpub_len;
unsigned char efek_buf[EFEK_MAXLEN];
unsigned char dpub_buf[DH_MAXLEN];
} dh_payload;
typedef struct _ss_key {
unsigned int len;
unsigned char buf[KEK_SS_LEN];
} __attribute__((__packed__)) ssk_t;
typedef struct dh_key {
unsigned int type;
unsigned int len;
unsigned char buf[DH_MAXLEN];
} __attribute__((__packed__)) dhk_t;
typedef struct _ss_payload {
dhk_t dh;
ssk_t ss;
} __attribute__((__packed__)) ss_payload;
/* Debug */
#define DEK_DEBUG 0
#if DEK_DEBUG
#define DEK_LOGD(...) printk("dek: "__VA_ARGS__)
#else
#define DEK_LOGD(...)
#endif /* DEK_DEBUG */
#define DEK_LOGE(...) printk("dek: "__VA_ARGS__)
void hex_key_dump(const char* tag, uint8_t *data, size_t data_len);
void key_dump(unsigned char *buf, int len);
int is_kek_available(int userid, int kek_type);
int dek_create_sysfs_asym_alg(struct device *d);
int dek_create_sysfs_key_dump(struct device *d);
int get_sdp_sysfs_asym_alg(void);
int get_sdp_sysfs_key_dump(void);
int is_root(void);
int is_current_epmd(void);
int is_current_adbd(void);
int is_system_server(void);
#ifdef CONFIG_FSCRYPT_SDP
extern int fscrypt_sdp_cache_init(void);
extern void fscrypt_sdp_cache_drop_inode_mappings(int engine_id);
#endif
#endif