Browse code

des: add possibility to calculate DES-CBC-MAC with small buffer

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>

David Goldwich authored on 2011/09/02 15:20:58
Showing 2 changed files
... ...
@@ -298,7 +298,7 @@ int av_des_init(AVDES *d, const uint8_t *key, int key_bits, int decrypt) {
298 298
     return 0;
299 299
 }
300 300
 
301
-void av_des_crypt(AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt) {
301
+static void av_des_crypt_mac(AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt, int mac) {
302 302
     uint64_t iv_val = iv ? AV_RB64(iv) : 0;
303 303
     while (count-- > 0) {
304 304
         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
321 321
         }
322 322
         AV_WB64(dst, dst_val);
323 323
         src += 8;
324
-        dst += 8;
324
+        if (!mac)
325
+            dst += 8;
325 326
     }
326 327
     if (iv)
327 328
         AV_WB64(iv, iv_val);
328 329
 }
329 330
 
331
+void av_des_crypt(AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt) {
332
+    av_des_crypt_mac(d, dst, src, count, iv, decrypt, 0);
333
+}
334
+
335
+void av_des_mac(AVDES *d, uint8_t *dst, const uint8_t *src, int count) {
336
+    av_des_crypt_mac(d, dst, src, count, (uint8_t[8]){0}, 0, 1);
337
+}
338
+
330 339
 #ifdef TEST
331 340
 #undef printf
332 341
 #undef rand
... ...
@@ -33,7 +33,7 @@ struct AVDES {
33 33
  * @brief Initializes an AVDES context.
34 34
  *
35 35
  * @param key_bits must be 64 or 192
36
- * @param decrypt 0 for encryption, 1 for decryption
36
+ * @param decrypt 0 for encryption/CBC-MAC, 1 for decryption
37 37
  */
38 38
 int av_des_init(struct AVDES *d, const uint8_t *key, int key_bits, int decrypt);
39 39
 
... ...
@@ -49,4 +49,13 @@ int av_des_init(struct AVDES *d, const uint8_t *key, int key_bits, int decrypt);
49 49
  */
50 50
 void av_des_crypt(struct AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt);
51 51
 
52
+/**
53
+ * @brief Calculates CBC-MAC using the DES algorithm.
54
+ *
55
+ * @param count number of 8 byte blocks
56
+ * @param dst destination array, can be equal to src, must be 8-byte aligned
57
+ * @param src source array, can be equal to dst, must be 8-byte aligned, may be NULL
58
+ */
59
+void av_des_mac(struct AVDES *d, uint8_t *dst, const uint8_t *src, int count);
60
+
52 61
 #endif /* AVUTIL_DES_H */