提交 981bd8a2 编写于 作者: D Dr. Stephen Henson

New EC functions.

New functions EC_POINT_point2buf and EC_KEY_key2buf which encode
a point and allocate a buffer in one call.

New function EC_KEY_oct2key() which sets public key in an EC_KEY
structure from an encoded point.
Reviewed-by: NRichard Levitte <levitte@openssl.org>
上级 19a86b03
...@@ -578,19 +578,11 @@ static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group, ...@@ -578,19 +578,11 @@ static ECPARAMETERS *ec_asn1_group2parameters(const EC_GROUP *group,
form = EC_GROUP_get_point_conversion_form(group); form = EC_GROUP_get_point_conversion_form(group);
len = EC_POINT_point2oct(group, point, form, NULL, len, NULL); len = EC_POINT_point2buf(group, point, form, &buffer, NULL);
if (len == 0) { if (len == 0) {
ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB); ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
goto err; goto err;
} }
if ((buffer = OPENSSL_malloc(len)) == NULL) {
ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
goto err;
}
if (!EC_POINT_point2oct(group, point, form, buffer, len, NULL)) {
ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_EC_LIB);
goto err;
}
if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) { if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE); ECerr(EC_F_EC_ASN1_GROUP2PARAMETERS, ERR_R_MALLOC_FAILURE);
goto err; goto err;
......
...@@ -575,3 +575,23 @@ void EC_KEY_clear_flags(EC_KEY *key, int flags) ...@@ -575,3 +575,23 @@ void EC_KEY_clear_flags(EC_KEY *key, int flags)
{ {
key->flags &= ~flags; key->flags &= ~flags;
} }
size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
unsigned char **pbuf, BN_CTX *ctx)
{
if (key == NULL || key->pub_key == NULL || key->group == NULL)
return 0;
return EC_POINT_point2buf(key->group, key->pub_key, form, pbuf, ctx);
}
int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
BN_CTX *ctx)
{
if (key == NULL || key->group == NULL)
return 0;
if (key->pub_key == NULL)
key->pub_key = EC_POINT_new(key->group);
if (key->pub_key == NULL)
return 0;
return EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx);
}
...@@ -190,3 +190,24 @@ int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point, ...@@ -190,3 +190,24 @@ int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
} }
return group->meth->oct2point(group, point, buf, len, ctx); return group->meth->oct2point(group, point, buf, len, ctx);
} }
size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point,
point_conversion_form_t form,
unsigned char **pbuf, BN_CTX *ctx)
{
size_t len;
unsigned char *buf;
len = EC_POINT_point2oct(group, point, form, NULL, 0, NULL);
if (len == 0)
return 0;
buf = OPENSSL_malloc(len);
if (buf == NULL)
return 0;
len = EC_POINT_point2oct(group, point, form, buf, len, ctx);
if (len == 0) {
OPENSSL_free(buf);
return 0;
}
*pbuf = buf;
return len;
}
...@@ -64,17 +64,10 @@ BIGNUM *EC_POINT_point2bn(const EC_GROUP *group, ...@@ -64,17 +64,10 @@ BIGNUM *EC_POINT_point2bn(const EC_GROUP *group,
size_t buf_len = 0; size_t buf_len = 0;
unsigned char *buf; unsigned char *buf;
buf_len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx); buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
if (buf_len == 0)
return NULL;
if ((buf = OPENSSL_malloc(buf_len)) == NULL)
return NULL;
if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx)) { if (buf_len == 0)
OPENSSL_free(buf);
return NULL; return NULL;
}
ret = BN_bin2bn(buf, buf_len, ret); ret = BN_bin2bn(buf, buf_len, ret);
...@@ -129,19 +122,12 @@ char *EC_POINT_point2hex(const EC_GROUP *group, ...@@ -129,19 +122,12 @@ char *EC_POINT_point2hex(const EC_GROUP *group,
{ {
char *ret, *p; char *ret, *p;
size_t buf_len = 0, i; size_t buf_len = 0, i;
unsigned char *buf, *pbuf; unsigned char *buf = NULL, *pbuf;
buf_len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx); buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
if (buf_len == 0)
return NULL;
if ((buf = OPENSSL_malloc(buf_len)) == NULL)
return NULL;
if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx)) { if (buf_len == 0)
OPENSSL_free(buf);
return NULL; return NULL;
}
ret = OPENSSL_malloc(buf_len * 2 + 2); ret = OPENSSL_malloc(buf_len * 2 + 2);
if (ret == NULL) { if (ret == NULL) {
......
...@@ -589,6 +589,20 @@ size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *p, ...@@ -589,6 +589,20 @@ size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *p,
int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *p, int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *p,
const unsigned char *buf, size_t len, BN_CTX *ctx); const unsigned char *buf, size_t len, BN_CTX *ctx);
/** Encodes an EC_POINT object to an allocated octet string
* \param group underlying EC_GROUP object
* \param point EC_POINT object
* \param form point conversion form
* \param pbuf returns pointer to allocated buffer
* \param len length of the memory buffer
* \param ctx BN_CTX object (optional)
* \return the length of the encoded octet string or 0 if an error occurred
*/
size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point,
point_conversion_form_t form,
unsigned char **pbuf, BN_CTX *ctx);
/* other interfaces to point2oct/oct2point: */ /* other interfaces to point2oct/oct2point: */
BIGNUM *EC_POINT_point2bn(const EC_GROUP *, const EC_POINT *, BIGNUM *EC_POINT_point2bn(const EC_GROUP *, const EC_POINT *,
point_conversion_form_t form, BIGNUM *, BN_CTX *); point_conversion_form_t form, BIGNUM *, BN_CTX *);
...@@ -887,6 +901,29 @@ int EC_KEY_check_key(const EC_KEY *key); ...@@ -887,6 +901,29 @@ int EC_KEY_check_key(const EC_KEY *key);
int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x, int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
BIGNUM *y); BIGNUM *y);
/** Encodes an EC_KEY public key to an allocated octet string
* \param key key to encode
* \param form point conversion form
* \param pbuf returns pointer to allocated buffer
* \param len length of the memory buffer
* \param ctx BN_CTX object (optional)
* \return the length of the encoded octet string or 0 if an error occurred
*/
size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
unsigned char **pbuf, BN_CTX *ctx);
/** Decodes a EC_KEY public key from a octet string
* \param key key to decode
* \param buf memory buffer with the encoded ec point
* \param len length of the encoded ec point
* \param ctx BN_CTX object (optional)
* \return 1 on success and 0 if an error occurred
*/
int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
BN_CTX *ctx);
/********************************************************************/ /********************************************************************/
/* de- and encoding functions for SEC1 ECPrivateKey */ /* de- and encoding functions for SEC1 ECPrivateKey */
/********************************************************************/ /********************************************************************/
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册