CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

SOLVED: ARM Code Porting Problems

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
matthewsachs



Joined: 19 Jun 2016
Posts: 22

View user's profile Send private message

SOLVED: ARM Code Porting Problems
PostPosted: Wed Aug 19, 2020 3:59 pm     Reply with quote

I realize the purpose of this forum is not to teach rookies ANSI C, but I’ve spent days unsuccessfully trying to port some seemingly simple ARM code to a CCS PIC implementation and have no hair left to pull out. I think the issue is related to the uniqueness of some things in CCS compared to any one of the ARM compilers, but maybe it's just me.

The original code comes from here: https://mouha.be/wp-content/uploads/chaskey-8bit.c

I can get my “ported” code to compile, but when I run it, the PIC crashes and the only way to revive it is with an ICD-U64 re-programming cycle.

CCS v5.094
18F66K80 Running at 5VDC and 40MHz External Osc

All the simple functions _add, _rol, etc. seem to work. The crash happens on the first call to the void chaskey(...) function

If anyone has any suggestions I'd be grateful to hear them

Here is a “side-by-side” comparison of the original ARM and my PIC code:

Code:

//ARM
void _xor(uint8_t r[4], const uint8_t x[4])
{
  r[0] ^= x[0];
  r[1] ^= x[1];
  r[2] ^= x[2];
  r[3] ^= x[3];
}

//PIC
void _xor(int8 *r, _readonly int8 * x)
{
   r[0] ^= x[0];
   r[1] ^= x[1];
   r[2] ^= x[2];
   r[3] ^= x[3];
}


Code:

//ARM
void _add(uint8_t r[4], const uint8_t x[4])
{
  uint8_t i;
  uint16_t tmp = 0;
  for (i=0; i<4; i++)
  {
    tmp = r[i] + x[i] + tmp;
    r[i] = tmp & 0xff;
    tmp >>= 8;
  }
}

//PIC
void _add(int8 *r, _readonly int8 * x)
{
  int8 i;
  int16 tmp = 0L;
  for (i=0; i<4; i++)
  {
    tmp = make16(0,r[i]) + make16(0,x[i]) + tmp;
    r[i] = make8(tmp,0);
    tmp >>= 8;
  }
}


Code:

//ARM
void _rol(uint8_t r[4], uint8_t n)
{
  uint8_t t[4];
  uint8_t u = (32-n) >> 3;
  uint8_t l = (32-n) & 7;
  uint8_t i;
  t[0] = r[0]; t[1] = r[1]; t[2] = r[2]; t[3] = r[3];
  for(i=0;i<4;i++)
    r[i] = (t[(i+u+1)&3] << (8-l)) | (t[(i+u)&3] >> l);
}

//PIC
void _rol(int8 *r, int8 n)
{
  int8 t[4];
  int8 u;
  int8 l;
  int8 i;
 
  u = (32-n);
  u >>= 3;
 
  l = (32-n);
  l &= 7;
 
  t[0] = r[0];
  t[1] = r[1];
  t[2] = r[2];
  t[3] = r[3];
 
  for(i=0; i<4; i++)
      r[i] = (t[(i+u+1)&3] << (8-l)) | (t[(i+u)&3] >> l);
}



Code:

//ARM
void _rol8(uint8_t r[4])
{
  uint8_t t = r[3];
  r[3] = r[2];
  r[2] = r[1];
  r[1] = r[0];
  r[0] = t;
}

//PIC
void _rol8(int8 *r)
{
  int8 t = 0;
  t = r[3];
  r[3] = r[2];
  r[2] = r[1];
  r[1] = r[0];
  r[0] = t;
}


Code:

//ARM
void _rol16(uint8_t r[4])
{
  uint8_t t = r[3];
  r[3] = r[1];
  r[1] = t;
  t = r[2];
  r[2] = r[0];
  r[0] = t;
}

//PIC
void _rol16(int8 *r)
{
  int8 t = 0;
  t = r[3];
  r[3] = r[1];
  r[1] = t;
  t = r[2];
  r[2] = r[0];
  r[0] = t;
}



Code:

