• AVR Freaks

Hot!PIC32MZ aes

Author
joset
Starting Member
  • Total Posts : 56
  • Reward points : 0
  • Joined: 2016/07/25 13:05:11
  • Location: 0
  • Status: offline
2019/11/17 07:14:17 (permalink)
0

PIC32MZ aes

hello, im trying the example of  aes crypto engine for pic32mz EFM.
the code:
const unsigned char msg[] = {0x6E,0x6F,0x77,0x20,0x69,0x73,0x20,0x74,
0x68,0x65,0x20,0x74,0x69,0x6D,0x65,0x20};

unsigned char key[16] ={0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11}; /* align */
unsigned char iv[16]={0x0F,0x1E,0x2D,0x3C,0x4B,0x5A,0x69,0x78,0x87,0x96,0xA5,0xB4,0xC3,0xD2,0xE1,0xF0}; /* align */
                                                               
unsigned char cipher[AES_BLOCK_SIZE] __attribute__((coherent, aligned(16)));
unsigned char plain [AES_BLOCK_SIZE] __attribute__((coherent, aligned(16)));

volatile bufferDescriptor enc_bd __attribute__((coherent, aligned (16)));
volatile bufferDescriptor dec_bd __attribute__((coherent, aligned (16)));
securityAssociation enc_sa __attribute__((coherent, aligned (16)));
securityAssociation dec_sa __attribute__((coherent, aligned (16)));

int aes_test(void)
{
    unsigned int bufLength = sizeof(msg);
    while (bufLength % 16)
        bufLength++;
    
    memset((void *)&enc_sa, 0, sizeof(enc_sa));
    memset((void *)&dec_sa, 0, sizeof(dec_sa));
    memset((void *)&enc_bd, 0, sizeof(enc_bd));
    memset((void *)&dec_bd, 0, sizeof(dec_bd));
    memset((void *)cipher, 0, sizeof(cipher));
    memset((void *)plain, 0, sizeof(plain));

    CECON = 1 << 6;
    while (CECON);

    CEPOLLCON = 10;
    CEBDPADDR = KVA_TO_PA(&enc_bd);
    CEINTEN = 0x07;
    CECON = 0xA7;

    /* Set up the Security Association */
    enc_sa.SA_CTRL.ALGO = 0b0000100; /* AES */
    enc_sa.SA_CTRL.LNC = 1;//1;
    enc_sa.SA_CTRL.LOADIV = 1;
    enc_sa.SA_CTRL.FB = 1;
    enc_sa.SA_CTRL.KEYSIZE = 00;
    enc_sa.SA_CTRL.ENCTYPE = 1; /* Encryption */
    enc_sa.SA_CTRL.CRYPTOALGO = 0b1001; /* RCBC */

 memcpy(&enc_sa.SA_ENCKEY, key, 16);
memcpy(enc_sa.SA_ENCIV, iv, 16);
/* Set up the Buffer Descriptor */
    enc_bd.BD_CTRL.BUFLEN = bufLength;
    enc_bd.BD_CTRL.LIFM = 1;
    enc_bd.BD_CTRL.SA_FETCH_EN = 1;
    enc_bd.BD_CTRL.LAST_BD = 0;
    enc_bd.BD_CTRL.CRY_MODE = 0;

    enc_bd.SA_ADDR = KVA_TO_PA(&enc_sa);
    enc_bd.SRCADDR = KVA_TO_PA(msg);
    enc_bd.DSTADDR = KVA_TO_PA(cipher);
    enc_bd.NXTPTR = KVA_TO_PA(&dec_bd);
    enc_bd.MSGLEN = sizeof(msg);
enc_bd.BD_CTRL.DESC_EN = 1;

    while (dec_bd.BD_CTRL.DESC_EN);

debug(cipher,sizeof(cipher));
    return 0;
}

 
but the result is always wrong, anyone have success using crypto engine in the MZ EFM?
#1

5 Replies Related Threads

    Larry.Standage
    Super Member
    • Total Posts : 927
    • Reward points : 0
    • Joined: 2011/12/30 09:50:47
    • Location: 0
    • Status: offline
    Re: PIC32MZ aes 2019/11/18 11:45:35 (permalink)
    0
    Your second memcpy of the iv doesn't reference the address of the enc_sa.SA_ENCIV. That's probably what's happening.
    #2
    joset
    Starting Member
    • Total Posts : 56
    • Reward points : 0
    • Joined: 2016/07/25 13:05:11
    • Location: 0
    • Status: offline
    Re: PIC32MZ aes 2019/11/18 11:57:23 (permalink)
    0
    Ive got like in the example "memcpy(enc_sa.SA_ENCIV, iv, 16);" how did you suggest?
    #3
    Larry.Standage
    Super Member
    • Total Posts : 927
    • Reward points : 0
    • Joined: 2011/12/30 09:50:47
    • Location: 0
    • Status: offline
    Re: PIC32MZ aes 2019/11/18 22:35:50 (permalink)
    0
    I'm not sure where the example is coming from. But memcpy always needs a destination pointer.
    Try
    memcpy(&enc_sa.SA_ENCIV, iv, 16);
    and see how that works for you.
    #4
    joset
    Starting Member
    • Total Posts : 56
    • Reward points : 0
    • Joined: 2016/07/25 13:05:11
    • Location: 0
    • Status: offline
    Re: PIC32MZ aes 2019/11/21 07:17:24 (permalink)
    0
    hello, and many thanks for your response, but the result is the same, always strange data in cipher result.
    #5
    joset
    Starting Member
    • Total Posts : 56
    • Reward points : 0
    • Joined: 2016/07/25 13:05:11
    • Location: 0
    • Status: offline
    Re: PIC32MZ aes 2019/12/10 06:40:45 (permalink)
    0
    Another strange error:
    if i print after encrypt-decrypt operation the result is not same.
    const unsigned char msg[] = {0x6E,0x6F,0x77,0x20,0x69,0x73,0x20,0x74,
    0x68,0x65,0x20,0x74,0x69,0x6D,0x65,0x20};

    unsigned char key[16] ={0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11}; /* align */
    unsigned char iv[16]={0x0F,0x1E,0x2D,0x3C,0x4B,0x5A,0x69,0x78,0x87,0x96,0xA5,0xB4,0xC3,0xD2,0xE1,0xF0}; /* align */

    aes_test();
    debug(msg,16);

     
    print a result not same as "msg" =B9 BD DD 81 CA CD 81 D1 A1 95 81 A2 A5 B5 95 81
     
    if i not performe any hardware crypto operation.
    const unsigned char msg[] = {0x6E,0x6F,0x77,0x20,0x69,0x73,0x20,0x74,
    0x68,0x65,0x20,0x74,0x69,0x6D,0x65,0x20};

    unsigned char key[16] ={0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11}; /* align */
    unsigned char iv[16]={0x0F,0x1E,0x2D,0x3C,0x4B,0x5A,0x69,0x78,0x87,0x96,0xA5,0xB4,0xC3,0xD2,0xE1,0xF0}; /* align */

    //aes_test();
    debug(msg,16);

     
    the print result is ok same as "msg"=6E 6F 77 20 69 73 20 74 68 65 20 74 69 6D 65 20
     
     
     
    #6
    Jump to:
    © 2020 APG vNext Commercial Version 4.5