Hi ,

Attached patch adds cbc mode to the existing code.

Supraja
From 3255d0bbaf31bf3c39b0e22882e52b34882f37d4 Mon Sep 17 00:00:00 2001
From: Supraja Meedinti <supraja0...@gmail.com>
Date: Wed, 17 Dec 2014 10:16:43 +0530
Subject: [PATCH] libavutil: Added cbc mode to cast128

Signed-off-by: Supraja Meedinti <supraja0...@gmail.com>
---
 Changelog         |  2 +-
 libavutil/cast5.c | 45 ++++++++++++++++++++++++++++++++++-----------
 libavutil/cast5.h |  2 +-
 3 files changed, 36 insertions(+), 13 deletions(-)

diff --git a/Changelog b/Changelog
index 7097a12..59129c0 100644
--- a/Changelog
+++ b/Changelog
@@ -11,7 +11,7 @@ version 2.5:
 - HEVC/H.265 RTP payload format (draft v6) packetizer
 - SUP/PGS subtitle demuxer
 - ffprobe -show_pixel_formats option
-- CAST128 symmetric block cipher, ECB mode
+- CAST128 symmetric block cipher
 - STL subtitle demuxer and decoder
 - libutvideo YUV 4:2:2 10bit support
 - XCB-based screen-grabber
diff --git a/libavutil/cast5.c b/libavutil/cast5.c
index 14dd701..3594e6b 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,10 @@ 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);
+    }
     AV_WB32(dst, r);
     AV_WB32(dst + 4, l);
 }
@@ -468,14 +472,21 @@ av_cold int av_cast5_init(AVCAST5* cs, const uint8_t *key, int key_bits)
     return 0;
 }
 
-void av_cast5_crypt(AVCAST5* cs, uint8_t* dst, const uint8_t* src, int count, int decrypt)
+void av_cast5_crypt(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);
+            decipher(cs, dst, src, iv);
+        } else {
+        if (iv) {
+            for (i = 0; i < 8; i++)
+                dst[i] = src[i] ^ iv[i];
+            encipher(cs, dst, dst);
         } else {
             encipher(cs, dst, src);
         }
+    }
         src = src + 8;
         dst = dst + 8;
     }
@@ -512,18 +523,18 @@ int main(int argc, char** argv)
     cs = av_cast5_alloc();
     if (!cs)
         return 1;
-    for (j = 0; j < 3; j++){
+    for (j = 0; j < 3; j++) {
 
         av_cast5_init(cs, Key[j], key_bits[j]);
-        av_cast5_crypt(cs, temp, rpt, 1, 0);
-        for (i = 0;i < 8; i++){
-            if (rct[j][i] != temp[i]){
+        av_cast5_crypt(cs, temp, rpt, 1, NULL, 0);
+        for (i = 0;i < 8; i++) {
+            if (rct[j][i] != temp[i]) {
                 av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rct[j][i], temp[i]);
                 err = 1;
             }
         }
 
-        av_cast5_crypt(cs, temp, rct[j], 1, 1);
+        av_cast5_crypt(cs, temp, rct[j], 1, NULL, 1);
         for (i =0; i < 8; i++) {
             if (rpt[i] != temp[i]) {
                 av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rpt[i], temp[i]);
@@ -533,11 +544,11 @@ int main(int argc, char** argv)
     }
     memcpy(rpt2[0], Key[0], 16);
     memcpy(rpt2[1], Key[0], 16);
-    for (i = 0; i < 1000000; i++){
+    for (i = 0; i < 1000000; i++) {
         av_cast5_init(cs, rpt2[1], 128);
-        av_cast5_crypt(cs, rpt2[0], rpt2[0], 2, 0);
+        av_cast5_crypt(cs, rpt2[0], rpt2[0], 2, NULL, 0);
         av_cast5_init(cs, rpt2[0], 128);
-        av_cast5_crypt(cs, rpt2[1], rpt2[1], 2, 0);
+        av_cast5_crypt(cs, rpt2[1], rpt2[1], 2, NULL, 0);
     }
     for (j = 0; j < 2; j++) {
         for (i = 0; i < 16; i++) {
@@ -547,6 +558,18 @@ int main(int argc, char** argv)
             }
         }
     }
+    for (j = 0; j < 3; j++) {
+
+        av_cast5_init(cs, Key[j], key_bits[j]);
+        av_cast5_crypt(cs, temp, rpt, 1, rct2[0], 0);
+        av_cast5_crypt(cs, temp, temp, 1, rct2[0], 1);
+        for (i =0; i < 8; i++) {
+            if (rpt[i] != temp[i]) {
+                av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rpt[i], temp[i]);
+                err = 1;
+            }
+        }
+    }
     av_free(cs);
     return err;
 }
diff --git a/libavutil/cast5.h b/libavutil/cast5.h
index 913d048..ce758f4 100644
--- a/libavutil/cast5.h
+++ b/libavutil/cast5.h
@@ -60,7 +60,7 @@ int av_cast5_init(struct AVCAST5 *ctx, const uint8_t *key, int key_bits);
   * @param count number of 8 byte blocks
   * @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);
+void av_cast5_crypt(struct AVCAST5 *ctx, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt);
 /**
  * @}
  */
-- 
1.8.3.2

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

Reply via email to