END_TEST





// test the full range in the tcost values.
START_TEST(test_PHS_tcost_values)
{


  int returnval;
  uint8 output[DIGEST_LENGTH];
  uint8 salt[MAX_SALT_LENGTH];
  uint8 password[MAX_PASSWORD_LENGTH];
  int i;


  // initialize the values
  get_random_bytes(MAX_SALT_LENGTH, salt);
  get_random_bytes(MAX_PASSWORD_LENGTH, password);


  // test all possible values for tcost.
  for(i=1;i<MAX_NUMBER_OF_SHARES; i++) {
      returnval = PHS(output, DIGEST_LENGTH, password, MAX_PASSWORD_LENGTH, salt,
      MAX_SALT_LENGTH, i, 0);
  
      ck_assert(returnval == 0);
  }


}END_TEST
Exemple #2
0
void Run(void *out, size_t outlen, size_t inlen, size_t saltlen, uint32_t t_cost, uint32_t m_cost) {
#ifdef _MEASURE
    uint64_t start_cycles, stop_cycles, delta;
    uint32_t ui1, ui2;

    clock_t start_time = clock();
    start_cycles = __rdtscp(&ui1);
#endif

    unsigned char zero_array[256];
    unsigned char one_array[256];

    memset(zero_array, 0, 256);
    memset(one_array, 1, 256);

    PHS(out, outlen, zero_array, inlen, one_array, saltlen, t_cost, m_cost);

#ifdef _MEASURE
    stop_cycles = __rdtscp(&ui2);
    clock_t finish_time = clock();

    delta = (stop_cycles - start_cycles) / (m_cost);
    float mcycles = (float) (stop_cycles - start_cycles) / (1 << 20);
    printf("Argon:  %2.2f cpb %2.2f Mcycles ", (float) delta / 1024, mcycles);

    float run_time = ((float) finish_time - start_time) / (CLOCKS_PER_SEC);
    printf("%2.4f seconds\n", run_time);
#endif

}
Exemple #3
0
int main(int argc, char *argv[])
{
	int i=0, j=0, k=0, l=0;

	if(argc != 5)
	{
		 ShowUsage();
	}

	char* pass = argv[1];
	char* salt =  argv[2];
	
	int m_cost = atoi(argv[3]);
	int t_cost = atoi(argv[4]);
	
	double MS_EL = 0;
	
	unsigned char Hash[64];

	printf("\n Password : %s", pass );
	printf("\n salt : %s", salt );
	printf("\n m_cost : %d", m_cost );
	printf("\n t_cost : %d", t_cost );

	PHS(Hash, 64, (unsigned char *)pass, strlen(pass), (unsigned char*)salt, strlen(salt), t_cost, m_cost);

	PrintHash("\n\n Hash : ", Hash);
	 		
	getchar();

	return 0;
}
Exemple #4
0
int main(){

	size_t saltlen = 16;
	size_t outlen  = 32;
	unsigned int t_cost = (unsigned int)pow(2,13);
	unsigned int m_cost = (unsigned int)pow(2,15);
	int i;

	char *passwd="password";
	uint8_t res[outlen] 
	__attribute__((__aligned__(__alignof__(uint32_t))));
	
	srand(time(NULL));
	rand();
	uint32_t salt[saltlen >> 2];
	for (i=0;i<(saltlen >> 2);i++)
		salt[i] = rand();

	clock_t start = -clock();

	PHS((void *)res,outlen,(void *)passwd,strlen(passwd),(void *)salt,saltlen,t_cost,m_cost);

	start += clock();
	
	float sec = (float)start/CLOCKS_PER_SEC;
	printf("%.3f secs,%.3f passwords\n",sec,(float)(1/sec));
	
	return 0;
}
Exemple #5
0
static int run_phs(uint8_t *out, size_t outlen, 
                   const uint8_t *in, size_t inlen,
                   const uint8_t *salt, size_t saltlen,
                   unsigned int t_cost, unsigned int m_cost) {

  struct timeval start, end;
  int in_printable = 1;
  int salt_printable = 1;
  size_t i;

  for(i=0; i<inlen; i++)
    if(in[i] < ' ' || in[i] > '~' || in[i] == '"')
      in_printable = 0;

  for(i=0; i<saltlen; i++)
    if(salt[i] < ' ' || salt[i] > '~' || salt[i] == '"')
      salt_printable = 0;

  printf("PHS(");
  if(in_printable)
    printf("\"%s\", ", in);
  else { 
    for(i=0; i<inlen && i<16; i++)
      printf("%.2hhx", in[i]);
    if(inlen > 16)
      printf("...");
    printf(", ");
  }

  if(salt_printable)
    printf("\"%s\", ", salt);
  else { 
    for(i=0; i<saltlen && i<16; i++)
      printf("%.2hhx", salt[i]);
    if(saltlen > 16)
      printf("...");
    printf(", ");
  }

  printf("%d, %d) = ", t_cost, m_cost);
  fflush(stdout);

  gettimeofday(&start, NULL);

  if(PHS(out, outlen, in, inlen, salt, saltlen, t_cost, m_cost) != 0)
    return -1;

  gettimeofday(&end, NULL);
  
  for(i=0; i<outlen; i++)
    printf("%.2hhx", out[i]);
  
  printf(" (%ldus)\n",
         1000000*(end.tv_sec - start.tv_sec) + 
         end.tv_usec - start.tv_usec);
  return 0;
}
Exemple #6
0
static void *runPHSThread(void *contextPtr) {
    struct ContextStruct *c = (struct ContextStruct *)contextPtr;
    for(uint32_t i = 0; i < c->repeat; i++) {
        int r = PHS(c->out, c->outlen, c->in, c->inlen, c->salt, c->saltlen, c->t_cost, c->m_cost);
        if (r) {
            printf("Error while hashing: %u\n", r);
            exit(1);
        }
    }
    pthread_exit(NULL);
}
Exemple #7
0
static void* phs_thread(void *thread_no) {
  uint8_t out[16];
  
  struct timeval start, end;
  
  gettimeofday(&start, NULL);
  PHS(out, sizeof out, (uint8_t*)"secret", 6, thread_no, 4, 10000, 16);
  gettimeofday(&end, NULL);
  printf("Thread %d: %ldus\n", *((uint32_t*)thread_no),
         1000000*(end.tv_sec - start.tv_sec) + 
         end.tv_usec - start.tv_usec);

  return NULL;
};
Exemple #8
0
int
_ecore_thread_win32_join(win32_thread *x,
                         void        **res)
{
   if (!PHE(x, PHS()))
     {
        WaitForSingleObject(x->thread, INFINITE);
        CloseHandle(x->thread);
     }
   if (res) *res = x->val;
   free(x);

   return 0;
}
Exemple #9
0
static void print_PHS(const void *in, size_t inlen,
    const void *salt, size_t saltlen, unsigned int t_cost, unsigned int m_cost)
{
	uint8_t dk[32];

	printf("PHS(");
	print_hex(in, inlen, ", ");
	print_hex(salt, saltlen, ", ");
	printf("%u, %u) = ", t_cost, m_cost);

	if (PHS(dk, sizeof(dk), in, inlen, salt, saltlen, t_cost, m_cost)) {
		puts("FAILED");
		return;
	}

	print_hex(dk, sizeof(dk), "\n");
}
Exemple #10
0
/* main function for demonstration purpose */
int main(int argc, char *argv[]) {
    uint8_t salt[PHS_SALT_SIZE];
    uint8_t pw[2];
    uint32_t ret;
    uint8_t out[64];

    /* initialize the password and salt memory */
    memset(salt, 0x00, PHS_SALT_SIZE);
    memset(pw, 0x00, 2);

    /* generate test vectors */
    for (uint8_t i = 0; i < 256; i++) {                            /* "pw" */
        printf("----- Password = %02X -----\n", i);
        pw[0] = i;

        for (uint8_t j = 0; j < 256; j++) {                        /* "salt" */
            salt[0] = j;

            for (uint8_t mcost = 0; mcost < 14; mcost++) {         /* m_cost */
                for (uint8_t tcost = 1; tcost < 20-mcost; tcost++) {     /* t_cost */

                    /* call PHS */
                    ret = PHS(out, 64, pw, 1, salt, PHS_SALT_SIZE, tcost, mcost);

                    if (ret != 0) {
                        printf("Error at run: pass = %d, seed = %d, m = %d, t = %d!\n", i, j, mcost, tcost);
                    }
                    else {
                        /* $t_cost$m_cost$salt$hash */
                        printf("Parameters: t_cost = %02" PRIu8 ", m_cost = %02" PRIu8 ", pw = %02X, output = $", tcost, mcost, i);
                        print_hex(salt, PHS_SALT_SIZE);
                        printf("$%02d:%02d$", tcost, mcost);
                        print_hex(out, 64);
                        printf("\n");
                    }
                }
            }
        }
    }

    /* done */
    return 0;
}
Exemple #11
0
void benchmarkNoMem(uint32_t times)
{
	uint64_t out[4];
	TIMER_TYPE s, e;

	TIMER_FUNC(s);
	for (uint32_t i = 0; i < times; i++)
	{
		PHS(out, sizeof(out), "password", 8, "salt", 4, 100000, 0);
	}
	TIMER_FUNC(e);
	printf("Normal method took average time of %0.9f seconds:\n", TIMER_DIFF(s, e) / times);
	printf("%016llx %016llx %016llx %016llx\n\n", SWAP_ENDIAN_64(out[0]), SWAP_ENDIAN_64(out[1]), SWAP_ENDIAN_64(out[2]), SWAP_ENDIAN_64(out[3]));

	TIMER_FUNC(s);
	for (uint32_t i = 0; i < times; i++)
	{
		PHS_Fast(out, sizeof(out), "password", 8, "salt", 4, 100000, 0);
	}
	TIMER_FUNC(e);
	printf("Fast method took average time of %0.9f seconds:\n", TIMER_DIFF(s, e) / times);
	printf("%016llx %016llx %016llx %016llx\n",  SWAP_ENDIAN_64(out[0]),  SWAP_ENDIAN_64(out[1]),  SWAP_ENDIAN_64(out[2]),  SWAP_ENDIAN_64(out[3]));
	printf("%016llx %016llx %016llx %016llx\n",     ~SWAP_ENDIAN_64(out[0]), ~SWAP_ENDIAN_64(out[1]), ~SWAP_ENDIAN_64(out[2]), ~SWAP_ENDIAN_64(out[3]));
}
END_TEST





