GNU Linux-libre 4.9-gnu1
[releases.git] / drivers / staging / greybus / Documentation / firmware / authenticate.c
1 /*
2  * Sample code to test CAP protocol
3  *
4  * This file is provided under a dual BSD/GPLv2 license.  When using or
5  * redistributing this file, you may do so under either license.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * Copyright(c) 2016 Google Inc. All rights reserved.
10  * Copyright(c) 2016 Linaro Ltd. All rights reserved.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of version 2 of the GNU General Public License as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License version 2 for more details.
20  *
21  * BSD LICENSE
22  *
23  * Copyright(c) 2016 Google Inc. All rights reserved.
24  * Copyright(c) 2016 Linaro Ltd. All rights reserved.
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions
28  * are met:
29  *
30  *  * Redistributions of source code must retain the above copyright
31  *    notice, this list of conditions and the following disclaimer.
32  *  * Redistributions in binary form must reproduce the above copyright
33  *    notice, this list of conditions and the following disclaimer in
34  *    the documentation and/or other materials provided with the
35  *    distribution.
36  *  * Neither the name of Google Inc. or Linaro Ltd. nor the names of
37  *    its contributors may be used to endorse or promote products
38  *    derived from this software without specific prior written
39  *    permission.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
42  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
43  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
44  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. OR
45  * LINARO LTD. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
46  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
47  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
48  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
49  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
51  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52  */
53
54 #include <stdio.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <sys/ioctl.h>
58 #include <sys/stat.h>
59 #include <fcntl.h>
60
61 #include "../../greybus_authentication.h"
62
63 struct cap_ioc_get_endpoint_uid uid;
64 struct cap_ioc_get_ims_certificate cert = {
65         .certificate_class = 0,
66         .certificate_id = 0,
67 };
68
69 struct cap_ioc_authenticate authenticate = {
70         .auth_type = 0,
71         .challenge = {0},
72 };
73
74 int main(int argc, char *argv[])
75 {
76         unsigned int timeout = 10000;
77         char *capdev;
78         int fd, ret;
79
80         /* Make sure arguments are correct */
81         if (argc != 2) {
82                 printf("\nUsage: ./firmware <Path of the gb-cap-X dev>\n");
83                 return 0;
84         }
85
86         capdev = argv[1];
87
88         printf("Opening %s authentication device\n", capdev);
89
90         fd = open(capdev, O_RDWR);
91         if (fd < 0) {
92                 printf("Failed to open: %s\n", capdev);
93                 return -1;
94         }
95
96         /* Get UID */
97         printf("Get UID\n");
98
99         ret = ioctl(fd, CAP_IOC_GET_ENDPOINT_UID, &uid);
100         if (ret < 0) {
101                 printf("Failed to get UID: %s (%d)\n", capdev, ret);
102                 ret = -1;
103                 goto close_fd;
104         }
105
106         printf("UID received: 0x%llx\n", *(long long unsigned int *)(uid.uid));
107
108         /* Get certificate */
109         printf("Get IMS certificate\n");
110
111         ret = ioctl(fd, CAP_IOC_GET_IMS_CERTIFICATE, &cert);
112         if (ret < 0) {
113                 printf("Failed to get IMS certificate: %s (%d)\n", capdev, ret);
114                 ret = -1;
115                 goto close_fd;
116         }
117
118         printf("IMS Certificate size: %d\n", cert.cert_size);
119
120         /* Authenticate */
121         printf("Authenticate module\n");
122
123         memcpy(authenticate.uid, uid.uid, 8);
124
125         ret = ioctl(fd, CAP_IOC_AUTHENTICATE, &authenticate);
126         if (ret < 0) {
127                 printf("Failed to authenticate module: %s (%d)\n", capdev, ret);
128                 ret = -1;
129                 goto close_fd;
130         }
131
132         printf("Authenticated, result (%02x), sig-size (%02x)\n",
133                 authenticate.result_code, authenticate.signature_size);
134
135 close_fd:
136         close(fd);
137
138         return ret;
139 }