1 /* $OpenBSD: bufec.c,v 1.1 2010/08/31 11:54:45 djm Exp $ */
3 * Copyright (c) 2010 Damien Miller <djm@mindrot.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #ifdef OPENSSL_HAS_ECC
22 #include <sys/types.h>
24 #include <openssl/bn.h>
25 #include <openssl/ec.h>
36 * Maximum supported EC GFp field length is 528 bits. SEC1 uncompressed
37 * encoding represents this as two bitstring points that should each
38 * be no longer than the field length, SEC1 specifies a 1 byte
40 * Being paranoid here may insulate us to parsing problems in
43 #define BUFFER_MAX_ECPOINT_LEN ((528*2 / 8) + 1)
46 * Append an EC_POINT to the buffer as a string containing a SEC1 encoded
47 * uncompressed point. Fortunately OpenSSL handles the gory details for us.
50 buffer_put_ecpoint_ret(Buffer
*buffer
, const EC_GROUP
*curve
,
51 const EC_POINT
*point
)
58 /* Determine length */
59 if ((bnctx
= BN_CTX_new()) == NULL
)
60 fatal("%s: BN_CTX_new failed", __func__
);
61 len
= EC_POINT_point2oct(curve
, point
, POINT_CONVERSION_UNCOMPRESSED
,
63 if (len
> BUFFER_MAX_ECPOINT_LEN
) {
64 error("%s: giant EC point: len = %lu (max %u)",
65 __func__
, (u_long
)len
, BUFFER_MAX_ECPOINT_LEN
);
70 if (EC_POINT_point2oct(curve
, point
, POINT_CONVERSION_UNCOMPRESSED
,
71 buf
, len
, bnctx
) != len
) {
72 error("%s: EC_POINT_point2oct length mismatch", __func__
);
76 buffer_put_string(buffer
, buf
, len
);
88 buffer_put_ecpoint(Buffer
*buffer
, const EC_GROUP
*curve
,
89 const EC_POINT
*point
)
91 if (buffer_put_ecpoint_ret(buffer
, curve
, point
) == -1)
92 fatal("%s: buffer error", __func__
);
96 buffer_get_ecpoint_ret(Buffer
*buffer
, const EC_GROUP
*curve
,
104 if ((buf
= buffer_get_string_ret(buffer
, &len
)) == NULL
) {
105 error("%s: invalid point", __func__
);
108 if ((bnctx
= BN_CTX_new()) == NULL
)
109 fatal("%s: BN_CTX_new failed", __func__
);
110 if (len
> BUFFER_MAX_ECPOINT_LEN
) {
111 error("%s: EC_POINT too long: %u > max %u", __func__
,
112 len
, BUFFER_MAX_ECPOINT_LEN
);
116 error("%s: EC_POINT buffer is empty", __func__
);
119 if (buf
[0] != POINT_CONVERSION_UNCOMPRESSED
) {
120 error("%s: EC_POINT is in an incorrect form: "
121 "0x%02x (want 0x%02x)", __func__
, buf
[0],
122 POINT_CONVERSION_UNCOMPRESSED
);
125 if (EC_POINT_oct2point(curve
, point
, buf
, len
, bnctx
) != 1) {
126 error("buffer_get_bignum2_ret: BN_bin2bn failed");
129 /* EC_POINT_oct2point verifies that the point is on the curve for us */
139 buffer_get_ecpoint(Buffer
*buffer
, const EC_GROUP
*curve
,
142 if (buffer_get_ecpoint_ret(buffer
, curve
, point
) == -1)
143 fatal("%s: buffer error", __func__
);
146 #endif /* OPENSSL_HAS_ECC */