[FFmpeg-devel] Patch for Cast128 algorithm implementation
Michael Niedermayer
michaelni at gmx.at
Thu Oct 16 05:32:31 CEST 2014
On Thu, Oct 16, 2014 at 03:25:42AM +0530, supraja reddy wrote:
> Extremely sorry for resending the patch . There was a trailing whitespace
> which I hadn't corrected .
> All the changes are done .
> Please let me if any changes needed further .
[...]
> diff --git a/libavutil/cast5.c b/libavutil/cast5.c
> new file mode 100644
> index 0000000..89b065d
> --- /dev/null
> +++ b/libavutil/cast5.c
> @@ -0,0 +1,535 @@
> +/*
> + * An implementation of the CAST128 algorithm as mentioned in RFC2144
> + * Copyright (c) 2014 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 "cast5.h"
> +#include "common.h"
> +#include "intreadwrite.h"
> +
> +#define IA(x) (((x)>>24) & 0xff)
& 0xff isnt needed here
[...]
> +#include<stdio.h>
> +#include<stdlib.h>
> +#include"log.h"
these should be under the #ifdef TEST
> +
> +#ifdef TEST
> +int main(int argc, char** argv)
> +{
> + AVCAST5 cs;
> + static uint8_t Key[3][16]={{0x01,0x23,0x45,0x67,0x12,0x34,0x56,0x78,0x23,0x45,0x67,0x89,0x34,0x56,0x78,0x9a},
> + {0x01,0x23,0x45,0x67,0x12,0x34,0x56,0x78,0x23,0x45},
> + {0x01,0x23,0x45,0x67,0x12}};
> + uint8_t rpt[8]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
> + uint8_t rct[3][8]={{0x23,0x8b,0x4f,0xe5,0x84,0x7e,0x44,0xb2},
> + {0xeb,0x6a,0x71,0x1a,0x2c,0x02,0x27,0x1b},
> + {0x7a,0xc8,0x16,0xd1,0x6e,0x9b,0x30,0x2e}};
all 3 of these arrays can be static const
> + int i,err=0;
> + uint8_t temp[8];
> + av_log_set_level(AV_LOG_DEBUG);
shouldnt be needed
> + av_cast5_init(&cs,Key[0],128);
> + av_cast5_crypt(&cs,temp,rpt,1,0);
> + for (i=0;i<8;i++){
> + if (rct[0][i]!=temp[i]){
> + av_log(NULL,AV_LOG_ERROR,"%d %02x %02x\n",i,rct[0][i],temp[i]);
> + err=1;
> + }
> + }
> + av_cast5_crypt(&cs,temp,rct[0],1,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_cast5_init(&cs,Key[1],80);
> + av_cast5_crypt(&cs,temp,rpt,1,0);
> + for (i=0;i<8;i++){
> + if (rct[1][i]!=temp[i]){
> + av_log(NULL,AV_LOG_ERROR,"%d %02x %02x\n",i,rct[1][i],temp[i]);
> + err=1;
> + }
> + }
> + av_cast5_crypt(&cs,temp,rct[1],1,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_cast5_init(&cs,Key[2],40);
> + av_cast5_crypt(&cs,temp,rpt,1,0);
> + for (i=0;i<8;i++){
> + if (rct[2][i]!=temp[i]){
> + av_log(NULL,AV_LOG_ERROR,"%d %02x %02x\n",i,rct[2][i],temp[i]);
> + err=1;
> + }
> + }
> + av_cast5_crypt(&cs,temp,rct[2],1,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;
> + }
> + }
this looks like it could be a loop that runs 3 iterations
each for a different keysize, should be simpler
also a test that enciphers and deciphers a larger amount of random
data could be added
> + return err;
> +}
> +#endif
> diff --git a/libavutil/cast5.h b/libavutil/cast5.h
> new file mode 100644
> index 0000000..d151031
> --- /dev/null
> +++ b/libavutil/cast5.h
> @@ -0,0 +1,60 @@
> +/*
> + * An implementation of the CAST128 algorithm as mentioned in RFC2144
> + * Copyright (c) 2014 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
> + */
> +
> +#ifndef AVUTIL_CAST5_H
> +#define AVUTIL_CAST5_H
> +
> +#include <stdint.h>
> +
> +#include "attributes.h"
> +#include "version.h"
> +
> +/**
> + * @file
> + * @brief Public header for libavutil CAST5 algorithm
> + * @defgroup lavu_cast5 CAST5
> + * @ingroup lavu_crypto
> + * @{
> + */
> +struct AVCAST5;
> +
> +/**
> + * Initialize an AVCAST5 context.
> + *
> + * @param ctx an AVCAST5 context
> + * @param key a key of 5,6,...16 bytes used for encryption/decryption
> + * @param key_bits number of keybits: possible are 40,48,...,128
> + */
> +int av_cast5_init(struct AVCAST5 *ctx, const uint8_t *key, int key_bits);
this initializes the struct but how should the user allocate it?
but overall the patch looks quite nice already
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
The misfortune of the wise is better than the prosperity of the fool.
-- Epicurus
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 181 bytes
Desc: Digital signature
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20141016/878eb511/attachment.asc>
More information about the ffmpeg-devel
mailing list