Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 2 | Copyright 1994 by Lance Ellinghouse, |
| 3 | Cathedral City, California Republic, United States of America. |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 11 | supporting documentation, and that the name of Lance Ellinghouse |
| 12 | not be used in advertising or publicity pertaining to distribution |
| 13 | of the software without specific, written prior permission. |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 15 | LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 17 | FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE BE LIABLE FOR ANY SPECIAL, |
| 18 | INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING |
| 19 | FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION |
| 21 | WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 22 | |
| 23 | ******************************************************************/ |
| 24 | |
| 25 | /* This creates an encryption and decryption engine I am calling |
| 26 | a rotor due to the original design was a harware rotor with |
| 27 | contacts used in Germany during WWII. |
| 28 | |
| 29 | Rotor Module: |
| 30 | |
| 31 | - rotor.newrotor('key') -> rotorobject (default of 6 rotors) |
| 32 | - rotor.newrotor('key', num_rotors) -> rotorobject |
| 33 | |
| 34 | Rotor Objects: |
| 35 | |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 36 | - ro.setkey('string') -> None (resets the key as defined in newrotor(). |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 37 | - ro.encrypt('string') -> encrypted string |
| 38 | - ro.decrypt('encrypted string') -> unencrypted string |
| 39 | |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 40 | - ro.encryptmore('string') -> encrypted string |
| 41 | - ro.decryptmore('encrypted string') -> unencrypted string |
| 42 | |
| 43 | NOTE: the {en,de}cryptmore() methods use the setup that was |
| 44 | established via the {en,de}crypt calls. They will NOT |
| 45 | re-initalize the rotors unless: 1) They have not been |
| 46 | initalized with {en,de}crypt since the last setkey() call; |
| 47 | 2) {en,de}crypt has not been called for this rotor yet. |
| 48 | |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 49 | NOTE: you MUST use the SAME key in rotor.newrotor() |
| 50 | if you wish to decrypt an encrypted string. |
| 51 | Also, the encrypted string is NOT 0-127 ASCII. |
| 52 | It is considered BINARY data. |
| 53 | |
| 54 | */ |
| 55 | |
| 56 | /* Rotor objects */ |
| 57 | |
Guido van Rossum | 602099a | 1994-09-14 13:32:22 +0000 | [diff] [blame] | 58 | #include "Python.h" |
Guido van Rossum | a320fd3 | 1995-03-09 12:14:15 +0000 | [diff] [blame] | 59 | #include "mymath.h" |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 61 | #define TRUE 1 |
| 62 | #define FALSE 0 |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 63 | |
| 64 | typedef struct { |
Guido van Rossum | 7b1e974 | 1994-08-29 10:46:42 +0000 | [diff] [blame] | 65 | PyObject_HEAD |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 66 | int seed[3]; |
| 67 | short key[5]; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 68 | int isinited; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 69 | int size; |
| 70 | int size_mask; |
| 71 | int rotors; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 72 | unsigned char *e_rotor; /* [num_rotors][size] */ |
| 73 | unsigned char *d_rotor; /* [num_rotors][size] */ |
| 74 | unsigned char *positions; /* [num_rotors] */ |
| 75 | unsigned char *advances; /* [num_rotors] */ |
| 76 | } Rotorobj; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 77 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 78 | staticforward PyTypeObject Rotor_Type; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 79 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 80 | #define is_rotor(v) ((v)->ob_type == &Rotor_Type) |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | This defines the necessary routines to manage rotor objects |
| 84 | */ |
| 85 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 86 | static void |
| 87 | set_seed(r) |
| 88 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 89 | { |
| 90 | r->seed[0] = r->key[0]; |
| 91 | r->seed[1] = r->key[1]; |
| 92 | r->seed[2] = r->key[2]; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 93 | r->isinited = FALSE; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* Return the next random number in the range [0.0 .. 1.0) */ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 97 | static float |
| 98 | r_random(r) |
| 99 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 100 | { |
| 101 | int x, y, z; |
| 102 | float val, term; |
| 103 | |
| 104 | x = r->seed[0]; |
| 105 | y = r->seed[1]; |
| 106 | z = r->seed[2]; |
| 107 | |
| 108 | x = 171 * (x % 177) - 2 * (x/177); |
| 109 | y = 172 * (y % 176) - 35 * (y/176); |
| 110 | z = 170 * (z % 178) - 63 * (z/178); |
| 111 | |
| 112 | if (x < 0) x = x + 30269; |
| 113 | if (y < 0) y = y + 30307; |
| 114 | if (z < 0) z = z + 30323; |
| 115 | |
| 116 | r->seed[0] = x; |
| 117 | r->seed[1] = y; |
| 118 | r->seed[2] = z; |
| 119 | |
| 120 | term = (float)( |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 121 | (((float)x)/(float)30269.0) + |
| 122 | (((float)y)/(float)30307.0) + |
| 123 | (((float)z)/(float)30323.0) |
| 124 | ); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 125 | val = term - (float)floor((double)term); |
| 126 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 127 | if (val >= 1.0) |
| 128 | val = 0.0; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 129 | |
| 130 | return val; |
| 131 | } |
| 132 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 133 | static short |
| 134 | r_rand(r, s) |
| 135 | Rotorobj *r; |
| 136 | short s; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 137 | { |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 138 | /*short tmp = (short)((int)(r_random(r) * (float)32768.0) % 32768);*/ |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 139 | short tmp = (short)((short)(r_random(r) * (float)s) % s); |
| 140 | return tmp; |
| 141 | } |
| 142 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 143 | static void |
| 144 | set_key(r, key) |
| 145 | Rotorobj *r; |
| 146 | char *key; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 147 | { |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 148 | #ifdef BUGGY_CODE_BW_COMPAT |
| 149 | /* See comments below */ |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 150 | int k1=995, k2=576, k3=767, k4=671, k5=463; |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 151 | #else |
| 152 | unsigned long k1=995, k2=576, k3=767, k4=671, k5=463; |
| 153 | #endif |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 154 | int i; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 155 | int len = strlen(key); |
| 156 | for (i = 0; i < len; i++) { |
Guido van Rossum | 0667626 | 1993-11-01 16:20:18 +0000 | [diff] [blame] | 157 | #ifdef BUGGY_CODE_BW_COMPAT |
| 158 | /* This is the code as it was originally released. |
| 159 | It causes warnings on many systems and can generate |
| 160 | different results as well. If you have files |
| 161 | encrypted using an older version you may want to |
| 162 | #define BUGGY_CODE_BW_COMPAT so as to be able to |
| 163 | decrypt them... */ |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 164 | k1 = (((k1<<3 | k1<<-13) + key[i]) & 65535); |
| 165 | k2 = (((k2<<3 | k2<<-13) ^ key[i]) & 65535); |
| 166 | k3 = (((k3<<3 | k3<<-13) - key[i]) & 65535); |
| 167 | k4 = ((key[i] - (k4<<3 | k4<<-13)) & 65535); |
| 168 | k5 = (((k5<<3 | k5<<-13) ^ ~key[i]) & 65535); |
Guido van Rossum | 0667626 | 1993-11-01 16:20:18 +0000 | [diff] [blame] | 169 | #else |
| 170 | /* This code should be more portable */ |
| 171 | k1 = (((k1<<3 | k1>>13) + key[i]) & 65535); |
| 172 | k2 = (((k2<<3 | k2>>13) ^ key[i]) & 65535); |
| 173 | k3 = (((k3<<3 | k3>>13) - key[i]) & 65535); |
| 174 | k4 = ((key[i] - (k4<<3 | k4>>13)) & 65535); |
| 175 | k5 = (((k5<<3 | k5>>13) ^ ~key[i]) & 65535); |
| 176 | #endif |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 177 | } |
| 178 | r->key[0] = (short)k1; |
| 179 | r->key[1] = (short)(k2|1); |
| 180 | r->key[2] = (short)k3; |
| 181 | r->key[3] = (short)k4; |
| 182 | r->key[4] = (short)k5; |
| 183 | |
| 184 | set_seed(r); |
| 185 | } |
| 186 | |
| 187 | /* These define the interface to a rotor object */ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 188 | static Rotorobj * |
| 189 | rotorobj_new(num_rotors, key) |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 190 | int num_rotors; |
| 191 | char *key; |
| 192 | { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 193 | Rotorobj *xp; |
| 194 | |
| 195 | xp = PyObject_NEW(Rotorobj, &Rotor_Type); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 196 | if (xp == NULL) |
| 197 | return NULL; |
Guido van Rossum | e6e9fe18 | 1992-07-31 15:11:01 +0000 | [diff] [blame] | 198 | set_key(xp, key); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 199 | |
| 200 | xp->size = 256; |
| 201 | xp->size_mask = xp->size - 1; |
| 202 | xp->size_mask = 0; |
| 203 | xp->rotors = num_rotors; |
Guido van Rossum | e6e9fe18 | 1992-07-31 15:11:01 +0000 | [diff] [blame] | 204 | xp->e_rotor = NULL; |
| 205 | xp->d_rotor = NULL; |
| 206 | xp->positions = NULL; |
| 207 | xp->advances = NULL; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 208 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 209 | if (!(xp->e_rotor = PyMem_NEW(unsigned char, num_rotors * xp->size))) |
| 210 | goto finally; |
| 211 | if (!(xp->d_rotor = PyMem_NEW(unsigned char, num_rotors * xp->size))) |
| 212 | goto finally; |
| 213 | if (!(xp->positions = PyMem_NEW(unsigned char, num_rotors))) |
| 214 | goto finally; |
| 215 | if (!(xp->advances = PyMem_NEW(unsigned char, num_rotors))) |
| 216 | goto finally; |
| 217 | |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 218 | return xp; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 219 | |
| 220 | finally: |
| 221 | PyMem_XDEL(xp->e_rotor); |
| 222 | PyMem_XDEL(xp->d_rotor); |
| 223 | PyMem_XDEL(xp->positions); |
| 224 | PyMem_XDEL(xp->advances); |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 225 | Py_DECREF(xp); |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 226 | return (Rotorobj*)PyErr_NoMemory(); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* These routines impliment the rotor itself */ |
| 230 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 231 | /* Here is a fairly sofisticated {en,de}cryption system. It is based |
| 232 | on the idea of a "rotor" machine. A bunch of rotors, each with a |
| 233 | different permutation of the alphabet, rotate around a different amount |
| 234 | after encrypting one character. The current state of the rotors is |
| 235 | used to encrypt one character. |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 236 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 237 | The code is smart enought to tell if your alphabet has a number of |
| 238 | characters equal to a power of two. If it does, it uses logical |
| 239 | operations, if not it uses div and mod (both require a division). |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 240 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 241 | You will need to make two changes to the code 1) convert to c, and |
| 242 | customize for an alphabet of 255 chars 2) add a filter at the begining, |
| 243 | and end, which subtracts one on the way in, and adds one on the way |
| 244 | out. |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 245 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 246 | You might wish to do some timing studies. Another viable alternative |
| 247 | is to "byte stuff" the encrypted data of a normal (perhaps this one) |
| 248 | encryption routine. |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 249 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 250 | j' |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 251 | */ |
| 252 | |
| 253 | /*(defun RTR-make-id-rotor (rotor) |
| 254 | "Set ROTOR to the identity permutation" |
| 255 | (let ((j 0)) |
| 256 | (while (< j RTR-size) |
| 257 | (aset rotor j j) |
| 258 | (setq j (+ 1 j))) |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 259 | rotor)) |
| 260 | */ |
| 261 | static void |
| 262 | RTR_make_id_rotor(r, rtr) |
| 263 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 264 | unsigned char *rtr; |
| 265 | { |
| 266 | register int j; |
| 267 | register int size = r->size; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 268 | for (j = 0; j < size; j++) { |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 269 | rtr[j] = (unsigned char)j; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | |
| 274 | /*(defvar RTR-e-rotors |
| 275 | (let ((rv (make-vector RTR-number-of-rotors 0)) |
| 276 | (i 0) |
| 277 | tr) |
| 278 | (while (< i RTR-number-of-rotors) |
| 279 | (setq tr (make-vector RTR-size 0)) |
| 280 | (RTR-make-id-rotor tr) |
| 281 | (aset rv i tr) |
| 282 | (setq i (+ 1 i))) |
| 283 | rv) |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 284 | "The current set of encryption rotors") |
| 285 | */ |
| 286 | static void |
| 287 | RTR_e_rotors(r) |
| 288 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 289 | { |
| 290 | int i; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 291 | for (i = 0; i < r->rotors; i++) { |
| 292 | RTR_make_id_rotor(r, &(r->e_rotor[(i*r->size)])); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | /*(defvar RTR-d-rotors |
| 297 | (let ((rv (make-vector RTR-number-of-rotors 0)) |
| 298 | (i 0) |
| 299 | tr) |
| 300 | (while (< i RTR-number-of-rotors) |
| 301 | (setq tr (make-vector RTR-size 0)) |
| 302 | (setq j 0) |
| 303 | (while (< j RTR-size) |
| 304 | (aset tr j j) |
| 305 | (setq j (+ 1 j))) |
| 306 | (aset rv i tr) |
| 307 | (setq i (+ 1 i))) |
| 308 | rv) |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 309 | "The current set of decryption rotors") |
| 310 | */ |
| 311 | static void |
| 312 | RTR_d_rotors(r) |
| 313 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 314 | { |
| 315 | register int i, j; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 316 | for (i = 0; i < r->rotors; i++) { |
| 317 | for (j = 0; j < r->size; j++) { |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 318 | r->d_rotor[((i*r->size)+j)] = (unsigned char)j; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /*(defvar RTR-positions (make-vector RTR-number-of-rotors 1) |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 324 | "The positions of the rotors at this time") |
| 325 | */ |
| 326 | static void |
| 327 | RTR_positions(r) |
| 328 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 329 | { |
| 330 | int i; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 331 | for (i = 0; i < r->rotors; i++) { |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 332 | r->positions[i] = 1; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /*(defvar RTR-advances (make-vector RTR-number-of-rotors 1) |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 337 | "The number of positions to advance the rotors at a time") |
| 338 | */ |
| 339 | static void |
| 340 | RTR_advances(r) |
| 341 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 342 | { |
| 343 | int i; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 344 | for (i = 0; i < r->rotors; i++) { |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 345 | r->advances[i] = 1; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /*(defun RTR-permute-rotor (e d) |
| 350 | "Permute the E rotor, and make the D rotor its inverse" |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 351 | ;; see Knuth for explaination of algorithm. |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 352 | (RTR-make-id-rotor e) |
| 353 | (let ((i RTR-size) |
| 354 | q j) |
| 355 | (while (<= 2 i) |
| 356 | (setq q (fair16 i)) ; a little tricky, decrement here |
| 357 | (setq i (- i 1)) ; since we have origin 0 array's |
| 358 | (setq j (aref e q)) |
| 359 | (aset e q (aref e i)) |
| 360 | (aset e i j) |
| 361 | (aset d j i)) |
| 362 | (aset e 0 (aref e 0)) ; don't forget e[0] and d[0] |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 363 | (aset d (aref e 0) 0))) |
| 364 | */ |
| 365 | static void |
| 366 | RTR_permute_rotor(r, e, d) |
| 367 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 368 | unsigned char *e; |
| 369 | unsigned char *d; |
| 370 | { |
| 371 | short i = r->size; |
| 372 | short q; |
| 373 | unsigned char j; |
| 374 | RTR_make_id_rotor(r,e); |
| 375 | while (2 <= i) { |
| 376 | q = r_rand(r,i); |
| 377 | i--; |
| 378 | j = e[q]; |
| 379 | e[q] = (unsigned char)e[i]; |
| 380 | e[i] = (unsigned char)j; |
| 381 | d[j] = (unsigned char)i; |
| 382 | } |
| 383 | e[0] = (unsigned char)e[0]; |
| 384 | d[(e[0])] = (unsigned char)0; |
| 385 | } |
| 386 | |
| 387 | /*(defun RTR-init (key) |
| 388 | "Given KEY (a list of 5 16 bit numbers), initialize the rotor machine. |
| 389 | Set the advancement, position, and permutation of the rotors" |
| 390 | (R16-set-state key) |
| 391 | (let (i) |
| 392 | (setq i 0) |
| 393 | (while (< i RTR-number-of-rotors) |
| 394 | (aset RTR-positions i (fair16 RTR-size)) |
| 395 | (aset RTR-advances i (+ 1 (* 2 (fair16 (/ RTR-size 2))))) |
| 396 | (message "Initializing rotor %d..." i) |
| 397 | (RTR-permute-rotor (aref RTR-e-rotors i) (aref RTR-d-rotors i)) |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 398 | (setq i (+ 1 i))))) |
| 399 | */ |
| 400 | static void |
| 401 | RTR_init(r) |
| 402 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 403 | { |
| 404 | int i; |
| 405 | set_seed(r); |
| 406 | RTR_positions(r); |
| 407 | RTR_advances(r); |
| 408 | RTR_e_rotors(r); |
| 409 | RTR_d_rotors(r); |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 410 | for (i = 0; i < r->rotors; i++) { |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 411 | r->positions[i] = r_rand(r,r->size); |
| 412 | r->advances[i] = (1+(2*(r_rand(r,r->size/2)))); |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 413 | RTR_permute_rotor(r, |
| 414 | &(r->e_rotor[(i*r->size)]), |
| 415 | &(r->d_rotor[(i*r->size)])); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 416 | } |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 417 | r->isinited = TRUE; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /*(defun RTR-advance () |
| 421 | "Change the RTR-positions vector, using the RTR-advances vector" |
| 422 | (let ((i 0) |
| 423 | (temp 0)) |
| 424 | (if RTR-size-mask |
| 425 | (while (< i RTR-number-of-rotors) |
| 426 | (setq temp (+ (aref RTR-positions i) (aref RTR-advances i))) |
| 427 | (aset RTR-positions i (logand temp RTR-size-mask)) |
| 428 | (if (and (>= temp RTR-size) |
| 429 | (< i (- RTR-number-of-rotors 1))) |
| 430 | (aset RTR-positions (+ i 1) |
| 431 | (+ 1 (aref RTR-positions (+ i 1))))) |
| 432 | (setq i (+ i 1))) |
| 433 | (while (< i RTR-number-of-rotors) |
| 434 | (setq temp (+ (aref RTR-positions i) (aref RTR-advances i))) |
| 435 | (aset RTR-positions i (% temp RTR-size)) |
| 436 | (if (and (>= temp RTR-size) |
| 437 | (< i (- RTR-number-of-rotors 1))) |
| 438 | (aset RTR-positions (+ i 1) |
| 439 | (+ 1 (aref RTR-positions (+ i 1))))) |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 440 | (setq i (+ i 1)))))) |
| 441 | */ |
| 442 | static void |
| 443 | RTR_advance(r) |
| 444 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 445 | { |
| 446 | register int i=0, temp=0; |
| 447 | if (r->size_mask) { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 448 | while (i < r->rotors) { |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 449 | temp = r->positions[i] + r->advances[i]; |
| 450 | r->positions[i] = temp & r->size_mask; |
| 451 | if ((temp >= r->size) && (i < (r->rotors - 1))) { |
| 452 | r->positions[(i+1)] = 1 + r->positions[(i+1)]; |
| 453 | } |
| 454 | i++; |
| 455 | } |
| 456 | } else { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 457 | while (i < r->rotors) { |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 458 | temp = r->positions[i] + r->advances[i]; |
| 459 | r->positions[i] = temp%r->size; |
| 460 | if ((temp >= r->size) && (i < (r->rotors - 1))) { |
| 461 | r->positions[(i+1)] = 1 + r->positions[(i+1)]; |
| 462 | } |
| 463 | i++; |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | /*(defun RTR-e-char (p) |
| 469 | "Encrypt the character P with the current rotor machine" |
| 470 | (let ((i 0)) |
| 471 | (if RTR-size-mask |
| 472 | (while (< i RTR-number-of-rotors) |
| 473 | (setq p (aref (aref RTR-e-rotors i) |
| 474 | (logand (logxor (aref RTR-positions i) |
| 475 | p) |
| 476 | RTR-size-mask))) |
| 477 | (setq i (+ 1 i))) |
| 478 | (while (< i RTR-number-of-rotors) |
| 479 | (setq p (aref (aref RTR-e-rotors i) |
| 480 | (% (logxor (aref RTR-positions i) |
| 481 | p) |
| 482 | RTR-size))) |
| 483 | (setq i (+ 1 i)))) |
| 484 | (RTR-advance) |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 485 | p)) |
| 486 | */ |
| 487 | static unsigned char |
| 488 | RTR_e_char(r, p) |
| 489 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 490 | unsigned char p; |
| 491 | { |
| 492 | register int i=0; |
| 493 | register unsigned char tp=p; |
| 494 | if (r->size_mask) { |
| 495 | while (i < r->rotors) { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 496 | tp = r->e_rotor[(i*r->size) + |
| 497 | (((r->positions[i] ^ tp) & |
| 498 | r->size_mask))]; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 499 | i++; |
| 500 | } |
| 501 | } else { |
| 502 | while (i < r->rotors) { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 503 | tp = r->e_rotor[(i*r->size) + |
| 504 | (((r->positions[i] ^ tp) % |
| 505 | (unsigned int) r->size))]; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 506 | i++; |
| 507 | } |
| 508 | } |
| 509 | RTR_advance(r); |
| 510 | return ((unsigned char)tp); |
| 511 | } |
| 512 | |
| 513 | /*(defun RTR-d-char (c) |
| 514 | "Decrypt the character C with the current rotor machine" |
| 515 | (let ((i (- RTR-number-of-rotors 1))) |
| 516 | (if RTR-size-mask |
| 517 | (while (<= 0 i) |
| 518 | (setq c (logand (logxor (aref RTR-positions i) |
| 519 | (aref (aref RTR-d-rotors i) |
| 520 | c)) |
| 521 | RTR-size-mask)) |
| 522 | (setq i (- i 1))) |
| 523 | (while (<= 0 i) |
| 524 | (setq c (% (logxor (aref RTR-positions i) |
| 525 | (aref (aref RTR-d-rotors i) |
| 526 | c)) |
| 527 | RTR-size)) |
| 528 | (setq i (- i 1)))) |
| 529 | (RTR-advance) |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 530 | c)) |
| 531 | */ |
| 532 | static unsigned char |
| 533 | RTR_d_char(r, c) |
| 534 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 535 | unsigned char c; |
| 536 | { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 537 | register int i = r->rotors - 1; |
| 538 | register unsigned char tc = c; |
| 539 | |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 540 | if (r->size_mask) { |
| 541 | while (0 <= i) { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 542 | tc = (r->positions[i] ^ |
| 543 | r->d_rotor[(i*r->size)+tc]) & r->size_mask; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 544 | i--; |
| 545 | } |
| 546 | } else { |
| 547 | while (0 <= i) { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 548 | tc = (r->positions[i] ^ |
| 549 | r->d_rotor[(i*r->size)+tc]) % |
| 550 | (unsigned int) r->size; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 551 | i--; |
| 552 | } |
| 553 | } |
| 554 | RTR_advance(r); |
| 555 | return(tc); |
| 556 | } |
| 557 | |
| 558 | /*(defun RTR-e-region (beg end key) |
| 559 | "Perform a rotor encryption of the region from BEG to END by KEY" |
| 560 | (save-excursion |
| 561 | (let ((tenth (/ (- end beg) 10))) |
| 562 | (RTR-init key) |
| 563 | (goto-char beg) |
| 564 | ;; ### make it stop evry 10% or so to tell us |
| 565 | (while (< (point) end) |
| 566 | (let ((fc (following-char))) |
| 567 | (insert-char (RTR-e-char fc) 1) |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 568 | (delete-char 1)))))) |
| 569 | */ |
| 570 | static void |
| 571 | RTR_e_region(r, beg, len, doinit) |
| 572 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 573 | unsigned char *beg; |
| 574 | int len; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 575 | int doinit; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 576 | { |
| 577 | register int i; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 578 | if (doinit || r->isinited == FALSE) |
| 579 | RTR_init(r); |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 580 | for (i = 0; i < len; i++) { |
| 581 | beg[i] = RTR_e_char(r, beg[i]); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
| 585 | /*(defun RTR-d-region (beg end key) |
| 586 | "Perform a rotor decryption of the region from BEG to END by KEY" |
| 587 | (save-excursion |
| 588 | (progn |
| 589 | (RTR-init key) |
| 590 | (goto-char beg) |
| 591 | (while (< (point) end) |
| 592 | (let ((fc (following-char))) |
| 593 | (insert-char (RTR-d-char fc) 1) |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 594 | (delete-char 1)))))) |
| 595 | */ |
| 596 | static void |
| 597 | RTR_d_region(r, beg, len, doinit) |
| 598 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 599 | unsigned char *beg; |
| 600 | int len; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 601 | int doinit; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 602 | { |
| 603 | register int i; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 604 | if (doinit || r->isinited == FALSE) |
| 605 | RTR_init(r); |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 606 | for (i = 0; i < len; i++) { |
| 607 | beg[i] = RTR_d_char(r, beg[i]); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
| 611 | |
| 612 | /*(defun RTR-key-string-to-ints (key) |
| 613 | "Convert a string into a list of 4 numbers" |
| 614 | (let ((k1 995) |
| 615 | (k2 576) |
| 616 | (k3 767) |
| 617 | (k4 671) |
| 618 | (k5 463) |
| 619 | (i 0)) |
| 620 | (while (< i (length key)) |
| 621 | (setq k1 (logand (+ (logior (lsh k1 3) (lsh k1 -13)) (aref key i)) 65535)) |
| 622 | (setq k2 (logand (logxor (logior (lsh k2 3) (lsh k2 -13)) (aref key i)) 65535)) |
| 623 | (setq k3 (logand (- (logior (lsh k3 3) (lsh k3 -13)) (aref key i)) 65535)) |
| 624 | (setq k4 (logand (- (aref key i) (logior (lsh k4 3) (lsh k4 -13))) 65535)) |
| 625 | (setq k5 (logand (logxor (logior (lsh k5 3) (lsh k5 -13)) (lognot (aref key i))) 65535)) |
| 626 | (setq i (+ i 1))) |
| 627 | (list k1 (logior 1 k2) k3 k4 k5)))*/ |
| 628 | /* This is done in set_key() above */ |
| 629 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 630 | #if 0 |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 631 | /*(defun encrypt-region (beg end key) |
| 632 | "Interactivly encrypt the region" |
| 633 | (interactive "r\nsKey:") |
| 634 | (RTR-e-region beg end (RTR-key-string-to-ints key)))*/ |
| 635 | static void encrypt_region(r, region, len) |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 636 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 637 | unsigned char *region; |
| 638 | int len; |
| 639 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 640 | RTR_e_region(r,region,len,TRUE); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /*(defun decrypt-region (beg end key) |
| 644 | "Interactivly decrypt the region" |
| 645 | (interactive "r\nsKey:") |
| 646 | (RTR-d-region beg end (RTR-key-string-to-ints key)))*/ |
| 647 | static void decrypt_region(r, region, len) |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 648 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 649 | unsigned char *region; |
| 650 | int len; |
| 651 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 652 | RTR_d_region(r,region,len,TRUE); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 653 | } |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 654 | #endif /* 0 */ |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 655 | |
| 656 | /* Rotor methods */ |
| 657 | |
| 658 | static void |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 659 | rotor_dealloc(xp) |
| 660 | Rotorobj *xp; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 661 | { |
Guido van Rossum | 7b1e974 | 1994-08-29 10:46:42 +0000 | [diff] [blame] | 662 | PyMem_XDEL(xp->e_rotor); |
| 663 | PyMem_XDEL(xp->d_rotor); |
| 664 | PyMem_XDEL(xp->positions); |
| 665 | PyMem_XDEL(xp->advances); |
| 666 | PyMem_DEL(xp); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 669 | static PyObject * |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 670 | rotorobj_encrypt(self, args) |
| 671 | Rotorobj *self; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 672 | PyObject * args; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 673 | { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 674 | char *string = NULL; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 675 | int len = 0; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 676 | PyObject *rtn = NULL; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 677 | char *tmp; |
| 678 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 679 | if (!PyArg_Parse(args, "s#", &string, &len)) |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 680 | return NULL; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 681 | if (!(tmp = PyMem_NEW(char, len+5))) { |
Guido van Rossum | 7b1e974 | 1994-08-29 10:46:42 +0000 | [diff] [blame] | 682 | PyErr_NoMemory(); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 683 | return NULL; |
| 684 | } |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 685 | memset(tmp, '\0', len+1); |
| 686 | memcpy(tmp, string, len); |
| 687 | RTR_e_region(self, (unsigned char *)tmp, len, TRUE); |
| 688 | rtn = PyString_FromStringAndSize(tmp, len); |
| 689 | PyMem_DEL(tmp); |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 690 | return(rtn); |
| 691 | } |
| 692 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 693 | static PyObject * |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 694 | rotorobj_encrypt_more(self, args) |
| 695 | Rotorobj *self; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 696 | PyObject * args; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 697 | { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 698 | char *string = NULL; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 699 | int len = 0; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 700 | PyObject *rtn = NULL; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 701 | char *tmp; |
| 702 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 703 | if (!PyArg_Parse(args, "s#", &string, &len)) |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 704 | return NULL; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 705 | if (!(tmp = PyMem_NEW(char, len+5))) { |
Guido van Rossum | 7b1e974 | 1994-08-29 10:46:42 +0000 | [diff] [blame] | 706 | PyErr_NoMemory(); |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 707 | return NULL; |
| 708 | } |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 709 | memset(tmp, '\0', len+1); |
| 710 | memcpy(tmp, string, len); |
| 711 | RTR_e_region(self, (unsigned char *)tmp, len, FALSE); |
| 712 | rtn = PyString_FromStringAndSize(tmp, len); |
| 713 | PyMem_DEL(tmp); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 714 | return(rtn); |
| 715 | } |
| 716 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 717 | static PyObject * |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 718 | rotorobj_decrypt(self, args) |
| 719 | Rotorobj *self; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 720 | PyObject * args; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 721 | { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 722 | char *string = NULL; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 723 | int len = 0; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 724 | PyObject *rtn = NULL; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 725 | char *tmp; |
| 726 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 727 | if (!PyArg_Parse(args, "s#", &string, &len)) |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 728 | return NULL; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 729 | if (!(tmp = PyMem_NEW(char, len+5))) { |
Guido van Rossum | 7b1e974 | 1994-08-29 10:46:42 +0000 | [diff] [blame] | 730 | PyErr_NoMemory(); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 731 | return NULL; |
| 732 | } |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 733 | memset(tmp, '\0', len+1); |
| 734 | memcpy(tmp, string, len); |
| 735 | RTR_d_region(self, (unsigned char *)tmp, len, TRUE); |
| 736 | rtn = PyString_FromStringAndSize(tmp, len); |
| 737 | PyMem_DEL(tmp); |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 738 | return(rtn); |
| 739 | } |
| 740 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 741 | static PyObject * |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 742 | rotorobj_decrypt_more(self, args) |
| 743 | Rotorobj *self; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 744 | PyObject * args; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 745 | { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 746 | char *string = NULL; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 747 | int len = 0; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 748 | PyObject *rtn = NULL; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 749 | char *tmp; |
| 750 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 751 | if (!PyArg_Parse(args, "s#", &string, &len)) |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 752 | return NULL; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 753 | if (!(tmp = PyMem_NEW(char, len+5))) { |
Guido van Rossum | 7b1e974 | 1994-08-29 10:46:42 +0000 | [diff] [blame] | 754 | PyErr_NoMemory(); |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 755 | return NULL; |
| 756 | } |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 757 | memset(tmp, '\0', len+1); |
| 758 | memcpy(tmp, string, len); |
| 759 | RTR_d_region(self, (unsigned char *)tmp, len, FALSE); |
| 760 | rtn = PyString_FromStringAndSize(tmp, len); |
| 761 | PyMem_DEL(tmp); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 762 | return(rtn); |
| 763 | } |
| 764 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 765 | static PyObject * |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 766 | rotorobj_setkey(self, args) |
| 767 | Rotorobj *self; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 768 | PyObject * args; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 769 | { |
Barry Warsaw | 9e3fceb | 1997-01-02 20:36:36 +0000 | [diff] [blame^] | 770 | char *key; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 771 | |
Barry Warsaw | 9e3fceb | 1997-01-02 20:36:36 +0000 | [diff] [blame^] | 772 | if (!PyArg_ParseTuple(args, "s", &key)) |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 773 | return NULL; |
| 774 | |
Barry Warsaw | 9e3fceb | 1997-01-02 20:36:36 +0000 | [diff] [blame^] | 775 | set_key(self, key); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 776 | Py_INCREF(Py_None); |
| 777 | return Py_None; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 780 | static struct PyMethodDef |
| 781 | rotorobj_methods[] = { |
| 782 | {"encrypt", (PyCFunction)rotorobj_encrypt}, |
| 783 | {"encryptmore", (PyCFunction)rotorobj_encrypt_more}, |
| 784 | {"decrypt", (PyCFunction)rotorobj_decrypt}, |
| 785 | {"decryptmore", (PyCFunction)rotorobj_decrypt_more}, |
| 786 | {"setkey", (PyCFunction)rotorobj_setkey, 1}, |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 787 | {NULL, NULL} /* sentinel */ |
| 788 | }; |
| 789 | |
| 790 | |
| 791 | /* Return a rotor object's named attribute. */ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 792 | static PyObject * |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 793 | rotorobj_getattr(s, name) |
| 794 | Rotorobj *s; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 795 | char *name; |
| 796 | { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 797 | return Py_FindMethod(rotorobj_methods, (PyObject*)s, name); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 800 | |
| 801 | statichere PyTypeObject Rotor_Type = { |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 802 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 803 | 0, /*ob_size*/ |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 804 | "rotor", /*tp_name*/ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 805 | sizeof(Rotorobj), /*tp_size*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 806 | 0, /*tp_itemsize*/ |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 807 | /* methods */ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 808 | (destructor)rotor_dealloc, /*tp_dealloc*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 809 | 0, /*tp_print*/ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 810 | (getattrfunc)rotorobj_getattr, /*tp_getattr*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 811 | 0, /*tp_setattr*/ |
| 812 | 0, /*tp_compare*/ |
| 813 | 0, /*tp_repr*/ |
Guido van Rossum | 7b1e974 | 1994-08-29 10:46:42 +0000 | [diff] [blame] | 814 | 0, /*tp_hash*/ |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 815 | }; |
| 816 | |
| 817 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 818 | static PyObject * |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 819 | rotor_rotor(self, args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 820 | PyObject * self; |
| 821 | PyObject * args; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 822 | { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 823 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 824 | char *string; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 825 | int len; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 826 | int num_rotors = 6; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 827 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 828 | if (!PyArg_ParseTuple(args, "s#|i", &string, &len, &num_rotors)) |
| 829 | return NULL; |
| 830 | |
| 831 | r = rotorobj_new(num_rotors, string); |
| 832 | return (PyObject *)r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 835 | |
| 836 | static struct PyMethodDef |
| 837 | rotor_methods[] = { |
| 838 | {"newrotor", rotor_rotor, 1}, |
| 839 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 840 | }; |
| 841 | |
| 842 | |
| 843 | /* Initialize this module. |
| 844 | This is called when the first 'import rotor' is done, |
| 845 | via a table in config.c, if config.c is compiled with USE_ROTOR |
| 846 | defined. */ |
| 847 | |
| 848 | void |
| 849 | initrotor() |
| 850 | { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 851 | (void)Py_InitModule("rotor", rotor_methods); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 852 | } |