/*
 *               Copyright (C) 2006, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and are protected by Federal copyright law. They may not be disclosed
 *  to third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 * $Id: sha.c,v 1.1 2006/09/29 22:37:48 achan Exp $
 */


#include <types.h>
#include <ioslibc.h>
#include <iop.h>
#include <sys.h>
#include <mmu.h>
#include <diag/proto.h>
#include <diag/aes_sw.h>
#include <diag/sha1.h>
#include <diag/sha_sw.h>
#include <addr_map.h>


#define DATA_START          IOP_GDDR3_START
#define DATA_SIZE           (64 * 1024)

#define SHA_BLOCK_SZ_SHIFT  6
#define HASH_RESULT_SIZE    20

/* Device control */
#define WAIT(dev)           while (IO_READ(dev##_CTRL_REG) & dev##_CTRL_EXEC)
#define FLUSH(dev)          ahbMemFlush(IOS_WB_##dev)
#define INVAL(dev)          ahbMemRBInvalidate(IOS_RB_##dev)


#define STACK_SIZE          (2 * 1024)
const u8    _initStack[STACK_SIZE];
const u32   _initStackSize = sizeof(_initStack);
const u32   _initPriority = 10;


void
fillBufFromRandom(u32 *data, u32 size)
{
    u32 count, value;

    value = 0;
    for (count = 0; count < (size / 4); count++) {
        value = (value * 1187) - 903;
        data[count] = value;
    }
}

void
hashCheck(u8 *data, u32 len)
{
    u8      hashHw[HASH_RESULT_SIZE], hashSw[HASH_RESULT_SIZE];
    u32     *hashPtr, count;
    SHA1Context sha;

    /* Hash the data */
    IO_WRITE(SHA_RPTR_REG, (u32) data);

    /* Initialize the hash value in big endian */
    IO_WRITE(SHA_BUF0_REG, 0x67452301);
    IO_WRITE(SHA_BUF1_REG, 0xefcdab89);
    IO_WRITE(SHA_BUF2_REG, 0x98badcfe);
    IO_WRITE(SHA_BUF3_REG, 0x10325476);
    IO_WRITE(SHA_BUF4_REG, 0xc3d2e1f0);

    /* Hash the data */
    FLUSH(IOD);
    INVAL(SHA);
    IO_WRITE(SHA_CTRL_REG, SHA_CTRL_EXEC |
                        ((len >> SHA_BLOCK_SZ_SHIFT) - 1));
    WAIT(SHA);

    /* Get the HW hash value */
    hashPtr = (u32 *) hashHw;
    for (count = 0; count < (HASH_RESULT_SIZE / 4); count++) {
        hashPtr[count] = IO_READ(SHA_BUF0_REG + (count * 4));
        printf("HW hash[%d]: 0x%x\n", count, hashPtr[count]);
    }

    /* Compute the software hash */
    sha_sw(&sha, data, len >> SHA_BLOCK_SZ_SHIFT, hashSw);
    hashPtr = (u32 *) hashSw;
    for (count = 0; count < (HASH_RESULT_SIZE / 4); count++) {
        printf("SW hash[%d]: 0x%x\n", count, hashPtr[count]);
    }

    /* Compare the hw and sw hashes */
    if (memcmp(hashHw, hashSw, HASH_RESULT_SIZE) != 0) {
        printf("Hash comparison failed\n");
        while (1);
    } else {
        printf("Hash comparison passed\n");
    }
}


s32
main(void)
{
    s32     rc = 0;

    /*
     * This test does the followings:
     *  1) Fill 64KB of random data
     *  2) Compare the hw hash and sw hash
     */

    fillBufFromRandom((u32 *) DATA_START, DATA_SIZE);
    hashCheck((u8 *) DATA_START, DATA_SIZE);

    return (rc);
}
