Re: [FFmpeg-devel] libavutil: Added twofish block cipher
On Wed, Jan 28, 2015 at 04:06:19PM +0100, Giorgio Vazzana wrote: 2015-01-27 22:01 GMT+01:00 supraja reddy supraja0...@gmail.com: Hello, +for (i = 0; i cs-ksize; i++) { +Me[i] = Key[2 * i]; +Mo[i] = Key[2 * i + 1]; +} +for (i = 0; i cs-ksize; i++) +cs-S[cs-ksize - i - 1] = tf_RS(Me[i], Mo[i]); Can't the previous two for loops be merged together? You wanted me to divide the two sets earlier so I had split them up. I have merged them now anyways. OK :) , at least you're using only one variable for the loop (i) now and not two (i, j). +/** + * Initialize an AVTWOFISH context. + * + * @param ctx an AVTWOFISH context + * @param key a key of size ranging from 1 to 32 bytes used for encryption/decryption + * @param key_bits number of keybits: 128, 192, 256 If less than the required, padded with zeroes to nearest valid value + */ You need to document the return values of this function. I hope the documentation is proper and as requested. I meant document the return value using @return, but we can fix it after we apply the patch. The rest LGTM, thanks. applied thanks [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB No human being will ever know the Truth, for even if they happen to say it by chance, they would not even known they had done so. -- Xenophanes signature.asc Description: Digital signature ___ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Re: [FFmpeg-devel] libavutil: Added twofish block cipher
Hello, +for (i = 0; i cs-ksize; i++) { +Me[i] = Key[2 * i]; +Mo[i] = Key[2 * i + 1]; +} +for (i = 0; i cs-ksize; i++) +cs-S[cs-ksize - i - 1] = tf_RS(Me[i], Mo[i]); Can't the previous two for loops be merged together? You wanted me to divide the two sets earlier so I had split them up. I have merged them now anyways. +/** + * Initialize an AVTWOFISH context. + * + * @param ctx an AVTWOFISH context + * @param key a key of size ranging from 1 to 32 bytes used for encryption/decryption + * @param key_bits number of keybits: 128, 192, 256 If less than the required, padded with zeroes to nearest valid value + */ You need to document the return values of this function. I hope the documentation is proper and as requested. I have updated the patch. Please let me know of any further changes. Thanks, Supraja From 85e77341645771a222621586c7f9dc2e56ba64ae Mon Sep 17 00:00:00 2001 From: Supraja Meedinti supraja0...@gmail.com Date: Mon, 26 Jan 2015 00:09:20 +0530 Subject: [PATCH] libavutil: Added twofish symmetric block cipher Signed-off-by: Supraja Meedinti supraja0...@gmail.com --- Changelog | 1 + libavutil/Makefile | 3 + libavutil/twofish.c | 375 libavutil/twofish.h | 70 ++ 4 files changed, 449 insertions(+) create mode 100644 libavutil/twofish.c create mode 100644 libavutil/twofish.h diff --git a/Changelog b/Changelog index 4575e8b..e68bd5d 100644 --- a/Changelog +++ b/Changelog @@ -11,6 +11,7 @@ version next: - tblend filter - cropdetect support for non 8bpp, absolute (if limit = 1) and relative (if limit 1.0) threshold - Camellia symmetric block cipher +- Twofish symmetric block cipher version 2.5: diff --git a/libavutil/Makefile b/libavutil/Makefile index 4db89b8..6caf896 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -60,6 +60,7 @@ HEADERS = adler32.h \ time.h\ timecode.h\ timestamp.h \ + twofish.h \ version.h \ xtea.h\ @@ -129,6 +130,7 @@ OBJS = adler32.o\ time.o \ timecode.o \ tree.o \ + twofish.o\ utils.o \ xga_font_data.o \ xtea.o \ @@ -184,6 +186,7 @@ TESTPROGS = adler32 \ sha512 \ softfloat \ tree\ +twofish \ utf8\ xtea\ diff --git a/libavutil/twofish.c b/libavutil/twofish.c new file mode 100644 index 000..337c099 --- /dev/null +++ b/libavutil/twofish.c @@ -0,0 +1,375 @@ +/* + * An implementation of the TwoFish algorithm + * Copyright (c) 2015 Supraja Meedinti + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include twofish.h +#include common.h +#include intreadwrite.h +#include attributes.h + +#define LR(x, n) ((x) (n) | (x) (32 - (n))) +#define RR(x, n) ((x) (n) | (x) (32 - (n))) + +typedef struct AVTWOFISH { +uint32_t K[40]; +uint32_t S[4]; +int ksize; +} AVTWOFISH; + +static const uint8_t MD1[256] = { +
Re: [FFmpeg-devel] libavutil: Added twofish block cipher
On 25 January 2015 20:00:07 CET, supraja reddy supraja0...@gmail.com wrote: Sorry. I forgot to make a small change. Updated the patch. Not specifically related to this patch, but with this many implementations I think it is high time we warn about the security implications of using them. My original implementation was only for decrypting of (pseudo)DRM video which is not an application I consider to have actual security requirements, but I think the risk now is high people use it for the wrong things. In particular, I think we need to state that our implementations have no protection against sidechannel attacks (and no intention to ever add them IMHO), so everyone using them should assume that in particular everyone having access to the same CPU can figure out the key. ___ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Re: [FFmpeg-devel] libavutil: Added twofish block cipher
Hello, I have made all the changes as suggested. If the number of if-else loops in init() seem to be too many, please let me know I will change it but I have put them to handle the return values and overflow issues. If there are any other changes, please let me know. Thanks, Supraja On Sun, Jan 25, 2015 at 2:22 AM, Giorgio Vazzana mywin...@gmail.com wrote: Hello, thanks for the new patch. As I said the code looks quite good, here's what I spotted in my review: From b46d6a457aeee319fc6e56217a265c9881a34c2c Mon Sep 17 00:00:00 2001 From: Supraja Meedinti supraja0...@gmail.com Date: Thu, 15 Jan 2015 21:35:16 +0530 Subject: [PATCH] libavutil: Added Twofish block cipher Signed-off-by: Supraja Meedinti supraja0...@gmail.com --- libavutil/Makefile | 3 + libavutil/twofish.c | 373 libavutil/twofish.h | 70 ++ Missing changelog entry. 3 files changed, 446 insertions(+) create mode 100644 libavutil/twofish.c create mode 100644 libavutil/twofish.h diff --git a/libavutil/Makefile b/libavutil/Makefile index 4db89b8..6caf896 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -60,6 +60,7 @@ HEADERS = adler32.h \ time.h \ timecode.h \ timestamp.h \ + twofish.h \ version.h \ xtea.h \ @@ -129,6 +130,7 @@ OBJS = adler32.o \ time.o \ timecode.o \ tree.o \ + twofish.o \ utils.o \ xga_font_data.o \ xtea.o \ @@ -184,6 +186,7 @@ TESTPROGS = adler32 \ sha512 \ softfloat \ tree \ +twofish \ utf8 \ xtea \ diff --git a/libavutil/twofish.c b/libavutil/twofish.c new file mode 100644 index 000..b57a48c --- /dev/null +++ b/libavutil/twofish.c @@ -0,0 +1,373 @@ +/* + * An implementation of the TwoFish algorithm + * Copyright (c) 2015 Supraja Meedinti + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include twofish.h +#include common.h +#include intreadwrite.h +#include attributes.h + +#define LR(x, n) ((x) (n) | (x) (32 - (n))) +#define RR(x, n) ((x) (n) | (x) (32 - (n))) +#define R4(x) ((x) 1 | (x) 3) This macro is never used. +#define sk_inc 0x02020202 +#define sk_nex 0x01010101 I believe these are not needed, see below. + +typedef struct AVTWOFISH { +uint32_t K[40]; +uint32_t S[4]; +int ksize; +} AVTWOFISH; + +static const uint8_t MD1[256] = { +0x00, 0x5b, 0xb6, 0xed, 0x05, 0x5e, 0xb3, 0xe8, 0x0a, 0x51, 0xbc, 0xe7, 0x0f, 0x54, 0xb9, 0xe2, +0x14, 0x4f, 0xa2, 0xf9, 0x11, 0x4a, 0xa7, 0xfc, 0x1e, 0x45, 0xa8, 0xf3, 0x1b, 0x40, 0xad, 0xf6, +0x28, 0x73, 0x9e, 0xc5, 0x2d, 0x76, 0x9b, 0xc0, 0x22, 0x79, 0x94, 0xcf, 0x27, 0x7c, 0x91, 0xca, +0x3c, 0x67, 0x8a, 0xd1, 0x39, 0x62, 0x8f, 0xd4, 0x36, 0x6d, 0x80, 0xdb, 0x33, 0x68, 0x85, 0xde, +0x50, 0x0b, 0xe6, 0xbd, 0x55, 0x0e, 0xe3, 0xb8, 0x5a, 0x01, 0xec, 0xb7, 0x5f, 0x04, 0xe9, 0xb2, +0x44, 0x1f, 0xf2, 0xa9, 0x41, 0x1a, 0xf7, 0xac, 0x4e, 0x15, 0xf8, 0xa3, 0x4b, 0x10, 0xfd, 0xa6, +0x78, 0x23, 0xce, 0x95, 0x7d, 0x26, 0xcb, 0x90, 0x72, 0x29, 0xc4, 0x9f, 0x77, 0x2c, 0xc1, 0x9a, +0x6c, 0x37, 0xda, 0x81, 0x69, 0x32, 0xdf, 0x84, 0x66, 0x3d, 0xd0, 0x8b, 0x63, 0x38, 0xd5, 0x8e, +0xa0, 0xfb, 0x16, 0x4d, 0xa5, 0xfe, 0x13, 0x48, 0xaa, 0xf1, 0x1c, 0x47, 0xaf, 0xf4, 0x19, 0x42, +0xb4, 0xef, 0x02, 0x59, 0xb1, 0xea, 0x07, 0x5c, 0xbe, 0xe5, 0x08, 0x53, 0xbb, 0xe0, 0x0d, 0x56, +0x88, 0xd3, 0x3e, 0x65, 0x8d, 0xd6, 0x3b, 0x60, 0x82, 0xd9, 0x34, 0x6f, 0x87, 0xdc, 0x31, 0x6a, +0x9c, 0xc7, 0x2a, 0x71, 0x99, 0xc2, 0x2f, 0x74, 0x96, 0xcd, 0x20, 0x7b, 0x93, 0xc8, 0x25, 0x7e, +0xf0, 0xab, 0x46, 0x1d, 0xf5, 0xae, 0x43, 0x18, 0xfa, 0xa1, 0x4c, 0x17, 0xff, 0xa4, 0x49, 0x12, +0xe4, 0xbf, 0x52, 0x09, 0xe1, 0xba, 0x57, 0x0c, 0xee, 0xb5, 0x58, 0x03, 0xeb, 0xb0, 0x5d, 0x06, +0xd8, 0x83, 0x6e, 0x35, 0xdd, 0x86, 0x6b, 0x30, 0xd2, 0x89, 0x64, 0x3f,
Re: [FFmpeg-devel] libavutil: Added twofish block cipher
Hello, thanks for the new patch. As I said the code looks quite good, here's what I spotted in my review: From b46d6a457aeee319fc6e56217a265c9881a34c2c Mon Sep 17 00:00:00 2001 From: Supraja Meedinti supraja0...@gmail.com Date: Thu, 15 Jan 2015 21:35:16 +0530 Subject: [PATCH] libavutil: Added Twofish block cipher Signed-off-by: Supraja Meedinti supraja0...@gmail.com --- libavutil/Makefile | 3 + libavutil/twofish.c | 373 libavutil/twofish.h | 70 ++ Missing changelog entry. 3 files changed, 446 insertions(+) create mode 100644 libavutil/twofish.c create mode 100644 libavutil/twofish.h diff --git a/libavutil/Makefile b/libavutil/Makefile index 4db89b8..6caf896 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -60,6 +60,7 @@ HEADERS = adler32.h \ time.h\ timecode.h\ timestamp.h \ + twofish.h \ version.h \ xtea.h\ @@ -129,6 +130,7 @@ OBJS = adler32.o \ time.o \ timecode.o \ tree.o \ + twofish.o\ utils.o \ xga_font_data.o \ xtea.o \ @@ -184,6 +186,7 @@ TESTPROGS = adler32 \ sha512 \ softfloat \ tree\ +twofish \ utf8\ xtea\ diff --git a/libavutil/twofish.c b/libavutil/twofish.c new file mode 100644 index 000..b57a48c --- /dev/null +++ b/libavutil/twofish.c @@ -0,0 +1,373 @@ +/* + * An implementation of the TwoFish algorithm + * Copyright (c) 2015 Supraja Meedinti + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include twofish.h +#include common.h +#include intreadwrite.h +#include attributes.h + +#define LR(x, n) ((x) (n) | (x) (32 - (n))) +#define RR(x, n) ((x) (n) | (x) (32 - (n))) +#define R4(x) ((x) 1 | (x) 3) This macro is never used. +#define sk_inc 0x02020202 +#define sk_nex 0x01010101 I believe these are not needed, see below. + +typedef struct AVTWOFISH { +uint32_t K[40]; +uint32_t S[4]; +int ksize; +} AVTWOFISH; + +static const uint8_t MD1[256] = { +0x00, 0x5b, 0xb6, 0xed, 0x05, 0x5e, 0xb3, 0xe8, 0x0a, 0x51, 0xbc, 0xe7, 0x0f, 0x54, 0xb9, 0xe2, +0x14, 0x4f, 0xa2, 0xf9, 0x11, 0x4a, 0xa7, 0xfc, 0x1e, 0x45, 0xa8, 0xf3, 0x1b, 0x40, 0xad, 0xf6, +0x28, 0x73, 0x9e, 0xc5, 0x2d, 0x76, 0x9b, 0xc0, 0x22, 0x79, 0x94, 0xcf, 0x27, 0x7c, 0x91, 0xca, +0x3c, 0x67, 0x8a, 0xd1, 0x39, 0x62, 0x8f, 0xd4, 0x36, 0x6d, 0x80, 0xdb, 0x33, 0x68, 0x85, 0xde, +0x50, 0x0b, 0xe6, 0xbd, 0x55, 0x0e, 0xe3, 0xb8, 0x5a, 0x01, 0xec, 0xb7, 0x5f, 0x04, 0xe9, 0xb2, +0x44, 0x1f, 0xf2, 0xa9, 0x41, 0x1a, 0xf7, 0xac, 0x4e, 0x15, 0xf8, 0xa3, 0x4b, 0x10, 0xfd, 0xa6, +0x78, 0x23, 0xce, 0x95, 0x7d, 0x26, 0xcb, 0x90, 0x72, 0x29, 0xc4, 0x9f, 0x77, 0x2c, 0xc1, 0x9a, +0x6c, 0x37, 0xda, 0x81, 0x69, 0x32, 0xdf, 0x84, 0x66, 0x3d, 0xd0, 0x8b, 0x63, 0x38, 0xd5, 0x8e, +0xa0, 0xfb, 0x16, 0x4d, 0xa5, 0xfe, 0x13, 0x48,
Re: [FFmpeg-devel] libavutil: Added twofish block cipher
Hi On Wed, Jan 21, 2015 at 01:26:18AM +0530, supraja reddy wrote: Hello, Thanks for you reviews. I see this is only use during init but if it still matters speedwise and assuming this is a galois field multiplication the it can be written as EXP_GF256[LOG_GF256[a] + LOG_GF256[b]] (for a!=0 b!=0) Do you want me to insert tables EXP_GF256 and LOG_GF256 in the code or are there any existing tables ? i think there are no existing tables I have updated the patch with other changes suggested. Please let me know if there is anything else to be changed. [...] +static uint32_t tf_h(uint32_t X, uint32_t L[4], int k) +{ +uint8_t y[4], l[4]; +AV_WL32(y, X); +if (k == 4) { +AV_WL32(l, L[3]); +y[0] = q1[y[0]] ^ l[0]; +y[1] = q0[y[1]] ^ l[1]; +y[2] = q0[y[2]] ^ l[2]; +y[3] = q1[y[3]] ^ l[3]; +} +if (k = 3) { +AV_WL32(l, L[2]); +y[0] = q1[y[0]] ^ l[0]; +y[1] = q1[y[1]] ^ l[1]; +y[2] = q0[y[2]] ^ l[2]; +y[3] = q0[y[3]] ^ l[3]; +} +AV_WL32(l, L[1]); +y[0] = q1[q0[q0[y[0]] ^ l[0]] ^ (L[0] 0xff)]; +y[1] = q0[q0[q1[y[1]] ^ l[1]] ^ ((L[0] 8) 0xff)]; +y[2] = q1[q1[q0[y[2]] ^ l[2]] ^ ((L[0] 16) 0xff)]; +y[3] = q0[q1[q1[y[3]] ^ l[3]] ^ (L[0] 24)]; + +l[0] = y[0] ^ MD2[y[1]] ^ MD1[y[2]] ^ MD1[y[3]]; +l[1] = MD1[y[0]] ^ MD2[y[1]] ^ MD2[y[2]] ^ y[3]; +l[2] = MD2[y[0]] ^ MD1[y[1]] ^ y[2] ^ MD2[y[3]]; +l[3] = MD2[y[0]] ^ y[1] ^ MD2[y[2]] ^ MD1[y[3]]; + +return AV_RL32(l); +} I think this can be implemented with a few 8bit-32bit LUTs which are computed during init [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB When you are offended at any man's fault, turn to yourself and study your own failings. Then you will forget your anger. -- Epictetus signature.asc Description: Digital signature ___ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Re: [FFmpeg-devel] libavutil: Added twofish block cipher
On Thu, Jan 15, 2015 at 10:21:22PM +0530, supraja reddy wrote: Hello, I have attached the patch for twofish implementation. Please let me know if there are any changes to be made. Thank you, Supraja [...] +av_cold int av_twofish_init(AVTWOFISH *cs, const uint8_t *Key, int key_bits) +{ +uint32_t key[8], Ke[4], Ko[4], rh, A, B; +uint8_t keypad[32]; +int i, j, k; +k = cs-ksize = ((key_bits =128) ? 128 : ((key_bits =192) ? 192 : 256)) 6; +memset(keypad, 0, sizeof(keypad)); +memcpy(keypad, Key, key_bits 3); key_bits should be checked to be a supported value otherwise the memcpy could write out of the array [...] +#ifdef TEST +#includestdio.h +#includestdlib.h +#includelog.h + +int main(int argc, char *argv[]) +{ +uint8_t Key[32] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff +}; +const uint8_t rct[6][16] = { +{0x9f, 0x58, 0x9f, 0x5c, 0xf6, 0x12, 0x2c, 0x32, 0xb6, 0xbf, 0xec, 0x2f, 0x2a, 0xe8, 0xc3, 0x5a}, +{0xcf, 0xd1, 0xd2, 0xe5, 0xa9, 0xbe, 0x9c, 0xdf, 0x50, 0x1f, 0x13, 0xb8, 0x92, 0xbd, 0x22, 0x48}, +{0x37, 0x52, 0x7b, 0xe0, 0x05, 0x23, 0x34, 0xb8, 0x9f, 0x0c, 0xfc, 0xca, 0xe8, 0x7c, 0xfa, 0x20}, +{0x5d, 0x9d, 0x4e, 0xef, 0xfa, 0x91, 0x51, 0x57, 0x55, 0x24, 0xf1, 0x15, 0x81, 0x5a, 0x12, 0xe0}, +{0xe7, 0x54, 0x49, 0x21, 0x2b, 0xee, 0xf9, 0xf4, 0xa3, 0x90, 0xbd, 0x86, 0x0a, 0x64, 0x09, 0x41}, +{0x37, 0xfe, 0x26, 0xff, 0x1c, 0xf6, 0x61, 0x75, 0xf5, 0xdd, 0xf4, 0xc3, 0x3b, 0x97, 0xa2, 0x05} +}; +uint8_t temp[32], iv[16], rpt[32]; +const int kbits[3] = {128, 192, 256}; +int i, j, err = 0; +AVTWOFISH *cs; +cs = av_twofish_alloc(); +if (!cs) +return 1; +memset(rpt, 0, sizeof(rpt)); could be avoided with: uint8_t rpt[32] = {0}; [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB There will always be a question for which you do not know the correct answer. signature.asc Description: Digital signature ___ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Re: [FFmpeg-devel] libavutil: Added twofish block cipher
Hello, Thanks for you reviews. I see this is only use during init but if it still matters speedwise and assuming this is a galois field multiplication the it can be written as EXP_GF256[LOG_GF256[a] + LOG_GF256[b]] (for a!=0 b!=0) Do you want me to insert tables EXP_GF256 and LOG_GF256 in the code or are there any existing tables ? I have updated the patch with other changes suggested. Please let me know if there is anything else to be changed. Thanks, Supraja On Tue, Jan 20, 2015 at 8:45 PM, Michael Niedermayer michae...@gmx.at wrote: On Thu, Jan 15, 2015 at 10:21:22PM +0530, supraja reddy wrote: Hello, I have attached the patch for twofish implementation. Please let me know if there are any changes to be made. Thank you, Supraja [...] +av_cold int av_twofish_init(AVTWOFISH *cs, const uint8_t *Key, int key_bits) +{ +uint32_t key[8], Ke[4], Ko[4], rh, A, B; +uint8_t keypad[32]; +int i, j, k; +k = cs-ksize = ((key_bits =128) ? 128 : ((key_bits =192) ? 192 : 256)) 6; +memset(keypad, 0, sizeof(keypad)); +memcpy(keypad, Key, key_bits 3); key_bits should be checked to be a supported value otherwise the memcpy could write out of the array [...] +#ifdef TEST +#includestdio.h +#includestdlib.h +#includelog.h + +int main(int argc, char *argv[]) +{ +uint8_t Key[32] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff +}; +const uint8_t rct[6][16] = { +{0x9f, 0x58, 0x9f, 0x5c, 0xf6, 0x12, 0x2c, 0x32, 0xb6, 0xbf, 0xec, 0x2f, 0x2a, 0xe8, 0xc3, 0x5a}, +{0xcf, 0xd1, 0xd2, 0xe5, 0xa9, 0xbe, 0x9c, 0xdf, 0x50, 0x1f, 0x13, 0xb8, 0x92, 0xbd, 0x22, 0x48}, +{0x37, 0x52, 0x7b, 0xe0, 0x05, 0x23, 0x34, 0xb8, 0x9f, 0x0c, 0xfc, 0xca, 0xe8, 0x7c, 0xfa, 0x20}, +{0x5d, 0x9d, 0x4e, 0xef, 0xfa, 0x91, 0x51, 0x57, 0x55, 0x24, 0xf1, 0x15, 0x81, 0x5a, 0x12, 0xe0}, +{0xe7, 0x54, 0x49, 0x21, 0x2b, 0xee, 0xf9, 0xf4, 0xa3, 0x90, 0xbd, 0x86, 0x0a, 0x64, 0x09, 0x41}, +{0x37, 0xfe, 0x26, 0xff, 0x1c, 0xf6, 0x61, 0x75, 0xf5, 0xdd, 0xf4, 0xc3, 0x3b, 0x97, 0xa2, 0x05} +}; +uint8_t temp[32], iv[16], rpt[32]; +const int kbits[3] = {128, 192, 256}; +int i, j, err = 0; +AVTWOFISH *cs; +cs = av_twofish_alloc(); +if (!cs) +return 1; +memset(rpt, 0, sizeof(rpt)); could be avoided with: uint8_t rpt[32] = {0}; [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB There will always be a question for which you do not know the correct answer. ___ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel From b46d6a457aeee319fc6e56217a265c9881a34c2c Mon Sep 17 00:00:00 2001 From: Supraja Meedinti supraja0...@gmail.com Date: Thu, 15 Jan 2015 21:35:16 +0530 Subject: [PATCH] libavutil: Added Twofish block cipher Signed-off-by: Supraja Meedinti supraja0...@gmail.com --- libavutil/Makefile | 3 + libavutil/twofish.c | 373 libavutil/twofish.h | 70 ++ 3 files changed, 446 insertions(+) create mode 100644 libavutil/twofish.c create mode 100644 libavutil/twofish.h diff --git a/libavutil/Makefile b/libavutil/Makefile index 4db89b8..6caf896 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -60,6 +60,7 @@ HEADERS = adler32.h \ time.h\ timecode.h\ timestamp.h \ + twofish.h \ version.h \ xtea.h\ @@ -129,6 +130,7 @@ OBJS = adler32.o\ time.o \ timecode.o \ tree.o \ + twofish.o\ utils.o \ xga_font_data.o \ xtea.o \ @@ -184,6 +186,7 @@ TESTPROGS = adler32 \ sha512 \ softfloat \ tree
[FFmpeg-devel] libavutil: Added twofish block cipher
Hello, I have attached the patch for twofish implementation. Please let me know if there are any changes to be made. Thank you, Supraja From f86554850a8b0be2e32fcd341ca297c7ce73a941 Mon Sep 17 00:00:00 2001 From: Supraja Meedinti supraja0...@gmail.com Date: Thu, 15 Jan 2015 21:35:16 +0530 Subject: [PATCH] libavutil: Added Twofish block cipher Signed-off-by: Supraja Meedinti supraja0...@gmail.com --- libavutil/Makefile | 3 + libavutil/twofish.c | 351 libavutil/twofish.h | 70 +++ 3 files changed, 424 insertions(+) create mode 100644 libavutil/twofish.c create mode 100644 libavutil/twofish.h diff --git a/libavutil/Makefile b/libavutil/Makefile index 4db89b8..6caf896 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -60,6 +60,7 @@ HEADERS = adler32.h \ time.h\ timecode.h\ timestamp.h \ + twofish.h \ version.h \ xtea.h\ @@ -129,6 +130,7 @@ OBJS = adler32.o\ time.o \ timecode.o \ tree.o \ + twofish.o\ utils.o \ xga_font_data.o \ xtea.o \ @@ -184,6 +186,7 @@ TESTPROGS = adler32 \ sha512 \ softfloat \ tree\ +twofish \ utf8\ xtea\ diff --git a/libavutil/twofish.c b/libavutil/twofish.c new file mode 100644 index 000..a66677b --- /dev/null +++ b/libavutil/twofish.c @@ -0,0 +1,351 @@ +/* + * An implementation of the TwoFish algorithm as mentioned in RFC3713 + * Copyright (c) 2015 Supraja Meedinti + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include twofish.h +#include common.h +#include intreadwrite.h +#include attributes.h + +#define LR(x, n) ((x) (n) | (x) (32 - (n))) +#define RR(x, n) ((x) (n) | (x) (32 - (n))) +#define R4(x) ((x) 1 | (x) 3) +#define sk_inc 0x02020202 +#define sk_nex 0x01010101 + +typedef struct AVTWOFISH { +uint32_t K[40]; +uint32_t S[4]; +int ksize; +} AVTWOFISH; + +static const uint8_t MD1[256] = { +0x00, 0x5b, 0xb6, 0xed, 0x05, 0x5e, 0xb3, 0xe8, 0x0a, 0x51, 0xbc, 0xe7, 0x0f, 0x54, 0xb9, 0xe2, +0x14, 0x4f, 0xa2, 0xf9, 0x11, 0x4a, 0xa7, 0xfc, 0x1e, 0x45, 0xa8, 0xf3, 0x1b, 0x40, 0xad, 0xf6, +0x28, 0x73, 0x9e, 0xc5, 0x2d, 0x76, 0x9b, 0xc0, 0x22, 0x79, 0x94, 0xcf, 0x27, 0x7c, 0x91, 0xca, +0x3c, 0x67, 0x8a, 0xd1, 0x39, 0x62, 0x8f, 0xd4, 0x36, 0x6d, 0x80, 0xdb, 0x33, 0x68, 0x85, 0xde, +0x50, 0x0b, 0xe6, 0xbd, 0x55, 0x0e, 0xe3, 0xb8, 0x5a, 0x01, 0xec, 0xb7, 0x5f, 0x04, 0xe9, 0xb2, +0x44, 0x1f, 0xf2, 0xa9, 0x41, 0x1a, 0xf7, 0xac, 0x4e, 0x15, 0xf8, 0xa3, 0x4b, 0x10, 0xfd, 0xa6, +0x78, 0x23, 0xce, 0x95, 0x7d, 0x26, 0xcb, 0x90, 0x72, 0x29, 0xc4, 0x9f, 0x77, 0x2c, 0xc1, 0x9a, +0x6c, 0x37, 0xda, 0x81, 0x69, 0x32, 0xdf, 0x84, 0x66, 0x3d, 0xd0, 0x8b, 0x63, 0x38, 0xd5, 0x8e, +0xa0, 0xfb, 0x16, 0x4d, 0xa5, 0xfe, 0x13, 0x48, 0xaa, 0xf1, 0x1c, 0x47, 0xaf, 0xf4, 0x19, 0x42, +0xb4, 0xef, 0x02, 0x59, 0xb1, 0xea, 0x07, 0x5c, 0xbe, 0xe5, 0x08, 0x53, 0xbb, 0xe0, 0x0d, 0x56, +0x88,
Re: [FFmpeg-devel] libavutil: Added twofish block cipher
On Thu, Jan 15, 2015 at 10:21:22PM +0530, supraja reddy wrote: Hello, I have attached the patch for twofish implementation. Please let me know if there are any changes to be made. Thank you, Supraja Makefile |3 twofish.c | 351 ++ twofish.h | 70 3 files changed, 424 insertions(+) b2382c15f440967745b06f0243a023647c5c78eb tf.patch From f86554850a8b0be2e32fcd341ca297c7ce73a941 Mon Sep 17 00:00:00 2001 From: Supraja Meedinti supraja0...@gmail.com Date: Thu, 15 Jan 2015 21:35:16 +0530 Subject: [PATCH] libavutil: Added Twofish block cipher Signed-off-by: Supraja Meedinti supraja0...@gmail.com --- libavutil/Makefile | 3 + libavutil/twofish.c | 351 libavutil/twofish.h | 70 +++ 3 files changed, 424 insertions(+) create mode 100644 libavutil/twofish.c create mode 100644 libavutil/twofish.h diff --git a/libavutil/Makefile b/libavutil/Makefile index 4db89b8..6caf896 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -60,6 +60,7 @@ HEADERS = adler32.h \ time.h\ timecode.h\ timestamp.h \ + twofish.h \ version.h \ xtea.h\ @@ -129,6 +130,7 @@ OBJS = adler32.o \ time.o \ timecode.o \ tree.o \ + twofish.o\ utils.o \ xga_font_data.o \ xtea.o \ @@ -184,6 +186,7 @@ TESTPROGS = adler32 \ sha512 \ softfloat \ tree\ +twofish \ utf8\ xtea\ diff --git a/libavutil/twofish.c b/libavutil/twofish.c new file mode 100644 index 000..a66677b --- /dev/null +++ b/libavutil/twofish.c @@ -0,0 +1,351 @@ +/* + * An implementation of the TwoFish algorithm as mentioned in RFC3713 + * Copyright (c) 2015 Supraja Meedinti + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include twofish.h +#include common.h +#include intreadwrite.h +#include attributes.h + +#define LR(x, n) ((x) (n) | (x) (32 - (n))) +#define RR(x, n) ((x) (n) | (x) (32 - (n))) +#define R4(x) ((x) 1 | (x) 3) +#define sk_inc 0x02020202 +#define sk_nex 0x01010101 + +typedef struct AVTWOFISH { +uint32_t K[40]; +uint32_t S[4]; +int ksize; +} AVTWOFISH; + +static const uint8_t MD1[256] = { +0x00, 0x5b, 0xb6, 0xed, 0x05, 0x5e, 0xb3, 0xe8, 0x0a, 0x51, 0xbc, 0xe7, 0x0f, 0x54, 0xb9, 0xe2, +0x14, 0x4f, 0xa2, 0xf9, 0x11, 0x4a, 0xa7, 0xfc, 0x1e, 0x45, 0xa8, 0xf3, 0x1b, 0x40, 0xad, 0xf6, +0x28, 0x73, 0x9e, 0xc5, 0x2d, 0x76, 0x9b, 0xc0, 0x22, 0x79, 0x94, 0xcf, 0x27, 0x7c, 0x91, 0xca, +0x3c, 0x67, 0x8a, 0xd1, 0x39, 0x62, 0x8f, 0xd4, 0x36, 0x6d, 0x80, 0xdb, 0x33, 0x68, 0x85, 0xde, +0x50, 0x0b, 0xe6, 0xbd, 0x55, 0x0e, 0xe3, 0xb8, 0x5a, 0x01, 0xec, 0xb7, 0x5f, 0x04, 0xe9, 0xb2, +0x44, 0x1f, 0xf2, 0xa9, 0x41, 0x1a, 0xf7, 0xac, 0x4e, 0x15, 0xf8, 0xa3, 0x4b, 0x10, 0xfd, 0xa6, +