summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.cvsignore4
-rw-r--r--.gitignore4
-rw-r--r--ChangeLog14
-rw-r--r--aes.c2
-rw-r--r--des.c236
-rw-r--r--des.h68
-rw-r--r--desCode.h102
-rw-r--r--desdata.c11
-rw-r--r--desinfo.h3
-rw-r--r--testsuite/.cvsignore4
-rw-r--r--testsuite/.gitignore4
11 files changed, 393 insertions, 59 deletions
diff --git a/.cvsignore b/.cvsignore
index f8189aa9..04091fb9 100644
--- a/.cvsignore
+++ b/.cvsignore
@@ -8,5 +8,9 @@ config.h.in
config.log
config.status
configure
+desdata
+keymap.h
+parity.h
+rotors.h
stamp-h
stamp-h.in
diff --git a/.gitignore b/.gitignore
index 1c078812..d3bda8de 100644
--- a/.gitignore
+++ b/.gitignore
@@ -40,5 +40,9 @@ core
/config.log
/config.status
/configure
+/desdata
+/keymap.h
+/parity.h
+/rotors.h
/stamp-h
/stamp-h.in
diff --git a/ChangeLog b/ChangeLog
index a963209c..72851de6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2001-06-12 Niels Möller <nisse@cuckoo.hack.org>
+
+ * testsuite/Makefile.am (TS_PROGS): Added tests for des and sha1.
+
+ * testsuite/sha1-test.m4: New file.
+
+ * testsuite/des-test.m4: New file.
+
+ * Added SHA1 files.
+
+ * Added desCore files.
+
+ * Makefile.am: Added desCore and sha1.
+
2001-04-17 Niels Möller <nisse@cuckoo.hack.org>
* install-sh: Copied the standard install script.
diff --git a/aes.c b/aes.c
index e0f97f9b..b4df1beb 100644
--- a/aes.c
+++ b/aes.c
@@ -1,4 +1,4 @@
-/* aes.h
+/* aes.c
*
* The aes/rijndael block cipher.
*/
diff --git a/des.c b/des.c
new file mode 100644
index 00000000..365113c0
--- /dev/null
+++ b/des.c
@@ -0,0 +1,236 @@
+/* des.c
+ *
+ * The des block cipher.
+ *
+ * $Id$
+ */
+
+/* nettle, low-level cryptographics library
+ *
+ * Copyright (C) 2001 Niels Möller
+ *
+ * The nettle library is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or (at your
+ * option) any later version.
+ *
+ * The GNU MP Library 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 Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with the GNU MP Library; see the file COPYING.LIB. If not, write to
+ * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/* des - fast & portable DES encryption & decryption.
+ * Copyright (C) 1992 Dana L. How
+ * Please see the file `descore.README' for the complete copyright notice.
+ */
+
+#include "des.h"
+
+#include "desCode.h"
+
+#include <assert.h>
+
+static ENCRYPT(DesSmallFipsEncrypt,TEMPSMALL, LOADFIPS,KEYMAPSMALL,SAVEFIPS)
+static DECRYPT(DesSmallFipsDecrypt,TEMPSMALL, LOADFIPS,KEYMAPSMALL,SAVEFIPS)
+
+/* various tables */
+
+uint32_t des_keymap[] = {
+#include "keymap.h"
+};
+
+static uint8_t rotors[] = {
+#include "rotors.h"
+};
+static char parity[] = {
+#include "parity.h"
+};
+
+int
+des_set_key(struct des_ctx *ctx, const uint8_t *key)
+{
+ register uint32_t n, w;
+ register char * b0, * b1;
+ char bits0[56], bits1[56];
+ uint32_t *method;
+ uint8_t *k;
+
+ /* check for bad parity and weak keys */
+ b0 = parity;
+ n = b0[key[0]]; n <<= 4;
+ n |= b0[key[1]]; n <<= 4;
+ n |= b0[key[2]]; n <<= 4;
+ n |= b0[key[3]]; n <<= 4;
+ n |= b0[key[4]]; n <<= 4;
+ n |= b0[key[5]]; n <<= 4;
+ n |= b0[key[6]]; n <<= 4;
+ n |= b0[key[7]];
+ w = 0x88888888l;
+ /* report bad parity in key */
+ if ( n & w )
+ {
+ ctx->status = DES_BAD_PARITY;
+ return 0;
+ }
+ ctx->status = DES_WEAK_KEY;
+ /* report a weak or semi-weak key */
+ if ( !((n - (w >> 3)) & w) ) { /* 1 in 10^10 keys passes this test */
+ if ( n < 0X41415151 ) {
+ if ( n < 0X31312121 ) {
+ if ( n < 0X14141515 ) {
+ /* 01 01 01 01 01 01 01 01 */
+ if ( n == 0X11111111 ) return 0;
+ /* 01 1F 01 1F 01 0E 01 0E */
+ if ( n == 0X13131212 ) return 0;
+ } else {
+ /* 01 E0 01 E0 01 F1 01 F1 */
+ if ( n == 0X14141515 ) return 0;
+ /* 01 FE 01 FE 01 FE 01 FE */
+ if ( n == 0X16161616 ) return 0;
+ }
+ } else {
+ if ( n < 0X34342525 ) {
+ /* 1F 01 1F 01 0E 01 0E 01 */
+ if ( n == 0X31312121 ) return 0;
+ /* 1F 1F 1F 1F 0E 0E 0E 0E */ /* ? */
+ if ( n == 0X33332222 ) return 0;;
+ } else {
+ /* 1F E0 1F E0 0E F1 0E F1 */
+ if ( n == 0X34342525 ) return 0;;
+ /* 1F FE 1F FE 0E FE 0E FE */
+ if ( n == 0X36362626 ) return 0;;
+ }
+ }
+ } else {
+ if ( n < 0X61616161 ) {
+ if ( n < 0X44445555 ) {
+ /* E0 01 E0 01 F1 01 F1 01 */
+ if ( n == 0X41415151 ) return 0;
+ /* E0 1F E0 1F F1 0E F1 0E */
+ if ( n == 0X43435252 ) return 0;
+ } else {
+ /* E0 E0 E0 E0 F1 F1 F1 F1 */ /* ? */
+ if ( n == 0X44445555 ) return 0;
+ /* E0 FE E0 FE F1 FE F1 FE */
+ if ( n == 0X46465656 ) return 0;
+ }
+ } else {
+ if ( n < 0X64646565 ) {
+ /* FE 01 FE 01 FE 01 FE 01 */
+ if ( n == 0X61616161 ) return 0;
+ /* FE 1F FE 1F FE 0E FE 0E */
+ if ( n == 0X63636262 ) return 0;
+ } else {
+ /* FE E0 FE E0 FE F1 FE F1 */
+ if ( n == 0X64646565 ) return 0;
+ /* FE FE FE FE FE FE FE FE */
+ if ( n == 0X66666666 ) return 0;
+ }
+ }
+ }
+ }
+
+ /* key is ok */
+ ctx->status = DES_OK;
+
+ /* explode the bits */
+ n = 56;
+ b0 = bits0;
+ b1 = bits1;
+ do {
+ w = (256 | *key++) << 2;
+ do {
+ --n;
+ b1[n] = 8 & w;
+ w >>= 1;
+ b0[n] = 4 & w;
+ } while ( w >= 16 );
+ } while ( n );
+
+ /* put the bits in the correct places */
+ n = 16;
+ k = rotors;
+ method = ctx->key;
+
+ do {
+ w = (b1[k[ 0 ]] | b0[k[ 1 ]]) << 4;
+ w |= (b1[k[ 2 ]] | b0[k[ 3 ]]) << 2;
+ w |= b1[k[ 4 ]] | b0[k[ 5 ]];
+ w <<= 8;
+ w |= (b1[k[ 6 ]] | b0[k[ 7 ]]) << 4;
+ w |= (b1[k[ 8 ]] | b0[k[ 9 ]]) << 2;
+ w |= b1[k[10 ]] | b0[k[11 ]];
+ w <<= 8;
+ w |= (b1[k[12 ]] | b0[k[13 ]]) << 4;
+ w |= (b1[k[14 ]] | b0[k[15 ]]) << 2;
+ w |= b1[k[16 ]] | b0[k[17 ]];
+ w <<= 8;
+ w |= (b1[k[18 ]] | b0[k[19 ]]) << 4;
+ w |= (b1[k[20 ]] | b0[k[21 ]]) << 2;
+ w |= b1[k[22 ]] | b0[k[23 ]];
+
+ method[0] = w;
+
+ w = (b1[k[ 0+24]] | b0[k[ 1+24]]) << 4;
+ w |= (b1[k[ 2+24]] | b0[k[ 3+24]]) << 2;
+ w |= b1[k[ 4+24]] | b0[k[ 5+24]];
+ w <<= 8;
+ w |= (b1[k[ 6+24]] | b0[k[ 7+24]]) << 4;
+ w |= (b1[k[ 8+24]] | b0[k[ 9+24]]) << 2;
+ w |= b1[k[10+24]] | b0[k[11+24]];
+ w <<= 8;
+ w |= (b1[k[12+24]] | b0[k[13+24]]) << 4;
+ w |= (b1[k[14+24]] | b0[k[15+24]]) << 2;
+ w |= b1[k[16+24]] | b0[k[17+24]];
+ w <<= 8;
+ w |= (b1[k[18+24]] | b0[k[19+24]]) << 4;
+ w |= (b1[k[20+24]] | b0[k[21+24]]) << 2;
+ w |= b1[k[22+24]] | b0[k[23+24]];
+
+ ROR(w, 4, 28); /* could be eliminated */
+ method[1] = w;
+
+ k += 48;
+ method += 2;
+ } while ( --n );
+
+ return 1;
+}
+
+void
+des_encrypt(struct des_ctx *ctx,
+ unsigned length, uint8_t *dst,
+ const uint8_t *src)
+{
+ assert(!(length % DES_BLOCK_SIZE));
+
+ while (length)
+ {
+ DesSmallFipsEncrypt(dst, ctx->key, src);
+ length -= DES_BLOCK_SIZE;
+ src += DES_BLOCK_SIZE;
+ dst += DES_BLOCK_SIZE;
+ }
+}
+
+void
+des_decrypt(struct des_ctx *ctx,
+ unsigned length, uint8_t *dst,
+ const uint8_t *src)
+{
+ assert(!(length % DES_BLOCK_SIZE));
+
+ while (length)
+ {
+ DesSmallFipsDecrypt(dst, ctx->key, src);
+ length -= DES_BLOCK_SIZE;
+ src += DES_BLOCK_SIZE;
+ dst += DES_BLOCK_SIZE;
+ }
+}
diff --git a/des.h b/des.h
new file mode 100644
index 00000000..3b713429
--- /dev/null
+++ b/des.h
@@ -0,0 +1,68 @@
+/* des.h
+ *
+ * The des block cipher.
+ */
+
+/* nettle, low-level cryptographics library
+ *
+ * Copyright (C) 1992, 2001, Dana L. How, Niels Möller
+ *
+ * The nettle library is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or (at your
+ * option) any later version.
+ *
+ * The GNU MP Library 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 Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with the GNU MP Library; see the file COPYING.LIB. If not, write to
+ * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*
+ * des - fast & portable DES encryption & decryption.
+ * Copyright (C) 1992 Dana L. How
+ * Please see the file `../lib/descore.README' for the complete copyright
+ * notice.
+ *
+ * Slightly edited by Niels Möller, 1997
+ */
+
+#ifndef NETTLE_DES_H
+#define NETTLE_DES_H
+
+#include <stdint.h>
+
+#define DES_KEY_SIZE 8
+#define DES_BLOCK_SIZE 8
+
+/* Expanded key length */
+#define _DES_KEY_LENGTH 32
+
+enum des_error { DES_OK, DES_BAD_PARITY, DES_WEAK_KEY };
+
+struct des_ctx
+{
+ uint32_t key[_DES_KEY_LENGTH];
+ enum des_error status;
+};
+
+/* On success, returns 1 and sets ctx->status to DES_OK. On error,
+ * returns 0 and sets ctx->status accordingly. */
+int
+des_set_key(struct des_ctx *ctx, const uint8_t *key);
+
+void
+des_encrypt(struct des_ctx *ctx,
+ unsigned length, uint8_t *dst,
+ const uint8_t *src);
+void
+des_decrypt(struct des_ctx *ctx,
+ unsigned length, uint8_t *dst,
+ const uint8_t *src);
+
+#endif /* NETTLE_DES_H */
diff --git a/desCode.h b/desCode.h
index 2334628f..d27df606 100644
--- a/desCode.h
+++ b/desCode.h
@@ -9,10 +9,8 @@
#include "des.h"
-#include "RCSID.h"
-RCSID2(desCode_hRcs, "$Id$");
-
-extern UINT32 des_keymap[], des_bigmap[];
+extern uint32_t des_keymap[];
+extern uint32_t des_bigmap[];
/* optional customization:
* the idea here is to alter the code so it will still run correctly
@@ -50,17 +48,17 @@ extern UINT32 des_keymap[], des_bigmap[];
/* The BYTE type is used as parameter for the encrypt/decrypt functions.
* It's pretty bad to have the function prototypes depend on
* a macro definition that the users of the function doesn't
- * know about. - Niels */
+ * know about. /Niels */
#if 0 /* didn't feel like deleting */
-#define SREGFREE ; s = (UINT8 *) D
+#define SREGFREE ; s = (uint8_t *) D
#define DEST s
#define D m0
-#define BYTE UINT32
+#define BYTE uint32_t
#else
#define SREGFREE
#define DEST d
#define D d
-#define BYTE UINT8
+#define BYTE uint8_t
#endif
/* handle constants in the optimal way for 386 & vax */
@@ -73,12 +71,12 @@ extern UINT32 des_keymap[], des_bigmap[];
#define MQ1 (des_bigmap + 64)
#define MQ2 (des_bigmap + 128)
#define MQ3 (des_bigmap + 192)
-#define HQ0(z) /* z |= 0X01000000L; */
-#define HQ2(z) /* z |= 0X03000200L; */
-#define LQ0(z) 0XFCFC & z
-#define LQ1(z) 0XFCFC & z
-#define LQ2(z) 0XFCFC & z
-#define LQ3(z) 0XFCFC & z
+#define HQ0(z) /* z |= 0x01000000L; */
+#define HQ2(z) /* z |= 0x03000200L; */
+#define LQ0(z) 0xFCFC & z
+#define LQ1(z) 0xFCFC & z
+#define LQ2(z) 0xFCFC & z
+#define LQ3(z) 0xFCFC & z
#define SQ 16
#define MS0 des_keymap
#define MS1 (des_keymap + 64)
@@ -89,10 +87,10 @@ extern UINT32 des_keymap[], des_bigmap[];
#define MS6 (des_keymap + 384)
#define MS7 (des_keymap + 448)
#define HS(z)
-#define LS0(z) 0XFC & z
-#define LS1(z) 0XFC & z
-#define LS2(z) 0XFC & z
-#define LS3(z) 0XFC & z
+#define LS0(z) 0xFC & z
+#define LS1(z) 0xFC & z
+#define LS2(z) 0xFC & z
+#define LS3(z) 0xFC & z
#define REGQUICK
#define SETQUICK
#define REGSMALL
@@ -133,10 +131,10 @@ extern UINT32 des_keymap[], des_bigmap[];
#define LS2(z) k1 & z
#define LS3(z) k2 & z
#define REGQUICK \
- register UINT32 k0, k1; \
- register UINT32 *m0, *m1, *m2, *m3;
+ register uint32_t k0, k1; \
+ register uint32_t *m0, *m1, *m2, *m3;
#define SETQUICK \
- ; k0 = 0XFCFC \
+ ; k0 = 0xFCFC \
; k1 = 16 \
/*k2 = 28 to speed up ROL */ \
; m0 = des_bigmap \
@@ -144,12 +142,12 @@ extern UINT32 des_keymap[], des_bigmap[];
; m2 = m1 + 64 \
; m3 = m2 + 64
#define REGSMALL \
- register UINT32 k0, k1, k2; \
- register UINT32 *m0, *m1, *m2, *m3;
+ register uint32_t k0, k1, k2; \
+ register uint32_t *m0, *m1, *m2, *m3;
#define SETSMALL \
- ; k0 = 0X01000100L \
- ; k1 = 0X0FC \
- ; k2 = 0X1FC \
+ ; k0 = 0x01000100L \
+ ; k1 = 0x0FC \
+ ; k2 = 0x1FC \
; m0 = des_keymap \
; m1 = m0 + 128 \
; m2 = m1 + 128 \
@@ -185,21 +183,21 @@ extern UINT32 des_keymap[], des_bigmap[];
#define MS6 m6
#define MS7 m7
#define HS(z)
-#define LS0(z) 0XFC & z
-#define LS1(z) 0XFC & z
-#define LS2(z) 0XFC & z
-#define LS3(z) 0XFC & z
+#define LS0(z) 0xFC & z
+#define LS1(z) 0xFC & z
+#define LS2(z) 0xFC & z
+#define LS3(z) 0xFC & z
#define REGQUICK \
- register UINT32 k0; \
- register UINT32 *m0, *m1, *m2, *m3;
+ register uint32_t k0; \
+ register uint32_t *m0, *m1, *m2, *m3;
#define SETQUICK \
- ; k0 = 0XFCFC \
+ ; k0 = 0xFCFC \
; m0 = des_bigmap \
; m1 = m0 + 64 \
; m2 = m1 + 64 \
; m3 = m2 + 64
#define REGSMALL \
- register UINT32 *m0, *m1, *m2, *m3, *m4, *m5, *m6, *m7;
+ register uint32_t *m0, *m1, *m2, *m3, *m4, *m5, *m6, *m7;
#define SETSMALL \
; m0 = des_keymap \
; m1 = m0 + 64 \
@@ -215,7 +213,9 @@ extern UINT32 des_keymap[], des_bigmap[];
/* some basic stuff */
/* generate addresses from a base and an index */
-#define ADD(b,x) (UINT32 *) ((UINT8 *)b + (x))
+/* FIXME: This is used only as *ADD(msi,lsi(z)) or *ADD(mqi,lqi(z)).
+ * Why not use plain indexing instead? /Niels */
+#define ADD(b,x) (uint32_t *) ((uint8_t *)b + (x))
/* low level rotate operations */
#define NOP(d,c,o)
@@ -263,12 +263,12 @@ extern UINT32 des_keymap[], des_bigmap[];
/* load data, do the initial permutation and put into efficient position */
#define LOADFIPS() \
LOADDATA(y, x); \
- SWAP(x, y, 0X0F0F0F0FL, 004); \
- SWAP(y, x, 0X0000FFFFL, 020); \
- SWAP(x, y, 0X33333333L, 002); \
- SWAP(y, x, 0X00FF00FFL, 010); \
+ SWAP(x, y, 0x0F0F0F0FL, 004); \
+ SWAP(y, x, 0x0000FFFFL, 020); \
+ SWAP(x, y, 0x33333333L, 002); \
+ SWAP(y, x, 0x00FF00FFL, 010); \
ROR1(x); \
- z = (x ^ y) & 0X55555555L; \
+ z = (x ^ y) & 0x55555555L; \
y ^= z; \
x ^= z; \
ROR1(y)
@@ -328,14 +328,14 @@ extern UINT32 des_keymap[], des_bigmap[];
/* do final permutation and write out result */
#define SAVEFIPS() \
ROL1(x); \
- z = (x ^ y) & 0X55555555L; \
+ z = (x ^ y) & 0x55555555L; \
y ^= z; \
x ^= z; \
ROL1(y); \
- SWAP(x, y, 0X00FF00FFL, 010); \
- SWAP(y, x, 0X33333333L, 002); \
- SWAP(x, y, 0X0000FFFFL, 020); \
- SWAP(y, x, 0X0F0F0F0FL, 004); \
+ SWAP(x, y, 0x00FF00FFL, 010); \
+ SWAP(y, x, 0x33333333L, 002); \
+ SWAP(x, y, 0x0000FFFFL, 020); \
+ SWAP(y, x, 0x0F0F0F0FL, 004); \
SAVEDATA(x, y)
@@ -345,10 +345,10 @@ extern UINT32 des_keymap[], des_bigmap[];
\
void \
NAME(REGISTER BYTE *D, \
- REGISTER const UINT32 *r, \
- REGISTER const UINT8 *s) \
+ REGISTER const uint32_t *r, \
+ REGISTER const uint8_t *s) \
{ \
- register UINT32 x, y, z; \
+ register uint32_t x, y, z; \
\
/* declare temps & load data */ \
TEMP(LOAD); \
@@ -381,10 +381,10 @@ NAME(REGISTER BYTE *D, \
\
void \
NAME(REGISTER BYTE *D, \
- REGISTER const UINT32 *r, \
- REGISTER const UINT8 *s) \
+ REGISTER const uint32_t *r, \
+ REGISTER const uint8_t *s) \
{ \
- register UINT32 x, y, z; \
+ register uint32_t x, y, z; \
\
/* declare temps & load data */ \
TEMP(LOAD); \
diff --git a/desdata.c b/desdata.c
index 293ff644..c078e86e 100644
--- a/desdata.c
+++ b/desdata.c
@@ -1,6 +1,6 @@
/* desdata.c
*
- * Generate tables used by desUtil.c and desCode.h.
+ * Generate tables used by des.c and desCode.h.
*
* $Id$ */
@@ -15,8 +15,11 @@
#include "desCode.h"
-#include "RCSID.h"
-RCSID2(desdata_cRcs, "$Id$");
+#if __GNUC__
+# define UNUSED __attribute__ ((__unused__))
+#else
+# define UNUSED
+#endif
/* list of weak and semi-weak keys
@@ -65,7 +68,7 @@ int printf(const char *, ...);
int
main(int argc UNUSED, char **argv UNUSED)
{
- UINT32 d, i, j, k, l, m, n, s;
+ uint32_t d, i, j, k, l, m, n, s;
char b[256], ksr[56];
switch ( argv[1][0] ) {
diff --git a/desinfo.h b/desinfo.h
index 8920caa1..0c4ce3b6 100644
--- a/desinfo.h
+++ b/desinfo.h
@@ -10,9 +10,6 @@
* Please see the file `descore.README' for the complete copyright notice.
*/
-#include "RCSID.h"
-RCSID2(desinfo_hRcs, "$Id$");
-
/* the initial permutation, E selection, and final permutation are hardwired */
/* Key Load: how to load the shift register from the user key */
diff --git a/testsuite/.cvsignore b/testsuite/.cvsignore
index 64bd63f8..a69f0289 100644
--- a/testsuite/.cvsignore
+++ b/testsuite/.cvsignore
@@ -5,5 +5,9 @@ aes-test
aes-test.c
arcfour-test
arcfour-test.c
+des-test
+des-test.c
md5-test
md5-test.c
+sha1-test
+sha1-test.c
diff --git a/testsuite/.gitignore b/testsuite/.gitignore
index 8b360324..db38a971 100644
--- a/testsuite/.gitignore
+++ b/testsuite/.gitignore
@@ -5,5 +5,9 @@
/aes-test.c
/arcfour-test
/arcfour-test.c
+/des-test
+/des-test.c
/md5-test
/md5-test.c
+/sha1-test
+/sha1-test.c