aes_test.c 20.5 KB
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
#define BCP_IPC
#include "pi_util.h"
#include "pi_tests.h"
#include "aes/pi_aes.h"
#define MAX_INPUT_BLOCKS 70


static unsigned char key[] = {
    0x00,0x10,0x20,0x30,
    0x40,0x50,0x60,0x30,
    0x80,0x90,0xa0,0xb0,
    0xc0,0xd0,0xe0,0xf0
};

static unsigned char iv[] = {
    0xaa,0xbb,0xcc,0xdd,
    0xaa,0xbb,0xcc,0xdd,
    0xaa,0xbb,0xcc,0xdd,
    0xaa,0xbb,0xcc,0xdd
};




/*
  Assume the AES controller is already not busy.
*/
int aesCheckClearIntr()
{
    u32 io;
    u32 ints, minte, mintem;

    io = IO_READ(PI_AES_CTRL_REG);
    if(io&PI_AES_CTRL_BUSY || !(io&PI_AES_CTRL_INTR)){
        _TRACE(DERROR, fprintf(LogFp,"Error: Expected PI_AES_CTRL_INTR.\n"));
        return FAIL;
    }

    /* check mi int and int1 */
    ints   = CHECK_INTERRUPT;
    minte  = IO_READ(MI_EINTR_REG);
    mintem = IO_READ(MI_INTR_EMASK_REG);

    if(!(minte&MI_INTR_AES)) {
        _TRACE(DERROR, fprintf(LogFp,"Error: Expected MI_INTR_AES.\n"));
        return FAIL;
    }

    if(mintem & MI_INTR_MASK_AES) {
        if(!(ints&2)) {
            _TRACE(DERROR, fprintf(LogFp,
                                   "Error: Expected INT1 to be set.\n"));
            return FAIL;
        }
    } else {
        if(ints&2) {
            _TRACE(DERROR, fprintf(LogFp,
                                   "Error: Did not expect INT1 to be set.\n"));
            return FAIL;
        }
    }

    /* we have the intr bit set, so try clearing */
    IO_WRITE(PI_AES_CTRL_REG,0);

    /* read back to check */
    io = IO_READ(PI_AES_CTRL_REG);
    ints   = CHECK_INTERRUPT;
    minte  = IO_READ(MI_EINTR_REG);

    if(io&(PI_AES_CTRL_INTR|PI_AES_CTRL_BUSY)) {
        _TRACE(DERROR, fprintf(LogFp,
                               "Error: Did not expect PI_AES_CTRL_INTR.\n"));
        return FAIL;
    }

    /* check MI intrs */
    if(minte&MI_INTR_AES) {
        _TRACE(DERROR, fprintf(LogFp,"Error: Did not expect MI_INTR_AES.\n"));
        return FAIL;
    }
    if(ints&2) {
        _TRACE(DERROR, fprintf(LogFp,
                               "Error: Did not expect INT1 to be set.\n"));
        return FAIL;
    }

    return PASS;
}

int aesCheckNoIntr()
{
    u32 io;
    u32 ints, minte, mintem;

    io = IO_READ(PI_AES_CTRL_REG);
    if((io&PI_AES_CTRL_BUSY) || io&PI_AES_CTRL_INTR) {
        _TRACE(DERROR, fprintf(LogFp,
                               "Error: Did not expect PI_AES_CTRL_INTR.\n"));
        return FAIL;
    }

    /* check mi int and int1 */
    ints   = CHECK_INTERRUPT;
    minte  = IO_READ(MI_EINTR_REG);
    mintem = IO_READ(MI_INTR_EMASK_REG);

    if(minte&MI_INTR_AES) {
        _TRACE(DERROR, fprintf(LogFp,"Error: Did not expect MI_INTR_AES.\n"));
        return FAIL;
    }

    if(ints&2) {
        _TRACE(DERROR, fprintf(LogFp,
                               "Error: Did not expect INT1 to be set.\n"
                               "ints = %08x, minte = %08x, mintem = %08x\n",
                                ints,minte,mintem));
        return FAIL;
    }

    return PASS;
}


