示例#1
0
文件: test-block.c 项目: bk2204/drew
int test_process_testcase(void *data, int type, const char *item,
		struct test_external *tep)
{
	struct testcase *tc = data;

	switch (type) {
		case 'T':
			if (!tc->id)
				tc->id = strdup(item);
			else if (strcmp(tc->id, item))
				return TEST_EXECUTE;
			break;
		case 'a':
			free(tc->algo);
			tc->algo = strdup(item);
			tc->blksize = get_block_size(tep, tc);
			break;
		case 'r':
			if (sscanf(item, "%zu", &tc->nrepeats) != 1)
				return TEST_CORRUPT;
			break;
		case 'K':
			if (sscanf(item, "%zu", &tc->klen) != 1)
				return TEST_CORRUPT | 'K';
			break;
		case 'k':
			if (!tc->klen)
				return TEST_CORRUPT | 3;
			if (process_bytes(tc->klen, &tc->key, item))
				return TEST_CORRUPT | 'k';
			break;
		case 'p':
			if (!tc->blksize)
				tc->blksize = strlen(item) / 2;
			if (process_bytes(tc->blksize, &tc->pt, item))
				return TEST_CORRUPT | 'p';
			break;
		case 'c':
			if (!tc->blksize)
				tc->blksize = strlen(item) / 2;
			if (process_bytes(tc->blksize, &tc->ct, item))
				return TEST_CORRUPT | 'c';
			break;
	}

	return TEST_OK;
}
示例#2
0
文件: a32.c 项目: jjdmol/LOFAR
int parse_stream (FILE *stream, void *resblock)
{
  char buffer[BLOCKSIZE + 72];
  size_t sum;


  /* Iterate over full file contents.  */
  while (1)
    {
      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
         computation function processes the whole buffer so that with the
         next round of the loop another block can be read.  */
      size_t n;
      sum = 0;

      /* Read block.  Take care for partial reads.  */
      while (1)
	{
	  n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);

	  sum += n;

	  if (sum == BLOCKSIZE)
	    break;

	  if (n == 0)
	    {
	      /* Check for the error flag IFF N == 0, so that we don't
	         exit the loop after a partial read due to e.g., EAGAIN
	         or EWOULDBLOCK.  */
	      if (ferror (stream))
		return 1;
	      goto process_partial_block;
	    }

	  /* We've read at least one byte, so ignore errors.  But always
	     check for EOF, since feof may be true even though N > 0.
	     Otherwise, we could end up calling fread after EOF.  */
	  if (feof (stream))
	    goto process_partial_block;
	}

      /* Process buffer with BLOCKSIZE bytes.  Note that
         BLOCKSIZE % 64 == 0
       */
      process_block (buffer, BLOCKSIZE);
    }

process_partial_block:

  /* Process any remaining bytes.  */
  if (sum > 0)
    process_bytes (buffer, sum);

  return 0;
}
/** Main program entry point. This routine contains the overall program flow, including initial
 *  setup of all components and the main program loop.
 */
