1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
/* The MIT License (MIT)
*
* Copyright (c) 2015 mehdi sotoodeh
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "curve25519_mehdi.h"
/* Trim private key */
void ecp_TrimSecretKey(U8 *X)
{
X[0] &= 0xf8;
X[31] = (X[31] | 0x40) & 0x7f;
}
/* Convert big-endian byte array to little-endian byte array and vice versa */
U8* ecp_ReverseByteOrder(OUT U8 *Y, IN const U8 *X)
{
int i;
for (i = 0; i < 32; i++) Y[i] = X[31-i];
return Y;
}
/* Convert little-endian byte array to little-endian word array */
U32* ecp_BytesToWords(OUT U32 *Y, IN const U8 *X)
{
int i;
M32 m;
for (i = 0; i < 8; i++)
{
m.u8.b0 = *X++;
m.u8.b1 = *X++;
m.u8.b2 = *X++;
m.u8.b3 = *X++;
Y[i] = m.u32;
}
return Y;
}
/* Convert little-endian word array to little-endian byte array */
U8* ecp_WordsToBytes(OUT U8 *Y, IN const U32 *X)
{
int i;
M32 m;
for (i = 0; i < 32;)
{
m.u32 = *X++;
Y[i++] = m.u8.b0;
Y[i++] = m.u8.b1;
Y[i++] = m.u8.b2;
Y[i++] = m.u8.b3;
}
return Y;
}
U8* ecp_EncodeInt(OUT U8 *Y, IN const U32 *X, IN U8 parity)
{
int i;
M32 m;
for (i = 0; i < 28;)
{
m.u32 = *X++;
Y[i++] = m.u8.b0;
Y[i++] = m.u8.b1;
Y[i++] = m.u8.b2;
Y[i++] = m.u8.b3;
}
m.u32 = *X;
Y[28] = m.u8.b0;
Y[29] = m.u8.b1;
Y[30] = m.u8.b2;
Y[31] = (U8)((m.u8.b3 & 0x7f) | (parity << 7));
return Y;
}
U8 ecp_DecodeInt(OUT U32 *Y, IN const U8 *X)
{
int i;
M32 m;
for (i = 0; i < 7; i++)
{
m.u8.b0 = *X++;
m.u8.b1 = *X++;
m.u8.b2 = *X++;
m.u8.b3 = *X++;
Y[i] = m.u32;
}
m.u8.b0 = *X++;
m.u8.b1 = *X++;
m.u8.b2 = *X++;
m.u8.b3 = *X & 0x7f;
Y[7] = m.u32;
return (U8)((*X >> 7) & 1);
}
void ecp_4Folds(U8* Y, const U32* X)
{
int i, j;
U8 a, b;
for (i = 32; i-- > 0; Y++)
{
a = 0;
b = 0;
for (j = 8; j > 1;)
{
j -= 2;
a = (a << 1) + ((X[j+1] >> i) & 1);
b = (b << 1) + ((X[j] >> i) & 1);
}
Y[0] = a;
Y[32] = b;
}
}
void ecp_8Folds(U8* Y, const U32* X)
{
int i, j;
U8 a = 0;
for (i = 32; i-- > 0;)
{
for (j = 8; j-- > 0;) a = (a << 1) + ((X[j] >> i) & 1);
*Y++ = a;
}
}
|