已合并
feat(cipher): add aes-ecb encryption/decryption demo #702
caibushihuichi创建于 2025年11月1日
feat(cipher): add aes-ecb encryption/decryption demo #702
已合并
caibushihuichi创建于 2025年11月1日
1 个文件变更+187-0
@@ -0,0 +1,187 @@
1+/* Copyright (c) 2025,Shandong University — School of Cyber Science and Technology
2+* Contributor: Fuzhi Wang
3+ * Instructor: Weijia Wang
4+*/
5+/*
6+ * This file is part of the openHiTLS project.
7+ *
8+ * openHiTLS is licensed under the Mulan PSL v2.
9+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
10+ * You may obtain a copy of Mulan PSL v2 at:
11+ *
12+ * http://license.coscl.org.cn/MulanPSL2
13+ *
14+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
15+ * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
16+ * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
17+ * See the Mulan PSL v2 for more details.
18+ */
19+#include <stdio.h>
20+#include <stdlib.h>
21+#include <stdint.h>
22+#include <string.h>
23+#include "crypt_eal_cipher.h" // Header for symmetric encryption/decryption
24+#include "bsl_err.h"
25+#include "crypt_algid.h" // Header for algorithm IDs
26+#include "crypt_errno.h" // Header for error codes
27+ 
28+// Define macros for constants to avoid magic numbers
29+#define AES_KEY_SIZE 16
30+#define BUFFER_SIZE 256
31+#define MODE_ENCRYPT 1
32+#define MODE_DECRYPT 0
33+ 
34+// A helper function to print error location
35+static void PrintLastError(void)
36+{
37+ const char *file = NULL;
38+ uint32_t line = 0;
39+ BSL_ERR_GetLastErrorFileLine(&file, &line); // Get the file name and line number of the error
40+ printf("Error occurred at file: %s, line: %u\n", file, line);
41+}
42+ 
43+static int32_t AesEcbDemoVerify(const unsigned char *plain, const unsigned char *decrypted, uint32_t len)
44+{
45+ printf("--- Verifying Result ---\n");
46+ if (memcmp(plain, decrypted, len) != 0) {
47+ printf("Verification FAILED! Decrypted text does not match original plaintext.\n");
48+ return -1;
49+ }
50+ printf("Verification PASSED! The decrypted text matches the original.\n");
51+ return 0;
52+}
53+ 
54+static int32_t AesEcbDemoEncrypt(CRYPT_EAL_CipherCtx *ctx, const unsigned char *in, uint32_t inLen,
55+ unsigned char *out, uint32_t *outLen)
56+{
57+ int32_t ret;
58+ uint32_t updateLen = *outLen;
59+ uint32_t finalLen = 0;
60+ uint32_t totalLen = 0;
61+ 
62+ ret = CRYPT_EAL_CipherUpdate(ctx, in, inLen, out, &updateLen);
63+ if (ret != CRYPT_SUCCESS) {
64+ PrintLastError();
65+ return ret;
66+ }
67+ totalLen = updateLen;
68+ 
69+ finalLen = *outLen - totalLen;
70+ ret = CRYPT_EAL_CipherFinal(ctx, out + totalLen, &finalLen);
71+ if (ret != CRYPT_SUCCESS) {
72+ PrintLastError();
73+ return ret;
74+ }
75+ 
76+ *outLen = totalLen + finalLen;
77+ printf("\nCiphertext Length: %u\n\n", *outLen);
78+ return CRYPT_SUCCESS;
79+}
80+ 
81+static int32_t AesEcbDemoDecrypt(CRYPT_EAL_CipherCtx *ctx, const unsigned char *in, uint32_t inLen,
82+ unsigned char *out, uint32_t *outLen)
83+{
84+ int32_t ret;
85+ uint32_t updateLen = *outLen;
86+ uint32_t finalLen = 0;
87+ uint32_t totalLen = 0;
88+ 
89+ ret = CRYPT_EAL_CipherUpdate(ctx, in, inLen, out, &updateLen);
90+ if (ret != CRYPT_SUCCESS) {
91+ PrintLastError();
92+ return ret;
93+ }
94+ totalLen = updateLen;
95+ 
96+ finalLen = *outLen - totalLen;
97+ ret = CRYPT_EAL_CipherFinal(ctx, out + totalLen, &finalLen);
98+ if (ret != CRYPT_SUCCESS) {
99+ PrintLastError();
100+ return ret;
101+ }
102+ 
103+ *outLen = totalLen + finalLen;
104+ return CRYPT_SUCCESS;
105+}
106+ 
107+static int32_t AesEcbDemoProcess(CRYPT_EAL_CipherCtx *ctx, const unsigned char *key,
108+ const unsigned char *plain, uint32_t plainLen)
109+{
110+ int32_t ret;
111+ unsigned char cipher[BUFFER_SIZE] = {0};
112+ unsigned char decrypted[BUFFER_SIZE] = {0};
113+ uint32_t cipherLen = BUFFER_SIZE;
114+ uint32_t decryptedLen = BUFFER_SIZE;
115+ 
116+ printf("--- Starting AES-ECB Encryption ---\n");
117+ ret = CRYPT_EAL_CipherInit(ctx, key, AES_KEY_SIZE, NULL, 0, 1);
118+ if (ret != CRYPT_SUCCESS) {
119+ PrintLastError();
120+ return ret;
121+ }
122+ ret = CRYPT_EAL_CipherSetPadding(ctx, CRYPT_PADDING_PKCS7);
123+ if (ret != CRYPT_SUCCESS) {
124+ PrintLastError();
125+ return ret;
126+ }
127+ 
128+ ret = AesEcbDemoEncrypt(ctx, plain, plainLen, cipher, &cipherLen);
129+ if (ret != CRYPT_SUCCESS) {
130+ return ret;
131+ }
132+ 
133+ printf("--- Starting AES-ECB Decryption ---\n");
134+ ret = CRYPT_EAL_CipherInit(ctx, key, AES_KEY_SIZE, NULL, 0, 0);
135+ if (ret != CRYPT_SUCCESS) {
136+ PrintLastError();
137+ return ret;
138+ }
139+ ret = CRYPT_EAL_CipherSetPadding(ctx, CRYPT_PADDING_PKCS7);
140+ if (ret != CRYPT_SUCCESS) {
141+ PrintLastError();
142+ return ret;
143+ }
144+ 
145+ ret = AesEcbDemoDecrypt(ctx, cipher, cipherLen, decrypted, &decryptedLen);
146+ if (ret != CRYPT_SUCCESS) {
147+ return ret;
148+ }
149+ 
150+ printf("Decrypted Plaintext: \"%s\"\nDecrypted Length: %u\n\n", decrypted, decryptedLen);
151+ 
152+ if (decryptedLen != plainLen) {
153+ return -1;
154+ }
155+ return AesEcbDemoVerify(plain, decrypted, plainLen);
156+}
157+ 
158+static int32_t RunAesEcbDemo(void)
159+{
160+ CRYPT_EAL_CipherCtx *ctx = NULL;
161+ unsigned char plainTextData[] = "This is a test message for AES-ECB.";
162+ unsigned char key[AES_KEY_SIZE] = "a_different_key!";
163+ int32_t ret;
164+ 
165+ printf("Original Plaintext: \"%s\"\nOriginal Length: %zu\n\n", plainTextData, sizeof(plainTextData) - 1);
166+ 
167+ ctx = CRYPT_EAL_CipherNewCtx(CRYPT_CIPHER_AES128_ECB);
168+ if (ctx == NULL) {
169+ printf("CRYPT_EAL_CipherNewCtx failed.\n");
170+ PrintLastError();
171+ return -1;
172+ }
173+ 
174+ ret = AesEcbDemoProcess(ctx, key, plainTextData, sizeof(plainTextData) - 1);
175+ 
176+ CRYPT_EAL_CipherFreeCtx(ctx);
177+ return ret;
178+}
179+ 
180+int main(void)
181+{
182+ int32_t ret = RunAesEcbDemo();
183+ if (ret == 0) {
184+ printf("\nAES-ECB Demo finished successfully!\n");
185+ }
186+ return ret;
187+}