/**
* are we using counter mode?
*/
- bool ctr_mode;
+ int mode;
/**
* counter state
*/
static bool set_iv(private_gcrypt_crypter_t *this, chunk_t iv)
{
- if (this->ctr_mode)
+ if (this->mode == GCRY_CIPHER_MODE_CTR)
{
memcpy(this->ctr.iv, iv.ptr, sizeof(this->ctr.iv));
this->ctr.counter = htonl(1);
return gcry_cipher_setctr(this->h, &this->ctr, sizeof(this->ctr)) == 0;
}
- return gcry_cipher_setiv(this->h, iv.ptr, iv.len) == 0;
+ if (iv.len)
+ {
+ return gcry_cipher_setiv(this->h, iv.ptr, iv.len) == 0;
+ }
+ return TRUE;
}
METHOD(crypter_t, decrypt, bool,
{
size_t len = 0;
- if (this->ctr_mode)
+ if (this->mode == GCRY_CIPHER_MODE_CTR)
{ /* counter mode does not need any padding */
return 1;
}
{
size_t len = 0;
- if (this->ctr_mode)
+ switch (this->mode)
{
- return sizeof(this->ctr.iv);
+ case GCRY_CIPHER_MODE_CTR:
+ return sizeof(this->ctr.iv);
+ case GCRY_CIPHER_MODE_ECB:
+ return 0;
+ default:
+ break;
}
gcry_cipher_algo_info(this->alg, GCRYCTL_GET_BLKLEN, NULL, &len);
return len;
size_t len = 0;
gcry_cipher_algo_info(this->alg, GCRYCTL_GET_KEYLEN, NULL, &len);
- if (this->ctr_mode)
+ if (this->mode == GCRY_CIPHER_MODE_CTR)
{
return len + sizeof(this->ctr.nonce);
}
METHOD(crypter_t, set_key, bool,
private_gcrypt_crypter_t *this, chunk_t key)
{
- if (this->ctr_mode)
+ if (this->mode == GCRY_CIPHER_MODE_CTR)
{
/* last 4 bytes are the nonce */
memcpy(this->ctr.nonce, key.ptr + key.len - sizeof(this->ctr.nonce),
},
},
.alg = gcrypt_alg,
- .ctr_mode = mode == GCRY_CIPHER_MODE_CTR,
+ .mode = mode,
);
err = gcry_cipher_open(&this->h, gcrypt_alg, mode, 0);