teatotal: Thread: "Read error" building for iPAQ running Linux


[<<] [<] Page 1 of 1 [>] [>>]
Subject: "Read error" building for iPAQ running Linux
From: Jay Sekora ####@####.####
Date: 12 Nov 2001 05:10:44 -0000
Message-Id: <333.1005541821@js.aq.org>

Hi.  I'm trying to build Tea Total for the Compaq iPAQ running Linux.
That's a StrongARM-110r4 processor running in little-endian mode.
There's a /usr/include/asm/types.h which includes the following:

typedef __signed__ char __s8;
typedef unsigned char __u8;

typedef __signed__ short __s16;
typedef unsigned short __u16;

typedef __signed__ int __s32;
typedef unsigned int __u32;

#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
typedef __signed__ long long __s64;
typedef unsigned long long __u64;
#endif

and there's an endian.h, so I'd expect that just using "ARCH = LINUX"
in config.inc ought to work.  (I also tried "ARCH = UNKNOWN_LITTLE" 
with the same results.)

The actual compilation goes smoothly and there are no errors in "./tea
test.key test.in testout.tea", but then the ./untea dies with "Read error".

I tried debugging it under gdb.  It's been a while since I really 
understood how to use a debugger, but I *think* what gdb is telling 
me is that the line in read_header() in teaprot.c that reads
        /* Check for the TEA magic number first */
        if(memcmp(state->pkt->hdr, TEA_HEADER, HEADER_LEN)) return -1;
is in fact matching and returning -1.  (I'm afraid I don't understand 
the logic here - it looks to me like the code is checking for "TEA" 
at the beginning of a file, returning -1 when it sees it, and the 
calling code expects -1 to be an error.  But I must be missing something.)

The last part of my debugging session is below.  Anyway, if what's 
going wrong is obvious to somebody on this list, I'd be very grateful 
to hear it.  Otherwise, is this likely actually to be an architecture-related 
problem?  Or might there be something wonky with the development environment 
on the somewhat strange box I'm building this on (maybe the headers 
aren't correct, or something like that)?  I know I must sound like 
an idiot, but that's OK. :-)

In closing, thanks very much to Alex for writing the code and making 
it freely available!  I've been using Tea Total on my Agenda (another 
Linux PDA) for several months and it's been really handy.

Cheers,

-j.

     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y

Starting program: ####@####.#### test.key testout.tea test.out

Breakpoint 4, read_header (state=0x205f828) at teaprot.c:36
36	{
(gdb) step
45		i = safe_read(state->infd, state->pkt, HEADER_LEN + 4);
(gdb) step
safe_readwrite (fd=33554624, buf=0x14, len=33945640, r=0) at util.c:127
127		long pos = 0, bytes;
(gdb) finish
Run till exit from #0  safe_readwrite (fd=33554624, buf=0x14, len=33945640, 
    r=0) at util.c:127
read_header (state=0x205f828) at teaprot.c:46
46		if(!i) return 1;			/* End of file */
Value returned is $5 = 7
(gdb) print i
$6 = 7
(gdb) step
47		if(i != HEADER_LEN + 4) return -1;	/* Read error */
(gdb) print i
$7 = 7
(gdb) step
50		if(memcmp(state->pkt->hdr, TEA_HEADER, HEADER_LEN)) return -1;
(gdb) print i
$8 = -1
(gdb) print state
$9 = (teastate *) 0x205f828
(gdb) print state->pkt
$10 = (teapacket *) 0x205f8b8
(gdb) print state->pkt->hdr
$11 = "TEA"
(gdb) bt
#0  read_header (state=0x205f828) at teaprot.c:50
#1  0x2000af0 in decode_block (state=0x205f828) at teaprot.c:78
#2  0x20006e8 in do_tea_untea (state=0x205f828) at teauntea.c:307
#3  0x2000488 in main (argc=4, argv=0xbffffd14) at teatotal.c:212
#4  0x2002bd4 in __libc_start_main ()
(gdb) finish
Run till exit from #0  read_header (state=0x205f828) at teaprot.c:50
0x2000af0 in decode_block (state=0x205f828) at teaprot.c:78
78		if((i = read_header(state))) return i;
Value returned is $12 = -1
(gdb) step
140		return 0;	/* Success */
(gdb) step
141	}
(gdb) step
do_tea_untea (state=0x205f828) at teauntea.c:315
315		if(i == -1) die("Read error");
(gdb) step
die (msg=0x205f828 "\021dEü¥Uá\024f\226Þ\004\204\026\r\035u;\n") at util.c:88
88	{
(gdb) bt
#0  die (msg=0x205f828 "\021dEü¥Uá\024f\226Þ\004\204\026\r\035u;\n")
    at util.c:88
#1  0x2000714 in do_tea_untea (state=0x205f828) at teauntea.c:315
#2  0x2000488 in main (argc=4, argv=0xbffffd14) at teatotal.c:212
#3  0x2002bd4 in __libc_start_main ()
(gdb) finish
Run till exit from #0  die (
    msg=0x205f828 "\021dEü¥Uá\024f\226Þ\004\204\026\r\035u;\n") at util.c:88
