Function returning garbage pointer

D

Daniel Rudy

Hello Group,

I've looked at this backwards and forwards and I can't seem to find the
problem here. What happens is I have a multidimensional array of char
that has 3 dimensions with a declaration assignment. After a certian
point in the array, I get garbage. Not sure why. Granted this is a
little off topic because of what the code does, but I'm hoping that a
few extra pairs of eyes can see what I'm missing.

#define CRYPTO_KEYMAX 16 /* number of key sizes */
#define CRYPTO_ALG_MAX 4 /* number of algorithms */

/* converts an algorithm number into a name */
const char * crypto_name_algorithm(int algorithm, int size)
{
char cs[CRYPTO_ALG_MAX][CRYPTO_KEYMAX][16] = {
{"","","","AES-128","","AES-192","","AES-256","","AES-320","",
"AES-384","","","","AES-512"},
{"","DES-64","","DES-128","","","","","","","","","","","",""},
{"","","","3DES-128","","3DES-192","","3DES-256","","3DES-320",
"","3DES-384","","","",""},
{"","","","ARC4-128","ARC4-160","ARC4-192","ARC4-224","ARC4-256",
"ARC4-288","ARC4-320","ARC4-352","ARC4-384","ARC4-416","ARC4-448",
"ARC4-480","ARC4-512"}};

DPRINTF(("%s\n", cs[algorithm][size]))
if (strlen(cs[algorithm][size]) > 0) return(cs[algorithm][size]);
return("Unknown/Invalid");
}


in crypto.h:
/* note: not all key sizes are supported by all algorithms.
crypto_initstate will return EFUNCTERR if an invalid key
size is encountered for a specific algorithm. */
#define CRYPTO_KEY32 0
#define CRYPTO_KEY64 1
#define CRYPTO_KEY96 2
#define CRYPTO_KEY128 3
#define CRYPTO_KEY160 4
#define CRYPTO_KEY192 5
#define CRYPTO_KEY224 6
#define CRYPTO_KEY256 7
#define CRYPTO_KEY288 8
#define CRYPTO_KEY320 9
#define CRYPTO_KEY352 10
#define CRYPTO_KEY384 11
#define CRYPTO_KEY416 12
#define CRYPTO_KEY448 13
#define CRYPTO_KEY480 14
#define CRYPTO_KEY512 15


/* cipher algorithm codes */
#define CRYPTO_ALG_AES 0 /* AES: blocksize 16 bytes */
#define CRYPTO_ALG_DES 1 /* DES: blocksize 8 bytes */
#define CRYPTO_ALG_DES3 2 /* 3DES: blocksize 8 bytes */
#define CRYPTO_ALG_ARC4 3 /* ARC4: stream (variable blocksize) */


in debug.h:
#define DPRINTF(arg) printf arg;

Now here's what I get when I run the test program:

strata:/home/dr2867/c/modules 1146 $$$ ->./crypto.test
Encryption Check Program

Encrypting Data...

