From: mdw Date: Sat, 15 Jul 2000 13:44:32 +0000 (+0000) Subject: New ciphers. X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/commitdiff_plain/ea937a0ebd081c146194a848ff3ca3829265a341 New ciphers. --- diff --git a/tea.c b/tea.c new file mode 100644 index 0000000..90d97cb --- /dev/null +++ b/tea.c @@ -0,0 +1,131 @@ +/* -*-c-*- + * + * $Id: tea.c,v 1.1 2000/07/15 13:44:31 mdw Exp $ + * + * The Tiny Encryption Algorithm + * + * (c) 2000 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. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: tea.c,v $ + * Revision 1.1 2000/07/15 13:44:31 mdw + * New ciphers. + * + */ + +/*----- Header files ------------------------------------------------------*/ + +#include + +#include "blkc.h" +#include "gcipher.h" +#include "paranoia.h" +#include "tea.h" + +/*----- Global variables --------------------------------------------------*/ + +const octet tea_keysz[] = { KSZ_RANGE, TEA_KEYSZ, 0, 16, 1 }; + +/*----- Main code ---------------------------------------------------------*/ + +/* --- @tea_init@ --- * + * + * Arguments: @tea_ctx *k@ = pointer to key block + * @const void *buf@ = pointer to key buffer + * @size_t sz@ = size of key material + * + * Returns: --- + * + * Use: Initializes a TEA key buffer. The key buffer must be 16 + * bytes long. + */ + +void tea_init(tea_ctx *k, const void *buf, size_t sz) +{ + octet kb[16]; + const octet *p; + + KSZ_ASSERT(tea, sz); + if (sz >= sizeof(kb)) + p = buf; + else { + memcpy(kb, buf, sz); + memset(kb + sz, 0, sizeof(kb) - sz); + p = kb; + } + + k->ka = LOAD32(p + 0); k->kb = LOAD32(p + 4); + k->kc = LOAD32(p + 8); k->kd = LOAD32(p + 12); + + if (p == kb) + BURN(kb); +} + +/* --- @tea_eblk@, @tea_dblk@ --- * + * + * Arguments: @const tea_ctx *k@ = pointer to key block + * @const uint32 s[2]@ = pointer to source block + * @uint32 d[2]@ = pointer to teatination block + * + * Returns: --- + * + * Use: Low-level block encryption and decryption. + */ + +#define DELTA 0x9e3779b9 + +void tea_eblk(const tea_ctx *k, const uint32 *s, uint32 *d) +{ + uint32 y = s[0], z = s[1]; + uint32 ka = k->ka, kb = k->kb, kc = k->kc, kd = k->kd; + uint32 n = 0; + unsigned i; + + for (i = 0; i < 32; i++) { + n += DELTA; + y = U32(y + (((z << 4) + ka) ^ (z + n) ^ ((z >> 5) + kb))); + z = U32(z + (((y << 4) + kc) ^ (y + n) ^ ((y >> 5) + kd))); + } + d[0] = y; d[1] = z; +} + +void tea_dblk(const tea_ctx *k, const uint32 *s, uint32 *d) +{ + uint32 y = s[0], z = s[1]; + uint32 ka = k->ka, kb = k->kb, kc = k->kc, kd = k->kd; + uint32 n = DELTA << 5; + unsigned i; + + for (i = 0; i < 32; i++) { + z = U32(z - (((y << 4) + kc) ^ (y + n) ^ ((y >> 5) + kd))); + y = U32(y - (((z << 4) + ka) ^ (z + n) ^ ((z >> 5) + kb))); + n -= DELTA; + } + d[0] = y; d[1] = z; +} + +BLKC_TEST(TEA, tea) + +/*----- That's all, folks -------------------------------------------------*/ diff --git a/tea.h b/tea.h new file mode 100644 index 0000000..c227ec5 --- /dev/null +++ b/tea.h @@ -0,0 +1,113 @@ +/* -*-c-*- + * + * $Id: tea.h,v 1.1 2000/07/15 13:44:31 mdw Exp $ + * + * The Tiny Encryption Algorithm + * + * (c) 2000 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. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: tea.h,v $ + * Revision 1.1 2000/07/15 13:44:31 mdw + * New ciphers. + * + */ + +/*----- Notes on the Tiny Encryption Algorithm ----------------------------* + * + * TEA is an amazingly simple 64-round Feistel network. It's tiny, fairly + * quick and surprisingly strong. It was invented by David Wheeler and Roger + * Needham. It's unpatented. The keyspace is has only 126 effective bits, + * and there are related-key attacks. If you want these fixed, use XTEA. + * + * This implementation uses big-endian byte order, following SCAN. + */ + +#ifndef CATACOMB_TEA_H +#define CATACOMB_TEA_H + +#ifdef __cplusplus + extern "C" { +#endif + +/*----- Header files ------------------------------------------------------*/ + +#include + +#include + +/*----- Magical numbers ---------------------------------------------------*/ + +#define TEA_BLKSZ 8 +#define TEA_KEYSZ 16 +#define TEA_CLASS (N, B, 64) + +extern const octet tea_keysz[]; + +/*----- Data structures ---------------------------------------------------*/ + +typedef struct tea_ctx { + uint32 ka, kb, kc, kd; +} tea_ctx; + +/*----- Functions provided ------------------------------------------------*/ + +/* --- @tea_init@ --- * + * + * Arguments: @tea_ctx *k@ = pointer to key block + * @const void *buf@ = pointer to key buffer + * @size_t sz@ = size of key material + * + * Returns: --- + * + * Use: Initializes a TEA key buffer. The key buffer may be up to 16 + * bytes long. + */ + +extern void tea_init(tea_ctx */*k*/, const void */*buf*/, size_t /*sz*/); + +/* --- @tea_eblk@, @tea_dblk@ --- * + * + * Arguments: @const tea_ctx *k@ = pointer to key block + * @const uint32 s[2]@ = pointer to source block + * @uint32 d[2]@ = pointer to teatination block + * + * Returns: --- + * + * Use: Low-level block encryption and decryption. + */ + +extern void tea_eblk(const tea_ctx */*k*/, + const uint32 */*s*/, uint32 */*d*/); +extern void tea_dblk(const tea_ctx */*k*/, + const uint32 */*s*/, uint32 */*d*/); + +/*----- That's all, folks -------------------------------------------------*/ + +#ifdef __cplusplus + } +#endif + +#endif diff --git a/tests/tea b/tests/tea new file mode 100644 index 0000000..7384207 --- /dev/null +++ b/tests/tea @@ -0,0 +1,72 @@ +# $Id: tea,v 1.1 2000/07/15 13:44:32 mdw Exp $ +# +# Test vectors for TEA + +# --- Generated from Needham and Wheeler's original implementation --- + +tea { + 00112233445566778899aabbccddeeff 0123456789abcdef 126c6b92c0653a3e; + d94576cd1a5f99e4155dc7bec5d33df2 d51399607f7aa9d7 7d1eb12827ef2121; + 07ef12bc9d06d7da20131116b665e335 10b233473624ccd3 ae694e579daeff54; + c6462b48afd604b75fcc455506ffd411 3b905e951e4fafe7 d9633fcf13c30394; + be195f703d6fec2232cd0890c981b27d 15b623d1fa2df9df 916ffcb1abaf086b; + 5b8410f7a9eb92da120810c56fcaf8c9 7c1035940696d91d 460aa2db964b580c; + b85c9e26661889d0ff1054c6be27fdeb 90b245144895f3ad 87d7e95a5a4aa89a; + a8a1e956d54d238f6ceab186e7ec99ed 4ef2cfd120b8d4dc 1752f2ee188018de; + 8bc4879201a96368927ecceee67024ee f6cb5353f4a1c432 7cfa8e0718d59807; + f1372d85d8c3d1a60df57eedf317ab81 215c080cf158a09b 3b85115e0d6c5a94; + 017f01683f5e009d196feeaad385c5a9 568583061da98d0d cb0e345182f4bf0b; + 281c932ddd3c364b09f1896007ffab03 0558b306316b0823 e7adf267c7d841c2; + c1e95465c7ef8eb04254d1a5463c6a43 fdcb2764303222c7 ed48b228c11e7bc6; + 0f86dd6c96dd31164b656b0f25e4b411 eaddba89070ec46c 89fb864135a57e33; + fbacfb479ae21194c35b8184e58dd66e c6507bcf99b9c2a1 670da449ce74c658; + 2eb36c4a9598d471700a1330070fffc9 c380b0f1c21d4d37 d84fe611431c0b5e; + 0a7f48127091780617a1f534b3271bc9 e5b7c1517f24f298 4be47c656cef6ffc; + 303fb24822a1b8a84d0ece77b86380ff d14ddf77fdda17bf 1cc73315c9f72ab1; + f04ebf798c193f199c1f532af383555b b9682750bb15caad 2b5f318fb66806b9; + 200c0ce59717d5d9af6813cd207fee73 9706769a18a2d03d 8e17eeb5008dc0cb; + 2828d51f98ae0b33f4de2b7fbd18a529 edb76c54847da59e dfa6c29beb40f9af; + 61aaba6be48b0d0d14fe5d27fab852a4 178a49bafba53f23 c3bbce6715d90ce6; + 2080e2409ba01c8532fc84403ee8c06a df4cdb61a5f38536 b4e6359c9d9458aa; + 271ad15192c4d1204a4a2561e3db6ff7 7c944d08def34c0c 195c0e8f49761539; + c1e297c0c761777d8a76fff02d22b859 f4c76c1d47fe568f 551ca67c7756ff56; + 23c807a2ef0a551f858fd52d125a47d8 cab16583e15d0074 de472175d55372c9; + 9fa5d4d8cbdfcecd559e3ce88bf78ee1 97b05c60774164ad bc4bd30cb50da39b; + 24f4e91083139099d663647a7ffac321 7017a263985b735c e47fdf06a51d8c24; + 7cf8626de76d8462218ed5c9dc8acc26 15475a90df04cd29 4df54282eaafa235; + 3c6ad7df17332a3e1e3de06007387357 87ca0aaccf146cc8 f7360f57cfcc09ac; + 24237a762d8a89383a2949f5a0f5ec08 9268af048ecbae67 e98e428a9d1fee9c; + 209a2833a39472d9720380ffb2f74761 af0062cd77a84ebd 44cd624b87cbee77; + 22ebe7a30e8855b3b453a6e926d4f187 f922ca875b991a31 1e92c0c51185f7c7; + 1b5e23ebd915c1fee59f57dd91af7347 b4ecc305c3dbd8e5 063d8f75a7a77501; + 6c21d105515290da29339f142685c135 a4c2f6dc597d13ea bf8ffec7550a496b; + 01f728a7433a47b13de34fcc08958449 3af7f1ae0f58ab1d bba581d07e51d8d6; + 9a2d4c50337d3ec337673782fd385601 791d8da8cbb0bc94 7d9a4f0363b6085f; + b3b99747560e246bcd0cdc3fb33e4910 91778f2bd9233375 4b85b79a58b1321e; + 4b269e2e8dbd68b94066c95244c3a195 abfb6a33e1471e96 53d089e9313e832d; + d10b956f675dc2276b6dd90442bece3e dbedddb6a1c15a21 b81739d49c10b4fd; + 28f8fab7541766f6dafbaa5b6f3bd700 2c14d5097ab11f61 c018b6e3cca4095f; + 2b61e60478fa0c0edb9531f26791eff9 b84c80b2d245fdfc 2301e2d68d241622; + a6bfb85fa91b8ca4197c8b502a62f972 3daadd7a9633e19b 65bc93b4dbbd652f; + 94e76e54d37f932cbcc069de6bf26d52 02739eeb468450fd a477a7ec244f1e9f; + 82760ea1560e8f9d1e35f20cad1f26cc 34aa5952b5a9110f 2564284b47152721; + 3aba91211327585e0f1270b59ef9277b b584d6b34b638ba4 c829dc4da560211f; + 727074f35cd523eb6f8aafbbf76fd5b1 f0e9268a27ab70c6 f24b80555fdcb67f; + 6e0aa1c91ea6315a4a7dd297ac5936a4 47374080f27fbb30 6fdb64c32920c0a5; + 63bd2d37271a250c8593b433188cffd0 776927909196eec5 57bca56240b39c0b; + dc7beb423c2053156329d7ab79f4d1d6 24c0adb3777860ad e040f57fe32f2158; + ed219454883709efe884bbc9d30507a3 587b1683dff7f178 e2c6512996117ba9; + 40509fc5c565e9b9f3995f3bc5d5c1f4 719a39f2302a98aa b8a9f3ab1b68ac1f; + f9f10ed2bc0193605913475fd54d69ae e35b94e2940a8db0 b23358feb43a29aa; + 23003c3fbe9661b6b9386748982ac3e2 d1dfd47fac22b9ff 4165de32289acf6a; + a2caa563c2f19b10fbac1427e2177858 f7743dfe957cc82b d88dcd158b0d81c2; + 8bb42b6937d65eac506e8326356071af 690a0fa32113bf34 9095cce6d70da9d6; + 219c7f982a0c352977b978b91fcb3c45 8fb4cb86db41ce30 c688b48de8ec7e14; + 157ff76f22538eede0528c541dbe7815 ea6cd44f238be18f 35578556b38660fa; + 346194289f6d865237cc673c45415003 71690fd4ba3d75de 719f1f0a6b316a58; + 032c8c1624f7f300d8a58d151de52d78 3c0f1746798ed5b7 62c9adb10b265534; + 019471106a5cd4ee3d1f627213649b80 558a8d7a4cdc0822 014521292dcbff85; + 45aa90191c449dbf9c541fb476d1bf74 bfba3dfd06e77671 b6a6825c69d9d5eb; + c86c21d8c26dc291f662c8f2fe79b74b 0993d3b68c1d4a5d d33c2e41dd5da131; + af4f4615c7c298639b9728251991419f 1e268f9e710313b5 a9478f8cf88b7e10; +} diff --git a/tests/tea-test.c b/tests/tea-test.c new file mode 100644 index 0000000..be91e16 --- /dev/null +++ b/tests/tea-test.c @@ -0,0 +1,47 @@ +#include +#include + +/* --- Needham and Wheeler's original code --- * + * + * Almost. I changed the types from long to unsigned long. + */ + +void code(unsigned long* v, unsigned long* k) { +unsigned long y=v[0],z=v[1], sum=0, /* set up */ + delta=0x9e3779b9, n=32 ; /* a key schedule constant */ +while (n-->0) { /* basic cycle start */ + sum += delta ; + y += (z<<4)+k[0] ^ z+sum ^ (z>>5)+k[1] ; + z += (y<<4)+k[2] ^ y+sum ^ (y>>5)+k[3] ; /* end cycle */ + } +v[0]=y ; v[1]=z ; } + +int main(void) +{ + unsigned long k[4] = { 0x00112233, 0x44556677, 0x8899aabb, 0xccddeeff }; + unsigned long p[2] = { 0x01234567, 0x89abcdef }; + fibrand f; + + int i; + + printf(" %08lx%08lx%08lx%08lx %08lx%08lx ", + k[0], k[1], k[2], k[3], p[0], p[1]); + code(p, k); + printf("%08lx%08lx;\n", p[0], p[1]); + + fibrand_lcseed(&f, 0); + for (i = 1; i < 64; i++) { + k[0] = fibrand_step(&f); + k[1] = fibrand_step(&f); + k[2] = fibrand_step(&f); + k[3] = fibrand_step(&f); + p[0] = fibrand_step(&f); + p[1] = fibrand_step(&f); + printf(" %08lx%08lx%08lx%08lx %08lx%08lx ", + k[0], k[1], k[2], k[3], p[0], p[1]); + code(p, k); + printf("%08lx%08lx;\n", p[0], p[1]); + } + + return (0); +} diff --git a/tests/xtea b/tests/xtea new file mode 100644 index 0000000..fcc68b9 --- /dev/null +++ b/tests/xtea @@ -0,0 +1,72 @@ +# $Id: xtea,v 1.1 2000/07/15 13:44:32 mdw Exp $ +# +# Test vectors for XTEA + +# --- Generated from Needham and Wheeler's original implementation --- + +xtea { + 00112233445566778899aabbccddeeff 0123456789abcdef b8bf2821622b5b30; + d94576cd1a5f99e4155dc7bec5d33df2 d51399607f7aa9d7 782e4e131c5ba746; + 07ef12bc9d06d7da20131116b665e335 10b233473624ccd3 57733a1deece283e; + c6462b48afd604b75fcc455506ffd411 3b905e951e4fafe7 776f4965deb65775; + be195f703d6fec2232cd0890c981b27d 15b623d1fa2df9df 3bd5ad5c9ed6101e; + 5b8410f7a9eb92da120810c56fcaf8c9 7c1035940696d91d 5f93fa1d06991415; + b85c9e26661889d0ff1054c6be27fdeb 90b245144895f3ad 6105bfb0947828b7; + a8a1e956d54d238f6ceab186e7ec99ed 4ef2cfd120b8d4dc 51fae58a10f7c3dd; + 8bc4879201a96368927ecceee67024ee f6cb5353f4a1c432 550ad71ba8a34568; + f1372d85d8c3d1a60df57eedf317ab81 215c080cf158a09b 13d1e24ba9ff7b63; + 017f01683f5e009d196feeaad385c5a9 568583061da98d0d 6439b1dfe2ecb541; + 281c932ddd3c364b09f1896007ffab03 0558b306316b0823 c8f82fe4491a8917; + c1e95465c7ef8eb04254d1a5463c6a43 fdcb2764303222c7 244dbfa1d2c914e4; + 0f86dd6c96dd31164b656b0f25e4b411 eaddba89070ec46c 5a4909646763454f; + fbacfb479ae21194c35b8184e58dd66e c6507bcf99b9c2a1 6f08ec714e8a7c18; + 2eb36c4a9598d471700a1330070fffc9 c380b0f1c21d4d37 72d4d4bdf5c00585; + 0a7f48127091780617a1f534b3271bc9 e5b7c1517f24f298 64374b40bb732354; + 303fb24822a1b8a84d0ece77b86380ff d14ddf77fdda17bf 014f1e707d02c8d8; + f04ebf798c193f199c1f532af383555b b9682750bb15caad 9001d21b57ab5360; + 200c0ce59717d5d9af6813cd207fee73 9706769a18a2d03d 835fd788c089fd45; + 2828d51f98ae0b33f4de2b7fbd18a529 edb76c54847da59e b0c0125494d39ce9; + 61aaba6be48b0d0d14fe5d27fab852a4 178a49bafba53f23 ade26742f25efbe1; + 2080e2409ba01c8532fc84403ee8c06a df4cdb61a5f38536 9c895e72168b409e; + 271ad15192c4d1204a4a2561e3db6ff7 7c944d08def34c0c b784e23e00b3b498; + c1e297c0c761777d8a76fff02d22b859 f4c76c1d47fe568f 62f9e58b0364b0b6; + 23c807a2ef0a551f858fd52d125a47d8 cab16583e15d0074 4563be765e74653a; + 9fa5d4d8cbdfcecd559e3ce88bf78ee1 97b05c60774164ad 714807767366854b; + 24f4e91083139099d663647a7ffac321 7017a263985b735c cc68e1ed7f7c7832; + 7cf8626de76d8462218ed5c9dc8acc26 15475a90df04cd29 416eab1ebc155782; + 3c6ad7df17332a3e1e3de06007387357 87ca0aaccf146cc8 ded575554058ef42; + 24237a762d8a89383a2949f5a0f5ec08 9268af048ecbae67 899b9b877106c764; + 209a2833a39472d9720380ffb2f74761 af0062cd77a84ebd 26db40c151b74530; + 22ebe7a30e8855b3b453a6e926d4f187 f922ca875b991a31 2763f255636a34c1; + 1b5e23ebd915c1fee59f57dd91af7347 b4ecc305c3dbd8e5 fbbec8f5dbf4cefd; + 6c21d105515290da29339f142685c135 a4c2f6dc597d13ea 7409829de05ae6d1; + 01f728a7433a47b13de34fcc08958449 3af7f1ae0f58ab1d 950083fbd332a987; + 9a2d4c50337d3ec337673782fd385601 791d8da8cbb0bc94 13f87f8d3326553f; + b3b99747560e246bcd0cdc3fb33e4910 91778f2bd9233375 2b1f2747b356e119; + 4b269e2e8dbd68b94066c95244c3a195 abfb6a33e1471e96 8ce0dc718e757924; + d10b956f675dc2276b6dd90442bece3e dbedddb6a1c15a21 19df78dea26ea579; + 28f8fab7541766f6dafbaa5b6f3bd700 2c14d5097ab11f61 3a6fd0a5fef50079; + 2b61e60478fa0c0edb9531f26791eff9 b84c80b2d245fdfc f24ea37046b97a03; + a6bfb85fa91b8ca4197c8b502a62f972 3daadd7a9633e19b 4de5c07ea1564a64; + 94e76e54d37f932cbcc069de6bf26d52 02739eeb468450fd 0d4ef5d93f419069; + 82760ea1560e8f9d1e35f20cad1f26cc 34aa5952b5a9110f 7676f2746bfac3b7; + 3aba91211327585e0f1270b59ef9277b b584d6b34b638ba4 2cfa1f17178df25d; + 727074f35cd523eb6f8aafbbf76fd5b1 f0e9268a27ab70c6 609aee69ed6e2a8e; + 6e0aa1c91ea6315a4a7dd297ac5936a4 47374080f27fbb30 5d86f6451024e051; + 63bd2d37271a250c8593b433188cffd0 776927909196eec5 ec617cc3810e278c; + dc7beb423c2053156329d7ab79f4d1d6 24c0adb3777860ad 7f6f6083713a68ba; + ed219454883709efe884bbc9d30507a3 587b1683dff7f178 86a95a8f48fa13b0; + 40509fc5c565e9b9f3995f3bc5d5c1f4 719a39f2302a98aa 503f9481c7049890; + f9f10ed2bc0193605913475fd54d69ae e35b94e2940a8db0 59bc9b0bd5004dae; + 23003c3fbe9661b6b9386748982ac3e2 d1dfd47fac22b9ff 756ec1ac4fa8deac; + a2caa563c2f19b10fbac1427e2177858 f7743dfe957cc82b 57ae9c0144dbb9d3; + 8bb42b6937d65eac506e8326356071af 690a0fa32113bf34 5136052b310e7038; + 219c7f982a0c352977b978b91fcb3c45 8fb4cb86db41ce30 fa01561e5b9311ab; + 157ff76f22538eede0528c541dbe7815 ea6cd44f238be18f 0c3f632af5cee432; + 346194289f6d865237cc673c45415003 71690fd4ba3d75de c4448db8c1144435; + 032c8c1624f7f300d8a58d151de52d78 3c0f1746798ed5b7 e1bfbd2a7d414c8a; + 019471106a5cd4ee3d1f627213649b80 558a8d7a4cdc0822 c90e5257ea513ef8; + 45aa90191c449dbf9c541fb476d1bf74 bfba3dfd06e77671 bc715327ada1b56b; + c86c21d8c26dc291f662c8f2fe79b74b 0993d3b68c1d4a5d 2b4195c3d67e3f99; + af4f4615c7c298639b9728251991419f 1e268f9e710313b5 2536ccd8fdfe30e1; +} diff --git a/tests/xtea-test.c b/tests/xtea-test.c new file mode 100644 index 0000000..4880905 --- /dev/null +++ b/tests/xtea-test.c @@ -0,0 +1,60 @@ +#include +#include + +/* --- Needham and Wheeler's original code --- * + * + * Almost. I changed the types from long to unsigned long. + */ + +void tean(unsigned long * v, unsigned long * k, long N) { +unsigned long y=v[0], z=v[1], DELTA=0x9e3779b9 ; +if (N>0) { + /* coding */ + unsigned long limit=DELTA*N, sum=0 ; + while (sum!=limit) + y+= (z<<4 ^ z>>5) + z ^ sum + k[sum&3], + sum+=DELTA, + z+= (y<<4 ^ y>>5) + y ^ sum + k[sum>>11 &3] ; + } +else { + + /* decoding */ + unsigned long sum=DELTA*(-N) ; + while (sum) + z-= (y<<4 ^ y>>5) + y ^ sum + k[sum>>11 &3], + sum-=DELTA, + y-= (z<<4 ^ z>>5) + z ^ sum + k[sum&3]; + } +v[0]=y, v[1]=z ; +return ; +} + +int main(void) +{ + unsigned long k[4] = { 0x00112233, 0x44556677, 0x8899aabb, 0xccddeeff }; + unsigned long p[2] = { 0x01234567, 0x89abcdef }; + fibrand f; + + int i; + + printf(" %08lx%08lx%08lx%08lx %08lx%08lx ", + k[0], k[1], k[2], k[3], p[0], p[1]); + tean(p, k, 32); + printf("%08lx%08lx;\n", p[0], p[1]); + + fibrand_lcseed(&f, 0); + for (i = 1; i < 64; i++) { + k[0] = fibrand_step(&f); + k[1] = fibrand_step(&f); + k[2] = fibrand_step(&f); + k[3] = fibrand_step(&f); + p[0] = fibrand_step(&f); + p[1] = fibrand_step(&f); + printf(" %08lx%08lx%08lx%08lx %08lx%08lx ", + k[0], k[1], k[2], k[3], p[0], p[1]); + tean(p, k, 32); + printf("%08lx%08lx;\n", p[0], p[1]); + } + + return (0); +} diff --git a/xtea.c b/xtea.c new file mode 100644 index 0000000..1d65c4d --- /dev/null +++ b/xtea.c @@ -0,0 +1,129 @@ +/* -*-c-*- + * + * $Id: xtea.c,v 1.1 2000/07/15 13:44:31 mdw Exp $ + * + * The Extended Tiny Encryption Algorithm + * + * (c) 2000 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. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: xtea.c,v $ + * Revision 1.1 2000/07/15 13:44:31 mdw + * New ciphers. + * + */ + +/*----- Header files ------------------------------------------------------*/ + +#include + +#include "blkc.h" +#include "gcipher.h" +#include "paranoia.h" +#include "xtea.h" + +/*----- Global variables --------------------------------------------------*/ + +const octet xtea_keysz[] = { KSZ_RANGE, XTEA_KEYSZ, 0, 16, 1 }; + +/*----- Main code ---------------------------------------------------------*/ + +/* --- @xtea_init@ --- * + * + * Arguments: @xtea_ctx *k@ = pointer to key block + * @const void *buf@ = pointer to key buffer + * @size_t sz@ = size of key material + * + * Returns: --- + * + * Use: Initializes an XTEA key buffer. The key buffer must be 16 + * bytes long. + */ + +void xtea_init(xtea_ctx *k, const void *buf, size_t sz) +{ + octet kb[16]; + const octet *p; + + KSZ_ASSERT(xtea, sz); + if (sz >= sizeof(kb)) + p = buf; + else { + memcpy(kb, buf, sz); + memset(kb + sz, 0, sizeof(kb) - sz); + p = kb; + } + + k->k[0] = LOAD32(p + 0); k->k[1] = LOAD32(p + 4); + k->k[2] = LOAD32(p + 8); k->k[3] = LOAD32(p + 12); + + if (p == kb) + BURN(kb); +} + +/* --- @xtea_eblk@, @xtea_dblk@ --- * + * + * Arguments: @const xtea_ctx *k@ = pointer to key block + * @const uint32 s[2]@ = pointer to source block + * @uint32 d[2]@ = pointer to xteatination block + * + * Returns: --- + * + * Use: Low-level block encryption and decryption. + */ + +#define DELTA 0x9e3779b9 + +void xtea_eblk(const xtea_ctx *k, const uint32 *s, uint32 *d) +{ + uint32 y = s[0], z = s[1]; + uint32 n = 0; + unsigned i; + + for (i = 0; i < 32; i++) { + y = U32(y + ((((z << 4) ^ (z >> 5)) + z) ^ (n + k->k[n & 3]))); + n += DELTA; + z = U32(z + ((((y << 4) ^ (y >> 5)) + y) ^ (n + k->k[(n >> 11) & 3]))); + } + d[0] = y; d[1] = z; +} + +void xtea_dblk(const xtea_ctx *k, const uint32 *s, uint32 *d) +{ + uint32 y = s[0], z = s[1]; + uint32 n = DELTA << 5; + unsigned i; + + for (i = 0; i < 32; i++) { + z = U32(z - ((((y << 4) ^ (y >> 5)) + y) ^ (n + k->k[(n >> 11) & 3]))); + n -= DELTA; + y = U32(y - ((((z << 4) ^ (z >> 5)) + z) ^ (n + k->k[n & 3]))); + } + d[0] = y; d[1] = z; +} + +BLKC_TEST(XTEA, xtea) + +/*----- That's all, folks -------------------------------------------------*/ diff --git a/xtea.h b/xtea.h new file mode 100644 index 0000000..e75b7c4 --- /dev/null +++ b/xtea.h @@ -0,0 +1,113 @@ +/* -*-c-*- + * + * $Id: xtea.h,v 1.1 2000/07/15 13:44:31 mdw Exp $ + * + * The Tiny Encryption Algorithm + * + * (c) 2000 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. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: xtea.h,v $ + * Revision 1.1 2000/07/15 13:44:31 mdw + * New ciphers. + * + */ + +/*----- Notes on the Tiny Encryption Algorithm ----------------------------* + * + * XTEA is an amazingly simple 64-round Feistel network. It's tiny, fairly + * quick and surprisingly strong. It was invented by David Wheeler and Roger + * Needham. It's unpatented. XTEA is a new version of TEA, by the same + * designers, which fixes some weaknesses in TEA's key schedule. + * + * This implementation uses big-endian byte order, following SCAN. + */ + +#ifndef CATACOMB_XTEA_H +#define CATACOMB_XTEA_H + +#ifdef __cplusplus + extern "C" { +#endif + +/*----- Header files ------------------------------------------------------*/ + +#include + +#include + +/*----- Magical numbers ---------------------------------------------------*/ + +#define XTEA_BLKSZ 8 +#define XTEA_KEYSZ 16 +#define XTEA_CLASS (N, B, 64) + +extern const octet xtea_keysz[]; + +/*----- Data structures ---------------------------------------------------*/ + +typedef struct xtea_ctx { + uint32 k[4]; +} xtea_ctx; + +/*----- Functions provided ------------------------------------------------*/ + +/* --- @xtea_init@ --- * + * + * Arguments: @xtea_ctx *k@ = pointer to key block + * @const void *buf@ = pointer to key buffer + * @size_t sz@ = size of key material + * + * Returns: --- + * + * Use: Initializes an XTEA key buffer. The key buffer may be up to + * 16 bytes long. + */ + +extern void xtea_init(xtea_ctx */*k*/, const void */*buf*/, size_t /*sz*/); + +/* --- @xtea_eblk@, @xtea_dblk@ --- * + * + * Arguments: @const xtea_ctx *k@ = pointer to key block + * @const uint32 s[2]@ = pointer to source block + * @uint32 d[2]@ = pointer to xteatination block + * + * Returns: --- + * + * Use: Low-level block encryption and decryption. + */ + +extern void xtea_eblk(const xtea_ctx */*k*/, + const uint32 */*s*/, uint32 */*d*/); +extern void xtea_dblk(const xtea_ctx */*k*/, + const uint32 */*s*/, uint32 */*d*/); + +/*----- That's all, folks -------------------------------------------------*/ + +#ifdef __cplusplus + } +#endif + +#endif