/* 
   Test encryption of 16B data (minimal size). Data is always
   located at the beginning of the pi buffer, and initialization
   vector is always at the PI_AES_INIT.

   The key, init vector and input plaintext are randomly computed.
   It is assumed that srand() has been called with an appropriate
   seed before calling this function.

   arguments:
     intEnable - if non-zero, set PI_AES_CTRL[INTR] on start.
         otherwise, clear.

   returns PASS, FAIL or TESTERROR. PASS if decrypted result 
   matches input plaintext.

   To split the test into start and check phases, pass non-NULL
   values for rdecdata and rindata.

   To execute the entire test, pass NULL for rdecdata and rindata.

   If rdecdata and rindata are non-NULLL, the test is executed
   up to but not including the poll for aes busy.  decdata and
   indata are returned in *rencdata, *rdecdata.

   If the test is split into start and check phasse, call
   piAesTestRandomKeyDataIvCheck() to check completion.  Pass the
   rdecdata and rindata returned by piAesTestRandomKeyDataIv().
*/
int piAesTestRandomKeyDataIv(int intEnable,u32 ivIndex16,u32 dataIndex16,u32 size16,
                             u8 **rdecdata, u8 **rindata)
{
    u8 expkey[44*4],*encdata=NULL,*decdata=NULL,*indata=NULL;
    int i,j,result=PASS;

    if(rdecdata) *rdecdata = NULL;
    if(rindata)  *rindata  = NULL;

    if( (encdata = (u8 *)malloc(size16<<4)) == NULL ){
        result = TESTERROR;
        goto exit;
    }
    if( (decdata = (u8 *)malloc(size16<<4)) == NULL ){
        result = TESTERROR;
        goto exit;
    }
    if( (indata = (u8 *)malloc(size16<<4)) == NULL ){
        result = TESTERROR;
        goto exit;
    }
    /* compute and write random expanded key */
    for(i=0;i<16;i+=4){
        *((u32 *)(key+i))=rand();
    }
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp,"The 128-bit key:\n"));
    for(i=0;i<16;i++){ 
      _TRACE(DSTATUS,fprintf(LogFp," %02x",key[i]));
    }
    _TRACE(DSTATUS,fprintf(LogFp,"\n"));
#endif
    aesKeyExpand(key,expkey);
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp,"Writing expanded key to buffer... "));
    fflush(NULL);
#endif
    ioWriteBuffer(expkey,44*4,PI_AES_EKEY_REG);
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp," done.\n"));
#endif

    /* write init vector to buffer */
    for(i=0;i<16;i+=4){
        *((u32 *)(iv+i))=rand();
    }
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp,"The 128-bit init vector:\n"));
    for(i=0;i<16;i++)_TRACE(DSTATUS,fprintf(LogFp," %02x",iv[i]));
    _TRACE(DSTATUS,fprintf(LogFp,"\n"));
    _TRACE(DSTATUS,fprintf(LogFp,"Writing init vector to buffer... "));
#endif
    ioWriteBuffer(iv,16,PI_BUFFER_0_START+(ivIndex16<<4));
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp," done.\n"));
#endif
    /* write encrypted data to buffer */
    for(i=0;i<16*size16;i+=4){
        *((u32 *)(indata+i))=rand();
    }
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp,"The 128-bit input data (plain):\n"));
    for(j=0;j<size16;j++){
        for(i=0;i<16;i++)_TRACE(DSTATUS,fprintf(LogFp," %02x",indata[i+j*16]));
        _TRACE(DSTATUS,fprintf(LogFp,"\n"));
    }
#endif
    aesEncrypt(key,iv,indata,16*size16,encdata);
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp,"The 128-bit encrypted input data:\n"));
    for(j=0;j<size16;j++){
        for(i=0;i<16;i++)_TRACE(DSTATUS,fprintf(LogFp," %02x",encdata[i+j*16]));
        _TRACE(DSTATUS,fprintf(LogFp,"\n"));
    }
    _TRACE(DSTATUS,fprintf(LogFp,"Writing encrypted data to buffer... "));
    fflush(NULL);
#endif
    ioWriteBuffer(encdata,16*size16,PI_BUFFER_0_START+(dataIndex16<<4));
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp," done.\n"));
#endif

    /* decrypt */
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp,"AES decryption started... "));
    fflush(NULL);
#endif
    aesStart(dataIndex16,ivIndex16,size16,intEnable,0);
    if(encdata)free(encdata);
    if(rdecdata && rindata) {
        *rdecdata = decdata;
        *rindata  = indata;
        return result;
    }

    return piAesTestRandomKeyDataIvCheck(intEnable,dataIndex16,size16,decdata,indata);

 exit:

    if(encdata)free(encdata);
    if(decdata)free(decdata);
    if(indata)free(indata);
    return result;
}

