Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

How to implement software AES-CTR in nRF52832 + S132 v2.0 + SDK11?

How to implement software AES-CTR in nRF52832 + S132 v2.0 + SDK11?

Parents
  • Hello again,

    It seems that the nrf5 SDK v11 does not support AES-CTR out of the box, only AES-ECB and AES-CCB which are implemented in hardware. The later SDK versions includes the crypto component which adds support for CTR (and many other modes) because it allows the usage of software implemented crypto for AES. Unfortunately I can't help you with this, besides recommending you to use a newer version of the SDK.

    I should mention that it might technically be possible for you to add support for CTR based on our ECB mode, but I would not recommend this.

    Regards,

    Elfving

  • Thank you for your reply.

    I finished it a few days ago.

    Maybe the code is not perfect.

    uint8_t AESCTR_Encrypt(uint8_t *pPlain, uint16_t usPLen, uint8_t *pEncrypted, uint16_t usELen, uint16_t *pEValidLen,
                           uint8_t *aIv, uint8_t *aKey)
    {
        if (usELen < usPLen)
        {
            return (uint8_t)-1;
        }
        (void)nrf_ecb_init();
        nrf_ecb_set_key(aKey);
        uint8_t aIvOri[AESCTR_IV_LEN] = {0};
        uint8_t aIvEcb[AESCTR_IV_LEN] = {0};
        memcpy(aIvOri, aIv, AESCTR_IV_LEN);
        (void)nrf_ecb_crypt(aIvEcb, aIvOri);
    
        for (uint16_t usI = 0; usI < usPLen; usI++)
        {
            pEncrypted[usI] = pPlain[usI] ^ aIvEcb[usI & 0x0F];
            if ((usI & 0x0F) == 0x0F)
            {
                for (uint8_t ucJ = AESCTR_IV_LEN - 1; ucJ > 0; ucJ--)
                {
                    if (++aIvOri[ucJ] != 0)
                    {
                        break;
                    }
                }
                (void)nrf_ecb_crypt(aIvEcb, aIvOri);
            }
        }
        *pEValidLen = usPLen;
        return 0;
    }

Reply
  • Thank you for your reply.

    I finished it a few days ago.

    Maybe the code is not perfect.

    uint8_t AESCTR_Encrypt(uint8_t *pPlain, uint16_t usPLen, uint8_t *pEncrypted, uint16_t usELen, uint16_t *pEValidLen,
                           uint8_t *aIv, uint8_t *aKey)
    {
        if (usELen < usPLen)
        {
            return (uint8_t)-1;
        }
        (void)nrf_ecb_init();
        nrf_ecb_set_key(aKey);
        uint8_t aIvOri[AESCTR_IV_LEN] = {0};
        uint8_t aIvEcb[AESCTR_IV_LEN] = {0};
        memcpy(aIvOri, aIv, AESCTR_IV_LEN);
        (void)nrf_ecb_crypt(aIvEcb, aIvOri);
    
        for (uint16_t usI = 0; usI < usPLen; usI++)
        {
            pEncrypted[usI] = pPlain[usI] ^ aIvEcb[usI & 0x0F];
            if ((usI & 0x0F) == 0x0F)
            {
                for (uint8_t ucJ = AESCTR_IV_LEN - 1; ucJ > 0; ucJ--)
                {
                    if (++aIvOri[ucJ] != 0)
                    {
                        break;
                    }
                }
                (void)nrf_ecb_crypt(aIvEcb, aIvOri);
            }
        }
        *pEValidLen = usPLen;
        return 0;
    }

Children
No Data
Related