Example #1
0
int main(void) {
  uint8_t buf[82];
  size_t i;

  CRYPTO_library_init();
  ERR_load_crypto_strings();

  for (i = 0; i < sizeof(kTests) / sizeof(kTests[0]); i++) {
    const hkdf_test_vector_t *test = &kTests[i];
    if (!HKDF(buf, test->out_len, test->md_func(), test->ikm, test->ikm_len,
              test->salt, test->salt_len, test->info, test->info_len)) {
      fprintf(stderr, "Call to HKDF failed\n");
      ERR_print_errors_fp(stderr);
      return 1;
    }
    if (memcmp(buf, test->out, test->out_len) != 0) {
      fprintf(stderr, "%u: Resulting key material does not match test vector\n",
              (unsigned)i);
      return 1;
    }
  }

  printf("PASS\n");
  ERR_free_strings();
  return 0;
}
Example #2
0
static int pkey_hkdf_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
                            size_t *keylen)
{
    HKDF_PKEY_CTX *kctx = ctx->data;

    if (kctx->md == NULL || kctx->key == NULL)
        return 0;

    switch (kctx->mode) {
    case EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND:
        return HKDF(kctx->md, kctx->salt, kctx->salt_len, kctx->key,
                    kctx->key_len, kctx->info, kctx->info_len, key,
                    *keylen) != NULL;

    case EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY:
        if (key == NULL) {
            *keylen = EVP_MD_size(kctx->md);
            return 1;
        }
        return HKDF_Extract(kctx->md, kctx->salt, kctx->salt_len, kctx->key,
                            kctx->key_len, key, keylen) != NULL;

    case EVP_PKEY_HKDEF_MODE_EXPAND_ONLY:
        return HKDF_Expand(kctx->md, kctx->key, kctx->key_len, kctx->info,
                           kctx->info_len, key, *keylen) != NULL;

    default:
        return 0;
    }
}
Example #3
0
int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
                    const byte* salt, word32 saltSz,
                    const byte* info, word32 infoSz,
                    byte* out, word32 outSz)
{
    return HKDF(type, inKey, inKeySz, salt, saltSz, info, infoSz, out, outSz);
}
Example #4
0
int main(int argc, char *argv[]){
	int k_iter;				//Key iteration
	int hkdf_iter;		//Derivation Key iteration
	int borne_start;	//create rand iteration between borne_start and borne_stop
	int borne_stop;
	char *alea;				//Alea from PRBG
	char *key_init;		//First key from PRBG
	char *key;					//Final key from Deruvation Function
	char *Ns;					//Serial number, the MAC adress here
	char *code;				//Final returned code
	int space = 6;		//Space between two '-'
	
	/**** If you want to enter the iteration by hand **
	if(argc < 3){printf("./%s <key iteration> <hkdf iteration>",argv[0]);return 1;}
	k_iter = argv[1];
	hkdf_iter = argv[2];
	/******** Otherwise it's a random ************************/
	srand(time(NULL));
	borne_start = 2560; //just to have 3 char with a least 1 alpha
	borne_stop = 4098;
	k_iter = rand()%(borne_stop - borne_start) + borne_start;
	hkdf_iter = rand()%(borne_stop - borne_start) + borne_start;
	/**************************************************/
	
	alea = (char *)malloc(sizeof(char)*ALEA_SIZE);
	key_init = (char *)malloc(sizeof(char)*HASH_SIZE);
	key = (char *)malloc(sizeof(char)*HASH_SIZE);
	Ns = (char *)malloc(sizeof(char)*MAX);
	code = (char *)malloc(sizeof(char)*MAX);
	/*** Gen Alea **/
	PRBG_RSA(&alea, k_iter);
	
	/*** Hash alea **/
	hash_function(alea,&key_init);

	/*** Get MAC adress for Ns ***/
	get_mac_adress(&Ns);
	
	/*** Key derivation **/
	HKDF(key_init, Ns, hkdf_iter, &key);

	/*** Traductor ***/
	traductor(&key, k_iter, hkdf_iter, &code, space);
	
	printf("The serial number is your MAC address...\nYour Serial Number is : %s\nYour Code is : %s\n",Ns,code);
	free(alea);
	free(key_init);
	free(key);
	free(Ns);
	free(code);
	return 0;
}
Example #5
0
int main(void) {
  uint8_t buf[82], prk[EVP_MAX_MD_SIZE];
  size_t i, prk_len;

  CRYPTO_library_init();

  for (i = 0; i < OPENSSL_ARRAY_SIZE(kTests); i++) {
    const hkdf_test_vector_t *test = &kTests[i];
    if (!HKDF_extract(prk, &prk_len, test->md_func(), test->ikm, test->ikm_len,
                      test->salt, test->salt_len)) {
      fprintf(stderr, "Call to HKDF_extract failed\n");
      ERR_print_errors_fp(stderr);
      return 1;
    }
    if (prk_len != test->prk_len ||
        memcmp(prk, test->prk, test->prk_len) != 0) {
      fprintf(stderr, "%zu: Resulting PRK does not match test vector\n", i);
      return 1;
    }
    if (!HKDF_expand(buf, test->out_len, test->md_func(), prk, prk_len,
                     test->info, test->info_len)) {
      fprintf(stderr, "Call to HKDF_expand failed\n");
      ERR_print_errors_fp(stderr);
      return 1;
    }
    if (memcmp(buf, test->out, test->out_len) != 0) {
      fprintf(stderr,
              "%zu: Resulting key material does not match test vector\n", i);
      return 1;
    }

    if (!HKDF(buf, test->out_len, test->md_func(), test->ikm, test->ikm_len,
              test->salt, test->salt_len, test->info, test->info_len)) {
      fprintf(stderr, "Call to HKDF failed\n");
      ERR_print_errors_fp(stderr);
      return 1;
    }
    if (memcmp(buf, test->out, test->out_len) != 0) {
      fprintf(stderr,
              "%zu: Resulting key material does not match test vector\n", i);
      return 1;
    }
  }

  printf("PASS\n");
  ERR_free_strings();
  return 0;
}
Example #6
0
File: hkdf.c Project: 1234-/openssl
static int pkey_hkdf_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
                            size_t *keylen)
{
    HKDF_PKEY_CTX *kctx = ctx->data;

    if (kctx->md == NULL || kctx->key == NULL)
        return 0;

    if (HKDF(kctx->md, kctx->salt, kctx->salt_len, kctx->key, kctx->key_len,
             kctx->info, kctx->info_len, key, *keylen) == NULL)
    {
        return 0;
    }

    return 1;
}