1

Téma: [vyřešeno] AES-128-CTR šifrování

Dobrý den,

v rámci diplomové práce používám SDS-BIG a snažím se zprovoznit šifrování pomocí AES-128-CTR. Vytvořil jsem proto testovací program, který vychází z tohoto ukázkového kódu: http://wiki.merenienergie.cz/index.php/FULL-C_secure_httpget_example

#define HTTPGET_PLAINTEXT_FOR_B64_LIMIT (1056) // (which is 1063 rounded to closest multiply of 16)
#define ENCPROTOCOLhdrSIZE              (23)   // bytes


void HTTP_GET_SECURE_SEND_STRING_TO_WEB(char * PlaintextBuf) {
    unsigned int b64len, clrLen, PlaintextLen;
    unsigned int IV[2];
    // setup IV = make sure it is a "number-used-once",
    // we will use rand() here
    IV[0] = rand() & 0x0FFFFFFF;
    IV[1] = rand() & 0x0FFFFFFF;

    PlaintextLen = strlen(PlaintextBuf);

     if (PlaintextLen < 1) {
           // failure
           printf("Empty String Provided - failure\n");
           return;
       }

     printf("PlaintextLen = %d\n", PlaintextLen);
     printf("%x\n", (~0x0F));
     PlaintextLen = (PlaintextLen +15) & (~0x0F);
     printf("PlaintextLen = %d\n", PlaintextLen);

    unsigned int LocalIV[2];
    // copy IV to local-IV, as the local-IV will
    // be modified in background by SDS_Crypto()
    LocalIV[0] = IV[0];
    LocalIV[1] = IV[1];
    // encrypt

       printf("Plain text: ");
       for (int i = 0; i < PlaintextLen; i++) {
        printf("%02x", PlaintextBuf[i]);
       }
       printf("\n");

    if (SDS_crypto(0x10, (unsigned int *)LocalIV, (void *)PlaintextBuf, PlaintextLen) == 0) {
        printf("SDS_crypto - failure\n");
        return;
    }

    if (SDS_crypto(0x10, (unsigned int *)LocalIV, (void *)PlaintextBuf, PlaintextLen) == 1) {
        printf("SDS_crypto - OK\n");
        printf("Cipher text: ");
           for (int i = 0; i < PlaintextLen; i++) {
               printf("%02x", PlaintextBuf[i]);
           }
           printf("\n");
        return;
    }
   // the encryption works over the very same single buffer,
   // so at this moment the contents of 'PlaintextBuf' is encrypted
   // and the 'PlaintextLen' is exactly the same (symmetrical-encryption)
}

Pokud nechám šifrovací algoritmus XTEA (0x10), vše proběhne bez problému viz výpis z konzole:

EXAMPLE OF ENCRYPTED HTTP-GET FOR SECURE WEB-DELIVERY FROM SDS
PlaintextLen = 16
Plain text: 5344537c73333d610000000000000000
SDS_crypto - OK
Cipher text: 1e238633c4866ffc42bd29d1d8d98f70

Pokud ale (na starém firmware) změním algoritmus na AES, text se nezašifruje (funkce SDS_crypto vrátí nulu). Předpokládám, že musím nějak změnit Counter Block, který je u AES 128-bit a inkrementuje se pouze posledních 32 bitů. Mohli byste sem prosím dát nějaký příklad, kde je šifrování provedeno pomocí AES-128-CTR a kód pro sestavení Counter Blocku?

Díky

2

Re: [vyřešeno] AES-128-CTR šifrování

Funkce AES128 ve FULL-C je k dispozici až ve firmware od 6.4.2017 a dále.


Zde je vzorový testovací kód:

// global
unsigned char IV[16];

// reset IV
void setup_iv(void)
{
 // setup the initial IV value

 /* this gives the same result as the followin IV[] setup below
 unsigned int IV4[4];
 IV4[0] = 0xf3f2f1f0; // constant (NONCE)
 IV4[1] = 0xf7f6f5f4; // constant (NONCE)
 IV4[2] = 0xfbfaf9f8; // constant (NONCE)
 IV4[3] = 0xfffefdfc; // only this one (IV[3]) is incremented by 1 on each 16B block
 */

 // First "init.couter" setup, per the F5.1
 IV[0] = 0xF0;
 IV[1] = 0xF1;
 IV[2] = 0xF2;
 IV[3] = 0xF3;
 IV[4] = 0xF4;
 IV[5] = 0xF5;
 IV[6] = 0xF6;
 IV[7] = 0xF7;
 IV[8] = 0xF8;
 IV[9] = 0xF9;
 IV[10] = 0xFA;
 IV[11] = 0xFB;
 IV[12] = 0xFC;
 IV[13] = 0xFD;
 IV[14] = 0xFE;
 IV[15] = 0xFF;
}

