已合并
feat(cipher): add sm4-xts encryption/decryption demo #819
pacholly创建于 2025年11月18日
feat(cipher): add sm4-xts encryption/decryption demo #819
已合并
共 2 个文件变更+142-1
| @@ -166,4 +166,4 @@ EXIT: | |||
| 166 | CRYPT_EAL_CipherFreeCtx(ctx); | 166 | CRYPT_EAL_CipherFreeCtx(ctx); |
| 167 | BSL_ERR_DeInit(); | 167 | BSL_ERR_DeInit(); |
| 168 | return ret; | 168 | return ret; |
| 169 | -} | 169 | +} |
| @@ -0,0 +1,141 @@ | |||
| 1 | +/* Copyright (c) 2025, Shandong University — School of Cyber Science and Technology | ||
| 2 | + * Contributor: Haolin Du | ||
| 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[SM4_XTS_KEY_LEN] = { | ||
| 34 | + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | ||
| 35 | + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, | ||
| 36 | + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, | ||
| 37 | + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f | ||
| 38 | +}; | ||
| 39 | + | ||
| 40 | +static const uint8_t DEMO_TWEAK[SM4_XTS_TWEAK_LEN] = { | ||
| 41 | + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | ||
| 42 | + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F | ||
| 43 | +}; | ||
| 44 | + | ||
| 45 | +// Test data | ||
| 46 | +static uint8_t g_demoData[SM4_XTS_DATA_LEN] = {0}; | ||
| 47 | + | ||
| 48 | +static void PrintLastError(void) | ||
| 49 | +{ | ||
| 50 | + const char *file = NULL; | ||
| 51 | + uint32_t line = 0U; | ||
| 52 | + BSL_ERR_GetLastErrorFileLine(&file, &line); | ||
| 53 | + printf("failed at file %s at line %u\n", file, line); | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | +static void PrintHex(const char *label, const uint8_t *data, uint32_t len) | ||
| 57 | +{ | ||
| 58 | + printf("%s", label); | ||
| 59 | + uint32_t printLen = (len > 32U) ? 32U : len; | ||
| 60 | + for (uint32_t i = 0U; i < printLen; i++) { | ||
| 61 | + printf("%02x", data[i]); | ||
| 62 | + } | ||
| 63 | + if (len > 32U) { | ||
| 64 | + printf("... (total %u bytes)", len); | ||
| 65 | + } | ||
| 66 | + printf("\n"); | ||
| 67 | +} | ||
| 68 | + | ||
| 69 | +/* Helper to perform SM4-XTS operation */ | ||
| 70 | +static int DoCipherOp(CRYPT_EAL_CipherCtx *ctx, uint8_t *in, uint32_t inLen, | ||
| 71 | + uint8_t *out, bool isEncrypt) | ||
| 72 | +{ | ||
| 73 | + uint32_t outLen = inLen; | ||
| 74 | + uint32_t finalLen = 0U; | ||
| 75 | + int ret; | ||
| 76 | + | ||
| 77 | + ret = CRYPT_EAL_CipherInit(ctx, (uint8_t *)DEMO_KEY, SM4_XTS_KEY_LEN, | ||
| 78 | + (uint8_t *)DEMO_TWEAK, SM4_XTS_TWEAK_LEN, isEncrypt); | ||
| 79 | + if (ret != CRYPT_SUCCESS) { | ||
| 80 | + return ret; | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + ret = CRYPT_EAL_CipherUpdate(ctx, in, inLen, out, &outLen); | ||
| 84 | + if (ret != CRYPT_SUCCESS) { | ||
| 85 | + return ret; | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + finalLen = inLen - outLen; | ||
| 89 | + ret = CRYPT_EAL_CipherFinal(ctx, out + outLen, &finalLen); | ||
| 90 | + return ret; | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +static int RunSm4XtsDemo(void) | ||
| 94 | +{ | ||
| 95 | + // use fixed-size array instead of malloc, initialize directly with ={0} | ||
| 96 | + uint8_t cipherText[SM4_XTS_DATA_LEN] = {0}; | ||
| 97 | + uint8_t plainText[SM4_XTS_DATA_LEN] = {0}; | ||
| 98 | + | ||
| 99 | + // Initialize test data (increment from 1 to 1024 bytes) | ||
| 100 | + for (uint32_t i = 0; i < SM4_XTS_DATA_LEN; i++) { | ||
| 101 | + g_demoData[i] = (uint8_t)(i % 256U); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(CRYPT_CIPHER_SM4_XTS); | ||
| 105 | + if (ctx == NULL) { | ||
| 106 | + printf("CipherNewCtx failed.\n"); | ||
| 107 | + return -1; | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + /* Encrypt */ | ||
| 111 | + if (DoCipherOp(ctx, (uint8_t *)g_demoData, SM4_XTS_DATA_LEN, cipherText, true) != CRYPT_SUCCESS) { | ||
| 112 | + PrintLastError(); | ||
| 113 | + CRYPT_EAL_CipherFreeCtx(ctx); | ||
| 114 | + return -1; | ||
| 115 | + } | ||
| 116 | + PrintHex("cipher text value is: ", cipherText, SM4_XTS_DATA_LEN); | ||
| 117 | + | ||
| 118 | + /* Decrypt */ | ||
| 119 | + if (DoCipherOp(ctx, cipherText, SM4_XTS_DATA_LEN, plainText, false) != CRYPT_SUCCESS) { | ||
| 120 | + PrintLastError(); | ||
| 121 | + CRYPT_EAL_CipherFreeCtx(ctx); | ||
| 122 | + return -1; | ||
| 123 | + } | ||
| 124 | + PrintHex("decrypted plain text value is: ", plainText, SM4_XTS_DATA_LEN); | ||
| 125 | + | ||
| 126 | + // Verify result | ||
| 127 | + int finalRet = (memcmp(g_demoData, plainText, SM4_XTS_DATA_LEN) == 0) ? 0U : -1U; | ||
| 128 | + if (finalRet == 0U) { | ||
| 129 | + printf("SM4-XTS Test Passed!\n"); | ||
| 130 | + } else { | ||
| 131 | + printf("SM4-XTS Test Failed: Plaintext mismatch.\n"); | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + CRYPT_EAL_CipherFreeCtx(ctx); | ||
| 135 | + return finalRet; | ||
| 136 | +} | ||
| 137 | + | ||
| 138 | +int main(void) | ||
| 139 | +{ | ||
| 140 | + return RunSm4XtsDemo(); | ||
| 141 | +} | ||