X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fsec%2Fcipher.h;h=22eb313bdd27e3f08db35394962d786e1971f63d;hb=HEAD;hp=f0f37a36e600fce33169ebdaec7075a11944c637;hpb=b3a62d6cd1d6f304304956ea87ab9d5c1ff87031;p=bertos.git diff --git a/bertos/sec/cipher.h b/bertos/sec/cipher.h index f0f37a36..22eb313b 100644 --- a/bertos/sec/cipher.h +++ b/bertos/sec/cipher.h @@ -43,7 +43,7 @@ typedef struct BlockCipher { - void (*set_key)(struct BlockCipher *c, const void *key); + void (*set_key)(struct BlockCipher *c, const void *key, size_t len); void (*enc_block)(struct BlockCipher *c, void *block); void (*dec_block)(struct BlockCipher *c, void *block); @@ -54,7 +54,10 @@ typedef struct BlockCipher /** - * Return the key length (in bytes) + * Return the key length (in bytes). + * + * In case of ciphers that allow a variabile key size with a fixed state + * (eg: Blowfish), this returns the preferred key length. */ INLINE size_t cipher_key_len(BlockCipher *c) { @@ -73,12 +76,29 @@ INLINE size_t cipher_block_len(BlockCipher *c) * Set the current key used by the cipher. * * \note the buffer pointed by \a key is not modified and it is - * not needed anymore after this call returns. + * not needed anymore after this call returns. Its lenght must match + * the value returned by \a cipher_key_len(). */ INLINE void cipher_set_key(BlockCipher *c, const void *key) { ASSERT(c->set_key); - c->set_key(c, key); + c->set_key(c, key, c->key_len); +} + +/** + * Set the current key (of variable size) used by the cipher. + * + * This function is useful for ciphers that allow a variable size for the key + * (even with a fixed state). For all the other ciphers, the length must + * match the value returned by \a cipher_key_len(). + * + * \note the buffer pointed by \a key is not modified and it is + * not needed anymore after this call returns. + */ +INLINE void cipher_set_vkey(BlockCipher *c, const void *key, size_t len) +{ + ASSERT(c->set_key); + c->set_key(c, key, len); } /*********************************************************************************/ @@ -149,13 +169,51 @@ 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); + + +/*********************************************************************************/ +/* OFB mode */ +/*********************************************************************************/ + +/** + * Initialize OFB by setting the IV. + * + * \note the memory pointed by \a iv will be used and modified by the OFB + * functions. It is caller's responsibility to keep it available until there is + * no more OFB work to do. + */ +INLINE void cipher_ofb_begin(BlockCipher *c, void *iv) +{ + c->buf = iv; +} + +/** + * Encrypt a block (in-place) using the current key in OFB mode. + */ +void cipher_ofb_encrypt(BlockCipher *c, void *block); + +/** + * Decrypt a block (in-place) using the current key in OFB mode. + */ +void cipher_ofb_decrypt(BlockCipher *c, void *block); + + #endif /* SEC_CIPHER_H */