[FFmpeg-devel] libavutil: Added twofish block cipher

Giorgio Vazzana mywing81 at gmail.com
Mon Jan 19 23:30:11 CET 2015


Hi,

2015-01-15 20:43 GMT+01:00 Michael Niedermayer <michaelni at gmx.at>:
> 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.

the code looks quite nice already, give me some days to properly
review / test it (been very busy lately).
Just a couple of comments:

>> diff --git a/libavutil/twofish.c b/libavutil/twofish.c
>> new file mode 100644
>> index 0000000..a66677b
>> --- /dev/null
>> +++ b/libavutil/twofish.c
>> @@ -0,0 +1,351 @@
>> +/*
>> + * An implementation of the TwoFish algorithm as mentioned in RFC3713

The reference to RFC3713 is probably a copy/paste error.

>> + * 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, 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, 0xd7, 0x8c, 0x61, 0x3a,
>> +    0xcc, 0x97, 0x7a, 0x21, 0xc9, 0x92, 0x7f, 0x24, 0xc6, 0x9d, 0x70, 0x2b, 0xc3, 0x98, 0x75, 0x2e
>> +};
>> +
>> +static const uint8_t MD2[256] = {
>> +    0x00, 0xef, 0xb7, 0x58, 0x07, 0xe8, 0xb0, 0x5f, 0x0e, 0xe1, 0xb9, 0x56, 0x09, 0xe6, 0xbe, 0x51,
>> +    0x1c, 0xf3, 0xab, 0x44, 0x1b, 0xf4, 0xac, 0x43, 0x12, 0xfd, 0xa5, 0x4a, 0x15, 0xfa, 0xa2, 0x4d,
>> +    0x38, 0xd7, 0x8f, 0x60, 0x3f, 0xd0, 0x88, 0x67, 0x36, 0xd9, 0x81, 0x6e, 0x31, 0xde, 0x86, 0x69,
>> +    0x24, 0xcb, 0x93, 0x7c, 0x23, 0xcc, 0x94, 0x7b, 0x2a, 0xc5, 0x9d, 0x72, 0x2d, 0xc2, 0x9a, 0x75,
>> +    0x70, 0x9f, 0xc7, 0x28, 0x77, 0x98, 0xc0, 0x2f, 0x7e, 0x91, 0xc9, 0x26, 0x79, 0x96, 0xce, 0x21,
>> +    0x6c, 0x83, 0xdb, 0x34, 0x6b, 0x84, 0xdc, 0x33, 0x62, 0x8d, 0xd5, 0x3a, 0x65, 0x8a, 0xd2, 0x3d,
>> +    0x48, 0xa7, 0xff, 0x10, 0x4f, 0xa0, 0xf8, 0x17, 0x46, 0xa9, 0xf1, 0x1e, 0x41, 0xae, 0xf6, 0x19,
>> +    0x54, 0xbb, 0xe3, 0x0c, 0x53, 0xbc, 0xe4, 0x0b, 0x5a, 0xb5, 0xed, 0x02, 0x5d, 0xb2, 0xea, 0x05,
>> +    0xe0, 0x0f, 0x57, 0xb8, 0xe7, 0x08, 0x50, 0xbf, 0xee, 0x01, 0x59, 0xb6, 0xe9, 0x06, 0x5e, 0xb1,
>> +    0xfc, 0x13, 0x4b, 0xa4, 0xfb, 0x14, 0x4c, 0xa3, 0xf2, 0x1d, 0x45, 0xaa, 0xf5, 0x1a, 0x42, 0xad,
>> +    0xd8, 0x37, 0x6f, 0x80, 0xdf, 0x30, 0x68, 0x87, 0xd6, 0x39, 0x61, 0x8e, 0xd1, 0x3e, 0x66, 0x89,
>> +    0xc4, 0x2b, 0x73, 0x9c, 0xc3, 0x2c, 0x74, 0x9b, 0xca, 0x25, 0x7d, 0x92, 0xcd, 0x22, 0x7a, 0x95,
>> +    0x90, 0x7f, 0x27, 0xc8, 0x97, 0x78, 0x20, 0xcf, 0x9e, 0x71, 0x29, 0xc6, 0x99, 0x76, 0x2e, 0xc1,
>> +    0x8c, 0x63, 0x3b, 0xd4, 0x8b, 0x64, 0x3c, 0xd3, 0x82, 0x6d, 0x35, 0xda, 0x85, 0x6a, 0x32, 0xdd,
>> +    0xa8, 0x47, 0x1f, 0xf0, 0xaf, 0x40, 0x18, 0xf7, 0xa6, 0x49, 0x11, 0xfe, 0xa1, 0x4e, 0x16, 0xf9,
>> +    0xb4, 0x5b, 0x03, 0xec, 0xb3, 0x5c, 0x04, 0xeb, 0xba, 0x55, 0x0d, 0xe2, 0xbd, 0x52, 0x0a, 0xe5
>> +};
>> +
>> +static const uint8_t t[2][64] = {
>> +    {0x08, 0x01, 0x07, 0x0d, 0x06, 0x0f, 0x03, 0x02, 0x00, 0x0b, 0x05, 0x09, 0x0e, 0x0c, 0x0a, 0x04,
>> +        0x0e, 0x0c, 0x0b, 0x08, 0x01, 0x02, 0x03, 0x05, 0x0f, 0x04, 0x0a, 0x06, 0x07, 0x00, 0x09, 0x0d,
>> +        0x0b, 0x0a, 0x05, 0x0e, 0x06, 0x0d, 0x09, 0x00, 0x0c, 0x08, 0x0f, 0x03, 0x02, 0x04, 0x07, 0x01,
>> +        0x0d, 0x07, 0x0f, 0x04, 0x01, 0x02, 0x06, 0x0e, 0x09, 0x0b, 0x03, 0x00, 0x08, 0x05, 0x0c, 0x0a},
>> +    {0x02, 0x08, 0x0b, 0x0d, 0x0f, 0x07, 0x06, 0x0e, 0x03, 0x01, 0x09, 0x04, 0x00, 0x0a, 0x0c, 0x05,
>> +        0x01, 0x0e, 0x02, 0x0b, 0x04, 0x0c, 0x03, 0x07, 0x06, 0x0d, 0x0a, 0x05, 0x0f, 0x09, 0x00, 0x08,
>> +        0x04, 0x0c, 0x07, 0x05, 0x01, 0x06, 0x09, 0x0a, 0x00, 0x0e, 0x0d, 0x08, 0x02, 0x0b, 0x03, 0x0f,
>> +        0x0b, 0x09, 0x05, 0x01, 0x0c, 0x03, 0x0d, 0x0e, 0x06, 0x04, 0x07, 0x0f, 0x02, 0x00, 0x08, 0x0a}
>> +};
>> +
>> +struct AVTWOFISH *av_twofish_alloc(void)
>> +{
>> +    return av_mallocz(sizeof(struct AVTWOFISH));
>> +}
>> +
>> +const int av_twofish_size = sizeof(AVTWOFISH);
>> +
>
>> +static uint8_t q(int i, uint8_t a)
>> +{
>> +    uint8_t aa, bb, b;
>> +    aa = a >> 4;
>> +    bb = a & 0x0f;
>> +    a = aa ^ bb;
>> +    b = aa ^ (R4(bb) & 0x0f) ^ ((aa << 3) & 0x0f);
>> +    aa = t[i][a] & 0x0f;
>> +    bb = t[i][16 + b] & 0x0f;
>> +    a = aa ^ bb;
>> +    b = aa ^ (R4(bb) & 0x0f) ^ ((aa << 3) & 0x0f);
>> +    aa = t[i][32 + a] & 0x0f;
>> +    bb = t[i][48 + b] & 0x0f;
>> +    return (bb << 4) + aa;
>> +}
>
> this could be done as a 512 entry look up table

Michael, thanks for the review. Permutation q0 and q1 can of course be
computed with lookup tables, but I have no strong preference here, we
can do it now or optimize afterwards.

Giorgio Vazzana


More information about the ffmpeg-devel mailing list