]> pilppa.org Git - familiar-h63xx-build.git/blob - org.handhelds.familiar/packages/teleport/files/crypt.c
OE tree imported from monotone branch org.openembedded.oz354fam083 at revision 8b12e3...
[familiar-h63xx-build.git] / org.handhelds.familiar / packages / teleport / files / crypt.c
1 /*
2  * Copyright (C) 2003 Philip Blundell <philb@gnu.org>
3  *
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.
8  */
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <glib.h>
13 #include <assert.h>
14
15 #include "libdisplaymigration/auth.h"
16 #include "libdisplaymigration/crypt.h"
17
18 static gcry_mpi_t
19 mpi_from_sexp (gcry_sexp_t r, char *tag)
20 {
21   gcry_sexp_t s = gcry_sexp_find_token (r, tag, 0);
22   return gcry_sexp_nth_mpi (s, 1, GCRYMPI_FMT_USG);
23 }
24
25 static char *
26 hex_from_mpi (gcry_mpi_t m)
27 {
28   char *buf;
29   gcry_mpi_aprint (GCRYMPI_FMT_HEX, (void *)&buf, NULL, m);
30   return buf;
31 }
32
33 void
34 displaymigration_crypt_create_hash (char *display, char *challenge, size_t len, char *result)
35 {
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);
41   g_free (buf);
42 }
43
44 static int
45 do_encode_md (const unsigned char *digest, size_t digestlen, int algo,
46               unsigned int nbits, gcry_mpi_t *r_val)
47 {
48   int nframe = (nbits+7) / 8;
49   unsigned char *frame;
50   int i, n;
51   unsigned char asn[100];
52   size_t asnlen;
53
54   asnlen = sizeof(asn);
55   if (gcry_md_algo_info (algo, GCRYCTL_GET_ASNOID, asn, &asnlen))
56     return -1;
57
58   if (digestlen + asnlen + 4  > nframe )
59     return -1;
60   
61   /* We encode the MD in this way:
62    *
63    *       0  1 PAD(n bytes)   0  ASN(asnlen bytes)  MD(len bytes)
64    *
65    * PAD consists of FF bytes.
66    */
67   frame = g_malloc (nframe);
68   n = 0;
69   frame[n++] = 0;
70   frame[n++] = 1; /* block type */
71   i = nframe - digestlen - asnlen -3 ;
72   assert ( i > 1 );
73   memset ( frame+n, 0xff, i ); n += i;
74   frame[n++] = 0;
75   memcpy ( frame+n, asn, asnlen ); n += asnlen;
76   memcpy ( frame+n, digest, digestlen ); n += digestlen;
77   assert ( n == nframe );
78       
79   gcry_mpi_scan (r_val, GCRYMPI_FMT_USG, frame, nframe, &nframe);
80   g_free (frame);
81   return 0;
82 }
83
84 gboolean
85 displaymigration_crypt_sign_hash (struct rsa_key *k, char *hash, gchar **result)
86 {
87   gcry_mpi_t mpi;
88   gcry_sexp_t data, sig, key;
89   int rc;
90   char *hex;
91
92   do_encode_md (hash, 20, GCRY_MD_SHA1, 1024, &mpi);
93
94   if (gcry_sexp_build (&data, NULL, "(data (value %m))", mpi))
95     return FALSE;
96  
97   gcry_mpi_release (mpi);
98
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))
101     {
102       gcry_sexp_release (data);
103       return FALSE;
104     }
105   
106   rc = gcry_pk_sign (&sig, data, key);
107
108   gcry_sexp_release (data);
109   gcry_sexp_release (key);
110
111   if (rc)
112     return FALSE;
113
114   mpi = mpi_from_sexp (sig, "s");
115   hex = hex_from_mpi (mpi);
116   *result = g_strdup (hex);
117   gcry_free (hex);
118   gcry_mpi_release (mpi);
119   gcry_sexp_release (sig);
120
121   return TRUE;
122 }
123
124 gboolean
125 displaymigration_crypt_check_signature (struct rsa_key *k, char *hash, char *sigbuf)
126 {
127   gcry_mpi_t mpi, mpi2;
128   gcry_sexp_t data, sig, key;
129   int rc;
130
131   do_encode_md (hash, 20, GCRY_MD_SHA1, 1024, &mpi);
132
133   gcry_sexp_build (&data, NULL, "(data (value %m))", mpi);
134   
135   gcry_mpi_release (mpi);
136
137   gcry_sexp_build (&key, NULL, "(public-key (rsa (n %m) (e %m)))", k->n, k->e);
138
139   if (gcry_mpi_scan (&mpi2, GCRYMPI_FMT_HEX, sigbuf, 0, NULL))
140     {
141       gcry_sexp_release (data);
142       return FALSE;
143     }
144
145   gcry_sexp_build (&sig, NULL, "(sig-val (rsa (s %m)))", mpi2);
146
147   rc = gcry_pk_verify (sig, data, key);
148
149   gcry_sexp_release (data);
150   gcry_sexp_release (key);
151   gcry_sexp_release (sig);
152   gcry_mpi_release (mpi2);
153
154   if (rc)
155     return FALSE;
156
157   return TRUE;
158 }