X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/a27430a2fc1347926142db0f1a24fe6ecaee9163..f52f2db067dc1388b16ab00ddb53e26a381a6e3e:/idea.c diff --git a/idea.c b/idea.c index cde3af4..c0f7fe7 100644 --- a/idea.c +++ b/idea.c @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: idea.c,v 1.3 2000/07/02 18:24:39 mdw Exp $ + * $Id: idea.c,v 1.5 2004/04/08 01:36:15 mdw Exp $ * * Implementation of the IDEA cipher * * (c) 1999 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Catacomb. * @@ -15,33 +15,18 @@ * 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. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: idea.c,v $ - * Revision 1.3 2000/07/02 18:24:39 mdw - * Use a new multiplication function from an Ascom white paper to resist - * timing attacks. - * - * Revision 1.2 2000/06/17 11:24:08 mdw - * New key size interface. - * - * Revision 1.1 1999/09/03 08:41:12 mdw - * Initial import. - * - */ - /*----- Header files ------------------------------------------------------*/ #include @@ -75,13 +60,16 @@ static uint16 inv(uint16 n) { uint32 m = 0x10001; uint32 a = 1, b = 0; + uint32 nn = n; + if (!nn) + nn = 0x10000; for (;;) { uint32 q, r, t; - if (!(r = m % n)) + if (!(r = m % nn)) break; - q = m / n; - m = n; n = r; + q = m / nn; + m = nn; nn = r; t = a; a = b - q * a; b = t; } if (a > MASK16) @@ -255,8 +243,8 @@ void idea_init(idea_ctx *k, const void *buf, size_t sz) ROUND(_k, _a, _b, _c, _d); \ ROUND(_k, _a, _c, _b, _d); \ MIX (_k, _a, _c, _b, _d); \ - c = (U16(_a) << 16) | U16(_c); \ - d = (U16(_b) << 16) | U16(_d); \ + c = ((uint32)U16(_a) << 16) | (uint32)U16(_c); \ + d = ((uint32)U16(_b) << 16) | (uint32)U16(_d); \ } while (0) #define DBLK(k, a, b) EBLK((k), (a), (b))