~ubuntu-branches/ubuntu/intrepid/libcrypto++/intrepid

« back to all changes in this revision

Viewing changes to modes.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Pierre Machard
  • Date: 2004-08-27 12:35:05 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040827123505-7evgxiu7k8memiyk
Tags: 5.2.1a-1
* Urgency set to high because lastest upload was unclean
* Rename libcrypto++-5.2.1.orig.tar.gz in  libcrypto++-5.2.1a.orig.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// modes.cpp - written and placed in the public domain by Wei Dai
2
2
 
3
3
#include "pch.h"
 
4
 
 
5
#ifndef CRYPTOPP_IMPORTS
 
6
 
4
7
#include "modes.h"
5
8
 
 
9
#ifndef NDEBUG
6
10
#include "des.h"
7
 
 
8
 
#include "strciphr.cpp"
 
11
#endif
9
12
 
10
13
NAMESPACE_BEGIN(CryptoPP)
11
14
 
 
15
#ifndef NDEBUG
12
16
void Modes_TestInstantiations()
13
17
{
14
18
        CFB_Mode<DES>::Encryption m0;
18
22
        ECB_Mode<DES>::Encryption m4;
19
23
        CBC_Mode<DES>::Encryption m5;
20
24
}
21
 
 
22
 
// explicit instantiations for Darwin gcc-932.1
23
 
template class CFB_CipherTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >;
24
 
template class CFB_EncryptionTemplate<>;
25
 
template class CFB_DecryptionTemplate<>;
26
 
template class AdditiveCipherTemplate<>;
27
 
template class CFB_CipherTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> >;
28
 
template class CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> >;
29
 
template class CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> >;
30
 
template class AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, OFB_ModePolicy> >;
31
 
template class AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, CTR_ModePolicy> >;
 
25
#endif
32
26
 
33
27
void CipherModeBase::SetKey(const byte *key, unsigned int length, const NameValuePairs &params)
34
28
{
35
 
        UncheckedSetKey(params, key, length);   // the underlying cipher will check the key length
 
29
        UncheckedSetKey(params, key, length, GetIVAndThrowIfInvalid(params));   // the underlying cipher will check the key length
36
30
}
37
31
 
38
32
void CipherModeBase::GetNextIV(byte *IV)
44
38
        memcpy(IV, m_register, BlockSize());
45
39
}
46
40
 
47
 
void CipherModeBase::SetIV(const byte *iv)
48
 
{
49
 
        if (iv)
50
 
                Resynchronize(iv);
51
 
        else if (IsResynchronizable())
52
 
        {
53
 
                if (!CanUseStructuredIVs())
54
 
                        throw InvalidArgument("CipherModeBase: this cipher mode cannot use a null IV");
55
 
 
56
 
                // use all zeros as default IV
57
 
                SecByteBlock iv(BlockSize());
58
 
                memset(iv, 0, iv.size());
59
 
                Resynchronize(iv);
60
 
        }
61
 
}
62
 
 
63
 
void CTR_ModePolicy::SeekToIteration(dword iterationCount)
 
41
void CTR_ModePolicy::SeekToIteration(lword iterationCount)
64
42
{
65
43
        int carry=0;
66
44
        for (int i=BlockSize()-1; i>=0; i--)
80
58
 
81
59
static inline void IncrementCounterByOne(byte *output, const byte *input, unsigned int s)
82
60
{
83
 
        for (int i=s-1, carry=1; i>=0; i--)
84
 
                carry = !(output[i] = input[i]+carry) && carry;
 
61
        int i, carry;
 
62
        for (i=s-1, carry=1; i>=0 && carry; i--)
 
63
                carry = !(output[i] = input[i]+1);
 
64
        memcpy(output, input, i+1);
 
65
}
 
66
 
 
67
void CTR_ModePolicy::GetNextIV(byte *IV)
 
68
{
 
69
        IncrementCounterByOne(IV, m_counterArray, BlockSize());
85
70
}
86
71
 
87
72
inline void CTR_ModePolicy::ProcessMultipleBlocks(byte *output, const byte *input, unsigned int n)
126
111
void CTR_ModePolicy::CipherResynchronize(byte *keystreamBuffer, const byte *iv)
127
112
{
128
113
        unsigned int s = BlockSize();
129
 
        memcpy(m_register, iv, s);
 
114
        CopyOrZero(m_register, iv, s);
130
115
        m_counterArray.New(s * m_cipher->OptimalNumberOfParallelBlocks());
131
 
        memcpy(m_counterArray, iv, s);
 
116
        CopyOrZero(m_counterArray, iv, s);
132
117
}
133
118
 
134
 
void BlockOrientedCipherModeBase::UncheckedSetKey(const NameValuePairs &params, const byte *key, unsigned int length)
 
119
void BlockOrientedCipherModeBase::UncheckedSetKey(const NameValuePairs &params, const byte *key, unsigned int length, const byte *iv)
135
120
{
136
121
        m_cipher->SetKey(key, length, params);
137
122
        ResizeBuffers();
138
 
        const byte *iv = params.GetValueWithDefault(Name::IV(), (const byte *)NULL);
139
 
        SetIV(iv);
 
123
        if (IsResynchronizable())
 
124
                Resynchronize(iv);
140
125
}
141
126
 
142
127
void BlockOrientedCipherModeBase::ProcessData(byte *outString, const byte *inString, unsigned int length)
264
249
}
265
250
 
266
251
NAMESPACE_END
 
252
 
 
253
#endif