3 * Constant-time operations
5 * (c) 2013 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
35 /*----- Header files ------------------------------------------------------*/
37 #include <mLib/bits.h>
39 /*----- Miscellaneous constant-time utilities -----------------------------*/
41 /* --- @ct_inteq@ --- *
43 * Arguments: @uint32 x, y@ = two 32-bit unsigned integers
45 * Returns: One if @x@ and @y@ are equal, zero if they differ.
47 * Use: Answers whether two integers are equal, in constant time.
50 extern int ct_inteq(uint32 /*x*/, uint32 /*y*/);
52 /* --- @ct_intle@ --- *
54 * Arguments: @uint32 x, y@ = two 32-bit unsigned integers
56 * Returns: One if %$x \le y$%, zero if @x@ is greater.
58 * Use: Answers whether two integers are ordered, in constant time.
61 extern int ct_intle(uint32 /*x*/, uint32 /*y*/);
63 /* --- @ct_pick@ --- *
65 * Arguments: @uint32 a@ = a switch, either zero or one
66 * @uint32 x0, x1@ = two 32-bit unsigned integers
68 * Returns: @x0@ if @a@ is zero; @x1@ if @a@ is one. Other values of @a@
69 * will give you unhelpful results.
71 * Use: Picks one of two results according to a switch variable, in
75 extern uint32 ct_pick(uint32 /*a*/, uint32 /*x0*/, uint32 /*x1*/);
77 /* --- @ct_condcopy@ --- *
79 * Arguments: @uint32 a@ = a switch, either zero or one
80 * @void *d@ = destination pointer
81 * @const void *s@ = source pointer
82 * @size_t n@ amount to copy
86 * Use: If @a@ is one then copy the @n@ bytes starting at @s@ to
87 * @d@; if @a@ is zero then leave @d@ unchanged (but it will
88 * still be written). All of this is done in constant time.
91 extern void ct_condcopy(uint32 /*a*/,
92 void */*d*/, const void */*s*/, size_t /*n*/);
96 * Arguments: @const void *p, *q@ = two pointers to buffers
97 * @size_t n@ = the (common) size of the buffers
99 * Returns: One if the two buffers are equal, zero if they aren't.
101 * Use: Compares two chunks of memory, in constant time.
104 extern int ct_memeq(const void */*p*/, const void */*q*/, size_t /*n*/);
106 /*----- Utilities for testing ---------------------------------------------*/
108 /* --- @ct_poison@ --- *
110 * Arguments: @const void *p@ = pointer to a secret
111 * @size_t sz@ = size of the secret
115 * Use: Ordinarily, does nothing. If the process is running under
116 * the control of Valgrind's `memcheck' utility, then mark the
117 * secret as `uninitialized', so that Valgrind warns about
118 * conditional execution or memory addressing based on the value
121 * Credit for this idea goes to Adam Langley, who described it
122 * in https://www.imperialviolet.org/2010/04/01/ctgrind.html,
123 * though this implementation doesn't require patching Valgrind.
126 extern void ct_poison(const void */*p*/, size_t /*sz*/);
128 /* --- @ct_remedy@ --- *
130 * Arguments: @const void *p@ = pointer to a secret
131 * @size_t sz@ = size of the secret
135 * Use: Ordinarily, does nothing. If the process is running under
136 * the control of Valgrind's `memcheck' utility, then mark the
137 * secret as `initialized'. This is intended to reverse the
138 * effect of @ct_poison@ so that a test program can verify
139 * function outputs wihtout Valgrind warning.
142 extern void ct_remedy(const void */*p*/, size_t /*sz*/);
144 /*----- That's all, folks -------------------------------------------------*/