/* 
 * Given private-capable privKey, initialize pubKey to be its corresponding 
 * public key.
 */
feeReturn feePubKeyInitPubKeyFromPriv(feePubKey privKey,
	feePubKey pubKey)
{
	pubKeyInst *privInst = (pubKeyInst *)privKey;
	pubKeyInst *pubInst  = (pubKeyInst *)pubKey;

	if((privInst == NULL) || (pubInst == NULL)) {
		return FR_BadPubKey;
	}
	if(privInst->privGiant == NULL) {
		return FR_IncompatibleKey;
	}
	pubInst->cp = curveParamsCopy(privInst->cp);
	if(pubInst == NULL) {
		return FR_Memory;
	}
	pubInst->plus   = new_public_with_key(privInst->plus,  pubInst->cp);
	if(pubInst->plus == NULL) {
		return FR_Memory;
	} 
	if(pubInst->cp->x1Minus != NULL) {
		pubInst->minus  = new_public_with_key(privInst->minus, pubInst->cp);
		if(pubInst->minus == NULL) {
			return FR_Memory;
		} 
	}
	return FR_Success;
}
Ejemplo n.º 2
0
/*
 * Alloc and init a feeFEEDExp object associated with specified feePubKey.
 */
feeFEEDExp feeFEEDExpNewWithPubKey(
	feePubKey pubKey,
	feeRandFcn randFcn,		// optional 
	void *randRef)
{
	feedInst 		*finst = (feedInst *) fmalloc(sizeof(feedInst));
	giant 			privGiant;

	finst->cp = curveParamsCopy(feePubKeyCurveParams(pubKey));
	finst->plus = new_public_with_key(feePubKeyPlusCurve(pubKey),
		finst->cp);
	finst->minus = new_public_with_key(feePubKeyMinusCurve(pubKey),
		finst->cp);

	/*
	 * These might yield NULL data; we can only encrypt in that case.
	 */
	privGiant = feePubKeyPrivData(pubKey);
	if(privGiant) {
		finst->gPriv = newGiant(finst->cp->maxDigits);
		gtog(privGiant, finst->gPriv);
	}
	else {
		finst->gPriv = NULL;
	}

	/*
	 * Conservative, rounding down, on plaintext blocks since we don't
	 * want to split bytes.
	 */
	if(finst->cp->primeType == FPT_General) {
	    unsigned blen = bitlen(finst->cp->basePrime);

	    finst->plainBlockSize = blen / 8;
	    if((blen % 8) == 0) {
	    	/*
		 * round down some more...
		 */
		finst->plainBlockSize--;
	    }
	}
	else {
	    finst->plainBlockSize = finst->cp->q / 8;
	    if(((finst->cp->q & 0x7) == 0) && (finst->cp->k > 0)) {
		/*
		 * Special case, with q mod 8 == 0. Here we have to trim back
		 * the plainBlockSize by one byte.
		 */
		finst->plainBlockSize--;
	    }
	}

	/*
	 * One block of ciphertext - two giants (with implied sign) and a
	 * parity byte
	 */
	finst->cipherBlockSize = (2 * finst->cp->minBytes) + 1;

	finst->xp = newGiant(finst->cp->maxDigits);
	finst->xc = newGiant(finst->cp->maxDigits);
	finst->xq = newGiant(finst->cp->maxDigits);
	finst->xm = newGiant(finst->cp->maxDigits);
	finst->xaux = newGiant(finst->cp->maxDigits);
	finst->rand = NULL;
	finst->randData = NULL;
	finst->randFcn = randFcn;
	finst->randRef = randRef;
	return finst;
}