// We will now test the generated input with non-ascii ranges.
START_TEST(test_PHS_input_ranges)
{
  
  
  int returnval;
  uint8 output[DIGEST_LENGTH];
  uint8 salt[MAX_SALT_LENGTH];
  uint8 precomputed_hash[DIGEST_LENGTH];
  uint8 password[MAX_PASSWORD_LENGTH];
  uint8 salted_password[MAX_SALT_LENGTH + MAX_PASSWORD_LENGTH];
  unsigned int i,j;
  unsigned int incidences, max_incidences = 6; 

  
  // generate a full range password (from 0 to 255)
  for(i=0;i<MAX_PASSWORD_LENGTH;i++) {
    password[i] = i%255;
  }

  // precompute the hash to produce.
  memcpy(salted_password, salt, MAX_SALT_LENGTH);
  memcpy(salted_password + MAX_SALT_LENGTH, password, MAX_PASSWORD_LENGTH);
  _calculate_digest(precomputed_hash, salted_password, 
      MAX_SALT_LENGTH + MAX_PASSWORD_LENGTH);

  // test with full range password
  incidences = 0;
  returnval = PHS(output, DIGEST_LENGTH, password, MAX_PASSWORD_LENGTH, salt,
      MAX_SALT_LENGTH, 2, 0);
  ck_assert(returnval == 0);

  // we will check how obscure the hash is, we want the resulting octets to
  // not resemble the original hash. 
  for(i=0;i<DIGEST_LENGTH; i++) {
    if(output[i] == precomputed_hash[i]) {
      incidences ++;
    } 
  }
  ck_assert(incidences < max_incidences);


  // test with a random password
  get_random_bytes(MAX_PASSWORD_LENGTH, password);

  // precompute the hash to produce.
  memcpy(salted_password, salt, MAX_SALT_LENGTH);
  memcpy(salted_password + MAX_SALT_LENGTH, password, MAX_PASSWORD_LENGTH);
  _calculate_digest(precomputed_hash, salted_password, 
      MAX_SALT_LENGTH + MAX_PASSWORD_LENGTH);

  returnval = PHS(output, DIGEST_LENGTH, password, MAX_PASSWORD_LENGTH, salt,
      MAX_SALT_LENGTH, 2, 0);

  // check results
  incidences = 0;
  ck_assert(returnval == 0);
  for(i=0;i<DIGEST_LENGTH; i++) {
    if(output[i] == precomputed_hash[i]) {
      incidences++;
    }
    
  }
  ck_assert(incidences < max_incidences);
 

  // add a full_range salt.
  for( i = 0; i < MAX_SALT_LENGTH; i++) {
    salt[i] = 255-i;
  }
  
  // precompute the hash to produce.
  memcpy(salted_password, salt, MAX_SALT_LENGTH);
  memcpy(salted_password + MAX_SALT_LENGTH, password, MAX_PASSWORD_LENGTH);
  _calculate_digest(precomputed_hash, salted_password, 
      MAX_SALT_LENGTH + MAX_PASSWORD_LENGTH);


  returnval = PHS(output, DIGEST_LENGTH, password, MAX_PASSWORD_LENGTH, salt,
      MAX_SALT_LENGTH, 2, 0);
  
  // check results
  incidences = 0;
  ck_assert(returnval == 0);
  for(i=0;i<DIGEST_LENGTH; i++) {
    if(output[i] == precomputed_hash[i]) {
      incidences++;
    }
    
  }
  ck_assert(incidences < max_incidences);
 
  
  // add random salt
  get_random_bytes(MAX_SALT_LENGTH, salt);

  // precompute the hash to produce.
  memcpy(salted_password, salt, MAX_SALT_LENGTH);
  memcpy(salted_password + MAX_SALT_LENGTH, password, MAX_PASSWORD_LENGTH);
  _calculate_digest(precomputed_hash, salted_password, 
      MAX_SALT_LENGTH + MAX_PASSWORD_LENGTH);

  returnval = PHS(output, DIGEST_LENGTH, password, MAX_PASSWORD_LENGTH, salt,
     MAX_SALT_LENGTH, 2, 0);

  // check results
  incidences = 0;
  ck_assert(returnval == 0);
  for(i=0;i<DIGEST_LENGTH; i++) {
    if(output[i] == precomputed_hash[i]) {
      incidences++;
    }
    
  }
  ck_assert(incidences < max_incidences);

  
  // testing for different tcost values.
  for(i = 1; i < MAX_NUMBER_OF_SHARES; i++) {
    // add random salt
    get_random_bytes(MAX_SALT_LENGTH, salt);

    // precompute the hash to produce.
    memcpy(salted_password, salt, MAX_SALT_LENGTH);
    memcpy(salted_password + MAX_SALT_LENGTH, password, MAX_PASSWORD_LENGTH);
    _calculate_digest(precomputed_hash, salted_password, 
      MAX_SALT_LENGTH + MAX_PASSWORD_LENGTH);

    returnval = PHS(output, DIGEST_LENGTH, password, MAX_PASSWORD_LENGTH, salt,
     MAX_SALT_LENGTH, i, 0);

    // check results
    incidences = 0;
    ck_assert(returnval == 0);
    for(j=0;j<DIGEST_LENGTH; j++) {
      if(output[j] == precomputed_hash[j]) {
        incidences++;
      }  
    }
    ck_assert(incidences < max_incidences);
  }
}
Exemple #13
0
int main(int argc, char *argv[]) {
    //=================== Basic variables, with default values =======================//
    unsigned int kLen = 64;
    unsigned int t_cost = 0;
    unsigned int m_cost = 0;


    char *pwd = "Lyra2 PHS";
    unsigned int pwdLen = 9;
    char *salt = "saltsaltsaltsalt";
    unsigned int saltLen = 16;
    //==========================================================================/

    switch (argc) {
        case 2:
            if (strcmp(argv[1], "--help") == 0) {
                printf("Usage: \n");
                printf("       %s pwd salt kLen tCost nRows \n\n", argv[0]);
                printf("Inputs:\n");
                printf(" - pwd: the password\n");
                printf(" - salt: the salt\n");
                printf(" - kLen: output size\n");
                printf(" - tCost: the time cost parameter\n");
                printf(" - nRows: the number of rows parameter\n");
                printf("\n");
                printf("Or:\n");
                printf("       %s tCost nRows --testVectors     (to generate test vectors and test Lyra2 operation)\n\n", argv[0]);
                return 0;
            } else {
                printf("Invalid options.\nFor more information, try \"%s --help\".\n", argv[0]);
                return 0;
            }
            break;

        case 6:
            pwd = argv[1];
            pwdLen = strlen(pwd);
            salt = argv[2];
            saltLen = strlen(salt);
            kLen = atol(argv[3]);
            t_cost = atol(argv[4]);
            m_cost = atol(argv[5]);
            break;
        case 4:
            if (strcmp(argv[3], "--testVectors") == 0) {
                t_cost = atoi(argv[1]);
                m_cost = atoi(argv[2]);
                testVectors(t_cost, m_cost);
                return 0;
            } else {
                printf("Invalid options.\nFor more information, try \"%s --help\".\n", argv[0]);
                return 0;
            }
            break;

        default:
            printf("Invalid options.\nTry \"%s --help\".\n", argv[0]);
            return 0;
    }

    if (m_cost < 3) {
        printf("nRows must be >= 3\n");
        return 1;
    }

    if ((m_cost / 2) % nPARALLEL != 0) {
        printf("(nRows / 2) mod p must be = 0\n");
        return 1;
    }
    
    unsigned char *K = malloc(kLen);
   
    printf("Inputs: \n");
    printf("\tPassword: %s\n", pwd);
    printf("\tPassword Length: %u\n", pwdLen);
    printf("\tSalt: %s\n", salt);
    printf("\tSalt Length: %u\n", saltLen);
    printf("\tOutput Length: %u\n", kLen);
    printf("------------------------------------------------------------------------------------------------------------------------------------------\n");

    printf("Parameters: \n");
    printf("\tT: %u\n", t_cost);
    printf("\tR: %u\n", m_cost);
    printf("\tC: %u\n", N_COLS);
    printf("\tParallelism: %u\n", nPARALLEL);
    
    char *spongeName ="";
    if (SPONGE==0){
        spongeName = "Blake2";
    }
    else if (SPONGE==1){
        spongeName = "BlaMka";
    }
    else{
        spongeName = "half-round BlaMka";
    }
    
    printf("\tSponge: %s\n", spongeName);
    printf("\tSponge Blocks (bitrate): %u = %u bits\n", BLOCK_LEN_INT64, BLOCK_LEN_INT64*64);
    
    size_t sizeMemMatrix = (size_t) ((size_t)m_cost * (size_t)ROW_LEN_BYTES);

    if(sizeMemMatrix > (1610612736)){
    printf("\tMemory: %ld bytes (IMPORTANT: This implementation is known to have "
            "issues for such a large memory usage)\n", sizeMemMatrix);
    }else{
        printf("\tMemory: %ld bytes\n", sizeMemMatrix);
    }
        

    printf("------------------------------------------------------------------------------------------------------------------------------------------\n");
    
#if (BENCH == 1)
    struct timeval start;
    struct timeval end;
    gettimeofday(&start, NULL);
#endif
    int result;
    
    result = PHS(K, kLen, pwd, pwdLen, salt, saltLen, t_cost, m_cost);
    
#if (BENCH == 1)
    gettimeofday(&end, NULL);
    unsigned long elapsed = (end.tv_sec-start.tv_sec)*1000000 + end.tv_usec-start.tv_usec;
    printf("Execution Time: %lu us\n", elapsed);
    printf("------------------------------------------------------------------------------------------------------------------------------------------\n");
#endif

    switch (result) {
        case 0:
            printf("Output: \n");

            printf("\n\tK: ");
            int i;
            for (i = 0; i < kLen; i++) {
                printf("%x|", K[i]);
            }
            break;
        case -1:
            printf("Error: unable to allocate memory (nRows too large?)\n");
            break;
        default:
            printf("Unexpected error\n");
            break;
    }

    printf("\n");
    printf("------------------------------------------------------------------------------------------------------------------------------------------\n");
    free(K);

    return 0;
}
Exemple #14
0
/**
 * Generates the test vectors for Lyra2.
 *
 * @param t Parameter to determine the processing time (T)
 * @param r  Memory cost parameter (defines the number of rows of the memory matrix, R)
 */