int piAesTestRandomKeyDataIvCheck(int intEnable,u32 dataIndex16,u32 size16,u8 *decdata,u8 *indata)
{
    int i,j,result=PASS;

    POLL_AES_BUSY;
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp," done.\n"));
#endif

    /* check ints */
    if(intEnable && (aesCheckClearIntr()!=PASS)){
        result = FAIL;
        goto exit;
    }
    else if(aesCheckNoIntr()!=PASS){
        result = FAIL;
        goto exit;
    }

    /* read back */
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp,"Reading decrypted data from buffer... "));
    fflush(NULL);
#endif
    ioReadBuffer(decdata,16*size16,PI_BUFFER_0_START+(dataIndex16<<4));
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp," done.\n"));
    _TRACE(DSTATUS,fprintf(LogFp,"The decrypted data:\n"));
    for(j=0;j<size16;j++){
        for(i=0;i<16;i++)_TRACE(DSTATUS,fprintf(LogFp," %02x",decdata[i+j*16]));
        _TRACE(DSTATUS,fprintf(LogFp,"\n"));
    }
#endif    
    for(i=0;i<16*size16;i++){
        if(indata[i]!=decdata[i])
            result = FAIL;
    }

 exit:

    if(decdata)free(decdata);
    if(indata)free(indata);
    return result;
}



/* key length fixed 128 bits, every thing else comes from calling program,
   memory allocated by calling program */
/* arguments are interrupt enable, IV location, IV, data location, number of
blocks 128 bits of data, encrypted data pointer, decrypted data pointer, input data pointer with contents, key with contents
*/
int piAesTestFixedData(int intEnable,u32 ivIndex16,u8 *iv,u32 dataIndex16,u32 size16,u8 *indata, int isBufferOne, int hardwareChaining){
       u8 expkey[44*4],*encdata=NULL,*decdata=NULL;
    int i,j,result=PASS;

    if( (encdata = (u8 *)malloc(size16<<4)) == NULL ){
        result = TESTERROR;
        goto exit;
    }
    if( (decdata = (u8 *)malloc(size16<<4)) == NULL ){
        result = TESTERROR;
        goto exit;
    }

    /* compute and write random expanded key */
    for(i=0;i<16;i+=4){
        *((u32 *)(key+i))=rand();
    }
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp,"The 128-bit key:\n"));
    for(i=0;i<16;i++)_TRACE(DSTATUS,fprintf(LogFp," %02x",key[i]));
    _TRACE(DSTATUS,fprintf(LogFp,"\n"));
#endif
    aesKeyExpand(key,expkey);
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp,"Writing expanded key to buffer... "));
    fflush(NULL);
#endif
    ioWriteBuffer(expkey,44*4,PI_AES_EKEY_REG);
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp," done.\n"));
#endif

    if(hardwareChaining ==0){
#ifdef CHATTY
      _TRACE(DSTATUS,fprintf(LogFp,"The 128-bit init vector:\n"));
      for(i=0;i<16;i++)_TRACE(DSTATUS,fprintf(LogFp," %02x",iv[i]));
      _TRACE(DSTATUS,fprintf(LogFp,"\n"));
      _TRACE(DSTATUS,fprintf(LogFp,"Writing init vector to buffer... "));
      fflush(NULL);
#endif
      ioWriteBuffer(iv,16,PI_BUFFER_0_START+(ivIndex16<<4));
    }
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp," done.\n"));
#endif
   
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp,"The 128-bit input data (plain):\n"));
    for(j=0;j<size16;j++){
        for(i=0;i<16;i++)_TRACE(DSTATUS,fprintf(LogFp," %02x",indata[i+j*16]));
        _TRACE(DSTATUS,fprintf(LogFp,"\n"));
    }
#endif
    aesEncrypt(key,iv,indata,16*size16,encdata);

    /* save iv to send back incase hardware chaining is used next time 
       we need software to send it back to this call*/
    for(i=0;i<16;i++){
      iv[i]= encdata[16*size16 -16 +i];
    }

#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp,"The 128-bit encrypted input data:\n"));
    for(j=0;j<size16;j++){
        for(i=0;i<16;i++)_TRACE(DSTATUS,fprintf(LogFp," %02x",encdata[i+j*16]));
        _TRACE(DSTATUS,fprintf(LogFp,"\n"));
    }
    _TRACE(DSTATUS,fprintf(LogFp,"Writing encrypted data to buffer... "));
    fflush(NULL);