//ARM
void timestwo(uint8_t out[16], const uint8_t in[16]) {
  uint8_t i;
  const volatile uint8_t C[2] = { 0x00, 0x87 };
  out[0] = (in[0] << 1) ^ C[in[15] >> 7];
  for (i = 1;i < 16;++i)
    out[i] = (in[i] << 1) | (in[i-1] >> 7);
}

//PIC
void timestwo(int8 *out, _readonly int8 * in)
{
  int8 i;
  _readonly volatile int8 C[2] = { 0x00, 0x87 };
  out[0] = (in[0] << 1) ^ C[in[15] >> 7];
  for (i = 1; i < 16; ++i)
      out[i] = (in[i] << 1) | (in[i-1] >> 7);
}


Code:

//ARM
void subkeys(uint8_t k1[16], uint8_t k2[16], const uint8_t k[16]) {
  timestwo(k1,k);
  timestwo(k2,k1);
}

//PIC
void subkeys(int8 *k1, int8 *k2, _readonly int8 * k)
{
  timestwo(k1,k);
  timestwo(k2,k1);
}


Code:

//ARM
#define ROUND \
  do { \
    _add(&v[0],&v[ 4]); _rol (&v[ 4], 5); _xor(&v[ 4],&v[0]); _rol16(&v[0]); \
    _add(&v[8],&v[12]); _rol8(&v[12]);    _xor(&v[12],&v[8]); \
    _add(&v[0],&v[12]); _rol (&v[12],13); _xor(&v[12],&v[0]); \
    _add(&v[8],&v[ 4]); _rol (&v[ 4], 7); _xor(&v[ 4],&v[8]); _rol16(&v[8]); \
  } while(0)
 
#define PERMUTE \
  for ( i = 0; i != 8; i++) { \
    ROUND; \
  }

void chaskey(uint8_t *tag, uint8_t taglen, const uint8_t *m, const unsigned int mlen, const uint8_t k[16], const uint8_t k1[16], const uint8_t k2[16]) {
  uint8_t i;
  const uint8_t *M = m;
  const uint8_t *end = M + (((mlen-1)>>4)<<4); /* pointer to last message block */
  const uint8_t *l;
  uint8_t v[16];
 
  memcpy(v,k,16);
 
  if (mlen != 0) {
    for ( ; M != end; M += 16 ) {
      for (i=0;i<16;i++) v[i] ^= M[i];
   
      PERMUTE;
    }
  }
 
  i = 0;
  for ( ; M != m + mlen; M++,i++) {
    v[i] ^= *M;
  }

  if (i < 16) v[i] ^= 0x01; /* padding bit */

  if ((mlen != 0) && ((mlen & 0xF) == 0)) {
    l = k1;
  } else {
    l = k2;
  }

  for (i=0;i<16;i++) v[i] ^= l[i];

  PERMUTE;
 
  for (i=0;i<16;i++) v[i] ^= l[i];

  memcpy(tag,v,taglen);
}

//PIC - THIS IS WHERE IT CRASHES
void chaskey(int8 *tag, int8 taglen, _readonly int8 *mu, int8 mlen, _readonly int8 * k, _readonly int8 * k1, _readonly int8 * k2)
{
  int8 i;
  int8 *M;
  int8 *end;
  int8 *l;
  int8 v[16];
 
 
  M = mu;
  end = M + (((mlen-1)>>4)<<4); /* pointer to last message block */
 
  memcpy(&v,k,16);
 
  if (mlen != 0)
  {
    for ( ; M != end; M += 16 )
    {
      for (i=0; i<16; i++) v[i] ^= M[i];
     
      for ( i = 0; i != 8; i++)
      {
       _add(&v[0],&v[ 4]); _rol (&v[ 4], 5); _xor(&v[ 4],&v[0]); _rol16(&v[0]);
       _add(&v[8],&v[12]); _rol8(&v[12]);    _xor(&v[12],&v[8]);
       _add(&v[0],&v[12]); _rol (&v[12],13); _xor(&v[12],&v[0]);
       _add(&v[8],&v[ 4]); _rol (&v[ 4], 7); _xor(&v[ 4],&v[8]); _rol16(&v[8]);
      }     
    }
  }
 
  i = 0;
  for ( ; M != m + mlen; M++,i++)
  {
    v[i] ^= *M;
  }

  if (i < 16) v[i] ^= 0x01; /* padding bit */

  if ((mlen != 0) && ((mlen & 0xF) == 0)) l = k1;
  else l = k2;

  for (i=0;i<16;i++) v[i] ^= l[i];

  for ( i = 0; i != 8; i++)
  {
    _add(&v[0],&v[ 4]); _rol (&v[ 4], 5); _xor(&v[ 4],&v[0]); _rol16(&v[0]);
    _add(&v[8],&v[12]); _rol8(&v[12]);    _xor(&v[12],&v[8]);
    _add(&v[0],&v[12]); _rol (&v[12],13); _xor(&v[12],&v[0]);
    _add(&v[8],&v[ 4]); _rol (&v[ 4], 7); _xor(&v[ 4],&v[8]); _rol16(&v[8]);
  }
 
  for (i=0; i<16; i++) v[i] ^= l[i];

  memcpy(tag,&v,taglen);
}


