Ejemplo n.º 1
0
int wc_Sha256Final(Sha256* sha256, byte* hash)
{
    byte* local = (byte*)sha256->buffer;
    int ret;

    AddLength(sha256, sha256->buffLen);  /* before adding pads */

    local[sha256->buffLen++] = 0x80;     /* add 1 */

    /* pad with zeros */
    if (sha256->buffLen > SHA256_PAD_SIZE) {
        XMEMSET(&local[sha256->buffLen], 0, SHA256_BLOCK_SIZE - sha256->buffLen);
        sha256->buffLen += SHA256_BLOCK_SIZE - sha256->buffLen;

        #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
            ByteReverseWords(sha256->buffer, sha256->buffer, SHA256_BLOCK_SIZE);
        #endif

        ret = XTRANSFORM(sha256, local);
        if (ret != 0)
            return ret;

        sha256->buffLen = 0;
    }
    XMEMSET(&local[sha256->buffLen], 0, SHA256_PAD_SIZE - sha256->buffLen);

    /* put lengths in bits */
    sha256->hiLen = (sha256->loLen >> (8*sizeof(sha256->loLen) - 3)) +
                 (sha256->hiLen << 3);
    sha256->loLen = sha256->loLen << 3;

    /* store lengths */
    #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
        ByteReverseWords(sha256->buffer, sha256->buffer, SHA256_BLOCK_SIZE);
    #endif
    /* ! length ordering dependent on digest endian type ! */
    XMEMCPY(&local[SHA256_PAD_SIZE], &sha256->hiLen, sizeof(word32));
    XMEMCPY(&local[SHA256_PAD_SIZE + sizeof(word32)], &sha256->loLen,
            sizeof(word32));

    #ifdef FREESCALE_MMCAU
        /* Kinetis requires only these bytes reversed */
        ByteReverseWords(&sha256->buffer[SHA256_PAD_SIZE/sizeof(word32)],
                         &sha256->buffer[SHA256_PAD_SIZE/sizeof(word32)],
                         2 * sizeof(word32));
    #endif

    ret = XTRANSFORM(sha256, local);
    if (ret != 0)
        return ret;

    #ifdef LITTLE_ENDIAN_ORDER
        ByteReverseWords(sha256->digest, sha256->digest, SHA256_DIGEST_SIZE);
    #endif
    XMEMCPY(hash, sha256->digest, SHA256_DIGEST_SIZE);

    return wc_InitSha256(sha256);  /* reset state */
}
Ejemplo n.º 2
0
int wc_ShaUpdate(Sha* sha, const byte* data, word32 len)
{
    /* do block size increments */
    byte* local = (byte*)sha->buffer;

    while (len) {
        word32 add = min(len, SHA_BLOCK_SIZE - sha->buffLen);
        XMEMCPY(&local[sha->buffLen], data, add);

        sha->buffLen += add;
        data         += add;
        len          -= add;

        if (sha->buffLen == SHA_BLOCK_SIZE) {
#if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
            ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE);
#endif
            XTRANSFORM(sha, local);
            AddLength(sha, SHA_BLOCK_SIZE);
            sha->buffLen = 0;
        }
    }

    return 0;
}
Ejemplo n.º 3
0
int wc_Sha256Update(Sha256 *sha256, const byte *data, word32 len)
{
	/* do block size increments */
	byte *local = (byte *)sha256->buffer;

	while (len) {
		word32 add = min(len, SHA256_BLOCK_SIZE - sha256->buffLen);
		XMEMCPY(&local[sha256->buffLen], data, add);

		sha256->buffLen += add;
		data            += add;
		len             -= add;

		if (sha256->buffLen == SHA256_BLOCK_SIZE) {
			int ret;

			ByteReverseWords(sha256->buffer, sha256->buffer,
								 SHA256_BLOCK_SIZE);
			ret = XTRANSFORM(sha256, local);
			if (ret != 0)
				return ret;

			AddLength(sha256, SHA256_BLOCK_SIZE);
			sha256->buffLen = 0;
		}
	}

	return 0;
}
Ejemplo n.º 4
0
individual_tPtr _AddNormalXMute(param_tPtr eps, individual_tPtr ind)
	 