#endif
    if(isBufferOne == 0){
      _TRACE(DSTATUS,fprintf(LogFp,"Writing in buffer 0\n"));
      fflush(NULL);
      ioWriteBuffer(encdata,16*size16,PI_BUFFER_0_START+(dataIndex16<<4));
    }
    else{
      _TRACE(DSTATUS,fprintf(LogFp,"Writing in buffer 1\n"));
      fflush(NULL);
      ioWriteBuffer(encdata,16*size16,PI_BUFFER_1_START+(dataIndex16<<4));
    }
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp," done.\n"));
#endif

    /* decrypt */
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp,"AES decryption started... "));
    fflush(NULL);
#endif
    if(isBufferOne ==0){
      aesStart(dataIndex16,ivIndex16,size16,intEnable, hardwareChaining);
    }
    else{
      aesStart(dataIndex16 + 32, ivIndex16, size16, intEnable,hardwareChaining);
    }
    POLL_AES_BUSY;
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp," done.\n"));
#endif

    /* check ints */
    if(intEnable && (aesCheckClearIntr()!=PASS)){
        return FAIL;
    }
    else if(aesCheckNoIntr()!=PASS){
        return FAIL;
    }

    /* read back */
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp,"Reading decrypted data from buffer...at %d ",(dataIndex16<<4)));
    fflush(NULL);
#endif
    if(isBufferOne == 0){
      _TRACE(DSTATUS,fprintf(LogFp,"Reading in buffer 0\n"));
      fflush(NULL);
      ioReadBuffer(decdata,16*size16,PI_BUFFER_0_START+(dataIndex16<<4));
    }
    else{
      _TRACE(DSTATUS,fprintf(LogFp,"Reading in buffer 1\n"));
      fflush(NULL); 
      ioReadBuffer(decdata,16*size16,PI_BUFFER_1_START+(dataIndex16<<4));
    }
#ifdef CHATTY
    _TRACE(DSTATUS,fprintf(LogFp," done.\n"));
    _TRACE(DSTATUS,fprintf(LogFp,"The decrypted data:\n"));
    for(j=0;j<size16;j++){
        for(i=0;i<16;i++)_TRACE(DSTATUS,fprintf(LogFp," %02x",decdata[i+j*16]));
        _TRACE(DSTATUS,fprintf(LogFp,"\n"));
    }
#endif    
    for(i=0;i<16*size16;i++){
        if(indata[i]!=decdata[i])
            result = FAIL;
    }

 exit:

    if(encdata)free(encdata);
    if(decdata)free(decdata);
   
    return result;
}



int piAesTestStopStart()
{
    u8 expkey[44*4],iv[16];
    u8 indata[512],encdata[512],decdata[512],key[16];
    int i,j,result=PASS;
    u32 size16=32;
    u32 ivIndex16=34;
    u32 dataIndex16=0;

    /* don't really care what iv and indata are for this test */
    for(i=0;i<16;i++)
        iv[i]=i;
    for(i=0;i<512;i++)
        indata[i]=i;

    /* compute and write random expanded key */
    for(i=0;i<16;i+=4){
        *((u32 *)(key+i))=rand();
    }
    aesKeyExpand(key,expkey);

    ioWriteBuffer(expkey,44*4,PI_AES_EKEY_REG);

    ioWriteBuffer(iv,16,PI_BUFFER_0_START+(ivIndex16<<4));

    aesEncrypt(key,iv,indata,16*size16,encdata);

    _TRACE(DSTATUS,fprintf(LogFp,"Writing in buffer 0\n"));
    fflush(NULL);
    ioWriteBuffer(encdata,16*size16,PI_BUFFER_0_START+(dataIndex16<<4));

    /* decrypt */
    aesStart(dataIndex16,ivIndex16,size16,0, 0);

    /* insure started, then stop */
    if(!(IO_READ(PI_AES_CTRL_REG)&PI_AES_CTRL_BUSY)){
        _TRACE(DERROR,fprintf(LogFp," failed to start controller\n"));
        result=FAIL;
        goto exit;
    }
    IO_WRITE(PI_AES_CTRL_REG,0);

    /* insure stopped */
    if((IO_READ(PI_AES_CTRL_REG)&PI_AES_CTRL_BUSY)){
        _TRACE(DERROR,fprintf(LogFp," failed to stop controller\n"));
        result=FAIL;
        goto exit;
    }

    /* start a new decryption */
    size16=1;    
    ioWriteBuffer(encdata,16*size16,PI_BUFFER_0_START+(dataIndex16<<4));
    aesStart(dataIndex16,ivIndex16,size16,0, 0);
    POLL_AES_BUSY;

    /* read back */
    _TRACE(DSTATUS,fprintf(LogFp,"Reading in buffer 0\n"));
    fflush(NULL);
    ioReadBuffer(decdata,16*size16,PI_BUFFER_0_START+(dataIndex16<<4));

    for(i=0;i<16*size16;i++){
        if(indata[i]!=decdata[i]){
            result = FAIL;
            goto exit;
        }
    }

 exit:

    return result;
}


