aboutsummaryrefslogtreecommitdiff
path: root/crypto.c
diff options
context:
space:
mode:
authorMarkus Teich <markus.teich@stusta.mhn.de>2016-06-13 21:09:41 +0200
committerMarkus Teich <markus.teich@stusta.mhn.de>2016-06-13 21:27:03 +0200
commit338c95de4190065149ddc995e5404d335c05f432 (patch)
tree2f7dbfb7b4fb274071fb5a2992de27703ac61960 /crypto.c
parent557fbe2cc32f3ff6d8c2c0c8aa272f7b55f45618 (diff)
downloadlibbrandt-338c95de4190065149ddc995e5404d335c05f432.tar.gz
libbrandt-338c95de4190065149ddc995e5404d335c05f432.zip
coding style
Diffstat (limited to 'crypto.c')
-rw-r--r--crypto.c135
1 files changed, 84 insertions, 51 deletions
diff --git a/crypto.c b/crypto.c
index 7f2940a..55ab26a 100644
--- a/crypto.c
+++ b/crypto.c
@@ -35,8 +35,13 @@ struct brandt_ec_pkey {
35}; 35};
36 36
37gcry_mpi_point_t ec_gen; 37gcry_mpi_point_t ec_gen;
38gcry_ctx_t ec_ctx; 38gcry_ctx_t ec_ctx;
39 39
40/**
41 * brandt_crypto_init
42 *
43 *
44 */
40void 45void
41brandt_crypto_init () 46brandt_crypto_init ()
42{ 47{
@@ -154,29 +159,28 @@ brandt_mpi_scan_unsigned (gcry_mpi_t *result, const void *data, size_t size)
154 brandt_assert_gpgerr (rc); 159 brandt_assert_gpgerr (rc);
155} 160}
156 161
157/* 162//gcry_mpi_point_t
158gcry_mpi_point_t 163//deserialize_point(const struct brandt_point* data, const int len)
159deserialize_point(const struct brandt_point* data, const int len) 164//{
160{ 165// gcry_sexp_t s;
161 gcry_sexp_t s; 166// gcry_ctx_t ctx;
162 gcry_ctx_t ctx; 167// gcry_mpi_point_t ret;
163 gcry_mpi_point_t ret; 168// gcry_error_t rc;
164 gcry_error_t rc; 169//
165 170// rc = gcry_sexp_build(&s, NULL, "(public-key(ecc(curve " CURVE ")(q %b)))",
166 rc = gcry_sexp_build(&s, NULL, "(public-key(ecc(curve " CURVE ")(q %b)))", 171// len, data);
167 len, data); 172// brandt_assert_gpgerr(rc);
168 brandt_assert_gpgerr(rc); 173//
169 174// rc = gcry_mpi_ec_new(&ctx, s, NULL);
170 rc = gcry_mpi_ec_new(&ctx, s, NULL); 175// brandt_assert_gpgerr(rc);
171 brandt_assert_gpgerr(rc); 176// gcry_sexp_release(s);
172 gcry_sexp_release(s); 177//
178// ret = gcry_mpi_ec_get_point("q", ctx, 0);
179// brandt_assert(ret);
180// gcry_ctx_release(ctx);
181// return ret;
182//}
173 183
174 ret = gcry_mpi_ec_get_point("q", ctx, 0);
175 brandt_assert(ret);
176 gcry_ctx_release(ctx);
177 return ret;
178}
179*/
180 184
181/* --- EC --- */ 185/* --- EC --- */
182 186
@@ -238,8 +242,13 @@ key_from_sexp (gcry_mpi_t *array, gcry_sexp_t sexp, const char *topname,
238 return 0; 242 return 0;
239} 243}
240 244
245/**
246 * brandt_ec_skey_create
247 *
248 * @param[out] skey where to store the generated secret key
249 */
241void 250void
242brandt_ec_skey_create (gcry_mpi_t* skey) 251brandt_ec_skey_create (gcry_mpi_t *skey)
243{ 252{
244 gcry_sexp_t s_keyparam; 253 gcry_sexp_t s_keyparam;
245 gcry_sexp_t priv_sexp; 254 gcry_sexp_t priv_sexp;
@@ -259,16 +268,26 @@ brandt_ec_skey_create (gcry_mpi_t* skey)
259 gcry_sexp_release (priv_sexp); 268 gcry_sexp_release (priv_sexp);
260} 269}
261 270
262 271/**
272 * brandt_ec_pkey_compute
273 *
274 * @param pkey TODO
275 * @param skey TODO
276 */
263void 277void
264brandt_ec_pkey_compute (gcry_mpi_point_t* pkey, const gcry_mpi_t skey) 278brandt_ec_pkey_compute (gcry_mpi_point_t *pkey, const gcry_mpi_t skey)
265{ 279{
266 280
267} 281}
268 282
269 283/**
284 * brandt_ec_keypair_create
285 *
286 * @param[out] pkey where to store the generated public key
287 * @param[out] skey where to store the generated secret key
288 */
270void 289void
271brandt_ec_keypair_create (gcry_mpi_point_t* pkey, gcry_mpi_t* skey) 290brandt_ec_keypair_create (gcry_mpi_point_t *pkey, gcry_mpi_t *skey)
272{ 291{
273 gcry_ctx_t ctx; 292 gcry_ctx_t ctx;
274 gcry_sexp_t s_keyparam; 293 gcry_sexp_t s_keyparam;
@@ -290,52 +309,66 @@ brandt_ec_keypair_create (gcry_mpi_point_t* pkey, gcry_mpi_t* skey)
290 brandt_assert_gpgerr (rc); 309 brandt_assert_gpgerr (rc);
291 gcry_sexp_release (priv_sexp); 310 gcry_sexp_release (priv_sexp);
292 311
293 *pkey = gcry_mpi_ec_get_point("q", ctx, 0); 312 *pkey = gcry_mpi_ec_get_point ("q", ctx, 0);
294 brandt_assert (NULL != *pkey); 313 brandt_assert (NULL != *pkey);
295 gcry_ctx_release (ctx); 314 gcry_ctx_release (ctx);
296} 315}
297 316
298 317/**
318 * brandt_ec_keypair_create_base
319 *
320 * @param[out] pkey where to store the generated public key
321 * @param[out] skey where to store the generated secret key
322 * @param[in] base which base point should be used to calculate the public key
323 */
299void 324void
300brandt_ec_keypair_create_base (gcry_mpi_point_t* pkey, gcry_mpi_t* skey, const gcry_mpi_point_t base) 325brandt_ec_keypair_create_base (gcry_mpi_point_t *pkey, gcry_mpi_t *skey,
326 const gcry_mpi_point_t base)
301{ 327{
302 brandt_ec_skey_create(skey); 328 brandt_ec_skey_create (skey);
303 brandt_assert(*skey); 329 brandt_assert (*skey);
304 *pkey = gcry_mpi_point_new(0); 330 *pkey = gcry_mpi_point_new (0);
305 brandt_assert(*pkey); 331 brandt_assert (*pkey);
306 gcry_mpi_ec_mul(*pkey, *skey, base, ec_ctx); 332 gcry_mpi_ec_mul (*pkey, *skey, base, ec_ctx);
307} 333}
308 334
309 335/**
336 * brandt_ec_point_cmp compares two curve points
337 *
338 * @param[in] a the first point
339 * @param[in] b the second point
340 * @return 0 if @a a and @a b represent the same point on the curve, something
341 * else otherwise
342 */
310int 343int
311brandt_ec_point_cmp (const gcry_mpi_point_t a, const gcry_mpi_point_t b) 344brandt_ec_point_cmp (const gcry_mpi_point_t a, const gcry_mpi_point_t b)
312{ 345{
313 int ret = 1; 346 int ret = 1;
314 gcry_mpi_t ax = gcry_mpi_new(0); 347 gcry_mpi_t ax = gcry_mpi_new (0);
315 gcry_mpi_t bx = gcry_mpi_new(0); 348 gcry_mpi_t bx = gcry_mpi_new (0);
316 gcry_mpi_t ay = gcry_mpi_new(0); 349 gcry_mpi_t ay = gcry_mpi_new (0);
317 gcry_mpi_t by = gcry_mpi_new(0); 350 gcry_mpi_t by = gcry_mpi_new (0);
318 351
319 brandt_assert (a && b); 352 brandt_assert (a && b);
320 if (!ax || !bx || !ay || !by) 353 if (!ax || !bx || !ay || !by)
321 { 354 {
322 weprintf("could not init point in point_cmp"); 355 weprintf ("could not init point in point_cmp");
323 return 1; 356 return 1;
324 } 357 }
325 358
326 if (!gcry_mpi_ec_get_affine(ax, ay, a, ec_ctx) && !gcry_mpi_ec_get_affine(bx, by, b, ec_ctx)) 359 if (!gcry_mpi_ec_get_affine (ax, ay, a, ec_ctx)
360 && !gcry_mpi_ec_get_affine (bx, by, b, ec_ctx))
327 { 361 {
328 ret = gcry_mpi_cmp(ax, bx) || gcry_mpi_cmp(ay, by); 362 ret = gcry_mpi_cmp (ax, bx) || gcry_mpi_cmp (ay, by);
329 } 363 }
330 364
331 gcry_mpi_release(ax); 365 gcry_mpi_release (ax);
332 gcry_mpi_release(bx); 366 gcry_mpi_release (bx);
333 gcry_mpi_release(ay); 367 gcry_mpi_release (ay);
334 gcry_mpi_release(by); 368 gcry_mpi_release (by);
335 return ret; 369 return ret;
336} 370}
337 371
338
339/** 372/**
340 * Convert the given private key from the network format to the 373 * Convert the given private key from the network format to the
341 * S-expression that can be used by libgcrypt. 374 * S-expression that can be used by libgcrypt.
@@ -395,7 +428,7 @@ brandt_ecdhe_key_get_public (const struct brandt_ec_skey *priv,
395int 428int
396brandt_ecdhe (const struct brandt_ec_skey *priv, 429brandt_ecdhe (const struct brandt_ec_skey *priv,
397 const struct brandt_ec_pkey *pub, 430 const struct brandt_ec_pkey *pub,
398 struct brandt_hash_code *key_material) 431 struct brandt_hash_code *key_material)
399{ 432{
400 gcry_error_t rc; 433 gcry_error_t rc;
401 int rc2; 434 int rc2;