Пример #1
0
void init_cipher(struct encryption_ctx *ctx, const unsigned char *iv, size_t iv_len, int is_cipher) {
    ctx->status = STATUS_INIT;
    if (ctx->cipher == CIPHER_OPENSSL) {
        EVP_CIPHER_CTX_init(ctx->ctx);
        EVP_CipherInit_ex(ctx->ctx, _cipher, NULL, NULL, NULL, is_cipher);
        if (!EVP_CIPHER_CTX_set_key_length(ctx->ctx, _key_len)) {
            cleanup_encryption(ctx);
            return;
        }
        EVP_CIPHER_CTX_set_padding(ctx->ctx, 1);
        unsigned char *true_key;
        if (_method == ENCRYPTION_RC4_MD5) {
            unsigned char key_iv[32];
            memcpy(key_iv, _key, 16);
            memcpy(key_iv + 16, iv, 16);
            true_key = MD5(key_iv, 32, NULL);
        } else {
            true_key = _key;
        }
        EVP_CipherInit_ex(ctx->ctx, NULL, NULL, true_key, iv, is_cipher);
    } else if (ctx->cipher == CIPHER_SODIUM) {
        ctx->ic = 0;
        ctx->bytes_remaining = 0;
    }
    ctx->iv_len = encryption_iv_len[_method];
}
Пример #2
0
void init_cipher(struct encryption_ctx *ctx, const unsigned char *iv, int iv_len, int is_cipher) {
    ctx->status = STATUS_INIT;
    if (_method != EncryptionTable) {
        EVP_CIPHER_CTX_init(ctx->ctx);
        EVP_CipherInit_ex(ctx->ctx, _cipher, NULL, NULL, NULL, is_cipher);
        if (!EVP_CIPHER_CTX_set_key_length(ctx->ctx, _key_len)) {
            cleanup_encryption(ctx);
//            NSLog(@"Invalid key length");
//            assert(0);
            // TODO free memory and report error
            return;
        }
        EVP_CIPHER_CTX_set_padding(ctx->ctx, 1);

        EVP_CipherInit_ex(ctx->ctx, NULL, NULL, _key, iv, is_cipher);

    }
}