case KSZ_SET: {
keyszset_pyobj *o =
PyObject_New(keyszset_pyobj, keyszset_pytype);
+ PyObject *l;
int i, n;
o->dfl = ARG(0);
for (i = 0; ARG(i); i++) ;
- n = i; o->set = PyTuple_New(n);
- for (i = 0; i < n; i++)
- PyTuple_SET_ITEM(o->set, i, PyInt_FromLong(ARG(i)));
+ n = i; l = PyList_New(n);
+ for (i = 0; i < n; i++) PyList_SET_ITEM(l, i, PyInt_FromLong(ARG(i)));
+ o->set = PyFrozenSet_New(l); Py_DECREF(l);
return ((PyObject *)o);
} break;
default:
PyObject *arg, PyObject *kw)
{
static const char *const kwlist[] = { "default", "min", "max", "mod", 0 };
- int dfl, min = 0, max = 0, mod = 1;
+ int dfl, min = 0, max, mod = 1;
+ PyObject *maxobj = Py_None;
keyszrange_pyobj *o;
- if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|iii:new", KWLIST,
- &dfl, &min, &max, &mod))
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|iOi:new", KWLIST,
+ &dfl, &min, &maxobj, &mod))
goto end;
- if (dfl < 0 || min < 0) VALERR("key size cannot be negative");
+ if (maxobj == Py_None)
+ max = 0;
+ else {
+ max = PyInt_AsLong(maxobj);
+ if (max == -1 && PyErr_Occurred()) goto end;
+ }
+ if (dfl < 0 || min < 0 || max < 0) VALERR("key size cannot be negative");
if (min > dfl || (max && dfl > max)) VALERR("bad key size bounds");
if (mod <= 0 || dfl%mod || min%mod || max%mod)
VALERR("bad key size modulus");
PyObject *arg, PyObject *kw)
{
static const char *const kwlist[] = { "default", "set", 0 };
- int dfl, i, n, xx;
+ int dfl, xx;
PyObject *set = 0;
- PyObject *x = 0, *l = 0;
+ PyObject *x = 0, *l = 0, *i = 0;
keyszset_pyobj *o = 0;
if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|O:new", KWLIST, &dfl, &set))
goto end;
- if (!set) set = PyTuple_New(0);
- else Py_INCREF(set);
- if (!PySequence_Check(set)) TYERR("want a sequence");
- n = PySequence_Size(set); if (n < 0) goto end;
+ if (set) i = PyObject_GetIter(set);
+ else { set = PyTuple_New(0); i = PyObject_GetIter(set); Py_DECREF(set); }
+ if (!i) goto end;
l = PyList_New(0); if (!l) goto end;
if (dfl < 0) VALERR("key size cannot be negative");
- x = PyInt_FromLong(dfl);
- PyList_Append(l, x);
- Py_DECREF(x);
- x = 0;
- for (i = 0; i < n; i++) {
- if ((x = PySequence_GetItem(set, i)) == 0) goto end;
- xx = PyInt_AsLong(x);
- if (PyErr_Occurred()) goto end;
- if (xx == dfl) continue;
+ x = PyInt_FromLong(dfl); PyList_Append(l, x); Py_DECREF(x); x = 0;
+ for (;;) {
+ x = PyIter_Next(i); if (!x) break;
+ xx = PyInt_AsLong(x); if (xx == -1 && PyErr_Occurred()) goto end;
if (xx < 0) VALERR("key size cannot be negative");
- PyList_Append(l, x);
- Py_DECREF(x);
- x = 0;
+ PyList_Append(l, x); Py_DECREF(x); x = 0;
}
- Py_DECREF(set);
- if ((set = PySequence_Tuple(l)) == 0) goto end;
+ if ((set = PyFrozenSet_New(l)) == 0) goto end;
o = (keyszset_pyobj *)ty->tp_alloc(ty, 0);
o->dfl = dfl;
o->set = set;
- Py_INCREF(set);
end:
- Py_XDECREF(set);
Py_XDECREF(l);
Py_XDECREF(x);
return ((PyObject *)o);
static PyObject *kaget_min(PyObject *me, void *hunoz)
{ return (PyInt_FromLong(0)); }
-#define kaget_max kaget_min
+static PyObject *kaget_max(PyObject *me, void *hunoz)
+ { RETURN_NONE; }
+
+static PyObject *krget_max(PyObject *me, void *hunoz)
+{
+ int max = ((keyszrange_pyobj *)me)->max;
+ if (max) return (PyInt_FromLong(max));
+ else RETURN_NONE;
+}
static PyObject *ksget_min(PyObject *me, void *hunoz)
{
- PyObject *set = ((keyszset_pyobj *)me)->set;
- int i, n, y, x = -1;
- n = PyTuple_GET_SIZE(set);
- for (i = 0; i < n; i++) {
- y = PyInt_AS_LONG(PyTuple_GET_ITEM(set, i));
+ PyObject *i = PyObject_GetIter(((keyszset_pyobj *)me)->set);
+ PyObject *v = 0;
+ int y, x = -1;
+ for (;;) {
+ v = PyIter_Next(i); if (!v) break;
+ y = PyInt_AsLong(v); assert(y >= 0);
if (x == -1 || y < x) x = y;
}
+ Py_DECREF(i); Py_XDECREF(v);
+ if (PyErr_Occurred()) return (0);
return (PyInt_FromLong(x));
}
static PyObject *ksget_max(PyObject *me, void *hunoz)
{
- PyObject *set = ((keyszset_pyobj *)me)->set;
- int i, n, y, x = -1;
- n = PyTuple_GET_SIZE(set);
- for (i = 0; i < n; i++) {
- y = PyInt_AS_LONG(PyTuple_GET_ITEM(set, i));
+ PyObject *i = PyObject_GetIter(((keyszset_pyobj *)me)->set);
+ PyObject *v = 0;
+ int y, x = -1;
+ for (;;) {
+ v = PyIter_Next(i); if (!v) break;
+ y = PyInt_AsLong(v); assert(y >= 0);
if (y > x) x = y;
}
+ Py_DECREF(i); Py_XDECREF(v);
+ if (PyErr_Occurred()) return (0);
return (PyInt_FromLong(x));
}
static const PyMemberDef keyszrange_pymembers[] = {
#define MEMBERSTRUCT keyszrange_pyobj
MEMBER(min, T_INT, READONLY, "KSZ.min -> smallest allowed key size")
- MEMBER(max, T_INT, READONLY, "KSZ.max -> largest allowed key size")
MEMBER(mod, T_INT, READONLY,
"KSZ.mod -> key size must be a multiple of this")
#undef MEMBERSTRUCT
{ 0 }
};
+static const PyGetSetDef keyszrange_pygetset[] = {
+#define GETSETNAME(op, name) kr##op##_##name
+ GET (max, "KSZ.max -> largest allowed key size")
+#undef GETSETNAME
+ { 0 }
+};
+
static const PyGetSetDef keyszset_pygetset[] = {
#define GETSETNAME(op, name) ks##op##_##name
GET (min, "KSZ.min -> smallest allowed key size")
0, /* @tp_iternext@ */
0, /* @tp_methods@ */
PYMEMBERS(keyszrange), /* @tp_members@ */
- 0, /* @tp_getset@ */
+ PYGETSET(keyszrange), /* @tp_getset@ */
0, /* @tp_base@ */
0, /* @tp_dict@ */
0, /* @tp_descr_get@ */
Py_TPFLAGS_BASETYPE,
/* @tp_doc@ */
- "KeySZSet(DEFAULT, SEQ)\n"
- " Key size constraints: size must be DEFAULT or an element of SEQ.",
+ "KeySZSet(DEFAULT, ITER)\n"
+ " Key size constraints: size must be DEFAULT or an element of ITER.",
0, /* @tp_traverse@ */
0, /* @tp_clear@ */
{ \
struct bin in; octet b[SZ_##W]; \
if (!PyArg_ParseTuple(arg, "O&:hashbuf" #w, convbin, &in)) goto end; \
- if (in.sz > MASK##n) TYERR("string too long"); \
+ if (in.sz > MASK##n) VALERR("too large"); \
STORE##W(b, in.sz); if (gaeadaad_hash(me, b, sizeof(b))) goto end; \
if (gaeadaad_hash(me, in.p, in.sz)) goto end; \
RETURN_ME; \
{ \
struct bin in; \
if (!PyArg_ParseTuple(arg, "O&:hashbuf" #w, convbin, &in)) goto end; \
- if (in.sz > MASK##n) TYERR("string too long"); \
+ if (in.sz > MASK##n) VALERR("too large"); \
GH_HASHBUF##W(GHASH_H(me), in.p, in.sz); \
RETURN_ME; \
end: \
{ 0 }
};
+static PyObject *gmget_name(PyObject *me, void *hunoz)
+ { return (TEXT_FROMSTR(GMAC_M(me)->ops->c->name)); }
+
+static PyObject *gmget_hashsz(PyObject *me, void *hunoz)
+ { return (PyInt_FromLong(GMAC_M(me)->ops->c->hashsz)); }
+#define gmget_tagsz gmget_hashsz
+
+static const PyGetSetDef gmac_pygetset[] = {
+#define GETSETNAME(op, name) gm##op##_##name
+ GET (hashsz, "M.hashsz -> MAC output size")
+ GET (tagsz, "M.tagsz -> MAC output size")
+ GET (name, "M.name -> name of this kind of MAC")
+#undef GETSETNAME
+ { 0 }
+};
+
static const PyTypeObject gcmac_pytype_skel = {
PyVarObject_HEAD_INIT(0, 0) /* Header */
"GCMAC", /* @tp_name@ */
0, /* @tp_iternext@ */
0, /* @tp_methods@ */
0, /* @tp_members@ */
- 0, /* @tp_getset@ */
+ PYGETSET(gmac), /* @tp_getset@ */
0, /* @tp_base@ */
0, /* @tp_dict@ */
0, /* @tp_descr_get@ */
struct bin in; \
octet b[SZ_##W]; \
if (!PyArg_ParseTuple(arg, "O&:hashbuf" #w, convbin, &in)) goto end; \
- if (in.sz > MASK##n) TYERR("string too long"); \
+ if (in.sz > MASK##n) VALERR("too large"); \
STORE##W(b, in.sz); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
poly1305_hash(P1305_CTX(me), in.p, in.sz); \
RETURN_ME; \
{ 0 }
};
+static PyObject *poly1305get_name(PyObject *me, void *hunoz)
+ { RETURN_OBJ(((PyHeapTypeObject *)poly1305key_pytype)->ht_name); }
+
+static PyObject *poly1305get_tagsz(PyObject *me, void *hunoz)
+ { return (PyInt_FromLong(16)); }
+
+static const PyGetSetDef poly1305_pygetset[] = {
+#define GETSETNAME(op, name) poly1305##op##_##name
+ GET (tagsz, "PK.tagsz -> MAC output size")
+ GET (name, "PK.name -> name of this kind of MAC")
+#undef GETSETNAME
+ { 0 }
+};
+
static const PyMethodDef poly1305hash_pymethods[] = {
#define METHNAME(name) polymeth_##name
NAMETH(copy, "P.copy() -> PP")
static const PyTypeObject poly1305cls_pytype_skel = {
PyVarObject_HEAD_INIT(0, 0) /* Header */
- "Poly1305Class", /* @tp_name@ */
+ "_Poly1305Class", /* @tp_name@ */
sizeof(PyHeapTypeObject), /* @tp_basicsize@ */
0, /* @tp_itemsize@ */
0, /* @tp_iternext@ */
0, /* @tp_methods@ */
0, /* @tp_members@ */
- 0, /* @tp_getset@ */
+ PYGETSET(poly1305), /* @tp_getset@ */
0, /* @tp_base@ */
0, /* @tp_dict@ */
0, /* @tp_descr_get@ */
struct bin in; \
octet b[SZ_##W]; \
if (!PyArg_ParseTuple(arg, "O&:hashbuf" #w, convbin, &in)) goto end; \
- if (in.sz > MASK##n) TYERR("string too long"); \
+ if (in.sz > MASK##n) VALERR("too large"); \
if (shake_check(me, 0)) goto end; \
STORE##W(b, in.sz); shake_hash(SHAKE_H(me), b, sizeof(b)); \
shake_hash(SHAKE_H(me), in.p, in.sz); \
0 /* @tp_is_gc@ */
};
+static PyTypeObject *kmac_pytype, *kmac128_pytype, *kmac256_pytype;
+
+static PyObject *kmac_dopynew(void (*initfn)(shake_ctx *,
+ const void *, size_t,
+ const void *, size_t),
+ PyTypeObject *ty,
+ PyObject *arg, PyObject *kw)
+{
+ shake_pyobj *rc = 0;
+ PyObject *pobj = Py_None;
+ struct bin k = { 0, 0 }, p = { 0, 0 };
+ static const char *const kwlist[] = { "key", "perso", 0 };
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O:new", KWLIST,
+ convbin, &k, &pobj))
+ goto end;
+ if (pobj != Py_None && !convbin(pobj, &p)) goto end;
+ rc = (shake_pyobj *)ty->tp_alloc(ty, 0);
+ initfn(&rc->h, p.p, p.sz, k.p, k.sz);
+ rc->st = 0;
+end:
+ return ((PyObject *)rc);
+}
+
+static PyObject *kmac128_pynew(PyTypeObject *ty,
+ PyObject *arg, PyObject *kw)
+ { return (kmac_dopynew(kmac128_init, ty, arg, kw)); }
+
+static PyObject *kmac256_pynew(PyTypeObject *ty,
+ PyObject *arg, PyObject *kw)
+ { return (kmac_dopynew(kmac256_init, ty, arg, kw)); }
+
+static PyObject *kmacmeth_xof(PyObject *me)
+{
+ if (shake_check(me, 0)) goto end;
+ kmac_xof(SHAKE_H(me));
+ SHAKE_ST(me) = 1;
+ RETURN_ME;
+end:
+ return (0);
+}
+
+static PyObject *kmacmeth_done(PyObject *me, PyObject *arg, PyObject *kw)
+{
+ PyObject *rc = 0;
+ size_t n = 100 - SHAKE_H(me)->h.r/2;
+ static const char *const kwlist[] = { "hsz", 0 };
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:done", KWLIST, convszt, &n))
+ goto end;
+ if (shake_check(me, 0)) goto end;
+ rc = bytestring_pywrap(0, n);
+ kmac_done(SHAKE_H(me), BIN_PTR(rc), n);
+ SHAKE_ST(me) = -1;
+end:
+ return (rc);
+}
+
+static const PyMethodDef kmac_pymethods[] = {
+#define METHNAME(func) kmacmeth_##func
+ NAMETH(xof, "K.xof()")
+ KWMETH(done, "K.done([hsz = CAP/2]) -> T")
+#undef METHNAME
+ { 0 }
+};
+
+static const PyTypeObject kmac_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "KMAC", /* @tp_name@ */
+ sizeof(shake_pyobj), /* @tp_basicsize@ */
+ 0, /* @tp_itemsize@ */
+
+ 0, /* @tp_dealloc@ */
+ 0, /* @tp_print@ */
+ 0, /* @tp_getattr@ */
+ 0, /* @tp_setattr@ */
+ 0, /* @tp_compare@ */
+ 0, /* @tp_repr@ */
+ 0, /* @tp_as_number@ */
+ 0, /* @tp_as_sequence@ */
+ 0, /* @tp_as_mapping@ */
+ 0, /* @tp_hash@ */
+ 0, /* @tp_call@ */
+ 0, /* @tp_str@ */
+ 0, /* @tp_getattro@ */
+ 0, /* @tp_setattro@ */
+ 0, /* @tp_as_buffer@ */
+ Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
+ Py_TPFLAGS_BASETYPE,
+
+ /* @tp_doc@ */
+ "KMAC base class.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ 0, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ 0, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ PYMETHODS(kmac), /* @tp_methods@ */
+ 0, /* @tp_members@ */
+ 0, /* @tp_getset@ */
+ 0, /* @tp_base@ */
+ 0, /* @tp_dict@ */
+ 0, /* @tp_descr_get@ */
+ 0, /* @tp_descr_set@ */
+ 0, /* @tp_dictoffset@ */
+ 0, /* @tp_init@ */
+ PyType_GenericAlloc, /* @tp_alloc@ */
+ abstract_pynew, /* @tp_new@ */
+ 0, /* @tp_free@ */
+ 0 /* @tp_is_gc@ */
+};
+
+static const PyTypeObject kmac128_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "KMAC128", /* @tp_name@ */
+ 0, /* @tp_basicsize@ */
+ 0, /* @tp_itemsize@ */
+
+ 0, /* @tp_dealloc@ */
+ 0, /* @tp_print@ */
+ 0, /* @tp_getattr@ */
+ 0, /* @tp_setattr@ */
+ 0, /* @tp_compare@ */
+ 0, /* @tp_repr@ */
+ 0, /* @tp_as_number@ */
+ 0, /* @tp_as_sequence@ */
+ 0, /* @tp_as_mapping@ */
+ 0, /* @tp_hash@ */
+ 0, /* @tp_call@ */
+ 0, /* @tp_str@ */
+ 0, /* @tp_getattro@ */
+ 0, /* @tp_setattro@ */
+ 0, /* @tp_as_buffer@ */
+ Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
+ Py_TPFLAGS_BASETYPE,
+
+ /* @tp_doc@ */
+ "KMAC128(KEY, [perso = STR]): KMAC XOMAC.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ 0, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ 0, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ 0, /* @tp_methods@ */
+ 0, /* @tp_members@ */
+ 0, /* @tp_getset@ */
+ 0, /* @tp_base@ */
+ 0, /* @tp_dict@ */
+ 0, /* @tp_descr_get@ */
+ 0, /* @tp_descr_set@ */
+ 0, /* @tp_dictoffset@ */
+ 0, /* @tp_init@ */
+ PyType_GenericAlloc, /* @tp_alloc@ */
+ kmac128_pynew, /* @tp_new@ */
+ 0, /* @tp_free@ */
+ 0 /* @tp_is_gc@ */
+};
+
+static const PyTypeObject kmac256_pytype_skel = {
+ PyVarObject_HEAD_INIT(0, 0) /* Header */
+ "KMAC256", /* @tp_name@ */
+ 0, /* @tp_basicsize@ */
+ 0, /* @tp_itemsize@ */
+
+ 0, /* @tp_dealloc@ */
+ 0, /* @tp_print@ */
+ 0, /* @tp_getattr@ */
+ 0, /* @tp_setattr@ */
+ 0, /* @tp_compare@ */
+ 0, /* @tp_repr@ */
+ 0, /* @tp_as_number@ */
+ 0, /* @tp_as_sequence@ */
+ 0, /* @tp_as_mapping@ */
+ 0, /* @tp_hash@ */
+ 0, /* @tp_call@ */
+ 0, /* @tp_str@ */
+ 0, /* @tp_getattro@ */
+ 0, /* @tp_setattro@ */
+ 0, /* @tp_as_buffer@ */
+ Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
+ Py_TPFLAGS_BASETYPE,
+
+ /* @tp_doc@ */
+ "KMAC256(KEY, [perso = STR]): KMAC XOMAC.",
+
+ 0, /* @tp_traverse@ */
+ 0, /* @tp_clear@ */
+ 0, /* @tp_richcompare@ */
+ 0, /* @tp_weaklistoffset@ */
+ 0, /* @tp_iter@ */
+ 0, /* @tp_iternext@ */
+ 0, /* @tp_methods@ */
+ 0, /* @tp_members@ */
+ 0, /* @tp_getset@ */
+ 0, /* @tp_base@ */
+ 0, /* @tp_dict@ */
+ 0, /* @tp_descr_get@ */
+ 0, /* @tp_descr_set@ */
+ 0, /* @tp_dictoffset@ */
+ 0, /* @tp_init@ */
+ PyType_GenericAlloc, /* @tp_alloc@ */
+ kmac256_pynew, /* @tp_new@ */
+ 0, /* @tp_free@ */
+ 0 /* @tp_is_gc@ */
+};
+
/*----- Pseudorandom permutations -----------------------------------------*/
static PyTypeObject *gcprp_pytype, *gprp_pytype;
INITTYPE(shake, root);
INITTYPE(shake128, shake);
INITTYPE(shake256, shake);
+ INITTYPE(kmac, shake);
+ INITTYPE(kmac128, kmac);
+ INITTYPE(kmac256, kmac);
INITTYPE(gcprp, type);
INITTYPE(gprp, root);
addmethods(methods);
INSERT("GMACHash", gmhash_pytype);
INSERT("gcmacs", make_algtab(gmactab, sizeof(gcmac *),
mac_namefn, mac_valfn));
- INSERT("Poly1305Class", poly1305cls_pytype);
+ INSERT("_Poly1305Class", poly1305cls_pytype);
INSERT("poly1305", poly1305key_pytype);
INSERT("Poly1305Hash", poly1305hash_pytype);
INSERT("Keccak1600", kxvik_pytype);
INSERT("Shake", shake_pytype);
INSERT("Shake128", shake128_pytype);
INSERT("Shake256", shake256_pytype);
+ INSERT("KMAC", kmac_pytype);
+ INSERT("KMAC128", kmac128_pytype);
+ INSERT("KMAC256", kmac256_pytype);
INSERT("GCPRP", gcprp_pytype);
INSERT("GPRP", gprp_pytype);
INSERT("gcprps", make_algtab(gprptab, sizeof(gcprp *),