コード例 #1
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;
    }
}
コード例 #2
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);
}