WvStreams
wvcountermode.cc
1 /*
2  * Worldvisions Tunnel Vision Software:
3  * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4  *
5  * A 'counter mode' cryptography engine abstraction.
6  */
7 #include "wvcountermode.h"
8 
9 
11  const void *_counter, size_t _countersize) :
12  keycrypt(_keycrypt), counter(NULL)
13 {
14  setcounter(_counter, _countersize);
15 }
16 
17 
18 WvCounterModeEncoder::~WvCounterModeEncoder()
19 {
20  delete keycrypt;
21  deletev counter;
22 }
23 
24 
25 void WvCounterModeEncoder::setcounter(const void *_counter, size_t _countersize)
26 {
27  deletev counter;
28  counter = new unsigned char[_countersize];
29  countersize = _countersize;
30  memcpy(counter, _counter, countersize);
31 }
32 
33 
34 void WvCounterModeEncoder::getcounter(void *_counter) const
35 {
36  memcpy(_counter, counter, countersize);
37 }
38 
39 
41 {
42  for (size_t i = 0; i < countersize && ! ++counter[i]; ++i);
43 }
44 
45 
47  bool flush)
48 {
49  bool success = true;
50  size_t avail = inbuf.used();
51  size_t offset = outbuf.used();
52 
53  // generate a key stream
54  size_t len;
55  for (len = avail; len >= countersize; len -= countersize)
56  {
57  counterbuf.reset(counter, countersize);
58  success = keycrypt->encode(counterbuf, outbuf, true);
59  if (! success) break;
60  incrcounter();
61  }
62  if (flush && len != 0 && success)
63  {
64  counterbuf.reset(counter, countersize);
65  success = keycrypt->encode(counterbuf, outbuf, true);
66  if (success)
67  {
68  outbuf.unalloc(countersize - len);
69  len = 0;
70  incrcounter();
71  }
72  else
73  outbuf.unalloc(outbuf.used() - offset - avail);
74  }
75  avail -= len;
76 
77  // XOR in the data
78  while (avail > 0)
79  {
80  len = outbuf.optpeekable(offset);
81  unsigned char *dataout = outbuf.mutablepeek(offset, len);
82  size_t lenopt = inbuf.optgettable();
83  if (len > lenopt)
84  len = lenopt;
85  const unsigned char *datain = inbuf.get(len);
86 
87  if (len >= avail)
88  {
89  len = avail;
90  avail = 0;
91  }
92  else
93  {
94  avail -= len;
95  offset += len;
96  }
97  while (len-- > 0)
98  *(dataout++) ^= *(datain++);
99  }
100  return success;
101 }
WvBufBaseCommonImpl::get
const T * get(size_t count)
Reads exactly the specified number of elements and returns a pointer to a storage location owned by t...
Definition: wvbufbase.h:114
WvEncoder
The base encoder class.
Definition: wvencoder.h:67
WvCounterModeEncoder::setcounter
void setcounter(const void *counter, size_t countersize)
Sets the Counter mode auto-incrementing counter.
Definition: wvcountermode.cc:25
WvBufBaseCommonImpl::unalloc
void unalloc(size_t count)
Unallocates exactly the specified number of elements by removing them from the buffer and releasing t...
Definition: wvbufbase.h:421
WvCounterModeEncoder::getcounter
void getcounter(void *counter) const
Stores the current counter in the supplied buffer.
Definition: wvcountermode.cc:34
WvBufBaseCommonImpl::optgettable
size_t optgettable() const
Returns the optimal maximum number of elements in the buffer currently available for reading without ...
Definition: wvbufbase.h:154
WvCounterModeEncoder::WvCounterModeEncoder
WvCounterModeEncoder(WvEncoder *_keycrypt, const void *_counter, size_t _countersize)
Create a new counter mode encoder / decoder.
Definition: wvcountermode.cc:10
WvEncoder::encode
bool encode(WvBuf &inbuf, WvBuf &outbuf, bool flush=false, bool finish=false)
Reads data from the input buffer, encodes it, and writes the result to the output buffer.
Definition: wvencoder.cc:36
WvCounterModeEncoder::incrcounter
virtual void incrcounter()
Increments the counter.
Definition: wvcountermode.cc:40
WvBufBase< unsigned char >
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers.
Definition: wvbuf.h:22
deletev
#define deletev
Remplacement for delete[].
Definition: delete.h:129
WvEncoder::flush
bool flush(WvBuf &inbuf, WvBuf &outbuf, bool finish=false)
Flushes the encoder and optionally finishes it.
Definition: wvencoder.h:163
WvBufBaseCommonImpl::used
size_t used() const
Returns the number of elements in the buffer currently available for reading.
Definition: wvbufbase.h:92
WvBufBaseCommonImpl::mutablepeek
T * mutablepeek(int offset, size_t count)
Returns a non-const pointer info the buffer at the specified offset to the specified number of elemen...
Definition: wvbufbase.h:461
WvCounterModeEncoder::_encode
virtual bool _encode(WvBuf &inbuf, WvBuf &outbuf, bool flush)
Template method implementation of encode().
Definition: wvcountermode.cc:46