{
  unsigned long	i, XLength, SigmaLength;
  
  if((XLength = inGetXLength(ind)) <
	 (SigmaLength = inGetSigmaLength(ind)))
	panic(A_FATAL, "_AddXNormalMute", "sigma length greater than x length : %s : %d",
		  __FILE__, __LINE__);
  
  for(i = 1; i <= SigmaLength; i++) {
	inAssignXComponent(i, 
					   XTRANSFORM(eps, ind, inGetXComponent(i, ind),
								  inGetXComponent(i, ind) +
								  inGetSigmaComponent(i, ind) * normal(1.0)), 
					   ind);
  }
  
  /*
   *	If there are fewer different standard deviations available
   *	than the dimension of the objective function requires, the
   *      last standard deviation is responsible for ALL remaining
   *      object variables (eps->SigComp == SIGCOMP_FIX) [Schw77].
   *	If eps->SigComp == SIGCOMP_RAN, then the coupling between the
   *	remaining x_i and the standard deviations is choosen at random.
   */
  
  for(i = SigmaLength + 1; i <= XLength; i++) {
	if((eps->SigComp == SIGCOMP_FIX) || (1 == SigmaLength)) {
	  inAssignXComponent(i,
						 XTRANSFORM(eps, ind, inGetXComponent(i, ind),
									inGetXComponent(i, ind) +
									inGetSigmaComponent(SigmaLength, ind) * normal(1.0)),
						 ind);
	}
	else {
	  inAssignXComponent(i,
						 XTRANSFORM(eps, ind, inGetXComponent(i, ind),
									inGetXComponent(i, ind) +
									inGetSigmaComponent((unsigned long) rnd(1,SigmaLength), ind) * 
									normal(1.0)),
						 ind);
	}
  }
  
  return(ind);
}
Ejemplo n.º 5
0
int wc_Sha256Final(Sha256 *sha256, byte *hash)
{
	byte *local = (byte *)sha256->buffer;
	int ret;

	AddLength(sha256, sha256->buffLen);  /* before adding pads */

	local[sha256->buffLen++] = 0x80;     /* add 1 */

	/* pad with zeros */
	if (sha256->buffLen > SHA256_PAD_SIZE) {
		XMEMSET(&local[sha256->buffLen], 0, SHA256_BLOCK_SIZE - sha256->buffLen);
		sha256->buffLen += SHA256_BLOCK_SIZE - sha256->buffLen;

		ByteReverseWords(sha256->buffer, sha256->buffer, SHA256_BLOCK_SIZE);

		ret = XTRANSFORM(sha256, local);
		if (ret != 0)
			return ret;

		sha256->buffLen = 0;
	}
	XMEMSET(&local[sha256->buffLen], 0, SHA256_PAD_SIZE - sha256->buffLen);

	/* put lengths in bits */
	sha256->hiLen = (sha256->loLen >> (8 * sizeof(sha256->loLen) - 3)) +
				 (sha256->hiLen << 3);
	sha256->loLen = sha256->loLen << 3;

	/* store lengths */
	ByteReverseWords(sha256->buffer, sha256->buffer, SHA256_BLOCK_SIZE);

	/* ! length ordering dependent on digest endian type ! */
	XMEMCPY(&local[SHA256_PAD_SIZE], &sha256->hiLen, sizeof(word32));
	XMEMCPY(&local[SHA256_PAD_SIZE + sizeof(word32)], &sha256->loLen,
			sizeof(word32));

	ret = XTRANSFORM(sha256, local);
	if (ret != 0)
		return ret;

	ByteReverseWords(sha256->digest, sha256->digest, SHA256_DIGEST_SIZE);
	XMEMCPY(hash, sha256->digest, SHA256_DIGEST_SIZE);

	return wc_InitSha256(sha256);  /* reset state */
}
Ejemplo n.º 6
0
void Md5Final(Md5* md5, byte* hash)
{
    byte* local = (byte*)md5->buffer;

    AddLength(md5, md5->buffLen);  /* before adding pads */

    local[md5->buffLen++] = 0x80;  /* add 1 */

    /* pad with zeros */
    if (md5->buffLen > MD5_PAD_SIZE) {
        XMEMSET(&local[md5->buffLen], 0, MD5_BLOCK_SIZE - md5->buffLen);
        md5->buffLen += MD5_BLOCK_SIZE - md5->buffLen;

        #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
            ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE);
        #endif
        XTRANSFORM(md5, local);
        md5->buffLen = 0;
    }
    XMEMSET(&local[md5->buffLen], 0, MD5_PAD_SIZE - md5->buffLen);
   
    /* put lengths in bits */
    md5->hiLen = (md5->loLen >> (8*sizeof(md5->loLen) - 3)) + 
                 (md5->hiLen << 3);
    md5->loLen = md5->loLen << 3;

    /* store lengths */
    #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
        ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE);
    #endif
    /* ! length ordering dependent on digest endian type ! */
    XMEMCPY(&local[MD5_PAD_SIZE], &md5->loLen, sizeof(word32));
    XMEMCPY(&local[MD5_PAD_SIZE + sizeof(word32)], &md5->hiLen, sizeof(word32));

    XTRANSFORM(md5, local);
    #ifdef BIG_ENDIAN_ORDER
        ByteReverseWords(md5->digest, md5->digest, MD5_DIGEST_SIZE);
    #endif
    XMEMCPY(hash, md5->digest, MD5_DIGEST_SIZE);

    InitMd5(md5);  /* reset state */
}
Ejemplo n.º 7
0
int wc_ShaFinal(Sha* sha, byte* hash)
{
    byte* local = (byte*)sha->buffer;

    AddLength(sha, sha->buffLen);  /* before adding pads */

    local[sha->buffLen++] = 0x80;  /* add 1 */

    /* pad with zeros */
    if (sha->buffLen > SHA_PAD_SIZE) {
        XMEMSET(&local[sha->buffLen], 0, SHA_BLOCK_SIZE - sha->buffLen);
        sha->buffLen += SHA_BLOCK_SIZE - sha->buffLen;

#if defined(LITTLE_ENDIAN_ORDER)
        ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE);
#endif
        XTRANSFORM(sha, local);
        sha->buffLen = 0;
    }
    XMEMSET(&local[sha->buffLen], 0, SHA_PAD_SIZE - sha->buffLen);

    /* put lengths in bits */
    sha->hiLen = (sha->loLen >> (8*sizeof(sha->loLen) - 3)) +
    (sha->hiLen << 3);
    sha->loLen = sha->loLen << 3;

    /* store lengths */
