Skip to content

Commit 384cd18

Browse files
committed
Add abstraction interface for elliptic points and elliptic diffie hellman
1 parent 829f799 commit 384cd18

File tree

4 files changed

+183
-3
lines changed

4 files changed

+183
-3
lines changed

contrib/win32/openssh/libssh.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@
262262
<ClCompile Include="$(OpenSSH-Src-Path)openssl-dh.c">
263263
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
264264
</ClCompile>
265+
<ClCompile Include="..\..\..\openssl-epoint.c" />
265266
</ItemGroup>
266267
<ItemGroup>
267268
<ClInclude Include="$(OpenSSH-Src-Path)crypto-wrap.h" />

contrib/win32/openssh/libssh.vcxproj.filters

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,18 @@
288288
<ClCompile Include="$(OpenSSH-Src-Path)xmalloc.c">
289289
<Filter>Source Files</Filter>
290290
</ClCompile>
291-
<ClCompile Include="..\..\..\openssl-dh.c">
291+
<ClCompile Include="$(OpenSSH-Src-Path)openssl-bn.c">
292292
<Filter>Source Files</Filter>
293293
</ClCompile>
294-
<ClCompile Include="..\..\..\openssl-bn.c">
294+
<ClCompile Include="$(OpenSSH-Src-Path)openssl-dh.c">
295+
<Filter>Source Files</Filter>
296+
</ClCompile>
297+
<ClCompile Include="..\..\..\openssl-epoint.c">
295298
<Filter>Source Files</Filter>
296299
</ClCompile>
297300
</ItemGroup>
298301
<ItemGroup>
299-
<ClInclude Include="..\..\..\crypto-wrap.h">
302+
<ClInclude Include="$(OpenSSH-Src-Path)crypto-wrap.h">
300303
<Filter>Header Files</Filter>
301304
</ClInclude>
302305
</ItemGroup>

crypto-wrap.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ struct sshdh;
66
struct sshbn;
77
struct sshbuf;
88
struct ssh;
9+
struct sshedh;
10+
struct sshepoint;
11+
struct sshecurve;
12+
913

1014
struct sshdh *sshdh_new(void);
1115
void sshdh_free(struct sshdh *dh);
@@ -21,6 +25,27 @@ int sshdh_new_group_hex(const char *gen, const char *modulus,
2125
struct sshdh **dhp);
2226
struct sshdh *sshdh_new_group(struct sshbn *gen, struct sshbn *modulus);
2327

28+
struct sshedh *sshedh_new(void);
29+
void sshedh_free(struct sshdh *dh);
30+
struct sshepoint *sshedh_pubkey(struct sshedh *dh);
31+
void sshedh_dump(struct sshedh *dh);
32+
size_t sshedh_shared_key_size(struct sshedh *dh);
33+
int sshedh_compute_key(struct sshedh *dh, struct sshepoint *pubkey,
34+
struct sshbn **shared_secretp);
35+
int sshedh_generate(struct sshedh *dh, size_t len);
36+
struct sshedh *sshedh_new_curve(int nid);
37+
38+
struct sshepoint * sshepoint_new(void);
39+
int sshepoint_from(struct sshbn * x, struct sshbn * y, struct sshecurve * sshecurve, struct sshepoint **retp);
40+
int sshepoint_to(struct sshepoint * pt, struct sshbn **retx, struct sshbn **rety, struct sshecurve ** retcurve);
41+
void sshepoint_free(struct sshepoint * pt);
42+
43+
struct sshecurve * sshecurve_new(void);
44+
void sshecurve_free(struct sshecurve * curve);
45+
struct sshecurve * sshecurve_new_curve(int nid);
46+
47+
48+
2449
struct sshbn *sshbn_new(void);
2550
void sshbn_free(struct sshbn *bn);
2651
int sshbn_from(const void *d, size_t l, struct sshbn **retp);

