[FFmpeg-devel] libavutil: Added cbc mode to cast5.c
Giorgio Vazzana
mywing81 at gmail.com
Thu Dec 18 22:31:03 CET 2014
2014-12-18 11:09 GMT+01:00 supraja reddy <supraja0493 at gmail.com>:
> Sorry for the mistake . I seem to have got cbc completely wrong.
> I have made the changes . Please let me know if there is anything missing.
> From 0bf794bc43ed7055f83bcd07cb5c1e2f4445e813 Mon Sep 17 00:00:00 2001
> From: Supraja Meedinti <supraja0493 at gmail.com>
> Date: Thu, 18 Dec 2014 15:26:32 +0530
> Subject: [PATCH] libavutil: Added cbc mode to cast128
>
> Signed-off-by: Supraja Meedinti <supraja0493 at gmail.com>
> ---
> libavutil/cast5.c | 44 ++++++++++++++++++++++++++++++++++++++++++--
> libavutil/cast5.h | 14 +++++++++++++-
> 2 files changed, 55 insertions(+), 3 deletions(-)
>
> diff --git a/libavutil/cast5.c b/libavutil/cast5.c
> index 14dd701..e338037 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);
> + }
> AV_WB32(dst, r);
> AV_WB32(dst + 4, l);
> }
> @@ -468,11 +473,31 @@ 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) {
> + decipher(cs, dst, src, iv);
> + } else {
> + if (iv) {
> + for (i = 0; i < 8; i++)
> + dst[i] = src[i] ^ iv[i];
> + encipher(cs, dst, dst);
> + memcpy(iv, dst, 8);
> + } 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 +529,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}
> };
> + static uint8_t temp2[8] = {0xee, 0xa9, 0xd0, 0xa2, 0x49, 0xfd, 0x3b, 0xa6};
I would call this variable iv instead of temp2, so: const uint8_t iv[2] = ...
(for another patch: we can probably remove the keyword 'static' from
all variables in main, and make key_bits const)
> static uint8_t rpt2[2][16];
> int i, j, err = 0;
> static int key_bits[3] = {128, 80, 40};
> @@ -547,6 +573,20 @@ int main(int argc, char** argv)
> }
> }
> }
> + for (j = 0; j < 3; j++) {
> +
> + av_cast5_init(cs, Key[j], key_bits[j]);
> + memcpy(temp, temp2, 8);
> + av_cast5_crypt2(cs, rpt2[0], rct2[0], 2, temp, 0);
> + memcpy(temp, temp2, 8);
> + av_cast5_crypt2(cs, rpt2[0], rpt2[0], 2, temp, 1);
> + for (i =0; i < 16; i++) {
Missing space between = and 0.
> + if (rct2[0][i] != rpt2[0][i]) {
> + av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rct2[0][i], rpt2[0][i]);
> + err = 1;
> + }
> + }
> + }
> av_free(cs);
> return err;
> }
> diff --git a/libavutil/cast5.h b/libavutil/cast5.h
> index 913d048..e5cc8b1 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
>
the rest LGTM, thanks.
Giorgio Vazzana
More information about the ffmpeg-devel
mailing list