random123

Counter-based Random Number Generators
git clone git://git.meso-star.com/random123.git
Log | Files | Refs | README | LICENSE

ut_aes.cpp (3995B)


      1 /*
      2 Copyright 2010-2011, D. E. Shaw Research.
      3 All rights reserved.
      4 
      5 Redistribution and use in source and binary forms, with or without
      6 modification, are permitted provided that the following conditions are
      7 met:
      8 
      9 * Redistributions of source code must retain the above copyright
     10   notice, this list of conditions, and the following disclaimer.
     11 
     12 * Redistributions in binary form must reproduce the above copyright
     13   notice, this list of conditions, and the following disclaimer in the
     14   documentation and/or other materials provided with the distribution.
     15 
     16 * Neither the name of D. E. Shaw Research nor the names of its
     17   contributors may be used to endorse or promote products derived from
     18   this software without specific prior written permission.
     19 
     20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31 */
     32 // Check our AES implementation against the example in FIPS-197
     33 
     34 #include <Random123/aes.h>
     35 #include <Random123/ReinterpretCtr.hpp>
     36 #if R123_USE_AES_OPENSSL
     37 #include <openssl/aes.h>
     38 #endif
     39 #include <string>
     40 #include <cstdio>
     41 #include <iostream>
     42 #include <cstring>
     43 #include <cassert>
     44 
     45 using namespace std;
     46 using namespace r123;
     47 
     48 #if !R123_USE_SSE
     49 int main(int, char **){
     50     std::cout << "No SSE support.  This test is not compiled\n";
     51     return 0;
     52 }
     53 #else
     54 
     55 #include "util_m128.h"
     56 
     57 int main(int, char **){
     58     r123array1xm128i IN, K;
     59 
     60     K.v[0].m =  m128i_from_charbuf("0001020304050607 08090a0b0c0d0e0f");
     61     IN.v[0].m = m128i_from_charbuf("0011223344556677 8899aabbccddeeff");
     62     // From FIPS-197, this is the official "right answer"
     63     r123array1xm128i right_answer;
     64     right_answer[0] = m128i_from_charbuf("69c4 e0d8 6a7b 0430 d8cd b780 70b4 c55a");
     65     (void)right_answer;  /* don't complain about an unused variable if neither NI nor OPENSSL are enabled. */
     66 #if R123_USE_AES_NI
     67     if( haveAESNI() ){
     68         AESNI1xm128i::key_type xk(K);
     69         AESNI1xm128i bx;
     70         AESNI1xm128i::ctr_type x = bx(IN, xk);
     71 
     72         assert( x==right_answer );
     73         cout << "IN: " << m128i_to_string(IN[0]) <<  "\n";
     74         cout << "K : " << m128i_to_string(K[0])  << "\n";
     75         cout << "AES:" << m128i_to_string(x[0])  << "\n";
     76         cout << "Hooray!  AESNI1xm128i(IN, K) matches the published test vector!\n";
     77     }else{
     78         cout << "The AES-NI instructions are not available on this hardware.  Skipping AES-NI tests\n";
     79     }
     80 #else
     81     cout << "The AES-NI Bijections are not compiled into this binary.  Skipping AES-NI tests\n";
     82 #endif
     83     
     84     // And let's do it with AESOpenSSL.  But since AESOpenSSL has its own
     85     // format for keys and counters we make a union for the key types and
     86     // use ReinterpretCtr to wrap a union around the counter types.
     87 #if R123_USE_AES_OPENSSL
     88 #if R123_USE_AES_NI
     89     typedef AESNI1xm128i::ctr_type nictype;
     90 #else
     91     typedef r123array1xm128i nictype;
     92 #endif
     93     AESOpenSSL16x8::ukey_type ouk;
     94     _mm_storeu_si128((__m128i*)&ouk.v[0], K.v[0].m);
     95     AESOpenSSL16x8::key_type okey(ouk);
     96     ReinterpretCtr<nictype, AESOpenSSL16x8> osslb;
     97     assert( osslb(IN, okey) == right_answer );
     98     cout << "Hooray!  AESOpenSSL16x8(IN, K) matches the published test vector!\n";
     99 #else
    100     cout << "The OpenSSL AES implementation is not linked with this binary.  Skipping the AESOpenSSL16x8\n";
    101 #endif // R123_USE_AES_OPENSSL
    102 
    103     return 0;
    104 }
    105 
    106 #endif