fn ccm_ctr_mode(out: &mut [u8], r#in: &[u8], ctr: &mut [u8], cipher: &Aes128) {
    let inlen = r#in.len();

    let mut buffer = [0u8; AES_BLOCK_SIZE];
    let mut nonce = [0u8; AES_BLOCK_SIZE];
    // Copy the counter to the nonce
    nonce.copy_from_slice(ctr);

    // Select the last 2 bytes of the nonce to be incremented
    let mut block_num = u16::from(nonce[14]) << 8 | u16::from(nonce[15]);
    for i in 0..inlen {
        if i % AES_BLOCK_SIZE == 0 {
            block_num += 1;
            nonce[14] = (block_num >> 8) as u8;
            nonce[15] = block_num as u8;
            // Encrypt the nonce into the buffer
            buffer.copy_from_slice(&nonce);
            cipher.encrypt_block(GenericArray::from_mut_slice(&mut buffer));
        }
        // Update the output
        out[i] = buffer[i % AES_BLOCK_SIZE] ^ r#in[i];
    }

    // Update the counter
    ctr[14] = nonce[14];
    ctr[15] = nonce[15];
}
/// 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 .
fn ccm_ctr_mode(out: &mut [u8], r#in: &[u8], ctr: &mut [u8], cipher: &Aes128) {
    let inlen = r#in.len();
    let mut buffer = [0u8; AES_BLOCK_SIZE];
    let mut nonce = [0u8; AES_BLOCK_SIZE];
    // Copy the counter to the nonce
    nonce.copy_from_slice(ctr);
    // Select the last 2 bytes of the nonce to be incremented
    let mut block_num = u16::from(nonce[14]) << 8 | u16::from(nonce[15]);
    for i in 0..inlen {
        if i % AES_BLOCK_SIZE == 0 {
            block_num += 1;
            nonce[14] = (block_num >> 8) as u8;
            nonce[15] = block_num as u8;
            // Encrypt the nonce into the buffer
            buffer.copy_from_slice(&nonce);
            cipher.encrypt_block(GenericArray::from_mut_slice(&mut buffer));
        }
        // Update the output
        out[i] = buffer[i % AES_BLOCK_SIZE] ^ r#in[i];
    }
    // Update the counter
    ctr[14] = nonce[14];
    ctr[15] = nonce[15];
}
fn ccm_ctr_mode ( out : & mut [ u8 ] , r#in : & [ u8 ] , ctr : & mut [ u8 ] , cipher : & Aes128 ) { let inlen = r#in . len ( ) ; let mut buffer = [ 0u8 ; AES_BLOCK_SIZE ] ; let mut nonce = [ 0u8 ; AES_BLOCK_SIZE ] ; // Copy the counter to the nonce nonce . copy_from_slice ( ctr ) ; // Select the last 2 bytes of the nonce to be incremented let mut block_num = u16 :: from ( nonce [ 14 ] ) << 8 | u16 :: from ( nonce [ 15 ] ) ; for i in 0 .. inlen { if i % AES_BLOCK_SIZE == 0 { block_num += 1 ; nonce [ 14 ] = ( block_num >> 8 ) as u8 ; nonce [ 15 ] = block_num as u8 ; // Encrypt the nonce into the buffer buffer . copy_from_slice ( & nonce ) ; cipher . encrypt_block ( GenericArray :: from_mut_slice ( & mut buffer ) ) ; } // Update the output out [ i ] = buffer [ i % AES_BLOCK_SIZE ] ^ r#in [ i ] ; } // Update the counter ctr [ 14 ] = nonce [ 14 ] ; ctr [ 15 ] = nonce [ 15 ] ; }
Variation of CTR mode used in CCM.
Variation of CTR mode used in CCM .
out
&mut [u8]
r#in
&[u8]
ctr
&mut [u8]
cipher
&Aes128
out
&mut [u8]
r#in
&[u8]
ctr
&mut [u8]
cipher
&Aes128