aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/pubkey/ec_group
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/pubkey/ec_group')
-rw-r--r--src/lib/pubkey/ec_group/ec_group.cpp2
-rw-r--r--src/lib/pubkey/ec_group/point_gfp.cpp48
-rw-r--r--src/lib/pubkey/ec_group/point_gfp.h29
3 files changed, 42 insertions, 37 deletions
diff --git a/src/lib/pubkey/ec_group/ec_group.cpp b/src/lib/pubkey/ec_group/ec_group.cpp
index 5fb79c923..ccf1969d2 100644
--- a/src/lib/pubkey/ec_group/ec_group.cpp
+++ b/src/lib/pubkey/ec_group/ec_group.cpp
@@ -528,7 +528,7 @@ EC_Group::DER_encode(EC_Group_Encoding form) const
.encode(BigInt::encode_1363(get_b(), p_bytes),
OCTET_STRING)
.end_cons()
- .encode(EC2OSP(get_base_point(), PointGFp::UNCOMPRESSED), OCTET_STRING)
+ .encode(get_base_point().encode(PointGFp::UNCOMPRESSED), OCTET_STRING)
.encode(get_order())
.encode(get_cofactor())
.end_cons()
diff --git a/src/lib/pubkey/ec_group/point_gfp.cpp b/src/lib/pubkey/ec_group/point_gfp.cpp
index 8f1093418..7974f8d79 100644
--- a/src/lib/pubkey/ec_group/point_gfp.cpp
+++ b/src/lib/pubkey/ec_group/point_gfp.cpp
@@ -618,50 +618,42 @@ bool PointGFp::operator==(const PointGFp& other) const
}
// encoding and decoding
-secure_vector<uint8_t> EC2OSP(const PointGFp& point, uint8_t format)
+std::vector<uint8_t> PointGFp::encode(PointGFp::Compression_Type format) const
{
- if(point.is_zero())
- return secure_vector<uint8_t>(1); // single 0 byte
+ if(is_zero())
+ return std::vector<uint8_t>(1); // single 0 byte
- const size_t p_bytes = point.get_curve().get_p().bytes();
+ const size_t p_bytes = m_curve.get_p().bytes();
- BigInt x = point.get_affine_x();
- BigInt y = point.get_affine_y();
+ const BigInt x = get_affine_x();
+ const BigInt y = get_affine_y();
- secure_vector<uint8_t> bX = BigInt::encode_1363(x, p_bytes);
- secure_vector<uint8_t> bY = BigInt::encode_1363(y, p_bytes);
+ std::vector<uint8_t> result;
if(format == PointGFp::UNCOMPRESSED)
{
- secure_vector<uint8_t> result;
- result.push_back(0x04);
-
- result += bX;
- result += bY;
-
- return result;
+ result.resize(1 + 2*p_bytes);
+ result[0] = 0x04;
+ BigInt::encode_1363(&result[1], p_bytes, x);
+ BigInt::encode_1363(&result[1+p_bytes], p_bytes, y);
}
else if(format == PointGFp::COMPRESSED)
{
- secure_vector<uint8_t> result;
- result.push_back(0x02 | static_cast<uint8_t>(y.get_bit(0)));
-
- result += bX;
-
- return result;
+ result.resize(1 + p_bytes);
+ result[0] = 0x02 | static_cast<uint8_t>(y.get_bit(0));
+ BigInt::encode_1363(&result[1], p_bytes, x);
}
else if(format == PointGFp::HYBRID)
{
- secure_vector<uint8_t> result;
- result.push_back(0x06 | static_cast<uint8_t>(y.get_bit(0)));
-
- result += bX;
- result += bY;
-
- return result;
+ result.resize(1 + 2*p_bytes);
+ result[0] = 0x06 | static_cast<uint8_t>(y.get_bit(0));
+ BigInt::encode_1363(&result[1], p_bytes, x);
+ BigInt::encode_1363(&result[1+p_bytes], p_bytes, y);
}
else
throw Invalid_Argument("EC2OSP illegal point encoding");
+
+ return result;
}
namespace {
diff --git a/src/lib/pubkey/ec_group/point_gfp.h b/src/lib/pubkey/ec_group/point_gfp.h
index 6f2e34f27..81e34c634 100644
--- a/src/lib/pubkey/ec_group/point_gfp.h
+++ b/src/lib/pubkey/ec_group/point_gfp.h
@@ -99,6 +99,12 @@ class BOTAN_PUBLIC_API(2,0) PointGFp final
PointGFp(const CurveGFp& curve, const BigInt& x, const BigInt& y);
/**
+ * EC2OSP - elliptic curve to octet string primitive
+ * @param format which format to encode using
+ */
+ std::vector<uint8_t> encode(PointGFp::Compression_Type format) const;
+
+ /**
* += Operator
* @param rhs the PointGFp to add to the local value
* @result resulting PointGFp
@@ -131,12 +137,6 @@ class BOTAN_PUBLIC_API(2,0) PointGFp final
}
/**
- * Return base curve of this point
- * @result the curve over GF(p) of this point
- */
- const CurveGFp& get_curve() const { return m_curve; }
-
- /**
* get affine x coordinate
* @result affine x coordinate
*/
@@ -199,7 +199,7 @@ class BOTAN_PUBLIC_API(2,0) PointGFp final
/**
* Point addition - mixed J+A
- * @param other affine point to add
+ * @param other affine point to add - assumed to be affine!
* @param workspace temp space, at least WORKSPACE_SIZE elements
*/
void add_affine(const PointGFp& other, std::vector<BigInt>& workspace);
@@ -226,6 +226,14 @@ class BOTAN_PUBLIC_API(2,0) PointGFp final
*/
PointGFp zero() const { return PointGFp(m_curve); }
+ /**
+ * Return base curve of this point
+ * @result the curve over GF(p) of this point
+ *
+ * You should not need to use this
+ */
+ const CurveGFp& get_curve() const { return m_curve; }
+
private:
CurveGFp m_curve;
BigInt m_coord_x, m_coord_y, m_coord_z;
@@ -281,7 +289,12 @@ inline PointGFp operator*(const PointGFp& point, const BigInt& scalar)
}
// encoding and decoding
-secure_vector<uint8_t> BOTAN_PUBLIC_API(2,0) EC2OSP(const PointGFp& point, uint8_t format);
+inline secure_vector<uint8_t> BOTAN_DEPRECATED("Use PointGFp::encode")
+ EC2OSP(const PointGFp& point, uint8_t format)
+ {
+ std::vector<uint8_t> enc = point.encode(static_cast<PointGFp::Compression_Type>(format));
+ return secure_vector<uint8_t>(enc.begin(), enc.end());
+ }
PointGFp BOTAN_PUBLIC_API(2,0) OS2ECP(const uint8_t data[], size_t data_len,
const CurveGFp& curve);