2 * Copyright (C) 2003 Philip Blundell <philb@gnu.org>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
15 #include "libdisplaymigration/auth.h"
16 #include "libdisplaymigration/crypt.h"
19 mpi_from_sexp (gcry_sexp_t r, char *tag)
21 gcry_sexp_t s = gcry_sexp_find_token (r, tag, 0);
22 return gcry_sexp_nth_mpi (s, 1, GCRYMPI_FMT_USG);
26 hex_from_mpi (gcry_mpi_t m)
29 gcry_mpi_aprint (GCRYMPI_FMT_HEX, (void *)&buf, NULL, m);
34 displaymigration_crypt_create_hash (char *display, char *challenge, size_t len, char *result)
36 size_t dlen = strlen (display);
37 gchar *buf = g_malloc (dlen + 1 + len);
38 strcpy (buf, display);
39 memcpy (buf + dlen + 1, challenge, len);
40 gcry_md_hash_buffer (GCRY_MD_SHA1, result, buf, len + dlen + 1);
45 do_encode_md (const unsigned char *digest, size_t digestlen, int algo,
46 unsigned int nbits, gcry_mpi_t *r_val)
48 int nframe = (nbits+7) / 8;
51 unsigned char asn[100];
55 if (gcry_md_algo_info (algo, GCRYCTL_GET_ASNOID, asn, &asnlen))
58 if (digestlen + asnlen + 4 > nframe )
61 /* We encode the MD in this way:
63 * 0 1 PAD(n bytes) 0 ASN(asnlen bytes) MD(len bytes)
65 * PAD consists of FF bytes.
67 frame = g_malloc (nframe);
70 frame[n++] = 1; /* block type */
71 i = nframe - digestlen - asnlen -3 ;
73 memset ( frame+n, 0xff, i ); n += i;
75 memcpy ( frame+n, asn, asnlen ); n += asnlen;
76 memcpy ( frame+n, digest, digestlen ); n += digestlen;
77 assert ( n == nframe );
79 gcry_mpi_scan (r_val, GCRYMPI_FMT_USG, frame, nframe, &nframe);
85 displaymigration_crypt_sign_hash (struct rsa_key *k, char *hash, gchar **result)
88 gcry_sexp_t data, sig, key;
92 do_encode_md (hash, 20, GCRY_MD_SHA1, 1024, &mpi);
94 if (gcry_sexp_build (&data, NULL, "(data (value %m))", mpi))
97 gcry_mpi_release (mpi);
99 if (gcry_sexp_build (&key, NULL, "(private-key (rsa (n %m) (e %m) (d %m) (p %m) (q %m) (u %m)))",
100 k->n, k->e, k->d, k->p, k->q, k->u))
102 gcry_sexp_release (data);
106 rc = gcry_pk_sign (&sig, data, key);
108 gcry_sexp_release (data);
109 gcry_sexp_release (key);
114 mpi = mpi_from_sexp (sig, "s");
115 hex = hex_from_mpi (mpi);
116 *result = g_strdup (hex);
118 gcry_mpi_release (mpi);
119 gcry_sexp_release (sig);
125 displaymigration_crypt_check_signature (struct rsa_key *k, char *hash, char *sigbuf)
127 gcry_mpi_t mpi, mpi2;
128 gcry_sexp_t data, sig, key;
131 do_encode_md (hash, 20, GCRY_MD_SHA1, 1024, &mpi);
133 gcry_sexp_build (&data, NULL, "(data (value %m))", mpi);
135 gcry_mpi_release (mpi);
137 gcry_sexp_build (&key, NULL, "(public-key (rsa (n %m) (e %m)))", k->n, k->e);
139 if (gcry_mpi_scan (&mpi2, GCRYMPI_FMT_HEX, sigbuf, 0, NULL))
141 gcry_sexp_release (data);
145 gcry_sexp_build (&sig, NULL, "(sig-val (rsa (s %m)))", mpi2);
147 rc = gcry_pk_verify (sig, data, key);
149 gcry_sexp_release (data);
150 gcry_sexp_release (key);
151 gcry_sexp_release (sig);
152 gcry_mpi_release (mpi2);