Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/609736783354576896
Update test to demonstrate latest benchmark capture.
Source Link
// Re-invent the byte using arbitrary length array (i.e. BigNumber)
// to increment 16-byte nonce

// Benchmark: 273,839 inc/s { 00, 04, 2D, AF } on ATmega328
void incBytes(byte *state, int i)
{
  if (state[i] < 0xff)
  {
    state[i]++;
    return;
  }

  state[i] = 0x00;

  if (i == 0)
    return;

  i--;
  incBytes(state, i);
}

void printBytes(byte *state, int len)
{
  int i;
  for (i = 0; i < len; i++)
  {
    printf("%02X ", state[i]);
  }

  printf("\n");
}

void testInctest()
{
  bytelong state[]t, =count;
 { 0x00,byte 0x00,nonce[16];

 0x01 fillBytes(nonce, 0xee0, }sizeof(nonce));
 
  intcount i;= 0;
  fort = millis(i);
 = 1;while i(millis() <=- 100;t i++< 1000)
  {
    incByte// cipher(stateplaintext, expandedKey);
    // cipherCtr(plaintext, expandedKey, nonce);
    incBytes(nonce, sizeof(statenonce) - 1);
    printBytescount++;
  }

  Serial.print(state,count);
 sizeof Serial.println(state" inc/s (incBytes)");
  }dumpBytes(nonce, sizeof(nonce));
}
// Re-invent the byte using arbitrary length array (i.e. BigNumber)
// to increment 16-byte nonce

// Benchmark: 273,839 inc/s { 00, 04, 2D, AF } on ATmega328
void incBytes(byte *state, int i)
{
  if (state[i] < 0xff)
  {
    state[i]++;
    return;
  }

  state[i] = 0x00;

  if (i == 0)
    return;

  i--;
  incBytes(state, i);
}

void printBytes(byte *state, int len)
{
  int i;
  for (i = 0; i < len; i++)
  {
    printf("%02X ", state[i]);
  }

  printf("\n");
}

void testInc()
{
  byte state[] = { 0x00, 0x00, 0x01, 0xee };
 
  int i;
  for (i = 1; i <= 100; i++)
  {
    incByte(state, sizeof(state) - 1);
    printBytes(state, sizeof(state));
  }
}
// Re-invent the byte using arbitrary length array (i.e. BigNumber)
// to increment 16-byte nonce

// Benchmark: 273,839 inc/s { 00, 04, 2D, AF } on ATmega328
void incBytes(byte *state, int i)
{
  if (state[i] < 0xff)
  {
    state[i]++;
    return;
  }

  state[i] = 0x00;

  if (i == 0)
    return;

  i--;
  incBytes(state, i);
}

void printBytes(byte *state, int len)
{
  int i;
  for (i = 0; i < len; i++)
  {
    printf("%02X ", state[i]);
  }

  printf("\n");
}

void test()
{
  long t, count;
  byte nonce[16];

  fillBytes(nonce, 0, sizeof(nonce));
  count = 0;
  t = millis();
  while (millis() - t < 1000)
  {
    // cipher(plaintext, expandedKey);
    // cipherCtr(plaintext, expandedKey, nonce);
    incBytes(nonce, sizeof(nonce) - 1);
    count++;
  }

  Serial.print(count);
  Serial.println(" inc/s (incBytes)");
  dumpBytes(nonce, sizeof(nonce));
}
Update benchmark
Source Link

After weeks of slaving over FIPS-197, I finally have my own working C/C# implementation of AES-128 which I'm quite happy about.

The next thing I'm looking to do is implement a nonce counter block of configurable length (either 8 or 16 bytes) to convert this block cipher into a stream cipher (CTR mode), according to NIST 800-38a recommendation.

So to do this, the idea I had was to create from scratch a sort of BigNumber implementation that basically reflects how we increment the bits in a byte, starting with the least significant bit:

0000
0001
0010
0011
0100
0101
0110
0111
1000
...

The concept is come up with the same counting system, incrementing each element in the array (starting with the last element), resetting the LSE ("least significant element") when it's at max and incrementing the previous element.

Here's what I came up with:

// Re-invent the byte using arbitrary length array (i.e. BigNumber)
// to increment 16-byte nonce 

// Benchmark: 273,839 inc/s { 00, 04, 2D, AF } on ATmega328
void incByteincBytes(byte *state, int i)
{
  // 0000 -> 0001
  if (state[i] < 0xff)
  {
    state[i]++;
    return;
  }

  //state[i] 0001= ->0x00;

 0010 if (i == 0)
  state[i] = 0x00;return;

  i--;
  incByteincBytes(state, i);
}

void printBytes(byte *state, int len)
{
  int i;
  for (i = 0; i < len; i++)
  {
    printf("%02X ", state[i]);
  }

  printf("\n");
}

void testInc()
{
  byte state[] = { 0x00, 0x00, 0x01, 0xee };

  int i;
  for (i = 1; i <= 100; i++)
  {
    incByte(state, sizeof(state) - 1);
    printBytes(state, sizeof(state));
  }
}

