Esempio n. 1
0
//
// Called from AppleKeyPairGenContext
//
void BSafe::BSafeKeyPairGenContext::generate(
		const Context 	&context,
		BinaryKey		&pubBinKey,		// valid on successful return
		BinaryKey		&privBinKey,	// ditto
		uint32			&keySize)		// ditto
{
	/* these casts throw exceptions if the keys are of the 
	 * wrong classes, which is a major bogon, since we created
	 * the keys in the above generate() function */
	BSafeBinaryKey &bsPubBinKey = 
		dynamic_cast<BSafeBinaryKey &>(pubBinKey);
	BSafeBinaryKey &bsPrivBinKey = 
		dynamic_cast<BSafeBinaryKey &>(privBinKey);
	
    if (!initialized) {
		setupAlgorithm(context, keySize);
        check(B_GenerateInit(bsAlgorithm, chooser(), bsSurrender), true);
        initialized = true;
    }

	setRandom();
    check(B_GenerateKeypair(bsAlgorithm, 
		bsPubBinKey.bsKey(), 
		bsPrivBinKey.bsKey(), 
		bsRandom, 
		bsSurrender), true);
}
Esempio n. 2
0
//
// DSA Parameter Generation
//
void BSafe::BSafeKeyPairGenContext::generate(
	const Context &context, 
	uint32 bitSize,
    CssmData &params,
    uint32 &attrCount, 
	Context::Attr * &attrs)
{
	assert(context.algorithm() == CSSM_ALGID_DSA);
	
    B_ALGORITHM_OBJ genAlg = NULL;
    B_ALGORITHM_OBJ result = NULL;

    try {
        check(B_CreateAlgorithmObject(&genAlg));

        B_DSA_PARAM_GEN_PARAMS genParams;
        genParams.primeBits = bitSize;
        check(B_SetAlgorithmInfo(genAlg, AI_DSAParamGen, POINTER(&genParams)));
        setRandom();
        check(B_GenerateInit(genAlg, chooser(), bsSurrender), true);
        check(B_CreateAlgorithmObject(&result));
        check(B_GenerateParameters(genAlg, result, bsRandom, bsSurrender));

        // get parameters out of algorithm object
        A_DSA_PARAMS *kParams = NULL;
        check(B_GetAlgorithmInfo((POINTER *)&kParams, result, AI_DSAKeyGen), true);

        // shred them into context attribute form
        attrs = normAllocator->alloc<Context::Attr>(3);
        attrs[0] = Context::Attr(CSSM_ATTRIBUTE_PRIME,
                   *BSafeItem(kParams->prime).copyp<CssmData>(*normAllocator));
        attrs[1] = Context::Attr(CSSM_ATTRIBUTE_SUBPRIME,
                   *BSafeItem(kParams->subPrime).copyp<CssmData>(*normAllocator));
        attrs[2] = Context::Attr(CSSM_ATTRIBUTE_BASE,
                   *BSafeItem(kParams->base).copyp<CssmData>(*normAllocator));
        attrCount = 3;

        // clean up
        B_DestroyAlgorithmObject(&result);
        B_DestroyAlgorithmObject(&genAlg);
    } catch (...) {
        // clean up
        B_DestroyAlgorithmObject(&result);
        B_DestroyAlgorithmObject(&genAlg);
        throw;
    }
}
Esempio n. 3
0
int
generate_key (const char *header)
{
  PUBLIC_KEY pubkey;
  PRIVATE_KEY privkey;
  unsigned char line[1024];
  int i, err, len;
  unsigned char iv[8];
  byte digest[16], ID[16], tmpbyte;
  byte des3key[24];
  BUFFER *b1, *encrypted_key;
  FILE *privring, *privlock;
#ifdef USE_RSAREF
  R_DIGEST_CTX digest_context;
  DES3_CBC_CTX context;
  R_RSA_PROTO_KEY protokey;
#else
  B_ALGORITHM_OBJ digest_obj;
  B_ALGORITHM_OBJ des_obj;
  B_ALGORITHM_OBJ gen_obj;
  B_KEY_OBJ key_obj;
  A_RSA_KEY_GEN_PARAMS keypar;
  A_PKCS_RSA_PRIVATE_KEY *keyinfo;
  unsigned char pubexpt[] =
  {1, 0, 1};
#endif

#ifdef DEBUG
  printf ("Generating key:\n%s", header);
#endif
  /* Generate a 1024 bit key with pub exponent = 65537 */
#ifdef USE_RSAREF
  protokey.bits = MIX_RSA_MOD;
  protokey.useFermat4 = 1;
  err = R_GeneratePEMKeys (&pubkey, &privkey, &protokey, &random_obj);
#else
  B_CreateAlgorithmObject (&gen_obj);
  /* Generate a 1024 bit key with pub exponent = 65537 */
  keypar.modulusBits = 1024;
  keypar.publicExponent.data = pubexpt;
  keypar.publicExponent.len = sizeof (pubexpt);
  B_SetAlgorithmInfo (gen_obj, AI_RSAKeyGen, (POINTER) & keypar);
  B_GenerateInit (gen_obj, CHOOSER, NULL);
  B_CreateKeyObject (&pubkey);
  B_CreateKeyObject (&privkey);
  err = (B_GenerateKeypair (gen_obj, pubkey, privkey, random_obj, NULL));
#endif
  if (err != 0)
    {
      fprintf (errlog, "Key generation error.\n");
      return (-1);
    }

#ifdef DEBUG
  printf ("Done.\n");
#endif
  /* put private key in a buffer */
  b1 = new_buffer ();
#ifdef USE_RSAREF
  /* Convert privkey.bits to two bytes */
  i = privkey.bits;
#else
  B_GetKeyInfo ((POINTER *) & keyinfo, privkey, KI_PKCS_RSAPrivate);
  i = keyinfo->modulus.len * 8;
#endif
  tmpbyte = i & 0xFF;
  add_to_buffer (b1, &tmpbyte, 1);	/* low byte of bits */
  tmpbyte = (i / 256) & 0xFF;
  add_to_buffer (b1, &tmpbyte, 1);	/* high byte of bits */

#ifdef USE_RSAREF
  add_to_buffer (b1, privkey.modulus, MAX_RSA_MODULUS_LEN);
  add_to_buffer (b1, privkey.publicExponent, MAX_RSA_MODULUS_LEN);

  /* Make Key ID */
  R_DigestInit (&digest_context, DA_MD5);
  R_DigestUpdate (&digest_context, pubkey.modulus, MAX_RSA_MODULUS_LEN);
  R_DigestUpdate (&digest_context, pubkey.exponent, MAX_RSA_MODULUS_LEN);
  R_DigestFinal (&digest_context, ID, &i);

  add_to_buffer (b1, privkey.exponent, MAX_RSA_MODULUS_LEN);
  add_to_buffer (b1, privkey.prime[0], MAX_RSA_PRIME_LEN);
  add_to_buffer (b1, privkey.prime[1], MAX_RSA_PRIME_LEN);
  add_to_buffer (b1, privkey.primeExponent[0], MAX_RSA_PRIME_LEN);
  add_to_buffer (b1, privkey.primeExponent[1], MAX_RSA_PRIME_LEN);
  add_to_buffer (b1, privkey.coefficient, MAX_RSA_PRIME_LEN);
#else
  i = (i + 7) / 8;

  /* Make Key ID */
  B_CreateAlgorithmObject (&digest_obj);
  B_SetAlgorithmInfo (digest_obj, AI_MD5, NULL);
  B_DigestInit (digest_obj, NULL, CHOOSER, NULL);

  add_to_buffer (b1, keyinfo->modulus.data, i);

  if (keyinfo->publicExponent.len < i)
    add_to_buffer (b1, NULL,
		   i - keyinfo->publicExponent.len);
  add_to_buffer (b1, keyinfo->publicExponent.data,
		 keyinfo->publicExponent.len);

  B_DigestUpdate (digest_obj, b1->message + 2,
		  2 * i,
		  NULL);
  B_DigestFinal (digest_obj, ID, &i, 16, NULL);
  B_DestroyAlgorithmObject (&digest_obj);

  if (keyinfo->privateExponent.len < i)
    add_to_buffer (b1, NULL,
		   i - keyinfo->privateExponent.len);
  add_to_buffer (b1, keyinfo->privateExponent.data,
		 keyinfo->privateExponent.len);

  i = (i + 1) / 2;

  if (keyinfo->prime[0].len < i)
    add_to_buffer (b1, NULL,
		   i - keyinfo->prime[0].len);
  add_to_buffer (b1, keyinfo->prime[0].data, keyinfo->prime[0].len);

  if (keyinfo->prime[1].len < i)
    add_to_buffer (b1, NULL,
		   i - keyinfo->prime[1].len);
  add_to_buffer (b1, keyinfo->prime[1].data, keyinfo->prime[1].len);

  if (keyinfo->primeExponent[0].len < i)
    add_to_buffer (b1, NULL,
		   i - keyinfo->primeExponent[0].len);
  add_to_buffer (b1, keyinfo->primeExponent[0].data,
		 keyinfo->primeExponent[0].len);

  if (keyinfo->primeExponent[1].len < i)
    add_to_buffer (b1, NULL,
		   i - keyinfo->primeExponent[1].len);
  add_to_buffer (b1, keyinfo->primeExponent[1].data,
		 keyinfo->primeExponent[1].len);

  if (keyinfo->coefficient.len < i)
    add_to_buffer (b1, NULL,
		   i - keyinfo->coefficient.len);
  add_to_buffer (b1, keyinfo->coefficient.data, keyinfo->coefficient.len);
#endif

  /* Encrypt the secret key */
  encrypted_key = new_buffer ();
  len = b1->length;
  if (len % 8 != 0)		/* ensure length is mult of 8 */
    len += 8 - len % 8;
  add_to_buffer (encrypted_key, malloc (len), len);
  our_randombytes (iv, 8);
#ifdef USE_RSAREF
  R_DigestInit (&digest_context, DA_MD5);
  R_DigestUpdate (&digest_context, PASSPHRASE, strlen (PASSPHRASE));
  R_DigestFinal (&digest_context, digest, &i);
  memcpy (des3key, digest, 16);	/* set first 2 keys */
  memcpy (des3key + 16, digest, 8);	/* third key = first key */
  DES3_CBCInit (&context, des3key, iv, 1);
  if (DES3_CBCUpdate (&context, encrypted_key->message, b1->message,
		      encrypted_key->length))
    {
      printf ("Error: Problem encrypting key\n");
      return (-1);
    }
#else
  B_CreateAlgorithmObject (&digest_obj);
  B_SetAlgorithmInfo (digest_obj, AI_MD5, NULL);

  B_DigestInit (digest_obj, NULL, CHOOSER, NULL);
  B_DigestUpdate (digest_obj, PASSPHRASE, strlen (PASSPHRASE), NULL);
  B_DigestFinal (digest_obj, digest, &i, 16, NULL);
  B_DestroyAlgorithmObject (&digest_obj);

  memcpy (des3key, digest, 16);	/* set first 2 keys */
  memcpy (des3key + 16, digest, 8);	/* third key = first key */
  B_CreateAlgorithmObject (&des_obj);
  B_SetAlgorithmInfo (des_obj, AI_DES_EDE3_CBC_IV8, iv);
  B_CreateKeyObject (&key_obj);
  B_SetKeyInfo (key_obj, KI_DES24Strong, des3key);
  B_EncryptInit (des_obj, key_obj, CHOOSER, NULL);
  B_EncryptUpdate (des_obj, encrypted_key->message, &len,
		   encrypted_key->length,
		   b1->message, b1->length, random_obj, NULL);
  B_EncryptFinal (des_obj, encrypted_key->message + len,
		  &len, encrypted_key->length - len, random_obj, NULL);
  /* err? XXX */
#endif
  memset ((void *) digest, 0, 16);	/* zero password */

  mix_lock ("secring", &privlock);
  if ((privring = open_mix_file (SECRING, "a+")) == NULL)
    {
      mix_unlock ("secring", privlock);
      return (-1);
    }
  fprintf (privring, "%s\n", begin_key);
  if (strlen (header) > 0)
    {
      fprintf (privring, KEY_VERSION "%s\n", VERSION);
      fprintf (privring, "%s", header);
    }
  print_ID (privring, ID);
  fprintf (privring, "%d\n", len);
  encode_block (line, &i, iv, 8);
  fwrite (line, 1, i, privring);
  fprintf (privring, "\n");

  /* Armor privkey */
  armor (encrypted_key);
  write_buffer (encrypted_key, privring);
  free_buffer (encrypted_key);
  fprintf (privring, "%s\n", end_key);
  fclose (privring);
  mix_unlock ("secring", privlock);
  return 0;
}
Esempio n. 4
0
int main(int argc, char *argv[])
{
    int status;

    int keySize = argv[1] ? atoi(argv[1]) : 512;
    printf("Key size = %d bits\n", keySize);

    B_ALGORITHM_OBJ pGen = NULL;
    check(B_CreateAlgorithmObject(&pGen));
    B_DSA_PARAM_GEN_PARAMS gParams;
    gParams.primeBits = keySize;
    check(B_SetAlgorithmInfo(pGen, AI_DSAParamGen, POINTER(&gParams)));

    B_ALGORITHM_OBJ random = NULL;
    check(B_CreateAlgorithmObject(&random));
    check(B_SetAlgorithmInfo(random, AI_X962Random_V0, NULL));
    check(B_RandomInit(random, chooser, NULL));
    check(B_RandomUpdate(random, seed, sizeof(seed), NULL));

    check(B_GenerateInit(pGen, chooser, NULL));
    B_ALGORITHM_OBJ result = NULL;
    check(B_CreateAlgorithmObject(&result));
    printf("Generating DSA parameters\n");
    check(B_GenerateParameters(pGen, result, random, NULL));
    printf("DSA generate complete, writing...\n");

    A_DSA_PARAMS *dParams;
    memset(&dParams, 0, sizeof(dParams));
    check(B_GetAlgorithmInfo((POINTER *)&dParams, result, AI_DSAKeyGen));
    dumpItem(dParams->prime, "prime");
    dumpItem(dParams->subPrime, "subprime");
    dumpItem(dParams->base, "base");

#if 0
    B_KEY_OBJ pubKey = NULL;
    check(B_CreateKeyObject(&pubKey));
    B_KEY_OBJ privKey = NULL;
    check(B_CreateKeyObject(&privKey));

    B_ALGORITHM_OBJ gen = NULL;
    check(B_CreateAlgorithmObject(&gen));
    A_RSA_KEY_GEN_PARAMS args;
    args.modulusBits = keySize;
    args.publicExponent.data = exponent;
    args.publicExponent.len = sizeof(exponent);
    check(B_SetAlgorithmInfo(gen, AI_RSAStrongKeyGen, POINTER(&args)));
    check(B_GenerateInit(gen, chooser, NULL));
    check(B_GenerateKeypair(gen, pubKey, privKey, random, NULL));

    B_ALGORITHM_OBJ enc = NULL;
    check(B_CreateAlgorithmObject(&enc));
    check(B_SetAlgorithmInfo(enc, AI_PKCS_RSAPublic, NULL));
    check(B_EncryptInit(enc, pubKey, chooser, NULL));
    unsigned int inLen;
    check(B_EncryptUpdate(enc, crypt, &inLen, sizeof(crypt),
                          POINTER(in), sizeof(in), random, NULL));
    printf("EncryptUpdate output = %u\n", inLen);
    check(B_EncryptFinal(enc, crypt, &inLen, sizeof(crypt), random, NULL));
    printf("EncryptFinal output=%u\n", inLen);

    B_ALGORITHM_OBJ dec = NULL;
    check(B_CreateAlgorithmObject(&dec));
    check(B_SetAlgorithmInfo(dec, AI_PKCS_RSAPrivate, NULL));
    check(B_DecryptInit(dec, privKey, chooser, NULL));
    unsigned int outLen, outLen2;
    check(B_DecryptUpdate(dec, out, &outLen, sizeof(out),
                          crypt, inLen, random, NULL));
    printf("DecryptUpdate output = %u\n", outLen);
    check(B_DecryptFinal(dec, out2, &outLen2, sizeof(out2), random, NULL));
    printf("DecryptFinal output=%u %s\n", outLen2, (char*)out2);
    B_DestroyKeyObject(&pubKey);
    B_DestroyKeyObject(&privKey);
#endif

    exit(0);
}