Read error

Program exited with code 01.
(gdb) 
Subject: Re: "Read error" building for iPAQ running Linux
From: Alex Holden ####@####.####
Date: 18 Nov 2001 09:54:00 -0000
Message-Id: <Pine.LNX.4.33.0111180929550.763-100000@hyperspace.linuxhacker.org>

On Mon, 12 Nov 2001, Jay Sekora wrote:
> Hi.  I'm trying to build Tea Total for the Compaq iPAQ running Linux.
> That's a StrongARM-110r4 processor running in little-endian mode.
> There's a /usr/include/asm/types.h which includes the following:

Hi Jay, sorry for taking so long to get back to you. I've run Tea Total on
my NetWinder before, so it ought to work okay on ARM Linux.

> I tried debugging it under gdb.  It's been a while since I really
> understood how to use a debugger, but I *think* what gdb is telling
> me is that the line in read_header() in teaprot.c that reads
>         /* Check for the TEA magic number first */
>         if(memcmp(state->pkt->hdr, TEA_HEADER, HEADER_LEN)) return -1;
> is in fact matching and returning -1.  (I'm afraid I don't understand
> the logic here - it looks to me like the code is checking for "TEA"
> at the beginning of a file, returning -1 when it sees it, and the
> calling code expects -1 to be an error.  But I must be missing something.)

