Blame nss/lib/pk11wrap/pk11pqg.c

Packit 40b132
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit 40b132
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit 40b132
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit 40b132
/* Thse functions are stub functions which will get replaced with calls through
Packit 40b132
 * PKCS #11.
Packit 40b132
 */
Packit 40b132
Packit 40b132
#include "pk11func.h"
Packit 40b132
#include "secmod.h"
Packit 40b132
#include "secmodi.h"
Packit 40b132
#include "secmodti.h"
Packit 40b132
#include "pkcs11t.h"
Packit 40b132
#include "pk11pqg.h"
Packit 40b132
#include "secerr.h"
Packit 40b132
Packit 40b132
Packit 40b132
/* Generate PQGParams and PQGVerify structs.
Packit 40b132
 * Length of P specified by L.
Packit 40b132
 *   if L is greater than 1024 then the resulting verify parameters will be
Packit 40b132
 *   DSA2.
Packit 40b132
 * Length of Q specified by N. If zero, The PKCS #11 module will
Packit 40b132
 *   pick an appropriately sized Q for P. If N is specified and L = 1024, then
Packit 40b132
 *   the resulting verify parameters will be DSA2, Otherwise DSA1 parameters 
Packit 40b132
 *   will be returned.
Packit 40b132
 * Length of SEED in bytes specified in seedBytes.
Packit 40b132
 *
Packit 40b132
 * The underlying PKCS #11 module will check the values for L, N, 
Packit 40b132
 * and seedBytes. The rules for softoken are:
Packit 40b132
 * 
Packit 40b132
 * If L <= 1024, then L must be between 512 and 1024 in increments of 64 bits.
Packit 40b132
 * If L <= 1024, then N must be 0 or 160.
Packit 40b132
 * If L >= 1024, then L and N must match the following table:
Packit 40b132
 *   L=1024   N=0 or 160
Packit 40b132
 *   L=2048   N=0 or 224
Packit 40b132
 *   L=2048   N=256
Packit 40b132
 *   L=3072   N=0 or 256
Packit 40b132
 * if L <= 1024
Packit 40b132
 *   seedBbytes must be in the range [20..256].
Packit 40b132
 * if L >= 1024
Packit 40b132
 *   seedBbytes must be in the range [20..L/16].
Packit 40b132
 */
Packit 40b132
extern SECStatus
Packit 40b132
PK11_PQG_ParamGenV2(unsigned int L, unsigned int N,
Packit 40b132
	 unsigned int seedBytes, PQGParams **pParams, PQGVerify **pVfy)
