-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathbf_opencl.c
78 lines (66 loc) · 2.49 KB
/
bf_opencl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// vim:ft=opencl:
/**
* @version 0.1.3 (2011)
* @author Johannes Gilger <[email protected]>
*
* Copyright 2011 Johannes Gilger
*
* This file is part of engine-cuda
*
* engine-cuda is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License or
* any later version.
*
* engine-cuda 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with engine-cuda. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <CL/opencl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <openssl/blowfish.h>
#include <sys/time.h>
#include "common.h"
#include "opencl_common.h"
static cl_mem bf_iv = NULL;
void BF_opencl_transfer_key_schedule(BF_KEY *ks,cl_mem *device_schedule,cl_command_queue queue) {
assert(ks);
clEnqueueWriteBuffer(queue,*device_schedule,CL_TRUE,0,sizeof(BF_KEY),ks,0,NULL,NULL);
}
void BF_opencl_transfer_iv(cl_context context, const unsigned char *iv,cl_command_queue queue) {
cl_int error;
if(!bf_iv)
CL_ASSIGN(bf_iv = clCreateBuffer(context,CL_MEM_READ_ONLY,BF_BLOCK_SIZE,NULL,&error));
CL_WRAPPER(clEnqueueWriteBuffer(queue,bf_iv,CL_TRUE,0,BF_BLOCK_SIZE,iv,0,NULL,NULL));
}
void BF_opencl_crypt(opencl_crypt_parameters *c) {
size_t blockSize[3] = {MAX_THREAD, 0, 0};
size_t gridSize[3] = {c->nbytes/BF_BLOCK_SIZE, 0, 0};
if (!(c->nbytes%(BF_BLOCK_SIZE*MAX_THREAD))==0) {
gridSize[0] += (MAX_THREAD - (gridSize[0]%MAX_THREAD));
}
clSetKernelArg(*c->d_kernel, 0, sizeof(cl_mem), c->d_in);
clSetKernelArg(*c->d_kernel, 1, sizeof(cl_mem), c->d_schedule);
cl_uint args;
clGetKernelInfo(*c->d_kernel,CL_KERNEL_NUM_ARGS,4,&args,NULL);
if(args > 3 && bf_iv) {
clSetKernelArg(*c->d_kernel, 2, sizeof(cl_mem), &bf_iv);
clSetKernelArg(*c->d_kernel, 3, sizeof(cl_mem), c->d_out);
}
clEnqueueWriteBuffer(*c->queue,*c->d_in,CL_FALSE,0,c->nbytes,c->in,0,NULL,NULL);
OPENCL_TIME_KERNEL("BF ",1)
if(args > 3) {
clEnqueueReadBuffer(*c->queue,*c->d_out,CL_FALSE,0,c->nbytes,c->out,0,NULL,NULL);
BF_opencl_transfer_iv(*c->context,c->in+c->nbytes-BF_BLOCK_SIZE,*c->queue);
} else {
clEnqueueReadBuffer(*c->queue,*c->d_in,CL_TRUE,0,c->nbytes,c->out,0,NULL,NULL);
}
}