int testVectors(unsigned int  t, unsigned int  r) {
    //=================== Basic variables, with default values =======================//
    int kLen = 64;
    unsigned char *pwd;
    int pwdLen = 11;
    unsigned char *salt;
    int saltLen = 16;
	
	srand(time(NULL));
	int i;
    int countSample;
    int indexSalt = 0;
    //==========================================================================/

    unsigned char *K = malloc(kLen);

    /* Generating vectors with the input size varying from 0 to 128 bytes,
     * and values varying from 0 to 127. The salt size is fixed in 16 bytes, 
	 * and its value varies from 0 to 256.
	 */
    for (countSample = 0; countSample <= 128; countSample++) {
        pwdLen = countSample;
        int count;
        pwd = malloc(sizeof (pwd) * pwdLen);
        for (count = 0; count < pwdLen; count++) {
            pwd[count] = count;
        }

        salt = malloc(sizeof (salt) * saltLen);
        for (count = 0; count < saltLen; count++) {
            salt[count] = saltLen * indexSalt + count;
        }
        indexSalt++;
        if (indexSalt == saltLen)
            indexSalt = 0;

        PHS(K, kLen, pwd, pwdLen, salt, saltLen, t, r);

        printf("\ninlen: %d\n", pwdLen);
        printf("t_cost: %d\n", t);
        printf("m_cost: %d\n", r);
        printf("outlen: %d\n", kLen);

        printf("In: ");
        for (i = 0; i < pwdLen; i++) {
            printf("%02x ", pwd[i]);
        }
        printf("\n");

        printf("Salt: ");
        for (i = 0; i < saltLen; i++) {
            printf("%02x ", salt[i]);
        }
        printf("\n");

        printf("Out: ");
        for (i = 0; i < kLen; i++) {
            printf("%02x ", K[i]);
        }
        printf("\n");
    }
    
	/* Generating vectors with the input size varying from 0 to 128 bytes,
     * and values varying from 128 to 255. The salt size is fixed in 16 bytes, 
	 * and its value varies from 0 to 256.
	 */
    for (countSample = 128; countSample < 256; countSample++) {
        pwdLen = countSample - 127;
        int count;
        pwd = malloc(sizeof (pwd) * pwdLen);
        for (count = 0; count < pwdLen; count++) {
            pwd[count] = count + 128;
        }

        salt = malloc(sizeof (salt) * saltLen);
        for (count = 0; count < saltLen; count++) {
            salt[count] = saltLen * indexSalt + count;
        }
        indexSalt++;
        if (indexSalt == saltLen)
            indexSalt = 0;

        PHS(K, kLen, pwd, pwdLen, salt, saltLen, t, r);

        printf("\ninlen: %d\n", pwdLen);
        printf("t_cost: %d\n", t);
        printf("m_cost: %d\n", r);
        printf("outlen: %d\n", kLen);

        printf("In: ");
        for (i = 0; i < pwdLen; i++) {
            printf("%02x ", pwd[i]);
        }
        printf("\n");

        printf("Salt: ");
        for (i = 0; i < saltLen; i++) {
            printf("%02x ", salt[i]);
        }
        printf("\n");

        printf("Out: ");
        for (i = 0; i < kLen; i++) {
            printf("%02x ", K[i]);
        }
        printf("\n");
    }
    return 0;
}
Exemple #15
0
int main(int argc, char *argv[]) {
    //=================== Basic variables, with default values =======================//
    int kLen = 64;
    int t = 0;
    int r = 0;
    char *pwd = "Lyra sponge";
    int pwdLen = 11;
    char *salt = "saltsaltsaltsalt";
    int saltLen = 16;
    //==========================================================================/
    
    switch (argc) {
        case 2:
            if (strcmp(argv[1], "--help") == 0) {
                printf("Usage: \n");
                printf("       Lyra2 pwd salt kLen tCost nRows \n\n");
                printf("Inputs:\n");
                printf(" - pwd: the password\n");
                printf(" - salt: the salt\n");
                printf(" - kLen: output size\n");
                printf(" - tCost: the time cost parameter\n");
                printf(" - nRows: the number of rows parameter\n");
                printf("\n");
                printf("Or:\n");
                printf("       Lyra2 tCost nRows --testVectors (to generate test vectors and test Lyra2 operation)\n\n");
                return 0;
            } else {
                printf("Invalid options.\nFor more information, try \"Lyra2 --help\".\n");
                return 0;
            }
            break;
        case 6:
            pwd = argv[1];
            pwdLen = strlen(pwd);
            salt = argv[2];
            saltLen = strlen(salt);
            kLen = atoi(argv[3]);
            t = atoi(argv[4]);
            r = atoi(argv[5]);
            break;
        case 4:
            if (strcmp(argv[3], "--testVectors") == 0) {
                t = atoi(argv[1]);
                r = atoi(argv[2]);
                testVectors(t, r);
                return 0;
            } else {
                printf("Invalid options.\nFor more information, try \"Lyra2 --help\".\n");
                return 0;
            }
            break;
        default:
            printf("Invalid options.\nTry \"Lyra2 --help\" for help.\n");
            return 0;
    }
    
    unsigned char *K = malloc(kLen);

    printf("Inputs: \n");
    printf("\tPassword: %s\n", pwd);
    printf("\tPassword Size: %d\n", pwdLen);
    printf("\tSalt: %s\n", salt);
    printf("\tOutput Size: %d\n", kLen);
    printf("------------------------------------------------------------------------------------------------------------------------------------------\n");

    printf("Parameters: \n");
    printf("\tT: %d\n", t);
    printf("\tR: %d\n", r);
    printf("\tC: %d\n", N_COLS);
    printf("\tMemory: %ld bits\n", ((long) (N_COLS * r * BLOCK_LEN_BYTES)));
    printf("------------------------------------------------------------------------------------------------------------------------------------------\n");

    PHS(K, kLen, pwd, pwdLen, salt, saltLen, t, r);

    printf("Output: \n");

    printf("\n\tK: ");
    int i;
    for (i = 0; i < kLen; i++) {
        printf("%x|", K[i]);
    }
    printf("\n");
    printf("------------------------------------------------------------------------------------------------------------------------------------------\n");
    return 0;
}
Exemple #16
0
int main(int argc, char **argv) {
    exeName = argv[0];

    uint8_t out[32];
    char *password = (char *)"password";
    uint32_t passwordlen = 8;
    uint8_t *salt = (uint8_t *)"saltsaltsaltsalt";
    uint32_t numCalls = 1;
    uint32_t outlen = 32;
    uint32_t saltlen = 16;
    uint32_t repeat = 1;
    uint32_t parallelism = 1;
    bool outputDieharderText = false;
    bool outputDieharderBinary = false;
    bool outputTime = false;
    int r;

    char c;
    while((c = getopt(argc, argv, "d:DGp:P:T:")) != -1) {
        switch (c) {
        case 'd':
            outputDieharderText = true;
            numCalls = readUint32(c, optarg);
            break;
        case 'D':
            outputDieharderBinary = true;
            break;
        case 'G':
            printTestVectors(100.0);
            return 0;
        case 'p':
            password = optarg;
            passwordlen = strlen(password);
            break;
        case 'T':
            outputTime = true;
            repeat = readUint32(c, optarg);
            break;
        case 'P':
            parallelism = readUint32(c, optarg);
            break;
        default:
            usage("Invalid argument");
        }
    }

    if(optind + 2 != argc) {
        usage("Invalid number of arguments");
    }
    uint32_t t_cost = readUint32('0', argv[optind]);
    uint32_t m_cost = readUint32('\0', argv[optind+1]);
        
    if(outputDieharderText) {
        printf("type: d\n"
            "count: %u\n"
            "numbit: 32\n", numCalls*8);
    }

    if(outputDieharderText || outputDieharderBinary) {
        uint32_t i;
        for(i = 0; (i < numCalls) || outputDieharderBinary; i++) {
            uint8_t pwdbuf[passwordlen + 4];
            memcpy(pwdbuf, password, passwordlen);
            encodeLittleEndian(pwdbuf + passwordlen, &i, 4);
            if((r = PHS(out, outlen, pwdbuf, passwordlen + 4, salt, saltlen, t_cost, m_cost))) {
                printf("Password hashing for %s failed with code %d!\n", argv[0], r);
                return 1;
            }
            if(outputDieharderText) {
                uint32_t j;
                for(j = 0; j < 8; j++) {
                    uint32_t v;
                    decodeLittleEndian(&v, out + j*4, 4);
                    printf("%u\n", v);
                }
            } else if(outputDieharderBinary) {
                fwrite(out, outlen, 1, stdout);
            } else {
                printHex("", out, outlen);
            }
        }
    } else if (outputTime) {
        double ms;
        if((r = time_PHS(repeat, parallelism, out, outlen, (uint8_t *)password,
                passwordlen, salt, saltlen, t_cost, m_cost, &ms))) {
            printf("Password hashing for %s failed with code %d!\n", argv[0], r);
            return 1;
        }
        printf("Runtime: %2.0f ms\n", ms);
        printHex("", out, outlen);
    } else {
        if((r = PHS(out, outlen, (uint8_t *)password, passwordlen, salt, saltlen, t_cost, m_cost))) {
            printf("Password hashing for %s failed with code %d!\n", argv[0], r);
            return 1;
        }
        printHex("", out, outlen);
    }

    return 0;
}
Exemple #17
0
/**
 * Generates the test vectors for Lyra2.
 *
 * @param t     Parameter to determine the processing time (T)
 * @param r     Memory cost parameter (defines the number of rows of the memory matrix, R)
 */
