X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/ba6e6b64033b1f9de49feccb5c9cd438354481f7..0f00dc4c8eb47e67bc0f148c2dd109f73a451e0a:/symm/des3.c diff --git a/symm/des3.c b/symm/des3.c new file mode 100644 index 0000000..dedd9f2 --- /dev/null +++ b/symm/des3.c @@ -0,0 +1,117 @@ +/* -*-c-*- + * + * Implementation of double- and triple-DES + * + * (c) 1999 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. + */ + +/*----- Header files ------------------------------------------------------*/ + +#include +#include +#include +#include + +#include + +#include "blkc.h" +#include "des-base.h" +#include "des.h" +#include "des3.h" +#include "gcipher.h" + +/*----- Global variables --------------------------------------------------*/ + +const octet des3_keysz[] = { KSZ_SET, 21, 7, 8, 14, 16, 24, 0 }; + +/*----- Main code ---------------------------------------------------------*/ + +/* --- @des3_init@ --- * + * + * Arguments: @des3_ctx *k@ = pointer to key block + * @const void *buf@ = pointer to key buffer + * @size_t sz@ = size of key material + * + * Returns: --- + * + * Use: Initializes a DES key buffer. The key buffer may have length + * 7, 8, 14, 16, 21, or 24. These correspond to one, two or + * three DES keys, either packed or unpacked (i.e., still + * containing parity bits). + */ + +void des3_init(des3_ctx *k, const void *buf, size_t sz) +{ + size_t step; + const octet *p = buf; + + KSZ_ASSERT(des3, sz); + + if (sz % 7 == 0) + step = 7; + else + step = 8; + + des_init(&k->a, p, step); + if (sz > 8) p += step; + des_init(&k->b, p, step); + if (sz > 16) p += step; else p = buf; + des_init(&k->c, p, step); +} + +/* --- @des3_eblk@, @des3_dblk@ --- * + * + * Arguments: @const des3_ctx *k@ = pointer to key block + * @const uint32 s[2]@ = pointer to source block + * @uint32 d[2]@ = pointer to destination block + * + * Returns: --- + * + * Use: Low-level block encryption and decryption. + */ + +void des3_eblk(const des3_ctx *k, const uint32 *s, uint32 *d) +{ + uint32 x = s[0], y = s[1]; + DES_IP(x, y); + DES_EBLK(k->a.k, x, y, x, y); + DES_DBLK(k->b.k, x, y, x, y); + DES_EBLK(k->c.k, x, y, x, y); + DES_IPINV(x, y); + d[0] = x, d[1] = y; +} + +void des3_dblk(const des3_ctx *k, const uint32 *s, uint32 *d) +{ + uint32 x = s[0], y = s[1]; + DES_IP(x, y); + DES_DBLK(k->c.k, x, y, x, y); + DES_EBLK(k->b.k, x, y, x, y); + DES_DBLK(k->a.k, x, y, x, y); + DES_IPINV(x, y); + d[0] = x, d[1] = y; +} + +BLKC_TEST(DES3, des3) + +/*----- That's all, folks -------------------------------------------------*/