コード例 #1
0
void hmac_sha1_init(hmac_sha1_ctx_t *s, const void* key, uint16_t keylength_b){
	uint8_t buffer[SHA1_BLOCK_BYTES];
	uint8_t i;
	
	memset(buffer, 0, SHA1_BLOCK_BYTES);
	if (keylength_b > SHA1_BLOCK_BITS){
		sha1((void*)buffer, key, keylength_b);
	} else {
		memcpy(buffer, key, (keylength_b+7)/8);
	}
	
	for (i=0; i<SHA1_BLOCK_BYTES; ++i){
		buffer[i] ^= IPAD;
	}
	sha1_init(&(s->a));
	sha1_nextBlock(&(s->a), buffer);
	
	for (i=0; i<SHA1_BLOCK_BYTES; ++i){
		buffer[i] ^= IPAD^OPAD;
	}
	sha1_init(&(s->b));
	sha1_nextBlock(&(s->b), buffer);
	
	
#if defined SECURE_WIPE_BUFFER
	memset(buffer, 0, SHA1_BLOCK_BYTES);
#endif
}
コード例 #2
0
ファイル: hmac.c プロジェクト: PHPDOTSQL/fwknop
/* Begin SHA1 HMAC functions
*/
static void
hmac_sha1_init(hmac_sha1_ctx *ctx, const char *key, const int key_len)
{
    unsigned char  final_key[MAX_DIGEST_BLOCK_LEN] = {0};
    unsigned char  init_key[MAX_DIGEST_BLOCK_LEN]  = {0};
    int            final_len = key_len;

    if(key_len > MAX_DIGEST_BLOCK_LEN)
        final_len = MAX_DIGEST_BLOCK_LEN;

    memcpy(init_key, key, final_len);

    if(SHA1_BLOCK_LEN < key_len)
    {
        /* Calculate the digest of the key
        */
        sha1(final_key, init_key, final_len);
    }
    else
    {
        memcpy(final_key, init_key, key_len);
    }

    pad_init(ctx->block_inner_pad, ctx->block_outer_pad, final_key, final_len);

    sha1_init(&ctx->ctx_inside);
    sha1_update(&ctx->ctx_inside, ctx->block_inner_pad, SHA1_BLOCK_LEN);

    sha1_init(&ctx->ctx_outside);
    sha1_update(&ctx->ctx_outside, ctx->block_outer_pad, SHA1_BLOCK_LEN);

    return;
}
コード例 #3
0
ファイル: sha1.c プロジェクト: ColinBS/bird
void
sha1_hmac_init(struct sha1_hmac_context *ctx, const byte *key, uint keylen)
{
  byte keybuf[SHA1_BLOCK_SIZE], buf[SHA1_BLOCK_SIZE];

  /* Hash the key if necessary */
  if (keylen <= SHA1_BLOCK_SIZE)
  {
    memcpy(keybuf, key, keylen);
    memset(keybuf + keylen, 0, SHA1_BLOCK_SIZE - keylen);
  }
  else
  {
    sha1_hash_buffer(keybuf, key, keylen);
    memset(keybuf + SHA1_SIZE, 0, SHA1_BLOCK_SIZE - SHA1_SIZE);
  }

  /* Initialize the inner digest */
  sha1_init(&ctx->ictx);
  int i;
  for (i = 0; i < SHA1_BLOCK_SIZE; i++)
    buf[i] = keybuf[i] ^ 0x36;
  sha1_update(&ctx->ictx, buf, SHA1_BLOCK_SIZE);

  /* Initialize the outer digest */
  sha1_init(&ctx->octx);
  for (i = 0; i < SHA1_BLOCK_SIZE; i++)
    buf[i] = keybuf[i] ^ 0x5c;
  sha1_update(&ctx->octx, buf, SHA1_BLOCK_SIZE);
}
コード例 #4
0
static void test_compress_file(const char *in_path, const char *out_path)
{
	const struct compression_handler *handler;
	struct istream *input, *file_input;
	struct ostream *output, *file_output;
	int fd_in, fd_out;
	struct sha1_ctxt sha1;
	unsigned char output_sha1[SHA1_RESULTLEN], input_sha1[SHA1_RESULTLEN];
	const unsigned char *data;
	size_t size;
	ssize_t ret;

	handler = compression_lookup_handler_from_ext(out_path);
	if (handler == NULL)
		i_fatal("Can't detect compression algorithm from path %s", out_path);
	if (handler->create_ostream == NULL)
		i_fatal("Support not compiled in for %s", handler->name);

	/* write the compressed output file */
	fd_in = open(in_path, O_RDONLY);
	if (fd_in == -1)
		i_fatal("open(%s) failed: %m", in_path);
	fd_out = open(out_path, O_TRUNC | O_CREAT | O_RDWR, 0600);
	if (fd_out == -1)
		i_fatal("creat(%s) failed: %m", out_path);

	sha1_init(&sha1);
	file_output = o_stream_create_fd_file(fd_out, 0, FALSE);
	output = handler->create_ostream(file_output, 1);
	input = i_stream_create_fd_autoclose(&fd_in, IO_BLOCK_SIZE);
	while (i_stream_read_data(input, &data, &size, 0) > 0) {
		sha1_loop(&sha1, data, size);
		o_stream_nsend(output, data, size);
		i_stream_skip(input, size);
	}
	if (o_stream_nfinish(output) < 0) {
		i_fatal("write(%s) failed: %s",
			out_path, o_stream_get_error(output));
	}
	i_stream_destroy(&input);
	o_stream_destroy(&output);
	o_stream_destroy(&file_output);
	sha1_result(&sha1, output_sha1);

	/* verify that we can read the compressed file */
	sha1_init(&sha1);
	file_input = i_stream_create_fd(fd_out, IO_BLOCK_SIZE, FALSE);
	input = handler->create_istream(file_input, FALSE);
	while ((ret = i_stream_read_data(input, &data, &size, 0)) > 0) {
		sha1_loop(&sha1, data, size);
		i_stream_skip(input, size);
	}
	i_stream_destroy(&input);
	i_stream_destroy(&file_input);
	sha1_result(&sha1, input_sha1);

	if (memcmp(input_sha1, output_sha1, sizeof(input_sha1)) != 0)
		i_fatal("Decompression couldn't get the original input");
	i_close_fd(&fd_out);
}
コード例 #5
0
/*
 * keylength in bits!
 * message length in bits!
 */
