Wiselib
wiselib.testing/algorithms/crypto/harpsutils.h
Go to the documentation of this file.
00001 /*
00002  * harpsutils.h
00003  *
00004  *  Created on: Aug 12, 2010
00005  *      Author: wiselib
00006  */
00007 //#include "harps.h"
00008 //#include "sha1.h"
00009 
00010 #ifndef HARPSUTILS_H_
00011 #define HARPSUTILS_H_
00012 
00013 namespace wiselib
00014 {
00015 template<typename HashAlgo_P>
00016 class HARPSUTILS {
00017 
00018 
00019 
00020 
00021 public:
00022 
00023 
00024 #define KEY_LENGTH 16
00025 
00026 
00027 #define taPoolSize  200//2000
00028 #define maxHashDepth  4//5
00029 #define keyPoolSize  25//255
00030 
00031 //#define DEBUG_L0
00032 //#define DEBUG_L1
00033 //#define DEBUG_L2
00034 //#define DEBUG_L3
00035 //#define DEBUG_L4
00036 //#define DEBUG_L5
00037 
00038    typedef HashAlgo_P HashAlgo;
00039 
00040    typedef short int int16_t;
00041 
00042    typedef struct HarpsPublicKey_t {
00043       uint16_t keyId;
00044       uint8_t hashDepth;
00045    }HarpsPublicKey;
00046 
00047    typedef struct HarpsKey_t {
00048       HarpsPublicKey pubKey;
00049       uint8_t hashedKey[KEY_LENGTH];
00050 
00051    }HarpsKey;
00052 
00053 
00054    HARPSUTILS(){
00055 
00056    }
00057    virtual ~HARPSUTILS(){
00058 
00059    }
00060 
00061    //input,input_len,Key,Key_len,output
00062    static void hash(uint8_t *input, int32_t input_len, uint8_t *key, int32_t key_len, uint8_t *output){
00063 
00064 #ifdef __ALGORITHMS_CRYPTO_SHA1_H_
00065 
00066       HashAlgo::hmac_sha1(input, input_len, key, key_len, output);
00067       return;
00068 #endif
00069 #ifdef __IEEE_HARDWARE_HASH_H_
00070       return;
00071 #endif
00072 #ifdef __XOR_HASH_H_
00073 
00074       HashAlgo::hash(input, input_len,output);
00075       return;
00076 #endif
00077 
00078 
00079 
00080    }
00081 
00082    static int16_t divide(HarpsPublicKey* pubKey, int16_t left, int16_t right){
00083 
00084       int16_t i = left;
00085       int16_t j = right - 1;
00086       int16_t pivot = pubKey[right].keyId;
00087 
00088       do {
00089          //search for a value greater then pivot from the left (low index)
00090          while(pubKey[i].keyId <= pivot && i < right) {
00091             i++;
00092          }
00093          //search for a value smaller then pivot from the right (high index)
00094          while(pubKey[j].keyId >= pivot && j > left) {
00095             j--;
00096          }
00097          //switch values if i < j
00098          if(i < j) {
00099             HarpsPublicKey tmp;
00100             memcpy(&tmp, &pubKey[i], 3);
00101             memcpy(&pubKey[i], &pubKey[j], 3);
00102             memcpy(&pubKey[j], &tmp, 3);
00103          }
00104       }while(i < j);
00105 
00106       //if pubKey[i].keyId is greater then pivot, then switch the values
00107       if(pubKey[i].keyId > pivot) {
00108          HarpsPublicKey tmp;
00109          memcpy(&tmp, &pubKey[i], 3);
00110          memcpy(&pubKey[i], &pubKey[right], 3);
00111          memcpy(&pubKey[right], &tmp, 3);
00112       }
00113       return i;
00114    }
00115    static void quicksort(HarpsPublicKey* pubKey, int16_t left, int16_t right){
00116 
00117       if(left < right) {
00118          int16_t splitpoint = divide(pubKey, left, right);
00119          quicksort(pubKey, left, splitpoint - 1);
00120          quicksort(pubKey, splitpoint + 1, right);
00121       }
00122    }
00123 
00124 
00125    //static uint32_t rand(uint32_t upper_bound, uint32_t* randomValue) {
00126       static uint32_t rand(uint32_t upper_bound, uint32_t* randomValue1,uint32_t* randomValue2) {
00127       /* Returns a pseudo-random-number between 0 and upper_bound-1,
00128        * Using a linear congruential generator:
00129        *
00130        *          X(i+1) = ((a*X(i))+c) mod m
00131        *
00132        * Common values (quote from Wikipedia, http://en.wikipedia.org/wiki/Linear_congruential_generator):
00133        * a = 1664525, c = 1013904223, m=2^32 (Numerical Recipes)
00134        * a = 22695477, c = 1, m=2^32 (Borland C/C++)
00135        * a = 69069, c = 5, m=2^32 (GNU Compiler Collection)
00136        * a = 1103515245, c = 12345, m=2^32 (ANSI C)
00137        * a = 134775813, c = 1, m=2^32 (Borland Delphi)
00138        * a = 214013, c = 2531011, m=2^32 (Microsoft Visual/Quick C/C++)
00139        *
00140        */
00141 
00142       /*
00143       *randomValue = ((1664525 * (*randomValue)) + 1013904223 ); // mod m   mit  m= 2^32
00144 
00145       uint8_t input [KEY_LENGTH] = {0};
00146       uint8_t output [KEY_LENGTH] = {0};
00147 
00148       for(uint8_t i = 0 ; i<KEY_LENGTH; i+=4){
00149 
00150          memcpy(input+i,randomValue,4);
00151 
00152 
00153       }
00154 
00155 
00156 
00157        uint8_t hashKey[KEY_LENGTH] = {0x05, 0x07, 0x07,0xff, 0x0d,0xd4,0x00,0x56, 0x05, 0x30, 0x81, 0xf4, 0x85, 0x51, 0x15};
00158        //SHA1::hmac_sha1(input,input_len,Key,Key_len,output);
00159        SHA1::hmac_sha1(input,KEY_LENGTH,hashKey,KEY_LENGTH,output);
00160        memcpy(randomValue,output,4);
00161       return ((*randomValue) % upper_bound);
00162       */
00163 
00164          uint32_t m_z = *randomValue1;
00165          uint32_t m_w = *randomValue2;
00166 
00167          *randomValue1 = (uint32_t)36969 * (m_z & (uint32_t)65535) + (m_z >> (uint32_t)16);
00168          *randomValue2 = (uint32_t)18000 * (m_w & (uint32_t)65535) + (m_w >> (uint32_t)16);
00169           return ((m_z << (uint32_t)16) + m_w)%upper_bound;  /* 32-bit result */
00170 
00171 
00172    }
00173 };
00174 
00175 
00176 }
00177 #endif /* HARPSUTILS_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines