X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fsec%2Fcipher.h;h=22eb313bdd27e3f08db35394962d786e1971f63d;hb=4d8a6a97c6c0e15296b51d5f02674079bede1aa0;hp=f67cb809d00d8a0530acb7b8069403aeee563418;hpb=20491f5ce0cb9b92b33e52d1d1015930618b00e8;p=bertos.git diff --git a/bertos/sec/cipher.h b/bertos/sec/cipher.h index f67cb809..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); } /*********************************************************************************/ @@ -168,4 +188,32 @@ void cipher_ctr_decrypt(BlockCipher *c, void *block); */ 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 */