And here is a link to it in action at Ideone.

So I'd just like to get some feedback, not so much on the purpose of all of this or other alternatives out there, but if there are ways to improve it or if this is pretty much it :)

It will then provide me with a way to ensure that the Nonce block for AES-128 in CTR mode gets incremented properly (hence unique) using any arbitrary length byte array.

Note: This is for an Atmega328 so I would like to avoid using long long (64-bit integers) altogether.

After weeks of slaving over FIPS-197, I finally have my own working C/C# implementation of AES-128 which I'm quite happy about.

The next thing I'm looking to do is implement a nonce counter block of configurable length (either 8 or 16 bytes) to convert this block cipher into a stream cipher (CTR mode), according to NIST 800-38a recommendation.

So to do this, the idea I had was to create from scratch a sort of BigNumber implementation that basically reflects how we increment the bits in a byte, starting with the least significant bit:

0000
0001
0010
0011
0100
0101
0110
0111
1000
...

The concept is come up with the same counting system, incrementing each element in the array (starting with the last element), resetting the LSE ("least significant element") when it's at max and incrementing the previous element.

Here's what I came up with:

// Re-invent the byte using arbitrary length array (i.e. BigNumber)
// to increment 16-byte nonce
void incByte(byte *state, int i)
{
  // 0000 -> 0001
  if (state[i] < 0xff)
  {
    state[i]++;
    return;
  }

  // 0001 -> 0010
  state[i] = 0x00;
  i--;
  incByte(state, i);
}

void printBytes(byte *state, int len)
{
  int i;
  for (i = 0; i < len; i++)
  {
    printf("%02X ", state[i]);
  }

  printf("\n");
}

void testInc()
{
  byte state[] = { 0x00, 0x00, 0x01, 0xee };

  int i;
  for (i = 1; i <= 100; i++)
  {
    incByte(state, sizeof(state) - 1);
    printBytes(state, sizeof(state));
  }
}

And here is a link to it in action at Ideone.

So I'd just like to get some feedback, not so much on the purpose of all of this or other alternatives out there, but if there are ways to improve it or if this is pretty much it :)

It will then provide me with a way to ensure that the Nonce block for AES-128 in CTR mode gets incremented properly (hence unique) using any arbitrary length byte array.

Note: This is for an Atmega328 so I would like to avoid using long long (64-bit integers) altogether.

After weeks of slaving over FIPS-197, I finally have my own working C/C# implementation of AES-128 which I'm quite happy about.

The next thing I'm looking to do is implement a nonce counter block of configurable length (either 8 or 16 bytes) to convert this block cipher into a stream cipher (CTR mode), according to NIST 800-38a recommendation.

So to do this, the idea I had was to create from scratch a sort of BigNumber implementation that basically reflects how we increment the bits in a byte, starting with the least significant bit:

0000
0001
0010
0011
0100
0101
0110
0111
1000
...

The concept is come up with the same counting system, incrementing each element in the array (starting with the last element), resetting the LSE ("least significant element") when it's at max and incrementing the previous element.

Here's what I came up with:

// Re-invent the byte using arbitrary length array (i.e. BigNumber)
// to increment 16-byte nonce 

// Benchmark: 273,839 inc/s { 00, 04, 2D, AF } on ATmega328
void incBytes(byte *state, int i)
{
  if (state[i] < 0xff)
  {
    state[i]++;
    return;
  }

  state[i] = 0x00;

  if (i == 0)
    return;

  i--;
  incBytes(state, i);
}

void printBytes(byte *state, int len)
{
  int i;
  for (i = 0; i < len; i++)
  {
    printf("%02X ", state[i]);
  }

  printf("\n");
}

void testInc()
{
  byte state[] = { 0x00, 0x00, 0x01, 0xee };

  int i;
  for (i = 1; i <= 100; i++)
  {
    incByte(state, sizeof(state) - 1);
    printBytes(state, sizeof(state));
  }
}

And here is a link to it in action at Ideone.

So I'd just like to get some feedback, not so much on the purpose of all of this or other alternatives out there, but if there are ways to improve it or if this is pretty much it :)

It will then provide me with a way to ensure that the Nonce block for AES-128 in CTR mode gets incremented properly (hence unique) using any arbitrary length byte array.

Note: This is for an Atmega328 so I would like to avoid using long long (64-bit integers) altogether.

Clarify platform requirements.
Source Link

After weeks of slaving over FIPS-197, I finally have my own working C/C# implementation of AES-128 which I'm quite happy about.

The next thing I'm looking to do is implement a nonce counter block of configurable length (either 8 or 16 bytes) to convert this block cipher into a stream cipher (CTR mode), according to NIST 800-38a recommendation.

So to do this, the idea I had was to create from scratch a sort of BigNumber implementation that basically reflects how we increment the bits in a byte, starting with the least significant bit:

0000
0001
0010
0011
0100
0101
0110
0111
1000
...

The concept is come up with the same counting system, incrementing each element in the array (starting with the last element), resetting the LSE ("least significant element") when it's at max and incrementing the previous element.

