├── .gitignore ├── LICENSE ├── README.md ├── des_cuda.sln ├── des_cuda ├── bit_utils.h ├── c_utils.h ├── consts.h ├── crack-des ├── cuda_utils.h ├── des.h ├── des_consts.h ├── des_kernel.h ├── des_utils.h ├── kernel.cu └── makefile ├── des_protocol.Rmd └── des_protocol.pdf /.gitignore: -------------------------------------------------------------------------------- 1 | .vs 2 | Debug 3 | *.swp 4 | *.opensdf 5 | *.sdf 6 | *.vcxproj 7 | *.vcxproj.user 8 | *.pdb 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Maciej Grzeszczak 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # des-cuda 2 | DES cracking using brute force algorithm and CUDA 3 | 4 | # License 5 | ``` 6 | MIT License 7 | 8 | Copyright (c) 2017 Maciej Grzeszczak 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in all 18 | copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | SOFTWARE. 27 | ``` 28 | -------------------------------------------------------------------------------- /des_cuda.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.23107.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "des_cuda", "des_cuda\des_cuda.vcxproj", "{C46915A5-A95B-47D8-8B6A-050888AD3399}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {C46915A5-A95B-47D8-8B6A-050888AD3399}.Debug|x64.ActiveCfg = Debug|x64 17 | {C46915A5-A95B-47D8-8B6A-050888AD3399}.Debug|x64.Build.0 = Debug|x64 18 | {C46915A5-A95B-47D8-8B6A-050888AD3399}.Debug|x86.ActiveCfg = Debug|Win32 19 | {C46915A5-A95B-47D8-8B6A-050888AD3399}.Debug|x86.Build.0 = Debug|Win32 20 | {C46915A5-A95B-47D8-8B6A-050888AD3399}.Release|x64.ActiveCfg = Release|x64 21 | {C46915A5-A95B-47D8-8B6A-050888AD3399}.Release|x64.Build.0 = Release|x64 22 | {C46915A5-A95B-47D8-8B6A-050888AD3399}.Release|x86.ActiveCfg = Release|Win32 23 | {C46915A5-A95B-47D8-8B6A-050888AD3399}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /des_cuda/bit_utils.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cuda_runtime.h" 3 | #include "device_launch_parameters.h" 4 | 5 | #include 6 | #include 7 | 8 | __device__ __host__ uint64_t bits_bit8(uint8_t input, int nr); 9 | __device__ __host__ uint64_t bits_bit16(uint16_t input, int nr); 10 | __device__ __host__ uint64_t bits_bit32(uint32_t input, int nr); 11 | __device__ __host__ uint64_t bits_bit64(uint64_t input, int nr); 12 | 13 | __device__ __host__ void bits_split64(uint64_t input, uint64_t *left, uint64_t *right); 14 | __device__ __host__ void bits_split(uint64_t input, uint64_t *left, uint64_t *right, int len); 15 | __device__ __host__ void bits_set(uint64_t input, int nr, uint64_t val); 16 | __device__ __host__ void bits_copy(uint64_t src, uint64_t *dst, int offset, int len); 17 | __device__ __host__ void bits_print(uint64_t val); 18 | __device__ __host__ void bits_print_grouped(uint64_t val, int group_size, int length); 19 | 20 | __device__ __host__ uint64_t bits_cycle_left(uint64_t val, int shift, int size); 21 | __device__ __host__ uint64_t bits_cycle_right(uint64_t val, int shift, int size); 22 | __device__ __host__ uint64_t bits_permutate(uint64_t key, int*permutation, int length, int key_length); 23 | 24 | __device__ __host__ uint64_t bits_bit8(uint8_t input, int nr) { 25 | return (input >> nr) & 1; 26 | } 27 | __device__ __host__ uint64_t bits_bit16(uint16_t input, int nr) { 28 | return (input >> nr) & 1; 29 | } 30 | __device__ __host__ uint64_t bits_bit32(uint32_t input, int nr) { 31 | return (input >> nr) & 1; 32 | } 33 | __device__ __host__ uint64_t bits_bit64(uint64_t input, int nr) { 34 | return (input >> nr) & 1; 35 | } 36 | 37 | __device__ __host__ void bits_set(uint64_t *data, int nr, uint64_t val) { 38 | *data = *data | ((uint64_t)val << nr); 39 | } 40 | 41 | __device__ __host__ void bits_copy(uint64_t src, uint64_t *dst, int src_offset,int dst_offset, int len) { 42 | for (int i = 0; i < len; i++) { 43 | bits_set(dst, i+dst_offset, bits_bit64(src, src_offset + i)); 44 | } 45 | } 46 | 47 | __device__ __host__ void bits_split64(uint64_t input, uint64_t *left, uint64_t *right) { 48 | bits_split(input, left, right, 64); 49 | } 50 | 51 | __device__ __host__ void bits_split(uint64_t input, uint64_t *left, uint64_t *right, int len) { 52 | *left = *right = 0; 53 | bits_copy(input, right, 0,0, len / 2); 54 | bits_copy(input, left, len / 2,0, len / 2); 55 | } 56 | 57 | __device__ __host__ void bits_print(uint64_t val) { 58 | for (int i = 63; i >=0 ; i--) { 59 | printf("%d ", bits_bit64(val, i)); 60 | if (i % 8 == 0) printf(" "); 61 | } 62 | } 63 | 64 | __device__ __host__ uint64_t bits_cycle_left(uint64_t val, int shift, int size) { 65 | uint64_t tmp = 0; 66 | bits_copy(val, &tmp, size-shift, 0, shift); 67 | val = val << shift; 68 | bits_copy(tmp, &val, 0, 0, shift); 69 | val = val & (~(0xffffffffffffffff << size)); 70 | return val; 71 | } 72 | 73 | __device__ __host__ uint64_t bits_cycle_right(uint64_t val, int shift, int size) { 74 | uint64_t tmp = 0; 75 | bits_copy(val, &tmp, size - shift, 0, shift); 76 | val = val << shift; 77 | bits_copy(tmp, &val, 0, 0, shift); 78 | val = val & (~(0xffffffffffffffff << size)); 79 | return val; 80 | } 81 | 82 | __device__ __host__ uint64_t bits_permutate(uint64_t key,int*permutation, int length, int key_length) { 83 | uint64_t result = 0; 84 | for (int i = 0; i < length; i++) { 85 | bits_set(&result, i, bits_bit64(key, key_length - permutation[length - 1 - i])); 86 | } 87 | return result; 88 | } 89 | 90 | __device__ __host__ void bits_print_grouped(uint64_t val, int group_size, int length) { 91 | for (int i = length - 1; i >= 0;i--) { 92 | printf("%d",bits_bit64(val,i)); 93 | if (i%group_size == 0) { 94 | printf(" "); 95 | } 96 | } 97 | printf("\n"); 98 | } -------------------------------------------------------------------------------- /des_cuda/c_utils.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "consts.h" 4 | 5 | void* _malloc(int size) { 6 | void* mem = malloc(size); 7 | if (mem == NULL) ERR("Failed to malloc on CPU\n"); 8 | return mem; 9 | } 10 | 11 | void* _realloc(void *data, int size) { 12 | void* mem = realloc(data,size); 13 | if (mem == NULL) ERR("Failed to realloc ON CPU\n"); 14 | return mem; 15 | } 16 | 17 | float randf() { 18 | return (float)rand() / (float)(RAND_MAX); 19 | } -------------------------------------------------------------------------------- /des_cuda/consts.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | /****************************************** 3 | MACROS 4 | ******************************************/ 5 | //#define ERR(s,...) (fprintf(stderr, s,__VA_ARGS__),\ 6 | // exit(EXIT_FAILURE)) 7 | 8 | void ERR(char* s){ 9 | fprintf(stderr,"%s\n",s); 10 | exit(EXIT_FAILURE); 11 | } 12 | 13 | /****************************************** 14 | CONSTANTS 15 | ******************************************/ 16 | const bool DEBUG = false; 17 | const int KB = 1024; 18 | const int MB = 1024 * 1024; 19 | const int MAX_THREADS_PER_BLOCK = 1024; 20 | const int MAX_BLOCKS_PER_GRID = 65535; 21 | -------------------------------------------------------------------------------- /des_cuda/crack-des: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mgrzeszczak/des-cuda/53f3eea1948d1f6170e1acc277c563f1c748aea0/des_cuda/crack-des -------------------------------------------------------------------------------- /des_cuda/cuda_utils.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cuda_runtime.h" 3 | #include "device_launch_parameters.h" 4 | 5 | #include 6 | #include 7 | #include "consts.h" 8 | 9 | void _cudaSetDevice(int device) { 10 | cudaError_t cudaStatus = cudaSetDevice(0); 11 | if (cudaStatus != cudaSuccess) { 12 | printf("%s\n", cudaGetErrorString(cudaStatus)); 13 | ERR("cudaSetDevice failed! Do you have a CUDA-capable GPU installed?\n"); 14 | } 15 | } 16 | 17 | void _cudaMalloc(void **dest, size_t size) { 18 | cudaError_t cudaStatus = cudaMalloc(dest, size); 19 | if (cudaStatus != cudaSuccess) { 20 | printf("%s\n", cudaGetErrorString(cudaStatus)); 21 | ERR("cudaMalloc failed!\n"); 22 | } 23 | } 24 | 25 | void _cudaMemset(void *dest, int val, size_t size) { 26 | cudaError_t cudaStatus = cudaMemset(dest, 0, size); 27 | if (cudaStatus != cudaSuccess) { 28 | printf("%s\n", cudaGetErrorString(cudaStatus)); 29 | ERR("cudaMemset failed!\n"); 30 | } 31 | } 32 | 33 | void _cudaMemcpy(void *dest, const void *src, size_t size, cudaMemcpyKind kind) { 34 | cudaError_t cudaStatus = cudaMemcpy(dest, src, size, kind); 35 | if (cudaStatus != cudaSuccess) { 36 | printf("%s\n", cudaGetErrorString(cudaStatus)); 37 | ERR("cudaMemcpy failed!\n"); 38 | } 39 | } 40 | 41 | void _cudaDeviceSynchronize(char* s) { 42 | cudaError_t cudaStatus = cudaGetLastError(); 43 | if (cudaStatus != cudaSuccess) { 44 | printf("%s\n", cudaGetErrorString(cudaStatus)); 45 | //ERR("%s launch failed: %s\n",s, cudaGetErrorString(cudaStatus)); 46 | ERR("launch failed"); 47 | } 48 | cudaStatus = cudaDeviceSynchronize(); 49 | if (cudaStatus != cudaSuccess) { 50 | printf("%s\n", cudaGetErrorString(cudaStatus)); 51 | //ERR("cudaDeviceSynchronize returned error code %d after launching %s!\n",cudaStatus,s); 52 | ERR("cudaDeviceSynchronize error"); 53 | } 54 | } 55 | 56 | void _cudaPrintMemory() { 57 | //if (!DEBUG) return; 58 | size_t mem_free; 59 | size_t mem_total; 60 | cudaMemGetInfo(&mem_free, &mem_total); 61 | mem_free /= MB; 62 | mem_total /= MB; 63 | printf("MEMORY: \nfree %u MB\ntotal %u MB\n", mem_free, mem_total); 64 | } 65 | 66 | void _cudaResizeStack() { 67 | size_t size_stack; 68 | cudaDeviceSetLimit(cudaLimitStackSize, 128*sizeof(uint64_t)); 69 | cudaDeviceGetLimit(&size_stack, cudaLimitStackSize); 70 | printf("Stack size (B): %d\n",size_stack); 71 | } 72 | -------------------------------------------------------------------------------- /des_cuda/des.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include "des_consts.h" 5 | #include "bit_utils.h" 6 | #include "des_utils.h" 7 | 8 | // des algorithm based on http://page.math.tu-berlin.de/~kant/teaching/hess/krypto-ws2006/des.htm 9 | 10 | __device__ __host__ void des_create_subkeys(uint64_t key, uint64_t *keys); 11 | __device__ __host__ uint64_t des_encode_block(uint64_t block, uint64_t *keys); 12 | __device__ __host__ uint64_t f(uint64_t right, uint64_t key); 13 | __host__ __device__ uint64_t full_des_encode_block(uint64_t key, uint64_t block); 14 | 15 | __device__ __host__ void des_create_subkeys(uint64_t key, uint64_t *keys) { 16 | int *PC_1; 17 | int *SHIFTS; 18 | int *PC_2; 19 | int *E_BIT; 20 | #ifdef __CUDA_ARCH__ 21 | PC_1 = dev_PC_1; 22 | SHIFTS = dev_SHIFTS; 23 | PC_2 = dev_PC_2; 24 | E_BIT = dev_E_BIT; 25 | #else 26 | PC_1 = h_PC_1; 27 | PC_2 = h_PC_2; 28 | SHIFTS = h_SHIFTS; 29 | E_BIT = h_E_BIT; 30 | #endif 31 | 32 | uint64_t key_plus = bits_permutate(key, PC_1, 56, 64); 33 | 34 | uint64_t left, right; 35 | bits_split(key_plus, &left, &right, 56); 36 | 37 | uint64_t c_blocks[17]; 38 | uint64_t d_blocks[17]; 39 | 40 | c_blocks[0] = left; 41 | d_blocks[0] = right; 42 | 43 | for (int i = 1; i <= 16; i++) { 44 | c_blocks[i] = bits_cycle_left(c_blocks[i - 1], SHIFTS[i - 1], 28); 45 | d_blocks[i] = bits_cycle_left(d_blocks[i - 1], SHIFTS[i - 1], 28); 46 | } 47 | 48 | for (int i = 1; i <= 16; i++) { 49 | keys[i - 1] = c_blocks[i] << 28 | d_blocks[i]; 50 | keys[i-1] = bits_permutate(keys[i - 1], PC_2, 48, 56); 51 | } 52 | } 53 | 54 | __device__ __host__ uint64_t f(uint64_t right, uint64_t key) { 55 | int *E_BIT; 56 | int **S; 57 | int *P; 58 | #ifdef __CUDA_ARCH__ 59 | E_BIT = dev_E_BIT; 60 | S = dev_S; 61 | P = dev_P; 62 | #else 63 | E_BIT = h_E_BIT; 64 | S = h_S; 65 | P = h_P; 66 | #endif 67 | 68 | uint64_t expanded = bits_permutate(right, E_BIT, 48, 32); 69 | uint64_t xored = expanded ^ key; 70 | uint64_t b[8]; 71 | uint64_t s[8]; 72 | 73 | for (int i = 7; i >=0; i--) { 74 | b[7-i] = 0; 75 | bits_copy(xored, &b[7-i], i * 6, 0, 6); 76 | uint64_t bb = b[7 - i]; 77 | 78 | uint64_t d_i = bits_bit64(bb, 5) << 1 | bits_bit64(bb, 0); 79 | uint64_t d_j = bits_bit64(bb, 4) << 3 | 80 | bits_bit64(bb, 3) << 2 | 81 | bits_bit64(bb, 2) << 1 | 82 | bits_bit64(bb, 1) << 0; 83 | 84 | int ii = d_i; 85 | int ij = d_j; 86 | int index = ii * 16 + ij; 87 | int data = S[7-i][index]; 88 | s[7 - i] = data; 89 | } 90 | 91 | uint64_t s_res = s[0] << 28 | 92 | s[1] << 24 | 93 | s[2] << 20 | 94 | s[3] << 16 | 95 | s[4] << 12 | 96 | s[5] << 8 | 97 | s[6] << 4 | 98 | s[7] << 0; 99 | 100 | uint64_t p = bits_permutate(s_res, P, 32, 32); 101 | return p; 102 | } 103 | 104 | __device__ __host__ uint64_t des_encode_block(uint64_t block, uint64_t *keys) { 105 | int *IP; 106 | int *IP_REV; 107 | #ifdef __CUDA_ARCH__ 108 | IP = dev_IP; 109 | IP_REV = dev_IP_REV; 110 | #else 111 | IP = h_IP; 112 | IP_REV = h_IP_REV; 113 | #endif 114 | 115 | uint64_t ip = bits_permutate(block, IP, 64, 64); 116 | uint64_t left, right; 117 | bits_split64(ip, &left, &right); 118 | 119 | for (int i = 0; i < 16; i++) { 120 | // 16 iterations 121 | // n = i +1 122 | uint64_t prev_right = right; 123 | uint64_t prev_left = left; 124 | left = prev_right; 125 | right = prev_left ^ f(prev_right, keys[i]); 126 | } 127 | 128 | uint64_t reversed = right << 32 | left; 129 | uint64_t encoded = bits_permutate(reversed, IP_REV, 64, 64); 130 | return encoded; 131 | } 132 | 133 | __host__ __device__ uint64_t full_des_encode_block(uint64_t key, uint64_t block) { 134 | uint64_t keys[16]; 135 | des_create_subkeys(key, keys); 136 | return des_encode_block(block, keys); 137 | } -------------------------------------------------------------------------------- /des_cuda/des_consts.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mgrzeszczak/des-cuda/53f3eea1948d1f6170e1acc277c563f1c748aea0/des_cuda/des_consts.h -------------------------------------------------------------------------------- /des_cuda/des_kernel.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cuda_runtime.h" 3 | #include "device_launch_parameters.h" 4 | #include 5 | #include "cuda_utils.h" 6 | #include "bit_utils.h" 7 | #include "c_utils.h" 8 | #include "consts.h" 9 | #include "des.h" 10 | 11 | __global__ void cuda_des_encode_block(uint64_t block, uint64_t key, uint64_t *encoded); 12 | __global__ void cuda_crack_des_kernel(uint64_t block, uint64_t encoded,uint64_t limit, uint64_t *key, int *done); 13 | 14 | void run_des_crack(uint64_t block, uint64_t encoded, int key_length, uint64_t *key); 15 | void run_des_encode_block(uint64_t key, uint64_t block, uint64_t *result); 16 | uint64_t calculate_limit(int key_length); 17 | 18 | __global__ void cuda_des_encode_block(uint64_t block, uint64_t key, uint64_t *encoded) { 19 | uint64_t keys[16]; 20 | des_create_subkeys(key, keys); 21 | uint64_t result = des_encode_block(block, keys); 22 | *encoded = result; 23 | } 24 | 25 | __constant__ uint64_t POW_2_42 = 4398046511104; 26 | __constant__ uint16_t POW_2_14 = 16384; 27 | 28 | __global__ void cuda_crack_des_kernel(uint64_t block, uint64_t encoded,uint64_t limit, uint64_t *key, int* done) { 29 | const int threadCount = 512; 30 | int blockCount = gridDim.x; 31 | int tbid = blockIdx.x*threadCount + threadIdx.x; 32 | int id = threadIdx.x; 33 | int offset = blockIdx.x * threadCount; 34 | 35 | if ((*done) == 1){ 36 | return; 37 | } 38 | 39 | // calculate current thread starting position 40 | uint64_t current_key = 0; 41 | uint64_t total_id = (uint64_t)blockIdx.x*(uint64_t)threadCount + (uint64_t)threadIdx.x; 42 | bits_copy(total_id, ¤t_key, 0, 1, 7); 43 | bits_copy(total_id, ¤t_key, 7, 9, 7); 44 | bits_copy(total_id, ¤t_key, 14, 17, 7); 45 | //bits_copy(total_id, ¤t_key, 21, 25, 1); 46 | //bits_copy(total_id, ¤t_key, 22, 26, 1); 47 | //bits_copy(total_id, ¤t_key, 23, 27, 1); 48 | 49 | /*if (tbid == 16383) { 50 | *key = current_key; 51 | }*/ 52 | 53 | uint64_t result; 54 | 55 | //const uint64_t max = 34359738368; 56 | for (uint64_t i=0;i> 40; 61 | // copy 62 | bits_copy(i, ¤t_key, 0, 25, 7); 63 | bits_copy(i, ¤t_key, 7, 33, 7); 64 | bits_copy(i, ¤t_key, 14, 41, 7); 65 | bits_copy(i, ¤t_key, 21, 49, 7); 66 | bits_copy(i, ¤t_key, 28, 57, 7); 67 | 68 | result = full_des_encode_block(current_key, block); 69 | if (result == encoded) { 70 | *key = current_key; 71 | *done = 1; 72 | break; 73 | } 74 | 75 | if (i % 1024 == 0 && (*done)==1) { 76 | break; 77 | } 78 | } 79 | } 80 | 81 | uint64_t calculate_limit(int key_length) { 82 | const int offset = 24; 83 | int input = key_length; 84 | input -= offset; 85 | int z = (input - 1) / 8 + 1; 86 | input -= z; 87 | 88 | uint64_t limit = 1; 89 | for (int i = 0; i < input; i++) { 90 | limit <<= 1; 91 | } 92 | //printf("%d -> %d, %u\n", key_length, input,limit); 93 | return limit; 94 | } 95 | 96 | void run_des_crack(uint64_t block, uint64_t encoded, int key_length, uint64_t *key) { 97 | uint64_t *dev_key; 98 | uint64_t key_val = 0; 99 | int *done; 100 | uint64_t limit = calculate_limit(key_length); 101 | 102 | // select device 103 | //_cudaSetDevice(0); 104 | //_cudaResizeStack(); 105 | // allocate memory 106 | _cudaMalloc((void**)&dev_key, sizeof(uint64_t)); 107 | _cudaMalloc((void**)&done,sizeof(int)); 108 | // copy values 109 | _cudaMemcpy(dev_key, &key_val, sizeof(uint64_t), cudaMemcpyHostToDevice); 110 | int done_value = 0; 111 | _cudaMemcpy(done,&done_value,sizeof(int),cudaMemcpyHostToDevice); 112 | 113 | //cuda_crack_des_kernel << <32678, 512 >> >(block, encoded, limit, dev_key, done); 114 | cuda_crack_des_kernel << <4096, 512 >> >(block, encoded, limit, dev_key, done); 115 | _cudaDeviceSynchronize("crack_des_kernel"); 116 | 117 | //_cudaMemcpy(&done_value,done,sizeof(int),cudaMemcpyDeviceToHost); 118 | //printf("done = %d\n",done_value); 119 | 120 | // copy result 121 | _cudaMemcpy(key, dev_key, sizeof(uint64_t), cudaMemcpyDeviceToHost); 122 | // free memory 123 | cudaFree(dev_key); 124 | } 125 | 126 | void run_des_encode_block(uint64_t key, uint64_t block, uint64_t *result) { 127 | uint64_t *dev_result; 128 | //_cudaSetDevice(0); 129 | _cudaMalloc((void**)&dev_result, sizeof(uint64_t)); 130 | 131 | cuda_des_encode_block<<<1,1>>>(block, key, dev_result); 132 | _cudaDeviceSynchronize("cuda_des_encode_block"); 133 | 134 | _cudaMemcpy(result, dev_result, sizeof(uint64_t), cudaMemcpyDeviceToHost); 135 | cudaFree(dev_result); 136 | } 137 | -------------------------------------------------------------------------------- /des_cuda/des_utils.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "des_consts.h" 8 | #include "bit_utils.h" 9 | #include "c_utils.h" 10 | #include 11 | 12 | uint64_t des_generate_key(); 13 | uint64_t des_generate_key_length(int key_length); 14 | void chop_into_blocks(char* data, int length, uint64_t **blocks, int *out_block_count); 15 | 16 | uint64_t des_generate_key() { 17 | srand(time(0)); 18 | uint64_t key = 0; 19 | for (int i = 0; i < 64; i++) { 20 | bits_set(&key, i, rand() % 64>32); 21 | } 22 | return key; 23 | } 24 | 25 | uint64_t des_generate_key_length(int key_length){ 26 | uint64_t key = des_generate_key(); 27 | int shift = 64-key_length+10; 28 | key = key << shift; 29 | key = key >> shift; 30 | return key; 31 | } 32 | 33 | void chop_into_blocks(char* data, int length, uint64_t **blocks, int *out_block_count) { 34 | double d = length / 8.0; 35 | int block_count = (int)(ceil(d)); 36 | char* filled = (char*)_malloc(block_count * 8); 37 | memset(filled, 0, block_count * 8); 38 | memcpy(filled, data, length); 39 | 40 | *blocks = (uint64_t*)_malloc(sizeof(uint64_t)*block_count); 41 | int offset = 0; 42 | for (int i = 0; i < block_count; i++) { 43 | *blocks[i] = ((uint64_t)data[0 + i*offset]) << 56 | 44 | ((uint64_t)data[1 + i*offset]) << 48 | 45 | ((uint64_t)data[2 + i*offset]) << 40 | 46 | ((uint64_t)data[3 + i*offset]) << 32 | 47 | ((uint64_t)data[4 + i*offset]) << 24 | 48 | ((uint64_t)data[5 + i*offset]) << 16 | 49 | ((uint64_t)data[6 + i*offset]) << 8 | 50 | ((uint64_t)data[7 + i*offset]) << 0; 51 | offset += 8; 52 | } 53 | *out_block_count = block_count; 54 | } 55 | -------------------------------------------------------------------------------- /des_cuda/kernel.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "device_launch_parameters.h" 3 | #include 4 | #include 5 | 6 | #include "c_utils.h" 7 | #include "des.h" 8 | #include "des_utils.h" 9 | #include "bit_utils.h" 10 | #include "des_consts.h" 11 | #include "des_kernel.h" 12 | #include "cuda_utils.h" 13 | 14 | 15 | void parse_args(int argc, char** argv, int *key_length); 16 | void usage(char* name); 17 | 18 | void parse_args(int argc, char** argv, int *key_length){ 19 | if (argc < 2){ 20 | usage(argv[0]); 21 | } 22 | *key_length = atoi(argv[1]); 23 | if (*key_length <=0 || *key_length>64){ 24 | usage(argv[0]); 25 | } 26 | } 27 | 28 | void usage(char* name){ 29 | printf("Usage:\n %s key_length(1-64)\n",name); 30 | exit(EXIT_FAILURE); 31 | } 32 | 33 | int main(int argc, char** argv) { 34 | int key_length; 35 | parse_args(argc,argv,&key_length); 36 | //printf("Key length: %d \n",key_length); 37 | uint64_t key = des_generate_key_length(key_length); 38 | uint64_t block = 0x0123456789ABCDEF; 39 | uint64_t encoded = full_des_encode_block(key, block); 40 | 41 | _cudaSetDevice(1); 42 | 43 | //printf("Real key:\n"); 44 | //bits_print_grouped(key, 8, 64); 45 | //printf("Cracking...\n"); 46 | uint64_t cracked_key = 0; 47 | 48 | clock_t start = clock(); 49 | run_des_crack(block, encoded, key_length, &cracked_key); 50 | clock_t end = clock(); 51 | float seconds = (float)(end - start) / CLOCKS_PER_SEC; 52 | printf("%d,%f\n", key_length,seconds); 53 | 54 | //printf("Cracked key:\n"); 55 | //bits_print_grouped(cracked_key, 8, 64); 56 | 57 | //bits_print_grouped(encoded,8,64); 58 | //bits_print_grouped(full_des_encode_block(cracked_key,block),8,64); 59 | return EXIT_SUCCESS; 60 | } 61 | -------------------------------------------------------------------------------- /des_cuda/makefile: -------------------------------------------------------------------------------- 1 | crack-des: *.cu *.h 2 | nvcc -o crack-des kernel.cu 3 | -------------------------------------------------------------------------------- /des_protocol.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "DES cuda brute force algorithm tests" 3 | author: "Maciej Grzeszczak" 4 | date: "May 13, 2017" 5 | output: pdf_document 6 | header-includes: 7 | - \usepackage{polski} 8 | --- 9 | 10 | ```{r setup, include=FALSE} 11 | knitr::opts_chunk$set(echo = TRUE) 12 | pdf.options(encoding = 'ISOLatin2') 13 | data <- read.csv('data.csv') 14 | data[,2] <- round(as.numeric(data[,2]),2) 15 | colnames(data) <- c('Key length','Time [s]') 16 | 17 | ``` 18 | 19 | ## Results 20 | ```{r echo=F, message=F, warning=F} 21 | library('knitr') 22 | kable(data) 23 | ``` 24 | 25 | ```{r echo=F, message=F, warning=F} 26 | data <- read.csv('data.csv') 27 | x <- as.numeric(data[,1]) 28 | y <- as.numeric(data[,2]) 29 | 30 | plot(x,y,xlab='Key length',ylab='Time [s]',type='l') 31 | ``` 32 | -------------------------------------------------------------------------------- /des_protocol.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mgrzeszczak/des-cuda/53f3eea1948d1f6170e1acc277c563f1c748aea0/des_protocol.pdf --------------------------------------------------------------------------------