~ubuntu-branches/debian/squeeze/sword/squeeze

« back to all changes in this revision

Viewing changes to src/modules/common/sapphire.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Marsden, Jonathan Marsden, Dmitrijs Ledkovs, Closed Bugs
  • Date: 2009-05-30 11:55:55 UTC
  • mfrom: (1.3.1 upstream) (6.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20090530115555-r427zsn3amivdpfu
Tags: 1.6.0+dfsg-1
[ Jonathan Marsden ]
* New upstream release. (Closes: #507960) (LP: #320558)
* debian/patches/02_libver.diff:
  - Bump SONAME to 8 -- SWORD 1.6 is not backward compatible with 1.5.11.
* debian/patches/series:
  - Remove 10_diatheke.diff -- included in upstream source.
* debian/patches/:
  - Remove several old unused .diff files.
  - Add 11_regex_only_when_needed.diff to conditionally include regex lib.
  - Add 12_fix_compiler_warnings.diff to remove all compiler warnings.
  - Add 13_fix_osis2mod_compression_default.diff from upstream svn.
  - Add 14_closing_section_not_chapter.diff from upstream svn.
* debian/libsword7.*: 
  - Rename to libsword8.*
  - Change libsword7 to libsword8 within files.
* debian/rules: 
  - SONAME bump to 8.
  - Set library version check to >= 1.6
* debian/control:
  - Change libsword7 to libsword8.
  - Add libsword7 to Conflicts.
  - Fix case of sword to SWORD in package descriptions.
  - Bump Standards-Version to 3.8.1 (no changes needed).
  - Fix section for libsword-dbg to avoid lintian warning.
* debian/rules:
  - Add DFSG get-orig-source target.
* debian/copyright:
  - Fix various mistakes in initial attempt to document copyrights.

[ Dmitrijs Ledkovs ]
* debian/rules: Added utils.mk to use missing-files target and call it on
  each build.
* debian/libsword-dev.install: Added libsword.la, previously missing.
* debian/libsword7.install: Added missing libicu translit files.
* debian/control:
  - Updated all uses of SWORD version to 1.6
  - Added libsword-dbg package
* debian/watch: Fixed a small mistake which was resulting in extra "."
  in final version name.
* debian/rules: simplified manpage processing.
* debian/libsword8.lintian-overrides: added override for module
  installation directory.
* debian/copyright: Updated with information about everyfile.
  Closes: #513448 LP: #322638
* debian/diatheke.examples: moved examples here from the diatheke.install
* debian/rules:
  - enabled shell script based testsuite
  - added commented out cppunit testsuite
* debian/patches/40_missing_includes.diff: 
  - added several missing stdio.h includes to prevent FTBFS of testsuite.

[ Closed Bugs ]
* FTBFS on intrepid (LP: #305172)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* sapphire.cpp -- the Saphire II stream cipher class.
 
2
   Dedicated to the Public Domain the author and inventor:
 
3
   (Michael Paul Johnson).  This code comes with no warranty.
 
4
   Use it at your own risk.
 
5
   Ported from the Pascal implementation of the Sapphire Stream
 
6
   Cipher 9 December 1994.
 
7
   Added hash pre- and post-processing 27 December 1994.
 
8
   Modified initialization to make index variables key dependent,
 
9
   made the output function more resistant to cryptanalysis,
 
10
   and renamed to Sapphire II 2 January 1995
 
11
*/
 
12
 
 
13
 
 
14
#include <string.h>
 
15
 
 
16
#include "sapphire.h"
 
17
 
 
18
SWORD_NAMESPACE_START
 
19
 
 
20
unsigned char sapphire::keyrand(int limit,
 
21
                                unsigned char *user_key,
 
22
                                unsigned char keysize,
 
23
                                unsigned char *rsum,
 
24
                                unsigned *keypos)
 
25
    {
 
26
    unsigned u,             // Value from 0 to limit to return.
 
27
        retry_limiter,      // No infinite loops allowed.
 
28
        mask;               // Select just enough bits.
 
29
 
 
30
    if (!limit) return 0;   // Avoid divide by zero error.
 
31
    retry_limiter = 0;
 
32
    mask = 1;               // Fill mask with enough bits to cover
 
33
    while (mask < (unsigned)limit)    // the desired range.
 
34
        mask = (mask << 1) + 1;
 
35
    do
 
36
        {
 
37
        *rsum = cards[*rsum] + user_key[(*keypos)++];
 
38
        if (*keypos >= keysize)
 
39
            {
 
40
            *keypos = 0;            // Recycle the user key.
 
41
            *rsum += keysize;   // key "aaaa" != key "aaaaaaaa"
 
42
            }
 
43
        u = mask & *rsum;
 
44
        if (++retry_limiter > 11)
 
45
            u %= limit;     // Prevent very rare long loops.
 
46
        }
 
47
    while (u > (unsigned)limit);
 
48
    return u;
 
49
    }
 
50
 
 
51
void sapphire::initialize(unsigned char *key, unsigned char keysize)
 
52
    {
 
53
    // Key size may be up to 256 bytes.
 
54
    // Pass phrases may be used directly, with longer length
 
55
    // compensating for the low entropy expected in such keys.
 
56
    // Alternatively, shorter keys hashed from a pass phrase or
 
57
    // generated randomly may be used. For random keys, lengths
 
58
    // of from 4 to 16 bytes are recommended, depending on how
 
59
    // secure you want this to be.
 
60
 
 
61
    int i;
 
62
    unsigned char toswap, swaptemp, rsum;
 
63
    unsigned keypos;
 
64
 
 
65
    // If we have been given no key, assume the default hash setup.
 
66
 
 
67
    if (keysize < 1)
 
68
        {
 
69
        hash_init();
 
70
        return;
 
71
        }
 
72
 
 
73
    // Start with cards all in order, one of each.
 
74
 
 
75
    for (i=0;i<256;i++)
 
76
        cards[i] = i;
 
77
 
 
78
    // Swap the card at each position with some other card.
 
79
 
 
80
    toswap = 0;
 
81
    keypos = 0;         // Start with first byte of user key.
 
82
    rsum = 0;
 
83
    for (i=255;i>=0;i--)
 
84
        {
 
85
        toswap = keyrand(i, key, keysize, &rsum, &keypos);
 
86
        swaptemp = cards[i];
 
87
        cards[i] = cards[toswap];
 
88
        cards[toswap] = swaptemp;
 
89
        }
 
90
 
 
91
    // Initialize the indices and data dependencies.
 
92
    // Indices are set to different values instead of all 0
 
93
    // to reduce what is known about the state of the cards
 
94
    // when the first byte is emitted.
 
95
 
 
96
    rotor = cards[1];
 
97
    ratchet = cards[3];
 
98
    avalanche = cards[5];
 
99
    last_plain = cards[7];
 
100
    last_cipher = cards[rsum];
 
101
 
 
102
    toswap = swaptemp = rsum = 0;
 
103
    keypos = 0;
 
104
    }
 
105
 
 
106
void sapphire::hash_init(void)
 
107
    {
 
108
    // This function is used to initialize non-keyed hash
 
109
    // computation.
 
110
 
 
111
    int i, j;
 
112
 
 
113
    // Initialize the indices and data dependencies.
 
114
 
 
115
    rotor = 1;
 
116
    ratchet = 3;
 
117
    avalanche = 5;
 
118
    last_plain = 7;
 
119
    last_cipher = 11;
 
120
 
 
121
    // Start with cards all in inverse order.
 
122
 
 
123
    for (i=0, j=255;i<256;i++,j--)
 
124
        cards[i] = (unsigned char) j;
 
125
    }
 
126
 
 
127
sapphire::sapphire(unsigned char *key, unsigned char keysize)
 
128
    {
 
129
    if (key && keysize)
 
130
        initialize(key, keysize);
 
131
    }
 
132
 
 
133
void sapphire::burn(void)
 
134
    {
 
135
    // Destroy the key and state information in RAM.
 
136
    memset(cards, 0, 256);
 
137
    rotor = ratchet = avalanche = last_plain = last_cipher = 0;
 
138
    }
 
139
 
 
140
sapphire::~sapphire()
 
141
    {
 
142
    burn();
 
143
    }
 
144
 
 
145
unsigned char sapphire::encrypt(unsigned char b)
 
146
    {
 
147
#ifdef USBINARY
 
148
    // Picture a single enigma rotor with 256 positions, rewired
 
149
    // on the fly by card-shuffling.
 
150
 
 
151
    // This cipher is a variant of one invented and written
 
152
    // by Michael Paul Johnson in November, 1993.
 
153
 
 
154
    unsigned char swaptemp;
 
155
 
 
156
    // Shuffle the deck a little more.
 
157
 
 
158
    ratchet += cards[rotor++];
 
159
    swaptemp = cards[last_cipher];
 
160
    cards[last_cipher] = cards[ratchet];
 
161
    cards[ratchet] = cards[last_plain];
 
162
    cards[last_plain] = cards[rotor];
 
163
    cards[rotor] = swaptemp;
 
164
    avalanche += cards[swaptemp];
 
165
 
 
166
    // Output one byte from the state in such a way as to make it
 
167
    // very hard to figure out which one you are looking at.
 
168
 
 
169
    last_cipher = b^cards[(cards[ratchet] + cards[rotor]) & 0xFF] ^
 
170
                  cards[cards[(cards[last_plain] +
 
171
                               cards[last_cipher] +
 
172
                               cards[avalanche])&0xFF]];
 
173
    last_plain = b;
 
174
    return last_cipher;
 
175
#else
 
176
    return b;
 
177
#endif
 
178
    }
 
179
 
 
180
unsigned char sapphire::decrypt(unsigned char b)
 
181
    {
 
182
    unsigned char swaptemp;
 
183
 
 
184
    // Shuffle the deck a little more.
 
185
 
 
186
    ratchet += cards[rotor++];
 
187
    swaptemp = cards[last_cipher];
 
188
    cards[last_cipher] = cards[ratchet];
 
189
    cards[ratchet] = cards[last_plain];
 
190
    cards[last_plain] = cards[rotor];
 
191
    cards[rotor] = swaptemp;
 
192
    avalanche += cards[swaptemp];
 
193
 
 
194
    // Output one byte from the state in such a way as to make it
 
195
    // very hard to figure out which one you are looking at.
 
196
 
 
197
    last_plain = b^cards[(cards[ratchet] + cards[rotor]) & 0xFF] ^
 
198
                   cards[cards[(cards[last_plain] +
 
199
                                cards[last_cipher] +
 
200
                                cards[avalanche])&0xFF]];
 
201
    last_cipher = b;
 
202
    return last_plain;
 
203
    }
 
204
 
 
205
void sapphire::hash_final(unsigned char *hash,      // Destination
 
206
                          unsigned char hashlength) // Size of hash.
 
207
    {
 
208
    int i;
 
209
 
 
210
    for (i=255;i>=0;i--)
 
211
        encrypt((unsigned char) i);
 
212
    for (i=0;i<hashlength;i++)
 
213
        hash[i] = encrypt(0);
 
214
    }
 
215
 
 
216
SWORD_NAMESPACE_END