openssl-epoint.c

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Copyright (c) 2015 Damien Miller <[email protected]>
3+
*
4+
* Permission to use, copy, modify, and distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
#include <includes.h>
18+
19+
20+
#include <sys/types.h>
21+
#include <stdio.h>
22+
#include <stdlib.h>
23+
#include <string.h>
24+
#include <limits.h>
25+
26+
27+
#include <openssl/bn.h>
28+
#include <openssl/ec.h>
29+
30+
31+
#include "sshbuf.h"
32+
#include "packet.h"
33+
#include "ssherr.h"
34+
#include "crypto-wrap.h"
35+
36+
struct sshepoint {
37+
EC_POINT *pt;
38+
EC_GROUP *gp;
39+
};
40+
41+
struct sshecurve {
42+
EC_GROUP *gp;
43+
};
44+
45+
46+
struct sshepoint *
47+
sshepoint_new(void)
48+
{
49+
return malloc(sizeof(struct sshepoint));
50+
}
51+
52+
void
53+
sshepoint_free(struct sshepoint *pt)
54+
{
55+
if (pt != NULL) {
56+
if (pt->pt != NULL)
57+
EC_POINT_free(pt->pt);
58+
if (pt->gp != NULL)
59+
EC_GROUP_free(pt->gp);
60+
explicit_bzero(pt, sizeof(*pt));
61+
free(pt);
62+
}
63+
}
64+
65+
66+
int sshepoint_from(struct sshbn * x, struct sshbn * y, struct sshecurve * curve, struct sshepoint **retp)
67+
{
68+
struct sshepoint *ret = NULL;
69+
70+
71+
*retp = NULL;
72+
if ((ret = sshepoint_new()) == NULL)
73+
{
74+
return SSH_ERR_ALLOC_FAIL;
75+
}
76+
if ((ret->pt = EC_POINT_new(curve->gp)) == NULL)
77+
{
78+
sshepoint_free(ret);
79+
return SSH_ERR_LIBCRYPTO_ERROR;
80+
}
81+
ret->gp = curve->gp;
82+
if (EC_POINT_set_affine_corrdinates_GFp(curve->gp, ret->pt, x, y)) {
83+
sshepoint_free(ret);
84+
return SSH_ERR_LIBCRYPTO_ERROR;
85+
}
86+
*retp = ret;
87+
return 0;
88+
}
89+
int sshepoint_to(struct sshepoint * pt, struct sshbn **retx, struct sshbn **rety, struct sshecurve ** retcurve)
90+
{
91+
struct sshbn * x = NULL;
92+
struct sshbn * y = NULL;
93+
struct sshecurve * curve = NULL;
94+
95+
if (((x = sshbn_new()) == NULL) ||
96+
((y = sshbn_new()) == NULL) ||
97+
((curve = sshecurve_new()) == NULL))
98+
{
99+
sshbn_free(x);
100+
sshbn_free(y);
101+
sshecurve_free(curve);
102+
return SSH_ERR_ALLOC_FAIL;
103+
}
104+
105+
curve->gp = pt->gp;
106+
if (EC_POINT_get_affine_coordinates_GFp(pt->gp, pt->pt, sshbn_bignum(x), sshbn_bignum(y), NULL))
107+
{
108+
sshecurve_free(curve);
109+
sshbn_free(x);
110+
sshbn_free(y);
111+
return SSH_ERR_LIBCRYPTO_ERROR;
112+
}
113+
*retcurve = curve;
114+
*retx = x;
115+
*rety = y;
116+
117+
return 0;
118+
}
119+
120+
struct sshecurve * sshecurve_new(void)
121+
{
122+
struct sshecurve * curve = NULL;
123+
124+
curve = (struct sshecurve *)malloc(sizeof(struct sshecurve));
125+
memset(curve, 0, sizeof(struct sshecurve));
126+
127+
return curve;
128+
}
129+
130+
void sshecurve_free(struct sshecurve * curve)
131+
{
132+
if (curve != NULL) {
133+
if (curve->gp != NULL)
134+
EC_GROUP_free(curve->gp);
135+
explicit_bzero(curve, sizeof(*curve));
136+
free(curve);
137+
}
138+
}
139+
140+
struct sshecurve * sshecurve_new_curve(int nid)
141+
{
142+
struct sshecurve * ret;
143+
144+
if ((ret = sshecurve_new()) == NULL)
145+
return NULL;
146+
ret->gp = EC_GROUP_new_by_curve_name(nid);
147+
148+
return ret;
149+
150+
151+
}

0 commit comments

Comments
 (0)