Add abstraction interface for elliptic points and elliptic diffie hellman

This commit is contained in:
dkulwin 2016-01-25 14:52:21 -06:00
parent 829f799ad9
commit 384cd18b56
4 changed files with 183 additions and 3 deletions

View File

@ -262,6 +262,7 @@
<ClCompile Include="$(OpenSSH-Src-Path)openssl-dh.c"> <ClCompile Include="$(OpenSSH-Src-Path)openssl-dh.c">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\..\openssl-epoint.c" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="$(OpenSSH-Src-Path)crypto-wrap.h" /> <ClInclude Include="$(OpenSSH-Src-Path)crypto-wrap.h" />

View File

@ -288,15 +288,18 @@
<ClCompile Include="$(OpenSSH-Src-Path)xmalloc.c"> <ClCompile Include="$(OpenSSH-Src-Path)xmalloc.c">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\..\openssl-dh.c"> <ClCompile Include="$(OpenSSH-Src-Path)openssl-bn.c">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\..\openssl-bn.c"> <ClCompile Include="$(OpenSSH-Src-Path)openssl-dh.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\..\openssl-epoint.c">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\..\crypto-wrap.h"> <ClInclude Include="$(OpenSSH-Src-Path)crypto-wrap.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>

View File

@ -6,6 +6,10 @@ struct sshdh;
struct sshbn; struct sshbn;
struct sshbuf; struct sshbuf;
struct ssh; struct ssh;
struct sshedh;
struct sshepoint;
struct sshecurve;
struct sshdh *sshdh_new(void); struct sshdh *sshdh_new(void);
void sshdh_free(struct sshdh *dh); void sshdh_free(struct sshdh *dh);
@ -21,6 +25,27 @@ int sshdh_new_group_hex(const char *gen, const char *modulus,
struct sshdh **dhp); struct sshdh **dhp);
struct sshdh *sshdh_new_group(struct sshbn *gen, struct sshbn *modulus); struct sshdh *sshdh_new_group(struct sshbn *gen, struct sshbn *modulus);
struct sshedh *sshedh_new(void);
void sshedh_free(struct sshdh *dh);
struct sshepoint *sshedh_pubkey(struct sshedh *dh);
void sshedh_dump(struct sshedh *dh);
size_t sshedh_shared_key_size(struct sshedh *dh);
int sshedh_compute_key(struct sshedh *dh, struct sshepoint *pubkey,
struct sshbn **shared_secretp);
int sshedh_generate(struct sshedh *dh, size_t len);
struct sshedh *sshedh_new_curve(int nid);
struct sshepoint * sshepoint_new(void);
int sshepoint_from(struct sshbn * x, struct sshbn * y, struct sshecurve * sshecurve, struct sshepoint **retp);
int sshepoint_to(struct sshepoint * pt, struct sshbn **retx, struct sshbn **rety, struct sshecurve ** retcurve);
void sshepoint_free(struct sshepoint * pt);
struct sshecurve * sshecurve_new(void);
void sshecurve_free(struct sshecurve * curve);
struct sshecurve * sshecurve_new_curve(int nid);
struct sshbn *sshbn_new(void); struct sshbn *sshbn_new(void);
void sshbn_free(struct sshbn *bn); void sshbn_free(struct sshbn *bn);
int sshbn_from(const void *d, size_t l, struct sshbn **retp); int sshbn_from(const void *d, size_t l, struct sshbn **retp);

151
openssl-epoint.c Normal file
View File

@ -0,0 +1,151 @@
/*
* Copyright (c) 2015 Damien Miller <djm@mindrot.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <includes.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <openssl/bn.h>
#include <openssl/ec.h>
#include "sshbuf.h"
#include "packet.h"
#include "ssherr.h"
#include "crypto-wrap.h"
struct sshepoint {
EC_POINT *pt;
EC_GROUP *gp;
};
struct sshecurve {
EC_GROUP *gp;
};
struct sshepoint *
sshepoint_new(void)
{
return malloc(sizeof(struct sshepoint));
}
void
sshepoint_free(struct sshepoint *pt)
{
if (pt != NULL) {
if (pt->pt != NULL)
EC_POINT_free(pt->pt);
if (pt->gp != NULL)
EC_GROUP_free(pt->gp);
explicit_bzero(pt, sizeof(*pt));
free(pt);
}
}
int sshepoint_from(struct sshbn * x, struct sshbn * y, struct sshecurve * curve, struct sshepoint **retp)
{
struct sshepoint *ret = NULL;
*retp = NULL;
if ((ret = sshepoint_new()) == NULL)
{
return SSH_ERR_ALLOC_FAIL;
}
if ((ret->pt = EC_POINT_new(curve->gp)) == NULL)
{
sshepoint_free(ret);
return SSH_ERR_LIBCRYPTO_ERROR;
}
ret->gp = curve->gp;
if (EC_POINT_set_affine_corrdinates_GFp(curve->gp, ret->pt, x, y)) {
sshepoint_free(ret);
return SSH_ERR_LIBCRYPTO_ERROR;
}
*retp = ret;
return 0;
}
int sshepoint_to(struct sshepoint * pt, struct sshbn **retx, struct sshbn **rety, struct sshecurve ** retcurve)
{
struct sshbn * x = NULL;
struct sshbn * y = NULL;
struct sshecurve * curve = NULL;
if (((x = sshbn_new()) == NULL) ||
((y = sshbn_new()) == NULL) ||
((curve = sshecurve_new()) == NULL))
{
sshbn_free(x);
sshbn_free(y);
sshecurve_free(curve);
return SSH_ERR_ALLOC_FAIL;
}
curve->gp = pt->gp;
if (EC_POINT_get_affine_coordinates_GFp(pt->gp, pt->pt, sshbn_bignum(x), sshbn_bignum(y), NULL))
{
sshecurve_free(curve);
sshbn_free(x);
sshbn_free(y);
return SSH_ERR_LIBCRYPTO_ERROR;
}
*retcurve = curve;
*retx = x;
*rety = y;
return 0;
}
struct sshecurve * sshecurve_new(void)
{
struct sshecurve * curve = NULL;
curve = (struct sshecurve *)malloc(sizeof(struct sshecurve));
memset(curve, 0, sizeof(struct sshecurve));
return curve;
}
void sshecurve_free(struct sshecurve * curve)
{
if (curve != NULL) {
if (curve->gp != NULL)
EC_GROUP_free(curve->gp);
explicit_bzero(curve, sizeof(*curve));
free(curve);
}
}
struct sshecurve * sshecurve_new_curve(int nid)
{
struct sshecurve * ret;
if ((ret = sshecurve_new()) == NULL)
return NULL;
ret->gp = EC_GROUP_new_by_curve_name(nid);
return ret;
}