void hmac_sha1(void* dest, const void* key, uint16_t keylength_b, const void* msg, uint32_t msglength_b){ /* a one-shot*/
	sha1_ctx_t s;
	uint8_t i;
	uint8_t buffer[SHA1_BLOCK_BYTES];
	
	memset(buffer, 0, SHA1_BLOCK_BYTES);
	
	/* if key is larger than a block we have to hash it*/
	if (keylength_b > SHA1_BLOCK_BITS){
		sha1((void*)buffer, key, keylength_b);
	} else {
		memcpy(buffer, key, (keylength_b+7)/8);
	}
	
	for (i=0; i<SHA1_BLOCK_BYTES; ++i){
		buffer[i] ^= IPAD;
	}
	sha1_init(&s);
	sha1_nextBlock(&s, buffer);
	while (msglength_b >= SHA1_BLOCK_BITS){
		sha1_nextBlock(&s, msg);
		msg = (uint8_t*)msg + SHA1_BLOCK_BYTES;
		msglength_b -=  SHA1_BLOCK_BITS;
	}
	sha1_lastBlock(&s, msg, msglength_b);
	/* since buffer still contains key xor ipad we can do ... */
	for (i=0; i<SHA1_BLOCK_BYTES; ++i){
		buffer[i] ^= IPAD ^ OPAD;
	}
	sha1_ctx2hash(dest, &s); /* save inner hash temporary to dest */
	sha1_init(&s);
	sha1_nextBlock(&s, buffer);
	sha1_lastBlock(&s, dest, SHA1_HASH_BITS);
	sha1_ctx2hash(dest, &s);
}
コード例 #6
0
ファイル: cryb_hmac_sha1.c プロジェクト: cryb-to/cryb-to
void
hmac_sha1_init(hmac_sha1_ctx *ctx, const void *key, size_t keylen)
{
	uint8_t keybuf[SHA1_BLOCK_LEN], pad[SHA1_BLOCK_LEN];

	/* prepare key */
	memset(keybuf, 0, sizeof keybuf);
        if (keylen > sizeof keybuf)
                sha1_complete(key, keylen, keybuf);
        else
                memcpy(keybuf, key, keylen);

	/* input pad */
	for (unsigned int i = 0; i < sizeof pad; ++i)
		pad[i] = 0x36 ^ keybuf[i];
	sha1_init(&ctx->ictx);
	sha1_update(&ctx->ictx, pad, sizeof pad);

	/* output pad */
	for (unsigned int i = 0; i < sizeof pad; ++i)
		pad[i] = 0x5c ^ keybuf[i];
	sha1_init(&ctx->octx);
	sha1_update(&ctx->octx, pad, sizeof pad);

	/* hide the evidence */
	memset(keybuf, 0, sizeof keybuf);
	memset(pad, 0, sizeof pad);
}
コード例 #7
0
ファイル: hmac.c プロジェクト: 42p/google-authenticator
void hmac_sha1(const uint8_t *key, int keyLength,
               const uint8_t *data, int dataLength,
               uint8_t *result, int resultLength) {
  SHA1_INFO ctx;
  uint8_t hashed_key[SHA1_DIGEST_LENGTH];
  if (keyLength > 64) {
    // The key can be no bigger than 64 bytes. If it is, we'll hash it down to
    // 20 bytes.
    sha1_init(&ctx);
    sha1_update(&ctx, key, keyLength);
    sha1_final(&ctx, hashed_key);
    key = hashed_key;
    keyLength = SHA1_DIGEST_LENGTH;
  }

  // The key for the inner digest is derived from our key, by padding the key
  // the full length of 64 bytes, and then XOR'ing each byte with 0x36.
  uint8_t tmp_key[64];
  for (int i = 0; i < keyLength; ++i) {
    tmp_key[i] = key[i] ^ 0x36;
  }
  if (keyLength < 64) {
    memset(tmp_key + keyLength, 0x36, 64 - keyLength);
  }

  // Compute inner digest
  sha1_init(&ctx);
  sha1_update(&ctx, tmp_key, 64);
  sha1_update(&ctx, data, dataLength);
  uint8_t sha[SHA1_DIGEST_LENGTH];
  sha1_final(&ctx, sha);

  // The key for the outer digest is derived from our key, by padding the key
  // the full length of 64 bytes, and then XOR'ing each byte with 0x5C.
  for (int i = 0; i < keyLength; ++i) {
    tmp_key[i] = key[i] ^ 0x5C;
  }
  memset(tmp_key + keyLength, 0x5C, 64 - keyLength);

  // Compute outer digest
  sha1_init(&ctx);
  sha1_update(&ctx, tmp_key, 64);
  sha1_update(&ctx, sha, SHA1_DIGEST_LENGTH);
  sha1_final(&ctx, sha);

  // Copy result to output buffer and truncate or pad as necessary
  memset(result, 0, resultLength);
  if (resultLength > SHA1_DIGEST_LENGTH) {
    resultLength = SHA1_DIGEST_LENGTH;
  }
  memcpy(result, sha, resultLength);

  // Zero out all internal data structures
  memset(hashed_key, 0, sizeof(hashed_key));
  memset(sha, 0, sizeof(sha));
  memset(tmp_key, 0, sizeof(tmp_key));
}
コード例 #8
0
ファイル: sha1.c プロジェクト: timgarbos/doorduino
int
main(int argc, char *argv[])
{
	unsigned int i;
	struct sha1_context ctx;
	char digest[SHA1_DIGEST_LENGTH];
	char output[2*SHA1_DIGEST_LENGTH + 5];

	/* silence gcc -Wextra */
	(void)argc;
	(void)argv;

	printf("Verifying SHA-1 implementation... ");
	fflush(stdout);

	for (i = 0; i < sizeof(test_data) / sizeof(test_data[0]); i++) {
		sha1_init(&ctx);
		sha1_update(&ctx, test_data[i], strlen(test_data[i]));
		sha1_final(&ctx, digest);
		digest_to_hex(digest, output);

		if (strcmp(output, test_results[i])) {
			fprintf(stdout, "FAIL\n");
			fprintf(stderr, "* hash of \"%s\" incorrect:\n"
			                "\t%s returned\n"
			                "\t%s is correct\n",
			                test_data[i], output, test_results[i]);
			return EXIT_FAILURE;
		}
	}

	/* the million 'a' vector we feed separately */
	sha1_init(&ctx);
	for (i = 0; i < 1000000; i++)
		sha1_update(&ctx, "a", 1);
	sha1_final(&ctx, digest);
	digest_to_hex(digest, output);

	if (strcmp(output, "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F")) {
		fprintf(stdout, "FAIL\n");
		fprintf(stderr, "* hash of a million a's is incorrect:\n"
		                "\t%s returned\n"
		                "\t34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F"
		                " is correct\n", output);
		return 1;
	}

	printf("OK\n");
	fflush(stdout);

	return EXIT_SUCCESS;
}
コード例 #9
0
ファイル: hmac.c プロジェクト: gabrieldelsaint/UIM
err_status_t
hmac_init(hmac_ctx_t *state, const octet_t *key, int key_len) {
  int i;

  /*
   * check key length - note that we don't support keys larger
   * than 20 bytes yet
   */
  if (key_len > 20)              
    return err_status_bad_param;
  
  /*
   * set values of ipad and opad in the context by exoring the key
   * into the appropriate constant values
   */
  for (i=0; i < key_len; i++) {    
    state->ipad[i] = key[i] ^ 0x36;
    state->opad[i] = key[i] ^ 0x5c;
  }  
  /* set the rest of ipad, opad to constant values */
  for (   ; i < 64; i++) {    
    ((octet_t *)state->ipad)[i] = 0x36;
    ((octet_t *)state->opad)[i] = 0x5c;
  }  

  debug_print(mod_hmac, "ipad: %s", octet_string_hex_string(state->ipad, 64));
  
  /* initialize sha1 context */
  sha1_init(&state->ctx);

  /* hash ipad ^ key */
  sha1_update(&state->ctx, (octet_t *)state->ipad, 64);

  return err_status_ok;
}
コード例 #10
0
ファイル: sha1_driver.c プロジェクト: ChristyAJones/libsrtp
err_status_t
sha1_test_case_validate(const hash_test_case_t *test_case) {
  sha1_ctx_t ctx;
  uint32_t hash_value[5];

  if (test_case == NULL)
    return err_status_bad_param;

  if (test_case->hash_len != 20)
    return err_status_bad_param;
  if (test_case->data_len > MAX_HASH_DATA_LEN)
    return err_status_bad_param;

  sha1_init(&ctx);
  sha1_update(&ctx, test_case->data, test_case->data_len);
  sha1_final(&ctx, hash_value);
  if (0 == memcmp(test_case->hash, hash_value, 20)) {
#if VERBOSE
    printf("PASSED: reference value: %s\n", 
	   octet_string_hex_string((const uint8_t *)test_case->hash, 20));
    printf("PASSED: computed value:  %s\n", 
	   octet_string_hex_string((const uint8_t *)hash_value, 20));   
#endif 
    return err_status_ok;
  }

  printf("reference value: %s\n", 
	 octet_string_hex_string((const uint8_t *)test_case->hash, 20));
  printf("computed value:  %s\n", 
	 octet_string_hex_string((const uint8_t *)hash_value, 20));

  return err_status_algo_fail;
  
}
コード例 #11
0
ファイル: x_udx-address.cpp プロジェクト: jurgen-kluft/xp2p
		virtual udx_hash		compute_hash(void* addrin, u32 addrinlen)
		{
			udx_hash hash;
			sha1_init(&m_ctx);
			sha1_update(&m_ctx, (const u8*)addrin, addrinlen);
			sha1_final(&m_ctx, (u8*)hash.m_hash);
		}
