tarequeh / DES

Implementation of Data Encryption Standard (DES) in C
http://www.codexn.com
MIT License
258 stars 140 forks source link

test failed #2

Open boyshell opened 7 years ago

boyshell commented 7 years ago
#include <malloc.h>
#include <string.h>
#include <cstdio>
#include <stdlib.h>
#include <cassert>
#include <cstdlib>
#include "des.h"

void c(const char* const src, char* const dst, const int size, const key_set* const key_sets, int m) {
    char buf_src[8], buf_dst[8];
    int block = size / 8;
    int mod = size % 8;
    memcpy(dst + block * 8, src + block * 8, mod);

    for (int i = 0; i < block; ++i) {
        memcpy(buf_src, src + i * 8, 8);
        process_message((unsigned char *) buf_src, (unsigned char *) buf_dst, (key_set *) key_sets, m);
        memcpy(dst + i * 8, buf_dst, 8);
    }
}

void c(const char* const src, char* const dst, const int size, int m) {
    char key1[] = {-5, 11, 24, -51, 34, 46, 98, -104};
    char key2[] = {-51, 101, 44, -21, 54, 47, 91, -114};
    char key3[] = {-57, 111, 27, -81, 38, 66, 58, -14};

    key_set* key_sets1 = (key_set*)malloc(17 * sizeof(key_set));
    key_set* key_sets2 = (key_set*)malloc(17 * sizeof(key_set));
    key_set* key_sets3 = (key_set*)malloc(17 * sizeof(key_set));

    generate_sub_keys((unsigned char *) key1, key_sets1);
    generate_sub_keys((unsigned char *) key2, key_sets2);
    generate_sub_keys((unsigned char *) key3, key_sets3);

    if (m == ENCRYPTION_MODE) {
        c(src, dst, size, key_sets1, m);
        c(dst, dst, size, key_sets2, m);
        c(dst, dst, size, key_sets3, m);
    } else {
        c(src, dst, size, key_sets3, m);
        c(dst, dst, size, key_sets2, m);
        c(dst, dst, size, key_sets1, m);
    }

    free(key_sets1);
    free(key_sets2);
    free(key_sets3);
}

bool test(char* src, int size) {
    char* dst = new char[size];
    char* dst2 = new char[size];

    c(src, dst, size, ENCRYPTION_MODE);
    c(dst, dst2, size, DECRYPTION_MODE);

    for (int i = 0; i < size; ++i) {
        if (dst2[i] != src[i]) {
            delete [] dst;
            delete [] dst2;
            return false;
        }
    }

    delete [] dst;
    delete [] dst2;
    return true;
}

int main(int argc, char** argv) {
    for (int i = 0; i < 1000; ++i) {
        int size = rand() % 9999;
        unsigned char* src = new unsigned char[size];
        for (int s = 0; s < size; ++s) {
            src[s] = rand() % 256;
        }
        if (!test((char*)src, size)) {
            printf("bug:%d,count:%d\n", i, size);
        }
        delete [] src;
    }
    return 0;
}

result bug:201,count:3066 os windows 10 compiler E:\workplace\sample\jni-classloader\src\main\java>g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=C:/Program\ Files/mingw-w64/x86_64-6.2.0-posix-seh-rt_v5- rev1/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/6.2.0/lto-wrapper.exe Target: x86_64-w64-mingw32 Configured with: ../../../src/gcc-6.2.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/mingw620/x86_64-620-posix-seh-rt_v5-rev1/mingw64 --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --enable-libstdcxx-filesystem-ts=yes --disable-isl-version-check --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw620/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw620/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw620/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/mingw620/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-seh-rev1, Built by MinGW-W64 project' --with-bugurl=http://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -I/c/mingw620/x86_64-620-posix-seh-rt_v5-rev1/mingw64/opt/include -I/c/mingw620/prerequisites/x86_64-zlib-static/include -I/c/mingw620/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -I/c/mingw620/x86_64-620-posix-seh-rt_v5-rev1/mingw64/opt/include -I/c/mingw620/prerequisites/x86_64-zlib-static/include -I/c/mingw620/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS= LDFLAGS='-pipe -L/c/mingw620/x86_64-620-posix-seh-rt_v5-rev1/mingw64/opt/lib -L/c/mingw620/prerequisites/x86_64-zlib-static/lib -L/c/mingw620/prerequisites/x86_64-w64-mingw32- static/lib ' Thread model: posix gcc version 6.2.0 (x86_64-posix-seh-rev1, Built by MinGW-W64 project)

falcon35180 commented 6 months ago

This is actually related to #7 - fix that and this problem goes away (confirmed with testing).