[FFmpeg-devel] libavutil: Added cbc mode to cast5.c
Giorgio Vazzana
mywing81 at gmail.com
Thu Dec 18 10:27:52 CET 2014
Hello,
2014-12-17 19:53 GMT+01:00 supraja reddy <supraja0493 at gmail.com>:
> I hope this fixes all the issues .
> From 16c8db86ebf92f68a8a750dcbe5e3598a8bd69b5 Mon Sep 17 00:00:00 2001
> From: Supraja Meedinti <supraja0493 at gmail.com>
> Date: Thu, 18 Dec 2014 00:17:45 +0530
> Subject: [PATCH] libavutil: Added cbc mode to cast128
>
> Signed-off-by: Supraja Meedinti <supraja0493 at gmail.com>
> ---
> libavutil/cast5.c | 40 ++++++++++++++++++++++++++++++++++++++--
> libavutil/cast5.h | 14 +++++++++++++-
> 2 files changed, 51 insertions(+), 3 deletions(-)
>
> diff --git a/libavutil/cast5.c b/libavutil/cast5.c
> index 14dd701..dba5b6a 100644
> --- a/libavutil/cast5.c
> +++ b/libavutil/cast5.c
> @@ -416,7 +416,7 @@ static void encipher(AVCAST5* cs, uint8_t* dst, const uint8_t* src)
> AV_WB32(dst + 4, l);
> }
>
> -static void decipher(AVCAST5* cs, uint8_t* dst, const uint8_t* src)
> +static void decipher(AVCAST5* cs, uint8_t* dst, const uint8_t* src, uint8_t *iv)
> {
> uint32_t f, I, r, l;
> l = AV_RB32(src);
> @@ -439,6 +439,11 @@ static void decipher(AVCAST5* cs, uint8_t* dst, const uint8_t* src)
> F3(r, l, 3);
> F2(l, r, 2);
> F1(r, l, 1);
> + if (iv) {
> + r ^= AV_RB32(iv);
> + l ^= AV_RB32(iv + 4);
> + memcpy(iv, src, 8);
Indentation is off.
> + }
> AV_WB32(dst, r);
> AV_WB32(dst + 4, l);
> }
> @@ -468,11 +473,30 @@ av_cold int av_cast5_init(AVCAST5* cs, const uint8_t *key, int key_bits)
> return 0;
> }
>
> +void av_cast5_crypt2(AVCAST5* cs, uint8_t* dst, const uint8_t* src, int count, uint8_t *iv, int decrypt)
> +{
> + int i;
> + while (count--) {
> + if (decrypt){
Missing space between ) and {.
> + decipher(cs, dst, src, iv);
> + } else {
The indentation of this else branch is wrong.
> + if (iv) {
> + for (i = 0; i < 8; i++)
> + dst[i] = src[i] ^ iv[i];
> + encipher(cs, dst, dst);
Again, you are not saving the IV for the next block here.
> + } else {
> + encipher(cs, dst, src);
> + }
> + }
> + src = src + 8;
> + dst = dst + 8;
> + }
> +}
> void av_cast5_crypt(AVCAST5* cs, uint8_t* dst, const uint8_t* src, int count, int decrypt)
> {
> while (count--) {
> if (decrypt){
> - decipher(cs, dst, src);
> + decipher(cs, dst, src, NULL);
> } else {
> encipher(cs, dst, src);
> }
> @@ -504,6 +528,7 @@ int main(int argc, char** argv)
> {0xee, 0xa9, 0xd0, 0xa2, 0x49, 0xfd, 0x3b, 0xa6, 0xb3, 0x43, 0x6f, 0xb8, 0x9d, 0x6d, 0xca, 0x92},
> {0xb2, 0xc9, 0x5e, 0xb0, 0x0c, 0x31, 0xad, 0x71, 0x80, 0xac, 0x05, 0xb8, 0xe8, 0x3d, 0x69, 0x6e}
> };
> + uint8_t iv[8] = {0xee, 0xa9, 0xd0, 0xa2, 0x49, 0xfd, 0x3b, 0xa6};
> static uint8_t rpt2[2][16];
> int i, j, err = 0;
> static int key_bits[3] = {128, 80, 40};
> @@ -547,6 +572,17 @@ int main(int argc, char** argv)
> }
> }
> }
> + for (j = 0; j < 3; j++) {
> + av_cast5_init(cs, Key[j], key_bits[j]);
> + av_cast5_crypt2(cs, temp, rpt, 1, iv, 0);
> + av_cast5_crypt2(cs, temp, temp, 1, iv, 1);
> + for (i =0; i < 8; i++) {
Missing space after =.
> + if (rpt[i] != temp[i]) {
> + av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rpt[i], temp[i]);
> + err = 1;
> + }
> + }
> + }
I see at least 2 problems with this for loop:
1) you need to use the same IV to encrypt and decrypt the block. It
seems to work here because you forgot to propagate the IV correctly in
av_cast5_crypt2, otherwise after the encryption the IV would change,
and so you would be using a different IV for the decryption.
2) to correctly test CBC mode we need to work on 2 blocks at least.
> av_free(cs);
> return err;
> }
> diff --git a/libavutil/cast5.h b/libavutil/cast5.h
> index 913d048..4a86743 100644
> --- a/libavutil/cast5.h
> +++ b/libavutil/cast5.h
> @@ -52,7 +52,7 @@ struct AVCAST5 *av_cast5_alloc(void);
> int av_cast5_init(struct AVCAST5 *ctx, const uint8_t *key, int key_bits);
>
> /**
> - * Encrypt or decrypt a buffer using a previously initialized context
> + * Encrypt or decrypt a buffer using a previously initialized context, ECB mode only
> *
> * @param ctx an AVCAST5 context
> * @param dst destination array, can be equal to src
> @@ -61,6 +61,18 @@ int av_cast5_init(struct AVCAST5 *ctx, const uint8_t *key, int key_bits);
> * @param decrypt 0 for encryption, 1 for decryption
> */
> void av_cast5_crypt(struct AVCAST5 *ctx, uint8_t *dst, const uint8_t *src, int count, int decrypt);
> +
> +/**
> + * Encrypt or decrypt a buffer using a previously initialized context
> + *
> + * @param ctx an AVCAST5 context
> + * @param dst destination array, can be equal to src
> + * @param src source array, can be equal to dst
> + * @param count number of 8 byte blocks
> + * @param iv initialization vector for cbc mode, NULL for ecb mode
> + * @param decrypt 0 for encryption, 1 for decryption
> + */
> +void av_cast5_crypt2(struct AVCAST5 *ctx, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt);
> /**
> * @}
> */
> --
> 1.8.3.2
>
More information about the ffmpeg-devel
mailing list