Ignore:
Timestamp:
Dec 15, 2010, 10:42:18 PM (11 years ago)
Author:
Sven Rech
Message:

keysearcher external client

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPlugins/KeySearcher/ExternalClient/Cryptool.cpp

    r2207 r2212  
    1 /* ============================================================
    2 
    3 Copyright (c) 2009 Advanced Micro Devices, Inc.  All rights reserved.
    4  
    5 Redistribution and use of this material is permitted under the following
    6 conditions:
    7  
    8 Redistributions must retain the above copyright notice and all terms of this
    9 license.
    10  
    11 In no event shall anyone redistributing or accessing or using this material
    12 commence or participate in any arbitration or legal action relating to this
    13 material against Advanced Micro Devices, Inc. or any copyright holders or
    14 contributors. The foregoing shall survive any expiration or termination of
    15 this license or any agreement or access or use related to this material.
    16 
    17 ANY BREACH OF ANY TERM OF THIS LICENSE SHALL RESULT IN THE IMMEDIATE REVOCATION
    18 OF ALL RIGHTS TO REDISTRIBUTE, ACCESS OR USE THIS MATERIAL.
    19 
    20 THIS MATERIAL IS PROVIDED BY ADVANCED MICRO DEVICES, INC. AND ANY COPYRIGHT
    21 HOLDERS AND CONTRIBUTORS "AS IS" IN ITS CURRENT CONDITION AND WITHOUT ANY
    22 REPRESENTATIONS, GUARANTEE, OR WARRANTY OF ANY KIND OR IN ANY WAY RELATED TO
    23 SUPPORT, INDEMNITY, ERROR FREE OR UNINTERRUPTED OPERA TION, OR THAT IT IS FREE
    24 FROM DEFECTS OR VIRUSES.  ALL OBLIGATIONS ARE HEREBY DISCLAIMED - WHETHER
    25 EXPRESS, IMPLIED, OR STATUTORY - INCLUDING, BUT NOT LIMITED TO, ANY IMPLIED
    26 WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
    27 ACCURACY, COMPLETENESS, OPERABILITY, QUALITY OF SERVICE, OR NON-INFRINGEMENT.
    28 IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. OR ANY COPYRIGHT HOLDERS OR
    29 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, PUNITIVE,
    30 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
    31 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, REVENUE, DATA, OR PROFITS; OR
    32 BUSINESS INTERRUPTION) HOWEVER CAUSED OR BASED ON ANY THEORY OF LIABILITY
    33 ARISING IN ANY WAY RELATED TO THIS MATERIAL, EVEN IF ADVISED OF THE POSSIBILITY
    34 OF SUCH DAMAGE. THE ENTIRE AND AGGREGATE LIABILITY OF ADVANCED MICRO DEVICES,
    35 INC. AND ANY COPYRIGHT HOLDERS AND CONTRIBUTORS SHALL NOT EXCEED TEN DOLLARS
    36 (US $10.00). ANYONE REDISTRIBUTING OR ACCESSING OR USING THIS MATERIAL ACCEPTS
    37 THIS ALLOCATION OF RISK AND AGREES TO RELEASE ADVANCED MICRO DEVICES, INC. AND
    38 ANY COPYRIGHT HOLDERS AND CONTRIBUTORS FROM ANY AND ALL LIABILITIES,
    39 OBLIGATIONS, CLAIMS, OR DEMANDS IN EXCESS OF TEN DOLLARS (US $10.00). THE
    40 FOREGOING ARE ESSENTIAL TERMS OF THIS LICENSE AND, IF ANY OF THESE TERMS ARE
    41 CONSTRUED AS UNENFORCEABLE, FAIL IN ESSENTIAL PURPOSE, OR BECOME VOID OR
    42 DETRIMENTAL TO ADVANCED MICRO DEVICES, INC. OR ANY COPYRIGHT HOLDERS OR
    43 CONTRIBUTORS FOR ANY REASON, THEN ALL RIGHTS TO REDISTRIBUTE, ACCESS OR USE
    44 THIS MATERIAL SHALL TERMINATE IMMEDIATELY. MOREOVER, THE FOREGOING SHALL
    45 SURVIVE ANY EXPIRATION OR TERMINATION OF THIS LICENSE OR ANY AGREEMENT OR
    46 ACCESS OR USE RELATED TO THIS MATERIAL.
    47 
    48 NOTICE IS HEREBY PROVIDED, AND BY REDISTRIBUTING OR ACCESSING OR USING THIS
    49 MATERIAL SUCH NOTICE IS ACKNOWLEDGED, THAT THIS MATERIAL MAY BE SUBJECT TO
    50 RESTRICTIONS UNDER THE LAWS AND REGULATIONS OF THE UNITED STATES OR OTHER
    51 COUNTRIES, WHICH INCLUDE BUT ARE NOT LIMITED TO, U.S. EXPORT CONTROL LAWS SUCH
    52 AS THE EXPORT ADMINISTRATION REGULATIONS AND NATIONAL SECURITY CONTROLS AS
    53 DEFINED THEREUNDER, AS WELL AS STATE DEPARTMENT CONTROLS UNDER THE U.S.
    54 MUNITIONS LIST. THIS MATERIAL MAY NOT BE USED, RELEASED, TRANSFERRED, IMPORTED,
    55 EXPORTED AND/OR RE-EXPORTED IN ANY MANNER PROHIBITED UNDER ANY APPLICABLE LAWS,
    56 INCLUDING U.S. EXPORT CONTROL LAWS REGARDING SPECIFICALLY DESIGNATED PERSONS,
    57 COUNTRIES AND NATIONALS OF COUNTRIES SUBJECT TO NATIONAL SECURITY CONTROLS.
    58 MOREOVER, THE FOREGOING SHALL SURVIVE ANY EXPIRATION OR TERMINATION OF ANY
    59 LICENSE OR AGREEMENT OR ACCESS OR USE RELATED TO THIS MATERIAL.
    60 
    61 NOTICE REGARDING THE U.S. GOVERNMENT AND DOD AGENCIES: This material is
    62 provided with "RESTRICTED RIGHTS" and/or "LIMITED RIGHTS" as applicable to
    63 computer software and technical data, respectively. Use, duplication,
    64 distribution or disclosure by the U.S. Government and/or DOD agencies is
    65 subject to the full extent of restrictions in all applicable regulations,
    66 including those found at FAR52.227 and DFARS252.227 et seq. and any successor
    67 regulations thereof. Use of this material by the U.S. Government and/or DOD
    68 agencies is acknowledgment of the proprietary rights of any copyright holders
    69 and contributors, including those of Advanced Micro Devices, Inc., as well as
    70 the provisions of FAR52.227-14 through 23 regarding privately developed and/or
    71 commercial computer software.
    72 
    73 This license forms the entire agreement regarding the subject matter hereof and
    74 supersedes all proposals and prior discussions and writings between the parties
    75 with respect thereto. This license does not affect any ownership, rights, title,
    76 or interest in, or relating to, this material. No terms of this license can be
    77 modified or waived, and no breach of this license can be excused, unless done
    78 so in a writing signed by all affected parties. Each term of this license is
    79 separately enforceable. If any term of this license is determined to be or
    80 becomes unenforceable or illegal, such term shall be reformed to the minimum
    81 extent necessary in order for this license to remain in effect in accordance
    82 with its terms as modified by such reformation. This license shall be governed
    83 by and construed in accordance with the laws of the State of Texas without
    84 regard to rules on conflicts of law of any state or jurisdiction or the United
    85 Nations Convention on the International Sale of Goods. All disputes arising out
    86 of this license shall be subject to the jurisdiction of the federal and state
    87 courts in Austin, Texas, and all defenses are hereby waived concerning personal
    88 jurisdiction and venue of these courts.
    89 
    90 ============================================================ */
    91 
    92 
    93 //
    94 // Copyright (c) 2009 Advanced Micro Devices, Inc. All rights reserved.
    95 //
    96 
    971#include <cstdio>
    982#include <cstdlib>
     
    12024    {
    12125        std::cerr << "Platform::get() failed (" << err << ")" << std::endl;
     26        throw std::exception();
     27    }
     28    if (platforms.size() == 0)
     29    {
     30        std::cerr << "No platforms available!" << std::endl;
    12231        throw std::exception();
    12332    }
     
    241150
    242151    // key
    243     cl::Buffer keybuffer = cl::Buffer(*context, CL_MEM_READ_ONLY, j.KeySize, NULL, &err);
     152    cl::Buffer keybuffer = cl::Buffer(*context, CL_MEM_READ_ONLY, j.KeySize*sizeof(float), NULL, &err);
    244153    if(err != CL_SUCCESS)
    245154    {
     
    248157    }
    249158
    250     err = queue.enqueueWriteBuffer(keybuffer, 1, 0, j.KeySize, j.Key);
    251 
     159    err = queue.enqueueWriteBuffer(keybuffer, 1, 0, j.KeySize*sizeof(float), j.Key);
    252160    if(err != CL_SUCCESS)
    253161    {
     
    256164    }
    257165
    258     err = kernel->setArg(0, keybuffer);
    259     if (err != CL_SUCCESS) {
    260         std::cerr << "Kernel::setArg() failed (" << err << ")\n";
    261         throw new std::exception();
    262     }
    263 
    264166    // results
    265     cl::Buffer entropies = cl::Buffer(*context, CL_MEM_WRITE_ONLY, sizeof(float)*j.ResultSize, NULL, &err);
     167    cl::Buffer costs = cl::Buffer(*context, CL_MEM_WRITE_ONLY, sizeof(float)*j.ResultSize, NULL, &err);
    266168
    267169    if(err != CL_SUCCESS)
    268170    {
    269171        std::cerr << "Failed allocate to entropybuffer(" << err << ")\n";
    270         throw new std::exception();
    271     }
    272 
    273     err = kernel->setArg(1, entropies);
    274 
    275     if (err != CL_SUCCESS) {
    276         std::cerr << "Kernel::setArg() failed (" << err << ")\n";
    277172        throw new std::exception();
    278173    }
     
    280175    //execute:
    281176    std::cout<<"Running CL program\n";
    282     err = queue.enqueueNDRangeKernel(
    283         *kernel, cl::NullRange, cl::NDRange(4, 4), cl::NDRange(2, 2)
    284     );
    285 
    286     if (err != CL_SUCCESS) {
    287         std::cerr << "CommandQueue::enqueueNDRangeKernel()" \
    288             " failed (" << err << ")\n";
    289         throw new std::exception();
    290     }
     177    enqueueKernel(queue, j.Size, keybuffer, costs);
    291178
    292179    err = queue.finish();
     
    296183    }
    297184
    298     std::cout<<"Done\nPassed!\n";
    299 
    300     float* localEntropy = new float[j.ResultSize];
    301 
    302     queue.enqueueReadBuffer(entropies, 1, 0, sizeof(float)*j.ResultSize, localEntropy);
     185    std::cout<<"Done Passed!\n";
     186
     187    float* localCosts = new float[j.ResultSize];
     188
     189    queue.enqueueReadBuffer(costs, 1, 0, sizeof(float)*j.ResultSize, localCosts);
    303190
    304191    //check results:
     
    309196    for(int i=0; i<j.Size; ++i)
    310197    {
    311         std::list<std::pair<float, int> >::iterator it = isInTop(res.ResultList, localEntropy[i], j.LargerThen);
     198std::cout << localCosts[i] << std::endl;
     199        std::list<std::pair<float, int> >::iterator it = isInTop(res.ResultList, localCosts[i], j.LargerThen);
    312200        if (it != res.ResultList.end())
    313             pushInTop(res.ResultList, it, localEntropy[i], i);
     201            pushInTop(res.ResultList, it, localCosts[i], i);
    314202    }
    315203
     
    317205}
    318206
    319 
     207const int subbatch = 256;
     208
     209void Cryptool::enqueueSubbatch(cl::CommandQueue& queue, cl::Buffer& keybuffer, cl::Buffer& costs, cl::NDRange offset, cl::NDRange worksize)
     210{
     211        cl_int err;
     212
     213        err = kernel->setArg(0, keybuffer);
     214        if (err != CL_SUCCESS) {
     215                std::cerr << "Kernel::setArg() failed (" << err << ")\n";
     216                throw new std::exception();
     217        }
     218
     219        err = kernel->setArg(1, costs);
     220        if (err != CL_SUCCESS) {
     221                std::cerr << "Kernel::setArg() failed (" << err << ")\n";
     222                throw new std::exception();
     223        }
     224
     225        err = kernel->setArg(2, 0);
     226        if (err != CL_SUCCESS) {
     227                std::cerr << "Kernel::setArg() failed (" << err << ")\n";
     228                throw new std::exception();
     229        }
     230
     231        err = queue.enqueueNDRangeKernel(*kernel, offset, worksize, cl::NullRange);
     232
     233        if (err != CL_SUCCESS) {
     234                std::cerr << "CommandQueue::enqueueNDRangeKernel()" \
     235                    " failed (" << err << ")\n";
     236                throw new std::exception();
     237        }
     238
     239        err = queue.finish();
     240        if (err != CL_SUCCESS) {
     241                std::cerr << "Event::wait() failed (" << err << ")\n";
     242                throw new std::exception();
     243        }
     244}
     245
     246void Cryptool::enqueueKernel(cl::CommandQueue& queue, int size, cl::Buffer& keybuffer, cl::Buffer& costs)
     247{
     248    for (int i = 0; i < (size/subbatch); i++)
     249    {
     250        enqueueSubbatch(queue, keybuffer, costs, cl::NDRange(i*subbatch), cl::NDRange(subbatch));
     251        std::cout << i << " von " << (size/subbatch) << std::endl;
     252    }
     253
     254    int remain = (size%subbatch);
     255    if (remain != 0)
     256    {
     257        enqueueSubbatch(queue, keybuffer, costs, cl::NDRange(size-remain), cl::NDRange(remain));
     258    }
     259}
    320260
    321261void Cryptool::pushInTop(std::list<std::pair<float, int> >& top, std::list<std::pair<float, int> >::iterator it, float val, int k) {
Note: See TracChangeset for help on using the changeset viewer.