summaryrefstree
path: root/kernel/security/sdp/dek_sysfs.c
blob: 7ae69d24ca53af8797a11d5afafe321d10199775 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * 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.
 */

#include <linux/kernel.h>
#include <linux/stat.h>
#include <linux/string.h>

#include <sdp/dek_common.h>

static int g_asym_alg = SDPK_DEFAULT_ALGOTYPE;

#define ALG_NAME_RSA_NAME "RSA"
#define ALG_NAME_DH_NAME "DH"
#define ALG_NAME_ECDH_NAME "ECDH"

static ssize_t dek_show_asym_alg(struct device *dev,
		struct device_attribute *attr, char *buf) {

	switch (g_asym_alg) {
	case SDPK_ALGOTYPE_ASYMM_RSA:
		return sprintf(buf, "%s\n", ALG_NAME_RSA_NAME);
	case SDPK_ALGOTYPE_ASYMM_DH:
		return sprintf(buf, "%s\n", ALG_NAME_DH_NAME);
	case SDPK_ALGOTYPE_ASYMM_ECDH:
		return sprintf(buf, "%s\n", ALG_NAME_ECDH_NAME);
	default:
		return sprintf(buf, "unknown\n");
	}

	return 0;
}

static ssize_t dek_set_asym_alg(struct device *dev,
			     struct device_attribute *attr,
			     const char *buf, size_t count) {
	if (!strncmp(ALG_NAME_RSA_NAME, buf, strlen(ALG_NAME_RSA_NAME))) {
		g_asym_alg = SDPK_ALGOTYPE_ASYMM_RSA;
		return strlen(ALG_NAME_RSA_NAME);
	}

	if (!strncmp(ALG_NAME_DH_NAME, buf, strlen(ALG_NAME_DH_NAME))) {
		g_asym_alg = SDPK_ALGOTYPE_ASYMM_DH;
		return strlen(ALG_NAME_DH_NAME);
	}

	if (!strncmp(ALG_NAME_ECDH_NAME, buf, strlen(ALG_NAME_ECDH_NAME))) {
		g_asym_alg = SDPK_ALGOTYPE_ASYMM_ECDH;
		return strlen(ALG_NAME_ECDH_NAME);
	}

	return -1;
}

static DEVICE_ATTR(asym_alg, S_IRUSR | S_IWUSR, dek_show_asym_alg, dek_set_asym_alg);

int dek_create_sysfs_asym_alg(struct device *d)
{
	int error;

	error = device_create_file(d, &dev_attr_asym_alg);
	if (error)
		return error;

	return 0;
}

int get_sdp_sysfs_asym_alg(void)
{
	return g_asym_alg;
}

#ifdef CONFIG_SDP_KEY_DUMP
static int kek_dump;

static ssize_t dek_show_key_dump(struct device *dev,
	struct device_attribute *attr, char *buf) {
	return sprintf(buf, "%d\n", kek_dump);
}

static ssize_t dek_set_key_dump(struct device *dev,
		 struct device_attribute *attr,
		 const char *buf, size_t count) {
	int flag = simple_strtoul(buf, NULL, 10);

	kek_dump = flag;

	return strlen(buf);
}

static DEVICE_ATTR(key_dump, 0664, dek_show_key_dump, dek_set_key_dump);

int dek_create_sysfs_key_dump(struct device *d)
{
	int error;

	error = device_create_file(d, &dev_attr_key_dump);
	if (error)
		return error;

	return 0;
}

int get_sdp_sysfs_key_dump(void)
{
	return kek_dump;
}
#else
int dek_create_sysfs_key_dump(struct device *d)
{
	printk("key_dump feature not available");

	return 0;
}

int get_sdp_sysfs_key_dump(void)
{
	printk("key_dump feature not available");

	return 0;
}
#endif