X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/a5b0f8ff5c3b7150e853db4744791dbc4daf2d33..d4bb7fded5f9a4b0db99f82e88f22effe94f2ff7:/ct.h diff --git a/ct.h b/ct.h new file mode 100644 index 0000000..00cecc0 --- /dev/null +++ b/ct.h @@ -0,0 +1,112 @@ +/* -*-c-*- + * + * Constant-time operations + * + * (c) 2013 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of Catacomb. + * + * Catacomb is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * Catacomb is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with Catacomb; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +#ifndef CATACOMB_CT_H +#define CATACOMB_CT_H + +#ifdef __cplusplus + extern "C" { +#endif + +/*----- Header files ------------------------------------------------------*/ + +#include + +/*----- Functions provided ------------------------------------------------*/ + +/* --- @ct_inteq@ --- * + * + * Arguments: @uint32 x, y@ = two 32-bit unsigned integers + * + * Returns: One if @x@ and @y@ are equal, zero if they differ. + * + * Use: Answers whether two integers are equal, in constant time. + */ + +extern int ct_inteq(uint32 /*x*/, uint32 /*y*/); + +/* --- @ct_intle@ --- * + * + * Arguments: @uint32 x, y@ = two 32-bit unsigned integers + * + * Returns: One if %$x \le y$% are equal, zero if @x@ is greater. + * + * Use: Answers whether two integers are ordered, in constant time. + */ + +extern int ct_intle(uint32 /*x*/, uint32 /*y*/); + +/* --- @ct_pick@ --- * + * + * Arguments: @uint32 a@ = a switch, either zero or one + * @uint32 x0, x1@ = two 32-bit unsigned integers + * + * Returns: @x0@ if @a@ is zero; @x1@ if @a@ is one. Other values of @a@ + * will give you unhelpful results. + * + * Use: Picks one of two results according to a switch variable, in + * constant time. + */ + +extern int ct_pick(uint32 /*a*/, uint32 /*x0*/, uint32 /*x1*/); + +/* --- @ct_condcopy@ --- * + * + * Arguments: @uint32 a@ = a switch, either zero or one + * @void *d@ = destination pointer + * @const void *s@ = source pointer + * @size_t n@ amount to copy + * + * Returns: --- + * + * Use: If @a@ is one then copy the @n@ bytes starting at @s@ to + * @d@; if @a@ is zero then leave @d@ unchanged (but it will + * still be written). All of this is done in constant time. + */ + +extern void ct_condcopy(uint32 /*a*/, + void */*d*/, const void */*s*/, size_t /*n*/); + +/* --- @ct_memeq@ --- + * + * Arguments: @const void *p, *q@ = two pointers to buffers + * @size_t n@ = the (common) size of the buffers + * + * Returns: One if the two buffers are equal, zero if they aren't. + * + * Use: Compares two chunks of memory, in constant time. + */ + +extern int ct_memeq(const void */*p*/, const void */*q*/, size_t /*n*/); + +/*----- That's all, folks -------------------------------------------------*/ + +#ifdef __cplusplus + } +#endif + +#endif