3 Copyright (C) 2014 Niels Möller
5 This file is part of GNU Nettle.
7 GNU Nettle is free software: you can redistribute it and/or
8 modify it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free
17 Software Foundation; either version 2 of the License, or (at your
18 option) any later version.
20 or both in parallel, as here.
22 GNU Nettle is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received copies of the GNU General Public License and
28 the GNU Lesser General Public License along with this program. If
29 not, see http://www.gnu.org/licenses/.
32 #include "testutils.h"
35 set_point (struct ecc_point *p,
36 const char *x, const char *y)
39 mpz_init_set_str (X, x, 0);
40 mpz_init_set_str (Y, y, 0);
41 if (!ecc_point_set (p, X, Y))
42 die ("Test point not on curve!\n");
49 set_scalar (struct ecc_scalar *s,
53 mpz_init_set_str (X, x, 0);
54 ecc_scalar_set (s, X);
59 check_point (const char *name, const char *label,
60 const struct ecc_point *P,
61 const struct ecc_point *R)
70 ecc_point_get (P, px, py);
71 ecc_point_get (R, rx, ry);
73 /* FIXME: Should have a public point compare function */
74 if (mpz_cmp (px, rx) != 0 || mpz_cmp (py, ry) != 0)
76 fprintf (stderr, "Failed %s %s\np_x = ", name, label);
77 mpz_out_str (stderr, 10, px);
78 fprintf (stderr, "\nr_x = ");
79 mpz_out_str (stderr, 10, rx);
80 fprintf (stderr, " (expected)\np_y = ");
81 mpz_out_str (stderr, 10, py);
82 fprintf (stderr, "\nr_y = ");
83 mpz_out_str (stderr, 10, ry);
84 fprintf (stderr, " (expected)\n");
94 test_dh (const char *name, const struct ecc_curve *ecc,
95 const char *a_priv, const char *ax, const char *ay,
96 const char *b_priv, const char *bx, const char *by,
97 const char *sx, const char *sy)
99 struct ecc_point A, B, S, T;
100 struct ecc_scalar A_priv, B_priv;
102 ecc_scalar_init (&A_priv, ecc);
103 set_scalar (&A_priv, a_priv);
104 ecc_point_init (&A, ecc);
105 set_point (&A, ax, ay);
107 ecc_scalar_init (&B_priv, ecc);
108 set_scalar (&B_priv, b_priv);
109 ecc_point_init (&B, ecc);
110 set_point (&B, bx, by);
112 ecc_point_init (&S, ecc);
113 set_point (&S, sx, sy);
115 ecc_point_init (&T, ecc);
117 ecc_point_mul_g (&T, &A_priv);
118 check_point (name, "a g", &T, &A);
120 ecc_point_mul (&T, &B_priv, &T);
121 check_point (name, "b (a g)", &T, &S);
123 ecc_point_mul_g (&T, &B_priv);
124 check_point (name, "b g", &T, &B);
126 ecc_point_mul (&T, &A_priv, &T);
127 check_point (name, "a (b g)", &T, &S);
129 ecc_scalar_clear (&A_priv);
130 ecc_scalar_clear (&B_priv);
132 ecc_point_clear (&A);
133 ecc_point_clear (&B);
134 ecc_point_clear (&S);
135 ecc_point_clear (&T);
141 test_dh ("secp-192r1", &nettle_secp_192r1,
142 "3406157206141798348095184987208239421004566462391397236532",
143 "1050363442265225480786760666329560655512990381040021438562",
144 "5298249600854377235107392014200406283816103564916230704184",
145 "738368960171459956677260317271477822683777845013274506165",
146 "2585840779771604687467445319428618542927556223024046979917",
147 "293088185788565313717816218507714888251468410990708684573",
148 "149293809021051532782730990145509724807636529827149481690",
149 "2891131861147398318714693938158856874319184314120776776192");
151 test_dh ("secp-224r1", &nettle_secp_224r1,
152 "1321072106881784386340709783538698930880431939595776773514895067682",
153 "6768311794185371282972144247871764855860666277647541840973645586477",
154 "2880077809069104378181313860274147139049600284805670362929579614547",
155 "13934723037778859565852601874354272638301919827851286722006496784914",
156 "373124771833407982305885866158843810218322878380632071540538232035",
157 "24223309755162432227459925493224336241652868856405241018762887667883",
158 "8330362698029245839097779050425944245826040430538860338085968752913",
159 "24167244512472228715617822000878192535267113543393576038737592837010");
161 test_dh ("secp-256r1", &nettle_secp_256r1,
162 "94731533361265297353914491124013058635674217345912524033267198103710636378786",
163 "22441589863306126152768848344973918725077248391248404659242620344938484650846",
164 "8673475622926171928656873398933611700804732317466515884933832073457396747355",
165 "97657865959185011849283028361556797595752581630732610898393589042714626616209",
166 "18453500628354973083413728373777272885280811435138222441593126858566687017580",
167 "14365748655141740924607822284126054269177292284541187981786689038777833170313",
168 "102958799567030688009123101477538973715497039396202015119148334812951370853564",
169 "29188877854984806245046208182450375893010623119030341548941791125497546766367");
171 test_dh ("secp-384r1", &nettle_secp_384r1,
172 "39086550219018474560700767788227987514008150214902287969462741484831311917159729009715909108606822193356890811565070",
173 "15536343869384820642787280162462493474000839389760580357050317691132784247078954166759523572989472049798969369413707",
174 "23268351460749985365652822073294615614961429585671989812206213135127969284347174876010177880230302801199500921999966",
175 "36869963309577906178833120963925446333578086292605692048464445726274368063284094788012795873582576522541658781990645",
176 "6571571183519639697971973492227725184968062063941037806786906539419849188357322949908539215960508669158121817812397",
177 "36555212611228586427448926841660565534959679681904941933188284044726925984417589749068550977832780023128545833460008",
178 "27780263733159299625371532605243698753833039933618994121416145881861678645978369807598146716869504289033472077532789",
179 "12327518461490664021199432424728005314646140038116972426756705356672414772151215711157356913456651047992140493843405");
181 test_dh ("secp-521r1", &nettle_secp_521r1,
182 "1177787298234877762125077260641419691552146813662613924864132680693789861345339466386194840381422980702458955378518702648732728796955434922249345867267377826",
183 "3168153642368000846168628288850857848098131369578410603904155841373678828215434925507474033105518841999665785152501356092020415699294327720257651796364374116",
184 "278603899104240796379373331240296114411332466119196525390128418935585486485808560319073463912513286987331907013829243645911963547435764718505394265715321106",
185 "4632844957395758597246278843156350179301194123641664447791935593091018103746003967476919616681982477804041933745387575872964923485212972039478646226080044590",
186 "3278857364905061449863537070675297207767865967146919975942590789168732752489407699106980407552332044280575891715425195464227794423128203118286002006478070253",
187 "4488572162727491199625798812850846214916160870437505769058530973184916706326908828109446998319674522651965593412129100088877891410841200092694907512496020182",
188 "2126311732129869456512627735193938710331935978955001830871465201548004444073866677974896970734635601049909886616595755762740651165670628002084824920216966370",
189 "4803556648772727869384704240411011976585308117802975396033423138930126997561438092192867119930177133880625991019440171972612468402200399449807843995563872782");
191 /* NOTE: This isn't the standard way to do curve25519
192 diffie-hellman, but it tests that the ecc_point interface works
193 also with curve25519. */
194 test_dh ("curve25519", &_nettle_curve25519,
195 "238301186166219052901200372289459967515481170332211409964804596991365959539",
196 "14283836751943535877833976277675258994717521964638468784408792140505262281235",
197 "43912344711849354965202408139054167824861850336739416536288592824181793690574",
198 "3795950278952272509684177709511717492358770264218705926196469999516028451559",
199 "9468726108732441384988851273894214794301501512287024874346147472389705411936",
200 "38072138078045635808869930165213470653418146012939584392304609812494425185763",
201 "10481077163111981870382976851703705086808805457403127024129174358161599078055",
202 "29260211489972704256554624312266763530759418996739976957020673870747051409679");