Here's what I came up with:

// Re-invent the byte using arbitrary length array (i.e. BigNumber)
// to increment 16-byte nonce
void incByte(byte *state, int i)
{
  // 0000 -> 0001
  if (state[i] < 0xff)
  {
    state[i]++;
    return;
  }

  // 0001 -> 0010
  state[i] = 0x00;
  i--;
  incByte(state, i);
}

void printBytes(byte *state, int len)
{
  int i;
  for (i = 0; i < len; i++)
  {
    printf("%02X ", state[i]);
  }

  printf("\n");
}

void testInc()
{
  byte state[] = { 0x00, 0x00, 0x01, 0xee };

  int i;
  for (i = 1; i <= 100; i++)
  {
    incByte(state, sizeof(state) - 1);
    printBytes(state, sizeof(state));
  }
}

And here is a link to it in action at Ideone.

So I'd just like to get some feedback, not so much on the purpose of all of this or other alternatives out there, but if there are ways to improve it or if this is pretty much it :)

It will then provide me with a way to ensure that the Nonce block for AES-128 in CTR mode gets incremented properly (hence unique) using any arbitrary length byte array.

Note: This is for an Atmega328 so I would like to avoid using long long (64-bit integers) altogether.

After weeks of slaving over FIPS-197, I finally have my own working C/C# implementation of AES-128 which I'm quite happy about.

The next thing I'm looking to do is implement a nonce counter block of configurable length (either 8 or 16 bytes) to convert this block cipher into a stream cipher (CTR mode), according to NIST 800-38a recommendation.

So to do this, the idea I had was to create from scratch a sort of BigNumber implementation that basically reflects how we increment the bits in a byte, starting with the least significant bit:

0000
0001
0010
0011
0100
0101
0110
0111
1000
...

The concept is come up with the same counting system, incrementing each element in the array (starting with the last element), resetting the LSE ("least significant element") when it's at max and incrementing the previous element.

Here's what I came up with:

// Re-invent the byte using arbitrary length array (i.e. BigNumber)
// to increment 16-byte nonce
void incByte(byte *state, int i)
{
  // 0000 -> 0001
  if (state[i] < 0xff)
  {
    state[i]++;
    return;
  }

  // 0001 -> 0010
  state[i] = 0x00;
  i--;
  incByte(state, i);
}

void printBytes(byte *state, int len)
{
  int i;
  for (i = 0; i < len; i++)
  {
    printf("%02X ", state[i]);
  }

  printf("\n");
}

void testInc()
{
  byte state[] = { 0x00, 0x00, 0x01, 0xee };

  int i;
  for (i = 1; i <= 100; i++)
  {
    incByte(state, sizeof(state) - 1);
    printBytes(state, sizeof(state));
  }
}

And here is a link to it in action at Ideone.

So I'd just like to get some feedback, not so much on the purpose of all of this or other alternatives out there, but if there are ways to improve it or if this is pretty much it :)

It will then provide me with a way to ensure that the Nonce block for AES-128 in CTR mode gets incremented properly (hence unique) using any arbitrary length byte array.

After weeks of slaving over FIPS-197, I finally have my own working C/C# implementation of AES-128 which I'm quite happy about.

The next thing I'm looking to do is implement a nonce counter block of configurable length (either 8 or 16 bytes) to convert this block cipher into a stream cipher (CTR mode), according to NIST 800-38a recommendation.

So to do this, the idea I had was to create from scratch a sort of BigNumber implementation that basically reflects how we increment the bits in a byte, starting with the least significant bit:

0000
0001
0010
0011
0100
0101
0110
0111
1000
...

The concept is come up with the same counting system, incrementing each element in the array (starting with the last element), resetting the LSE ("least significant element") when it's at max and incrementing the previous element.

Here's what I came up with:

// Re-invent the byte using arbitrary length array (i.e. BigNumber)
// to increment 16-byte nonce
void incByte(byte *state, int i)
{
  // 0000 -> 0001
  if (state[i] < 0xff)
  {
    state[i]++;
    return;
  }

  // 0001 -> 0010
  state[i] = 0x00;
  i--;
  incByte(state, i);
}

void printBytes(byte *state, int len)
{
  int i;
  for (i = 0; i < len; i++)
  {
    printf("%02X ", state[i]);
  }

  printf("\n");
}

void testInc()
{
  byte state[] = { 0x00, 0x00, 0x01, 0xee };

  int i;
  for (i = 1; i <= 100; i++)
  {
    incByte(state, sizeof(state) - 1);
    printBytes(state, sizeof(state));
  }
}

And here is a link to it in action at Ideone.

So I'd just like to get some feedback, not so much on the purpose of all of this or other alternatives out there, but if there are ways to improve it or if this is pretty much it :)

It will then provide me with a way to ensure that the Nonce block for AES-128 in CTR mode gets incremented properly (hence unique) using any arbitrary length byte array.

Note: This is for an Atmega328 so I would like to avoid using long long (64-bit integers) altogether.

deleted 11 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
Loading
Source Link
Loading