Skip to content

Commit

Permalink
Bug 957105 - Curve25519, r=mt,rrelyea
Browse files Browse the repository at this point in the history
--HG--
extra : rebase_source : 5cb9476753a43f513701916c0ed1dc9da999fe92
  • Loading branch information
franziskuskiefer committed Sep 19, 2016
1 parent cc10192 commit f2a024c
Show file tree
Hide file tree
Showing 39 changed files with 2,081 additions and 333 deletions.
4 changes: 3 additions & 1 deletion cmd/bltest/blapitest.c
Expand Up @@ -159,7 +159,7 @@ Usage()
PRINTUSAGE("", "", " c2onb239v4, c2onb239v5, c2pnb272w1, c2pnb304w1,");
PRINTUSAGE("", "", " c2tnb359w1, c2pnb368w1, c2tnb431r1, secp112r1,");
PRINTUSAGE("", "", " secp112r2, secp128r1, secp128r2, sect113r1, sect113r2,");
PRINTUSAGE("", "", " sect131r1, sect131r2");
PRINTUSAGE("", "", " sect131r1, sect131r2, curve25519");
#endif
PRINTUSAGE("", "-p", "do performance test");
PRINTUSAGE("", "-4", "run test in multithread mode. th_num number of parallel threads");
Expand Down Expand Up @@ -496,6 +496,7 @@ static CurveNameTagPair nameTagPair[] =
{ "sect113r2", SEC_OID_SECG_EC_SECT113R2 },
{ "sect131r1", SEC_OID_SECG_EC_SECT131R1 },
{ "sect131r2", SEC_OID_SECG_EC_SECT131R2 },
{ "curve25519", SEC_OID_CURVE25519 },
};

static SECItem *
Expand Down Expand Up @@ -1887,6 +1888,7 @@ bltest_ecdsa_init(bltestCipherInfo *cipherInfo, PRBool encrypt)
pubkey->ecParams.DEREncoding.len = key->ecParams.DEREncoding.len;
pubkey->ecParams.DEREncoding.data = key->ecParams.DEREncoding.data;
pubkey->ecParams.name = key->ecParams.name;
pubkey->ecParams.pointSize = key->ecParams.pointSize;
pubkey->publicValue.len = key->publicValue.len;
pubkey->publicValue.data = key->publicValue.data;
asymk->pubKey = pubkey;
Expand Down
2 changes: 1 addition & 1 deletion cmd/certutil/certutil.c
Expand Up @@ -1257,7 +1257,7 @@ luG(enum usage_level ul, const char *command)
#ifndef NSS_DISABLE_ECC
FPS "%-20s Elliptic curve name (ec only)\n",
" -q curve-name");
FPS "%-20s One of nistp256, nistp384, nistp521\n", "");
FPS "%-20s One of nistp256, nistp384, nistp521, curve25519\n", "");
#ifdef NSS_ECC_MORE_THAN_SUITE_B
FPS "%-20s sect163k1, nistk163, sect163r1, sect163r2,\n", "");
FPS "%-20s nistb163, sect193r1, sect193r2, sect233k1, nistk233,\n", "");
Expand Down
1 change: 1 addition & 0 deletions cmd/certutil/keystuff.c
Expand Up @@ -476,6 +476,7 @@ static CurveNameTagPair nameTagPair[] =
{ "sect113r2", SEC_OID_SECG_EC_SECT113R2 },
{ "sect131r1", SEC_OID_SECG_EC_SECT131R1 },
{ "sect131r2", SEC_OID_SECG_EC_SECT131R2 },
{ "curve25519", SEC_OID_CURVE25519 },
};

static SECKEYECParams *
Expand Down
143 changes: 87 additions & 56 deletions cmd/ecperf/ecperf.c
Expand Up @@ -86,7 +86,8 @@ static SECOidTag ecCurve_oid_map[] = {
SEC_OID_UNKNOWN, /* ECCurve_WTLS_1 */
SEC_OID_UNKNOWN, /* ECCurve_WTLS_8 */
SEC_OID_UNKNOWN, /* ECCurve_WTLS_9 */
SEC_OID_UNKNOWN /* ECCurve_pastLastCurve */
SEC_OID_CURVE25519,
SEC_OID_UNKNOWN /* ECCurve_pastLastCurve */
};

typedef SECStatus (*op_func)(void *, void *, void *);
Expand Down Expand Up @@ -258,20 +259,37 @@ M_TimeOperation(void (*threadFunc)(void *),
}