void piAesTests()
{

/*listing of tests */

/* Test 1: Random Tests run 10 times */
    unsigned int seed;
    int i, ret, errCount;
    u8 *indata;
    unsigned char key[16];
    unsigned char iv[16];
    u32 mintme;
    int result = PASS;
    int offset =0;
    int num_blocks =0;

    printf("TEST 1\n");
    
    IO_WRITE(MI_INTR_EMASK_REG, MI_INTR_MASK_SET_FLASH);
    
    /* TODO: set max on loop to appropriate number of tests */
    for(i=0;i<10;i++){
        printf("AESRandomKeyDataIv %d ",i);
        
        ret = piAesTestRandomKeyDataIv(0,PI_AES_INIT_INDX16,1,2,NULL,NULL);
        OUTPUT_TEST_PASSFAIL(ret);
    }
    /* with interrupt*/
    for(i=0;i<10;i++){
        printf("AESRandomKeyDataIv %d ",i);
        
        ret = piAesTestRandomKeyDataIv(1,PI_AES_INIT_INDX16,1,2,NULL,NULL);
        OUTPUT_TEST_PASSFAIL(ret);
    }

    if (!((mintme=IO_READ(MI_INTR_EMASK_REG)) & MI_INTR_MASK_FLASH)) {
        printf("aesTest1: expected MI_INTR_MASK_FLASH set: %08x    ", mintme);
        OUTPUT_TEST_PASSFAIL(FAIL);
        fflush(NULL);
    }
    IO_WRITE(MI_INTR_EMASK_REG, MI_INTR_MASK_CLR_FLASH);



/* Test 2: call with fixed data, random keys, more than one block*/
    /* data = ff ff ... */
    /* initialize and call piAesTestFixedData */
    num_blocks=2;
       
    if( (indata = (u8 *)malloc(MAX_INPUT_BLOCKS<<4)) == NULL ){
        result = TESTERROR;
        OUTPUT_TEST_PASSFAIL(result);
        return;
    }


    /*all xff data*/
    for(i=0;i<16*num_blocks;i+=4){
      *((u32 *)(indata+i))=0xffffffff;
    }

    /* write init vector to buffer */
    for(i=0;i<16;i+=4){
        *((u32 *)(iv+i))=rand();
    }
    printf("TEST 2\n");
     
    ret = piAesTestFixedData(0,PI_AES_INIT_INDX16,iv,1,num_blocks,indata,0,0);
    OUTPUT_TEST_PASSFAIL(ret);

    /* TEST 3: data = 0xff00ff00.... 3 blocks*/
    
    num_blocks = 3;
    /*all xff data*/
    
    for(i=0;i<16*num_blocks;i+=4){
      *((u32 *)(indata+i))=0xff00ff00;
    }
    printf("TEST 3\n");
     
    ret = piAesTestFixedData(1,PI_AES_INIT_INDX16,iv,1,num_blocks,indata,0,0);
    OUTPUT_TEST_PASSFAIL(ret);

    if ((mintme=IO_READ(MI_INTR_EMASK_REG)) & MI_INTR_MASK_FLASH) {
        printf("aesTest3: expected MI_INTR_MASK_FLASH clr: %08x    ", mintme);
        OUTPUT_TEST_PASSFAIL(FAIL);
        fflush(NULL);
    }
    
    IO_WRITE(MI_INTR_EMASK_REG, MI_INTR_MASK_SET_FLASH);
    
    for(i=0;i<16*num_blocks;i+=4){
      *((u32 *)(indata+i))=0xffffffff;
    }
    
    
    /*Test 4: Vary offset, keep IV and key locations same, move upto end,
     data in data buffer 0, random input*/
    
    num_blocks =1;
    for(i=0;i<16*num_blocks;i+=4){
      *((u32 *)(indata+i))=0xffffffff;
    }
    printf("TEST 4\n");
     
    for(offset = 0; offset < 32; offset++){
      ret = piAesTestFixedData(0,PI_AES_INIT_INDX16,iv,offset,num_blocks,indata, 0, 0);
      OUTPUT_TEST_PASSFAIL(ret);
    }
    
    /*Test 5: Vary offset, keep IV and key locations same, move upto end,
     data in data buffer 1*/
    
    printf("TEST 5\n");
     
    for(offset = 0; offset < 32; offset++){
      ret = piAesTestFixedData(1,PI_AES_INIT_INDX16,iv,offset,num_blocks,indata,  1,0);
      OUTPUT_TEST_PASSFAIL(ret);
    }
    
    /*Test 6: Vary size, offset beginning, upto end, data buffer 0*/

    printf("TEST 6\n");
     
    offset =0;
    for(num_blocks = 1; num_blocks <= 64; num_blocks++){
 
      for(i=0;i<16*num_blocks;i+=4){
	*((u32 *)(indata+i))=0xff00ff00;
      }
      ret = piAesTestFixedData(1,PI_AES_INIT_INDX16,iv,offset,num_blocks,indata,  0,0);
      OUTPUT_TEST_PASSFAIL(ret);
    }
    
    /*Test 7: Vary size, offset fixed, upto end, data buffer 1*/
    printf("TEST 7\n");
     
    offset =16;
    for(num_blocks = 1; num_blocks <= 16; num_blocks++){
       
      for(i=0;i<16*num_blocks;i+=4){
	*((u32 *)(indata+i))=0xff00ff00;
      }
      ret = piAesTestFixedData(0,PI_AES_INIT_INDX16,iv,offset,num_blocks,indata,  1,0);
      OUTPUT_TEST_PASSFAIL(ret);
    }
    
    /*Test 8: Vary IV location, every thing else fixed,data immediately
before and after */
    num_blocks =2;
    printf("TEST 8\n");
     
    for(i=0;i<16*num_blocks;i+=4){
      *((u32 *)(indata+i))=0xff00ff00;
    }
    /*IV followed by data */
    ret = piAesTestFixedData(0, 8, iv, 9, num_blocks, indata, 0, 0);
    OUTPUT_TEST_PASSFAIL(ret);

    /*data followed by IV*/
    ret = piAesTestFixedData(0, 9, iv, 7, num_blocks, indata, 0, 0);
    OUTPUT_TEST_PASSFAIL(ret);

    /*Test 9: test hardware chaining */
    num_blocks =2;
    printf("TEST 9\n");
     
    for(i=0;i<16*num_blocks;i+=4){
      *((u32 *)(indata+i))=0xff00ff00;
    }
    /*IV followed by data: first block */
    ret = piAesTestFixedData(0, 8, iv, 9, 2, indata, 0, 0);
    OUTPUT_TEST_PASSFAIL(ret);

    /*second block data, use previous IV to send for software encryption, but
     the function does not send it to the hardware. hardware keeps state from
    previous decryption*/
    ret = piAesTestFixedData(1, 0, iv, 9, 2, indata, 0, 1);
    OUTPUT_TEST_PASSFAIL(ret);

    /* toggle hardware chaining back*/
    
    ret = piAesTestFixedData(0, 0, iv, 9, 2, indata, 0, 0);
    OUTPUT_TEST_PASSFAIL(ret);

    if (!((mintme=IO_READ(MI_INTR_EMASK_REG)) & MI_INTR_MASK_FLASH)) {
        printf("aesTest1: expected MI_INTR_MASK_FLASH set: %08x    ", mintme);
        OUTPUT_TEST_PASSFAIL(FAIL);
        fflush(NULL);
    }

    /* Test 10: start engine, stop before complete, start another
     *          and verify it finishes.
     */
    printf("TEST 10: insure can stop controller\n");
    ret = piAesTestStopStart();
    printf("TEST 10 result: ");
    OUTPUT_TEST_PASSFAIL(ret);

    if(indata)free(indata);
}


void aesGateLevel()
{
    u8 indata[16];
    int i,ret;

    _TRACE(DLOG, fprintf(LogFp,"Running gate-levl aes Test.\n"));
    fflush(NULL);

    for(i=0;i<16;i+=4){
	*((u32 *)(indata+i))=rand();
    }

    ret = piAesTestFixedData(
        0 /* intEnable */,
        PI_AES_INIT_INDX16,
        iv,
        1 /* dataIndex16 */,
        1 /* size16 */,
        indata, 
        0 /* isBufferOne */, 
        0 /* hardwareChaining */
        );

    printf("aesGateLevel ");
    OUTPUT_TEST_PASSFAIL(ret);
}