summaryrefslogtreecommitdiffstats
path: root/src/ec25519_secret.c
blob: 7f3d987729ec325d4c788f355fb307a8c9948943 (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
  Copyright (c) 2012, Matthias Schiffer <mschiffer@universe-factory.net>
  Partly based on public domain code by Matthew Dempsky and D. J. Bernstein.
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
  Simple finite field operations on the prime field F_q for
  q = 2^252 + 27742317777372353535851937790883648493, which
  is the order of the base point used for ec25519
*/

#include <libuecc/ecc.h>


#define IS_NEGATIVE(n) ((int)((((unsigned)n) >> (8*sizeof(n)-1))&1))
#define ASR(n,s) (((n) >> s)|(IS_NEGATIVE(n)*((unsigned)-1) << (8*sizeof(n)-s)))


static const unsigned char q[32] = {
	0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
	0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
};


static void select(unsigned char out[32], const unsigned char r[32], const unsigned char s[32], unsigned int b) {
	unsigned int j;
	unsigned int t;
	unsigned int bminus1;

	bminus1 = b - 1;
	for (j = 0;j < 32;++j) {
		t = bminus1 & (r[j] ^ s[j]);
		out[j] = s[j] ^ t;
	}
}

int ecc_25519_secret_is_zero(const ecc_secret_key_256 *in) {
	int i;
	ecc_secret_key_256 r;
	unsigned int bits;

	ecc_25519_secret_reduce(&r, in);

	for (i = 0; i < 32; i++)
		bits |= r.s[i];

	return (((bits-1)>>8) & 1);
}

void ecc_25519_secret_add(ecc_secret_key_256 *out, const ecc_secret_key_256 *in1, const ecc_secret_key_256 *in2) {
	unsigned int j;
	unsigned int u;
	int nq = 1 - (in1->s[31]>>4) - (in2->s[31]>>4);

	u = 0;
	for (j = 0; j < 32; ++j) {
		u += in1->s[j] + in2->s[j] + nq*q[j];

		out->s[j] = u;
		u = ASR(u, 8);
	}
}

void ecc_25519_secret_sub(ecc_secret_key_256 *out, const ecc_secret_key_256 *in1, const ecc_secret_key_256 *in2) {
	unsigned int j;
	unsigned int u;
	int nq = 8 - (in1->s[31]>>4) + (in2->s[31]>>4);

	u = 0;
	for (j = 0; j < 32; ++j) {
		u += in1->s[j] - in2->s[j] + nq*q[j];

		out->s[j] = u;
		u = ASR(u, 8);
	}
}

static void reduce(unsigned char a[32]) {
	unsigned int j;
	unsigned int nq = a[31] >> 4;
	unsigned int u1, u2;
	unsigned char out1[32], out2[32];

	u1 = u2 = 0;
	for (j = 0; j < 31; ++j) {
		u1 += a[j] - nq*q[j];
		u2 += a[j] - (nq-1)*q[j];

		out1[j] = u1; out2[j] = u2;
		u1 = ASR(u1, 8);
		u2 = ASR(u2, 8);
	}
	u1 += a[31] - nq*q[31];
	u2 += a[31] - (nq-1)*q[31];
	out1[31] = u1; out2[31] = u2;

	select(a, out1, out2, IS_NEGATIVE(u1));
}

void ecc_25519_secret_reduce(ecc_secret_key_256 *out, const ecc_secret_key_256 *in) {
	int i;

	for (i = 0; i < 32; i++)
		out->s[i] = in->s[i];

	reduce(out->s);
}

/* Montgomery modular multiplication algorithm */
static void montgomery(unsigned char out[32], const unsigned char a[32], const unsigned char b[32]) {
	unsigned int i, j;
	unsigned int nq;
	unsigned int u;

	for (i = 0; i < 32; i++)
		out[i] = 0;

	for (i = 0; i < 32; i++) {
		u = out[0] + a[i]*b[0];
		nq = (u*27) & 255;
		u += nq*q[0];

		for (j = 1; j < 32; ++j) {
			u += (out[j] + a[i]*b[j] + nq*q[j]) << 8;
			u >>= 8;
			out[j-1] = u;
		}

		out[31] = u >> 8;
	}
}


void ecc_25519_secret_mult(ecc_secret_key_256 *out, const ecc_secret_key_256 *in1, const ecc_secret_key_256 *in2) {
	/* 2^512 mod q */
	static const unsigned char C[32] = {
		0x01, 0x0f, 0x9c, 0x44, 0xe3, 0x11, 0x06, 0xa4,
		0x47, 0x93, 0x85, 0x68, 0xa7, 0x1b, 0x0e, 0xd0,
		0x65, 0xbe, 0xf5, 0x17, 0xd2, 0x73, 0xec, 0xce,
		0x3d, 0x9a, 0x30, 0x7c, 0x1b, 0x41, 0x99, 0x03
	};

	unsigned char B[32];
	unsigned char R[32];
	unsigned int i;

	for (i = 0; i < 32; i++)
		B[i] = in2->s[i];

	reduce(B);

	montgomery(R, in1->s, B);
	montgomery(out->s, R, C);
}

void ecc_25519_secret_sanitize(ecc_secret_key_256 *out, const ecc_secret_key_256 *in) {
	int i;

	for (i = 0; i < 32; i++)
		out->s[i] = in->s[i];

	out->s[0] &= 0xf8;
	out->s[31] &= 0x7f;
	out->s[31] |= 0x40;
}