int testVectors(unsigned int t, unsigned int m_cost) {
    //=================== Basic variables, with default values =======================//
    int kLen = 64;
    unsigned char *pwd;
    int pwdLen = 11;
    unsigned char *salt;
    int saltLen = 16;

    srand(time(NULL));

    int i;
    int countSample;
    int indexSalt = 0;
    //==========================================================================/

    unsigned char *K = malloc(kLen);

    /* Generating vectors with the input size varying from 0 to 128 bytes,
     * and values varying from 0 to 127. The salt size is fixed in 16 bytes, 
     * and its value varies from 0 to 256.
     */
    for (countSample = 0; countSample <= 128; countSample++) {
        pwdLen = countSample;
        int count;
        pwd = malloc(sizeof (pwd) * pwdLen);
        for (count = 0; count < pwdLen; count++) {
                pwd[count] = count;
        }


        salt = malloc(sizeof (salt) * saltLen);
        for (count = 0; count < saltLen; count++) {
                salt[count] = saltLen * indexSalt + count;
        }
        indexSalt++;
        if (indexSalt == saltLen)
                indexSalt = 0;


        PHS(K, kLen, pwd, pwdLen, salt, saltLen, t, m_cost);

        printf("\ninlen: %d\n", pwdLen);
        printf("outlen: %d\n", kLen);
        printf("t_costs: %d\n", t);
        printf("m_costs: \tR: %d \tC: %d\n", m_cost, N_COLS);
        printf("parallelism: %u\n", nPARALLEL);
    
        char *spongeName ="";
        if (SPONGE==0){
            spongeName = "Blake2";
        }
        else if (SPONGE==1){
            spongeName = "BlaMka";
        }
        else{
            spongeName = "half-round BlaMka";
        }

        printf("sponge: %s\n", spongeName);
        printf("sponge blocks (bitrate): %u = %u bits\n", BLOCK_LEN_INT64, BLOCK_LEN_INT64*64);


        printf("In: ");
        for (i = 0; i < pwdLen; i++) {
                printf("%02x ", pwd[i]);
        }


        printf("\n");

        printf("Salt: ");
        for (i = 0; i < saltLen; i++) {
                printf("%02x ", salt[i]);
        }
        printf("\n");


        printf("Out: ");
        for (i = 0; i < kLen; i++) {
                printf("%02x ", K[i]);
        }
        printf("\n");
    }

    /* Generating vectors with the input size varying from 0 to 128 bytes,
     * and values varying from 128 to 255. The salt size is fixed in 16 bytes, 
     * and its value varies from 0 to 256.
     */
    for (countSample = 128; countSample <= 256; countSample++) {
	pwdLen = countSample - 127;
	int count;
	pwd = malloc(sizeof (pwd) * pwdLen);
	for (count = 0; count < pwdLen; count++) {
	    pwd[count] = count + 128;
	}

	salt = malloc(sizeof (salt) * saltLen);
	for (count = 0; count < saltLen; count++) {
	    salt[count] = saltLen * indexSalt + count;
	}
	indexSalt++;
	if (indexSalt == saltLen)
	    indexSalt = 0;

	PHS(K, kLen, pwd, pwdLen, salt, saltLen, t, m_cost);

	printf("\ninlen: %d\n", pwdLen);
        printf("outlen: %d\n", kLen);
        printf("t_costs: %d\n", t);
        printf("m_costs: \tR: %d \tC: %d\n", m_cost, N_COLS);
        printf("parallelism: %u\n", nPARALLEL);
    
        char *spongeName ="";
        if (SPONGE==0){
            spongeName = "Blake2";
        }
        else if (SPONGE==1){
            spongeName = "BlaMka";
        }
        else{
            spongeName = "half-round BlaMka";
        }

        printf("sponge: %s\n", spongeName);
        printf("sponge blocks (bitrate): %u = %u bits\n", BLOCK_LEN_INT64, BLOCK_LEN_INT64*64);

	printf("In: ");
	for (i = 0; i < pwdLen; i++) {
	    printf("%02x ", pwd[i]);
	}
	printf("\n");

	printf("Salt: ");
	for (i = 0; i < saltLen; i++) {
	    printf("%02x ", salt[i]);
	}
	printf("\n");

	printf("Out: ");
	for (i = 0; i < kLen; i++) {
	    printf("%02x ", K[i]);
	}
	printf("\n");
    }
    return 0;
}