/* Generates a new EC key pair. The private key is a random value and * the public key is the result of performing a scalar point multiplication * of that value with the curve's base point. */ SECStatus EC_NewKey(ECParams *ecParams, ECPrivateKey **privKey) { SECStatus rv = SECFailure; #ifndef NSS_DISABLE_ECC int len; unsigned char *privKeyBytes = NULL; if (!ecParams) { PORT_SetError(SEC_ERROR_INVALID_ARGS); return SECFailure; } len = ecParams->order.len; privKeyBytes = ec_GenerateRandomPrivateKey(ecParams->order.data, len); if (privKeyBytes == NULL) goto cleanup; /* generate public key */ CHECK_SEC_OK( ec_NewKey(ecParams, privKey, privKeyBytes, len) ); cleanup: if (privKeyBytes) { PORT_ZFree(privKeyBytes, len); } #if EC_DEBUG printf("EC_NewKey returning %s\n", (rv == SECSuccess) ? "success" : "failure"); #endif #else PORT_SetError(SEC_ERROR_UNSUPPORTED_KEYALG); #endif /* NSS_DISABLE_ECC */ return rv; }
/* Generates a new EC key pair. The private key is a random value and * the public key is the result of performing a scalar point multiplication * of that value with the curve's base point. */ SECStatus EC_NewKey(ECParams *ecParams, ECPrivateKey **privKey, const unsigned char* random, int randomlen, int kmflag) { SECStatus rv = SECFailure; int len; unsigned char *privKeyBytes = NULL; if (!ecParams) { PORT_SetError(SEC_ERROR_INVALID_ARGS); return SECFailure; } len = ecParams->order.len; privKeyBytes = ec_GenerateRandomPrivateKey(ecParams->order.data, len, random, randomlen, kmflag); if (privKeyBytes == NULL) goto cleanup; /* generate public key */ CHECK_SEC_OK( ec_NewKey(ecParams, privKey, privKeyBytes, len, kmflag) ); cleanup: if (privKeyBytes) { PORT_ZFree(privKeyBytes, len * 2); } #if EC_DEBUG printf("EC_NewKey returning %s\n", (rv == SECSuccess) ? "success" : "failure"); #endif return rv; }
/* ** Computes the ECDSA signature on the digest using the given key ** and a random seed. */ SECStatus ECDSA_SignDigest(ECPrivateKey *key, SECItem *signature, const SECItem *digest, const unsigned char* random, int randomLen, int kmflag) { SECStatus rv = SECFailure; int len; unsigned char *kBytes= NULL; if (!key) { PORT_SetError(SEC_ERROR_INVALID_ARGS); return SECFailure; } /* Generate random value k */ len = key->ecParams.order.len; kBytes = ec_GenerateRandomPrivateKey(key->ecParams.order.data, len, random, randomLen, kmflag); if (kBytes == NULL) goto cleanup; /* Generate ECDSA signature with the specified k value */ rv = ECDSA_SignDigestWithSeed(key, signature, digest, kBytes, len, kmflag); cleanup: if (kBytes) { PORT_ZFree(kBytes, len * 2); } #if EC_DEBUG printf("ECDSA signing %s\n", (rv == SECSuccess) ? "succeeded" : "failed"); #endif return rv; }
/* ** Computes the ECDSA signature on the digest using the given key ** and a random seed. */ SECStatus ECDSA_SignDigest(ECPrivateKey *key, SECItem *signature, const SECItem *digest) { SECStatus rv = SECFailure; #ifndef NSS_DISABLE_ECC int len; unsigned char *kBytes = NULL; if (!key) { PORT_SetError(SEC_ERROR_INVALID_ARGS); return SECFailure; } /* Generate random value k */ len = key->ecParams.order.len; kBytes = ec_GenerateRandomPrivateKey(key->ecParams.order.data, len); if (kBytes == NULL) goto cleanup; /* Generate ECDSA signature with the specified k value */ rv = ECDSA_SignDigestWithSeed(key, signature, digest, kBytes, len); cleanup: if (kBytes) { PORT_ZFree(kBytes, len); } #if EC_DEBUG printf("ECDSA signing %s\n", (rv == SECSuccess) ? "succeeded" : "failed"); #endif #else PORT_SetError(SEC_ERROR_UNSUPPORTED_KEYALG); #endif /* NSS_DISABLE_ECC */ return rv; }