#if defined(LITTLE_ENDIAN_ORDER)
    ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE);
#endif
    /* ! length ordering dependent on digest endian type ! */
    XMEMCPY(&local[SHA_PAD_SIZE], &sha->hiLen, sizeof(word32));
    XMEMCPY(&local[SHA_PAD_SIZE + sizeof(word32)], &sha->loLen, sizeof(word32));

    XTRANSFORM(sha, local);
#ifdef LITTLE_ENDIAN_ORDER
    ByteReverseWords(sha->digest, sha->digest, SHA_DIGEST_SIZE);
#endif
    XMEMCPY(hash, sha->digest, SHA_DIGEST_SIZE);

    return wc_InitSha(sha);  /* reset state */
}
Ejemplo n.º 8
0
void Md5Update(Md5* md5, const byte* data, word32 len)
{
    /* do block size increments */
    byte* local = (byte*)md5->buffer;

    while (len) {
        word32 add = min(len, MD5_BLOCK_SIZE - md5->buffLen);
        XMEMCPY(&local[md5->buffLen], data, add);

        md5->buffLen += add;
        data         += add;
        len          -= add;

        if (md5->buffLen == MD5_BLOCK_SIZE) {
            #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
                ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE);
            #endif
            XTRANSFORM(md5, local);
            AddLength(md5, MD5_BLOCK_SIZE);
            md5->buffLen = 0;
        }
    }
}
Ejemplo n.º 9
0
void Sha256Update(Sha256* sha256, const byte* data, word32 len)
{
    /* do block size increments */
    byte* local = (byte*)sha256->buffer;

    while (len) {
        word32 add = min(len, SHA256_BLOCK_SIZE - sha256->buffLen);
        XMEMCPY(&local[sha256->buffLen], data, add);

        sha256->buffLen += add;
        data            += add;
        len             -= add;

        if (sha256->buffLen == SHA256_BLOCK_SIZE) {
            #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
                ByteReverseBytes(local, local, SHA256_BLOCK_SIZE);
            #endif
            XTRANSFORM(sha256, local);
            AddLength(sha256, SHA256_BLOCK_SIZE);
            sha256->buffLen = 0;
        }
    }
}
Ejemplo n.º 10
0
individual_tPtr _AddCorrNormalXMute(param_tPtr eps, individual_tPtr ind)
	 
