Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestSpeck.ino fails on samd21 M0 after changing the plaintext #70

Open
Maximize0987 opened this issue Jan 30, 2022 · 1 comment
Open

Comments

@Maximize0987
Copy link

I modified the code to drop all specksmall and tiny, and it still passes works properly, I then change the plaintext so that it matches the first bytes I want to send over a rmf95. It then fails I modified the code to show the encrypted array, and the decrypted array. I assume the encrypted array is correct, but the decrypted array matches the original plaintext! Where is it getting the old array from? I then wrote this sketch to a different samd21 that never had the original plaintext and it still serial prints the old array I don't know where its getting it from. following is the original struct, then my modified TestSpeck.ino and the serial output. Thanks for your help.

THE ORIGINAL UNMODIFIED PARTS.........................................................................................

// Define the test vectors from http://eprint.iacr.org/2013/404
static TestVector const testVectorSpeck128 = {
.name = "Speck-128-ECB",
.key = {0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00},
.plaintext = {0x6c, 0x61, 0x76, 0x69, 0x75, 0x71, 0x65, 0x20,
0x74, 0x69, 0x20, 0x65, 0x64, 0x61, 0x6d, 0x20},

.ciphertext = {0xa6, 0x5d, 0x98, 0x51, 0x79, 0x78, 0x32, 0x65,
0x78, 0x60, 0xfe, 0xdf, 0x5c, 0x57, 0x0d, 0x18}
};

MY MODIFIED TESTSPECK.INO......................................................................................................

/*

  • Copyright (C) 2015 Southern Storm Software, Pty Ltd.
  • Permission is hereby granted, free of charge, to any person obtaining a
  • copy of this software and associated documentation files (the "Software"),
  • to deal in the Software without restriction, including without limitation
  • the rights to use, copy, modify, merge, publish, distribute, sublicense,
  • and/or sell copies of the Software, and to permit persons to whom the
  • Software is furnished to do so, subject to the following conditions:
  • The above copyright notice and this permission notice shall be included
  • in all copies or substantial portions of the Software.
  • THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  • OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  • FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  • AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  • LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  • FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  • DEALINGS IN THE SOFTWARE.
    */

/*
This example runs tests on the Speck implementation to verify correct behaviour.
*/

#include <Crypto.h>
#include <Speck.h>
#include <string.h>

struct TestVector
{
const char *name;
byte key[16];
byte plaintext[16];
byte ciphertext[16];
};

// Define the test vectors from http://eprint.iacr.org/2013/404
static TestVector const testVectorSpeck128 = {
.name = "Speck-128-ECB",
.key = {0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00},
.plaintext = {0x0B, 0x89, 0xA4, 0xC2, 0x6B, 0xC9, 0xA0, 0x6C,
0x1A, 0xD4, 0xC0, 0xB5, 0xFC, 0x6E, 0x83, 0xD2},

.ciphertext = {0xa6, 0x5d, 0x98, 0x51, 0x79, 0x78, 0x32, 0x65,
0x78, 0x60, 0xfe, 0xdf, 0x5c, 0x57, 0x0d, 0x18}
};

Speck speck;

byte buffer[16];

void testCipher(BlockCipher *cipher, const struct TestVector *test, size_t keySize, bool decryption = true)
{
crypto_feed_watchdog();

Serial.print(test->name);
Serial.print(" Encryption ... ");
cipher->setKey(test->key, keySize);
cipher->encryptBlock(buffer, test->plaintext);

for(byte b=0; b<16; b++) {
  Serial.print(buffer[b], HEX);
}

Serial.print("  ");

if (memcmp(buffer, test->ciphertext, 16) == 0)
    Serial.println("Passed");
else
    Serial.println("Failed");

//if (!decryption)
//    return;

Serial.print(test->name);
Serial.print(" Decryption ... ");
cipher->decryptBlock(buffer, test->ciphertext);

for(byte b=0; b<16; b++) {
  Serial.print(buffer[b], HEX);
}

Serial.print("  ");

if (memcmp(buffer, test->plaintext, 16) == 0)
    Serial.println("Passed");
else
    Serial.println("Failed");

}

void perfCipher(BlockCipher *cipher, const struct TestVector *test, size_t keySize, bool decryption = true)
{
unsigned long start;
unsigned long elapsed;
int count;

crypto_feed_watchdog();

Serial.print(test->name);
Serial.print(" Set Key ... ");
start = micros();
for (count = 0; count < 10000; ++count) {
    cipher->setKey(test->key, keySize);
}
elapsed = micros() - start;
Serial.print(elapsed / 10000.0);
Serial.print("us per operation, ");
Serial.print((10000.0 * 1000000.0) / elapsed);
Serial.println(" per second");

Serial.print(test->name);
Serial.print(" Encrypt ... ");
start = micros();
for (count = 0; count < 5000; ++count) {
    cipher->encryptBlock(buffer, buffer);
}
elapsed = micros() - start;
Serial.print(elapsed / (5000.0 * 16.0));
Serial.print("us per byte, ");
Serial.print((16.0 * 5000.0 * 1000000.0) / elapsed);
Serial.println(" bytes per second");

// if (!decryption) {
// Serial.println();
// return;
// }

Serial.print(test->name);
Serial.print(" Decrypt ... ");
start = micros();
for (count = 0; count < 5000; ++count) {
    cipher->decryptBlock(buffer, buffer);
}
elapsed = micros() - start;
Serial.print(elapsed / (5000.0 * 16.0));
Serial.print("us per byte, ");
Serial.print((16.0 * 5000.0 * 1000000.0) / elapsed);
Serial.println(" bytes per second");

Serial.println();

}

void setup()
{
Serial.begin(115200);

Serial.println();

}

void loop()
{
Serial.println("State Sizes:");
Serial.print("Speck ... ");
Serial.println(sizeof(Speck));

Serial.println("Speck Test Vectors:");

testCipher(&speck, &testVectorSpeck128, 16);

Serial.println();

Serial.println("Speck Performance Tests:");

perfCipher(&speck, &testVectorSpeck128, 16);

Serial.println();

delay(2000);

}

THIS IS THE END OF THE SKETCH.................................................................................................................................

State Sizes:
Speck ... 288
Speck Test Vectors:
Speck-128-ECB Encryption ... 68DC68CAE751527E564EB93FBD776A Failed
Speck-128-ECB Decryption ... 6C617669757165207469206564616D20 Failed

Speck Performance Tests:
Speck-128-ECB Set Key ... 89.00us per operation, 11235.90 per second
Speck-128-ECB Encrypt ... 2.88us per byte, 346724.11 bytes per second
Speck-128-ECB Decrypt ... 3.29us per byte, 303831.31 bytes per second

@Maximize0987
Copy link
Author

I dont know how to show code properly or why it shows only sections as code. Sorry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant