blob: 3e1751a15d12497584dc84983d88d90a8424c284 [file] [log] [blame]
developer67cb0a82023-12-25 23:36:27 +08001--- a/Makefile.am
2+++ b/Makefile.am
developerd042eba2023-11-14 14:23:39 +08003@@ -229,6 +229,16 @@ EXTRA_bin_kcapi_dgst_DEPENDENCIES = libt
4
5 SCAN_FILES += $(bin_kcapi_dgst_SOURCES)
6 man_MANS += apps/kcapi-dgst.1
7+
8+bin_PROGRAMS += bin/kcapi-mtk-dgst
9+
10+bin_kcapi_mtk_dgst_CPPFLAGS = $(COMMON_CPPFLAGS) -I$(top_srcdir)/lib/
11+bin_kcapi_mtk_dgst_LDFLAGS = $(COMMON_LDFLAGS)
12+bin_kcapi_mtk_dgst_LDADD = libkcapi.la
13+bin_kcapi_mtk_dgst_SOURCES = apps/kcapi-mtk-dgst.c apps/app-internal.c
14+EXTRA_bin_kcapi_dgst_DEPENDENCIES = libtool
15+
16+SCAN_FILES += $(bin_kcapi_mtk_dgst_SOURCES)
17 endif
18
19 if HAVE_CLANG
20--- /dev/null
developer67cb0a82023-12-25 23:36:27 +080021+++ b/apps/kcapi-mtk-dgst.c
developerd042eba2023-11-14 14:23:39 +080022@@ -0,0 +1,287 @@
23+// SPDX-License-Identifier: BSD-2-Clause
24+/*
25+ * Copyright (C) 2023, MediaTek Inc. All rights reserved.
26+ */
27+
28+#define _GNU_SOURCE
29+#include <unistd.h>
30+#include <sys/syscall.h>
31+#include <errno.h>
32+#include <limits.h>
33+#include <stdint.h>
34+#include <sys/types.h>
35+#include <sys/stat.h>
36+#include <fcntl.h>
37+#include <stdio.h>
38+#include <stdlib.h>
39+#include <string.h>
40+#include <getopt.h>
41+
42+#include <kcapi.h>
43+
44+#include "app-internal.h"
45+
46+#define TYPE_SHA 0
47+#define TYPE_HMAC 1
48+
49+static char *name = NULL;
50+static unsigned char* key = NULL;
51+static unsigned char* message = NULL;
52+static uint32_t key_len = 0;
53+static uint32_t message_len = 0;
54+int type = -1, type_index = -1;
55+int empty = 0;
56+struct sha {
57+ char *name;
58+ ssize_t (*fun)(const uint8_t*, size_t,
59+ uint8_t*, size_t);
60+};
61+
62+struct hmac {
63+ char *name;
64+ ssize_t (*fun)(const uint8_t*, uint32_t,
65+ const uint8_t*, size_t,
66+ uint8_t*, size_t);
67+};
68+
69+struct sha msg_digest[5] = {
70+ {
71+ .name = "sha1",
72+ .fun = kcapi_md_sha1,
73+ },
74+ {
75+ .name = "sha224",
76+ .fun = kcapi_md_sha224,
77+ },
78+ {
79+ .name = "sha256",
80+ .fun = kcapi_md_sha256,
81+ },
82+ {
83+ .name = "sha384",
84+ .fun = kcapi_md_sha384,
85+ },
86+ {
87+ .name = "sha512",
88+ .fun = kcapi_md_sha512,
89+ },
90+};
91+
92+struct hmac key_msg_digest[5] = {
93+ {
94+ .name = "hmac-sha1",
95+ .fun = kcapi_md_hmac_sha1,
96+ },
97+ {
98+ .name = "hmac-sha224",
99+ .fun = kcapi_md_hmac_sha224,
100+ },
101+ {
102+ .name = "hmac-sha256",
103+ .fun = kcapi_md_hmac_sha256,
104+ },
105+ {
106+ .name = "hmac-sha384",
107+ .fun = kcapi_md_hmac_sha384,
108+ },
109+ {
110+ .name = "hmac-sha512",
111+ .fun = kcapi_md_hmac_sha512,
112+ },
113+};
114+
115+static unsigned char* hextobin(char* hexstr, uint32_t *ret_len) {
116+ size_t len = strlen(hexstr);
117+ size_t i = 0;
118+ char *pos = hexstr;
119+ unsigned char *ret = (unsigned char*) malloc((len + 1) / 2);
120+
121+ for (i = 0; i < (len + 1) / 2; i++) {
122+ if(i == 0 && (len % 2) == 1) {
123+ sscanf(pos, "%1hhx", &ret[i]);
124+ pos += 1;
125+ } else {
126+ sscanf(pos, "%2hhx", &ret[i]);
127+ pos += 2;
128+ }
129+ }
130+ *ret_len = (len + 1) / 2;
131+
132+ return ret;
133+}
134+
135+static void uninit() {
136+ memset(key, 0, key_len);
137+ memset(message, 0, message_len);
138+ if(key)
139+ free(key);
140+ if(message)
141+ free(message);
142+}
143+
144+static void usage(void)
145+{
146+ fprintf(stderr, "\nKernel Crypto API SHA & HMAC\n");
147+ fprintf(stderr, "Usage:\n");
148+ fprintf(stderr, "\t-k \tKey (hmac required option)\n");
149+ fprintf(stderr, "\t-n \tDigest name such as sha1, sha224, sha256 sha512\n");
150+ fprintf(stderr, "\t \thmac-sha1 hmac-sha224 hmac-sha256 hmac-sha384 hmac-sha512\n");
151+ fprintf(stderr, "\t-m \tmessage(require option))\n");
152+ fprintf(stderr, "\t-e \tempty message \n");
153+ fprintf(stderr, "\t-l \toutput len(require option))\n");
154+ fprintf(stderr, "\t\t\t\tnotation\n");
155+ fprintf(stderr, "\t-h --help\t\tThis help information\n");
156+
157+ uninit();
158+ exit(1);
159+}
160+
161+static int check_type(char* name) {
162+ int i = 0;
163+
164+ for(i = 0; i < 5; i++) {
165+ if(!strncmp(name, msg_digest[i].name, strlen(name))) {
166+ type_index = i;
167+ return TYPE_SHA;
168+ }
169+ }
170+
171+ for(i = 0; i < 5; i++) {
172+ if(!strncmp(name, key_msg_digest[i].name, strlen(name))) {
173+ type_index = i;
174+ return TYPE_HMAC;
175+ }
176+ }
177+
178+ return TYPE_HMAC + 1;
179+}
180+
181+static int parse_opts(int argc, char *argv[], size_t *outlen)
182+{
183+ int c = 0;
184+ size_t len = 0;
185+
186+ while (1) {
187+ int opt_index = 0;
188+ static struct option opts[] = {
189+ {"key", required_argument, 0, 'k'},
190+ {"name", required_argument, 0, 'n'},
191+ {"message", required_argument, 0, 'm'},
192+ {"len", required_argument, 0, 'l'},
193+ {"help", required_argument, 0, 'h'},
194+ {"empty", required_argument, 0, 'e'},
195+ {0, 0, 0, 0}
196+ };
197+ c = getopt_long(argc, argv, "k:n:m:l:he", opts, &opt_index);
198+ if (-1 == c)
199+ break;
200+ switch (c) {
201+ case 0:
202+ switch (opt_index) {
203+ case 0:
204+ key = hextobin(optarg, &key_len);
205+ break;
206+ case 1:
207+ name = optarg;
208+ type = check_type(name);
209+ if(type > TYPE_HMAC){
210+ usage();
211+ return -EINVAL;
212+ }
213+
214+ break;
215+ case 2:
216+ message = hextobin(optarg, &message_len);
217+ break;
218+ case 3:
219+ len = strtoul(optarg, NULL, 10);
220+ if (len == ULONG_MAX) {
221+ usage();
222+ return -EINVAL;
223+ }
224+ break;
225+ default:
226+ usage();
227+ }
228+ break;
229+ case 'k':
230+ key = hextobin(optarg, &key_len);
231+ break;
232+ case 'n':
233+ name = optarg;
234+ type = check_type(name);
235+ if(type > TYPE_HMAC){
236+ usage();
237+ return -EINVAL;
238+ }
239+ break;
240+ case 'm':
241+ message = hextobin(optarg, &message_len);
242+ break;
243+ case 'l':
244+ len = strtoul(optarg, NULL, 10);
245+ if (len == ULONG_MAX) {
246+ usage();
247+ return -EINVAL;
248+ }
249+ break;
250+ case 'e':
251+ empty = 1;
252+ break;
253+ default:
254+ usage();
255+ }
256+ }
257+
258+ if(type == TYPE_HMAC)
259+ if(!key)
260+ usage();
261+ if (!name || !message || !len)
262+ usage();
263+
264+ *outlen = (size_t)len;
265+ return 0;
266+}
267+
268+static void print(unsigned char *out, int outlen) {
269+ int i = 0;
270+
271+ for(i = 0; i < outlen; i++)
272+ printf("%02x", out[i]);
273+ printf("\n");
274+}
275+
276+int main(int argc, char *argv[])
277+{
278+ size_t outlen = 0;
279+ ssize_t ret = 0;
280+ unsigned char *out = NULL;
281+
282+ ret = parse_opts(argc, argv, &outlen);
283+ if (ret)
284+ return (int)ret;
285+
286+ out = (unsigned char*)malloc(outlen);
287+ if (type == TYPE_SHA) {
288+ if (empty)
289+ ret = msg_digest[type_index].fun(message, 0, out, outlen);
290+ else
291+ ret = msg_digest[type_index].fun(message, message_len, out, outlen);
292+ } else if (type == TYPE_HMAC){
293+ if(empty)
294+ ret = key_msg_digest[type_index].fun(key, key_len, message, 0,
295+ out, outlen);
296+ else
297+ ret = key_msg_digest[type_index].fun(key, key_len, message, message_len,
298+ out, outlen);
299+ }
300+ if(ret < 0)
301+ goto out;
302+ print(out, outlen);
303+
304+out:
305+ uninit();
306+ if(out)
307+ free(out);
308+ return (int)ret;
309+}