diff --git a/src/zuc256.c b/src/zuc256.c index 0fd22f3..e39c821 100644 --- a/src/zuc256.c +++ b/src/zuc256.c @@ -1,4 +1,36 @@ - +/* + * Copyright (C) 2025. Institute of Information Engineering, CAS + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * @file: zuc256.c + * @brief: zuc256 的纯c代码 + * @author: QZH + * @version: 1.0.0 + * @date: 2025-09-01 + * + * @note: 无 + * + * Change Logs: + * Date Author Notes + * 2025-08-04 QZH 创建文件 + */ #include #include #include "zuc256.h" @@ -386,18 +418,18 @@ void zuc256_crypt(ZUC_STATE *state, const uint8_t *in, size_t inlen, uint8_t *ou if (!state || !in || !out) return; ZUC256_ENCRYPT_CTX ctx; - // 修复1:初始化ctx内存(仅清空,不调用zuc256_encrypt_init) + // 修复1:初始化ctx内存(仅清空,不调用zuc256_encrypt_init) memset(&ctx, 0, sizeof(ZUC256_ENCRYPT_CTX)); - // 修复2:将传入的合法state复制到ctx->state,复用已有状态(含K/IV对应的初始化结果) + // 修复2:将传入的合法state复制到ctx->state,复用已有状态(含K/IV对应的初始化结果) memcpy(&ctx.state, state, sizeof(ZUC_STATE)); - // 正常执行加解密(使用复用的state) + // 正常执行加解密(使用复用的state) zuc256_encrypt_update(&ctx, in, inlen, out); - // 计算剩余数据的偏移:(inlen / 4)*4 是完整4字节块的长度,剩余数据从这里开始 + // 计算剩余数据的偏移:(inlen / 4)*4 是完整4字节块的长度,剩余数据从这里开始 size_t remaining_offset = (inlen / 4) * 4; zuc256_encrypt_finish(&ctx, out + remaining_offset); - // 修复3:将ctx->state的最新状态回写到传入的state(确保后续连续加解密的状态正确) + // 修复3:将ctx->state的最新状态回写到传入的state(确保后续连续加解密的状态正确) memcpy(state, &ctx.state, sizeof(ZUC_STATE)); } void extract_iv(const uint8_t *input_25byte, uint8_t *output_23byte) { @@ -420,10 +452,10 @@ void extract_iv(const uint8_t *input_25byte, uint8_t *output_23byte) { /** * @brief 初始化ZUC256 MAC上下文 - * @param ctx:MAC上下文指针(输出) - * @param key:256位密钥(32字节,输入) - * @param iv:23字节初始向量(输入) - * @param macbits:期望MAC输出位数(32/64/128,自动调整范围:<32→32,>128→128) + * @param ctx:MAC上下文指针(输出) + * @param key:256位密钥(32字节,输入) + * @param iv:23字节初始向量(输入) + * @param macbits:期望MAC输出位数(32/64/128,自动调整范围:<32→32,>128→128) */ void zuc256_mac_init(ZUC256_MAC_CTX *ctx, const uint8_t key[32], const uint8_t iv[23], int macbits) @@ -439,10 +471,10 @@ void zuc256_mac_init(ZUC256_MAC_CTX *ctx, const uint8_t key[32], ctx->macbits = (macbits/32) * 32; } /** - * @brief 更新ZUC256 MAC待认证数据(支持分块输入) - * @param ctx:已初始化的MAC上下文(输入/输出) - * @param data:待认证数据块(输入,可NULL) - * @param len:待认证数据长度(字节,输入,0则无操作) + * @brief 更新ZUC256 MAC待认证数据(支持分块输入) + * @param ctx:已初始化的MAC上下文(输入/输出) + * @param data:待认证数据块(输入,可NULL) + * @param len:待认证数据长度(字节,输入,0则无操作) */ void zuc256_mac_update(ZUC256_MAC_CTX *ctx, const uint8_t *data, size_t len) { @@ -515,10 +547,10 @@ void zuc256_mac_update(ZUC256_MAC_CTX *ctx, const uint8_t *data, size_t len) } /** * @brief 完成ZUC256 MAC计算,输出最终认证码 - * @param ctx:已更新数据的MAC上下文(输入/输出,调用后清空) - * @param data:最后一块待认证数据(可NULL,若需补充不足1字节的比特) - * @param nbits:最后一块数据的额外比特数(0~7,仅当data非NULL时有效) - * @param mac:MAC输出缓冲区(需提前分配至少 ctx->macbits/8 字节空间) + * @param ctx:已更新数据的MAC上下文(输入/输出,调用后清空) + * @param data:最后一块待认证数据(可NULL,若需补充不足1字节的比特) + * @param nbits:最后一块数据的额外比特数(0~7,仅当data非NULL时有效) + * @param mac:MAC输出缓冲区(需提前分配至少 ctx->macbits/8 字节空间) */ void zuc256_mac_finish(ZUC256_MAC_CTX *ctx, const uint8_t *data, size_t nbits, uint8_t *mac) { @@ -568,13 +600,13 @@ void zuc256_mac_finish(ZUC256_MAC_CTX *ctx, const uint8_t *data, size_t nbits, u memset(ctx, 0, sizeof(*ctx)); } /** - * @brief 一次性ZUC256 MAC计算(简化接口,适用于非流式数据) - * @param K:256位密钥(32字节,输入) - * @param IV:23字节初始向量(输入) - * @param data:待认证数据(输入,可NULL) - * @param len:待认证数据长度(字节,输入) - * @param macbits:MAC输出位数(32/64/128,输入) - * @param mac:MAC输出缓冲区(输出,需提前分配空间) + * @brief 一次性ZUC256 MAC计算(简化接口,适用于非流式数据 + * @param K:256位密钥(32字节,输入) + * @param IV:23字节初始向量(输入) + * @param data:待认证数据(输入,可NULL) + * @param len:待认证数据长度(字节,输入) + * @param macbits:MAC输出位数(32/64/128,输入) + * @param mac:MAC输出缓冲区(输出,需提前分配空间) */ void zuc256_mac(const uint8_t K[32], const uint8_t IV[23], const uint8_t *data, size_t len, int macbits, uint8_t *mac) { ZUC256_MAC_CTX ctx;