xref: /openbmc/linux/tools/crypto/ccp/dbc.c (revision a325f174)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * AMD Secure Processor Dynamic Boost Control sample library
4  *
5  * Copyright (C) 2023 Advanced Micro Devices, Inc.
6  *
7  * Author: Mario Limonciello <mario.limonciello@amd.com>
8  */
9 
10 #include <assert.h>
11 #include <string.h>
12 #include <sys/ioctl.h>
13 
14 /* if uapi header isn't installed, this might not yet exist */
15 #ifndef __packed
16 #define __packed __attribute__((packed))
17 #endif
18 #include <linux/psp-dbc.h>
19 
20 int get_nonce(int fd, void *nonce_out, void *signature)
21 {
22 	struct dbc_user_nonce tmp = {
23 		.auth_needed = !!signature,
24 	};
25 	int ret;
26 
27 	assert(nonce_out);
28 
29 	if (signature)
30 		memcpy(tmp.signature, signature, sizeof(tmp.signature));
31 
32 	ret = ioctl(fd, DBCIOCNONCE, &tmp);
33 	if (ret)
34 		return ret;
35 	memcpy(nonce_out, tmp.nonce, sizeof(tmp.nonce));
36 
37 	return 0;
38 }
39 
40 int set_uid(int fd, __u8 *uid, __u8 *signature)
41 {
42 	struct dbc_user_setuid tmp;
43 
44 	assert(uid);
45 	assert(signature);
46 
47 	memcpy(tmp.uid, uid, sizeof(tmp.uid));
48 	memcpy(tmp.signature, signature, sizeof(tmp.signature));
49 
50 	return ioctl(fd, DBCIOCUID, &tmp);
51 }
52 
53 int process_param(int fd, int msg_index, __u8 *signature, int *data)
54 {
55 	struct dbc_user_param tmp = {
56 		.msg_index = msg_index,
57 		.param = *data,
58 	};
59 	int ret;
60 
61 	assert(signature);
62 	assert(data);
63 
64 	memcpy(tmp.signature, signature, sizeof(tmp.signature));
65 
66 	ret = ioctl(fd, DBCIOCPARAM, &tmp);
67 	if (ret)
68 		return ret;
69 
70 	*data = tmp.param;
71 	return 0;
72 }
73