ext/lib/crypto/tinycrypt/source/ccm_mode.c
/**
 * Variation of CTR mode used in CCM.
 * The CTR mode used by CCM is slightly different than the conventional CTR
 * mode (the counter is increased before encryption, instead of after
 * encryption). Besides, it is assumed that the counter is stored in the last
 * 2 bytes of the nonce.
 */
Variation of CTR mode used in CCM.
The CTR mode used by CCM is slightly different than the conventional CTR
mode (the counter is increased before encryption, instead of after
encryption). Besides, it is assumed that the counter is stored in the last
2 bytes of the nonce.
Variation of CTR mode used in CCM . The CTR mode used by CCM is slightly different than the conventional CTR mode ( the counter is increased before encryption instead of after encryption ) . Besides it is assumed that the counter is stored in the last 2 bytes of the nonce .
static int32_t ccm_ctr_mode(uint8_t *out, uint32_t outlen, const uint8_t *in,
			     uint32_t inlen, uint8_t *ctr, const TCAesKeySched_t sched)
{
	uint8_t buffer[TC_AES_BLOCK_SIZE];
	uint8_t nonce[TC_AES_BLOCK_SIZE];
	uint16_t block_num;
	uint32_t i;
	if (out == (uint8_t *) 0 ||
	    in == (uint8_t *) 0 ||
	    ctr == (uint8_t *) 0 ||
	    sched == (TCAesKeySched_t) 0 ||
	    inlen == 0 ||
	    outlen == 0 ||
	    outlen != inlen) {
		return TC_CRYPTO_FAIL;
	}
	(void) _copy(nonce, sizeof(nonce), ctr, sizeof(nonce));
	block_num = (uint16_t) ((nonce[14] << 8)|(nonce[15]));
	for (i = 0; i < inlen; ++i) {
		if ((i % (TC_AES_BLOCK_SIZE)) == 0) {
			block_num++;
			nonce[14] = (uint8_t)(block_num >> 8);
			nonce[15] = (uint8_t)(block_num);
			if (!tc_aes_encrypt(buffer, nonce, sched)) {
				return TC_CRYPTO_FAIL;
			}
		}
		*out++ = buffer[i % (TC_AES_BLOCK_SIZE)] ^ *in++;
	}
	ctr[14] = nonce[14]; ctr[15] = nonce[15];
	return TC_CRYPTO_SUCCESS;
}
static int32_t ccm_ctr_mode ( uint8_t * out , uint32_t outlen , const uint8_t * in , uint32_t inlen , uint8_t * ctr , const TCAesKeySched_t sched ) { uint8_t buffer [ TC_AES_BLOCK_SIZE ] ; uint8_t nonce [ TC_AES_BLOCK_SIZE ] ; uint16_t block_num ; uint32_t i ; if ( out == ( uint8_t * ) 0 || in == ( uint8_t * ) 0 || ctr == ( uint8_t * ) 0 || sched == ( TCAesKeySched_t ) 0 || inlen == 0 || outlen == 0 || outlen != inlen ) { return TC_CRYPTO_FAIL ; } ( void ) _copy ( nonce , sizeof ( nonce ) , ctr , sizeof ( nonce ) ) ; block_num = ( uint16_t ) ( ( nonce [ 14 ] << 8 ) | ( nonce [ 15 ] ) ) ; for ( i = 0 ; i < inlen ; ++ i ) { if ( ( i % ( TC_AES_BLOCK_SIZE ) ) == 0 ) { block_num ++ ; nonce [ 14 ] = ( uint8_t ) ( block_num >> 8 ) ; nonce [ 15 ] = ( uint8_t ) ( block_num ) ; if ( ! tc_aes_encrypt ( buffer , nonce , sched ) ) { return TC_CRYPTO_FAIL ; } } * out ++ = buffer [ i % ( TC_AES_BLOCK_SIZE ) ] ^ * in ++ ; } ctr [ 14 ] = nonce [ 14 ] ; ctr [ 15 ] = nonce [ 15 ] ; return TC_CRYPTO_SUCCESS ; }
Variation of CTR mode used in CCM.
Variation of CTR mode used in CCM .
/* input sanity check: */ /* copy the counter to the nonce */ /* select the last 2 bytes of the nonce to be incremented */ /* update the output */ /* update the counter */