Original Key: asdfasdfasdf123412341234
Original Keysize: 24
Crypto Index: 3
Crypto Keysize: 11
ARC4-384 <----------- from DPRINTF in crypto_name_algorithm
Crypto Text: ˆì¿¿+(²¿¿´(+(ÓH²¿¿
Nonce: 0x00000023
Skip: 0x00000003

Key:
cb29747e91b2ff308d15551b41d3fdc22e3ec22eb390d89921ee329a2919b4b4a9bba4f27465db607721985624a8722a29a94fbe
7e46d56344a1a3d971528aca
Salt:
20d560efc13cb94f0ea38c5e7f3114a10f1d61e126acffc78a2a553f5f0746687ee60a40790eea5f850622a36a55aff7d2106d48
9126bc97e6642dc1ae17d3fb
Seed:
04c135c8b4cbf85691cdc4212790091ab2e7851f460743b98ac6947b040f622c281d3b82ce7adb3c47ec1c38b1635ff2f548cfdd
a767adec608bff56f4bb0bb9
Input Size: 67
Output Size: 67
Encrypted Data: 26 cf 28 66 78 5a 3c 85 54 03 20 0d 06 a0 29 ad ad 08 5b
db 73 46 b8 39 8a e2 15 3b c4 44 de b
5 06 24 44 39 b4 40 5c 13 53 33 18 bf 99 2d 17 f6 3f 7c 87 00 75 5c f8
82 bc 56 c6 4b 71 ba 58 ad a2 d4 a4


It only starts at the position that holds the string ARC4-384. All
previous references are ok. Any ideas?


--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
S

santosh

Daniel said:
Hello Group,

I've looked at this backwards and forwards and I can't seem to find the
problem here. What happens is I have a multidimensional array of char
that has 3 dimensions with a declaration assignment. After a certian
point in the array, I get garbage. Not sure why. Granted this is a
little off topic because of what the code does, but I'm hoping that a
few extra pairs of eyes can see what I'm missing.

#define CRYPTO_KEYMAX 16 /* number of key sizes */
#define CRYPTO_ALG_MAX 4 /* number of algorithms */

/* converts an algorithm number into a name */
const char * crypto_name_algorithm(int algorithm, int size)
{
char cs[CRYPTO_ALG_MAX][CRYPTO_KEYMAX][16] = {
{"","","","AES-128","","AES-192","","AES-256","","AES-320","",
"AES-384","","","","AES-512"},
{"","DES-64","","DES-128","","","","","","","","","","","",""},
{"","","","3DES-128","","3DES-192","","3DES-256","","3DES-320",
"","3DES-384","","","",""},
{"","","","ARC4-128","ARC4-160","ARC4-192","ARC4-224","ARC4-256",
"ARC4-288","ARC4-320","ARC4-352","ARC4-384","ARC4-416","ARC4-448",
"ARC4-480","ARC4-512"}};

DPRINTF(("%s\n", cs[algorithm][size]))
if (strlen(cs[algorithm][size]) > 0) return(cs[algorithm][size]);
return("Unknown/Invalid");
}

<snip rest>

The function returns a local object. I think that's the reason for
corrupted values.
 
D

Daniel Rudy

At about the time of 3/5/2007 6:58 AM, santosh stated the following:
Daniel said:
Hello Group,

I've looked at this backwards and forwards and I can't seem to find the
problem here. What happens is I have a multidimensional array of char
that has 3 dimensions with a declaration assignment. After a certian
point in the array, I get garbage. Not sure why. Granted this is a
little off topic because of what the code does, but I'm hoping that a
few extra pairs of eyes can see what I'm missing.

#define CRYPTO_KEYMAX 16 /* number of key sizes */
#define CRYPTO_ALG_MAX 4 /* number of algorithms */

/* converts an algorithm number into a name */
const char * crypto_name_algorithm(int algorithm, int size)
{
char cs[CRYPTO_ALG_MAX][CRYPTO_KEYMAX][16] = {
{"","","","AES-128","","AES-192","","AES-256","","AES-320","",
"AES-384","","","","AES-512"},
{"","DES-64","","DES-128","","","","","","","","","","","",""},
{"","","","3DES-128","","3DES-192","","3DES-256","","3DES-320",
"","3DES-384","","","",""},
{"","","","ARC4-128","ARC4-160","ARC4-192","ARC4-224","ARC4-256",
"ARC4-288","ARC4-320","ARC4-352","ARC4-384","ARC4-416","ARC4-448",
"ARC4-480","ARC4-512"}};

DPRINTF(("%s\n", cs[algorithm][size]))
if (strlen(cs[algorithm][size]) > 0) return(cs[algorithm][size]);
return("Unknown/Invalid");
}

<snip rest>

The function returns a local object. I think that's the reason for
corrupted values.

Thank you for pointing that out. I didn't even realize that little
fact. After moving it, and others like it from inside the function to
the global context, it's now working properly. I know what const does,
but what exactly does static do? It was something that I read that I
should use if I declare something in the global context, but it doesn't
exactly explain why.




/* string litteral used to reject on crypto_name_algorithm */
static const char keyname_reject[] = "Unknown/Invalid";

/* algorithm/size to name map table */
static const char keyname[CRYPTO_ALG_MAX][CRYPTO_KEYMAX][16] = {

{"","","","AES-128","","AES-192","","AES-256","","AES-320","","AES-384","","","","AES-512"},
{"","DES-64","","DES-128","","","","","","","","","","","",""},

{"","","","3DES-128","","3DES-192","","3DES-256","","3DES-320","","3DES-384","","","",""},

{"","","","ARC4-128","ARC4-160","ARC4-192","ARC4-224","ARC4-256","ARC4-288","ARC4-320",
"ARC4-352","ARC4-384","ARC4-416","ARC4-448","ARC4-480","ARC4-512"}};

/* algorithm/size valid check map table */
static const int keyvalid[CRYPTO_ALG_MAX][CRYPTO_KEYMAX] = {
{ -1, -1, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0},
{ -1, 0,- 1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, -1, -1, -1},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
/* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */

/* algorithm/size key 1 size data table */
static const int keysize1[CRYPTO_ALG_MAX][CRYPTO_KEYMAX] = {
{ -1, -1, -1,128, -1,192, -1,256, -1,192, -1,256, -1,256, -1,256},
{ -1, 64, -1, 64, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1,128, -1,192, -1,128, -1,192, -1,192, -1, -1, -1, -1},
{ 32, 64, 96,128,160,192,224,256,288,320,352,384,416,448,480,512}};
/* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */

/* algorithm/size key 2 size data table */
static const int keysize2[CRYPTO_ALG_MAX][CRYPTO_KEYMAX] = {
{ -1, -1, -1, 0, -1, 0, -1, 0, -1,128, -1,128, -1,192, -1,256},
{ -1, 0,- 1, 64, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, 0, -1, 0, -1,128, -1,128, -1,192, -1, -1, -1, -1},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
/* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */

/* template for additional algorithms
{32,64,96,128,160,192,224,256,288,320,352,384,416,448,480,512}
*/

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
S

santosh

Daniel said:
At about the time of 3/5/2007 6:58 AM, santosh stated the following:
Daniel said:
Hello Group,

I've looked at this backwards and forwards and I can't seem to find the
problem here. What happens is I have a multidimensional array of char
that has 3 dimensions with a declaration assignment. After a certian
point in the array, I get garbage. Not sure why. Granted this is a
little off topic because of what the code does, but I'm hoping that a
few extra pairs of eyes can see what I'm missing.

#define CRYPTO_KEYMAX 16 /* number of key sizes */
#define CRYPTO_ALG_MAX 4 /* number of algorithms */

/* converts an algorithm number into a name */
const char * crypto_name_algorithm(int algorithm, int size)
{
char cs[CRYPTO_ALG_MAX][CRYPTO_KEYMAX][16] = {
{"","","","AES-128","","AES-192","","AES-256","","AES-320","",
"AES-384","","","","AES-512"},
{"","DES-64","","DES-128","","","","","","","","","","","",""},
{"","","","3DES-128","","3DES-192","","3DES-256","","3DES-320",
"","3DES-384","","","",""},
{"","","","ARC4-128","ARC4-160","ARC4-192","ARC4-224","ARC4-256",
"ARC4-288","ARC4-320","ARC4-352","ARC4-384","ARC4-416","ARC4-448",
"ARC4-480","ARC4-512"}};

DPRINTF(("%s\n", cs[algorithm][size]))
if (strlen(cs[algorithm][size]) > 0) return(cs[algorithm][size]);
return("Unknown/Invalid");
}

<snip rest>

The function returns a local object. I think that's the reason for
corrupted values.

Thank you for pointing that out. I didn't even realize that little
fact. After moving it, and others like it from inside the function to
the global context, it's now working properly. I know what const does,
but what exactly does static do? It was something that I read that I
should use if I declare something in the global context, but it doesn't
exactly explain why.

<snip>

When the static modifier is applied to a identifier at file scope,
(whether it's an object or a function), then that identifier's scope
is restricted to that translation unit, from that point of it's
declaration to the end of the unit. If the static modifier is applied
to objects with function scope, it causes the object to persist
throughout the programme's lifetime, and retain it's value, even
between seperate calls to that function. It's visibility however, is
the same as any local variable.
 
S

Stephen Sprunk

Daniel Rudy said:
At about the time of 3/5/2007 6:58 AM, santosh stated the following:

Thank you for pointing that out. I didn't even realize that little
fact. After moving it, and others like it from inside the function to
the global context, it's now working properly. I know what const
does, but what exactly does static do? It was something that I
read that I should use if I declare something in the global context,
but it doesn't exactly explain why.

"static" does varying things depending on where it appears.

If no other function needs to access that array by name, keep it inside the
function but with the "static" modifier so that it persists for the lifetime
of the program instead of being recreated each time the function is
called -- and destroyed each time it returns.

If multiple functions inside that file need to access that array by name,
then move it to file scope and make it static. If functions in multiple
files need to access the array by name, then move it to file scope and
_don't_ make it static.

S
 
O

Old Wolf

const char * crypto_name_algorithm(int algorithm, int size)
{
char cs[CRYPTO_ALG_MAX][CRYPTO_KEYMAX][16] = {
{"","","","AES-128","","AES-192","","AES-256","","AES-320","",
"AES-384","","","","AES-512"},
{"","DES-64","","DES-128","","","","","","","","","","","",""},
{"","","","3DES-128","","3DES-192","","3DES-256","","3DES-320",
"","3DES-384","","","",""},
{"","","","ARC4-128","ARC4-160","ARC4-192","ARC4-224","ARC4-256",
"ARC4-288","ARC4-320","ARC4-352","ARC4-384","ARC4-416","ARC4-448",
"ARC4-480","ARC4-512"}};

You could also solve your problem by writing:

char const *cs[CRYPTO_ALG_MAX][CRYPTO_KEYMAX] = { ...

This might also use less memory and less runtime resource.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top