Code:

//ARM
int test_vectors() {
  uint8_t m[64];
  uint8_t tag[16];
  const uint8_t k[16] = { 0x33, 0x34, 0x3D, 0x83,
                          0x9F, 0x38, 0x9F, 0x00,
                          0x4F, 0xE6, 0x98, 0x23,
                          0x39, 0xCF, 0x7A, 0x41 };
  uint8_t k1[16], k2[16];
 
  int i;
  int ok = 1;
  uint8_t taglen = 16;

  /* key schedule */
  subkeys(k1,k2,k);

  /* mac */
  for (i = 0; i < 64; i++) {
    m[i] = i;
   
    chaskey(tag, taglen, m, i, k, k1, k2);

    if (memcmp( tag, vectors[i], taglen )) {
      printf("test vector failed for %d-byte message\n", i);
      ok = 0;
    }
  }

  return ok;
}

int main() {
  if (test_vectors()) printf("test vectors ok\n");

  return 0;
}

//PIC
int1 test_vectors() {
  int8 m[64];
  int8 tag[16];
  _readonly int8 k[16] = { 0x33, 0x34, 0x3D, 0x83,
                          0x9F, 0x38, 0x9F, 0x00,
                          0x4F, 0xE6, 0x98, 0x23,
                          0x39, 0xCF, 0x7A, 0x41 };
  int8 k1[16];
  int8 k2[16];
 
  int8 localVector[16];
  int8 n = 0;
 
  int8 i;
  int1 ok = 1;
  int8 taglen = 16;

  /* key schedule */
  subkeys(k1,k2,k);

  /* mac */
  for (i = 0; i < 64; i++)
  {
    m[i] = i;
   
    chaskey(tag, taglen, m, i, k, k1, k2);
   
    for(n = 0; n < 16; n++) localVector[n] = vectors[i][n];

    if (memcmp( tag, localVector, taglen )) ok = 0;
  }
 
  return ok;
}


Code:

//ARM
const uint8_t vectors[64][16] =
{
  { 0xE5, 0x8F, 0x2E, 0x79, 0xAA, 0x87, 0xCE, 0x75, 0xB5, 0x50, 0x14, 0x2D, 0x0B, 0x97, 0x91, 0x11 },
  { 0x7B, 0x30, 0xA9, 0x13, 0x89, 0x2C, 0xE6, 0x50, 0x88, 0xBD, 0x77, 0x45, 0x18, 0xDC, 0xBB, 0xC0 },
  { 0x22, 0x89, 0xDF, 0x55, 0x77, 0xF5, 0x7F, 0x2C, 0xF4, 0x9E, 0x80, 0x73, 0xC0, 0x84, 0x50, 0x4E },
  { 0x64, 0xB2, 0xDB, 0x1B, 0xD8, 0x80, 0x76, 0xA0, 0xB8, 0x2A, 0x5B, 0x8E, 0x13, 0x04, 0x66, 0x20 },
  { 0x71, 0xD1, 0xB2, 0x30, 0xFB, 0x32, 0x85, 0xE3, 0x16, 0x7C, 0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0xE8, 0xFA, 0xE5, 0xC8, 0xC1, 0xF0, 0x47, 0xCD, 0x8C, 0x82 },
  { 0x15, 0x1D, 0xBD, 0x3D, 0x7B, 0x2D, 0x09, 0x05, 0xE3, 0xC6, 0x6F, 0x21, 0xFB, 0x60, 0x68, 0x44 },
  { 0x02, 0x91, 0xF3, 0xEB, 0x08, 0x17, 0x4C, 0x8F, 0x36, 0x2F, 0x9D, 0x51, 0x37, 0x54, 0x7C, 0xC6 },
  { 0x54, 0xD4, 0xA0, 0x89, 0x82, 0xA2, 0x01, 0x92, 0x50, 0x1E, 0x1B, 0xEA, 0xDC, 0xBE, 0x71, 0x17 },
  { 0xD7, 0xFA, 0x47, 0x90, 0x8C, 0x6D, 0x13, 0x88, 0x6B, 0x28, 0x88, 0xA4, 0x2C, 0x35, 0xE9, 0x7F },
};