int main(void)
{
    
    bitSet(DDRC,7); // HACK DEBUG
    
	SetupHardware();

	/* Create a regular character stream for the interface so that it can be used with the stdio.h functions */
	CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream);

	GlobalInterruptEnable();

	for (;;)
	{
		process_bytes();
		
		/*if(do_charge_test){
			charging_test();
		}*/
		
		CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
		USB_USBTask();
	}
}
示例#4
0
/* type is one of the following: 1, 224, 256, 384, 512 (for SHA) or 5 (for MD5) */
static void *
hmac_sha_md5 (const char *buffer, size_t len, char *key, size_t key_len, char *resbuf, int type) {
  /* SHA512_BLOCKSIZE and SHA512_DIGESTSIZE are the biggest, so we can use it with other algorithms */
  char Ki[SHA512_BLOCKSIZE];
  char Ko[SHA512_BLOCKSIZE];
  char tmpbuf[SHA512_DIGESTSIZE];
  int i;
  int blocksize;
  int digestsize;
  void *ctx;
  void (*init_ctx)(void *);
  void (*process_bytes)(void *, size_t, void *);
  void *(*finish_ctx)(void *, void *);
  void *result;

  switch (type) {
          case 5: /* Oh, it's MD5! */
                  ctx = malloc(sizeof(struct md5_ctx));
                  init_ctx      = (void(*)(void *))                 &md5_init_ctx;
                  process_bytes = (void(*)(void *, size_t, void *)) &md5_process_bytes;
                  finish_ctx    = (void* (*)(void *, void *))       &md5_finish_ctx;
                  blocksize = MD5_BLOCKSIZE;
                  digestsize = MD5_DIGESTSIZE;
                  break;
               /*   .--------< SHA variant   
                    |                        
                    v                   */
          SHA_CASE(  1,   1)
          SHA_CASE(224, 256)
          SHA_CASE(256, 256)
          SHA_CASE(384, 512)
          SHA_CASE(512, 512)
          default:
                  return NULL;
  }

  /* if given key is longer that algorithm's block, we must change it to
     hash of the original key (of size of algorithm's digest) */
  if (key_len > blocksize) {
          init_ctx (&ctx);
          process_bytes (key, key_len, &ctx);
          finish_ctx (&ctx, Ki);
          key_len = digestsize;
          memcpy(Ko, Ki, key_len);
  } else {
          memcpy(Ki, key, key_len);
          memcpy(Ko, key, key_len);
  }

  /* prepare input and output key */
  for (i = 0; i < key_len; i++) {
          Ki[i] ^= 0x36;
          Ko[i] ^= 0x5c;
  }
  for (; i < blocksize; i++) {
          Ki[i] = 0x36;
          Ko[i] = 0x5c;
  }

  init_ctx (ctx);
  process_bytes (Ki, blocksize, ctx);
  process_bytes ((void *)buffer, len, ctx);
  finish_ctx (ctx, tmpbuf);

  init_ctx (ctx);
  process_bytes (Ko, blocksize, ctx);
  process_bytes (tmpbuf, digestsize, ctx);

  result = finish_ctx (ctx, resbuf);
  free(ctx);

  return result;
}
示例#5
0
文件: test-mode.c 项目: bk2204/drew
int test_process_testcase(void *data, int type, const char *item,
		struct test_external *tep)
{
	struct testcase *tc = data;

	switch (type) {
		case 'T':
			if (!tc->id)
				tc->id = strdup(item);
			else if (strcmp(tc->id, item))
				return TEST_EXECUTE;
			break;
		case 'a':
			free(tc->algo);
			tc->algo = strdup(item);
			break;
		case 'm':
			free(tc->mode);
			tc->mode = strdup(item);
			break;
		case 'F':
			if (sscanf(item, "%zu", &tc->feedbackBits) != 1)
				return TEST_CORRUPT;
			break;
		case 'K':
			if (sscanf(item, "%zu", &tc->klen) != 1)
				return TEST_CORRUPT;
			break;
		case 'k':
			if (!tc->klen)
				return TEST_CORRUPT;
			if (process_bytes(tc->klen, &tc->key, item))
				return TEST_CORRUPT;
			break;
		case 'N':
			if (sscanf(item, "%zu", &tc->nlen) != 1)
				return TEST_CORRUPT;
			break;
		case 'n':
			if (!tc->nlen)
				return TEST_CORRUPT;
			if (process_bytes(tc->nlen, &tc->nonce, item))
				return TEST_CORRUPT;
			break;
		case 'p':
			tc->len = strlen(item) / 2;
			if (process_bytes(tc->len, &tc->pt, item))
				return TEST_CORRUPT;
			break;
		case 'c':
			tc->ctlen = strlen(item) / 2;
			if (process_bytes(tc->ctlen, &tc->ct, item))
				return TEST_CORRUPT;
			break;
		case 'd':
			tc->aadlen = strlen(item) / 2;
			if (process_bytes(tc->aadlen, &tc->aad, item))
				return TEST_CORRUPT;
			break;
	}

	return TEST_OK;
}