source: trunk/CrypPlugins/KeySearcher/OpenCL/KeySearcherOpenCLCode.cs @ 2202

Last change on this file since 2202 was 2202, checked in by Sven Rech, 11 years ago

Added code for using keysearcher with external client.

External client not available yet.

File size: 3.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Cryptool.PluginBase;
6using Cryptool.PluginBase.Control;
7using OpenCLNet;
8
9namespace KeySearcher
10{
11    class KeySearcherOpenCLCode
12    {
13        private readonly KeySearcher keySearcher;
14        private byte[] encryptedData;
15        private IControlCost controlCost;
16        private IControlEncryption encryptionController;
17        private int approximateNumberOfKeys;
18
19        private IKeyTranslator keyTranslatorOfCode = null;
20        private string openCLCode = null;
21        private Kernel openCLKernel = null;
22
23        public KeySearcherOpenCLCode(KeySearcher keySearcher, byte[] encryptedData, IControlEncryption encryptionController, IControlCost controlCost, int approximateNumberOfKeys)
24        {
25            this.keySearcher = keySearcher;
26            this.encryptedData = encryptedData;
27            this.encryptionController = encryptionController;
28            this.controlCost = controlCost;
29            this.approximateNumberOfKeys = approximateNumberOfKeys;
30        }
31
32        public string CreateOpenCLBruteForceCode(IKeyTranslator keyTranslator)
33        {
34            if (keyTranslatorOfCode == keyTranslator)
35            {
36                return openCLCode;
37            }
38
39            int bytesUsed = controlCost.getBytesToUse();
40            if (encryptedData.Length < bytesUsed)
41                bytesUsed = encryptedData.Length;
42
43            string code = encryptionController.GetOpenCLCode(bytesUsed);
44            if (code == null)
45                throw new Exception("OpenCL not supported in this configuration!");
46
47            //put cost function stuff into code:
48            code = controlCost.ModifyOpenCLCode(code);
49
50            //put input to be bruteforced into code:
51            string inputarray = string.Format("__constant unsigned char inn[{0}] = {{ \n", bytesUsed);
52            for (int i = 0; i < bytesUsed; i++)
53            {
54                inputarray += String.Format("0x{0:X2}, ", this.encryptedData[i]);
55            }
56            inputarray = inputarray.Substring(0, inputarray.Length - 2);
57            inputarray += "}; \n";
58            code = code.Replace("$$INPUTARRAY$$", inputarray);
59
60            //put key movement of pattern into code:
61            code = keyTranslator.ModifyOpenCLCode(code, approximateNumberOfKeys);
62
63            keyTranslatorOfCode = keyTranslator;
64            this.openCLCode = code;
65           
66            return code;
67        }
68
69
70        public Kernel GetBruteforceKernel(OpenCLManager oclManager, IKeyTranslator keyTranslator)
71        {
72            if (keyTranslatorOfCode == keyTranslator)
73            {
74                return openCLKernel;
75            }
76
77            try
78            {
79                var program = oclManager.CompileSource(CreateOpenCLBruteForceCode(keyTranslator));
80                //keySearcher.GuiLogMessage(string.Format("Using OpenCL with (virtually) {0} threads.", keyTranslator.GetOpenCLBatchSize()), NotificationLevel.Info);
81                openCLKernel = program.CreateKernel("bruteforceKernel");
82                return openCLKernel;
83            }
84            catch (Exception ex)
85            {
86                throw new Exception("An error occured when trying to compile OpenCL code: " + ex.Message);
87            }
88        }
89    }
90}
Note: See TracBrowser for help on using the repository browser.