コード例 #1
0
void  MAJ(ARBRE*racine,ARBRE*tete)
{
	  if(tete)
 	  {
	   		if((tete->d.code==racine->d.code)&& (tete->fils))
	   		copy_arbre(tete->fils,&(racine->fils));
      }
      else
      MAJ(racine->fils,tete);
      MAJ(racine->frere,tete);
}
コード例 #2
0
ファイル: arith.c プロジェクト: PieterD/QuantumTest
void ArithAddV(Register *r, SubReg A, SubReg B, unsigned long g, unsigned long c) {
	int i;
	r=r; g=g;
	assert(A.N == B.N);
	MAJ(r, g, B.qb[0], A.qb[0]);
	for (i=1; i<(int)A.N; i++) {
		MAJ(r, A.qb[i-1], B.qb[i], A.qb[i]);
	}
	GateCNot(r, A.qb[A.N-1], c);
	for (i=A.N-1; i>0; i--) {
		UMS(r, A.qb[i-1], B.qb[i], A.qb[i]);
	}
	UMS(r, g, B.qb[0], A.qb[0]);
}
コード例 #3
0
ファイル: sha2.c プロジェクト: peterwillcn/Avalon-nano
void sha256_loc(const unsigned char *buf, unsigned int *per_a, unsigned int *per_b)
{
    uint32_t w[64];
    uint32_t wv[8];
    uint32_t t1, t2;
    int j;
    uint32_t *buf_word_p = (uint32_t *)buf;
    for(j = 0; j < 3; j++){
	    w[j] = buf_word_p[13+j];
    }

    for (j = 0; j < 8; j++) {
        wv[j] = buf_word_p[j];
    }

    for (j = 0; j < 64; j++) {
        t1 = wv[7] + SHA256_F2(wv[4]) + CH(wv[4], wv[5], wv[6]) + sha256_k[j] + w[j];
        t2 = SHA256_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]);
        wv[7] = wv[6];
        wv[6] = wv[5];
        wv[5] = wv[4];
        wv[4] = wv[3] + t1;
        wv[3] = wv[2];
        wv[2] = wv[1];
        wv[1] = wv[0];
        wv[0] = t1 + t2;
		per_a[j] = wv[0];
		per_b[j] = wv[4];
        if (j == 2) {
    	    break;
        }
    }
}
コード例 #4
0
ファイル: sha256.cpp プロジェクト: Ablu/mana
void SHA256Transform(SHA256Context *ctx,
                     unsigned char *message,
                     unsigned int block_nb)
{
    uint32_t w[64];
    uint32_t wv[8];
    unsigned char *sub_block;
    for (unsigned int i = 1; i <= block_nb; i++)
    {
        int j;
        sub_block = message + ((i - 1) << 6);

        for (j = 0; j < 16; j++)
            PACK32(&sub_block[j << 2], &w[j]);
        for (j = 16; j < 64; j++)
            SHA256_SCR(j);
        for (j = 0; j < 8; j++)
            wv[j] = ctx->h[j];
        for (j = 0; j < 64; j++)
        {
            uint32_t t1 = wv[7] + SHA256_F2(wv[4]) + CH(wv[4], wv[5], wv[6]) + sha256_k[j] + w[j];
            uint32_t t2 = SHA256_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]);
            wv[7] = wv[6];
            wv[6] = wv[5];
            wv[5] = wv[4];
            wv[4] = wv[3] + t1;
            wv[3] = wv[2];
            wv[2] = wv[1];
            wv[1] = wv[0];
            wv[0] = t1 + t2;
        }
        for (j = 0; j < 8; j++)
            ctx->h[j] += wv[j];
    }
}
コード例 #5
0
ファイル: sha256.cpp プロジェクト: Iktwo/mac2wepkey
/*
 * Process a single block of input using the hash algorithm.
 */
