summarylogtreecommitdiffstats
path: root/0002-Support-ECC-and-DSA-pub-keys.patch
blob: d426e460934eb1624889e50eed1b19bd3280bf1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
From 5cf6de5ded961302afdfac5e0a7995ae456aaef2 Mon Sep 17 00:00:00 2001
From: ap4y <mail@ap4y.me>
Date: Wed, 3 Apr 2019 21:59:31 +1300
Subject: [PATCH] Support ECC and DSA pub keys

---
 src/util/support.c | 90 ++++++++++++++++++++++++++++++++++++----------
 src/util/support.h |  7 ++++
 2 files changed, 79 insertions(+), 18 deletions(-)

diff --git a/src/util/support.c b/src/util/support.c
index 8e576c6..773f3d8 100644
--- a/src/util/support.c
+++ b/src/util/support.c
@@ -86,26 +86,17 @@ challenge_verify_sexp (gcry_sexp_t sexp_key,
   gpg_error_t err = GPG_ERR_NO_ERROR;
   gcry_sexp_t sexp_signature = NULL;
   gcry_sexp_t sexp_data = NULL;
-  gcry_mpi_t mpi_signature = NULL;
+  int algo = pk_algo (sexp_key);
 
-  /* Convert buffers into MPIs.  */
-  if (! err)
-    {
-      if (gcry_mpi_scan (&mpi_signature, GCRYMPI_FMT_USG, response, response_n,
-			 NULL))
-	err = gpg_error (GPG_ERR_BAD_MPI);
-    }
+  if (algo == 0)
+    return gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM);
+
+  err = challenge_data (&sexp_data, algo, challenge, challenge_n);
 
-  /* Create according S-Expressions.  */
-  if (! err)
-    err = gcry_sexp_build (&sexp_data, NULL,
-			   "(data (flags pkcs1) (hash sha1 %b))",
-			   challenge_n, challenge);
   if (! err)
-    err = gcry_sexp_build (&sexp_signature, NULL, "(sig-val (rsa (s %m)))",
-			   mpi_signature);
+    err = response_signature (&sexp_signature, algo,
+                              response, response_n);
 
-  /* Verify.  */
   if (! err)
     err = gcry_pk_verify (sexp_signature, sexp_data, sexp_key);
 
@@ -113,8 +104,6 @@ challenge_verify_sexp (gcry_sexp_t sexp_key,
     gcry_sexp_release (sexp_data);
   if (sexp_signature)
     gcry_sexp_release (sexp_signature);
-  if (mpi_signature)
-    gcry_mpi_release (mpi_signature);
 
   return err;
 }
@@ -387,4 +376,69 @@ my_strlen (const char *s)
   return ret;
 }
 
+int
+pk_algo (gcry_sexp_t sexp_key)
+{
+  gcry_sexp_t sexp_data;
+  char *algoname;
+  int algo;
+
+  sexp_data = gcry_sexp_find_token (sexp_key, "public-key", 0);
+  if (!sexp_data)
+    return 0;
+
+  gcry_sexp_t sexp_tmp = gcry_sexp_cadr (sexp_data);
+  gcry_sexp_release (sexp_data);
+  sexp_data = sexp_tmp;
+
+  algoname = gcry_sexp_nth_string (sexp_data, 0);
+  gcry_sexp_release (sexp_data);
+  if (!algoname)
+    return 0;
+
+  algo = gcry_pk_map_name (algoname);
+  xfree(algoname);
+  return algo;
+}
+
+gpg_error_t
+challenge_data (gcry_sexp_t *data, int algo,
+                unsigned char *challenge, size_t challenge_n)
+{
+  if (algo == GCRY_PK_ECC)
+    {
+      return gcry_sexp_build (data, NULL,
+                              "(data (flags eddsa) (hash-algo sha512) (value %b))",
+                              challenge_n, challenge);
+    }
+
+  return gcry_sexp_build (data, NULL,
+                          "(data (flags pkcs1) (hash sha1 %b))",
+                          challenge_n, challenge);
+}
+
+gpg_error_t
+response_signature (gcry_sexp_t *sig, int algo,
+                    unsigned char *response, size_t response_n)
+{
+  switch (algo)
+    {
+    case GCRY_PK_RSA:
+      return gcry_sexp_build (sig, NULL,
+                              "(sig-val (rsa (s %b)))",
+                              response_n, response);
+    case GCRY_PK_DSA:
+      return gcry_sexp_build (sig, NULL,
+                              "(sig-val (dsa (r %b) (s %b)))",
+                              response_n / 2, response,
+                              response_n / 2, response + response_n / 2);
+    case GCRY_PK_ECC:
+      return gcry_sexp_build (sig, NULL,
+                              "(sig-val (eddsa (r %b) (s %b)))",
+                              response_n / 2, response,
+                              response_n / 2, response + response_n / 2);
+    default:
+      return gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM);
+    }
+}
 /* END */
diff --git a/src/util/support.h b/src/util/support.h
index e25cf01..740ffa0 100644
--- a/src/util/support.h
+++ b/src/util/support.h
@@ -71,6 +71,13 @@ void char_vector_free (char **a);
 
 int my_strlen (const char *s);
 
+int pk_algo (gcry_sexp_t sexp_key);
+
+gpg_error_t challenge_data (gcry_sexp_t *data, int algo,
+                            unsigned char *challenge, size_t challenge_n);
+
+gpg_error_t response_signature (gcry_sexp_t *sig, int algo,
+                                unsigned char *response, size_t response_n);
 #endif
 
 /* END */
-- 
2.20.1