{
  /*
   *	The underlying idea of the additiv correlated mutation of the x gen
   *	is a coordinate transformation T with respect to the axes i and j
   *	and the angle alpha_(i,j).
   *	The mutation process works like :
   *	x_i' = x_i + sigma^c_i, where sigma^c is the correlated vector
   *	of the realization vector (N(0,sigma_1), ... , N(0,sigma_n)) = sigma^u.
   *	sigma^c = T(sigma^u). For the creation of sigma^u the vector
   *	sigma of standard deviations has to be completed, either fixed,
   *	the last position sigma_(n_sigma) or at random. (see Baeck, p. 64)
   */
  
  unsigned long   i, k,
	n1, n2,
	XLength, SigmaLength, AlphaLength;
  double		d1, d2, S, C;
  vector_tPtr	sigma_u_c = NULL;
  
  
  if((XLength = inGetXLength(ind)) <
	 (SigmaLength = inGetSigmaLength(ind)))
	panic(A_FATAL, "_AddXCorrNormalMute", "sigma length greater than x length : %s : %d",
		  __FILE__, __LINE__);
  
  if(1 == SigmaLength)	/* Only normal mutation is possible */
	return(_AddNormalXMute(eps, ind));
  
  if(0 == SigmaLength)
	panic(A_FATAL, "_AddXCorrNormalMute", "no correlation possible for sigma length = 0 : %s : %d",
		  __FILE__, __LINE__);		      
  
  if(0 == (AlphaLength = inGetAlphaLength(ind)))
	panic(A_FATAL, "_AddXCorrNormalMute", "no correlation possible for alpha length = 0 : %s : %d",
		  __FILE__, __LINE__);		      
  
  sigma_u_c = veNewVector(XLength);
  
  /* creation of sigma^u */
  for(i = 1; i <= XLength && i <= SigmaLength; i++)
	veAssignVectorComponent(i, inGetSigmaComponent(i, ind) * normal(1.0),
							sigma_u_c);
  for(k = i; k <= XLength; k++)
	if(eps->SigComp == SIGCOMP_FIX)
	  veAssignVectorComponent(k,
							  inGetSigmaComponent(SigmaLength, ind) * normal(1.0),
							  sigma_u_c);
	else
	  veAssignVectorComponent(k, inGetSigmaComponent((unsigned long) rnd(1,SigmaLength), ind) * 
							  normal(1.0),
							  sigma_u_c);
  
  /* coordinate transformation */
  for (k = XLength - SigmaLength + 1; k < XLength; k++) {
	n1 = XLength - k;
	n2 = XLength;
	for (i = 1; i <= k; i++) {
	  d1 = veGetVectorComponent(n1, sigma_u_c);
	  d2 = veGetVectorComponent(n2, sigma_u_c);
	  S  = sin(inGetAlphaComponent(AlphaLength, ind));
	  C  = cos(inGetAlphaComponent(AlphaLength, ind));
	  veAssignVectorComponent(n2, d1 * S + d2 * C,
							  sigma_u_c);
	  veAssignVectorComponent(n1, d1 * C - d2 * S,
							  sigma_u_c);
	  n2--;
	  AlphaLength--;
	}
  }
  
  /*  
  for (k = XLength - SigmaLength; k < XLength - 1; k++) {
	n1 = XLength - k - 1;
	n2 = XLength - 1;
	for (i = 0; i < k; i++) {
	  d1 = veGetVectorComponent(n1 + 1, sigma_u_c);
	  d2 = veGetVectorComponent(n2 + 1, sigma_u_c);
	  S  = sin(inGetAlphaComponent(AlphaLength, ind));
	  C  = cos(inGetAlphaComponent(AlphaLength, ind));
	  veAssignVectorComponent(n2 + 1, d1 * S + d2 * C,
							  sigma_u_c);
	  veAssignVectorComponent(n1 + 1, d1 * C - d2 * S,
							  sigma_u_c);
	  n2--;
	  AlphaLength--;
	}
  }
  */  	    
  for(i = 1; i <= XLength; i++)
	inAssignXComponent(i, XTRANSFORM(eps, ind, inGetXComponent(i, ind),
									 inGetXComponent(i, ind) +
									 veGetVectorComponent(i, sigma_u_c)), ind);
  
  sigma_u_c = veDeleteVector(sigma_u_c);
  
  return(ind);
}