+/*----- X25519 and related algorithms -------------------------------------*/
+
+#define XDHS(_) \
+ _(X25519, x25519) \
+ _(X448, x448)
+
+#define DEFXDH(X, x) \
+ static PyObject *meth_##x(PyObject *me, PyObject *arg) \
+ { \
+ struct bin k, p; \
+ PyObject *rc = 0; \
+ if (!PyArg_ParseTuple(arg, "O&O&:" #x, convbin, &k, convbin, &p)) \
+ goto end; \
+ if (k.sz != X##_KEYSZ) VALERR("bad key length"); \
+ if (p.sz != X##_PUBSZ) VALERR("bad public length"); \
+ rc = bytestring_pywrap(0, X##_OUTSZ); \
+ x((octet *)BIN_PTR(rc), k.p, p.p); \
+ return (rc); \
+ end: \
+ return (0); \
+ }
+XDHS(DEFXDH)
+#undef DEFXDH
+
+/*----- Ed25519 and related algorithms ------------------------------------*/
+
+#define EDDSAS(_) \
+ _(ED25519, ed25519, -1, ctx) \
+ _(ED448, ed448, 0, )
+
+#define DEFEDDSA(ED, ed, phdflt, sigver) \
+ \
+ static PyObject *meth_##ed##_pubkey(PyObject *me, PyObject *arg) \
+ { \
+ struct bin k; \
+ PyObject *rc = 0; \
+ if (!PyArg_ParseTuple(arg, "O&:" #ed "_pubkey", convbin, &k)) \
+ goto end; \
+ rc = bytestring_pywrap(0, ED##_PUBSZ); \
+ ed##_pubkey((octet *)BIN_PTR(rc), k.p, k.sz); \
+ return (rc); \
+ end: \
+ return (0); \
+ } \
+ \
+ static PyObject *meth_##ed##_sign(PyObject *me, PyObject *arg, \
+ PyObject *kw) \
+ { \
+ struct bin k, p = { 0, 0}, c = { 0, 0 }, m; \
+ int ph = phdflt; \
+ PyObject *rc = 0; \
+ octet pp[ED##_PUBSZ]; \
+ static const char *const kwlist[] = \
+ { "key", "msg", "pub", "perso", "phflag", 0 }; \
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, \
+ "O&O&|O&O&O&:" #ed "_sign", \
+ KWLIST, \
+ convbin, &k, convbin, &m, \
+ convbin, &p, convbin, &c, \
+ convbool, &ph)) \
+ goto end; \
+ if (p.p && p.sz != ED##_PUBSZ) VALERR("bad public length"); \
+ if (c.p && c.sz > ED##_MAXPERSOSZ) \
+ VALERR("personalization string too long"); \
+ if (c.p && ph == -1) ph = 0; \
+ if (!p.p) { p.p = pp; ed##_pubkey(pp, k.p, k.sz); } \
+ rc = bytestring_pywrap(0, ED##_SIGSZ); \
+ ed##sigver##_sign((octet *)BIN_PTR(rc), k.p, k.sz, \
+ p.p, ph, c.p, c.sz, m.p, m.sz); \
+ return (rc); \
+ end: \
+ return (0); \
+ } \
+ \
+ static PyObject *meth_##ed##_verify(PyObject *me, \
+ PyObject *arg, PyObject *kw) \
+ { \
+ struct bin p, c = { 0, 0 }, m, s; \
+ int ph = phdflt; \
+ PyObject *rc = 0; \
+ static const char *const kwlist[] = \
+ { "pub", "msg", "sig", "perso", "phflag", 0 }; \
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, \
+ "O&O&O&|O&O&:" #ed "_verify", \
+ KWLIST, \
+ convbin, &p, convbin, &m, \
+ convbin, &s, \
+ convbin, &c, convbool, &ph)) \
+ goto end; \
+ if (p.sz != ED##_PUBSZ) VALERR("bad public length"); \
+ if (s.sz != ED##_SIGSZ) VALERR("bad signature length"); \
+ if (c.p && c.sz > ED##_MAXPERSOSZ) \
+ VALERR("personalization string too long"); \
+ if (c.p && ph == -1) ph = 0; \
+ rc = getbool(!ed##sigver##_verify(p.p, ph, c.p, c.sz, \
+ m.p, m.sz, s.p)); \
+ return (rc); \
+ end: \
+ return (0); \
+ }
+EDDSAS(DEFEDDSA)
+#undef DEFEDDSA
+