//PIC
const int8 vectors[64][16] =
{
  { 0xE5, 0x8F, 0x2E, 0x79, 0xAA, 0x87, 0xCE, 0x75, 0xB5, 0x50, 0x14, 0x2D, 0x0B, 0x97, 0x91, 0x11 },
  { 0x7B, 0x30, 0xA9, 0x13, 0x89, 0x2C, 0xE6, 0x50, 0x88, 0xBD, 0x77, 0x45, 0x18, 0xDC, 0xBB, 0xC0 },
  { 0x22, 0x89, 0xDF, 0x55, 0x77, 0xF5, 0x7F, 0x2C, 0xF4, 0x9E, 0x80, 0x73, 0xC0, 0x84, 0x50, 0x4E },
  { 0x64, 0xB2, 0xDB, 0x1B, 0xD8, 0x80, 0x76, 0xA0, 0xB8, 0x2A, 0x5B, 0x8E, 0x13, 0x04, 0x66, 0x20 },
  { 0x71, 0xD1, 0xB2, 0x30, 0xFB, 0x32, 0x85, 0xE3, 0x16, 0x7C, 0x70, 0x16, 0xF0, 0x45, 0xED, 0x73 },
  { 0x0C, 0x3D, 0x98, 0xBC, 0x64, 0x40, 0xB1, 0x31, 0xA2, 0xD7, 0x4C, 0x23, 0xF9, 0xBB, 0x92, 0x0C },
  { 0x8A, 0x68, 0xD0, 0x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0x44 },
  { 0x02, 0x91, 0xF3, 0xEB, 0x08, 0x17, 0x4C, 0x8F, 0x36, 0x2F, 0x9D, 0x51, 0x37, 0x54, 0x7C, 0xC6 },
  { 0x54, 0xD4, 0xA0, 0x89, 0x82, 0xA2, 0x01, 0x92, 0x50, 0x1E, 0x1B, 0xEA, 0xDC, 0xBE, 0x71, 0x17 },
  { 0xD7, 0xFA, 0x47, 0x90, 0x8C, 0x6D, 0x13, 0x88, 0x6B, 0x28, 0x88, 0xA4, 0x2C, 0x35, 0xE9, 0x7F }
};


Last edited by matthewsachs on Fri Aug 21, 2020 3:59 pm; edited 1 time in total
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Aug 19, 2020 4:29 pm     Reply with quote