// go
void main(void)
{

 unsigned char Buf[32];

 unsigned int i, ret;

 printf("TEST AES-128 ENC/DEC (NIST F5.1 Vectors)\n");
 printf("\n");
 printf("in web-admin, GO set the CRYPT-16B KEY (32-chars) to \"2b7e151628aed2a6abf7158809cf4f3c\" \n");
 printf("\n");

 // unsigned int SDS_crypto(unsigned int Algo, unsigned int *CounterBlock, void *PayloadData, unsigned int PayloadDataSize);

 // NIST 800-38A 2001 - paragraph F5.1 - CTR-AES128 Encrypt

 setup_iv();

 // setup the input data - 32 bytes of plain-text data

 // Block #1 - 6bc1bee22e409f96e93d7e117393172a
 Buf[0] = 0x6b;
 Buf[1] = 0xc1;
 Buf[2] = 0xbe;
 Buf[3] = 0xe2;
 Buf[4] = 0x2e;
 Buf[5] = 0x40;
 Buf[6] = 0x9f;
 Buf[7] = 0x96;
 Buf[8] = 0xe9;
 Buf[9] = 0x3d;
 Buf[10] = 0x7e;
 Buf[11] = 0x11;
 Buf[12] = 0x73;
 Buf[13] = 0x93;
 Buf[14] = 0x17;
 Buf[15] = 0x2a;
 // BLock #2 - ae2d8a571e03ac9c9eb76fac45af8e51
 Buf[16] = 0xae;
 Buf[17] = 0x2d;
 Buf[18] = 0x85;
 Buf[19] = 0x57;
 Buf[20] = 0x1e;
 Buf[21] = 0x03;
 Buf[22] = 0xac;
 Buf[23] = 0x9c;
 Buf[24] = 0x9e;
 Buf[25] = 0xb7;
 Buf[26] = 0x6f;
 Buf[27] = 0xac;
 Buf[28] = 0x45;
 Buf[29] = 0xaf;
 Buf[30] = 0x8e;
 Buf[31] = 0x51;

 // encryption -----------------------------------------------------------------

 // debug
 printf("(plain-text) input   = ", Buf);
 for (i = 0; i < 32; i++)
 {
   printf("%02x", Buf[i]);
   if (i == 15) printf(" ");
 }
 printf("\n");

 // AES-128 encrypt (0x20)
 ret = SDS_crypto(0x20, (void *)&IV, (void *)&Buf, 32);

 // debug
 printf("(encrypted) ret-val = %d\n", ret);
 printf("(encrypted) result  = ");
 for (i = 0; i < 32; i++)
 {
   printf("%02x", Buf[i]);
   if (i == 15) printf(" ");
 }
 printf("\n");


 // decryption -----------------------------------------------------------------

 // Buf[] now contains 32 bytes of AES128-CTR ENCRYPTED data

 // reset the IV to decrypt from start
 setup_iv();

 // AES-128 decrypt
 // (yes, 0x20 is used for both AES-CTR encrypt and decrypt,
 //  due to the way the CTR works)
 ret = SDS_crypto(0x20, (void *)&IV, (void *)&Buf, 32);

 // debug
 printf("(decrypted) ret-val = %d\n", ret);
 printf("(decrypted) result  = ");
 for (i = 0; i < 32; i++)
 {
   printf("%02x", Buf[i]);
   if (i == 15) printf(" ");
 }
 printf("\n");

 // done -----------------------------------------------------------------------

 printf("\n");


 /*

 THIS IS THE 192.168.1.250/echo.htm OUTPUT:

TEST AES-128 ENC/DEC (NIST F5.1 Vectors)
in web-admin, GO set the CRYPT-16B KEY (32-chars) to "2b7e151628aed2a6abf7158809cf4f3c"
(plain-text) input = 6bc1bee22e409f96e93d7e117393172a ae2d85571e03ac9c9eb76fac45af8e51
(encrypted) ret-val = 1
(encrypted) result = 874d6191b620e3261bef6864990db6ce 9806f96b7970fdff8617187bb9fffdff
(decrypted) ret-val = 1
(decrypted) result = 6bc1bee22e409f96e93d7e117393172a ae2d85571e03ac9c9eb76fac45af8e51

 Note that the (decrypted) matches the (plain-text).

 */

}

Detaily k funkci samotné: http://wiki.merenienergie.cz/index.php/FULL-C_crypto_functions

Pochlubte se - popište jak využíváte své zařízení SDS zde ! Můžete si bezplatně přidat svou reklamu !

3

Re: [vyřešeno] AES-128-CTR šifrování

Povedlo se vám to použít (s novým FW) ?

Pochlubte se - popište jak využíváte své zařízení SDS zde ! Můžete si bezplatně přidat svou reklamu !

4

Re: [vyřešeno] AES-128-CTR šifrování

Ano, šifrování AES se mi dnes podařilo zprovoznit, díky!