LCOV - code coverage report
Current view: top level - libavutil/tests - des.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 40 48 83.3 %
Date: 2017-12-17 23:02:56 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This file is part of FFmpeg.
       3             :  *
       4             :  * FFmpeg is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Lesser General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2.1 of the License, or (at your option) any later version.
       8             :  *
       9             :  * FFmpeg is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Lesser General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public
      15             :  * License along with FFmpeg; if not, write to the Free Software
      16             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      17             :  */
      18             : 
      19             : #include "libavutil/timer.h"
      20             : 
      21             : #include "libavutil/des.c"
      22             : 
      23             : #include <stdint.h>
      24             : #include <stdio.h>
      25             : #include <stdlib.h>
      26             : #include <string.h>
      27             : 
      28             : #include "libavutil/time.h"
      29             : 
      30        4000 : static uint64_t rand64(void)
      31             : {
      32        4000 :     uint64_t r = rand();
      33        4000 :     r = (r << 32) | rand();
      34        4000 :     return r;
      35             : }
      36             : 
      37             : static const uint8_t test_key[] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 };
      38             : static const DECLARE_ALIGNED(8, uint8_t, plain)[] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
      39             : static const DECLARE_ALIGNED(8, uint8_t, crypt_ref)[] = { 0x4a, 0xb6, 0x5b, 0x3d, 0x4b, 0x06, 0x15, 0x18 };
      40             : static DECLARE_ALIGNED(8, uint8_t, tmp)[8];
      41             : static DECLARE_ALIGNED(8, uint8_t, large_buffer)[10002][8];
      42             : static const uint8_t cbc_key[] = {
      43             :     0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
      44             :     0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01,
      45             :     0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23
      46             : };
      47             : 
      48           4 : static int run_test(int cbc, int decrypt)
      49             : {
      50             :     AVDES d;
      51           4 :     int delay = cbc && !decrypt ? 2 : 1;
      52             :     uint64_t res;
      53           4 :     AV_WB64(large_buffer[0], 0x4e6f772069732074ULL);
      54           4 :     AV_WB64(large_buffer[1], 0x1234567890abcdefULL);
      55           4 :     AV_WB64(tmp,             0x1234567890abcdefULL);
      56           4 :     av_des_init(&d, cbc_key, 192, decrypt);
      57           4 :     av_des_crypt(&d, large_buffer[delay], large_buffer[0], 10000, cbc ? tmp : NULL, decrypt);
      58           4 :     res = AV_RB64(large_buffer[9999 + delay]);
      59           4 :     if (cbc) {
      60           2 :         if (decrypt)
      61           1 :             return res == 0xc5cecf63ecec514cULL;
      62             :         else
      63           1 :             return res == 0xcb191f85d1ed8439ULL;
      64             :     } else {
      65           2 :         if (decrypt)
      66           1 :             return res == 0x8325397644091a0aULL;
      67             :         else
      68           1 :             return res == 0xdd17e8b8b437d232ULL;
      69             :     }
      70             : }
      71             : 
      72             : union word_byte {
      73             :     uint64_t word;
      74             :     uint8_t byte[8];
      75             : };
      76             : 
      77           1 : int main(void)
      78             : {
      79             :     AVDES d;
      80             :     int i;
      81             :     union word_byte key[3], data, ct;
      82             :     uint64_t roundkeys[16];
      83           1 :     srand(av_gettime());
      84           1 :     key[0].word = AV_RB64(test_key);
      85           1 :     data.word   = AV_RB64(plain);
      86           1 :     gen_roundkeys(roundkeys, key[0].word);
      87           1 :     if (des_encdec(data.word, roundkeys, 0) != AV_RB64(crypt_ref)) {
      88           0 :         printf("Test 1 failed\n");
      89           0 :         return 1;
      90             :     }
      91           1 :     av_des_init(&d, test_key, 64, 0);
      92           1 :     av_des_crypt(&d, tmp, plain, 1, NULL, 0);
      93           1 :     if (memcmp(tmp, crypt_ref, sizeof(crypt_ref))) {
      94           0 :         printf("Public API decryption failed\n");
      95           0 :         return 1;
      96             :     }
      97           1 :     if (!run_test(0, 0) || !run_test(0, 1) || !run_test(1, 0) || !run_test(1, 1)) {
      98           0 :         printf("Partial Monte-Carlo test failed\n");
      99           0 :         return 1;
     100             :     }
     101        1001 :     for (i = 0; i < 1000; i++) {
     102        1000 :         key[0].word = rand64();
     103        1000 :         key[1].word = rand64();
     104        1000 :         key[2].word = rand64();
     105        1000 :         data.word   = rand64();
     106        1000 :         av_des_init(&d, key[0].byte, 192, 0);
     107        1000 :         av_des_crypt(&d, ct.byte, data.byte, 1, NULL, 0);
     108        1000 :         av_des_init(&d, key[0].byte, 192, 1);
     109        1000 :         av_des_crypt(&d, ct.byte, ct.byte, 1, NULL, 1);
     110        1000 :         if (ct.word != data.word) {
     111           0 :             printf("Test 2 failed\n");
     112           0 :             return 1;
     113             :         }
     114             :     }
     115             : #ifdef GENTABLES
     116             :     printf("static const uint32_t S_boxes_P_shuffle[8][64] = {\n");
     117             :     for (i = 0; i < 8; i++) {
     118             :         int j;
     119             :         printf("    {");
     120             :         for (j = 0; j < 64; j++) {
     121             :             uint32_t v = S_boxes[i][j >> 1];
     122             :             v   = j & 1 ? v >> 4 : v & 0xf;
     123             :             v <<= 28 - 4 * i;
     124             :             v   = shuffle(v, P_shuffle, sizeof(P_shuffle));
     125             :             printf((j & 7) == 0 ? "\n    " : " ");
     126             :             printf("0x%08X,", v);
     127             :         }
     128             :         printf("\n    },\n");
     129             :     }
     130             :     printf("};\n");
     131             : #endif
     132           1 :     return 0;
     133             : }

Generated by: LCOV version 1.13