【问题标题】:ECDH secrets generated by BouncyCastle Java API and by OpenSSL are differentBouncyCastle Java API 和 OpenSSL 生成的 ECDH 机密是不同的
【发布时间】:2014-03-03 14:27:12
【问题描述】:

我正在尝试使用椭圆曲线加密。我需要同一事物的两种实现,一种在 Java 中,另一种在 C 中。我正在使用使用曲线 secp256k1 生成的两个密钥对来测试它们。当我在 Java 中生成派生密钥时,我得到的数字总是与从 OpenSSL 得到的数字不同。

Java 代码:

/* privateKey and peerPublicKey are generated with the following parameters */
ECParameterSpec paramSpec = ECNamedCurveTable.getParameterSpec("secp256k1");
/* ... */
Provider BC = new BouncyCastleProvider();
KeyAgreement agr = KeyAgreement.getInstance("ECDH", BC);
agr.init(privateKey);
agr.doPhase(peerPublicKey, true);
byte[] secret = agr.generateSecret();

C 代码

/* pkey and peerkey are generated using EC_KEY_new_by_curve_name(NID_secp256k1) */
/* and than wrapped in an EVP_PKEY */
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pkey, NULL);
uint8_t *secret = NULL;
size_t secret_len;
EVP_PKEY_derive_init(ctx);
EVP_PKEY_derive_set_peer(ctx, peerkey);
EVP_PKEY_derive(ctx, NULL, &secret_len);
secret = malloc(secret_len);
EVP_PKEY_derive(ctx, secret, &secret_len);

我确定密钥是有效的,并且它们在 C 代码和 Java 代码中都是相同的,但我不明白为什么派生的秘密是不同的。我错过了什么吗?

谢谢

【问题讨论】:

  • “当我在 Java 中生成派生密钥时,我得到的数字总是与从 OpenSSL 得到的数字不同。” - 确切地说,你的意思是什么?协议的每次执行是否会产生不同的秘密?还是 OpenSSL 客户端和 BC 客户端之间的协议执行未达到共享密钥的情况?

标签: java openssl bouncycastle elliptic-curve diffie-hellman


【解决方案1】:
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pkey, NULL);
uint8_t *secret = NULL;
size_t secret_len;
EVP_PKEY_derive_init(ctx);
EVP_PKEY_derive_set_peer(ctx, peerkey);
EVP_PKEY_derive(ctx, NULL, &secret_len);
secret = malloc(secret_len);
EVP_PKEY_derive(ctx, secret, &secret_len);

此代码看起来缺少几个步骤。例如,EVP_PKEY_paramgen_init 不存在。

OpenSSL wiki 在Elliptic Curve Diffie-Hellman 上有一个示例。我将在下面复制/粘贴它以避免仅提供链接的答案,但我相信它是 Matt Caswell 的作品。

EVP_PKEY_CTX *pctx, *kctx;
EVP_PKEY_CTX *ctx;
unsigned char *secret;
EVP_PKEY *pkey = NULL, *peerkey, *params = NULL;

/* Create the context for parameter generation */
if(NULL == (pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL))) handleErrors();

/* Initialise the parameter generation */
if(1 != EVP_PKEY_paramgen_init(pctx)) handleErrors();

/* We're going to use the ANSI X9.62 Prime 256v1 curve */
if(1 != EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_X9_62_prime256v1)) handleErrors();

/* Create the parameter object params */
if (!EVP_PKEY_paramgen(pctx, &params)) handleErrors();

/* Create the context for the key generation */
if(NULL == (kctx = EVP_PKEY_CTX_new(params, NULL))) handleErrors();

/* Generate the key */
if(1 != EVP_PKEY_keygen_init(kctx)) handleErrors();
if (1 != EVP_PKEY_keygen(kctx, &pkey)) handleErrors();

/* Get the peer's public key, and provide the peer with our public key -
 * how this is done will be specific to your circumstances */
peerkey = get_peerkey(pkey);

/* Create the context for the shared secret derivation */
if(NULL == (ctx = EVP_PKEY_CTX_new(pkey, NULL))) handleErrors();

/* Initialise */
if(1 != EVP_PKEY_derive_init(ctx)) handleErrors();

/* Provide the peer public key */
if(1 != EVP_PKEY_derive_set_peer(ctx, peerkey)) handleErrors();

/* Determine buffer length for shared secret */
if(1 != EVP_PKEY_derive(ctx, NULL, secret_len)) handleErrors();

/* Create the buffer */
if(NULL == (secret = OPENSSL_malloc(*secret_len))) handleErrors();

/* Derive the shared secret */
if(1 != (EVP_PKEY_derive(ctx, secret, secret_len))) handleErrors();

EVP_PKEY_CTX_free(ctx);
EVP_PKEY_free(peerkey);
EVP_PKEY_free(pkey);
EVP_PKEY_CTX_free(kctx);
EVP_PKEY_free(params);
EVP_PKEY_CTX_free(pctx);

/* Never use a derived secret directly. Typically it is passed
 * through some hash function to produce a key */
return secret;

当我在 Java 中生成派生密钥时,我得到的数字总是与从 OpenSSL 得到的数字不同。

协议的每次运行都会产生不同的结果。这是因为每一方都会为协议的每次运行选择一个随机值。即g^a中的a是随机的,每次运行都不一样,所以每次运行的公钥A = g^a都不一样。

如果一切正常,您将永远不会看到各方使用相同的值,或者一方重用过去的值。独立执行永远不会产生相同的结果。它的 OpenSSL ↔ OpenSSL、OpenSSL ↔ Java 还是 Java ↔ Java 无关紧要。它们总会产生不同的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 2012-01-30
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    相关资源
    最近更新 更多