コード例 #12
0
ファイル: test-sha1.c プロジェクト: AlickHill/Lantern
static void
test_one(const struct test_vector *vec)
{
    uint8_t md[SHA1_DIGEST_SIZE];
    int i;

    /* All at once. */
    sha1_bytes(vec->data, vec->size, md);
    assert(!memcmp(md, vec->output, SHA1_DIGEST_SIZE));

    /* In two pieces. */
    for (i = 0; i < 20; i++) {
        int n0 = vec->size ? random_range(vec->size) : 0;
        int n1 = vec->size - n0;
        struct sha1_ctx sha1;

        sha1_init(&sha1);
        sha1_update(&sha1, vec->data, n0);
        sha1_update(&sha1, vec->data + n0, n1);
        sha1_final(&sha1, md);
        assert(!memcmp(md, vec->output, SHA1_DIGEST_SIZE));
    }

    putchar('.');
    fflush(stdout);
}
コード例 #13
0
ファイル: sha1.c プロジェクト: NSGod/chmlib
/**
  Self-test the hash
  @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
*/
int sha1_test(void) {
    static const struct {
        char* msg;
        unsigned char hash[20];
    } tests[] = {{"abc",
                  {0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e, 0x25, 0x71, 0x78,
                   0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d}},
                 {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
                  {0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE, 0x4A, 0xA1, 0xF9,
                   0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1}}};

    int i;
    unsigned char tmp[20];
    sha1_state state;

    for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
        sha1_init(&state);
        sha1_process(&state, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
        sha1_done(&state, tmp);
        if (memcpy(tmp, tests[i].hash, 20) != 0) {
            return CRYPT_FAIL_TESTVECTOR;
        }
    }
    return CRYPT_OK;
}
コード例 #14
0
ファイル: log.c プロジェクト: l8huang/ovs
static struct ovsdb_error *
parse_body(struct ovsdb_log *file, off_t offset, unsigned long int length,
           uint8_t sha1[SHA1_DIGEST_SIZE], struct json **jsonp)
{
    struct json_parser *parser;
    struct sha1_ctx ctx;

    sha1_init(&ctx);
    parser = json_parser_create(JSPF_TRAILER);

    while (length > 0) {
        char input[BUFSIZ];
        int chunk;

        chunk = MIN(length, sizeof input);
        if (fread(input, 1, chunk, file->stream) != chunk) {
            json_parser_abort(parser);
            return ovsdb_io_error(ferror(file->stream) ? errno : EOF,
                                  "%s: error reading %lu bytes "
                                  "starting at offset %lld", file->name,
                                  length, (long long int) offset);
        }
        sha1_update(&ctx, input, chunk);
        json_parser_feed(parser, input, chunk);
        length -= chunk;
    }

    sha1_final(&ctx, sha1);
    *jsonp = json_parser_finish(parser);
    return NULL;
}
コード例 #15
0
ファイル: libcrypto.c プロジェクト: codinn/libssh
ssh_mac_ctx ssh_mac_ctx_init(enum ssh_mac_e type){
  ssh_mac_ctx ctx = malloc(sizeof(struct ssh_mac_ctx_struct));
  if (ctx == NULL) {
    return NULL;
  }

  ctx->mac_type=type;
  switch(type){
    case SSH_MAC_SHA1:
      ctx->ctx.sha1_ctx = sha1_init();
      return ctx;
    case SSH_MAC_SHA256:
      ctx->ctx.sha256_ctx = sha256_init();
      return ctx;
    case SSH_MAC_SHA384:
      ctx->ctx.sha384_ctx = sha384_init();
      return ctx;
    case SSH_MAC_SHA512:
      ctx->ctx.sha512_ctx = sha512_init();
      return ctx;
    default:
      SAFE_FREE(ctx);
      return NULL;
  }
}
コード例 #16
0
ファイル: uuid.c プロジェクト: shettyg/ovs
static void
do_init(void)
{
    uint8_t sha1[SHA1_DIGEST_SIZE];
    struct sha1_ctx sha1_ctx;
    uint8_t random_seed[16];
    struct timeval now;

    /* Get seed data. */
    get_entropy_or_die(random_seed, sizeof random_seed);
    xgettimeofday(&now);

    /* Convert seed into key. */
    sha1_init(&sha1_ctx);
    sha1_update(&sha1_ctx, random_seed, sizeof random_seed);
    sha1_update(&sha1_ctx, &now, sizeof now);
    sha1_update_int(&sha1_ctx, getpid());
#ifndef _WIN32
    sha1_update_int(&sha1_ctx, getppid());
    sha1_update_int(&sha1_ctx, getuid());
    sha1_update_int(&sha1_ctx, getgid());
#endif
    sha1_final(&sha1_ctx, sha1);

    /* Generate key. */
    BUILD_ASSERT(sizeof sha1 >= 16);
    aes128_schedule(&key, sha1);

    /* Generate initial counter. */
    get_entropy_or_die(counter, sizeof counter);
}
コード例 #17
0
ファイル: integrity.c プロジェクト: APBaltic/wimlib
static int
calculate_chunk_sha1(struct filedes *in_fd, size_t this_chunk_size,
		     off_t offset, u8 sha1_md[])
{
	u8 buf[BUFFER_SIZE];
	SHA_CTX ctx;
	size_t bytes_remaining;
	size_t bytes_to_read;
	int ret;

	bytes_remaining = this_chunk_size;
	sha1_init(&ctx);
	do {
		bytes_to_read = min(bytes_remaining, sizeof(buf));
		ret = full_pread(in_fd, buf, bytes_to_read, offset);
		if (ret) {
			ERROR_WITH_ERRNO("Read error while calculating "
					 "integrity checksums");
			return ret;
		}
		sha1_update(&ctx, buf, bytes_to_read);
		bytes_remaining -= bytes_to_read;
		offset += bytes_to_read;
	} while (bytes_remaining);
	sha1_final(sha1_md, &ctx);
	return 0;
}
コード例 #18
0
ファイル: bif_crypto.c プロジェクト: MCRedJay/ling
term_t cbif_sha_init0(proc_t *proc, term_t *regs)
{
	struct sha1_ctx *ctx;
	term_t bin = heap_make_bin(&proc->hp, sizeof(*ctx), (uint8_t **)&ctx);
	sha1_init(ctx);
	return bin;
}
コード例 #19
0
ファイル: fork-echoserv.c プロジェクト: Andersbakken/wslay
/*
 * Calculates SHA-1 hash of *src*. The size of *src* is *src_length* bytes.
 * *dst* must be at least SHA1_DIGEST_SIZE.
 */
