コード例 #1
0
ファイル: encrypt.c プロジェクト: wongzigii/shadowsocks-iOS
void encrypt_buf(struct encryption_ctx *ctx, unsigned char *buf, size_t *len) {
    if (ctx->cipher == CIPHER_TABLE) {
        table_encrypt(buf, *len);
    } else {
        if (ctx->status == STATUS_EMPTY) {
            size_t iv_len = encryption_iv_len[_method];
            memset(ctx->iv, 0, iv_len);
            RAND_bytes(ctx->iv, iv_len);
            init_cipher(ctx, ctx->iv, iv_len, 1);
            size_t out_len = *len + ctx->iv_len;
            unsigned char *cipher_text = malloc(out_len);
            cipher_update(ctx, cipher_text, &out_len, buf, *len);
            memcpy(buf, ctx->iv, iv_len);
            memcpy(buf + iv_len, cipher_text, out_len);
            *len = iv_len + out_len;
            free(cipher_text);
        } else {
            size_t out_len = *len + ctx->iv_len;
            unsigned char *cipher_text = malloc(out_len);
            cipher_update(ctx, cipher_text, &out_len, buf, *len);
            memcpy(buf, cipher_text, out_len);
            *len = out_len;
            free(cipher_text);
        }
    }
}
コード例 #2
0
ファイル: encrypt.c プロジェクト: hynnet/ShadowWeb
void encrypt_buf(struct encryption_ctx *ctx, char *buf, int *len) {
    if (_method == EncryptionTable) {
        table_encrypt(buf, *len);
    } else {
        if (ctx->status == STATUS_EMPTY) {
            int iv_len = encryption_iv_len[_method];
            unsigned char iv[EVP_MAX_IV_LENGTH];
            memset(iv, 0, iv_len);
            RAND_bytes(iv, iv_len);
            init_cipher(ctx, iv, iv_len, 1);
            int out_len = *len + EVP_CIPHER_CTX_block_size(ctx->ctx);
            unsigned char *cipher_text = malloc(out_len);
            EVP_CipherUpdate(ctx->ctx, cipher_text, &out_len, buf, *len);
            memcpy(buf, iv, iv_len);
            memcpy(buf + iv_len, cipher_text, out_len);
            *len = iv_len + out_len;
            free(cipher_text);
        } else {
            int out_len = *len + EVP_CIPHER_CTX_block_size(ctx->ctx);
            unsigned char *cipher_text = malloc(out_len);
            EVP_CipherUpdate(ctx->ctx, cipher_text, &out_len, buf, *len);
            memcpy(buf, cipher_text, out_len);
            *len = out_len;
            free(cipher_text);
        }
    }
}