blob: c77d1ecb98eeea77a29a9dc7fd6909085285cba5 [file] [log] [blame]
Damien Millereb8b60e2010-08-31 22:41:14 +10001/* $OpenBSD: bufec.c,v 1.1 2010/08/31 11:54:45 djm Exp $ */
2/*
3 * Copyright (c) 2010 Damien Miller <djm@mindrot.org>
4 *
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.
8 *
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.
16 */
Damien Millerc79ff072010-08-31 22:50:48 +100017
18#include "includes.h"
19
Damien Millereb8b60e2010-08-31 22:41:14 +100020#include <sys/types.h>
21
22#include <openssl/bn.h>
23#include <openssl/ec.h>
24
25#include <string.h>
26#include <stdarg.h>
27
28#include "xmalloc.h"
29#include "buffer.h"
30#include "log.h"
31#include "misc.h"
32
33/*
34 * Maximum supported EC GFp field length is 528 bits. SEC1 uncompressed
35 * encoding represents this as two bitstring points that should each
36 * be no longer than the field length, SEC1 specifies a 1 byte
37 * point type header.
38 * Being paranoid here may insulate us to parsing problems in
39 * EC_POINT_oct2point.
40 */
41#define BUFFER_MAX_ECPOINT_LEN ((528*2 / 8) + 1)
42
43/*
44 * Append an EC_POINT to the buffer as a string containing a SEC1 encoded
45 * uncompressed point. Fortunately OpenSSL handles the gory details for us.
46 */
47int
48buffer_put_ecpoint_ret(Buffer *buffer, const EC_GROUP *curve,
49 const EC_POINT *point)
50{
51 u_char *buf = NULL;
52 size_t len;
53 BN_CTX *bnctx;
54 int ret = -1;
55
56 /* Determine length */
57 if ((bnctx = BN_CTX_new()) == NULL)
58 fatal("%s: BN_CTX_new failed", __func__);
59 len = EC_POINT_point2oct(curve, point, POINT_CONVERSION_UNCOMPRESSED,
60 NULL, 0, bnctx);
61 if (len > BUFFER_MAX_ECPOINT_LEN) {
62 error("%s: giant EC point: len = %lu (max %u)",
63 __func__, (u_long)len, BUFFER_MAX_ECPOINT_LEN);
64 goto out;
65 }
66 /* Convert */
67 buf = xmalloc(len);
68 if (EC_POINT_point2oct(curve, point, POINT_CONVERSION_UNCOMPRESSED,
69 buf, len, bnctx) != len) {
70 error("%s: EC_POINT_point2oct length mismatch", __func__);
71 goto out;
72 }
73 /* Append */
74 buffer_put_string(buffer, buf, len);
75 ret = 0;
76 out:
77 if (buf != NULL) {
78 bzero(buf, len);
79 xfree(buf);
80 }
81 BN_CTX_free(bnctx);
82 return ret;
83}
84
85void
86buffer_put_ecpoint(Buffer *buffer, const EC_GROUP *curve,
87 const EC_POINT *point)
88{
89 if (buffer_put_ecpoint_ret(buffer, curve, point) == -1)
90 fatal("%s: buffer error", __func__);
91}
92
93int
94buffer_get_ecpoint_ret(Buffer *buffer, const EC_GROUP *curve,
95 EC_POINT *point)
96{
97 u_char *buf;
98 u_int len;
99 BN_CTX *bnctx;
100 int ret = -1;
101
102 if ((buf = buffer_get_string_ret(buffer, &len)) == NULL) {
103 error("%s: invalid point", __func__);
104 return -1;
105 }
106 if ((bnctx = BN_CTX_new()) == NULL)
107 fatal("%s: BN_CTX_new failed", __func__);
108 if (len > BUFFER_MAX_ECPOINT_LEN) {
109 error("%s: EC_POINT too long: %u > max %u", __func__,
110 len, BUFFER_MAX_ECPOINT_LEN);
111 goto out;
112 }
113 if (len == 0) {
114 error("%s: EC_POINT buffer is empty", __func__);
115 goto out;
116 }
117 if (buf[0] != POINT_CONVERSION_UNCOMPRESSED) {
118 error("%s: EC_POINT is in an incorrect form: "
119 "0x%02x (want 0x%02x)", __func__, buf[0],
120 POINT_CONVERSION_UNCOMPRESSED);
121 goto out;
122 }
123 if (EC_POINT_oct2point(curve, point, buf, len, bnctx) != 1) {
124 error("buffer_get_bignum2_ret: BN_bin2bn failed");
125 goto out;
126 }
127 /* EC_POINT_oct2point verifies that the point is on the curve for us */
128 ret = 0;
129 out:
130 BN_CTX_free(bnctx);
131 bzero(buf, len);
132 xfree(buf);
133 return ret;
134}
135
136void
137buffer_get_ecpoint(Buffer *buffer, const EC_GROUP *curve,
138 EC_POINT *point)
139{
140 if (buffer_get_ecpoint_ret(buffer, curve, point) == -1)
141 fatal("%s: buffer error", __func__);
142}
143