/src/openssl30/crypto/ec/ec_oct.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 2011-2021 The OpenSSL Project Authors. All Rights Reserved. | 
| 3 |  |  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved | 
| 4 |  |  * | 
| 5 |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
| 6 |  |  * this file except in compliance with the License.  You can obtain a copy | 
| 7 |  |  * in the file LICENSE in the source distribution or at | 
| 8 |  |  * https://www.openssl.org/source/license.html | 
| 9 |  |  */ | 
| 10 |  |  | 
| 11 |  | /* | 
| 12 |  |  * ECDSA low level APIs are deprecated for public use, but still ok for | 
| 13 |  |  * internal use. | 
| 14 |  |  */ | 
| 15 |  | #include "internal/deprecated.h" | 
| 16 |  |  | 
| 17 |  | #include <string.h> | 
| 18 |  |  | 
| 19 |  | #include <openssl/err.h> | 
| 20 |  | #include <openssl/opensslv.h> | 
| 21 |  |  | 
| 22 |  | #include "ec_local.h" | 
| 23 |  |  | 
| 24 |  | int EC_POINT_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point, | 
| 25 |  |                                         const BIGNUM *x, int y_bit, BN_CTX *ctx) | 
| 26 | 137k | { | 
| 27 | 137k |     if (group->meth->point_set_compressed_coordinates == NULL | 
| 28 | 137k |         && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) { | 
| 29 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | 
| 30 | 0 |         return 0; | 
| 31 | 0 |     } | 
| 32 | 137k |     if (!ec_point_is_compat(point, group)) { | 
| 33 | 0 |         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); | 
| 34 | 0 |         return 0; | 
| 35 | 0 |     } | 
| 36 | 137k |     if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) { | 
| 37 | 137k |         if (group->meth->field_type == NID_X9_62_prime_field) | 
| 38 | 80.7k |             return ossl_ec_GFp_simple_set_compressed_coordinates(group, point, x, | 
| 39 | 80.7k |                                                                  y_bit, ctx); | 
| 40 | 56.9k |         else | 
| 41 |  | #ifdef OPENSSL_NO_EC2M | 
| 42 |  |         { | 
| 43 |  |             ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED); | 
| 44 |  |             return 0; | 
| 45 |  |         } | 
| 46 |  | #else | 
| 47 | 56.9k |             return ossl_ec_GF2m_simple_set_compressed_coordinates(group, point, | 
| 48 | 56.9k |                                                                   x, y_bit, ctx); | 
| 49 | 137k | #endif | 
| 50 | 137k |     } | 
| 51 | 0 |     return group->meth->point_set_compressed_coordinates(group, point, x, | 
| 52 | 0 |                                                          y_bit, ctx); | 
| 53 | 137k | } | 
| 54 |  |  | 
| 55 |  | #ifndef OPENSSL_NO_DEPRECATED_3_0 | 
| 56 |  | int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group, | 
| 57 |  |                                             EC_POINT *point, const BIGNUM *x, | 
| 58 |  |                                             int y_bit, BN_CTX *ctx) | 
| 59 | 0 | { | 
| 60 | 0 |     return EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx); | 
| 61 | 0 | } | 
| 62 |  |  | 
| 63 |  | # ifndef OPENSSL_NO_EC2M | 
| 64 |  | int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group, | 
| 65 |  |                                              EC_POINT *point, const BIGNUM *x, | 
| 66 |  |                                              int y_bit, BN_CTX *ctx) | 
| 67 | 0 | { | 
| 68 | 0 |     return EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx); | 
| 69 | 0 | } | 
| 70 |  | # endif | 
| 71 |  | #endif | 
| 72 |  |  | 
| 73 |  | size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point, | 
| 74 |  |                           point_conversion_form_t form, unsigned char *buf, | 
| 75 |  |                           size_t len, BN_CTX *ctx) | 
| 76 | 56.5k | { | 
| 77 | 56.5k |     if (group->meth->point2oct == 0 | 
| 78 | 56.5k |         && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) { | 
| 79 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | 
| 80 | 0 |         return 0; | 
| 81 | 0 |     } | 
| 82 | 56.5k |     if (!ec_point_is_compat(point, group)) { | 
| 83 | 0 |         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); | 
| 84 | 0 |         return 0; | 
| 85 | 0 |     } | 
| 86 | 56.5k |     if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) { | 
| 87 | 56.5k |         if (group->meth->field_type == NID_X9_62_prime_field) | 
| 88 | 53.6k |             return ossl_ec_GFp_simple_point2oct(group, point, form, buf, len, | 
| 89 | 53.6k |                                                 ctx); | 
| 90 | 2.88k |         else | 
| 91 |  | #ifdef OPENSSL_NO_EC2M | 
| 92 |  |         { | 
| 93 |  |             ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED); | 
| 94 |  |             return 0; | 
| 95 |  |         } | 
| 96 |  | #else | 
| 97 | 2.88k |             return ossl_ec_GF2m_simple_point2oct(group, point, | 
| 98 | 2.88k |                                                  form, buf, len, ctx); | 
| 99 | 56.5k | #endif | 
| 100 | 56.5k |     } | 
| 101 |  |  | 
| 102 | 0 |     return group->meth->point2oct(group, point, form, buf, len, ctx); | 
| 103 | 56.5k | } | 
| 104 |  |  | 
| 105 |  | int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point, | 
| 106 |  |                        const unsigned char *buf, size_t len, BN_CTX *ctx) | 
| 107 | 261k | { | 
| 108 | 261k |     if (group->meth->oct2point == 0 | 
| 109 | 261k |         && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) { | 
| 110 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | 
| 111 | 0 |         return 0; | 
| 112 | 0 |     } | 
| 113 | 261k |     if (!ec_point_is_compat(point, group)) { | 
| 114 | 0 |         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS); | 
| 115 | 0 |         return 0; | 
| 116 | 0 |     } | 
| 117 | 261k |     if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) { | 
| 118 | 261k |         if (group->meth->field_type == NID_X9_62_prime_field) | 
| 119 | 169k |             return ossl_ec_GFp_simple_oct2point(group, point, buf, len, ctx); | 
| 120 | 92.5k |         else | 
| 121 |  | #ifdef OPENSSL_NO_EC2M | 
| 122 |  |         { | 
| 123 |  |             ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED); | 
| 124 |  |             return 0; | 
| 125 |  |         } | 
| 126 |  | #else | 
| 127 | 92.5k |             return ossl_ec_GF2m_simple_oct2point(group, point, buf, len, ctx); | 
| 128 | 261k | #endif | 
| 129 | 261k |     } | 
| 130 | 0 |     return group->meth->oct2point(group, point, buf, len, ctx); | 
| 131 | 261k | } | 
| 132 |  |  | 
| 133 |  | size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point, | 
| 134 |  |                           point_conversion_form_t form, | 
| 135 |  |                           unsigned char **pbuf, BN_CTX *ctx) | 
| 136 | 26.8k | { | 
| 137 | 26.8k |     size_t len; | 
| 138 | 26.8k |     unsigned char *buf; | 
| 139 |  |  | 
| 140 | 26.8k |     len = EC_POINT_point2oct(group, point, form, NULL, 0, NULL); | 
| 141 | 26.8k |     if (len == 0) | 
| 142 | 114 |         return 0; | 
| 143 | 26.7k |     if ((buf = OPENSSL_malloc(len)) == NULL) { | 
| 144 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 145 | 0 |         return 0; | 
| 146 | 0 |     } | 
| 147 | 26.7k |     len = EC_POINT_point2oct(group, point, form, buf, len, ctx); | 
| 148 | 26.7k |     if (len == 0) { | 
| 149 | 0 |         OPENSSL_free(buf); | 
| 150 | 0 |         return 0; | 
| 151 | 0 |     } | 
| 152 | 26.7k |     *pbuf = buf; | 
| 153 | 26.7k |     return len; | 
| 154 | 26.7k | } |