This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2530 裸机AES加密

Other Parts Discussed in Thread: CC2530

请问,有CC2530 AES裸机加密和解密的资料吗?目前只能实现裸机加密,但解密解出的值不正确

  • 请参考这边文档: http://www.ti.com/lit/an/swra172c/swra172c.pdf

  • 你好,

    那个文档里的不带DMA的CBC和ECB模式的加密可以用,但是解密出来的数据是错误的。

    是我代码写错了吗

  • 你好,

    根据文档写的不带DMA的,加密是正确的,但是解密出来的数据是错误的,麻烦可以看下哪里有错误吗

    ENCCS = 0x40;//ECB
    // Generate Key:
    // In a radio application this is typically generated based on
    // a standard/protocol specification. However, for simplicity,
    // this app report does not use any particular specification
    // for generating it.
    for(i = 0; i < SIZE_OF_AES_BLOCK; i++) { key[i] = i * 2; }
    // Download Key (Set ENCCS.CMD = 10b),
    // and start corresponding AES process (ENCCS.ST = 1)
    ENCCS = 0x04 | 0x01;
    printf("key ");
    for (i = 0; i < SIZE_OF_AES_BLOCK; i++) {
    ENCDI = key[i];
    printf("%x ",key[i]);
    }
    printf("\r\n");
    // Monitor AES (ENCCS.RDY) to wait until key downloaded
    while(!(ENCCS & 0x08));
    // Generate IV/NONCE:
    // In a radio application this is typically generated based on
    // a standard/protocol specification. However, for simplicity,
    // this app report does not use any particular specification
    // for generating it.
    //printf("IV ");
    //for(i = 0; i < SIZE_OF_AES_BLOCK; i++) { iv_nonce[i] = i;
    //printf("%x ",iv_nonce[i]);
    //}
    //printf("\r\n");
    //// Dwonload IV/NONCE (Set ENCCS.CMD = 11b)
    //// and start corresponding AES process (ENCCS.ST = 1)
    //ENCCS = 0x06 | 0x01;
    //for (i = 0; i < SIZE_OF_AES_BLOCK; i++) {
    //ENCDI = iv_nonce[i];
    //}
    //// Monitor AES (ENCCS.RDY) to wait until IV/NONCE downloaded
    //while(!(ENCCS & 0x08));
    //

    ////////////////////////////////////////////////////////////////
    // Place code here for downloading key and IV/NONCE (see Example 5)
    ////////////////////////////////////////////////////////////////
    // Perform AES encryption/decryption on allocated AES buffer ("aes_buffer_1"):
    for (j = 0; j < SIZE_OF_ENC_PACKET/SIZE_OF_AES_BLOCK; j++) {
    // Configure and start AES coprocessor for the desired AES mode:
    // Encryption => ENCCS.CMD = 00b, decryption mode => ENCCS.CMD = 01b.
    // Use so-called Cipher Block Chaining encryption mode => ENCCS.MODE = 000b.
    // Start AES processing (ENCCS.ST = 1) of source buffer (aes_buffer_1).
    ENCCS = 0x00 | 0x01; // Only valid for encryption mode !
    //ENCCS = 0x02 | 0x01; // Only valid for decryption mode !
    // Download data block (16 bytes) to AES coprocessor:
    // In encryption mode, "aes_buffer_1" represents the data to be encrypted.
    // In decryption mode, "aes_buffer_1" represents the data to be decrypted.
    printf("text ");
    for (i = 0; i < SIZE_OF_AES_BLOCK; i++) {
    ENCDI = aes_buffer_1[i+(j*SIZE_OF_AES_BLOCK)];
    printf("%x ",aes_buffer_1[i+(j*SIZE_OF_AES_BLOCK)]);
    }
    printf("\r\n");
    // Wait until AES download is finished, that is; apply delay
    // equivalent to 40 NOPs:
    asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
    asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
    asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
    asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
    asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
    asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
    // Upload data block (16 bytes) to allocated AES buffer:
    // In encryption mode, "aes_buffer_2" represents the encrypted data.
    // In decryption mode, "aes_buffer_2" represents the decrypted data.
    printf("result ");
    for (i = 0; i < SIZE_OF_AES_BLOCK; i++) {
    aes_buffer_2[i+(j*SIZE_OF_AES_BLOCK)] = ENCDO;
    printf("%x ",aes_buffer_2[i+(j*SIZE_OF_AES_BLOCK)]);
    }
    printf("\r\n");
    // Monitor AES (ENCCS.RDY) to wait until data block downloaded
    while(!(ENCCS & 0x08));
    }


    printf("\r\n");
    printf("\r\n");
    printf("\r\n");
    printf("\r\n");
    ////////////////////////////////////////////////////////////////
    // Place code here for downloading key and IV/NONCE (see Example 5)
    ////////////////////////////////////////////////////////////////
    // Perform AES encryption/decryption on allocated AES buffer ("aes_buffer_1"):
    for (j = 0; j < SIZE_OF_ENC_PACKET/SIZE_OF_AES_BLOCK; j++) {
    // Configure and start AES coprocessor for the desired AES mode:
    // Encryption => ENCCS.CMD = 00b, decryption mode => ENCCS.CMD = 01b.
    // Use so-called Cipher Block Chaining encryption mode => ENCCS.MODE = 000b.
    // Start AES processing (ENCCS.ST = 1) of source buffer (aes_buffer_1).
    //ENCCS = 0x00 | 0x01; // Only valid for encryption mode !
    ENCCS = 0x02 | 0x01; // Only valid for decryption mode !
    // Download data block (16 bytes) to AES coprocessor:
    // In encryption mode, "aes_buffer_1" represents the data to be encrypted.
    // In decryption mode, "aes_buffer_1" represents the data to be decrypted.
    printf("text ");
    for (i = 0; i < SIZE_OF_AES_BLOCK; i++) {
    ENCDI = aes_buffer_2[i+(j*SIZE_OF_AES_BLOCK)];
    printf("%x ",aes_buffer_2[i+(j*SIZE_OF_AES_BLOCK)]);
    }
    printf("\r\n");
    // Wait until AES download is finished, that is; apply delay
    // equivalent to 40 NOPs:
    asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
    asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
    asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
    asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
    asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
    asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
    // Upload data block (16 bytes) to allocated AES buffer:
    // In encryption mode, "aes_buffer_2" represents the encrypted data.
    // In decryption mode, "aes_buffer_2" represents the decrypted data.
    printf("result ");
    for (i = 0; i < SIZE_OF_AES_BLOCK; i++) {
    aes_buffer_1[i+(j*SIZE_OF_AES_BLOCK)] = ENCDO;
    printf("%x ",aes_buffer_1[i+(j*SIZE_OF_AES_BLOCK)]);
    }
    printf("\r\n");
    // Monitor AES (ENCCS.RDY) to wait until data block downloaded
    while(!(ENCCS & 0x08));
    }