~ubuntu-branches/ubuntu/utopic/dropbear/utopic-proposed

« back to all changes in this revision

Viewing changes to libtomcrypt/src/modes/cbc/cbc_decrypt.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Johnston
  • Date: 2005-12-08 19:20:21 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051208192021-nyp9rwnt77nsg6ty
Tags: 0.47-1
* New upstream release.
* SECURITY: Fix incorrect buffer sizing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
 
2
 *
 
3
 * LibTomCrypt is a library that provides various cryptographic
 
4
 * algorithms in a highly modular and flexible manner.
 
5
 *
 
6
 * The library is free for all purposes without any express
 
7
 * guarantee it works.
 
8
 *
 
9
 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
 
10
 */
 
11
#include "tomcrypt.h"
 
12
 
 
13
/**
 
14
   @file cbc_decrypt.c
 
15
   CBC implementation, encrypt block, Tom St Denis
 
16
*/
 
17
 
 
18
 
 
19
#ifdef CBC
 
20
 
 
21
/**
 
22
  CBC decrypt
 
23
  @param ct     Ciphertext
 
24
  @param pt     [out] Plaintext
 
25
  @param len    The number of bytes to process (must be multiple of block length)
 
26
  @param cbc    CBC state
 
27
  @return CRYPT_OK if successful
 
28
*/
 
29
int cbc_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CBC *cbc)
 
30
{
 
31
   int x, err;
 
32
   unsigned char tmp[16];
 
33
#ifdef LTC_FAST
 
34
   LTC_FAST_TYPE tmpy;
 
35
#else
 
36
   unsigned char tmpy;
 
37
#endif         
 
38
 
 
39
   LTC_ARGCHK(pt  != NULL);
 
40
   LTC_ARGCHK(ct  != NULL);
 
41
   LTC_ARGCHK(cbc != NULL);
 
42
 
 
43
   if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
 
44
       return err;
 
45
   }
 
46
   
 
47
   /* is blocklen valid? */
 
48
   if (cbc->blocklen < 0 || cbc->blocklen > (int)sizeof(cbc->IV)) {
 
49
      return CRYPT_INVALID_ARG;
 
50
   }    
 
51
 
 
52
   if (len % cbc->blocklen) {
 
53
      return CRYPT_INVALID_ARG;
 
54
   }
 
55
#ifdef LTC_FAST
 
56
   if (len % sizeof(LTC_FAST_TYPE)) {   
 
57
      return CRYPT_INVALID_ARG;
 
58
   }
 
59
#endif
 
60
   
 
61
   if (cipher_descriptor[cbc->cipher].accel_cbc_decrypt != NULL) {
 
62
      cipher_descriptor[cbc->cipher].accel_cbc_decrypt(ct, pt, len / cbc->blocklen, cbc->IV, &cbc->key);
 
63
   } else {
 
64
      while (len) {
 
65
         /* decrypt */
 
66
         cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key);
 
67
 
 
68
         /* xor IV against plaintext */
 
69
         #if defined(LTC_FAST)
 
70
             for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
 
71
                 tmpy = *((LTC_FAST_TYPE*)((unsigned char *)cbc->IV + x)) ^ *((LTC_FAST_TYPE*)((unsigned char *)tmp + x));
 
72
                 *((LTC_FAST_TYPE*)((unsigned char *)cbc->IV + x)) = *((LTC_FAST_TYPE*)((unsigned char *)ct + x));
 
73
                 *((LTC_FAST_TYPE*)((unsigned char *)pt + x)) = tmpy;
 
74
             }
 
75
         #else 
 
76
            for (x = 0; x < cbc->blocklen; x++) {
 
77
               tmpy       = tmp[x] ^ cbc->IV[x];
 
78
               cbc->IV[x] = ct[x];
 
79
               pt[x]      = tmpy;
 
80
            }
 
81
         #endif
 
82
       
 
83
         ct  += cbc->blocklen;
 
84
         pt  += cbc->blocklen;
 
85
         len -= cbc->blocklen;
 
86
      }
 
87
   }
 
88
   return CRYPT_OK;
 
89
}
 
90
 
 
91
#endif
 
92
 
 
93
/* $Source: /cvs/libtom/libtomcrypt/src/modes/cbc/cbc_decrypt.c,v $ */
 
94
/* $Revision: 1.9 $ */
 
95
/* $Date: 2005/05/05 14:35:59 $ */