blob: 6ae5c11f3ffdab4b8dd99094b0a3d2b4bd36adf2 [file] [log] [blame]
developer70436792023-05-19 09:31:20 +08001#include "common.h"
2#include "aesgcm.h"
3#include "aesccm.h"
4
5
6struct common_data input_data = {
7 .key = NULL,
8 .iv = NULL,
9 .pt = NULL,
10 .add = NULL,
11 .ct = NULL,
12 .tag = NULL,
13 .nonce = NULL,
14 .adata = NULL,
15 .payload = NULL,
16 .key_len = 0,
17 .iv_len = 0,
18 .pt_len = 0,
19 .add_len = 0,
20 .ct_len = 0,
21 .tag_len = 0,
22 .nonce_len = 0,
23 .adata_len = 0,
24 .payload_len = 0,
25 .tag_output_size = 0,
26 .algo = -1,
27 .oper = -1,
28};
29
30ENGINE *setup_engine(void)
31{
32 ENGINE *e;
33
34 OpenSSL_add_all_algorithms();
35 ENGINE_load_builtin_engines();
36 e = ENGINE_by_id("devcrypto");
37
38 if (e == NULL) {
39 printf("engine error\n");
40 return NULL;
41 }
42 if (!ENGINE_init(e)) {
43 printf("error2\n");
44 ENGINE_free(e);
45 return NULL;
46 }
47 return e;
48}
49
50void usage(void)
51{
52 printf(
53 "gcm and gcm tool:\n"
54 "gcm Operations:\n"
55 "-e - encrypt\n"
56 "-d - decrypt\n"
57 "Common requirement parameters:\n"
58 "-k key(hex) - key in hex (must)\n"
59 "-i iv(hex) - initial vector in hex (must)\n"
60 "-p plain (hex) - plain text in hex\n"
61 "-c cipher(hex) - cipher text in hex\n"
62 "-a aad(hex) - additional authentication data in hex\n"
63 "-t tag(hex) - tag in hex (decrypt must)\n"
64 "-g tag size(dec)- tag output size (default 16)\n"
65 "ccm Operation:\n"
66 "-k key(hex) - key in hex (must)\n"
67 "-n nonce(hex) - nonce in hex (must)\n"
68 "-f adata(hex) - adata in hex\n"
69 "-l payload(hex) - payload in hex\n"
70 "-t encrypt size - tag size (must)\n"
71 " decrypt tag - tag (must)\n"
72 "tools: gcm or ccm\n"
73 "example:\n"
74 "gcm encrypt ./openssl-fips-ext -e -k data -i data ... gcm\n"
75 "gcm decrypt ./openssl-fips-ext -d -k data -i data -t data ... gcm\n"
76 "gcm encrypt ./openssl-fips-ext -e -k data -n data -t size ... ccm\n"
77 "gcm encrypt ./openssl-fips-ext -d -k data -n data -t data ... ccm\n");
78}
79
80void init_algo_data(void)
81{
82 if (input_data.algo == GCM)
83 cur.oper = gcm_oper;
84 else if (input_data.algo == CCM)
85 cur.oper = ccm_oper;
86}
87
88void do_operation(void)
89{
90 cur.oper.init();
91
92 if (cur.oper.check()) {
93 printf("Input error\n");
94 return;
95 }
96
97 if (input_data.oper == ENCRYPT)
98 cur.oper.encrypt();
99 else if (input_data.oper == DECRYPT)
100 cur.oper.decrypt();
101
102 cur.oper.uninit();
103}
104
105void hex2bin(unsigned char **des, char *src, long *len)
106{
107 if (src != NULL) {
108 *des = OPENSSL_hexstr2buf(src, len);
109 if (*des == NULL)
110 printf("openssl str to buf error\n");
111 } else {
112 *des = NULL;
113 *len = 0;
114 }
115}
116
117void print_hex(unsigned char *str, int len)
118{
119 for (int i = 0; i < len; i++)
120 printf("%02x", str[i]);
121 printf("\n");
122}
123
124void free_openssl_data(unsigned char *data)
125{
126 if (data != NULL)
127 OPENSSL_free(data);
128}