Home | History | Annotate | Line # | Download | only in ec
      1 /*
      2  * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5  * this file except in compliance with the License.  You can obtain a copy
      6  * in the file LICENSE in the source distribution or at
      7  * https://www.openssl.org/source/license.html
      8  */
      9 
     10 /*
     11  * Suppress deprecation warnings for EC low level implementations that are
     12  * kept until removal.
     13  */
     14 #define OPENSSL_SUPPRESS_DEPRECATED
     15 
     16 #include <openssl/crypto.h>
     17 #include <openssl/err.h>
     18 #include <openssl/ec.h>
     19 
     20 #ifndef OPENSSL_NO_DEPRECATED_3_0
     21 BIGNUM *EC_POINT_point2bn(const EC_GROUP *group,
     22                           const EC_POINT *point,
     23                           point_conversion_form_t form,
     24                           BIGNUM *ret, BN_CTX *ctx)
     25 {
     26     size_t buf_len = 0;
     27     unsigned char *buf;
     28 
     29     buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
     30 
     31     if (buf_len == 0)
     32         return NULL;
     33 
     34     ret = BN_bin2bn(buf, buf_len, ret);
     35 
     36     OPENSSL_free(buf);
     37 
     38     return ret;
     39 }
     40 
     41 EC_POINT *EC_POINT_bn2point(const EC_GROUP *group,
     42                             const BIGNUM *bn, EC_POINT *point, BN_CTX *ctx)
     43 {
     44     size_t buf_len = 0;
     45     unsigned char *buf;
     46     EC_POINT *ret;
     47 
     48     if ((buf_len = BN_num_bytes(bn)) == 0)
     49         buf_len = 1;
     50     if ((buf = OPENSSL_malloc(buf_len)) == NULL) {
     51         ECerr(EC_F_EC_POINT_BN2POINT, ERR_R_MALLOC_FAILURE);
     52         return NULL;
     53     }
     54 
     55     if (BN_bn2binpad(bn, buf, buf_len) < 0) {
     56         OPENSSL_free(buf);
     57         return NULL;
     58     }
     59 
     60     if (point == NULL) {
     61         if ((ret = EC_POINT_new(group)) == NULL) {
     62             OPENSSL_free(buf);
     63             return NULL;
     64         }
     65     } else
     66         ret = point;
     67 
     68     if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx)) {
     69         if (ret != point)
     70             EC_POINT_clear_free(ret);
     71         OPENSSL_free(buf);
     72         return NULL;
     73     }
     74 
     75     OPENSSL_free(buf);
     76     return ret;
     77 }
     78 #endif /* OPENSSL_NO_DEPRECATED_3_0 */
     79