From: rasky Date: Wed, 29 Sep 2010 16:35:39 +0000 (+0000) Subject: SEC: extract the CTR step into a public function, for algorithms that might use it. X-Git-Tag: 2.6.0~42 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=20491f5ce0cb9b92b33e52d1d1015930618b00e8;p=bertos.git SEC: extract the CTR step into a public function, for algorithms that might use it. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@4359 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/sec/cipher.c b/bertos/sec/cipher.c index 22bebe60..7977cc46 100644 --- a/bertos/sec/cipher.c +++ b/bertos/sec/cipher.c @@ -64,16 +64,21 @@ static void ctr_increment(void *buf, size_t len) return; } +void cipher_ctr_step(BlockCipher *c, void *block) +{ + memcpy(block, c->buf, c->block_len); + c->enc_block(c, block); + ctr_increment(c->buf, c->block_len); +} + void cipher_ctr_encrypt(BlockCipher *c, void *block) { uint8_t temp[c->block_len]; - memcpy(temp, c->buf, c->block_len); - c->enc_block(c, temp); + cipher_ctr_step(c, temp); xor_block(block, block, temp, c->block_len); PURGE(temp); - ctr_increment(c->buf, c->block_len); } void cipher_ctr_decrypt(BlockCipher *c, void *block) diff --git a/bertos/sec/cipher.h b/bertos/sec/cipher.h index f0f37a36..f67cb809 100644 --- a/bertos/sec/cipher.h +++ b/bertos/sec/cipher.h @@ -149,13 +149,23 @@ INLINE void cipher_ctr_begin(BlockCipher *c, void *counter) } /** - * Encrypt a block (in-place) using the current key in CBC mode. + * Encrypt a block (in-place) using the current key in CTR mode. */ void cipher_ctr_encrypt(BlockCipher *c, void *block); /** - * Decrypt a block (in-place) using the current key in CBC mode. + * Decrypt a block (in-place) using the current key in CTR mode. */ void cipher_ctr_decrypt(BlockCipher *c, void *block); +/** + * Generate the crypted stream block in CTR mode for the current + * counter, and then bump it. + * + * This function is basically the core CTR operation, without the final + * XOR pass with the plaintext or ciphertext. For normal CTR usage, + * you never need to call it. + */ +void cipher_ctr_step(BlockCipher *c, void *block); + #endif /* SEC_CIPHER_H */