Example #1
0
void iWA_Crypto_Sha1Interleave(SHA1Context *sha_ctx, BIGNUM *result, BIGNUM *input)
{
#define iWAmacro_CRYPTO_SHA1_INTERLEAVE_INPUT_SIZE        (32)
#define iWAmacro_CRYPTO_SHA1_INTERLEAVE_RESULT_SIZE      (40)

    iWAuint8 t[iWAmacro_CRYPTO_SHA1_INTERLEAVE_INPUT_SIZE];
    iWAuint8 t1[iWAmacro_CRYPTO_SHA1_INTERLEAVE_INPUT_SIZE/2];
    iWAuint8 digest[SHA1HashSize];
    iWAuint8 vK[iWAmacro_CRYPTO_SHA1_INTERLEAVE_RESULT_SIZE];
    iWAint32 i, j;

    BN_bn2bin(input, t);
    
    for(i = 0, j = sizeof(t1) - 1; j >= 0; i++, j--)    t1[j] = t[i * 2];
    SHA1Reset(sha_ctx);    
    SHA1Input(sha_ctx, t1, sizeof(t1));
    SHA1Result(sha_ctx, digest);    
    for(i = 0, j = SHA1HashSize - 1; j >= 0; i++, j--)   vK[i*2] = digest[j];
    
    for(i = 0, j = sizeof(t1) - 1; j >= 0; i++, j--)    t1[j] = t[i * 2 + 1];
    SHA1Reset(sha_ctx);    
    SHA1Input(sha_ctx, t1, sizeof(t1));
    SHA1Result(sha_ctx, digest);    
    for(i = 0, j = SHA1HashSize - 1; j >= 0; i++, j--)   vK[i*2 + 1] = digest[j];

    BN_bin2bn(vK, iWAmacro_CRYPTO_SHA1_INTERLEAVE_RESULT_SIZE, result);   
}
Example #2
0
void OAuth::HMAC_SHA1(SHA1DG dst, const char* key, int klen, const char* src, int n) {
	char tmp_key[128];
	if(klen > 64) {
		SHA1DG keydg;
		SHA1Context sha;
		SHA1Reset(&sha);
		SHA1Input(&sha, (const uint8_t*)key, klen);
		SHA1Result(&sha, keydg);
		HMAC_SHA1(dst, (const char*)keydg, 20, src, n);
		return;
	}

	memcpy(tmp_key, key, klen);
	for(int i=klen ; i<64+1 ; i++)
		tmp_key[i] = 0x00;

	char tmp_0[64], tmp_1[64];
	for(int i=0 ; i<64 ; i++) {
		tmp_0[i] = tmp_key[i] ^ 0x5c;
		tmp_1[i] = tmp_key[i] ^ 0x36;
	}

	SHA1Context sha;
	SHA1Reset(&sha);
	SHA1Input(&sha, (const uint8_t*)tmp_1, 64);
	SHA1Input(&sha, (const uint8_t*)src, n);
	SHA1Result(&sha, dst);

	SHA1Reset(&sha);
	SHA1Input(&sha, (const uint8_t*)tmp_0, 64);
	SHA1Input(&sha, dst, 20);
	SHA1Result(&sha, dst);
}
Example #3
0
void nls_get_x(nls_t* nls, mpz_t x_c, const char* raw_salt) {
    char* userpass;
    uint8_t hash[20], final_hash[20];
    SHA1Context shac;
    
    // build the string Username:Password
    userpass = (char*) malloc(nls->username_len + nls->password_len + 2);
    memcpy(userpass, nls->username, nls->username_len);
    userpass[nls->username_len] = ':';
    memcpy(userpass + nls->username_len + 1, nls->password, nls->password_len);
    userpass[nls->username_len + nls->password_len + 1] = 0; // null-terminator
    
    // get the SHA-1 hash of the string
    SHA1Reset(&shac);
    SHA1Input(&shac, (uint8_t*) userpass,
        (nls->username_len + nls->password_len + 1));
    SHA1Result(&shac, hash);
    free(userpass);
    
    // get the SHA-1 hash of the salt and user:pass hash
    SHA1Reset(&shac);
    SHA1Input(&shac, (uint8_t*) raw_salt, 32);
    SHA1Input(&shac, hash, 20);
    SHA1Result(&shac, final_hash);
    SHA1Reset(&shac);
    
    // create an arbitrary-length integer from the hash and return it
    mpz_init2(x_c, 160);
    mpz_import(x_c, 20, -1, 1, 0, 0, (char*) final_hash);
}
Example #4
0
void hmac_final(hmac_ctx *ctx, unsigned char *hmac)
{
    int i;
    unsigned char hash[0x14];

    SHA1Result(&ctx->hash_ctx);

    // this sha1 implementation is buggy, needs to switch endian
    for(i=0; i<5; ++i) {
        wbe32(hash + 4*i, ctx->hash_ctx.Message_Digest[i]);
    }

    for(i=0; i<0x40; ++i)
        ctx->key[i] ^= 0x36^0x5c; // opad

    SHA1Reset(&ctx->hash_ctx);
    SHA1Input(&ctx->hash_ctx,ctx->key,0x40);
    SHA1Input(&ctx->hash_ctx,hash,0x14);
    SHA1Result(&ctx->hash_ctx);

    for(i=0; i<5; ++i) {
        wbe32(hash + 4*i, ctx->hash_ctx.Message_Digest[i]);
    }
    memcpy(hmac, hash, 20);
}
Example #5
0
void HMAC_SHA1( unsigned char *result,
		unsigned char *key, int key_length,
		unsigned char *text1, int text1_length,
		unsigned char *text2, int text2_length )
{
	unsigned char state1[0x40];
	unsigned char state2[0x40+0x14];
	int i;
	struct SHA1Context context;

	for(i=0x40-1; i>=key_length;--i) state1[i] = 0x36;
	for(;i>=0;--i) state1[i] = key[i] ^ 0x36;

	SHA1Reset(&context);
	SHA1Input(&context,state1,0x40);
	SHA1Input(&context,text1,text1_length);
	SHA1Input(&context,text2,text2_length);
	SHA1Result(&context,&state2[0x40]);

	for(i=0x40-1; i>=key_length;--i) state2[i] = 0x5C;
	for(;i>=0;--i) state2[i] = key[i] ^ 0x5C;

	SHA1Reset(&context);
	SHA1Input(&context,state2,0x40+0x14);
	SHA1Result(&context,result);

}
Example #6
0
static int sha1_hmac( lua_State *L )
{
	SHA1Context sha1,sha2;
	char key[BLOCKSIZE];
	char hash[SHA1_LEN];
	int i;
	size_t len;

	const char *secret,*msg;

	luaL_checkstring(L,1);
	msg = luaL_checkstring(L,2);

	//Get length from LUA (may contain zeroes)
	secret = lua_tolstring(L,1,&len);

	//Fill with zeroes
	memset(key, 0, BLOCKSIZE);

	if (len > BLOCKSIZE) {
		//Too long key, shorten with hash
		SHA1Reset(&sha1);
		SHA1Input(&sha1, (const unsigned char *)secret, len);
		SHA1Result(&sha1);
		get_hash(&sha1, key);
	} else {
		memcpy(key, secret, len);
	}

	//XOR key with IPAD
	for (i=0; i<BLOCKSIZE; i++) {
		key[i] ^= IPAD;
	}

	//First SHA hash
	SHA1Reset(&sha1);
	SHA1Input(&sha1, (const unsigned char *)key, BLOCKSIZE);
	SHA1Input(&sha1, (const unsigned char *)msg, strlen(msg));
	SHA1Result(&sha1);
	get_hash(&sha1, hash);

	//XOR key with OPAD
	for (i=0; i<BLOCKSIZE; i++) {
		key[i] ^= IPAD ^ OPAD;
	}

	//Second hash
	SHA1Reset(&sha2);
	SHA1Input(&sha2, (const unsigned char *)key, BLOCKSIZE);
	SHA1Input(&sha2, (const unsigned char *)hash, SHA1_LEN);
	SHA1Result(&sha2);

	char s[SHA1_STR_LEN];
	for(i = 0; i < 5 ; i++) {
		sprintf(s+i*8,"%.8x", sha2.Message_Digest[i]);
	}
	lua_pushlstring(L,s,SHA1_STR_LEN);
	return 1;
}
Example #7
0
static char *
jingle_get_hmac_sha1(const char *key, int keylen, char *data, int datalen)
{

  unsigned char k_ipad[65];
  unsigned char k_opad[65];
  SHA1Context sha;
  SHA1Context sha2;
  int i;
  uint8_t Message_Digest[20];
  uint8_t *Message_Digest2;
  md5_state_t state;
  md5_byte_t digest[16];
  md5_byte_t *__key = (md5_byte_t *) key;
  int __keylen = keylen;

  x_memset(digest, 0, 16);

  if (__keylen > 64)
    {
      md5_init(&state);
      md5_append(&state, (const md5_byte_t *) __key, __keylen);
      md5_finish(&state, digest);
      __key = digest;
      __keylen = 16;
    }

  x_memset(k_ipad, 0, sizeof(k_ipad));
  x_memset(k_opad, 0, sizeof(k_opad));
  x_memcpy(k_ipad, __key, __keylen);
  x_memcpy(k_opad, __key, __keylen);

  /* XOR key with ipad and opad values */
  for (i = 0; i < 64; i++)
    {
      k_ipad[i] ^= IPAD;
      k_opad[i] ^= OPAD;
    }

  SHA1Reset(&sha);
  SHA1Input(&sha, (const uint8_t *) k_ipad, 64);
  SHA1Input(&sha, (const uint8_t *) data, datalen);
  SHA1Result(&sha, Message_Digest);

  Message_Digest2 = x_malloc(20);
  SHA1Reset(&sha2);
  SHA1Input(&sha2, (const uint8_t *) k_opad, 64);
  SHA1Input(&sha2, (const uint8_t *) Message_Digest, 20);
  SHA1Result(&sha2, Message_Digest2);

  // TRACE("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
  //TRACE("HMAC-SHA1: ");
  // hexdump((char *)Message_Digest2,20);
  //TRACE("\n");

  return (char *) Message_Digest2;
}
Example #8
0
void
BCMROMFN(hmac_sha1)(unsigned char *text, int text_len, unsigned char *key,
                    int key_len, unsigned char *digest)
{
    SHA1Context icontext, ocontext;
    uint32 pki[(PRF_MAX_KEY_LEN / sizeof(uint32)) + 1];
    uint32 pko[(PRF_MAX_KEY_LEN / sizeof(uint32)) + 1];
    uint8 *k_ipad = (uint8 *)pki;
    uint8 *k_opad = (uint8 *)pko;
    int i;

    /* if key is longer than 64 bytes reset it to key = SHA1(key) */
    if (key_len > 64) {
        SHA1Context      tctx;
        SHA1Reset(&tctx);
        SHA1Input(&tctx, key, key_len);
        SHA1Result(&tctx, key);
        key_len = 20;
    }

    /*
     * the HMAC_SHA1 transform looks like:
     *
     * SHA1(K XOR opad, SHA1(K XOR ipad, text))
     *
     * where K is an n byte key
     * ipad is the byte 0x36 repeated 64 times
     * opad is the byte 0x5c repeated 64 times
     * and text is the data being protected
     */

    /* start out by storing key in pads */
    memcpy(k_ipad, key, key_len);
    memset(&k_ipad[key_len], 0, PRF_MAX_KEY_LEN + 1 - key_len);
    memcpy(k_opad, k_ipad, PRF_MAX_KEY_LEN + 1);

    /* XOR key with ipad and opad values */
    for (i = 0; i < 16; i++) {
        pki[i] ^= 0x36363636;
        pko[i] ^= 0x5c5c5c5c;
    }
    /* init contexts */
    SHA1Reset(&icontext);
    memcpy(&ocontext, &icontext, sizeof(icontext));

    /* perform inner SHA1 */
    SHA1Input(&icontext, k_ipad, 64);     /* start with inner pad */
    SHA1Input(&icontext, text, text_len); /* then text of datagram */
    SHA1Result(&icontext, digest);		/* finish up 1st pass */

    /* perform outer SHA1 */
    SHA1Input(&ocontext, k_opad, 64);     /* start with outer pad */
    SHA1Input(&ocontext, digest, 20);     /* then results of 1st hash */
    SHA1Result(&ocontext, digest);          /* finish up 2nd pass */
}
Example #9
0
void iWA_Crypto_TestSHA1(void)
{
    iWAuint8 s[32] = 
    {
        0x98, 0x79, 0x0A, 0x0C, 0x01, 0x51, 0xE9, 0xD7, 
        0xBD, 0x04, 0xC4, 0x3B, 0xC8, 0xBD, 0xC0, 0xD4, 
        0xD7, 0x06, 0x76, 0x12, 0x9C, 0x0B, 0x56, 0x56, 
        0x21, 0xEA, 0x40, 0x7D, 0x10, 0xFF, 0xE7, 0xA5
    };

    iWAuint8 p[20] = 
    {
        0x0C, 0x4B, 0x05, 0xE6, 0x9D, 0x9A, 0x17, 0xDD, 
        0x38, 0xB5, 0x2A, 0x94, 0x42, 0xD7, 0x69, 0xE8, 
        0x5C, 0xB2, 0x32, 0xCA
    };


    iWAint32 ret;

    static SHA1Context ctx;
    static iWAuint8 digest[SHA1HashSize];

    ret = SHA1Reset(&ctx);
    ret = SHA1Input(&ctx, "LOUHAO:LOUHAO", 13);
    ret = SHA1Result(&ctx, digest); // digest should be CA32B25CE869D742942AB538DD179A9DE6054B0C

    {
        int a,b;
        iWAuint8 c;
        for(a=0,b=31;a<16;a++,b--)
        {
            c = s[a];
            s[a] = s[b];
            s[b] = c;
        }
        for(a=0,b=19;a<10;a++,b--)
        {
            c = p[a];
            p[a] = p[b];
            p[b] = c;
        }

    }

    ret = SHA1Reset(&ctx);
    ret = SHA1Input(&ctx, s, 32);
    ret = SHA1Input(&ctx, p, 20);
    ret = SHA1Result(&ctx, digest);  //digest should be 45DC3D9EDAD2D18DDBDC97ED7FAD827CEC84725E
    
    return;
}
Example #10
0
void hmac_sha1(const char*  pBlob,
               size_t nBlob,
               const char*  pKey,
               size_t nKey,
               char*        digest) 
{
    SHA1Context context ;
    char cTempKey[20] ;
    char iPad[64] ;
    char oPad[64] ;
    int i ;

    // If the key length is larger then 64 bytes, replace with hash of the key
    if (nKey > 64)
    {        
        SHA1Reset(&context) ;
        SHA1Input(&context, pKey, nKey) ;
        memset(cTempKey, 0, sizeof(cTempKey)) ;
        SHA1Result(&context, cTempKey) ;

        pKey = cTempKey ;
        nKey = 20 ;
    }

    // Initialize opad and ipad
    memset(iPad, 0, 64) ;
    memset(oPad, 0, 64) ;
    if (pKey)
    {
        memcpy(iPad, pKey, nKey) ;
        memcpy(oPad, pKey, nKey) ;
    }

    for (i=0; i<64; i++)
    {
        iPad[i] ^= 0x36 ;
        oPad[i] ^= 0x5c ;
    }


    // Step one using iPad
    SHA1Reset(&context) ;
    SHA1Input(&context, iPad, 64) ;
    SHA1Input(&context, pBlob, nBlob) ;    
    SHA1Result(&context, digest) ;

    // Step two using oPad
    SHA1Reset(&context) ;
    SHA1Input(&context, oPad, 64) ;
    SHA1Input(&context, digest, 20) ;    
    SHA1Result(&context, digest) ;
}
Example #11
0
key_t
bb_utils_ntok_user(const char* name, int32_t user_specific) {
  key_t s_key;
  int32_t retcode;

  SHA1Context sha;
  uint8_t Message_Digest[20];

  /* We use the first byte of a SHA1 hash of the BBname
   * unless the algorithm fail.
   * If SHA1 fail we go back to poor key generation method
   * using the name length.
   * In both case we must Xored the key with user_specific in order
   * to isolate different user from using the same key
   */
  retcode  = SHA1Reset(&sha);
  retcode &= SHA1Input(&sha, (const unsigned char *) name,strlen(name));
  retcode &= SHA1Result(&sha, Message_Digest);

  /* SHA 1 NOK back to old poor method */
  if (0 != retcode) {
    s_key = ((strlen(name) << 16) & 0xFFFF0000) ^ (user_specific & 0x0000FFFF);
  } else {
    s_key = (Message_Digest[0]        |
	     (Message_Digest[1] << 8) |
	     (Message_Digest[2] << 16)|
	     (Message_Digest[3] << 24)) ^
      user_specific;
  }
  return s_key;
} /* end of bb_utils_ntok_user */
Example #12
0
void GetHashStr(wchar_t *Password, char *HashStr)
{
    HashStr[0]='\0';
	SHA1Context sha;

	SHA1Reset(&sha);
    SHA1Input(&sha, (const unsigned char *) Password, (DWORD)(wcslen(Password)+1)*2);

	if (SHA1Result(&sha)) { 
        // Crea la stringa per la comparazione
		unsigned char *ptr = (unsigned char *)sha.Message_Digest;
        char TmpBuf[128];
        unsigned char tail=0;
		// Calcolo Tail
        for(int i=0; i<20; i++) {
            unsigned char c = ptr[i];
            tail += c;
		}
		for(int i=0; i<5; i++) {
            wsprintf(TmpBuf,"%s%.8X", HashStr, sha.Message_Digest[i]);
            strcpy_s(HashStr, 1024, TmpBuf);
        }
        // Aggiunge gli ultimi 2 byte
        wsprintf(TmpBuf, "%s%2.2X", HashStr, tail);
        strcpy_s(HashStr, 1024, TmpBuf);
	}
}
Example #13
0
void calc_accept_key(char* s, char* r)
{
  const char* magicKey="258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
  char sInput[64]={0x00,};
  char tmpBuf[32]={0x00,};
  strcpy(sInput,s);
  strcpy(sInput+strlen(s),magicKey);
  printf("input: %s\r\n",sInput);
  SHA1Context sha;
  SHA1Reset(&sha);
  SHA1Input(&sha, (const unsigned char *)sInput, strlen(sInput));
  if (!SHA1Result(&sha)){
    printf("ERROR-- could not compute message digest\n");
  }else{
    for(unsigned char i = 0; i < 5 ; i++){
      tmpBuf[i*4+0]=*((char*)&sha.Message_Digest[i]+3);
      tmpBuf[i*4+1]=*((char*)&sha.Message_Digest[i]+2);
      tmpBuf[i*4+2]=*((char*)&sha.Message_Digest[i]+1);
      tmpBuf[i*4+3]=*((char*)&sha.Message_Digest[i]+0);
    }
    for(unsigned char i=0;i<20;i++)
      printf("%02X", tmpBuf[i]);
    printf("\r\n");
    base64encode(tmpBuf,r,20);
  }
}
Example #14
0
void sha1_mac(char *key, size_t key_size, char *message, size_t message_size, char *mac) {
    SHA1Context ctx;
    assert(shaSuccess == SHA1Reset(&ctx));
    assert(shaSuccess == SHA1Input(&ctx, (uint8_t *)key, key_size));
    assert(shaSuccess == SHA1Input(&ctx, (uint8_t *)message, message_size));
    assert(shaSuccess == SHA1Result(&ctx, (uint8_t *)mac));
}
Example #15
0
int main(int argc , char *argv[])
{
    SHA1Context sha;
    int i;
    char sha1hash[70];
    int num =2;
    char ulstring[70];
    sha1hash[0]='\0';
    char command[100];


    SHA1Reset(&sha);
    SHA1Input(&sha, (const unsigned char *) argv[1], strlen(argv[1]));

    if (!SHA1Result(&sha))
    {
        fprintf(stderr, "ERROR-- could not compute message digest\n");
    }
    else
    {
        printf("\t");
        for(i = 0; i < 5 ; i++)
        {
            sprintf(ulstring,"%X", sha.Message_Digest[i]);
            strcat(sha1hash,ulstring);

        }

    }

    printf("\n");
    printf("\t%s\n",sha1hash);

    return 0;
}
Example #16
0
File: sha1.c Project: CUBRID/cubrid
int
SHA1Compute (const unsigned char *message_array, unsigned length, SHA1Hash * hash)
{
  SHA1Context context;

  assert (message_array != NULL);
  assert (hash != NULL);

  SHA1Reset (&context);
  SHA1Input (&context, message_array, length);

  if (!SHA1Result (&context))
    {
      assert (false);
      /* TODO: Set an appropiate error message. */
      er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_GENERIC_ERROR, 0);
      return ER_FAILED;
    }

  hash->h[0] = (INT32) context.Message_Digest[0];
  hash->h[1] = (INT32) context.Message_Digest[1];
  hash->h[2] = (INT32) context.Message_Digest[2];
  hash->h[3] = (INT32) context.Message_Digest[3];
  hash->h[4] = (INT32) context.Message_Digest[4];

  return NO_ERROR;
}
Example #17
0
char *
clicon_sha1hex(const char *str)
{
    int i;
    char *retstr;
    SHA1Context context;
    

    SHA1Reset(&context);
    SHA1Input(&context, (const unsigned char *)str, strlen(str));
    if (!SHA1Result(&context)) {
	clicon_err(OE_UNIX, 0, "Could not compute SHA1 message digest");
	return NULL;
    }
    
    /* 160 bits = 20 bytes = 40 hexdigits + '\0' */
    if ((retstr = malloc(41)) == NULL) {
	clicon_err(OE_UNIX, errno, "malloc");
	return NULL;
    }
    
    for(i = 0; i < 5 ; i++)
	sprintf(retstr + (i*8), "%.8x", context.Message_Digest[i]);
    
    return retstr;
}
Example #18
0
File: main.c Project: xwaynec/eplab
void main()
{

	char i;
	idata SHA1Context sha;

	store_cpu_rate(16);

    P0_DIR &= ~0x28;
    P0_ALT &= ~0x28;

	serial_init(19200);


	for(i=0;i<6;i++)
	{
		blink_led();
		mdelay(400);
	}	
	
	sha_counter++;

	while(1)
	{
		SHA1Reset(&sha);
		SHA1Input(&sha, (const unsigned char *) TESTA, 3);

		SHA1Result(&sha);
		sha_counter++;
		//blink_led();
		int_print(sha_counter);
		puts("\r\n");
	}	

}
Example #19
0
static int get_sha1(const char *path, char *result)
{
	int fd;
	int rv;
	unsigned char buff[4096];
	SHA1Context sha;

	fd = open(path, O_RDONLY);
	if (fd < 0) {
		return -errno;
	}

	SHA1Reset(&sha);
	rv = 1;
	while (rv > 0) {
		rv = read(fd, buff, 4096);
		if (rv < 0) {
			close(fd);
			return -errno;
		}

		SHA1Input(&sha, buff, rv);
	}

	if (!SHA1Result(&sha)) {
		return -1;
	}

	sprintf(result, "%08X%08X%08X%08X%08X",
		sha.Message_Digest[0],
		sha.Message_Digest[1],
		sha.Message_Digest[2],
		sha.Message_Digest[3], sha.Message_Digest[4]);
	return 0;
}
Example #20
0
const char * generate_websocket_resp(const char * req, size_t reqlen, char * dest, size_t destlen)
{
	/* char origin[256]; */
	/* char protocol[256]; */
	/* char key1[256]; */
	/* char key2[256]; */
	char key[256];
	char upgrade[64];
	/* get_header_value(req, reqlen, "Origin", origin, sizeof(origin)); */
	/* get_header_value(req, reqlen, "Sec-WebSocket-Protocol", protocol, sizeof(protocol)); */
	get_header_value(req, reqlen, "Upgrade", upgrade, sizeof(upgrade));
	/* get_header_value(req, reqlen, "Sec-WebSocket-Key1", key1, sizeof(key1)); */
	/* get_header_value(req, reqlen, "Sec-WebSocket-Key2", key2, sizeof(key2)); */
	get_header_value(req, reqlen, "Sec-WebSocket-Key", key, sizeof(key));
	if (key[0] != '\0') {
		char key_sha1[128];
		char key_base64[128];
		SHA1Context sha;
		strcat(key, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
		SHA1Reset(&sha);
		SHA1Input(&sha, ( const unsigned char * )key, strlen(key));
		SHA1Result(&sha);
		memset(key_sha1, 0, sizeof(key_sha1));
		memset(key_base64, 0, sizeof(key_base64));
		int i, j;
		for (i = 0; i < 5; i++) {
			for (j = 0; j < 4; j++) {
				key_sha1[i*4+j] = sha.Message_Digest[i] << (8*j) >> 24;
			}
		}
		base64_encode(key_sha1, strlen(key_sha1), key_base64, sizeof(key_base64));
		snprintf(dest, destlen, "HTTP/1.1 101 Switching Protocols\r\nUpgrade: %s\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: %s\r\n\r\n", upgrade, key_base64);
		return dest;
	}
Example #21
0
/**********************************************
 SumComputeBuff(): Compute the checksum, allocate and
 return a string containing the sum value.
 NOTE: The calling function must free() the string!
 Returns NULL on error.
 **********************************************/
Cksum *	SumComputeBuff	(CksumFile *CF)
{
  int rc;
  SHA1Context sha1;
  MyMD5_CTX md5;
  Cksum *Sum;

  Sum = (Cksum *)calloc(1,sizeof(Cksum));
  if (!Sum) return(NULL);
  Sum->DataLen = CF->MmapSize;

  MyMD5_Init(&md5);
  rc = SHA1Reset(&sha1);
  if (rc)
    {
    fprintf(stderr,"ERROR: Unable to initialize sha1\n");
    free(Sum);
    return(NULL);
    }

  MyMD5_Update(&md5,CF->Mmap,CF->MmapSize);
  MyMD5_Final(Sum->MD5digest,&md5);
  SHA1Input(&sha1,CF->Mmap,CF->MmapSize);
  rc = SHA1Result(&sha1,Sum->SHA1digest);
  if (rc)
    {
    fprintf(stderr,"ERROR: Failed to compute sha1\n");
    free(Sum);
    return(NULL);
    }
  return(Sum);
} /* SumComputeBuff() */
Example #22
0
int sr_handle_auth_request(struct sr_instance* sr, c_auth_request* req) {
#define AUTH_KEY_LEN 64
#define SHA1_LEN 20
    char auth_key[AUTH_KEY_LEN+1];
    FILE* fp;
    SHA1Context sha1;
    c_auth_reply* ar;
    char* buf;
    int len, len_username, i, ret;

    /* read in the user's auth key */
    fp = fopen(sr->auth_key_fn, "r");
    if(fp) {
        if(fgets(auth_key, AUTH_KEY_LEN+1, fp) != auth_key) {
            fclose(fp);
            return 0;
        }
        fclose(fp);

        /* compute the salted SHA1 of password from auth_key */
        SHA1Reset(&sha1);
        SHA1Input(&sha1, req->salt, ntohl(req->mLen) - sizeof(*req));
        SHA1Input(&sha1, (unsigned char*)auth_key, AUTH_KEY_LEN);
        if(!SHA1Result(&sha1)) {
            fprintf(stderr, "SHA1 result could not be computed\n");
            return 0;
        }

        /* build the auth reply packet and then send it */
        len_username = strlen(sr->user);
        len = sizeof(c_auth_reply) + len_username + SHA1_LEN;
        buf = (char*)malloc(len);
        if(!buf) {
            perror("malloc failed");
            return 0;
        }
        ar = (c_auth_reply*)buf;
        ar->mLen = htonl(len);
        ar->mType = htonl(VNS_AUTH_REPLY);
        ar->usernameLen = htonl(len_username);
        strcpy(ar->username, sr->user);
        for(i=0; i<5; i++)
            sha1.Message_Digest[i] = htonl(sha1.Message_Digest[i]);
        memcpy(ar->username + len_username, sha1.Message_Digest, SHA1_LEN);

        if(send(sr->sockfd, buf, len, 0) != len) {
            perror("send(..):sr_client.c::sr_handle_auth_request()");
            ret = 0;
        }
        else
            ret = 1;
        free(buf);
        return ret;
    }
    else {
        perror("unable to read credentials from authentication key file");
        return 0; /* failed */
    }
}
Example #23
0
void check_password()
{
	SHA1Context sha;
	char pass_sha1[20];


	if (password_str_len <= 0)
	{
		if (restricted_screen_mode != RESTRICTED_SCREEN_FILTER_OPTION)
			filter_option_to_set = FILTER_OPTION_TO_SET_NONE;
		restricted_screen_mode = RESTRICTED_SCREEN_CHECK_PASSWORD;
		enter_password_screen(get_nls_text("enter_password"));
	}
	else
	{
		SHA1Reset(&sha);
		SHA1Input(&sha, (const unsigned char *) password_string, password_str_len);
		SHA1Result(&sha);
		memcpy(pass_sha1, sha.Message_Digest, 20);
		if (memcmp(pass_sha1, restriction_pass1, 20))
		{
			password_str_len = 0;
			if (restricted_screen_mode != RESTRICTED_SCREEN_CHANGE_PASSWORD)
				restricted_screen_mode = RESTRICTED_SCREEN_CHECK_PASSWORD;
			enter_password_screen(get_nls_text("try_again"));
		}
		else
		{
			if (restricted_screen_mode == RESTRICTED_SCREEN_CHANGE_PASSWORD)
			{
				password_str_len = 0;
				keyboard_set_mode(KEYBOARD_PASSWORD_CHAR);
				enter_password_screen(get_nls_text("enter_new_password"));
				restricted_screen_mode = RESTRICTED_SCREEN_SET_PASSWORD;
			}
			else if (filter_option_to_set == FILTER_OPTION_TO_SET_ON)
			{
				restriction_filter_off = 0;
				save_password(1);
				last_display_mode = DISPLAY_MODE_ARTICLE;
				display_mode = DISPLAY_MODE_ARTICLE;
				display_retrieved_article(saved_idx_article);
			}
			else if (filter_option_to_set == FILTER_OPTION_TO_SET_OFF)
			{
				restriction_filter_off = 1;
				save_password(2);
				last_display_mode = DISPLAY_MODE_ARTICLE;
				display_mode = DISPLAY_MODE_ARTICLE;
				display_retrieved_article(saved_idx_article);
			}
			else
			{
				display_mode = DISPLAY_MODE_ARTICLE;
				display_retrieved_article(saved_idx_article);
			}
		}
	}
}
Example #24
0
/**
 * Generate new token for given version string.
 */
static char *
tok_generate(time_t now, const char *version)
{
	char token[TOKEN_BASE64_SIZE + 1];
	char digest[TOKEN_VERSION_SIZE];
	char lvldigest[LEVEL_SIZE];
	char lvlbase64[LEVEL_BASE64_SIZE + 1];
	const struct tokkey *tk;
	uint32 crc32;
	uint idx;
	const char *key;
	SHA1Context ctx;
    struct sha1 sha1;
	int lvlsize;
	int i;

	/*
	 * Compute token.
	 */

	key = random_key(now, &idx, &tk);
	now = clock_loc2gmt(now);				/* As close to GMT as possible */

	poke_be32(&digest[0], now);
	random_bytes(&digest[4], 3);
	digest[6] &= 0xe0U;			/* Upper 3 bits only */
	digest[6] |= idx & 0xffU;	/* Has 5 bits for the index */

	SHA1Reset(&ctx);
	SHA1Input(&ctx, key, strlen(key));
	SHA1Input(&ctx, digest, 7);
	SHA1Input(&ctx, version, strlen(version));
	SHA1Result(&ctx, &sha1);
	memcpy(&digest[7], sha1.data, SHA1_RAW_SIZE);

	/*
	 * Compute level.
	 */

	lvlsize = G_N_ELEMENTS(token_keys) - (tk - token_keys);
	crc32 = crc32_update(0, digest, TOKEN_VERSION_SIZE);

	for (i = 0; i < lvlsize; i++) {
		poke_be16(&lvldigest[i*2], tok_crc(crc32, tk));
		tk++;
	}

	/*
	 * Encode into base64.
	 */

	base64_encode_into(digest, TOKEN_VERSION_SIZE, token, TOKEN_BASE64_SIZE);
	token[TOKEN_BASE64_SIZE] = '\0';

	ZERO(&lvlbase64);
	base64_encode_into(lvldigest, 2 * lvlsize, lvlbase64, LEVEL_BASE64_SIZE);

	return g_strconcat(token, "; ", lvlbase64, (void *) 0);
}
Example #25
0
static int
verify_sha1_final(void)
{
	int ret;

	ret = SHA1Result(&verify_sha1.context, &verify_sha1.digest);
	return shaSuccess == ret ? 0 : -1;
}
Example #26
0
int hash_sha1(const uint8_t* data, unsigned int bytes, uint8_t sha1[20])
{
	SHA1Context ctx;
	SHA1Reset(&ctx);
	SHA1Input(&ctx, data, bytes);
	SHA1Result(&ctx, sha1);
	return 0;
}
Example #27
0
/**
 * Calculates the CD-Key hash for use in SID_AUTH_CHECK (0x51)
 * Returns the length of the generated hash; call getHash and pass
 * it a character array that is at least this size.  Returns 0 on failure.
 *
 * Note that clientToken and serverToken will be added to the buffer and
 * hashed as-is, regardless of system endianness.  It is assumed that
 * the program's extraction of the server token does not change its
 * endianness, and since the client token is generated by the client,
 * endianness is not a factor.
 */
size_t CDKeyDecoder::calculateHash(uint32_t clientToken,
    uint32_t serverToken)
{
    struct CDKEYHASH kh;
    SHA1Context sha;
    
    if (!initialized || !keyOK) return 0;
    hashLen = 0;
    
    kh.clientToken = clientToken;
    kh.serverToken = serverToken;
    
    switch (keyType) {
        case KEY_STARCRAFT:
        case KEY_WARCRAFT2:
			kh.product = (uint32_t) LSB4(product);
			kh.value1 = (uint32_t) LSB4(value1);

            kh.value2.s.zero = 0;
            kh.value2.s.v = (uint32_t) LSB4(value2);
            
            keyHash = new char[20];
            calcHashBuf((char*) &kh, 24, keyHash);
            hashLen = 20;

#if DEBUG
			bncsutil_debug_message_a("%s: Hash calculated.", cdkey);
			bncsutil_debug_dump(keyHash, 20);
#endif

            return 20;
        case KEY_WARCRAFT3:
			kh.product = (uint32_t) MSB4(product);
			kh.value1 = (uint32_t) MSB4(value1);
            memcpy(kh.value2.l.v, w3value2, 10);

            if (SHA1Reset(&sha))
                return 0;
            if (SHA1Input(&sha, (const unsigned char*) &kh, 26))
                return 0;
            keyHash = new char[20];
            if (SHA1Result(&sha, (unsigned char*) keyHash)) {
                SHA1Reset(&sha);
                return 0;
            }
            SHA1Reset(&sha);
            hashLen = 20;
			
#if DEBUG
			bncsutil_debug_message_a("%s: Hash calculated.", cdkey);
			bncsutil_debug_dump(keyHash, 20);
#endif

            return 20;
        default:
            return 0;
    }
}
static int sttSaveAvatar()
{
	FILE* in;
	SHA1Context sha;
//	MD5Context md5;
	unsigned char buf[ 512 ];
	char tFileName[ MAX_PATH ];
	int i, pictureType;
	int bIsFirst = 1;
	struct _stat statbuf;
	uint8_t digest[20];

	if (_stat(szFileName, &statbuf)) return 1;
	if (statbuf.st_size > 6 * 1024) return 1;
	
	TlenGetAvatarFileName( NULL, tFileName, sizeof tFileName );
	if ( CopyFileA( szFileName, tFileName, FALSE ) == FALSE ) {
		JabberLog( "Copy failed with error %d", GetLastError() );
		return 1;
	}
	SHA1Reset(&sha);
//	md5_init(&md5);
	in = fopen( tFileName, "rb" );
	if ( in == NULL )
		return 1;

	while( !feof( in )) {
		int bytes = fread( buf, 1, sizeof buf, in );
		if ( bIsFirst ) {
			pictureType = JabberGetPictureType( buf );
			bIsFirst = 0;
		}
		SHA1Input(&sha, buf, bytes);
//		md5_update(&md5, buf, bytes);
	}
	fclose( in );
	if ( pictureType == PA_FORMAT_UNKNOWN )
		return 1;
	SHA1Result(&sha, digest);
//	md5_finalize(&md5);
	for (i=0;i<20;i++) {
//		unsigned int val = (md5.state[i>>2] >> 8*(i%4)) & 0xFF;
		sprintf( buf+( i<<1 ), "%02x", digest[i]);
	}
	DBWriteContactSettingString(NULL, jabberProtoName, "AvatarHash", buf);
	DBWriteContactSettingDword(NULL, jabberProtoName, "AvatarFormat", pictureType);
	if (userAvatarHash != NULL) {
		free (userAvatarHash);
		userAvatarHash = NULL;
	}
	userAvatarHash = strdup(buf);
	userAvatarFormat = pictureType;
	TlenGetAvatarFileName(NULL, szFileName, MAX_PATH);
	if ( strcmp( szFileName, tFileName ))
		MoveFileA( tFileName, szFileName );
	return 0;
}
Example #29
0
MEXP(void) nls_get_K(nls_t* nls, char* out, const char* S) {
    char odd[16], even[16];
    uint8_t odd_hash[20], even_hash[20];
    
    char* Sp = (char*) S;
    char* op = odd;
    char* ep = even;
    unsigned int i;
    
    SHA1Context ctx;

	if (!nls)
		return;

	if (nls->K) {
		memcpy(out, nls->K, 40);
		return;
	}
    
    for (i = 0; i < 16; i++) {
        *(op++) = *(Sp++);
        *(ep++) = *(Sp++);
    }
    
    SHA1Reset(&ctx);
    SHA1Input(&ctx, (uint8_t*) odd, 16);
    SHA1Result(&ctx, odd_hash);
    
    SHA1Reset(&ctx);
    SHA1Input(&ctx, (uint8_t*) even, 16);
    SHA1Result(&ctx, even_hash);
    
    Sp = out;
    op = (char*) odd_hash;
    ep = (char*) even_hash;
    for (i = 0; i < 20; i++) {
        *(Sp++) = *(op++);
        *(Sp++) = *(ep++);
    }

	nls->K = (char*) malloc(40);
	if (nls->K)
		memcpy(nls->K, out, 40);
}
Example #30
0
// Private non-vararg version of sha1
// Accepts size and an array of bytes.
// Returns a pointer to a 20-byte hash.
uint8_t* _sha1(int size, uint8_t* buffer) {
  SHA1Context context;
  SHA1Reset(&context);
  SHA1Input(&context, buffer, size);

  uint8_t* hash = malloc(20 * sizeof(uint8_t));
  if( SHA1Result(&context, hash) != shaSuccess ) { return NULL; }

  return hash;
}