Packit 40b132
{
Packit 40b132
    PK11SlotInfo *slot = NULL;
Packit 40b132
    CK_ATTRIBUTE genTemplate[5];
Packit 40b132
    CK_ATTRIBUTE *attrs = genTemplate;
Packit 40b132
    int count = sizeof(genTemplate)/sizeof(genTemplate[0]);
Packit 40b132
    CK_MECHANISM mechanism;
Packit 40b132
    CK_OBJECT_HANDLE objectID = CK_INVALID_HANDLE;
Packit 40b132
    CK_RV crv;
Packit 40b132
    CK_ATTRIBUTE pTemplate[] = {
Packit 40b132
	{ CKA_PRIME, NULL, 0 },
Packit 40b132
	{ CKA_SUBPRIME, NULL, 0 },
Packit 40b132
	{ CKA_BASE, NULL, 0 },
Packit 40b132
    };
Packit 40b132
    CK_ATTRIBUTE vTemplate[] = {
Packit 40b132
	{ CKA_NETSCAPE_PQG_COUNTER, NULL, 0 },
Packit 40b132
	{ CKA_NETSCAPE_PQG_SEED, NULL, 0 },
Packit 40b132
	{ CKA_NETSCAPE_PQG_H, NULL, 0 },
Packit 40b132
    };
Packit 40b132
    CK_ULONG primeBits = L;
Packit 40b132
    CK_ULONG subPrimeBits = N;
Packit 40b132
    int pTemplateCount = sizeof(pTemplate)/sizeof(pTemplate[0]);
Packit 40b132
    int vTemplateCount = sizeof(vTemplate)/sizeof(vTemplate[0]);
Packit 40b132
    PLArenaPool *parena = NULL;
Packit 40b132
    PLArenaPool *varena = NULL;
Packit 40b132
    PQGParams *params = NULL;
Packit 40b132
    PQGVerify *verify = NULL;
Packit 40b132
    CK_ULONG seedBits = seedBytes*8;
Packit 40b132
Packit 40b132
    *pParams = NULL;
Packit 40b132
    *pVfy =  NULL;
Packit 40b132
Packit 40b132
    if (primeBits == (CK_ULONG)-1) {
Packit 40b132
	PORT_SetError(SEC_ERROR_INVALID_ARGS);
Packit 40b132
	goto loser;
Packit 40b132
    }
Packit 40b132
    PK11_SETATTRS(attrs, CKA_PRIME_BITS,&primeBits,sizeof(primeBits)); attrs++;
Packit 40b132
    if (subPrimeBits != 0) {
Packit 40b132
    	PK11_SETATTRS(attrs, CKA_SUB_PRIME_BITS, 
Packit 40b132
				&subPrimeBits, sizeof(subPrimeBits)); attrs++;
Packit 40b132
    }
Packit 40b132
    if (seedBits != 0) {
Packit 40b132
    	PK11_SETATTRS(attrs, CKA_NETSCAPE_PQG_SEED_BITS, 
Packit 40b132
					&seedBits, sizeof(seedBits)); attrs++;
Packit 40b132
    }
Packit 40b132
    count = attrs - genTemplate;
Packit 40b132
    PR_ASSERT(count <= sizeof(genTemplate)/sizeof(CK_ATTRIBUTE));
Packit 40b132
Packit 40b132
    slot = PK11_GetInternalSlot();
Packit 40b132
    if (slot == NULL) {
Packit 40b132
	/* set error */
Packit 40b132
	PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);/* shouldn't happen */
Packit 40b132
	goto loser;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /* make sure the internal slot can handle DSA2 type parameters. */
Packit 40b132
    if (primeBits > 1024) {
Packit 40b132
	CK_MECHANISM_INFO mechanism_info;
Packit 40b132
Packit 40b132
	if (!slot->isThreadSafe) PK11_EnterSlotMonitor(slot);
Packit 40b132
	crv = PK11_GETTAB(slot)->C_GetMechanismInfo(slot->slotID,
Packit 40b132
			CKM_DSA_PARAMETER_GEN, &mechanism_info);
Packit 40b132
	if (!slot->isThreadSafe) PK11_ExitSlotMonitor(slot);
Packit 40b132
	/* a bug in the old softoken left CKM_DSA_PARAMETER_GEN off of the
Packit 40b132
	 * mechanism List. If we get a failure asking for this value, we know
Packit 40b132
	 * it can't handle DSA2 */
Packit 40b132
	if ((crv != CKR_OK) || (mechanism_info.ulMaxKeySize < primeBits)) {
Packit 40b132
	    PK11_FreeSlot(slot);
Packit 40b132
	    slot = PK11_GetBestSlotWithAttributes(CKM_DSA_PARAMETER_GEN, 0,
Packit 40b132
						primeBits, NULL);
Packit 40b132
	    if (slot == NULL) {
Packit 40b132
		PORT_SetError(SEC_ERROR_NO_TOKEN); /* can happen */
Packit 40b132
		goto loser;
Packit 40b132
	    }
Packit 40b132
	    /* ditch seedBits in this case, they are NSS specific and at
Packit 40b132
	     * this point we have a token that claims to handle DSA2 */
Packit 40b132
	    if (seedBits) {
Packit 40b132
		attrs--;
Packit 40b132
	    }
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /* Initialize the Key Gen Mechanism */
Packit 40b132
    mechanism.mechanism = CKM_DSA_PARAMETER_GEN;
Packit 40b132
    mechanism.pParameter = NULL;
Packit 40b132
    mechanism.ulParameterLen = 0;
Packit 40b132
Packit 40b132
    PK11_EnterSlotMonitor(slot);
Packit 40b132
    crv = PK11_GETTAB(slot)->C_GenerateKey(slot->session,
Packit 40b132
			 &mechanism, genTemplate, count, &objectID);
Packit 40b132
    PK11_ExitSlotMonitor(slot);
Packit 40b132
Packit 40b132
    if (crv != CKR_OK) {
Packit 40b132
	PORT_SetError( PK11_MapError(crv) );
Packit 40b132
	goto loser;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    parena = PORT_NewArena(60);
Packit 40b132
    if (!parena) {
Packit 40b132
	goto loser;
Packit 40b132
    }        
Packit 40b132
Packit 40b132
    crv = PK11_GetAttributes(parena, slot, objectID, pTemplate, pTemplateCount);
Packit 40b132
    if (crv != CKR_OK) {
Packit 40b132
	PORT_SetError( PK11_MapError(crv) );
Packit 40b132
	goto loser;
Packit 40b132
    }
Packit 40b132
Packit 40b132
Packit 40b132
    params = (PQGParams *)PORT_ArenaAlloc(parena,sizeof(PQGParams));
Packit 40b132
    if (params == NULL) {
Packit 40b132
	goto loser;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /* fill in Params */
Packit 40b132
    params->arena = parena;
Packit 40b132
    params->prime.type = siUnsignedInteger;
Packit 40b132
    params->prime.data = pTemplate[0].pValue;
Packit 40b132
    params->prime.len = pTemplate[0].ulValueLen;
Packit 40b132
    params->subPrime.type = siUnsignedInteger;
Packit 40b132
    params->subPrime.data = pTemplate[1].pValue;
Packit 40b132
    params->subPrime.len = pTemplate[1].ulValueLen;
Packit 40b132
    params->base.type = siUnsignedInteger;
Packit 40b132
    params->base.data = pTemplate[2].pValue;
Packit 40b132
    params->base.len = pTemplate[2].ulValueLen;
Packit 40b132
Packit 40b132
Packit 40b132
    varena = PORT_NewArena(60);
Packit 40b132
    if (!varena) {
Packit 40b132
	goto loser;
Packit 40b132
    }        
Packit 40b132
Packit 40b132
    crv = PK11_GetAttributes(varena, slot, objectID, vTemplate, vTemplateCount);
Packit 40b132
    if (crv != CKR_OK) {
Packit 40b132
	PORT_SetError( PK11_MapError(crv) );
Packit 40b132
	goto loser;
Packit 40b132
    }
Packit 40b132
Packit 40b132
Packit 40b132
    verify = (PQGVerify *)PORT_ArenaAlloc(varena,sizeof(PQGVerify));
Packit 40b132
    if (verify == NULL) {
Packit 40b132
	goto loser;
Packit 40b132
    }
Packit 40b132
    /* fill in Params */
Packit 40b132
    verify->arena = varena;
Packit 40b132
    verify->counter = (unsigned int)(*(CK_ULONG*)vTemplate[0].pValue);
Packit 40b132
    verify->seed.type = siUnsignedInteger;
Packit 40b132
    verify->seed.data = vTemplate[1].pValue;
Packit 40b132
    verify->seed.len = vTemplate[1].ulValueLen;
Packit 40b132
    verify->h.type = siUnsignedInteger;
Packit 40b132
    verify->h.data = vTemplate[2].pValue;
Packit 40b132
    verify->h.len = vTemplate[2].ulValueLen;
Packit 40b132
Packit 40b132
    PK11_DestroyObject(slot,objectID);
Packit 40b132
    PK11_FreeSlot(slot);
Packit 40b132
Packit 40b132
    *pParams = params;
Packit 40b132
    *pVfy =  verify;
Packit 40b132
Packit 40b132
    return SECSuccess;
Packit 40b132
Packit 40b132
loser:
Packit 40b132
    if (objectID != CK_INVALID_HANDLE) {
Packit 40b132
	PK11_DestroyObject(slot,objectID);
Packit 40b132
    }
Packit 40b132
    if (parena != NULL) {
Packit 40b132
	PORT_FreeArena(parena,PR_FALSE);
Packit 40b132
    }
Packit 40b132
    if (varena != NULL) {
Packit 40b132
	PORT_FreeArena(varena,PR_FALSE);
Packit 40b132
    }
Packit 40b132
    if (slot) {
Packit 40b132
	PK11_FreeSlot(slot);
Packit 40b132
    }
Packit 40b132
    return SECFailure;
Packit 40b132
}
Packit 40b132
Packit 40b132
/* Generate PQGParams and PQGVerify structs.
Packit 40b132
 * Length of P specified by j.  Length of h will match length of P.
Packit 40b132
 * Length of SEED in bytes specified in seedBytes.
Packit 40b132
 * seedBbytes must be in the range [20..255] or an error will result.
Packit 40b132
 */
Packit 40b132
extern SECStatus
Packit 40b132
PK11_PQG_ParamGenSeedLen( unsigned int j, unsigned int seedBytes,
Packit 40b132
				 PQGParams **pParams, PQGVerify **pVfy)
Packit 40b132
{
Packit 40b132
    unsigned int primeBits = PQG_INDEX_TO_PBITS(j);
Packit 40b132
    return PK11_PQG_ParamGenV2(primeBits, 0, seedBytes, pParams, pVfy);
Packit 40b132
}
Packit 40b132
Packit 40b132
/* Generate PQGParams and PQGVerify structs.
Packit 40b132
 * Length of seed and length of h both equal length of P. 
Packit 40b132
 * All lengths are specified by "j", according to the table above.
Packit 40b132
 */
Packit 40b132
extern SECStatus
Packit 40b132
PK11_PQG_ParamGen(unsigned int j, PQGParams **pParams, PQGVerify **pVfy)
Packit 40b132
{
Packit 40b132
    unsigned int primeBits = PQG_INDEX_TO_PBITS(j);
Packit 40b132
    return PK11_PQG_ParamGenV2(primeBits, 0, 0, pParams, pVfy);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*  Test PQGParams for validity as DSS PQG values.
Packit 40b132
 *  If vfy is non-NULL, test PQGParams to make sure they were generated
Packit 40b132
 *       using the specified seed, counter, and h values.
Packit 40b132
 *
Packit 40b132
 *  Return value indicates whether Verification operation ran successfully
Packit 40b132
 *  to completion, but does not indicate if PQGParams are valid or not.
Packit 40b132
 *  If return value is SECSuccess, then *pResult has these meanings:
Packit 40b132
 *       SECSuccess: PQGParams are valid.
Packit 40b132
 *       SECFailure: PQGParams are invalid.
Packit 40b132
 */
Packit 40b132
Packit 40b132
extern SECStatus
Packit 40b132
PK11_PQG_VerifyParams(const PQGParams *params, const PQGVerify *vfy, 
Packit 40b132
							SECStatus *result)
Packit 40b132
{
Packit 40b132
    CK_ATTRIBUTE keyTempl[] = {
Packit 40b132
	{ CKA_CLASS, NULL, 0 },
Packit 40b132
	{ CKA_KEY_TYPE, NULL, 0 },
Packit 40b132
	{ CKA_PRIME, NULL, 0 },
Packit 40b132
	{ CKA_SUBPRIME, NULL, 0 },
Packit 40b132
	{ CKA_BASE, NULL, 0 },
Packit 40b132
	{ CKA_TOKEN, NULL, 0 },
Packit 40b132
	{ CKA_NETSCAPE_PQG_COUNTER, NULL, 0 },
Packit 40b132
	{ CKA_NETSCAPE_PQG_SEED, NULL, 0 },
Packit 40b132
	{ CKA_NETSCAPE_PQG_H, NULL, 0 },
Packit 40b132
    };
Packit 40b132
    CK_ATTRIBUTE *attrs;
Packit 40b132
    CK_BBOOL ckfalse = CK_FALSE;
Packit 40b132
    CK_OBJECT_CLASS class = CKO_KG_PARAMETERS;
Packit 40b132
    CK_KEY_TYPE keyType = CKK_DSA;
Packit 40b132
    SECStatus rv = SECSuccess;
Packit 40b132
    PK11SlotInfo *slot;
Packit 40b132
    int keyCount;
Packit 40b132
    CK_OBJECT_HANDLE objectID;
Packit 40b132
    CK_ULONG counter;
Packit 40b132
    CK_RV crv;
Packit 40b132
Packit 40b132
    attrs = keyTempl;
Packit 40b132
    PK11_SETATTRS(attrs, CKA_CLASS, &class, sizeof(class)); attrs++;
Packit 40b132
    PK11_SETATTRS(attrs, CKA_KEY_TYPE, &keyType, sizeof(keyType)); attrs++;
Packit 40b132
    PK11_SETATTRS(attrs, CKA_PRIME, params->prime.data, 
Packit 40b132
						params->prime.len); attrs++;
Packit 40b132
    PK11_SETATTRS(attrs, CKA_SUBPRIME, params->subPrime.data, 
Packit 40b132
						params->subPrime.len); attrs++;
Packit 40b132
    if (params->base.len) {
Packit 40b132
        PK11_SETATTRS(attrs, CKA_BASE,params->base.data,params->base.len);
Packit 40b132
	 attrs++;
Packit 40b132
    }
Packit 40b132
    PK11_SETATTRS(attrs, CKA_TOKEN, &ckfalse, sizeof(ckfalse)); attrs++;
Packit 40b132
    if (vfy) {
Packit 40b132
	if (vfy->counter != -1) {
Packit 40b132
	    counter = vfy->counter;
Packit 40b132
	    PK11_SETATTRS(attrs, CKA_NETSCAPE_PQG_COUNTER, 
Packit 40b132
			&counter, sizeof(counter)); attrs++;
Packit 40b132
	}
Packit 40b132
	PK11_SETATTRS(attrs, CKA_NETSCAPE_PQG_SEED, 
Packit 40b132
			vfy->seed.data, vfy->seed.len); attrs++;
Packit 40b132
	if (vfy->h.len) {
Packit 40b132
	    PK11_SETATTRS(attrs, CKA_NETSCAPE_PQG_H, 
Packit 40b132
			vfy->h.data, vfy->h.len); attrs++;
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
Packit 40b132
    keyCount = attrs - keyTempl;
Packit 40b132
    PORT_Assert(keyCount <= sizeof(keyTempl)/sizeof(keyTempl[0]));
Packit 40b132
Packit 40b132
Packit 40b132
    slot = PK11_GetInternalSlot();
Packit 40b132
    if (slot == NULL) {
Packit 40b132
	return SECFailure;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    PK11_EnterSlotMonitor(slot);
Packit 40b132
    crv = PK11_GETTAB(slot)->C_CreateObject(slot->session, keyTempl, keyCount, 
Packit 40b132
								&objectID);
Packit 40b132
    PK11_ExitSlotMonitor(slot);
Packit 40b132
Packit 40b132
    /* throw away the keys, we only wanted the return code */
Packit 40b132
    PK11_DestroyObject(slot,objectID);
Packit 40b132
    PK11_FreeSlot(slot);
Packit 40b132
Packit 40b132
    *result = SECSuccess;
Packit 40b132
    if (crv == CKR_ATTRIBUTE_VALUE_INVALID) {
Packit 40b132
	*result = SECFailure;
Packit 40b132
    } else if (crv != CKR_OK) {
Packit 40b132
	PORT_SetError( PK11_MapError(crv) );
Packit 40b132
	rv = SECFailure;
Packit 40b132
    }
Packit 40b132
    return rv;
Packit 40b132
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
Packit 40b132
/**************************************************************************
Packit 40b132
 *  Free the PQGParams struct and the things it points to.                *
Packit 40b132
 **************************************************************************/
Packit 40b132
extern void 
Packit 40b132
PK11_PQG_DestroyParams(PQGParams *params) {
Packit 40b132
    if (params == NULL) 
Packit 40b132
    	return;
Packit 40b132
    if (params->arena != NULL) {
Packit 40b132
	PORT_FreeArena(params->arena, PR_FALSE);	/* don't zero it */
Packit 40b132
    } else {
Packit 40b132
	SECITEM_FreeItem(&params->prime,    PR_FALSE); /* don't free prime */
Packit 40b132
	SECITEM_FreeItem(&params->subPrime, PR_FALSE); /* don't free subPrime */
Packit 40b132
	SECITEM_FreeItem(&params->base,     PR_FALSE); /* don't free base */
Packit 40b132
	PORT_Free(params);
Packit 40b132
    }
Packit 40b132
}
Packit 40b132
Packit 40b132
/**************************************************************************
Packit 40b132
 *  Free the PQGVerify struct and the things it points to.                *
Packit 40b132
 **************************************************************************/
Packit 40b132
extern void
Packit 40b132
PK11_PQG_DestroyVerify(PQGVerify *vfy) {
Packit 40b132
    if (vfy == NULL) 
Packit 40b132
    	return;
Packit 40b132
    if (vfy->arena != NULL) {
Packit 40b132
	PORT_FreeArena(vfy->arena, PR_FALSE);	/* don't zero it */
Packit 40b132
    } else {
Packit 40b132
	SECITEM_FreeItem(&vfy->seed,   PR_FALSE); /* don't free seed */
Packit 40b132
	SECITEM_FreeItem(&vfy->h,      PR_FALSE); /* don't free h */
Packit 40b132
	PORT_Free(vfy);
Packit 40b132
    }
Packit 40b132
}
Packit 40b132
Packit 40b132
#define PQG_DEFAULT_CHUNKSIZE 2048	/* bytes */
Packit 40b132
Packit 40b132
/**************************************************************************
Packit 40b132
 *  Return a pointer to a new PQGParams struct that is constructed from   *
Packit 40b132
 *  copies of the arguments passed in.                                    *
Packit 40b132
 *  Return NULL on failure.                                               *
Packit 40b132
 **************************************************************************/
Packit 40b132
extern PQGParams *
Packit 40b132
PK11_PQG_NewParams(const SECItem * prime, const SECItem * subPrime, 
Packit 40b132
                                 		const SECItem * base) {
Packit 40b132
    PLArenaPool *arena;
Packit 40b132
    PQGParams *dest;
Packit 40b132
    SECStatus status;
Packit 40b132
Packit 40b132
    arena = PORT_NewArena(PQG_DEFAULT_CHUNKSIZE);
Packit 40b132
    if (arena == NULL)
Packit 40b132
	goto loser;
Packit 40b132
Packit 40b132
    dest = (PQGParams*)PORT_ArenaZAlloc(arena, sizeof(PQGParams));
Packit 40b132
    if (dest == NULL)
Packit 40b132
	goto loser;
Packit 40b132
Packit 40b132
    dest->arena = arena;
Packit 40b132
Packit 40b132
    status = SECITEM_CopyItem(arena, &dest->prime, prime);
Packit 40b132
    if (status != SECSuccess)
Packit 40b132
	goto loser;
Packit 40b132
Packit 40b132
    status = SECITEM_CopyItem(arena, &dest->subPrime, subPrime);
Packit 40b132
    if (status != SECSuccess)
Packit 40b132
	goto loser;
Packit 40b132
Packit 40b132
    status = SECITEM_CopyItem(arena, &dest->base, base);
Packit 40b132
    if (status != SECSuccess)
Packit 40b132
	goto loser;
Packit 40b132
Packit 40b132
    return dest;
Packit 40b132
Packit 40b132
loser:
Packit 40b132
    if (arena != NULL)
Packit 40b132
	PORT_FreeArena(arena, PR_FALSE);
Packit 40b132
    return NULL;
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
/**************************************************************************
Packit 40b132
 * Fills in caller's "prime" SECItem with the prime value in params.
Packit 40b132
 * Contents can be freed by calling SECITEM_FreeItem(prime, PR_FALSE);	
Packit 40b132
 **************************************************************************/
Packit 40b132
extern SECStatus 
Packit 40b132
PK11_PQG_GetPrimeFromParams(const PQGParams *params, SECItem * prime) {
Packit 40b132
    return SECITEM_CopyItem(NULL, prime, &params->prime);
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
/**************************************************************************
Packit 40b132
 * Fills in caller's "subPrime" SECItem with the prime value in params.
Packit 40b132
 * Contents can be freed by calling SECITEM_FreeItem(subPrime, PR_FALSE);	
Packit 40b132
 **************************************************************************/
Packit 40b132
extern SECStatus
Packit 40b132
PK11_PQG_GetSubPrimeFromParams(const PQGParams *params, SECItem * subPrime) {
Packit 40b132
    return SECITEM_CopyItem(NULL, subPrime, &params->subPrime);
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
/**************************************************************************
Packit 40b132
 * Fills in caller's "base" SECItem with the base value in params.
Packit 40b132
 * Contents can be freed by calling SECITEM_FreeItem(base, PR_FALSE);	
Packit 40b132
 **************************************************************************/
Packit 40b132
extern SECStatus 
Packit 40b132
PK11_PQG_GetBaseFromParams(const PQGParams *params, SECItem *base) {
Packit 40b132
    return SECITEM_CopyItem(NULL, base, &params->base);
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
/**************************************************************************
Packit 40b132
 *  Return a pointer to a new PQGVerify struct that is constructed from   *
Packit 40b132
 *  copies of the arguments passed in.                                    *
Packit 40b132
 *  Return NULL on failure.                                               *
Packit 40b132
 **************************************************************************/
Packit 40b132
extern PQGVerify *
Packit 40b132
PK11_PQG_NewVerify(unsigned int counter, const SECItem * seed, 
Packit 40b132
							const SECItem * h) {
Packit 40b132
    PLArenaPool *arena;
Packit 40b132
    PQGVerify *  dest;
Packit 40b132
    SECStatus    status;
Packit 40b132
Packit 40b132
    arena = PORT_NewArena(PQG_DEFAULT_CHUNKSIZE);
Packit 40b132
    if (arena == NULL)
Packit 40b132
	goto loser;
Packit 40b132
Packit 40b132
    dest = (PQGVerify*)PORT_ArenaZAlloc(arena, sizeof(PQGVerify));
Packit 40b132
    if (dest == NULL)
Packit 40b132
	goto loser;
Packit 40b132
Packit 40b132
    dest->arena   = arena;
Packit 40b132
    dest->counter = counter;
Packit 40b132
Packit 40b132
    status = SECITEM_CopyItem(arena, &dest->seed, seed);
Packit 40b132
    if (status != SECSuccess)
Packit 40b132
	goto loser;
Packit 40b132
Packit 40b132
    status = SECITEM_CopyItem(arena, &dest->h, h);
Packit 40b132
    if (status != SECSuccess)
Packit 40b132
	goto loser;
Packit 40b132
Packit 40b132
    return dest;
Packit 40b132
Packit 40b132
loser:
Packit 40b132
    if (arena != NULL)
Packit 40b132
	PORT_FreeArena(arena, PR_FALSE);
Packit 40b132
    return NULL;
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
/**************************************************************************
Packit 40b132
 * Returns "counter" value from the PQGVerify.
Packit 40b132
 **************************************************************************/
Packit 40b132
extern unsigned int 
Packit 40b132
PK11_PQG_GetCounterFromVerify(const PQGVerify *verify) {
Packit 40b132
    return verify->counter;
Packit 40b132
}
Packit 40b132
Packit 40b132
/**************************************************************************
Packit 40b132
 * Fills in caller's "seed" SECItem with the seed value in verify.
Packit 40b132
 * Contents can be freed by calling SECITEM_FreeItem(seed, PR_FALSE);	
Packit 40b132
 **************************************************************************/
Packit 40b132
extern SECStatus 
Packit 40b132
PK11_PQG_GetSeedFromVerify(const PQGVerify *verify, SECItem *seed) {
Packit 40b132
    return SECITEM_CopyItem(NULL, seed, &verify->seed);
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
/**************************************************************************
Packit 40b132
 * Fills in caller's "h" SECItem with the h value in verify.
Packit 40b132
 * Contents can be freed by calling SECITEM_FreeItem(h, PR_FALSE);	
Packit 40b132
 **************************************************************************/
Packit 40b132
extern SECStatus 
Packit 40b132
PK11_PQG_GetHFromVerify(const PQGVerify *verify, SECItem * h) {
Packit 40b132
    return SECITEM_CopyItem(NULL, h, &verify->h);
Packit 40b132
}