docker: source checking container
[gnuk/gnuk.git] / src / ec_p256r1.c
1 /*                                                    -*- coding: utf-8 -*-
2  * ec_p256r1.c - Elliptic curve over GF(p256r1)
3  *
4  * Copyright (C) 2014 Free Software Initiative of Japan
5  * Author: NIIBE Yutaka <gniibe@fsij.org>
6  *
7  * This file is a part of Gnuk, a GnuPG USB Token implementation.
8  *
9  * Gnuk is free software: you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * Gnuk is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include <stdint.h>
25 #include <string.h>
26 #include "bn.h"
27 #include "modp256r1.h"
28 #include "affine.h"
29 #include "jpc-ac_p256r1.h"
30 #include "mod.h"
31 #include "ec_p256r1.h"
32
33 #define FIELD p256r1
34 #define COEFFICIENT_A_IS_MINUS_3 1
35
36 /*
37  * a = -3 mod p256r1
38  */
39 static const bn256 coefficient_a[1] = {
40   {{  0xfffffffc, 0xffffffff, 0xffffffff, 0x00000000,
41       0x00000000, 0x00000000, 0x00000001, 0xffffffff }}
42 };
43
44 static const bn256 coefficient_b[1] = {
45   {{ 0x27d2604b, 0x3bce3c3e, 0xcc53b0f6, 0x651d06b0,
46      0x769886bc, 0xb3ebbd55, 0xaa3a93e7, 0x5ac635d8 }}
47 };
48
49
50 static const ac precomputed_KG[15] = {
51   {
52     {{{ 0xd898c296, 0xf4a13945, 0x2deb33a0, 0x77037d81,
53         0x63a440f2, 0xf8bce6e5, 0xe12c4247, 0x6b17d1f2 }}},
54     {{{ 0x37bf51f5, 0xcbb64068, 0x6b315ece, 0x2bce3357,
55         0x7c0f9e16, 0x8ee7eb4a, 0xfe1a7f9b, 0x4fe342e2 }}}
56   }, {
57     {{{ 0x8e14db63, 0x90e75cb4, 0xad651f7e, 0x29493baa,
58         0x326e25de, 0x8492592e, 0x2811aaa5, 0x0fa822bc }}},
59     {{{ 0x5f462ee7, 0xe4112454, 0x50fe82f5, 0x34b1a650,
60         0xb3df188b, 0x6f4ad4bc, 0xf5dba80d, 0xbff44ae8 }}}
61   }, {
62     {{{ 0x097992af, 0x93391ce2, 0x0d35f1fa, 0xe96c98fd,
63         0x95e02789, 0xb257c0de, 0x89d6726f, 0x300a4bbc }}},
64     {{{ 0xc08127a0, 0xaa54a291, 0xa9d806a5, 0x5bb1eead,
65         0xff1e3c6f, 0x7f1ddb25, 0xd09b4644, 0x72aac7e0 }}}
66   }, {
67     {{{ 0xd789bd85, 0x57c84fc9, 0xc297eac3, 0xfc35ff7d,
68         0x88c6766e, 0xfb982fd5, 0xeedb5e67, 0x447d739b }}},
69     {{{ 0x72e25b32, 0x0c7e33c9, 0xa7fae500, 0x3d349b95,
70         0x3a4aaff7, 0xe12e9d95, 0x834131ee, 0x2d4825ab }}}
71   }, {
72     {{{ 0x2a1d367f, 0x13949c93, 0x1a0a11b7, 0xef7fbd2b,
73         0xb91dfc60, 0xddc6068b, 0x8a9c72ff, 0xef951932 }}},
74     {{{ 0x7376d8a8, 0x196035a7, 0x95ca1740, 0x23183b08,
75         0x022c219c, 0xc1ee9807, 0x7dbb2c9b, 0x611e9fc3 }}}
76   }, {
77     {{{ 0x0b57f4bc, 0xcae2b192, 0xc6c9bc36, 0x2936df5e,
78         0xe11238bf, 0x7dea6482, 0x7b51f5d8, 0x55066379 }}},
79     {{{ 0x348a964c, 0x44ffe216, 0xdbdefbe1, 0x9fb3d576,
80         0x8d9d50e5, 0x0afa4001, 0x8aecb851, 0x15716484 }}}
81   }, {
82     {{{ 0xfc5cde01, 0xe48ecaff, 0x0d715f26, 0x7ccd84e7,
83         0xf43e4391, 0xa2e8f483, 0xb21141ea, 0xeb5d7745 }}},
84     {{{ 0x731a3479, 0xcac917e2, 0x2844b645, 0x85f22cfe,
85         0x58006cee, 0x0990e6a1, 0xdbecc17b, 0xeafd72eb }}}
86   }, {
87     {{{ 0x313728be, 0x6cf20ffb, 0xa3c6b94a, 0x96439591,
88         0x44315fc5, 0x2736ff83, 0xa7849276, 0xa6d39677 }}},
89     {{{ 0xc357f5f4, 0xf2bab833, 0x2284059b, 0x824a920c,
90         0x2d27ecdf, 0x66b8babd, 0x9b0b8816, 0x674f8474 }}}
91   }, {
92     {{{ 0x677c8a3e, 0x2df48c04, 0x0203a56b, 0x74e02f08,
93         0xb8c7fedb, 0x31855f7d, 0x72c9ddad, 0x4e769e76 }}},
94     {{{ 0xb824bbb0, 0xa4c36165, 0x3b9122a5, 0xfb9ae16f,
95         0x06947281, 0x1ec00572, 0xde830663, 0x42b99082 }}}
96   }, {
97     {{{ 0xdda868b9, 0x6ef95150, 0x9c0ce131, 0xd1f89e79,
98         0x08a1c478, 0x7fdc1ca0, 0x1c6ce04d, 0x78878ef6 }}},
99     {{{ 0x1fe0d976, 0x9c62b912, 0xbde08d4f, 0x6ace570e,
100         0x12309def, 0xde53142c, 0x7b72c321, 0xb6cb3f5d }}}
101   }, {
102     {{{ 0xc31a3573, 0x7f991ed2, 0xd54fb496, 0x5b82dd5b,
103         0x812ffcae, 0x595c5220, 0x716b1287, 0x0c88bc4d }}},
104     {{{ 0x5f48aca8, 0x3a57bf63, 0xdf2564f3, 0x7c8181f4,
105         0x9c04e6aa, 0x18d1b5b3, 0xf3901dc6, 0xdd5ddea3 }}}
106   }, {
107     {{{ 0x3e72ad0c, 0xe96a79fb, 0x42ba792f, 0x43a0a28c,
108         0x083e49f3, 0xefe0a423, 0x6b317466, 0x68f344af }}},
109     {{{ 0x3fb24d4a, 0xcdfe17db, 0x71f5c626, 0x668bfc22,
110         0x24d67ff3, 0x604ed93c, 0xf8540a20, 0x31b9c405 }}}
111   }, {
112     {{{ 0xa2582e7f, 0xd36b4789, 0x4ec39c28, 0xd1a1014,
113         0xedbad7a0, 0x663c62c3, 0x6f461db9, 0x4052bf4b }}},
114     {{{ 0x188d25eb, 0x235a27c3, 0x99bfcc5b, 0xe724f339,
115         0x71d70cc8, 0x862be6bd, 0x90b0fc61, 0xfecf4d51 }}}
116   }, {
117     {{{ 0xa1d4cfac, 0x74346c10, 0x8526a7a4, 0xafdf5cc0,
118         0xf62bff7a, 0x123202a8, 0xc802e41a, 0x1eddbae2 }}},
119     {{{ 0xd603f844, 0x8fa0af2d, 0x4c701917, 0x36e06b7e,
120         0x73db33a0, 0x0c45f452, 0x560ebcfc, 0x43104d86 }}}
121   }, {
122     {{{ 0x0d1d78e5, 0x9615b511, 0x25c4744b, 0x66b0de32,
123         0x6aaf363a, 0x0a4a46fb, 0x84f7a21c, 0xb48e26b4 }}},
124     {{{ 0x21a01b2d, 0x06ebb0f6, 0x8b7b0f98, 0xc004e404,
125         0xfed6f668, 0x64131bcd, 0x4d4d3dab, 0xfac01540 }}}
126   }
127 };
128
129 static const ac precomputed_2E_KG[15] = {
130   {
131     {{{ 0x185a5943, 0x3a5a9e22, 0x5c65dfb6, 0x1ab91936,
132         0x262c71da, 0x21656b32, 0xaf22af89, 0x7fe36b40 }}},
133     {{{ 0x699ca101, 0xd50d152c, 0x7b8af212, 0x74b3d586,
134         0x07dca6f1, 0x9f09f404, 0x25b63624, 0xe697d458 }}}
135   }, {
136     {{{ 0x7512218e, 0xa84aa939, 0x74ca0141, 0xe9a521b0,
137         0x18a2e902, 0x57880b3a, 0x12a677a6, 0x4a5b5066 }}},
138     {{{ 0x4c4f3840, 0x0beada7a, 0x19e26d9d, 0x626db154,
139         0xe1627d40, 0xc42604fb, 0xeac089f1, 0xeb13461c }}}
140   }, {
141     {{{ 0x27a43281, 0xf9faed09, 0x4103ecbc, 0x5e52c414,
142         0xa815c857, 0xc342967a, 0x1c6a220a, 0x0781b829 }}},
143     {{{ 0xeac55f80, 0x5a8343ce, 0xe54a05e3, 0x88f80eee,
144         0x12916434, 0x97b2a14f, 0xf0151593, 0x690cde8d }}}
145   }, {
146     {{{ 0xf7f82f2a, 0xaee9c75d, 0x4afdf43a, 0x9e4c3587,
147         0x37371326, 0xf5622df4, 0x6ec73617, 0x8a535f56 }}},
148     {{{ 0x223094b7, 0xc5f9a0ac, 0x4c8c7669, 0xcde53386,
149         0x085a92bf, 0x37e02819, 0x68b08bd7, 0x0455c084 }}}
150   }, {
151     {{{ 0x9477b5d9, 0x0c0a6e2c, 0x876dc444, 0xf9a4bf62,
152         0xb6cdc279, 0x5050a949, 0xb77f8276, 0x06bada7a }}},
153     {{{ 0xea48dac9, 0xc8b4aed1, 0x7ea1070f, 0xdebd8a4b,
154         0x1366eb70, 0x427d4910, 0x0e6cb18a, 0x5b476dfd }}}
155   }, {
156     {{{ 0x278c340a, 0x7c5c3e44, 0x12d66f3b, 0x4d546068,
157         0xae23c5d8, 0x29a751b1, 0x8a2ec908, 0x3e29864e }}},
158     {{{ 0x26dbb850, 0x142d2a66, 0x765bd780, 0xad1744c4,
159         0xe322d1ed, 0x1f150e68, 0x3dc31e7e, 0x239b90ea }}}
160   }, {
161     {{{ 0x7a53322a, 0x78c41652, 0x09776f8e, 0x305dde67,
162         0xf8862ed4, 0xdbcab759, 0x49f72ff7, 0x820f4dd9 }}},
163     {{{ 0x2b5debd4, 0x6cc544a6, 0x7b4e8cc4, 0x75be5d93,
164         0x215c14d3, 0x1b481b1b, 0x783a05ec, 0x140406ec }}}
165   }, {
166     {{{ 0xe895df07, 0x6a703f10, 0x01876bd8, 0xfd75f3fa,
167         0x0ce08ffe, 0xeb5b06e7, 0x2783dfee, 0x68f6b854 }}},
168     {{{ 0x78712655, 0x90c76f8a, 0xf310bf7f, 0xcf5293d2,
169         0xfda45028, 0xfbc8044d, 0x92e40ce6, 0xcbe1feba }}}
170   }, {
171     {{{ 0x4396e4c1, 0xe998ceea, 0x6acea274, 0xfc82ef0b,
172         0x2250e927, 0x230f729f, 0x2f420109, 0xd0b2f94d }}},
173     {{{ 0xb38d4966, 0x4305addd, 0x624c3b45, 0x10b838f8,
174         0x58954e7a, 0x7db26366, 0x8b0719e5, 0x97145982 }}}
175   }, {
176     {{{ 0x23369fc9, 0x4bd6b726, 0x53d0b876, 0x57f2929e,
177         0xf2340687, 0xc2d5cba4, 0x4a866aba, 0x96161000 }}},
178     {{{ 0x2e407a5e, 0x49997bcd, 0x92ddcb24, 0x69ab197d,
179         0x8fe5131c, 0x2cf1f243, 0xcee75e44, 0x7acb9fad }}}
180   }, {
181     {{{ 0x23d2d4c0, 0x254e8394, 0x7aea685b, 0xf57f0c91,
182         0x6f75aaea, 0xa60d880f, 0xa333bf5b, 0x24eb9acc }}},
183     {{{ 0x1cda5dea, 0xe3de4ccb, 0xc51a6b4f, 0xfeef9341,
184         0x8bac4c4d, 0x743125f8, 0xacd079cc, 0x69f891c5 }}}
185   }, {
186     {{{ 0x702476b5, 0xeee44b35, 0xe45c2258, 0x7ed031a0,
187         0xbd6f8514, 0xb422d1e7, 0x5972a107, 0xe51f547c }}},
188     {{{ 0xc9cf343d, 0xa25bcd6f, 0x097c184e, 0x8ca922ee,
189         0xa9fe9a06, 0xa62f98b3, 0x25bb1387, 0x1c309a2b }}}
190   }, {
191     {{{ 0x1967c459, 0x9295dbeb, 0x3472c98e, 0xb0014883,
192         0x08011828, 0xc5049777, 0xa2c4e503, 0x20b87b8a }}},
193     {{{ 0xe057c277, 0x3063175d, 0x8fe582dd, 0x1bd53933,
194         0x5f69a044, 0x0d11adef, 0x919776be, 0xf5c6fa49 }}}
195   }, {
196     {{{ 0x0fd59e11, 0x8c944e76, 0x102fad5f, 0x3876cba1,
197         0xd83faa56, 0xa454c3fa, 0x332010b9, 0x1ed7d1b9 }}},
198     {{{ 0x0024b889, 0xa1011a27, 0xac0cd344, 0x05e4d0dc,
199         0xeb6a2a24, 0x52b520f0, 0x3217257a, 0x3a2b03f0 }}}
200   }, {
201     {{{ 0xdf1d043d, 0xf20fc2af, 0xb58d5a62, 0xf330240d,
202         0xa0058c3b, 0xfc7d229c, 0xc78dd9f6, 0x15fee545 }}},
203     {{{ 0x5bc98cda, 0x501e8288, 0xd046ac04, 0x41ef80e5,
204         0x461210fb, 0x557d9f49, 0xb8753f81, 0x4ab5b6b2 }}}
205   }
206 };
207
208 /*
209  * N: order of G
210  */
211 static const bn256 N[1] = {
212   {{ 0xfc632551, 0xf3b9cac2, 0xa7179e84, 0xbce6faad,
213      0xffffffff, 0xffffffff, 0x00000000, 0xffffffff }}
214 };
215
216 /*
217  * MU = 2^512 / N
218  * MU = ( (1 << 256) | MU_lower )
219  */
220 static const bn256 MU_lower[1] = {
221   {{ 0xeedf9bfe, 0x012ffd85, 0xdf1a6c21, 0x43190552,
222      0xffffffff, 0xfffffffe, 0xffffffff, 0x00000000 }}
223 };
224
225
226 #include "ecc.c"