Implement new _libssh2_cipher_* API.
This commit is contained in:
@@ -151,3 +151,65 @@ int _libssh2_dsa_sha1_verify(libssh2_dsa_ctx *dsactx,
|
||||
|
||||
return (rc == 0) ? 0 : -1;
|
||||
}
|
||||
|
||||
int _libssh2_cipher_init (_libssh2_cipher_ctx *h,
|
||||
_libssh2_cipher_type(algo),
|
||||
unsigned char *iv,
|
||||
unsigned char *secret,
|
||||
int encrypt)
|
||||
{
|
||||
int mode = 0, keylen, err;
|
||||
keylen = gcry_cipher_get_algo_keylen (algo);
|
||||
if (algo != GCRY_CIPHER_ARCFOUR)
|
||||
{
|
||||
mode = GCRY_CIPHER_MODE_CBC;
|
||||
}
|
||||
|
||||
err = gcry_cipher_open (h, algo, mode, 0);
|
||||
if (err)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
err = gcry_cipher_setkey (*h, secret, keylen);
|
||||
if (err)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (algo != GCRY_CIPHER_ARCFOUR)
|
||||
{
|
||||
int blklen = gcry_cipher_get_algo_blklen (algo);
|
||||
err = gcry_cipher_setiv (*h, iv, blklen);
|
||||
if (err)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _libssh2_cipher_crypt(_libssh2_cipher_ctx *ctx,
|
||||
_libssh2_cipher_type(algo),
|
||||
int encrypt,
|
||||
unsigned char *block)
|
||||
{
|
||||
size_t blklen = gcry_cipher_get_algo_blklen (algo);
|
||||
int err;
|
||||
if (blklen == 1) {
|
||||
/* Hack for arcfour. */
|
||||
blklen = 8;
|
||||
}
|
||||
|
||||
if (encrypt)
|
||||
{
|
||||
err = gcry_cipher_encrypt (*ctx, block, blklen,
|
||||
block, blklen);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = gcry_cipher_decrypt (*ctx, block, blklen,
|
||||
block, blklen);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user