blob: 2d8bf690e3d129170881ee3d0cf005ce8459efd4 [file] [log] [blame]
developer70436792023-05-19 09:31:20 +08001#include "aesccm.h"
2
3
4struct operator ccm_oper = {
5 .init = ccm_init,
6 .uninit = ccm_uninit,
7 .encrypt = ccm_encrypt,
8 .decrypt = ccm_decrypt,
9 .check = ccm_check,
10};
11
12static struct ccm_data data;
13
14int ccm_init(void)
15{
16 hex2bin(&data.key, input_data.key, &(data.key_size));
17 hex2bin(&data.nonce, input_data.nonce, &(data.nonce_size));
18 hex2bin(&data.adata, input_data.adata, &(data.adata_size));
19 hex2bin(&data.payload, input_data.payload, &(data.payload_size));
20 hex2bin(&data.ct, input_data.ct, &(data.ct_size));
21 if (input_data.oper == DECRYPT) {
22 hex2bin(&data.tag, input_data.tag, &(data.tag_size));
23 } else if (input_data.oper == ENCRYPT) {
24 if (input_data.tag != NULL)
25 data.tag_size = atoi(input_data.tag);
26 else
27 data.tag_size = 16;
28 }
29 return 0;
30}
31
32void init_ccm_evp_encrypt(EVP_CIPHER_CTX **ctx)
33{
34 if (data.key_size == 16)
35 EVP_EncryptInit_ex(*ctx, EVP_aes_128_ccm(), NULL, NULL, NULL);
36 else if (data.key_size == 24)
37 EVP_EncryptInit_ex(*ctx, EVP_aes_192_ccm(), NULL, NULL, NULL);
38 else if (data.key_size == 32)
39 EVP_EncryptInit_ex(*ctx, EVP_aes_256_ccm(), NULL, NULL, NULL);
40}
41
42void init_ccm_evp_decrypt(EVP_CIPHER_CTX **ctx)
43{
44 if (data.key_size == 16)
45 EVP_DecryptInit_ex(*ctx, EVP_aes_128_ccm(), NULL, NULL, NULL);
46 else if (data.key_size == 24)
47 EVP_DecryptInit_ex(*ctx, EVP_aes_192_ccm(), NULL, NULL, NULL);
48 else if (data.key_size == 32)
49 EVP_DecryptInit_ex(*ctx, EVP_aes_256_ccm(), NULL, NULL, NULL);
50}
51int ccm_encrypt(void)
52{
53 EVP_CIPHER_CTX *ctx;
54 int outlen;
55 unsigned char outbuf[1024];
56
57 ctx = EVP_CIPHER_CTX_new();
58 init_ccm_evp_encrypt(&ctx);
59
60 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, data.nonce_size, NULL);
61 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, data.tag_size, NULL);
62 EVP_EncryptInit_ex(ctx, NULL, NULL, data.key, data.nonce);
63
64 EVP_EncryptUpdate(ctx, NULL, &outlen, NULL, data.payload_size);
65 if (data.adata != NULL)
66 EVP_EncryptUpdate(ctx, NULL, &outlen, data.adata, data.adata_size);
67 if (data.payload != NULL) {
68 EVP_EncryptUpdate(ctx, outbuf, &outlen, data.payload, data.payload_size);
69 printf("Ciphertext: ");
70 print_hex(outbuf, outlen);
71 }
72 EVP_EncryptFinal_ex(ctx, outbuf, &outlen);
73 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, data.tag_size, outbuf);
74 printf("Tags: ");
75 print_hex(outbuf, data.tag_size);
76 EVP_CIPHER_CTX_free(ctx);
77 return 0;
78}
79
80int ccm_decrypt(void)
81{
82 EVP_CIPHER_CTX *ctx;
83 int outlen, rv;
84 unsigned char outbuf[1024];
85
86 ctx = EVP_CIPHER_CTX_new();
87
88 init_ccm_evp_decrypt(&ctx);
89
90 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, data.nonce_size, NULL);
91 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, data.tag_size, data.tag);
92 EVP_DecryptInit_ex(ctx, NULL, NULL, data.key, data.nonce);
93 EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, data.ct_size);
94
95 if (data.adata != NULL)
96 EVP_DecryptUpdate(ctx, NULL, &outlen, data.adata, data.adata_size);
97
98 if (data.ct == NULL)
99 rv = EVP_DecryptUpdate(ctx, outbuf, &outlen, "", data.ct_size);
100 else
101 rv = EVP_DecryptUpdate(ctx, outbuf, &outlen, data.ct, data.ct_size);
102
103 if (rv > 0) {
104 printf("Tag Verify: Pass\n");
105 if (data.ct != NULL) {
106 printf("Plaintext: ");
107 print_hex(outbuf, outlen);
108 }
109 } else {
110 printf("Tag Verify: Fail\n");
111 }
112
113 EVP_CIPHER_CTX_free(ctx);
114 return 0;
115}
116int ccm_uninit(void)
117{
118 free_openssl_data(data.key);
119 free_openssl_data(data.nonce);
120 free_openssl_data(data.adata);
121 free_openssl_data(data.payload);
122 free_openssl_data(data.ct);
123 free_openssl_data(data.tag);
124 return 0;
125}
126
127int ccm_check(void)
128{
129 if (data.key == NULL || data.nonce == NULL) {
130 printf("ccm must have Key and IV\n");
131 return -1;
132 }
133
134 if (input_data.oper == DECRYPT && data.tag == NULL) {
135 printf("ccm decrypt must have Tag\n");
136 return -1;
137 }
138
139 return 0;
140}