This patch adds the possibility to calculate the DES-CBC-MAC of a source buffer (i.e. the last block of the buffer encrypted in CBC mode) without having to allocate a destination buffer that is as long as the complete source buffer, but instead only 8 bytes for the MAC. Signed-off-by: David Goldwich <david.goldwich@gmail.com> Signed-off-by: Anton Khirnov <anton@khirnov.net>tags/n0.9
| @@ -298,7 +298,7 @@ int av_des_init(AVDES *d, const uint8_t *key, int key_bits, int decrypt) { | |||||
| return 0; | return 0; | ||||
| } | } | ||||
| void av_des_crypt(AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt) { | |||||
| static void av_des_crypt_mac(AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt, int mac) { | |||||
| uint64_t iv_val = iv ? AV_RB64(iv) : 0; | uint64_t iv_val = iv ? AV_RB64(iv) : 0; | ||||
| while (count-- > 0) { | while (count-- > 0) { | ||||
| uint64_t dst_val; | uint64_t dst_val; | ||||
| @@ -321,12 +321,21 @@ void av_des_crypt(AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t | |||||
| } | } | ||||
| AV_WB64(dst, dst_val); | AV_WB64(dst, dst_val); | ||||
| src += 8; | src += 8; | ||||
| dst += 8; | |||||
| if (!mac) | |||||
| dst += 8; | |||||
| } | } | ||||
| if (iv) | if (iv) | ||||
| AV_WB64(iv, iv_val); | AV_WB64(iv, iv_val); | ||||
| } | } | ||||
| void av_des_crypt(AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt) { | |||||
| av_des_crypt_mac(d, dst, src, count, iv, decrypt, 0); | |||||
| } | |||||
| void av_des_mac(AVDES *d, uint8_t *dst, const uint8_t *src, int count) { | |||||
| av_des_crypt_mac(d, dst, src, count, (uint8_t[8]){0}, 0, 1); | |||||
| } | |||||
| #ifdef TEST | #ifdef TEST | ||||
| #undef printf | #undef printf | ||||
| #undef rand | #undef rand | ||||
| @@ -33,7 +33,7 @@ struct AVDES { | |||||
| * @brief Initializes an AVDES context. | * @brief Initializes an AVDES context. | ||||
| * | * | ||||
| * @param key_bits must be 64 or 192 | * @param key_bits must be 64 or 192 | ||||
| * @param decrypt 0 for encryption, 1 for decryption | |||||
| * @param decrypt 0 for encryption/CBC-MAC, 1 for decryption | |||||
| */ | */ | ||||
| int av_des_init(struct AVDES *d, const uint8_t *key, int key_bits, int decrypt); | int av_des_init(struct AVDES *d, const uint8_t *key, int key_bits, int decrypt); | ||||
| @@ -49,4 +49,13 @@ int av_des_init(struct AVDES *d, const uint8_t *key, int key_bits, int decrypt); | |||||
| */ | */ | ||||
| void av_des_crypt(struct AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt); | void av_des_crypt(struct AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt); | ||||
| /** | |||||
| * @brief Calculates CBC-MAC using the DES algorithm. | |||||
| * | |||||
| * @param count number of 8 byte blocks | |||||
| * @param dst destination array, can be equal to src, must be 8-byte aligned | |||||
| * @param src source array, can be equal to dst, must be 8-byte aligned, may be NULL | |||||
| */ | |||||
| void av_des_mac(struct AVDES *d, uint8_t *dst, const uint8_t *src, int count); | |||||
| #endif /* AVUTIL_DES_H */ | #endif /* AVUTIL_DES_H */ | ||||