memcmp returns 0 if its arguments match, so that line only returns if the
memcmp returns non zero (ie. the header isn't "TEA"). The first five
letters of your testout.tea file should be "TEA1L", signifying an
uncompressed tea encrypted file with little endian encoding. Can you send
me a copy of your testout.tea? Perhaps you could also set DEBUGGING = Y
and  STATIC = Y in config.inc, then do a "make clean and send me the
resulting "tea" binary itself, and I'll see if I can find out what it's
doing wrong.

PS. I have found one bug since the 0.4 release. It's unlikely to be
causing your problem but you may want to apply this patch anyway:

diff -u tea-total-0.4/getarg.c tea-total-0.4-fixed/getarg.c
--- tea-total-0.4/getarg.c	Mon Jan  1 19:18:54 2001
+++ tea-total-0.4-fixed/getarg.c	Mon Jul  9 17:24:45 2001
@@ -110,7 +110,7 @@

 	/* Copy the arguments, ignoring the first argument (the program name) */
 	for(i = 1; i < argc; i++) {
-		len = strlen(argv[i]);
+		len = strlen(argv[i]) + 1;
 		if(!(state->argv[i - 1] = malloc(len))) {
 			/* It failed, so free the list and return */
 			while(i-- > 1) free(state->argv[i]);

-- 
------- Alex Holden -------
http://www.linuxhacker.org/
 http://www.robogeeks.org/

Subject: Re: "Read error" building for iPAQ running Linux
From: Alex Holden ####@####.####
Date: 18 Nov 2001 11:17:51 -0000
Message-Id: <Pine.LNX.4.33.0111181107350.763-100000@hyperspace.linuxhacker.org>

On Sun, 18 Nov 2001, Alex Holden wrote:
> Hi Jay, sorry for taking so long to get back to you. I've run Tea Total on
> my NetWinder before, so it ought to work okay on ARM Linux.

Oops, I just tried it on my NetWinder again and it seems that the reason
it worked before was that the kernel I was using then had the user mode
alignment trap handler. Newer kernels don't have it, and there was an
alignment bug in Tea Total 0.4. Does the following patch fix your
problems?

diff -urN tea-total-0.4/config.inc tea-total-0.4a/config.inc
--- tea-total-0.4/config.inc	Tue Jan  2 13:09:08 2001
+++ tea-total-0.4a/config.inc	Sun Nov 18 11:04:41 2001
@@ -38,7 +38,7 @@
 # platforms).
 DEBUGGING = N

-# Set to Y to build with -pedantic turned on (may not work on all platorms).
+# Set to Y to build with -pedantic turned on (may not work on all platforms).
 PEDANTIC = N

 # Set to Y to link statically (may not work on all platforms).
diff -urN tea-total-0.4/getarg.c tea-total-0.4a/getarg.c
--- tea-total-0.4/getarg.c	Mon Jan  1 19:18:54 2001
+++ tea-total-0.4a/getarg.c	Sun Nov 18 11:06:56 2001
@@ -110,7 +110,7 @@

 	/* Copy the arguments, ignoring the first argument (the program name) */
 	for(i = 1; i < argc; i++) {
-		len = strlen(argv[i]);
+		len = strlen(argv[i]) + 1;
 		if(!(state->argv[i - 1] = malloc(len))) {
 			/* It failed, so free the list and return */
 			while(i-- > 1) free(state->argv[i]);
diff -urN tea-total-0.4/md5.c tea-total-0.4a/md5.c
--- tea-total-0.4/md5.c	Sun Dec 24 20:29:54 2000
+++ tea-total-0.4a/md5.c	Sun Nov 18 11:01:59 2001
@@ -16,6 +16,8 @@
  *
  */

+#include <string.h>
+
 #include "arch.h"
 #include "md5.h"

diff -urN tea-total-0.4/tea-kgen.c tea-total-0.4a/tea-kgen.c
--- tea-total-0.4/tea-kgen.c	Mon Jan  1 19:19:17 2001
+++ tea-total-0.4a/tea-kgen.c	Sun Nov 18 11:01:48 2001
@@ -9,6 +9,7 @@
  * tea-kgen.c: The tea-kgen applet.
  */

+#include <string.h>
 #include <unistd.h>

 #include "teatotal.h"
diff -urN tea-total-0.4/teaprot.c tea-total-0.4a/teaprot.c
--- tea-total-0.4/teaprot.c	Mon Jan  1 22:50:45 2001
+++ tea-total-0.4a/teaprot.c	Sun Nov 18 11:00:51 2001
@@ -146,6 +146,7 @@
  */
 int encode_block(teastate *state)
 {
+	u8 *p;
 	int i, block_len, clen;

 	/* Copy the TEA header into the packet */
@@ -181,6 +182,16 @@

 	/* encrypt the data */
 	btea(&state->pkt->seq, state->k, block_len / 4, 0, 0);
+
+	/*
+	 * The seq field is aligned because the tea encoder requires that, but
+	 * that does mean that on some architectures there will be a byte (or more)
+	 * of packing between the header and the encoded data which we don't want
+	 * in the final encoded data block. So if necessary, shuffle the data
+	 * down to get rid of any packing.
+	 */
+	p = (u8 *)&state->pkt->datalen + 2;
+	if((u8 *)&state->pkt->seq > p) memmove(p, &state->pkt->seq, block_len);

 	/* The entire packet includes both the encrypted data and the header */
 	block_len += HEADER_LEN + 4;
diff -urN tea-total-0.4/teatotal.h tea-total-0.4a/teatotal.h
--- tea-total-0.4/teatotal.h	Mon Jan  1 20:44:42 2001
+++ tea-total-0.4a/teatotal.h	Sun Nov 18 10:59:51 2001
@@ -30,15 +30,19 @@
 #include "base64.h"
 #endif

-/* The packed attributes are needed to prevent gcc from aligning the structure
- * elements on certain architectures */
+/*
+ * The packed attributes are needed to prevent gcc from aligning the structure
+ * elements on certain architectures. The seq field is intentionally not packed
+ * because the tea encoder/decoder needs its data block to be aligned on some
+ * architectures.
+ */
 typedef struct {
 	u8 hdr[3] __attribute__ ((packed));	/* "TEA" */
 	u8 encoding __attribute__ ((packed));	/* Encoding type */
 	u8 byte_order __attribute__ ((packed));	/* Byte order */
 	u16 datalen __attribute__ ((packed));	/* Length of encoded data */
-	u32 seq __attribute__ ((packed));	/* Encoded sequence number */
-	u32 data[MAXPKTLEN] __attribute__ ((packed));	/* Encoded data */
+	u32 seq;				/* Encoded sequence number */
+	u32 data[MAXPKTLEN] __attribute__ ((packed)); /* Encoded data */
 } teapacket;

 typedef struct {
diff -urN tea-total-0.4/util.c tea-total-0.4a/util.c
--- tea-total-0.4/util.c	Tue Jan  2 09:01:18 2001
+++ tea-total-0.4a/util.c	Sun Nov 18 11:02:12 2001
@@ -13,6 +13,7 @@
 #include <fcntl.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <string.h>
 #include <sys/time.h>
 #include <termios.h>

-- 
------- Alex Holden -------
http://www.linuxhacker.org/
 http://www.robogeeks.org/

Subject: Re: "Read error" building for iPAQ running Linux
From: Jay Sekora ####@####.####
Date: 18 Nov 2001 14:49:56 -0000
Message-Id: <11753.1006094984@js.aq.org>

> Oops, I just tried it on my NetWinder again and it seems that the reason
> it worked before was that the kernel I was using then had the user mode
> alignment trap handler. Newer kernels don't have it, and there was an
> alignment bug in Tea Total 0.4. Does the following patch fix your
> problems?

Yes, it certainly did!  Thanks!  How convenient that you had a Netwinder
handy to test this on.  -j.

[<<] [<] Page 1 of 1 [>] [>>]


Powered by ezmlm-browse 0.20.