/* Test curve using specific field arithmetic. */
#define ECTEST_NAMED_GFP(name_c, name_v) \
if (usefreebl) { \
printf("Testing %s using freebl implementation...\n", name_c); \
rv = ectest_curve_freebl(name_v, iterations, numThreads); \
if (rv != SECSuccess) \
goto cleanup; \
printf("... okay.\n"); \
} \
if (usepkcs11) { \
printf("Testing %s using pkcs11 implementation...\n", name_c); \
rv = ectest_curve_pkcs11(name_v, iterations, numThreads); \
if (rv != SECSuccess) \
goto cleanup; \
printf("... okay.\n"); \
#define ECTEST_NAMED_GFP(name_c, name_v) \
if (usefreebl) { \
printf("Testing %s using freebl implementation...\n", name_c); \
rv = ectest_curve_freebl(name_v, iterations, numThreads, ec_field_GFp); \
if (rv != SECSuccess) \
goto cleanup; \
printf("... okay.\n"); \
} \
if (usepkcs11) { \
printf("Testing %s using pkcs11 implementation...\n", name_c); \
rv = ectest_curve_pkcs11(name_v, iterations, numThreads); \
if (rv != SECSuccess) \
goto cleanup; \
printf("... okay.\n"); \
}

/* Test curve using specific field arithmetic. */
#define ECTEST_NAMED_CUSTOM(name_c, name_v) \
if (usefreebl) { \
printf("Testing %s using freebl implementation...\n", name_c); \
rv = ectest_curve_freebl(name_v, iterations, numThreads, ec_field_plain); \
if (rv != SECSuccess) \
goto cleanup; \
printf("... okay.\n"); \
} \
if (usepkcs11) { \
printf("Testing %s using pkcs11 implementation...\n", name_c); \
rv = ectest_curve_pkcs11(name_v, iterations, numThreads); \
if (rv != SECSuccess) \
goto cleanup; \
printf("... okay.\n"); \
}

/*
Expand Down Expand Up @@ -510,29 +528,34 @@ ectest_curve_pkcs11(ECCurveName curve, int iterations, int numThreads)

lock = PR_NewLock();

rv = M_TimeOperation(PKCS11Thread, (op_func)PKCS11_Derive, "ECDH_Derive",
&ecPriv, &mech, NULL, iterations, numThreads,
lock, session, 0, &deriveRate);
if (rv != SECSuccess) {
goto cleanup;
}
rv = M_TimeOperation(PKCS11Thread, (op_func)PKCS11_Sign, "ECDSA_Sign",
(void *)&ecPriv, &sig, &digest, iterations, numThreads,
lock, session, 1, &signRate);
if (rv != SECSuccess) {
goto cleanup;
}
printf(" ECDHE max rate = %.2f\n", (deriveRate + signRate) / 4.0);
/* get a signature */
rv = PKCS11_Sign(session, &ecPriv, &sig, &digest);
if (rv != SECSuccess) {
goto cleanup;
if (ecCurve_map[curve]->usage & KU_KEY_AGREEMENT) {
rv = M_TimeOperation(PKCS11Thread, (op_func)PKCS11_Derive, "ECDH_Derive",
&ecPriv, &mech, NULL, iterations, numThreads,
lock, session, 0, &deriveRate);
if (rv != SECSuccess) {
goto cleanup;
}
}
rv = M_TimeOperation(PKCS11Thread, (op_func)PKCS11_Verify, "ECDSA_Verify",
(void *)&ecPub, &sig, &digest, iterations, numThreads,
lock, session, 0, NULL);
if (rv != SECSuccess) {
goto cleanup;

if (ecCurve_map[curve]->usage & KU_DIGITAL_SIGNATURE) {
rv = M_TimeOperation(PKCS11Thread, (op_func)PKCS11_Sign, "ECDSA_Sign",
(void *)&ecPriv, &sig, &digest, iterations, numThreads,
lock, session, 1, &signRate);
if (rv != SECSuccess) {
goto cleanup;
}
printf(" ECDHE max rate = %.2f\n", (deriveRate + signRate) / 4.0);
/* get a signature */
rv = PKCS11_Sign(session, &ecPriv, &sig, &digest);
if (rv != SECSuccess) {
goto cleanup;
}
rv = M_TimeOperation(PKCS11Thread, (op_func)PKCS11_Verify, "ECDSA_Verify",
(void *)&ecPub, &sig, &digest, iterations, numThreads,
lock, session, 0, NULL);
if (rv != SECSuccess) {
goto cleanup;
}
}

cleanup:
Expand Down Expand Up @@ -562,7 +585,8 @@ ECDH_DeriveWrap(ECPrivateKey *priv, ECPublicKey *pub, int *dummy)
* If tests fail, then it prints an error message, aborts, and returns an
* error code. Otherwise, returns 0. */
SECStatus
ectest_curve_freebl(ECCurveName curve, int iterations, int numThreads)
ectest_curve_freebl(ECCurveName curve, int iterations, int numThreads,
ECFieldType fieldType)
{
ECParams ecParams = { 0 };
ECPrivateKey *ecPriv = NULL;
Expand Down Expand Up @@ -594,9 +618,10 @@ ectest_curve_freebl(ECCurveName curve, int iterations, int numThreads)
ecParams.curve.seed.len = 0;
ecParams.DEREncoding.data = NULL;
ecParams.DEREncoding.len = 0;
ecParams.pointSize = ecCurve_map[curve]->pointSize;

ecParams.fieldID.size = ecCurve_map[curve]->size;
ecParams.fieldID.type = ec_field_GFp;
ecParams.fieldID.type = fieldType;
hexString2SECItem(arena, &ecParams.fieldID.u.prime, ecCurve_map[curve]->irr);
hexString2SECItem(arena, &ecParams.curve.a, ecCurve_map[curve]->curvea);
hexString2SECItem(arena, &ecParams.curve.b, ecCurve_map[curve]->curveb);
Expand All @@ -622,24 +647,29 @@ ectest_curve_freebl(ECCurveName curve, int iterations, int numThreads)
ecPub.ecParams = ecParams;
ecPub.publicValue = ecPriv->publicValue;

rv = M_TimeOperation(genericThread, (op_func)ECDH_DeriveWrap, "ECDH_Derive",
ecPriv, &ecPub, NULL, iterations, numThreads, 0, 0, 0, &deriveRate);
if (rv != SECSuccess) {
goto cleanup;
}
rv = M_TimeOperation(genericThread, (op_func)ECDSA_SignDigest, "ECDSA_Sign",
ecPriv, &sig, &digest, iterations, numThreads, 0, 0, 1, &signRate);
if (rv != SECSuccess)
goto cleanup;
printf(" ECDHE max rate = %.2f\n", (deriveRate + signRate) / 4.0);
rv = ECDSA_SignDigest(ecPriv, &sig, &digest);
if (rv != SECSuccess) {
goto cleanup;
if (ecCurve_map[curve]->usage & KU_KEY_AGREEMENT) {
rv = M_TimeOperation(genericThread, (op_func)ECDH_DeriveWrap, "ECDH_Derive",
ecPriv, &ecPub, NULL, iterations, numThreads, 0, 0, 0, &deriveRate);
if (rv != SECSuccess) {
goto cleanup;
}
}
rv = M_TimeOperation(genericThread, (op_func)ECDSA_VerifyDigest, "ECDSA_Verify",
&ecPub, &sig, &digest, iterations, numThreads, 0, 0, 0, NULL);
if (rv != SECSuccess) {
goto cleanup;

if (ecCurve_map[curve]->usage & KU_DIGITAL_SIGNATURE) {
rv = M_TimeOperation(genericThread, (op_func)ECDSA_SignDigest, "ECDSA_Sign",
ecPriv, &sig, &digest, iterations, numThreads, 0, 0, 1, &signRate);
if (rv != SECSuccess)
goto cleanup;
printf(" ECDHE max rate = %.2f\n", (deriveRate + signRate) / 4.0);
rv = ECDSA_SignDigest(ecPriv, &sig, &digest);
if (rv != SECSuccess) {
goto cleanup;
}
rv = M_TimeOperation(genericThread, (op_func)ECDSA_VerifyDigest, "ECDSA_Verify",
&ecPub, &sig, &digest, iterations, numThreads, 0, 0, 0, NULL);
if (rv != SECSuccess) {
goto cleanup;
}
}

cleanup:
Expand Down Expand Up @@ -743,6 +773,7 @@ main(int argv, char **argc)
ECTEST_NAMED_GFP("NIST-P256", ECCurve_NIST_P256);
ECTEST_NAMED_GFP("NIST-P384", ECCurve_NIST_P384);
ECTEST_NAMED_GFP("NIST-P521", ECCurve_NIST_P521);
ECTEST_NAMED_CUSTOM("Curve25519", ECCurve25519);
}
#ifdef NSS_ECC_MORE_THAN_SUITE_B
if (ansi) {
Expand Down
6 changes: 0 additions & 6 deletions cmd/ecperf/manifest.mn
Expand Up @@ -11,12 +11,6 @@ MODULE = nss

INCLUDES += -I$(CORE_DEPTH)/nss/lib/softoken

# This next line is used by .mk files
# and gets translated into $LINCS in manifest.mnw
REQUIRES = dbm seccmd

# DIRS =

CSRCS = ecperf.c

PROGRAM = ecperf
Expand Down

0 comments on commit f2a024c

Please sign in to comment.