void sha1(uint8_t *dst, const uint8_t *src, size_t src_length)
{
  struct sha1_ctx ctx;
  sha1_init(&ctx);
  sha1_update(&ctx, src_length, src);
  sha1_digest(&ctx, SHA1_DIGEST_SIZE, dst);
}
コード例 #20
0
ファイル: pam_tfa.c プロジェクト: orgcandman/pam_tfa
//// Google TOTP HMAC SHA1
static void hmac_sha1(const unsigned char *secret, size_t secret_len,
                      const unsigned char *input, size_t input_len,
                      unsigned char *result, size_t result_size)
{
    SHA1_INFO context;
    // The scope on this is required here, because we may assign secret to
    // point to this if secret_len > 64
    unsigned char tmp_internal_hash[64];

    // temp length
    int i;

    if( secret_length > 64 )
    {
        sha1_init(&context);
        sha1_update(&context, secret, secret_len);
        sha1_final(&context, tmp_internal_hash);
        secret_len = SHA1_DIGEST_LENGTH;
    }
    else
    {
        memcpy(tmp_internal_hash, secret, secret_len);
    }

    for(i = 0; i < secret_len; ++i)
    {
        tmp_internal_hash[i] ^= 0x36; 
    }

    memset(tmp_internal_hash + secret_len, 0x36, 64 - secret_len);
}
コード例 #21
0
ファイル: lpc_sdmmc.c プロジェクト: roma-jam/stm32_template
static inline void lpc_sdmmc_verify(CORE* core)
{
    SHA1_CTX sha1;
    uint8_t hash_in[SHA1_BLOCK_SIZE];
    if ((core->sdmmc.state != SDMMC_STATE_VERIFY) && (core->sdmmc.state != SDMMC_STATE_WRITE_VERIFY))
        return;
    sha1_init(&sha1);
    sha1_update(&sha1, io_data(core->sdmmc.io), core->sdmmc.total);
    sha1_final(&sha1, core->sdmmc.state == SDMMC_STATE_VERIFY ? hash_in : core->sdmmc.hash);

    if (core->sdmmc.state == SDMMC_STATE_WRITE_VERIFY)
    {
        core->sdmmc.state = SDMMC_STATE_VERIFY;
        lpc_sdmmc_prepare_descriptors(core);
        if (sdmmcs_read(&core->sdmmc.sdmmcs, core->sdmmc.sector, core->sdmmc.total / core->sdmmc.sdmmcs.sector_size))
            return;
    }
    else
    {
        if (memcmp(core->sdmmc.hash, hash_in, SHA1_BLOCK_SIZE) == 0)
        {
            io_complete(core->sdmmc.process, HAL_IO_CMD(HAL_SDMMC, IPC_WRITE), core->sdmmc.user, core->sdmmc.io);
            core->sdmmc.io = NULL;
            core->sdmmc.process = INVALID_HANDLE;
            core->sdmmc.state = SDMMC_STATE_IDLE;
            return;
        }
    }
    io_complete_ex(core->sdmmc.process, HAL_IO_CMD(HAL_SDMMC, IPC_WRITE), core->sdmmc.user, core->sdmmc.io, get_last_error());
    core->sdmmc.io = NULL;
    core->sdmmc.process = INVALID_HANDLE;
    core->sdmmc.state = SDMMC_STATE_IDLE;
}
コード例 #22
0
ファイル: test_nettle.c プロジェクト: icsecurity/pbgp
void
rsa_sign_msg(struct rsa_private_key *priv,mpz_t s,  uint8_t *msg, const size_t len)
{
      uint8_t digest[SHA1_DIGEST_SIZE];
      struct sha1_ctx sha1ctx;
      int i = 0,nloop = 0;
      

      if(priv == NULL || msg == NULL || len == 0) {
            die("priv == NULL || msg == NULL || len == 0");
      }

      //printf("\nMSG_LEN: %d\n",len);
      bzero(&sha1ctx,sizeof(struct sha1_ctx));
      bzero(&digest[0],SHA1_DIGEST_SIZE);

      mpz_init(s);
      sha1_init(&sha1ctx);
      sha1_update(&sha1ctx,len,msg);
      sha1_digest(&sha1ctx,SHA1_DIGEST_SIZE,digest);
            
      if (!rsa_sha1_sign_digest(priv, digest, s)) {
      //      out("rsa_sha1_sign...try again\n");
            rsa_sign_msg(priv,s,msg,len);
      }
}
コード例 #23
0
ファイル: chord.c プロジェクト: willzxd/Chord
/**
Get the Node ID
*/
long getId(char *str){

	sha1nfo i;

	char ipPort[30], tmp[30];
	if (strcmp(str, "\0") == 0){
		//connect ip and port
		strcpy (ipPort, current->ip);
    	strcat (ipPort,": ");
    	sprintf(tmp, "%d", current->port);
    	strcat (ipPort, tmp);
	} else
		strcpy(ipPort, str);

	sha1_init(&i);
	sha1_write(&i, ipPort, strlen(ipPort));


	uint8_t* hash = sha1_result(&i);
	uint32_t b0 = hash[0] | hash[1] << 8 | hash[2] << 16 | hash[3] << 24;
	uint32_t b1 = hash[4] | hash[5] << 8 | hash[6] << 16 | hash[7] << 24;
	uint32_t b2 = hash[8] | hash[9] << 8 | hash[10] << 16 | hash[11] << 24;
	uint32_t b3 = hash[12] | hash[13] << 8 | hash[14] << 16 | hash[15] << 24;
	uint32_t b4 = hash[16] | hash[17] << 8 | hash[18] << 16 | hash[19] << 24;
	
	long id;
	id = b0 ^ b1 ^ b2 ^ b3 ^ b4;
	return id;
}
コード例 #24
0
ファイル: dbrandom.c プロジェクト: CoffeMug/dropbear
/* return len bytes of pseudo-random data */
void genrandom(unsigned char* buf, unsigned int len) {

	hash_state hs;
	unsigned char hash[SHA1_HASH_SIZE];
	unsigned int copylen;

	if (!donerandinit) {
		dropbear_exit("seedrandom not done");
	}

	while (len > 0) {
		sha1_init(&hs);
		sha1_process(&hs, (void*)hashpool, sizeof(hashpool));
		sha1_process(&hs, (void*)&counter, sizeof(counter));
		sha1_done(&hs, hash);

		counter++;
		if (counter > MAX_COUNTER) {
			seedrandom();
		}

		copylen = MIN(len, SHA1_HASH_SIZE);
		memcpy(buf, hash, copylen);
		len -= copylen;
		buf += copylen;
	}
	m_burn(hash, sizeof(hash));
}
コード例 #25
0
ファイル: sha1.c プロジェクト: vni/programming
int unit_test(void)
{
	struct {
		const char *str;
		char digest[41]; /* printable version of digest */
	} tests[] = {
		{ "", "da39a3ee5e6b4b0d3255bfef95601890afd80709" },
		{ "abc", "a9993e364706816aba3e25717850c26c9cd0d89d" },
		{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq","84983e441c3bd26ebaae4aa1f95129e5e54670f1" },
		{ "aaaaaaaaaa", "3495ff69d34671d1e15b33a63c1379fdedd3a32a" },
	};

	sha1_ctx_t sha1;
	int i, j;
	char dbuf[41], digest[41], buf[64];

	for (i=0; i < (sizeof(tests)/sizeof(tests[0])); i++) {
		total_tests++;
		sha1_init(&sha1);
		sha1_process(&sha1, tests[i].str, strlen(tests[i].str));
		sha1_finish(&sha1);
		for (j = 0; j < 20; j++)
			snprintf(dbuf+j*2, sizeof(dbuf)-j*2, "%02x", sha1.buf[j]);
		if (strncmp(dbuf, tests[i].digest, sizeof(dbuf))) {
			fprintf(stderr, "FAILED: on string \"%s\". Expected digest: \"%s\". Got: \"%s\"\n",
					tests[i].str, tests[i].digest, dbuf);
			failed_tests++;
		}
	}
	printf("Total tests run: %d, passed: %d, failed: %d\n", total_tests, total_tests - failed_tests, failed_tests);

	return failed_tests;
}
コード例 #26
0
ファイル: imapc-mail.c プロジェクト: Distrotech/dovecot
static int imapc_mail_get_hdr_hash(struct index_mail *imail)
{
	struct istream *input;
	const unsigned char *data;
	size_t size;
	uoff_t old_offset;
	struct sha1_ctxt sha1_ctx;
	unsigned char sha1_output[SHA1_RESULTLEN];
	const char *sha1_str;

	sha1_init(&sha1_ctx);
	old_offset = imail->data.stream == NULL ? 0 :
		imail->data.stream->v_offset;
	if (mail_get_hdr_stream(&imail->mail.mail, NULL, &input) < 0)
		return -1;
	while (i_stream_read_data(input, &data, &size, 0) > 0) {
		sha1_loop(&sha1_ctx, data, size);
		i_stream_skip(input, size);
	}
	i_stream_seek(imail->data.stream, old_offset);
	sha1_result(&sha1_ctx, sha1_output);

	sha1_str = binary_to_hex(sha1_output, sizeof(sha1_output));
	imail->data.guid = p_strdup(imail->mail.data_pool, sha1_str);
	return 0;
}
コード例 #27
0
ファイル: hardware.c プロジェクト: gelraen/cros-ec
uint8_t *flash_hash_rw(void)
{
	sha1_init(&ctx);
	sha1_update(&ctx, (void *)CONFIG_FLASH_BASE + CONFIG_FW_RW_OFF,
		    CONFIG_FW_RW_SIZE - 32);
	return sha1_final(&ctx);
}
コード例 #28
0
ファイル: iq.c プロジェクト: shiplu/bitlbee
static xt_status jabber_do_iq_auth( struct im_connection *ic, struct xt_node *node, struct xt_node *orig )
{
	struct jabber_data *jd = ic->proto_data;
	struct xt_node *reply, *query;
	xt_status st;
	char *s;
	
	if( !( query = xt_find_node( node->children, "query" ) ) )
	{
		imcb_log( ic, "Warning: Received incomplete IQ packet while authenticating" );
		imc_logout( ic, FALSE );
		return XT_HANDLED;
	}
	
	/* Time to authenticate ourselves! */
	reply = xt_new_node( "query", NULL, NULL );
	xt_add_attr( reply, "xmlns", XMLNS_AUTH );
	xt_add_child( reply, xt_new_node( "username", jd->username, NULL ) );
	xt_add_child( reply, xt_new_node( "resource", set_getstr( &ic->acc->set, "resource" ), NULL ) );
	
	if( xt_find_node( query->children, "digest" ) && ( s = xt_find_attr( jd->xt->root, "id" ) ) )
	{
		/* We can do digest authentication, it seems, and of
		   course we prefer that. */
		sha1_state_t sha;
		char hash_hex[41];
		unsigned char hash[20];
		int i;
		
		sha1_init( &sha );
		sha1_append( &sha, (unsigned char*) s, strlen( s ) );
		sha1_append( &sha, (unsigned char*) ic->acc->pass, strlen( ic->acc->pass ) );
		sha1_finish( &sha, hash );
		
		for( i = 0; i < 20; i ++ )
			sprintf( hash_hex + i * 2, "%02x", hash[i] );
		
		xt_add_child( reply, xt_new_node( "digest", hash_hex, NULL ) );
	}
	else if( xt_find_node( query->children, "password" ) )
	{
		/* We'll have to stick with plaintext. Let's hope we're using SSL/TLS... */
		xt_add_child( reply, xt_new_node( "password", ic->acc->pass, NULL ) );
	}
	else
	{
		xt_free_node( reply );
		
		imcb_error( ic, "Can't find suitable authentication method" );
		imc_logout( ic, FALSE );
		return XT_ABORT;
	}
	
	reply = jabber_make_packet( "iq", "set", NULL, reply );
	jabber_cache_add( ic, reply, jabber_finish_iq_auth );
	st = jabber_write_packet( ic, reply );
	
	return st ? XT_HANDLED : XT_ABORT;
}
コード例 #29
0
ファイル: libcrypto.c プロジェクト: cedral/libssh
void sha1(unsigned char *digest, int len, unsigned char *hash)
{
    SHACTX c = sha1_init();
    if (c != NULL) {
        sha1_update(c, digest, len);
        sha1_final(hash, c);
    }
}
コード例 #30
0
ファイル: sha1.c プロジェクト: dcbradley/parrot_cvmfs
void sha1_mem(const void *buf, const unsigned buf_size, 
              unsigned char digest[SHA1_DIGEST_LENGTH]) 
{
   sha1_context_t ctx;
   sha1_init(&ctx);
   sha1_update(&ctx, (const unsigned char *)buf, buf_size);
   sha1_final(digest, &ctx);
}