[FFmpeg-cvslog] libavutil: Added cbc mode to cast128

Supraja Meedinti git at videolan.org
Fri Dec 19 14:47:27 CET 2014


ffmpeg | branch: master | Supraja Meedinti <supraja0493 at gmail.com> | Thu Dec 18 15:44:32 2014 +0530| [4e1ecfe21476ee2631525a7d6dc2a0bc8151d0a9] | committer: Michael Niedermayer

libavutil: Added cbc mode to cast128

Signed-off-by: Supraja Meedinti <supraja0493 at gmail.com>
Previous version reviewed-by: Giorgio Vazzana <mywing81 at gmail.com>
Signed-off-by: Michael Niedermayer <michaelni at gmx.at>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=4e1ecfe21476ee2631525a7d6dc2a0bc8151d0a9
---

 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..6f58382 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}
     };
+    const 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 +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, iv, 8);
+        av_cast5_crypt2(cs, rpt2[0], rct2[0], 2, temp, 0);
+        memcpy(temp, iv, 8);
+        av_cast5_crypt2(cs, rpt2[0], rpt2[0], 2, temp, 1);
+        for (i = 0; i < 16; i++) {
+            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);
 /**
  * @}
  */



More information about the ffmpeg-cvslog mailing list