void SHA256::ProcessBlock(const unsigned char *block)
{
        uint32_t W[64];
        uint32_t a, b, c, d, e, f, g, h;
        uint32_t temp, temp2;
        int t;

        /* Unpack the block into 64 32-bit words */
        for(t = 0; t < 16; ++t)
        {
                W[t] = (((uint32_t)(block[t * 4 + 0])) << 24) |
                       (((uint32_t)(block[t * 4 + 1])) << 16) |
                       (((uint32_t)(block[t * 4 + 2])) <<  8) |
                        ((uint32_t)(block[t * 4 + 3]));
        }
        for(t = 16; t < 64; ++t)
        {
                W[t] = TRUNCLONG(RHO1(W[t - 2]) + W[t - 7] +
                                                 RHO0(W[t - 15]) + W[t - 16]);
        }

        /* Load the SHA-256 state into local variables */
        a = this->A;
        b = this->B;
        c = this->C;
        d = this->D;
        e = this->E;
        f = this->F;
        g = this->G;
        h = this->H;

        /* Perform 64 rounds of hash computations */
        for(t = 0; t < 64; ++t)
        {
                temp = TRUNCLONG(h + SUM1(e) + CH(e, f, g) + K[t] + W[t]);
                temp2 = TRUNCLONG(SUM0(a) + MAJ(a, b, c));
                h = g;
                g = f;
                f = e;
                e = TRUNCLONG(d + temp);
                d = c;
                c = b;
                b = a;
                a = TRUNCLONG(temp + temp2);
        }

        /* Combine the previous SHA-256 state with the new state */
        this->A = TRUNCLONG(this->A + a);
        this->B = TRUNCLONG(this->B + b);
        this->C = TRUNCLONG(this->C + c);
        this->D = TRUNCLONG(this->D + d);
        this->E = TRUNCLONG(this->E + e);
        this->F = TRUNCLONG(this->F + f);
        this->G = TRUNCLONG(this->G + g);
        this->H = TRUNCLONG(this->H + h);

        /* Clear the temporary state */
        memset(W, 0 , sizeof(uint32_t) * 64);
        a = b = c = d = e = f = g = h = temp = temp2 = 0;
}
コード例 #6
0
void sha2_large_common_nextBlock(sha2_large_common_ctx_t* ctx, const void* block){
	uint64_t w[16], wx;
	uint64_t a[8];
	uint64_t t1, t2;
	const uint64_t *k=sha2_large_common_const;
	uint8_t i;
	i=16;
	do{
		w[16-i] = change_endian64(*((const uint64_t*)block));
		block = (uint8_t*)block + 8;
	}while(--i);
	memcpy(a, ctx->h, 8*8);
	for(i=0; i<80; ++i){
		if(i<16){
			wx=w[i];
		}else{
			wx = SIGMA_b(w[14]) + w[9] + SIGMA_a(w[1]) + w[0];
			memmove(&(w[0]), &(w[1]), 15*8);
			w[15] = wx;
		}
		t1 = a[7] + SIGMA_1(a[4]) + CH(a[4], a[5], a[6]) + pgm_read_uint64_t_P(k++) + wx;
		t2 = SIGMA_0(a[0]) + MAJ(a[0], a[1], a[2]);
		memmove(&(a[1]), &(a[0]), 7*8);
		a[0] = t1 + t2;
		a[4] += t1;
	}
	i=7;
	do{
		ctx->h[i] += a[i];
	}while(i--);
	ctx->length += 1;
}
コード例 #7
0
ファイル: sha2.c プロジェクト: drew-buckley/xmosh
XMoshSum digestBlock( hp_byte* initialByteBlock, XMoshSum sum )
{

	sha2_word block[ ROUNDS ];

	sha2_word* words = ( sha2_word* ) &sum.data;

	for( unsigned int i = 0; i < 16; i++ )
	{
		BLOCK_COPY( block, initialByteBlock, i );
	}

	for( unsigned i = 16; i < ROUNDS; i++ )
	{
		block[ i ] = block[ i - 16 ]
				+ SIGMA0( block, i ) + block[ i - 7 ] + SIGMA1( block, i );
	}

	sha2_word a = words[ 0 ];
	sha2_word b = words[ 1 ];
	sha2_word c = words[ 2 ];
	sha2_word d = words[ 3 ];
	sha2_word e = words[ 4 ];
	sha2_word f = words[ 5 ];
	sha2_word g = words[ 6 ];
	sha2_word h = words[ 7 ];

	sha2_word temp1, temp2;

	for( unsigned i = 0; i < ROUNDS; i++ )
	{

		temp1 = ( h + ( SUM1( e ) ) + CH( e, f, g ) + K[ i ] + block[ i ] );
		temp2 = ( SUM0( a ) ) + MAJ( a, b, c );

		h = g;
		g = f;
		f = e;
		e = d + temp1;
		d = c;
		c = b;
		b = a;
		a = temp1 + temp2;
	}

	words[ 0 ] += a;
	words[ 1 ] += b;
	words[ 2 ] += c;
	words[ 3 ] += d;
	words[ 4 ] += e;
	words[ 5 ] += f;
	words[ 6 ] += g;
	words[ 7 ] += h;

	return sum;
}
コード例 #8
0
ファイル: sha512.c プロジェクト: blinkingnoise/Sming
static void SHA512_Process(SHA512_CTX *ctx)
{
    int t;
    uint64_t temp1;
    uint64_t temp2;
 
    // Initialize the 8 working registers
    uint64_t a = ctx->h_dig.h[0];
    uint64_t b = ctx->h_dig.h[1];
    uint64_t c = ctx->h_dig.h[2];
    uint64_t d = ctx->h_dig.h[3];
    uint64_t e = ctx->h_dig.h[4];
    uint64_t f = ctx->h_dig.h[5];
    uint64_t g = ctx->h_dig.h[6];
    uint64_t h = ctx->h_dig.h[7];
 
    // Process message in 16-word blocks
    uint64_t *w = ctx->w_buf.w;
 
    // Convert from big-endian byte order to host byte order
    for (t = 0; t < 16; t++)
       w[t] = be64toh(w[t]);

    // Prepare the message schedule
    for (t = 16; t < 80; t++)
       w[t] = SIGMA4(w[t - 2]) + w[t - 7] + SIGMA3(w[t - 15]) + w[t - 16];
 
    // SHA-512 hash computation
    for (t = 0; t < 80; t++)
    {
       // Calculate T1 and T2
       temp1 = h + SIGMA2(e) + CH(e, f, g) + k[t] + w[t];
       temp2 = SIGMA1(a) + MAJ(a, b, c);
 
       // Update the working registers
       h = g;
       g = f;
       f = e;
       e = d + temp1;
       d = c;
       c = b;
       b = a;
       a = temp1 + temp2;
    }
 
    // Update the hash value
    ctx->h_dig.h[0] += a;
    ctx->h_dig.h[1] += b;
    ctx->h_dig.h[2] += c;
    ctx->h_dig.h[3] += d;
    ctx->h_dig.h[4] += e;
    ctx->h_dig.h[5] += f;
    ctx->h_dig.h[6] += g;
    ctx->h_dig.h[7] += h;
 }
