已合并
feat(cipher): add aes-xts encryption/decryption demo #845
loading······创建于 2025年11月24日
feat(cipher): add aes-xts encryption/decryption demo #845
已合并
共 1 个文件变更+131-0
| @@ -0,0 +1,131 @@ | |||
| 1 | +/* Copyright (c) 2025,Shandong University — School of Cyber Science and Technology | ||
| 2 | +* Contributor: Zengji Sun | ||
| 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 | + | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + | ||
| 23 | + | ||
| 24 | + | ||
| 25 | + | ||
| 26 | + | ||
| 27 | + | ||
| 28 | + | ||
| 29 | + | ||
| 30 | + | ||
| 31 | + | ||
| 32 | + | ||
| 33 | +static const uint8_t DEMO_KEY[AES_XTS_KEY_LEN] = { | ||
| 34 | + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, | ||
| 35 | + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, | ||
| 36 | + 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, | ||
| 37 | + 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18 | ||
| 38 | +}; | ||
| 39 | + | ||
| 40 | +static const uint8_t DEMO_TWEAK[AES_XTS_TWEAK_LEN] = { | ||
| 41 | + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 | ||
| 42 | +}; | ||
| 43 | + | ||
| 44 | +static const uint8_t DEMO_DATA[AES_XTS_DATA_LEN] = { | ||
| 45 | + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, | ||
| 46 | + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 | ||
| 47 | +}; | ||
| 48 | + | ||
| 49 | +static void PrintLastError(void) | ||
| 50 | +{ | ||
| 51 | + const char *file = NULL; | ||
| 52 | + uint32_t line = 0; | ||
| 53 | + BSL_ERR_GetLastErrorFileLine(&file, &line); | ||
| 54 | + printf("failed at file %s at line %u\n", file, line); | ||
| 55 | +} | ||
| 56 | + | ||
| 57 | +static void PrintHex(const char *label, const uint8_t *data, uint32_t len) | ||
| 58 | +{ | ||
| 59 | + printf("%s", label); | ||
| 60 | + for (uint32_t i = 0; i < len; i++) { | ||
| 61 | + printf("%02x", data[i]); | ||
| 62 | + } | ||
| 63 | + printf("\n"); | ||
| 64 | +} | ||
| 65 | + | ||
| 66 | +/* Helper to perform AES-XTS operation */ | ||
| 67 | +static int DoCipherOp(CRYPT_EAL_CipherCtx *ctx, uint8_t *in, uint32_t inLen, | ||
| 68 | + uint8_t *out, bool isEncrypt) | ||
| 69 | +{ | ||
| 70 | + uint32_t outLen = inLen; | ||
| 71 | + uint32_t finalLen = 0; | ||
| 72 | + int ret; | ||
| 73 | + | ||
| 74 | + ret = CRYPT_EAL_CipherInit(ctx, (uint8_t *)DEMO_KEY, AES_XTS_KEY_LEN, | ||
| 75 | + (uint8_t *)DEMO_TWEAK, AES_XTS_TWEAK_LEN, isEncrypt); | ||
| 76 | + if (ret != CRYPT_SUCCESS) { | ||
| 77 | + return ret; | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + ret = CRYPT_EAL_CipherUpdate(ctx, in, inLen, out, &outLen); | ||
| 81 | + if (ret != CRYPT_SUCCESS) { | ||
| 82 | + return ret; | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + finalLen = inLen - outLen; | ||
| 86 | + ret = CRYPT_EAL_CipherFinal(ctx, out + outLen, &finalLen); | ||
| 87 | + return ret; | ||
| 88 | +} | ||
| 89 | + | ||
| 90 | +static int RunAesXtsDemo(void) | ||
| 91 | +{ | ||
| 92 | + uint8_t cipherText[AES_XTS_DATA_LEN] = {0}; | ||
| 93 | + uint8_t plainText[AES_XTS_DATA_LEN] = {0}; | ||
| 94 | + CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(CRYPT_CIPHER_AES256_XTS); | ||
| 95 | + | ||
| 96 | + if (ctx == NULL) { | ||
| 97 | + printf("CipherNewCtx failed.\n"); | ||
| 98 | + return -1; | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + /* Encrypt */ | ||
| 102 | + if (DoCipherOp(ctx, (uint8_t *)DEMO_DATA, AES_XTS_DATA_LEN, cipherText, true) != CRYPT_SUCCESS) { | ||
| 103 | + PrintLastError(); | ||
| 104 | + CRYPT_EAL_CipherFreeCtx(ctx); | ||
| 105 | + return -1; | ||
| 106 | + } | ||
| 107 | + PrintHex("cipher text value is: ", cipherText, AES_XTS_DATA_LEN); | ||
| 108 | + | ||
| 109 | + /* Decrypt */ | ||
| 110 | + if (DoCipherOp(ctx, cipherText, AES_XTS_DATA_LEN, plainText, false) != CRYPT_SUCCESS) { | ||
| 111 | + PrintLastError(); | ||
| 112 | + CRYPT_EAL_CipherFreeCtx(ctx); | ||
| 113 | + return -1; | ||
| 114 | + } | ||
| 115 | + PrintHex("decrypted plain text value is: ", plainText, AES_XTS_DATA_LEN); | ||
| 116 | + | ||
| 117 | + int finalRet = (memcmp(DEMO_DATA, plainText, AES_XTS_DATA_LEN) == 0) ? 0 : -1; | ||
| 118 | + if (finalRet == 0) { | ||
| 119 | + printf("AES-XTS Test Passed!\n"); | ||
| 120 | + } else { | ||
| 121 | + printf("AES-XTS Test Failed: Plaintext mismatch.\n"); | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + CRYPT_EAL_CipherFreeCtx(ctx); | ||
| 125 | + return finalRet; | ||
| 126 | +} | ||
| 127 | + | ||
| 128 | +int main(void) | ||
| 129 | +{ | ||
| 130 | + return RunAesXtsDemo(); | ||
| 131 | +} | ||