按规范「注释只讲解作用,不掺杂历史原因,尽量英文」:把 includes 下 75 个 .vp 的中文 # 注释与三引号 docstring 改写为英文,每个注释块压到 2 行以内。 保留要点:作用、约束、危险警告(不得/必须/注意)、ABI 与类型约定(如 BYTEPTR | t.CPtr = i8** 的跨模块坍缩坑)、位宽/长度约定、fail-fast 要求、内存与句柄所有权。 未动:文件头注释(规范豁免,仍是中文)、运行时中文字符串常量(printf / fatal_error / MlirFail 等用户可见消息,属代码字面量)。 验证:独立脚本比对 HEAD 与工作区的代码骨架(剥离 # 注释与三引号 docstring 后的非空行)——75 个文件逐字节一致;行尾 CRLF/LF 未被改动;全文剩余中文仅 547 行头注释、0 条行尾注释。bootstrap 三重重编 SUCCESS;全量 77/77 顺序回归通过。
233 lines
12 KiB
Plaintext
233 lines
12 KiB
Plaintext
import t, c
|
|
from openssl.types import *
|
|
|
|
# ============================================================
|
|
# openssl/evp.vp — EVP 高层密码 API
|
|
# 对应头文件: evp.h
|
|
#
|
|
# 包含:消息摘要(EVP_Digest*)、对称加解密(EVP_Cipher*/Encrypt*/Decrypt*)、
|
|
# Base64 编解码(EVP_EncodeBlock/DecodeBlock)、非对称(EVP_PKEY* 签名/验签/加解密)
|
|
# ============================================================
|
|
|
|
define EvpMaxMdSize = 64
|
|
define EvpMaxKeyLength = 64
|
|
define EvpMaxIvLength = 16
|
|
define EvpMaxBlockLength = 32
|
|
|
|
# ============================================================
|
|
# 1) Digest algorithm objects (const EVP_MD*)
|
|
# ============================================================
|
|
extern def EVP_md5() -> EvpMdPtr
|
|
extern def EVP_sha1() -> EvpMdPtr
|
|
extern def EVP_sha224() -> EvpMdPtr
|
|
extern def EVP_sha256() -> EvpMdPtr
|
|
extern def EVP_sha384() -> EvpMdPtr
|
|
extern def EVP_sha512() -> EvpMdPtr
|
|
extern def EVP_sha3_224() -> EvpMdPtr
|
|
extern def EVP_sha3_256() -> EvpMdPtr
|
|
extern def EVP_sha3_384() -> EvpMdPtr
|
|
extern def EVP_sha3_512() -> EvpMdPtr
|
|
extern def EVP_sm3() -> EvpMdPtr
|
|
# const EVP_MD *EVP_get_digestbyname(const char *name)
|
|
extern def EVP_get_digestbyname(name: str) -> EvpMdPtr
|
|
# int EVP_MD_get_size(const EVP_MD *md)
|
|
extern def EVP_MD_get_size(md: EvpMdPtr) -> int
|
|
# int EVP_MD_get_block_size(const EVP_MD *md)
|
|
extern def EVP_MD_get_block_size(md: EvpMdPtr) -> int
|
|
# int EVP_MD_get_type(const EVP_MD *md)
|
|
extern def EVP_MD_get_type(md: EvpMdPtr) -> int
|
|
|
|
# ============================================================
|
|
# 2) Digest contexts (incremental)
|
|
# ============================================================
|
|
# EVP_MD_CTX *EVP_MD_CTX_new(void)
|
|
extern def EVP_MD_CTX_new() -> EvpMdCtxPtr
|
|
# void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
|
|
extern def EVP_MD_CTX_free(ctx: EvpMdCtxPtr)
|
|
# int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
|
|
extern def EVP_MD_CTX_reset(ctx: EvpMdCtxPtr) -> int
|
|
# int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
|
|
extern def EVP_DigestInit_ex(ctx: EvpMdCtxPtr, type: EvpMdPtr, impl: bytes) -> int
|
|
# int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
|
|
extern def EVP_DigestInit(ctx: EvpMdCtxPtr, type: EvpMdPtr) -> int
|
|
# int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt)
|
|
extern def EVP_DigestUpdate(ctx: EvpMdCtxPtr, d: bytes, cnt: sizet) -> int
|
|
# int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s)
|
|
extern def EVP_DigestFinal_ex(ctx: EvpMdCtxPtr, md: bytes, s: bytes) -> int
|
|
# int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s)
|
|
extern def EVP_DigestFinal(ctx: EvpMdCtxPtr, md: bytes, s: bytes) -> int
|
|
# int EVP_MD_CTX_get_size(const EVP_MD_CTX *ctx)
|
|
extern def EVP_MD_CTX_get_size(ctx: EvpMdCtxPtr) -> int
|
|
# One-shot digest: int EVP_Digest(const void *data, size_t count, unsigned char *md,
|
|
# unsigned int *size, const EVP_MD *type, ENGINE *impl)
|
|
extern def EVP_Digest(data: bytes, count: sizet, md: bytes, size: bytes,
|
|
type: EvpMdPtr, impl: bytes) -> int
|
|
|
|
# ============================================================
|
|
# 3) Symmetric cipher objects (const EVP_CIPHER*)
|
|
# ============================================================
|
|
extern def EVP_aes_128_ecb() -> EvpCipherPtr
|
|
extern def EVP_aes_128_cbc() -> EvpCipherPtr
|
|
extern def EVP_aes_128_cfb() -> EvpCipherPtr
|
|
extern def EVP_aes_128_ofb() -> EvpCipherPtr
|
|
extern def EVP_aes_128_ctr() -> EvpCipherPtr
|
|
extern def EVP_aes_128_gcm() -> EvpCipherPtr
|
|
extern def EVP_aes_192_ecb() -> EvpCipherPtr
|
|
extern def EVP_aes_192_cbc() -> EvpCipherPtr
|
|
extern def EVP_aes_192_ctr() -> EvpCipherPtr
|
|
extern def EVP_aes_192_gcm() -> EvpCipherPtr
|
|
extern def EVP_aes_256_ecb() -> EvpCipherPtr
|
|
extern def EVP_aes_256_cbc() -> EvpCipherPtr
|
|
extern def EVP_aes_256_cfb() -> EvpCipherPtr
|
|
extern def EVP_aes_256_ofb() -> EvpCipherPtr
|
|
extern def EVP_aes_256_ctr() -> EvpCipherPtr
|
|
extern def EVP_aes_256_gcm() -> EvpCipherPtr
|
|
extern def EVP_des_ede3_cbc() -> EvpCipherPtr
|
|
extern def EVP_chacha20() -> EvpCipherPtr
|
|
extern def EVP_rc4() -> EvpCipherPtr
|
|
# const EVP_CIPHER *EVP_get_cipherbyname(const char *name)
|
|
extern def EVP_get_cipherbyname(name: str) -> EvpCipherPtr
|
|
# Parameter queries
|
|
extern def EVP_CIPHER_get_block_size(cipher: EvpCipherPtr) -> int
|
|
extern def EVP_CIPHER_get_key_length(cipher: EvpCipherPtr) -> int
|
|
extern def EVP_CIPHER_get_iv_length(cipher: EvpCipherPtr) -> int
|
|
extern def EVP_CIPHER_get_nid(cipher: EvpCipherPtr) -> int
|
|
extern def EVP_CIPHER_get_type(cipher: EvpCipherPtr) -> int
|
|
|
|
# ============================================================
|
|
# 4) Symmetric cipher contexts
|
|
# ============================================================
|
|
# EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
|
|
extern def EVP_CIPHER_CTX_new() -> EvpCipherCtxPtr
|
|
# void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *c)
|
|
extern def EVP_CIPHER_CTX_free(c: EvpCipherCtxPtr)
|
|
# int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *c)
|
|
extern def EVP_CIPHER_CTX_reset(c: EvpCipherCtxPtr) -> int
|
|
# int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
|
|
extern def EVP_CIPHER_CTX_copy(out: EvpCipherCtxPtr, inn: EvpCipherCtxPtr) -> int
|
|
|
|
# Unified interface (enc: 1 encrypt / 0 decrypt)
|
|
# int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
|
|
# ENGINE *impl, const unsigned char *key, const unsigned char *iv, int enc)
|
|
extern def EVP_CipherInit_ex(ctx: EvpCipherCtxPtr, cipher: EvpCipherPtr,
|
|
impl: bytes, key: bytes, iv: bytes, enc: int) -> int
|
|
# int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
|
|
# const unsigned char *in, int inl)
|
|
extern def EVP_CipherUpdate(ctx: EvpCipherCtxPtr, out: bytes, outl: bytes,
|
|
inn: bytes, inl: int) -> int
|
|
# int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl)
|
|
extern def EVP_CipherFinal_ex(ctx: EvpCipherCtxPtr, outm: bytes, outl: bytes) -> int
|
|
# int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
|
|
# const unsigned char *key, const unsigned char *iv, int enc)
|
|
extern def EVP_CipherInit(ctx: EvpCipherCtxPtr, cipher: EvpCipherPtr,
|
|
key: bytes, iv: bytes, enc: int) -> int
|
|
|
|
# Split encrypt/decrypt (specializations of the unified interface above)
|
|
# int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
|
|
# ENGINE *impl, const unsigned char *key, const unsigned char *iv)
|
|
extern def EVP_EncryptInit_ex(ctx: EvpCipherCtxPtr, cipher: EvpCipherPtr,
|
|
impl: bytes, key: bytes, iv: bytes) -> int
|
|
# int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
|
|
# const unsigned char *in, int inl)
|
|
extern def EVP_EncryptUpdate(ctx: EvpCipherCtxPtr, out: bytes, outl: bytes,
|
|
inn: bytes, inl: int) -> int
|
|
# int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
|
|
extern def EVP_EncryptFinal_ex(ctx: EvpCipherCtxPtr, out: bytes, outl: bytes) -> int
|
|
|
|
# int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
|
|
# ENGINE *impl, const unsigned char *key, const unsigned char *iv)
|
|
extern def EVP_DecryptInit_ex(ctx: EvpCipherCtxPtr, cipher: EvpCipherPtr,
|
|
impl: bytes, key: bytes, iv: bytes) -> int
|
|
# int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
|
|
# const unsigned char *in, int inl)
|
|
extern def EVP_DecryptUpdate(ctx: EvpCipherCtxPtr, out: bytes, outl: bytes,
|
|
inn: bytes, inl: int) -> int
|
|
# int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl)
|
|
extern def EVP_DecryptFinal_ex(ctx: EvpCipherCtxPtr, outm: bytes, outl: bytes) -> int
|
|
|
|
# Context query / control
|
|
extern def EVP_CIPHER_CTX_set_padding(c: EvpCipherCtxPtr, pad: int) -> int
|
|
extern def EVP_CIPHER_CTX_set_key_length(c: EvpCipherCtxPtr, keylen: int) -> int
|
|
extern def EVP_CIPHER_CTX_ctrl(ctx: EvpCipherCtxPtr, type: int, arg: int, ptr: bytes) -> int
|
|
extern def EVP_CIPHER_CTX_key_length(ctx: EvpCipherCtxPtr) -> int
|
|
extern def EVP_CIPHER_CTX_iv_length(ctx: EvpCipherCtxPtr) -> int
|
|
extern def EVP_CIPHER_CTX_get_block_size(ctx: EvpCipherCtxPtr) -> int
|
|
extern def EVP_CIPHER_CTX_get_key_length(ctx: EvpCipherCtxPtr) -> int
|
|
extern def EVP_CIPHER_CTX_get_iv_length(ctx: EvpCipherCtxPtr) -> int
|
|
# int EVP_CIPHER_CTX_get_iv(const EVP_CIPHER_CTX *ctx, void *iv, size_t len)
|
|
extern def EVP_CIPHER_CTX_get_iv(ctx: EvpCipherCtxPtr, iv: bytes, len: sizet) -> int
|
|
# int EVP_CIPHER_CTX_set_iv(EVP_CIPHER_CTX *ctx, const void *iv, size_t len)
|
|
extern def EVP_CIPHER_CTX_set_iv(ctx: EvpCipherCtxPtr, iv: bytes, len: sizet) -> int
|
|
|
|
# ---- GCM ctrl constants (used with EVP_CIPHER_CTX_ctrl) ----
|
|
define EvpCtrlGcmSetIvl = 0x9# EVP_CTRL_AEAD_SET_IVLEN
|
|
define EvpCtrlGcmGetTag = 0x10# EVP_CTRL_AEAD_GET_TAG
|
|
define EvpCtrlGcmSetTag = 0x11# EVP_CTRL_AEAD_SET_TAG
|
|
|
|
# ============================================================
|
|
# 5) Base64 (EVP encode/decode block functions)
|
|
# ============================================================
|
|
# int EVP_EncodeBlock(unsigned char *t, const unsigned char *f, int n)
|
|
extern def EVP_EncodeBlock(t: bytes, f: bytes, n: int) -> int
|
|
# int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int n)
|
|
extern def EVP_DecodeBlock(t: bytes, f: bytes, n: int) -> int
|
|
|
|
# ============================================================
|
|
# 6) Asymmetric (EVP_PKEY / EVP_PKEY_CTX)
|
|
# ============================================================
|
|
# EVP_PKEY *EVP_PKEY_new(void) / void EVP_PKEY_free(EVP_PKEY *pkey)
|
|
extern def EVP_PKEY_new() -> EvpPkeyPtr
|
|
extern def EVP_PKEY_free(pkey: EvpPkeyPtr)
|
|
# int EVP_PKEY_get_size(const EVP_PKEY *pkey) (signature/ciphertext length)
|
|
extern def EVP_PKEY_get_size(pkey: EvpPkeyPtr) -> int
|
|
extern def EVP_PKEY_get_id(pkey: EvpPkeyPtr) -> int
|
|
extern def EVP_PKEY_get_base_id(pkey: EvpPkeyPtr) -> int
|
|
extern def EVP_PKEY_eq(a: EvpPkeyPtr, b: EvpPkeyPtr) -> int
|
|
|
|
# EVP_PKEY_CTX
|
|
# EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
|
|
extern def EVP_PKEY_CTX_new(pkey: EvpPkeyPtr, e: bytes) -> EvpPkeyCtxPtr
|
|
# void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
|
|
extern def EVP_PKEY_CTX_free(ctx: EvpPkeyCtxPtr)
|
|
|
|
# Sign / verify (raw RSA/EC)
|
|
extern def EVP_PKEY_sign_init(ctx: EvpPkeyCtxPtr) -> int
|
|
extern def EVP_PKEY_verify_init(ctx: EvpPkeyCtxPtr) -> int
|
|
extern def EVP_PKEY_encrypt_init(ctx: EvpPkeyCtxPtr) -> int
|
|
extern def EVP_PKEY_decrypt_init(ctx: EvpPkeyCtxPtr) -> int
|
|
# int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
|
|
# const unsigned char *tbs, size_t tbslen)
|
|
extern def EVP_PKEY_sign(ctx: EvpPkeyCtxPtr, sig: bytes, siglen: bytes,
|
|
tbs: bytes, tbslen: sizet) -> int
|
|
extern def EVP_PKEY_verify(ctx: EvpPkeyCtxPtr, sig: bytes, siglen: sizet,
|
|
tbs: bytes, tbslen: sizet) -> int
|
|
extern def EVP_PKEY_encrypt(ctx: EvpPkeyCtxPtr, out: bytes, outlen: bytes,
|
|
inn: bytes, inlen: sizet) -> int
|
|
extern def EVP_PKEY_decrypt(ctx: EvpPkeyCtxPtr, out: bytes, outlen: bytes,
|
|
inn: bytes, inlen: sizet) -> int
|
|
|
|
# Digest sign/verify (DigestSign*: mostly RSA-PSS/ECDSA)
|
|
# int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type,
|
|
# ENGINE *e, EVP_PKEY *pkey)
|
|
extern def EVP_DigestSignInit(ctx: EvpMdCtxPtr, pctx: bytes, type: EvpMdPtr,
|
|
e: bytes, pkey: EvpPkeyPtr) -> int
|
|
extern def EVP_DigestSignUpdate(ctx: EvpMdCtxPtr, data: bytes, dsize: sizet) -> int
|
|
# int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen)
|
|
extern def EVP_DigestSignFinal(ctx: EvpMdCtxPtr, sigret: bytes, siglen: bytes) -> int
|
|
# int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type,
|
|
# ENGINE *e, EVP_PKEY *pkey)
|
|
extern def EVP_DigestVerifyInit(ctx: EvpMdCtxPtr, pctx: bytes, type: EvpMdPtr,
|
|
e: bytes, pkey: EvpPkeyPtr) -> int
|
|
extern def EVP_DigestVerifyUpdate(ctx: EvpMdCtxPtr, data: bytes, dsize: sizet) -> int
|
|
extern def EVP_DigestVerifyFinal(ctx: EvpMdCtxPtr, sig: bytes, siglen: sizet) -> int
|
|
|
|
# ============================================================
|
|
# 7) Key derivation / helpers
|
|
# ============================================================
|
|
# int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
|
|
# const unsigned char *salt, const unsigned char *data, int datal, int count,
|
|
# unsigned char *key, unsigned char *iv)
|
|
extern def EVP_BytesToKey(type: EvpCipherPtr, md: EvpMdPtr, salt: bytes,
|
|
data: bytes, datal: int, count: int,
|
|
key: bytes, iv: bytes) -> int
|