pgroups: Ship a keyring file containing the custom prime groups.
[u/mdw/catacomb] / mkpgroups
diff --git a/mkpgroups b/mkpgroups
new file mode 100755 (executable)
index 0000000..3b3874f
--- /dev/null
+++ b/mkpgroups
@@ -0,0 +1,42 @@
+#! /usr/bin/python
+
+import catacomb as C
+import mLib as M
+from sys import stdin
+
+kf = C.KeyFile('pgroups.kr', C.KOPEN_WRITE)
+
+for line in stdin:
+  line = line.strip()
+  if line == '' or (line.startswith('#') and not line.startswith('#:')):
+    continue
+  F = line.split()
+  if F[0] == 'group':
+    name = F[1]
+    if not name.startswith('catacomb'):
+      continue
+    def snarf(what):
+      F = stdin.next().split()
+      assert F[0] == what
+      return F[1]
+    p = C.MP(snarf('p'))
+    q = C.MP(snarf('q'))
+    g = C.MP(snarf('g'))
+    ff = []
+    while True:
+      F = stdin.next().split()
+      if not F or F[0] != '#:factor':
+        break
+      ff.append(C.MP(F[1]))
+    seed = C.rmd160().hash(name).done()
+    k = kf.newkey(C.ReadBuffer(seed).getu32(), 'dh-param')
+    k.tag = name
+    k.data = C.KeyDataStructured({
+      'p': C.KeyDataMP(p, 'shared'),
+      'q': C.KeyDataMP(q, 'shared'),
+      'g': C.KeyDataMP(g, 'shared')
+    })
+    k.attr['factor'] = ', '.join([f.tostring() for f in ff])
+    k.attr['genseed'] = M.base64_encode(seed)
+    k.attr['seedalg'] = 'rmd160-mgf'
+kf.save()