コード例 #9
0
ファイル: sha512.c プロジェクト: woo2/gatekeeper-v2
void sha512ProcessBlock(Sha512Context *context)
{
   uint_t t;
   uint64_t temp1;
   uint64_t temp2;

   //Initialize the 8 working registers
   uint64_t a = context->h[0];
   uint64_t b = context->h[1];
   uint64_t c = context->h[2];
   uint64_t d = context->h[3];
   uint64_t e = context->h[4];
   uint64_t f = context->h[5];
   uint64_t g = context->h[6];
   uint64_t h = context->h[7];

   //Process message in 16-word blocks
   uint64_t *w = context->w;

   //Convert from big-endian byte order to host byte order
   for(t = 0; t < 16; t++)
      w[t] = betoh64(w[t]);

   //SHA-512 hash computation (alternate method)
   for(t = 0; t < 80; t++)
   {
      //Prepare the message schedule
      if(t >= 16)
         W(t) += SIGMA4(W(t + 14)) + W(t + 9) + SIGMA3(W(t + 1));

      //Calculate T1 and T2
      temp1 = h + SIGMA2(e) + CH(e, f, g) + k[t] + W(t);
      temp2 = SIGMA1(a) + MAJ(a, b, c);

      //Update the working registers
      h = g;
      g = f;
      f = e;
      e = d + temp1;
      d = c;
      c = b;
      b = a;
      a = temp1 + temp2;
   }

   //Update the hash value
   context->h[0] += a;
   context->h[1] += b;
   context->h[2] += c;
   context->h[3] += d;
   context->h[4] += e;
   context->h[5] += f;
   context->h[6] += g;
   context->h[7] += h;
}
コード例 #10
0
ファイル: sha512.c プロジェクト: adfernandes/cifra
static void sha512_update_block(void *vctx, const uint8_t *inp)
{
  cf_sha512_context *ctx = vctx;

  uint64_t W[16];

  uint64_t a = ctx->H[0],
           b = ctx->H[1],
           c = ctx->H[2],
           d = ctx->H[3],
           e = ctx->H[4],
           f = ctx->H[5],
           g = ctx->H[6],
           h = ctx->H[7],
           Wt;

  for (size_t t = 0; t < 80; t++)
  {
    if (t < 16)
    {
      W[t] = Wt = read64_be(inp);
      inp += 8;
    } else {
      Wt = SSIG1(W[(t - 2) % 16]) +
           W[(t - 7) % 16] +
           SSIG0(W[(t - 15) % 16]) +
           W[(t - 16) % 16];
      W[t % 16] = Wt;
    }

    uint64_t T1 = h + BSIG1(e) + CH(e, f, g) + K[t] + Wt;
    uint64_t T2 = BSIG0(a) + MAJ(a, b, c);
    h = g;
    g = f;
    f = e;
    e = d + T1;
    d = c;
    c = b;
    b = a;
    a = T1 + T2;
  }

  ctx->H[0] += a;
  ctx->H[1] += b;
  ctx->H[2] += c;
  ctx->H[3] += d;
  ctx->H[4] += e;
  ctx->H[5] += f;
  ctx->H[6] += g;
  ctx->H[7] += h;

  ctx->blocks++;
}
コード例 #11
0
void  r_normaliser(ARBRE*racine,ARBRE* tete)
{
 	  if (racine)
 	  {
             if(!(racine->fils))
 		       {
			       MAJ(racine,tete);
                   r_normaliser(racine->fils,tete);
                   r_normaliser(racine->frere,tete);
			   }
	  }
}
コード例 #12
0
/***********************************************************************************************
* 函数名称:void sha256_ProChunk()
* 功    能:处理一个数据块(512位)
***********************************************************************************************/
void sha256_ProChunk()
{
	short i;
	unsigned long t1,t2;
	
	//步骤一
	for(i=0;i<64;i++)
	{
		if(0<=i&&i<=15)
		{
		}
		if(16<=i&&i<=63)
		{
			sha256_w[i]=Q1(sha256_w[i-2])+sha256_w[i-7]+Q0(sha256_w[i-15])+sha256_w[i-16];
		}    
	}
	
	//步骤二
	sha256_a=sha256_hh[0];
	sha256_b=sha256_hh[1];
	sha256_c=sha256_hh[2];
	sha256_d=sha256_hh[3];
	sha256_e=sha256_hh[4];
	sha256_f=sha256_hh[5];
	sha256_g=sha256_hh[6];
	sha256_h=sha256_hh[7];
	
	//步骤三
	for(i=0;i<64;i++)
	{
		t1=sha256_h+E1(sha256_e)+CH(sha256_e,sha256_f,sha256_g)+sha256_K[i]+sha256_w[i];
		t2=E0(sha256_a)+MAJ(sha256_a,sha256_b,sha256_c);
		sha256_h=sha256_g;
		sha256_g=sha256_f;
		sha256_f=sha256_e;
		sha256_e=sha256_d+t1;
		sha256_d=sha256_c;
		sha256_c=sha256_b;
		sha256_b=sha256_a;
		sha256_a=t1+t2;
	}
	
	//步骤四
	sha256_hh[0] += sha256_a;
	sha256_hh[1] += sha256_b;
	sha256_hh[2] += sha256_c;
	sha256_hh[3] += sha256_d;
	sha256_hh[4] += sha256_e;
	sha256_hh[5] += sha256_f;
	sha256_hh[6] += sha256_g;
	sha256_hh[7] += sha256_h;
}
コード例 #13
0
ファイル: sha384.cpp プロジェクト: stahta01/wxCode_components
void wxSHA384::ComputeTempHash(const size_t &t)
{
    T1 = h + SUM1_384(e) + CH(e,f,g) + K_384[t] + W[t];
    T2 = SUM0_384(a) + MAJ(a,b,c);
    h = g;
    g = f;
    f = e;
    e = d + T1;;
    d = c;
    c = b;
    b = a;
    a = T1 + T2;
}
コード例 #14
0
ファイル: sha2.c プロジェクト: ctapang/v0_70_01b
void sha256_transf(sha256_ctx *ctx, const unsigned char *message,
                   unsigned int block_nb)
{
    uint32_t w[64];
    uint32_t wv[8];
    uint32_t t1, t2;
    const unsigned char *sub_block;
    int i;

    int j;

    for (i = 0; i < (int) block_nb; i++) {
        sub_block = message + (i << 6);

        for (j = 0; j < 16; j++) {
            PACK32(&sub_block[j << 2], &w[j]);
        }

        for (j = 16; j < 64; j++) {
            SHA256_SCR(j);
        }

        for (j = 0; j < 8; j++) {
            wv[j] = ctx->h[j];
        }

        for (j = 0; j < 64; j++) {
            t1 = wv[7] + SHA256_F2(wv[4]) + CH(wv[4], wv[5], wv[6])
                + sha256_k[j] + w[j];
            t2 = SHA256_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]);
            wv[7] = wv[6];
            wv[6] = wv[5];
            wv[5] = wv[4];
            wv[4] = wv[3] + t1;
            wv[3] = wv[2];
            wv[2] = wv[1];
            wv[1] = wv[0];
            wv[0] = t1 + t2;
	    if (j == 2) {
		    break;
	    }
        }

        for (j = 0; j < 8; j++) {
            ctx->h[j] = wv[j];
        }
    }
}
コード例 #15
0
ファイル: fdm_hmac.c プロジェクト: CreatorKit/device-manager
static void Sha256_Transform(Sha256ContextType *context, uint8_t * data)
{
    uint32_t a,b,c,d,e,f,g,h,i,j,t1,t2,m[64];

    for (i = 0, j = 0; i < 16; ++i, j += 4)
    {
        m[i] = (data[j] << 24) | (data[j+1] << 16) | (data[j+2] << 8) | (data[j+3]);
    }

    for (; i < 64; i++)
    {
        m[i] = SIG1(m[i-2]) + m[i-7] + SIG0(m[i-15]) + m[i-16];
    }

    a = context->State[0];
    b = context->State[1];
    c = context->State[2];
    d = context->State[3];
    e = context->State[4];
    f = context->State[5];
    g = context->State[6];
    h = context->State[7];

    for (i = 0; i < 64; i++)
    {
        t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i];
        t2 = EP0(a) + MAJ(a,b,c);
        h = g;
        g = f;
        f = e;
        e = d + t1;
        d = c;
        c = b;
        b = a;
        a = t1 + t2;
    }

    context->State[0] += a;
    context->State[1] += b;
    context->State[2] += c;
    context->State[3] += d;
    context->State[4] += e;
    context->State[5] += f;
    context->State[6] += g;
    context->State[7] += h;
}
コード例 #16
0
ファイル: TAP.cpp プロジェクト: empire1601/STATS
////////////////////////////////////////////////////////////////////TAP\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//->Cstr : Définition de la population
TaP::TaP(POPULATION *pop, QWidget *parent) : POPULATION(parent,4,50)
{
    population=pop;
    //id_courant=0;
    setNomVar(0, "Modalités");
    setNomVar(1, "Effectifs");
    setTypeVar(1, TYPES::QUALITATIF_PURE);
    setNomVar(2, "Fréquences");
    setTypeVar(2, TYPES::QUALITATIF_PURE);
    setNomVar(3, "Fréq. Cumulées");
    setTypeVar(3, TYPES::QUALITATIF_PURE);
    connect(population, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(MAJ(QModelIndex,QModelIndex)));
    if(rowCount()>1)
    {
        removeRows(0,rowCount());
    }
    setData(0,0,"TOTAL");
    setData(2,0, "1,00");
    var_courante=0;
}
コード例 #17
0
ファイル: erlsha2_nif.c プロジェクト: T0ha/biten
static void
sha5xx_chunk(Context* ctx, unsigned char* chunk)
{
    uint64_t* hash = (uint64_t*)ctx->digest.data;
    uint64_t a, b, c, d, e, f, g, h;
    uint64_t words[80];
    int i;
#ifndef WORDS_BIGENDIAN
    {
        uint64_t* from = (uint64_t*)chunk;
        for (i = 0; i < 16; ++i) {
            words[i] = BYTESWAP64(from[i]);
        }
    }
#else
    memcpy(words, chunk, 16*sizeof(*words));
#endif
    for (i = 16; i < sizeof(words)/sizeof(*words); ++i) {
        uint64_t w15 = words[i-15], w2 = words[i-2];
        uint64_t s0 = SM_SIGMA512_0(w15), s1 = SM_SIGMA512_1(w2);
        uint64_t w7 = words[i-7], w16 = words[i-16];
        words[i] = s1 + w7 + s0 + w16;
    }
    a = hash[0]; b = hash[1]; c = hash[2]; d = hash[3];
    e = hash[4]; f = hash[5]; g = hash[6]; h = hash[7];
    for (i = 0; i < sizeof(words)/sizeof(*words); ++i) {
        uint64_t t1, t2;
        t1 = h + BIG_SIGMA512_1(e) + CH(e,f,g) + K512[i] + words[i];
        t2 = BIG_SIGMA512_0(a) + MAJ(a,b,c);
        h = g;
        g = f;
        f = e;
        e = d + t1;
        d = c;
        c = b;
        b = a;
        a = t1 + t2;
    }
    hash[0] += a; hash[1] += b; hash[2] += c; hash[3] += d;
    hash[4] += e; hash[5] += f; hash[6] += g; hash[7] += h;
}
コード例 #18
0
ファイル: sha256.c プロジェクト: coreboot/chrome-ec
static void SHA256_transform(struct sha256_ctx *ctx, const uint8_t *message,
			     unsigned int block_nb)
{
	/* Note: this function requires a considerable amount of stack */
	uint32_t w[64];
	uint32_t wv[8];
	uint32_t t1, t2;
	const unsigned char *sub_block;
	int i, j;

	for (i = 0; i < (int) block_nb; i++) {
		sub_block = message + (i << 6);

		for (j = 0; j < 16; j++)
			PACK32(&sub_block[j << 2], &w[j]);

		for (j = 16; j < 64; j++)
			SHA256_SCR(j);

		for (j = 0; j < 8; j++)
			wv[j] = ctx->h[j];

		for (j = 0; j < 64; j++) {
			t1 = wv[7] + SHA256_F2(wv[4]) + CH(wv[4], wv[5], wv[6])
				+ sha256_k[j] + w[j];
			t2 = SHA256_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]);
			wv[7] = wv[6];
			wv[6] = wv[5];
			wv[5] = wv[4];
			wv[4] = wv[3] + t1;
			wv[3] = wv[2];
			wv[2] = wv[1];
			wv[1] = wv[0];
			wv[0] = t1 + t2;
		}

		for (j = 0; j < 8; j++)
			ctx->h[j] += wv[j];
	}
}
コード例 #19
0
ファイル: sha256.c プロジェクト: ryanfzy/c-codes
void sha256(char *pInput, unsigned int iInputLength, _hash *p_hash)
{
    //printf("length:%d\n",iInputLength);
    unsigned long h1 = 0x6a09e667;
    unsigned long h2 = 0xbb67ae85;
    unsigned long h3 = 0x3c6ef372;
    unsigned long h4 = 0xa54ff53a;
    unsigned long h5 = 0x510e527f;
    unsigned long h6 = 0x9b05688c;
    unsigned long h7 = 0x1f83d9ab;
    unsigned long h8 = 0x5be0cd19;
    //print8longs(h1, h2, h3, h4, h5, h6, h7, h8);

    unsigned int isize = (iInputLength / 64 > 0) ? (iInputLength / 64 * 64 + 64) : 64;
    isize =  iInputLength % 64 >= 56 ? isize + 64 : isize;
    //printf("size:%d\n", isize);
    unsigned long *pPreparedInput = (unsigned long*)malloc(isize);
    prepare_input(pPreparedInput, isize/4, pInput, iInputLength);

    //printstr((char*)pPreparedInput, isize);

    for (int i = 0; i < isize/4; i = i + 16)
    {
        unsigned long W[64] = {0};
        for (int j = 0; j < 16; j++)
            W[j] = pPreparedInput[i+j];
        for (int j = 16; j < 64; j++)
            W[j] = Q1(W[j-2]) + W[j-7] + Q0(W[j-15]) + W[j-16];
        unsigned long a = h1;
        unsigned long b = h2;
        unsigned long c = h3;
        unsigned long d = h4;
        unsigned long e = h5;
        unsigned long f = h6;
        unsigned long g = h7;
        unsigned long h = h8; 
        for (int j = 0; j < 64; j++)
        {
            unsigned long t1 = h + E1(e) + CH(e, f, g) + K[j] + W[j];
            unsigned long t2 = E0(a) + MAJ(a, b, c);
            /*
            printf("h:");
            printlong(h);
            printf("\n");
            printf("E1(e):");
            printlong(E1(e));
            printf("\n");
            printf("CH(e,f,g):");
            printlong(CH(e,f,g));
            printf("\n");
            printf("K[%d]:", j);
            printlong(K[j]);
            printf("\n");
            printf("W[%d]:", j);
            printlong(W[j]);
            printf("\n");
            printf("T1");
            printlong(t1);
            printf("\n");
            */
            h = g;
            g = f;
            f = e;
            e = d + t1;
            d = c;
            c = b;
            b = a;
            a = t1 + t2;
            //printf("%d:", j);
            //print8longs(a, b, c, d, e, f, g, h);
        }
        h1 += a;
        h2 += b;
        h3 += c;
        h4 += d;
        h5 += e;
        h6 += f;
        h7 += g;
        h8 += h;
    }
    //print8longs(h1, h2, h3, h4, h5, h6, h7, h8);
    long2char4(h1, p_hash->X);
    long2char4(h2, p_hash->X+4);
    long2char4(h3, p_hash->X+8);
    long2char4(h4, p_hash->X+12);
    long2char4(h5, p_hash->X+16);
    long2char4(h6, p_hash->X+20);
    long2char4(h7, p_hash->X+24);
    long2char4(h8, p_hash->X+28);

    free(pPreparedInput);
}
コード例 #20
0
ファイル: sha2.c プロジェクト: xharbour/core
static void sha512_transf( sha512_ctx * ctx, const unsigned char * message,
                           unsigned int block_nb )
{
   uint64                  w[ 80 ];
   uint64                  wv[ 8 ];
   uint64                  t1, t2;
   const unsigned char *   sub_block;
   int                     i, j;

   for( i = 0; i < ( int ) block_nb; i++ )
   {
      sub_block = message + ( i << 7 );

#ifndef UNROLL_LOOPS
      for( j = 0; j < 16; j++ )
      {
         PACK64( &sub_block[ j << 3 ], &w[ j ] );
      }

      for( j = 16; j < 80; j++ )
      {
         SHA512_SCR( j );
      }

      for( j = 0; j < 8; j++ )
      {
         wv[ j ] = ctx->h[ j ];
      }

      for( j = 0; j < 80; j++ )
      {
         t1       = wv[ 7 ] + SHA512_F2( wv[ 4 ] ) + CH( wv[ 4 ], wv[ 5 ], wv[ 6 ] )
                    + sha512_k[ j ] + w[ j ];
         t2       = SHA512_F1( wv[ 0 ] ) + MAJ( wv[ 0 ], wv[ 1 ], wv[ 2 ] );
         wv[ 7 ]  = wv[ 6 ];
         wv[ 6 ]  = wv[ 5 ];
         wv[ 5 ]  = wv[ 4 ];
         wv[ 4 ]  = wv[ 3 ] + t1;
         wv[ 3 ]  = wv[ 2 ];
         wv[ 2 ]  = wv[ 1 ];
         wv[ 1 ]  = wv[ 0 ];
         wv[ 0 ]  = t1 + t2;
      }

      for( j = 0; j < 8; j++ )
      {
         ctx->h[ j ] += wv[ j ];
      }
#else
      PACK64( &sub_block[ 0 ], &w[ 0 ] ); PACK64( &sub_block[ 8 ], &w[ 1 ] );
      PACK64( &sub_block[ 16 ], &w[ 2 ] ); PACK64( &sub_block[ 24 ], &w[ 3 ] );
      PACK64( &sub_block[ 32 ], &w[ 4 ] ); PACK64( &sub_block[ 40 ], &w[ 5 ] );
      PACK64( &sub_block[ 48 ], &w[ 6 ] ); PACK64( &sub_block[ 56 ], &w[ 7 ] );
      PACK64( &sub_block[ 64 ], &w[ 8 ] ); PACK64( &sub_block[ 72 ], &w[ 9 ] );
      PACK64( &sub_block[ 80 ], &w[ 10 ] ); PACK64( &sub_block[ 88 ], &w[ 11 ] );
      PACK64( &sub_block[ 96 ], &w[ 12 ] ); PACK64( &sub_block[ 104 ], &w[ 13 ] );
      PACK64( &sub_block[ 112 ], &w[ 14 ] ); PACK64( &sub_block[ 120 ], &w[ 15 ] );

      SHA512_SCR( 16 ); SHA512_SCR( 17 ); SHA512_SCR( 18 ); SHA512_SCR( 19 );
      SHA512_SCR( 20 ); SHA512_SCR( 21 ); SHA512_SCR( 22 ); SHA512_SCR( 23 );
      SHA512_SCR( 24 ); SHA512_SCR( 25 ); SHA512_SCR( 26 ); SHA512_SCR( 27 );
      SHA512_SCR( 28 ); SHA512_SCR( 29 ); SHA512_SCR( 30 ); SHA512_SCR( 31 );
      SHA512_SCR( 32 ); SHA512_SCR( 33 ); SHA512_SCR( 34 ); SHA512_SCR( 35 );
      SHA512_SCR( 36 ); SHA512_SCR( 37 ); SHA512_SCR( 38 ); SHA512_SCR( 39 );
      SHA512_SCR( 40 ); SHA512_SCR( 41 ); SHA512_SCR( 42 ); SHA512_SCR( 43 );
      SHA512_SCR( 44 ); SHA512_SCR( 45 ); SHA512_SCR( 46 ); SHA512_SCR( 47 );
      SHA512_SCR( 48 ); SHA512_SCR( 49 ); SHA512_SCR( 50 ); SHA512_SCR( 51 );
      SHA512_SCR( 52 ); SHA512_SCR( 53 ); SHA512_SCR( 54 ); SHA512_SCR( 55 );
      SHA512_SCR( 56 ); SHA512_SCR( 57 ); SHA512_SCR( 58 ); SHA512_SCR( 59 );
      SHA512_SCR( 60 ); SHA512_SCR( 61 ); SHA512_SCR( 62 ); SHA512_SCR( 63 );
      SHA512_SCR( 64 ); SHA512_SCR( 65 ); SHA512_SCR( 66 ); SHA512_SCR( 67 );
      SHA512_SCR( 68 ); SHA512_SCR( 69 ); SHA512_SCR( 70 ); SHA512_SCR( 71 );
      SHA512_SCR( 72 ); SHA512_SCR( 73 ); SHA512_SCR( 74 ); SHA512_SCR( 75 );
      SHA512_SCR( 76 ); SHA512_SCR( 77 ); SHA512_SCR( 78 ); SHA512_SCR( 79 );

      wv[ 0 ]  = ctx->h[ 0 ]; wv[ 1 ] = ctx->h[ 1 ];
      wv[ 2 ]  = ctx->h[ 2 ]; wv[ 3 ] = ctx->h[ 3 ];
      wv[ 4 ]  = ctx->h[ 4 ]; wv[ 5 ] = ctx->h[ 5 ];
      wv[ 6 ]  = ctx->h[ 6 ]; wv[ 7 ] = ctx->h[ 7 ];

      j        = 0;

      do
      {
         SHA512_EXP( 0, 1, 2, 3, 4, 5, 6, 7, j ); j++;
         SHA512_EXP( 7, 0, 1, 2, 3, 4, 5, 6, j ); j++;
         SHA512_EXP( 6, 7, 0, 1, 2, 3, 4, 5, j ); j++;
         SHA512_EXP( 5, 6, 7, 0, 1, 2, 3, 4, j ); j++;
         SHA512_EXP( 4, 5, 6, 7, 0, 1, 2, 3, j ); j++;
         SHA512_EXP( 3, 4, 5, 6, 7, 0, 1, 2, j ); j++;
         SHA512_EXP( 2, 3, 4, 5, 6, 7, 0, 1, j ); j++;
         SHA512_EXP( 1, 2, 3, 4, 5, 6, 7, 0, j ); j++;
      }
      while( j < 80 );

      ctx->h[ 0 ] += wv[ 0 ]; ctx->h[ 1 ] += wv[ 1 ];
      ctx->h[ 2 ] += wv[ 2 ]; ctx->h[ 3 ] += wv[ 3 ];
      ctx->h[ 4 ] += wv[ 4 ]; ctx->h[ 5 ] += wv[ 5 ];
      ctx->h[ 6 ] += wv[ 6 ]; ctx->h[ 7 ] += wv[ 7 ];
#endif /* !UNROLL_LOOPS */
   }
}
コード例 #21
0
ファイル: sha2.c プロジェクト: xharbour/core
static void sha256_transf( sha256_ctx * ctx, const unsigned char * message,
                           unsigned int block_nb )
{
   uint32                  w[ 64 ];
   uint32                  wv[ 8 ];
   uint32                  t1, t2;
   const unsigned char *   sub_block;
   int                     i;

#ifndef UNROLL_LOOPS
   int                     j;
#endif

   for( i = 0; i < ( int ) block_nb; i++ )
   {
      sub_block = message + ( i << 6 );

#ifndef UNROLL_LOOPS
      for( j = 0; j < 16; j++ )
      {
         PACK32( &sub_block[ j << 2 ], &w[ j ] );
      }

      for( j = 16; j < 64; j++ )
      {
         SHA256_SCR( j );
      }

      for( j = 0; j < 8; j++ )
      {
         wv[ j ] = ctx->h[ j ];
      }

      for( j = 0; j < 64; j++ )
      {
         t1       = wv[ 7 ] + SHA256_F2( wv[ 4 ] ) + CH( wv[ 4 ], wv[ 5 ], wv[ 6 ] )
                    + sha256_k[ j ] + w[ j ];
         t2       = SHA256_F1( wv[ 0 ] ) + MAJ( wv[ 0 ], wv[ 1 ], wv[ 2 ] );
         wv[ 7 ]  = wv[ 6 ];
         wv[ 6 ]  = wv[ 5 ];
         wv[ 5 ]  = wv[ 4 ];
         wv[ 4 ]  = wv[ 3 ] + t1;
         wv[ 3 ]  = wv[ 2 ];
         wv[ 2 ]  = wv[ 1 ];
         wv[ 1 ]  = wv[ 0 ];
         wv[ 0 ]  = t1 + t2;
      }

      for( j = 0; j < 8; j++ )
      {
         ctx->h[ j ] += wv[ j ];
      }
#else
      PACK32( &sub_block[ 0 ], &w[ 0 ] ); PACK32( &sub_block[ 4 ], &w[ 1 ] );
      PACK32( &sub_block[ 8 ], &w[ 2 ] ); PACK32( &sub_block[ 12 ], &w[ 3 ] );
      PACK32( &sub_block[ 16 ], &w[ 4 ] ); PACK32( &sub_block[ 20 ], &w[ 5 ] );
      PACK32( &sub_block[ 24 ], &w[ 6 ] ); PACK32( &sub_block[ 28 ], &w[ 7 ] );
      PACK32( &sub_block[ 32 ], &w[ 8 ] ); PACK32( &sub_block[ 36 ], &w[ 9 ] );
      PACK32( &sub_block[ 40 ], &w[ 10 ] ); PACK32( &sub_block[ 44 ], &w[ 11 ] );
      PACK32( &sub_block[ 48 ], &w[ 12 ] ); PACK32( &sub_block[ 52 ], &w[ 13 ] );
      PACK32( &sub_block[ 56 ], &w[ 14 ] ); PACK32( &sub_block[ 60 ], &w[ 15 ] );

      SHA256_SCR( 16 ); SHA256_SCR( 17 ); SHA256_SCR( 18 ); SHA256_SCR( 19 );
      SHA256_SCR( 20 ); SHA256_SCR( 21 ); SHA256_SCR( 22 ); SHA256_SCR( 23 );
      SHA256_SCR( 24 ); SHA256_SCR( 25 ); SHA256_SCR( 26 ); SHA256_SCR( 27 );
      SHA256_SCR( 28 ); SHA256_SCR( 29 ); SHA256_SCR( 30 ); SHA256_SCR( 31 );
      SHA256_SCR( 32 ); SHA256_SCR( 33 ); SHA256_SCR( 34 ); SHA256_SCR( 35 );
      SHA256_SCR( 36 ); SHA256_SCR( 37 ); SHA256_SCR( 38 ); SHA256_SCR( 39 );
      SHA256_SCR( 40 ); SHA256_SCR( 41 ); SHA256_SCR( 42 ); SHA256_SCR( 43 );
      SHA256_SCR( 44 ); SHA256_SCR( 45 ); SHA256_SCR( 46 ); SHA256_SCR( 47 );
      SHA256_SCR( 48 ); SHA256_SCR( 49 ); SHA256_SCR( 50 ); SHA256_SCR( 51 );
      SHA256_SCR( 52 ); SHA256_SCR( 53 ); SHA256_SCR( 54 ); SHA256_SCR( 55 );
      SHA256_SCR( 56 ); SHA256_SCR( 57 ); SHA256_SCR( 58 ); SHA256_SCR( 59 );
      SHA256_SCR( 60 ); SHA256_SCR( 61 ); SHA256_SCR( 62 ); SHA256_SCR( 63 );

      wv[ 0 ]  = ctx->h[ 0 ]; wv[ 1 ] = ctx->h[ 1 ];
      wv[ 2 ]  = ctx->h[ 2 ]; wv[ 3 ] = ctx->h[ 3 ];
      wv[ 4 ]  = ctx->h[ 4 ]; wv[ 5 ] = ctx->h[ 5 ];
      wv[ 6 ]  = ctx->h[ 6 ]; wv[ 7 ] = ctx->h[ 7 ];

      SHA256_EXP( 0, 1, 2, 3, 4, 5, 6, 7, 0 ); SHA256_EXP( 7, 0, 1, 2, 3, 4, 5, 6, 1 );
      SHA256_EXP( 6, 7, 0, 1, 2, 3, 4, 5, 2 ); SHA256_EXP( 5, 6, 7, 0, 1, 2, 3, 4, 3 );
      SHA256_EXP( 4, 5, 6, 7, 0, 1, 2, 3, 4 ); SHA256_EXP( 3, 4, 5, 6, 7, 0, 1, 2, 5 );
      SHA256_EXP( 2, 3, 4, 5, 6, 7, 0, 1, 6 ); SHA256_EXP( 1, 2, 3, 4, 5, 6, 7, 0, 7 );
      SHA256_EXP( 0, 1, 2, 3, 4, 5, 6, 7, 8 ); SHA256_EXP( 7, 0, 1, 2, 3, 4, 5, 6, 9 );
      SHA256_EXP( 6, 7, 0, 1, 2, 3, 4, 5, 10 ); SHA256_EXP( 5, 6, 7, 0, 1, 2, 3, 4, 11 );
      SHA256_EXP( 4, 5, 6, 7, 0, 1, 2, 3, 12 ); SHA256_EXP( 3, 4, 5, 6, 7, 0, 1, 2, 13 );
      SHA256_EXP( 2, 3, 4, 5, 6, 7, 0, 1, 14 ); SHA256_EXP( 1, 2, 3, 4, 5, 6, 7, 0, 15 );
      SHA256_EXP( 0, 1, 2, 3, 4, 5, 6, 7, 16 ); SHA256_EXP( 7, 0, 1, 2, 3, 4, 5, 6, 17 );
      SHA256_EXP( 6, 7, 0, 1, 2, 3, 4, 5, 18 ); SHA256_EXP( 5, 6, 7, 0, 1, 2, 3, 4, 19 );
      SHA256_EXP( 4, 5, 6, 7, 0, 1, 2, 3, 20 ); SHA256_EXP( 3, 4, 5, 6, 7, 0, 1, 2, 21 );
      SHA256_EXP( 2, 3, 4, 5, 6, 7, 0, 1, 22 ); SHA256_EXP( 1, 2, 3, 4, 5, 6, 7, 0, 23 );
      SHA256_EXP( 0, 1, 2, 3, 4, 5, 6, 7, 24 ); SHA256_EXP( 7, 0, 1, 2, 3, 4, 5, 6, 25 );
      SHA256_EXP( 6, 7, 0, 1, 2, 3, 4, 5, 26 ); SHA256_EXP( 5, 6, 7, 0, 1, 2, 3, 4, 27 );
      SHA256_EXP( 4, 5, 6, 7, 0, 1, 2, 3, 28 ); SHA256_EXP( 3, 4, 5, 6, 7, 0, 1, 2, 29 );
      SHA256_EXP( 2, 3, 4, 5, 6, 7, 0, 1, 30 ); SHA256_EXP( 1, 2, 3, 4, 5, 6, 7, 0, 31 );
      SHA256_EXP( 0, 1, 2, 3, 4, 5, 6, 7, 32 ); SHA256_EXP( 7, 0, 1, 2, 3, 4, 5, 6, 33 );
      SHA256_EXP( 6, 7, 0, 1, 2, 3, 4, 5, 34 ); SHA256_EXP( 5, 6, 7, 0, 1, 2, 3, 4, 35 );
      SHA256_EXP( 4, 5, 6, 7, 0, 1, 2, 3, 36 ); SHA256_EXP( 3, 4, 5, 6, 7, 0, 1, 2, 37 );
      SHA256_EXP( 2, 3, 4, 5, 6, 7, 0, 1, 38 ); SHA256_EXP( 1, 2, 3, 4, 5, 6, 7, 0, 39 );
      SHA256_EXP( 0, 1, 2, 3, 4, 5, 6, 7, 40 ); SHA256_EXP( 7, 0, 1, 2, 3, 4, 5, 6, 41 );
      SHA256_EXP( 6, 7, 0, 1, 2, 3, 4, 5, 42 ); SHA256_EXP( 5, 6, 7, 0, 1, 2, 3, 4, 43 );
      SHA256_EXP( 4, 5, 6, 7, 0, 1, 2, 3, 44 ); SHA256_EXP( 3, 4, 5, 6, 7, 0, 1, 2, 45 );
      SHA256_EXP( 2, 3, 4, 5, 6, 7, 0, 1, 46 ); SHA256_EXP( 1, 2, 3, 4, 5, 6, 7, 0, 47 );
      SHA256_EXP( 0, 1, 2, 3, 4, 5, 6, 7, 48 ); SHA256_EXP( 7, 0, 1, 2, 3, 4, 5, 6, 49 );
      SHA256_EXP( 6, 7, 0, 1, 2, 3, 4, 5, 50 ); SHA256_EXP( 5, 6, 7, 0, 1, 2, 3, 4, 51 );
      SHA256_EXP( 4, 5, 6, 7, 0, 1, 2, 3, 52 ); SHA256_EXP( 3, 4, 5, 6, 7, 0, 1, 2, 53 );
      SHA256_EXP( 2, 3, 4, 5, 6, 7, 0, 1, 54 ); SHA256_EXP( 1, 2, 3, 4, 5, 6, 7, 0, 55 );
      SHA256_EXP( 0, 1, 2, 3, 4, 5, 6, 7, 56 ); SHA256_EXP( 7, 0, 1, 2, 3, 4, 5, 6, 57 );
      SHA256_EXP( 6, 7, 0, 1, 2, 3, 4, 5, 58 ); SHA256_EXP( 5, 6, 7, 0, 1, 2, 3, 4, 59 );
      SHA256_EXP( 4, 5, 6, 7, 0, 1, 2, 3, 60 ); SHA256_EXP( 3, 4, 5, 6, 7, 0, 1, 2, 61 );
      SHA256_EXP( 2, 3, 4, 5, 6, 7, 0, 1, 62 ); SHA256_EXP( 1, 2, 3, 4, 5, 6, 7, 0, 63 );

      ctx->h[ 0 ] += wv[ 0 ]; ctx->h[ 1 ] += wv[ 1 ];
      ctx->h[ 2 ] += wv[ 2 ]; ctx->h[ 3 ] += wv[ 3 ];
      ctx->h[ 4 ] += wv[ 4 ]; ctx->h[ 5 ] += wv[ 5 ];
      ctx->h[ 6 ] += wv[ 6 ]; ctx->h[ 7 ] += wv[ 7 ];
#endif /* !UNROLL_LOOPS */
   }
}