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