hmm...
for ( ; M != end; M += 16 ) {
...

Ok, I assumed you need all 3 factors in a for loop but no...(had to read the manual..)
the problem I have is just WHAT is the 1st value for M if not defined ?
WHO decide what it is ? what if M already has a value in it, a wrong value ??

say M is an 8 bit integer. does it 'start' as -127 or 0 ??
rather confusing for an old guy like me..
now I'm thinking is 'case' an issue ? 'm' is NOT the same as 'M', if case is enabled. By default case is not enabled( got caught on that before....)

I'm sure the pros will let me know.....

Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 19, 2020 5:29 pm     Reply with quote

void chaskey(int8 *tag, int8 taglen, _readonly int8 *mu, int8 mlen,
Quote:
_readonly int8 * k, _readonly int8 * k1, _readonly int8 * k2)
{
int8 i;
int8 *M;
int8 *end;
int8 *l;
int8 v[16];


M = mu;
end = M + (((mlen-1)>>4)<<4); /* pointer to last message block */

memcpy(&v,k,16);

The item in bold is going to crash it. You do not take the address of an
array by putting & in front of it. You just use the name of the array,
which is v in this case.

Also this one down at the end:
Quote:
memcpy(tag,&v,taglen);

It's the same thing. You don't put & in front of the array name.
Just use the name: v
matthewsachs



Joined: 19 Jun 2016
Posts: 22

View user's profile Send private message

PostPosted: Wed Aug 19, 2020 7:07 pm     Reply with quote

Thanks guys

Jay, the ARM code used m and M which did "get me", so I changed m to mu.

PCM, I changed those two lines and it's still crashing. There is more going on in that function than I can follow, which is why I'm struggling to faithfully replicate its behavior in CCS (one of the reasons anyways ;-) .

For example, the chaskey function takes a pointer mu as one its arguments, assigns it to a local pointer M and then uses M like a variable:
Code:
end = M + (((mlen-1)>>4)<<4);


Here is the updated chaskey function, but as PCM pointed out probably a ton more wrong with it.

Code:

void chaskey(int8 *tag, int8 taglen, _readonly int8 *mu, int8 mlen, _readonly int8 * k, _readonly int8 * k1, _readonly int8 * k2)
{
  int8 i;
  int8 *M;
  int8 *end;
  int8 *l;
  int8 v[16];
 
 
  M = mu;
  end = M + (((mlen-1)>>4)<<4); /* pointer to last message block */
 
  memcpy(v,k,16);
 
  if (mlen != 0)
  {
    for ( ; M != end; M += 16 )
    {
      for (i=0; i<16; i++) v[i] ^= M[i];
     
      for ( i = 0; i != 8; i++)
      {
       _add(&v[0],&v[ 4]); _rol (&v[ 4], 5); _xor(&v[ 4],&v[0]); _rol16(&v[0]);
       _add(&v[8],&v[12]); _rol8(&v[12]);    _xor(&v[12],&v[8]);
       _add(&v[0],&v[12]); _rol (&v[12],13); _xor(&v[12],&v[0]);
       _add(&v[8],&v[ 4]); _rol (&v[ 4], 7); _xor(&v[ 4],&v[8]); _rol16(&v[8]);
      }     
    }
  }
 
  i = 0;
  for ( ; M != m + mlen; M++,i++)
  {
    v[i] ^= *M;
  }

  if (i < 16) v[i] ^= 0x01; /* padding bit */

  if ((mlen != 0) && ((mlen & 0xF) == 0)) l = k1;
  else l = k2;

  for (i=0; i<16; i++) v[i] ^= l[i];

  for ( i = 0; i != 8; i++)
  {
    _add(&v[0],&v[ 4]); _rol (&v[ 4], 5); _xor(&v[ 4],&v[0]); _rol16(&v[0]);
    _add(&v[8],&v[12]); _rol8(&v[12]);    _xor(&v[12],&v[8]);
    _add(&v[0],&v[12]); _rol (&v[12],13); _xor(&v[12],&v[0]);
    _add(&v[8],&v[ 4]); _rol (&v[ 4], 7); _xor(&v[ 4],&v[8]); _rol16(&v[8]);
  }
 
  for (i=0; i<16; i++) v[i] ^= l[i];

  memcpy(tag,v,taglen);

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 19, 2020 8:49 pm     Reply with quote

Comment out lines until it stops crashing. Find the lines that are causing it to crash.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Aug 20, 2020 1:30 am     Reply with quote

The screaming 'big difference, is the meaning of 'const'.

In CCS, a 'const' array is an array stored in ROM. In the ARM, a 'const' array
is a normal RAM array, that is protected against writing.
In CCS, you cannot use pointers to ROM arrays. The reason is that the
PIC has two complete address spaces. There is a ROM address '0' and a RAM
address '0'. You are declaring the vector array as 'const', and then trying to
use a pointer to this. The result will be a pointer that goes somewhere
completely wrong. Disaster.
If you look at the manual for memcmp, there is the line:

Note that s1 and s2 MAY NOT BE A CONSTANT

Now if you simplify chaskey down to just:
Code:

void chaskey(int8 * k)
{
   int8 v[16];
   memcpy(v,k,16);
}

//and call this with:
void main()
{
   const int8 k[16] = { 0x33, 0x34, 0x3D, 0x83,
                          0x9F, 0x38, 0x9F, 0x00,
                          0x4F, 0xE6, 0x98, 0x23,
                          0x39, 0xCF, 0x7A, 0x41 };   
   chaskey(k);

   while(TRUE)
   {
   }
}

This will merrily fail to compile, and give the error 'Attempt to create a
pointer to a constant'

You can create pointers to the rom, by instead using:
Code:

//memcpy function written to support copying from rom
void rommemcpy(int8 * target, rom int8 * source, int16 count)
{
   while (count--)
      *(target++)=*(source++);
}

void chaskey(rom int8 * k)
{
  int8 v[16];
  rommemcpy(v,k,16);
  delay_cycles(1); //At this point, 'v' will correctly contain the values from
  //k.
}

void main()
{
   rom int8 k[16] = { 0x33, 0x34, 0x3D, 0x83,
                          0x9F, 0x38, 0x9F, 0x00,
                          0x4F, 0xE6, 0x98, 0x23,
                          0x39, 0xCF, 0x7A, 0x41 };   
   chaskey(k);

   while(TRUE)
   {
      //TODO: User Code
   }
}


You need to use rom, instead of const, and ensure that all functions
using the rom pointers, understand how to use rom pointers.
matthewsachs



Joined: 19 Jun 2016
Posts: 22

View user's profile Send private message

PostPosted: Fri Aug 21, 2020 3:58 pm     Reply with quote

I couldn't isolate the problem, so I basically rewrote everything from scratch. While it won't land me a job at Google, here is the working code in case anyone else needs to implement this functionality.

Big thanks to Ttelmah, PCM and temtronic.

Code:

//Globals
rom int8 SecretKey[16] = { 0x33, 0x34, 0x3D, 0x83,
                           0x9F, 0x38, 0x9F, 0x00,
                           0x4F, 0xE6, 0x98, 0x23,
                           0x39, 0xCF, 0x7A, 0x41  };
                           
int8 msg[32] = {  0x00, 0x01, 0x02, 0x03,
                  0x04, 0x05, 0x06, 0x07,
                  0x08, 0x09, 0x0A, 0x0B,
                  0x0C, 0x0D, 0x0E, 0x0F,
                  0x10, 0x11, 0x12, 0x13,
                  0x14, 0x15, 0x16, 0x17,
                  0x18, 0x19, 0x1A, 0x1B,
                  0x1C, 0x1D, 0x1E, 0x1F  };

//CCS-Friendly Chaskey Code
void _xor(int8 *r, int8 *x)
{
  r[0] ^= x[0];
  r[1] ^= x[1];
  r[2] ^= x[2];
  r[3] ^= x[3];
}

void _add(int8 *r, int8 *x)
{
  int8 i;
  int16 tmp = 0L;
  for (i=0; i<4; i++)
  {
    tmp = make16(0,r[i]) + make16(0,x[i]) + tmp;
    r[i] = make8(tmp,0);
    tmp >>= 8;
  }
}

void _rol(int8 *r, int8 n)
{
  int8 t[4];
  int8 u;
  int8 l;
  int8 i;
 
  u = (32-n);
  u >>= 3;
 
  l = (32-n);
  l &= 7;
 
  t[0] = r[0];
  t[1] = r[1];
  t[2] = r[2];
  t[3] = r[3];
 
  for(i=0; i<4; i++)
      r[i] = (t[(i+u+1)&3] << (8-l)) | (t[(i+u)&3] >> l);
}

void _rol8(int8 *r)
{
  int8 t = 0;
  t = r[3];
  r[3] = r[2];
  r[2] = r[1];
  r[1] = r[0];
  r[0] = t;
}

void _rol16(int8 *r)
{
  int8 t = 0;
  t = r[3];
  r[3] = r[1];
  r[1] = t;
  t = r[2];
  r[2] = r[0];
  r[0] = t;
}

void timestwo(int8 *out, int8 *in)
{
  int8 i;
  int8 C[2] = { 0x00, 0x87 };
  out[0] = (in[0] << 1) ^ C[in[15] >> 7];
  for (i = 1; i < 16; ++i) out[i] = (in[i] << 1) | (in[i-1] >> 7);
}

void chaskey()
{
   int8 i = 0;
   int8 mlen = 5;
   
   int8 *M;
   int8 *end;
   
   int8 k[16];
   int8 k1[16];
   int8 k2[16];
   
   int8 v[16];
   
   rommemcpy(k, SecretKey, 16L);
   
   // replaces the original subkeys function
   timestwo(k1,k);
   timestwo(k2,k1);
   
   M = msg;
   end = M + (((mlen-1)>>4)<<4); /* pointer to last message block */
   
   for(i=0; i<16; i++) v[i] = k[i]; // replaces memcpy(v,k,16);
   
   if (mlen != 0)
   {
      for ( ; M != end; M += 16) //Not needed for messages <=16 bytes long
      {
         for (i=0; i<16; i++) v[i] ^= M[i];
             
         for (i=0; i<8; i++) //PERMUTE
         {
             _add(&v[0],&v[ 4]);
             _rol (&v[ 4], 5);
             _xor(&v[ 4],&v[0]);
             _rol16(&v[0]);
             _add(&v[8],&v[12]);
             _rol8(&v[12]);   
             _xor(&v[12],&v[8]);
             _add(&v[0],&v[12]);
             _rol (&v[12],13);
             _xor(&v[12],&v[0]);
             _add(&v[8],&v[ 4]);
             _rol (&v[ 4], 7);
             _xor(&v[ 4],&v[8]);
             _rol16(&v[8]);
         }     
      }
   }
   
   i=0;
   for ( ; M != msg + mlen; M++, i++) v[i] ^= *M;
   
   if (i<16) v[i] ^= 0x01; /* padding bit */
 
   if ((mlen != 0) && ((mlen & 0xF) == 0))
   {
      for (i=0; i<16; i++) v[i] ^= k1[i]; //17th line
   }
   else
   {
      for (i=0; i<16; i++) v[i] ^= k2[i];
   }
   
   for (i=0; i<8; i++) //PERMUTE
   {
    _add(&v[0],&v[ 4]);
    _rol (&v[ 4], 5);
    _xor(&v[ 4],&v[0]);
    _rol16(&v[0]);
    _add(&v[8],&v[12]);
    _rol8(&v[12]);   
    _xor(&v[12],&v[8]);
    _add(&v[0],&v[12]);
    _rol (&v[12],13);
    _xor(&v[12],&v[0]);
    _add(&v[8],&v[ 4]);
    _rol (&v[ 4], 7);
    _xor(&v[ 4],&v[8]);
    _rol16(&v[8]);
   }
   
   if ((mlen != 0) && ((mlen & 0xF) == 0))
   {
      for (i=0; i<16; i++) v[i] ^= k1[i]; //17th line
   }
   else
   {
      for (i=0; i<16; i++) v[i] ^= k2[i];
   }
   //At this point v[] contains the tag values we want
   //Print them out, copy to another global[] or modify the function to accept a pointer to an array
}

//memcpy function written to support copying from rom
void rommemcpy(int8 * target, rom int8 * source, int16 count)
{
   while (count--)
   *(target++)=*(source++);
}

temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Aug 21, 2020 4:04 pm     Reply with quote

GREAt I like seeing [solved] in posts !!!

Some times you just HAVE to 'start from scratch' and 'roll your own'.....

I have to ask WHAT does all this 'funky kode' do ??

Jay
matthewsachs



Joined: 19 Jun 2016
Posts: 22

View user's profile Send private message

PostPosted: Fri Aug 21, 2020 4:10 pm     Reply with quote

temtronic wrote:
GREAt I like seeing [solved] in posts !!!

Some times you just HAVE to 'start from scratch' and 'roll your own'.....

I have to ask WHAT does all this 'funky kode' do ??

Jay


Jay this webpage where it comes from explains it a lot better than I can: https://mouha.be/chaskey/
It generates unique "signatures" based on a secret key and some public, but unique data (like a serial number)
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Aug 21, 2020 4:32 pm     Reply with quote

thank you !
Hopefully you can test and confirm it really,really does work 100%, wouldn't be fun if you got locked out of your own house using YOUR code...... Laughing
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group