-bool cipher_counter_xor(cipher_t *cipher, const void *indata, size_t inlen, void *outdata) {
- if(!cipher->counter) {
- logger(LOG_ERR, "Counter not initialized");
- return false;
- }
-
- const unsigned char *in = indata;
- unsigned char *out = outdata;
-
- while(inlen--) {
- // Encrypt the new counter value if we need it
- if(!cipher->counter->n) {
- int len;
- if(!EVP_EncryptUpdate(&cipher->ctx, cipher->counter->block, &len, cipher->counter->counter, cipher->cipher->block_size)) {
- logger(LOG_ERR, "Error while encrypting: %s", ERR_error_string(ERR_get_error(), NULL));
- return false;
- }
-
- // Increase the counter value
- for(int i = 0; i < cipher->cipher->block_size; i++)
- if(++cipher->counter->counter[i])
- break;
- }
-
- *out++ = *in++ ^ cipher->counter->counter[cipher->counter->n++];
-
- if(cipher->counter->n >= cipher->cipher->block_size)
- cipher->counter->n = 0;
- }
-
- return true;
+bool cipher_set_key_from_rsa(cipher_t *cipher, void *key, size_t len, bool encrypt) {
+ unsigned char *k = (unsigned char *)key + len - EVP_CIPHER_key_length(cipher->cipher);
+ unsigned char *iv = k - EVP_CIPHER_iv_length(cipher->cipher);
+ return cipher_init_ctx(cipher, encrypt, k, iv);