FFmpeg
camellia.c
Go to the documentation of this file.
1 /*
2  * An implementation of the CAMELLIA algorithm as mentioned in RFC3713
3  * Copyright (c) 2014 Supraja Meedinti
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <stddef.h>
23 #include <string.h>
24 
25 #include "libavutil/camellia.h"
26 #include "libavutil/log.h"
27 #include "libavutil/mem.h"
28 
29 int main(int argc, char *argv[])
30 {
31  const uint8_t Key[3][32] = {
32  {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10},
33  {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77},
34  {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}
35  };
36  const uint8_t rct[3][16] = {
37  {0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73, 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43},
38  {0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9,0x96, 0xf8, 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9},
39  {0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c, 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09}
40  };
41  const uint8_t rpt[32] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
42  const int kbits[3] = {128, 192, 256};
43  int i, j, err = 0;
44  uint8_t temp[32], iv[16];
45  struct AVCAMELLIA *cs;
46  cs = av_camellia_alloc();
47  if (!cs)
48  return 1;
49  for (j = 0; j < 3; j++) {
50  av_camellia_init(cs, Key[j], kbits[j]);
51  av_camellia_crypt(cs, temp, rpt, 1, NULL, 0);
52  for (i = 0; i < 16; i++) {
53  if (rct[j][i] != temp[i]) {
54  av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rct[j][i], temp[i]);
55  err = 1;
56  }
57  }
58  av_camellia_crypt(cs, temp, rct[j], 1, NULL, 1);
59  for (i = 0; i < 16; i++) {
60  if (rpt[i] != temp[i]) {
61  av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rpt[i], temp[i]);
62  err = 1;
63  }
64  }
65  }
66  av_camellia_init(cs, Key[0], 128);
67  memcpy(iv, "HALLO123HALLO123", 16);
68  av_camellia_crypt(cs, temp, rpt, 2, iv, 0);
69  memcpy(iv, "HALLO123HALLO123", 16);
70  av_camellia_crypt(cs, temp, temp, 2, iv, 1);
71  for (i = 0; i < 32; i++) {
72  if (rpt[i] != temp[i]) {
73  av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rpt[i], temp[i]);
74  err = 1;
75  }
76  }
77  av_free(cs);
78  return err;
79 }
av_camellia_alloc
struct AVCAMELLIA * av_camellia_alloc(void)
Allocate an AVCAMELLIA context To free the struct: av_free(ptr)
Definition: camellia.c:351
AVCAMELLIA
Definition: camellia.c:46
av_camellia_crypt
void av_camellia_crypt(AVCAMELLIA *cs, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt)
Encrypt or decrypt a buffer using a previously initialized context.
Definition: camellia.c:397
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_camellia_init
av_cold int av_camellia_init(AVCAMELLIA *cs, const uint8_t *key, int key_bits)
Initialize an AVCAMELLIA context.
Definition: camellia.c:356
main
int main(int argc, char *argv[])
Definition: camellia.c:29
NULL
#define NULL
Definition: coverity.c:32
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:270
temp
else temp
Definition: vf_mcdeint.c:263
mem.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
camellia.h
Public header for libavutil CAMELLIA algorithm.