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 | 19a6c8a | 1997-05-20 15:58:36 +0000 | [diff] [blame] | 61 | #ifndef TRUE |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 62 | #define TRUE 1 |
Guido van Rossum | 19a6c8a | 1997-05-20 15:58:36 +0000 | [diff] [blame] | 63 | #endif |
| 64 | #ifndef FALSE |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 65 | #define FALSE 0 |
Guido van Rossum | 19a6c8a | 1997-05-20 15:58:36 +0000 | [diff] [blame] | 66 | #endif |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 67 | |
| 68 | typedef struct { |
Guido van Rossum | 7b1e974 | 1994-08-29 10:46:42 +0000 | [diff] [blame] | 69 | PyObject_HEAD |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 70 | int seed[3]; |
| 71 | short key[5]; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 72 | int isinited; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 73 | int size; |
| 74 | int size_mask; |
| 75 | int rotors; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 76 | unsigned char *e_rotor; /* [num_rotors][size] */ |
| 77 | unsigned char *d_rotor; /* [num_rotors][size] */ |
| 78 | unsigned char *positions; /* [num_rotors] */ |
| 79 | unsigned char *advances; /* [num_rotors] */ |
| 80 | } Rotorobj; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 81 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 82 | staticforward PyTypeObject Rotor_Type; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 83 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 84 | #define is_rotor(v) ((v)->ob_type == &Rotor_Type) |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 85 | |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 86 | |
| 87 | /* This defines the necessary routines to manage rotor objects */ |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 88 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 89 | static void |
| 90 | set_seed(r) |
| 91 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 92 | { |
| 93 | r->seed[0] = r->key[0]; |
| 94 | r->seed[1] = r->key[1]; |
| 95 | r->seed[2] = r->key[2]; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 96 | r->isinited = FALSE; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* Return the next random number in the range [0.0 .. 1.0) */ |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 100 | static double |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 101 | r_random(r) |
| 102 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 103 | { |
| 104 | int x, y, z; |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 105 | double val, term; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 106 | |
| 107 | x = r->seed[0]; |
| 108 | y = r->seed[1]; |
| 109 | z = r->seed[2]; |
| 110 | |
| 111 | x = 171 * (x % 177) - 2 * (x/177); |
| 112 | y = 172 * (y % 176) - 35 * (y/176); |
| 113 | z = 170 * (z % 178) - 63 * (z/178); |
| 114 | |
| 115 | if (x < 0) x = x + 30269; |
| 116 | if (y < 0) y = y + 30307; |
| 117 | if (z < 0) z = z + 30323; |
| 118 | |
| 119 | r->seed[0] = x; |
| 120 | r->seed[1] = y; |
| 121 | r->seed[2] = z; |
| 122 | |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 123 | term = (double)( |
| 124 | (((double)x)/(double)30269.0) + |
| 125 | (((double)y)/(double)30307.0) + |
| 126 | (((double)z)/(double)30323.0) |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 127 | ); |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 128 | val = term - (double)floor((double)term); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 129 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 130 | if (val >= 1.0) |
| 131 | val = 0.0; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 132 | |
| 133 | return val; |
| 134 | } |
| 135 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 136 | static short |
| 137 | r_rand(r, s) |
| 138 | Rotorobj *r; |
| 139 | short s; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 140 | { |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 141 | return (short)((short)(r_random(r) * (double)s) % s); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 144 | static void |
| 145 | set_key(r, key) |
| 146 | Rotorobj *r; |
| 147 | char *key; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 148 | { |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 149 | unsigned long k1=995, k2=576, k3=767, k4=671, k5=463; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 150 | int i; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 151 | int len = strlen(key); |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 152 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 153 | for (i = 0; i < len; i++) { |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 154 | unsigned short ki = Py_CHARMASK(key[i]); |
| 155 | |
| 156 | k1 = (((k1<<3 | k1>>13) + ki) & 65535); |
| 157 | k2 = (((k2<<3 | k2>>13) ^ ki) & 65535); |
| 158 | k3 = (((k3<<3 | k3>>13) - ki) & 65535); |
| 159 | k4 = ((ki - (k4<<3 | k4>>13)) & 65535); |
| 160 | k5 = (((k5<<3 | k5>>13) ^ ~ki) & 65535); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 161 | } |
| 162 | r->key[0] = (short)k1; |
| 163 | r->key[1] = (short)(k2|1); |
| 164 | r->key[2] = (short)k3; |
| 165 | r->key[3] = (short)k4; |
| 166 | r->key[4] = (short)k5; |
| 167 | |
| 168 | set_seed(r); |
| 169 | } |
| 170 | |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 171 | |
| 172 | |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 173 | /* These define the interface to a rotor object */ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 174 | static Rotorobj * |
| 175 | rotorobj_new(num_rotors, key) |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 176 | int num_rotors; |
| 177 | char *key; |
| 178 | { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 179 | Rotorobj *xp; |
| 180 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame^] | 181 | xp = PyObject_New(Rotorobj, &Rotor_Type); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 182 | if (xp == NULL) |
| 183 | return NULL; |
Guido van Rossum | e6e9fe18 | 1992-07-31 15:11:01 +0000 | [diff] [blame] | 184 | set_key(xp, key); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 185 | |
| 186 | xp->size = 256; |
| 187 | xp->size_mask = xp->size - 1; |
| 188 | xp->size_mask = 0; |
| 189 | xp->rotors = num_rotors; |
Guido van Rossum | e6e9fe18 | 1992-07-31 15:11:01 +0000 | [diff] [blame] | 190 | xp->e_rotor = NULL; |
| 191 | xp->d_rotor = NULL; |
| 192 | xp->positions = NULL; |
| 193 | xp->advances = NULL; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 194 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 195 | if (!(xp->e_rotor = PyMem_NEW(unsigned char, num_rotors * xp->size))) |
| 196 | goto finally; |
| 197 | if (!(xp->d_rotor = PyMem_NEW(unsigned char, num_rotors * xp->size))) |
| 198 | goto finally; |
| 199 | if (!(xp->positions = PyMem_NEW(unsigned char, num_rotors))) |
| 200 | goto finally; |
| 201 | if (!(xp->advances = PyMem_NEW(unsigned char, num_rotors))) |
| 202 | goto finally; |
| 203 | |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 204 | return xp; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 205 | |
| 206 | finally: |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame^] | 207 | if (xp->e_rotor) |
| 208 | PyMem_DEL(xp->e_rotor); |
| 209 | if (xp->d_rotor) |
| 210 | PyMem_DEL(xp->d_rotor); |
| 211 | if (xp->positions) |
| 212 | PyMem_DEL(xp->positions); |
| 213 | if (xp->advances) |
| 214 | PyMem_DEL(xp->advances); |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 215 | Py_DECREF(xp); |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 216 | return (Rotorobj*)PyErr_NoMemory(); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 219 | |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 220 | /* These routines impliment the rotor itself */ |
| 221 | |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 222 | /* Here is a fairly sophisticated {en,de}cryption system. It is based on |
| 223 | the idea of a "rotor" machine. A bunch of rotors, each with a |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 224 | different permutation of the alphabet, rotate around a different amount |
| 225 | after encrypting one character. The current state of the rotors is |
| 226 | used to encrypt one character. |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 227 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 228 | The code is smart enought to tell if your alphabet has a number of |
| 229 | characters equal to a power of two. If it does, it uses logical |
| 230 | 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] | 231 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 232 | You will need to make two changes to the code 1) convert to c, and |
| 233 | customize for an alphabet of 255 chars 2) add a filter at the begining, |
| 234 | and end, which subtracts one on the way in, and adds one on the way |
| 235 | out. |
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 | You might wish to do some timing studies. Another viable alternative |
| 238 | is to "byte stuff" the encrypted data of a normal (perhaps this one) |
| 239 | encryption routine. |
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 | j' |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 242 | |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 243 | */ |
| 244 | |
| 245 | /* Note: the C code here is a fairly straightforward transliteration of a |
| 246 | * rotor implemented in lisp. The original lisp code has been removed from |
| 247 | * this file to for simplification, but I've kept the docstrings as |
| 248 | * comments in front of the functions. |
| 249 | */ |
| 250 | |
| 251 | |
| 252 | /* Set ROTOR to the identity permutation */ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 253 | static void |
| 254 | RTR_make_id_rotor(r, rtr) |
| 255 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 256 | unsigned char *rtr; |
| 257 | { |
| 258 | register int j; |
| 259 | register int size = r->size; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 260 | for (j = 0; j < size; j++) { |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 261 | rtr[j] = (unsigned char)j; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 266 | /* The current set of encryption rotors */ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 267 | static void |
| 268 | RTR_e_rotors(r) |
| 269 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 270 | { |
| 271 | int i; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 272 | for (i = 0; i < r->rotors; i++) { |
| 273 | RTR_make_id_rotor(r, &(r->e_rotor[(i*r->size)])); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 277 | /* The current set of decryption rotors */ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 278 | static void |
| 279 | RTR_d_rotors(r) |
| 280 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 281 | { |
| 282 | register int i, j; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 283 | for (i = 0; i < r->rotors; i++) { |
| 284 | for (j = 0; j < r->size; j++) { |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 285 | r->d_rotor[((i*r->size)+j)] = (unsigned char)j; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 290 | /* The positions of the rotors at this time */ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 291 | static void |
| 292 | RTR_positions(r) |
| 293 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 294 | { |
| 295 | int i; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 296 | for (i = 0; i < r->rotors; i++) { |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 297 | r->positions[i] = 1; |
| 298 | } |
| 299 | } |
| 300 | |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 301 | /* The number of positions to advance the rotors at a time */ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 302 | static void |
| 303 | RTR_advances(r) |
| 304 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 305 | { |
| 306 | int i; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 307 | for (i = 0; i < r->rotors; i++) { |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 308 | r->advances[i] = 1; |
| 309 | } |
| 310 | } |
| 311 | |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 312 | /* Permute the E rotor, and make the D rotor its inverse |
| 313 | * see Knuth for explanation of algorithm. |
| 314 | */ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 315 | static void |
| 316 | RTR_permute_rotor(r, e, d) |
| 317 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 318 | unsigned char *e; |
| 319 | unsigned char *d; |
| 320 | { |
| 321 | short i = r->size; |
| 322 | short q; |
| 323 | unsigned char j; |
| 324 | RTR_make_id_rotor(r,e); |
| 325 | while (2 <= i) { |
| 326 | q = r_rand(r,i); |
| 327 | i--; |
| 328 | j = e[q]; |
| 329 | e[q] = (unsigned char)e[i]; |
| 330 | e[i] = (unsigned char)j; |
| 331 | d[j] = (unsigned char)i; |
| 332 | } |
| 333 | e[0] = (unsigned char)e[0]; |
| 334 | d[(e[0])] = (unsigned char)0; |
| 335 | } |
| 336 | |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 337 | /* Given KEY (a list of 5 16 bit numbers), initialize the rotor machine. |
| 338 | * Set the advancement, position, and permutation of the rotors |
| 339 | */ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 340 | static void |
| 341 | RTR_init(r) |
| 342 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 343 | { |
| 344 | int i; |
| 345 | set_seed(r); |
| 346 | RTR_positions(r); |
| 347 | RTR_advances(r); |
| 348 | RTR_e_rotors(r); |
| 349 | RTR_d_rotors(r); |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 350 | for (i = 0; i < r->rotors; i++) { |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 351 | r->positions[i] = (unsigned char) r_rand(r,r->size); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 352 | r->advances[i] = (1+(2*(r_rand(r,r->size/2)))); |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 353 | RTR_permute_rotor(r, |
| 354 | &(r->e_rotor[(i*r->size)]), |
| 355 | &(r->d_rotor[(i*r->size)])); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 356 | } |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 357 | r->isinited = TRUE; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 360 | /* Change the RTR-positions vector, using the RTR-advances vector */ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 361 | static void |
| 362 | RTR_advance(r) |
| 363 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 364 | { |
| 365 | register int i=0, temp=0; |
| 366 | if (r->size_mask) { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 367 | while (i < r->rotors) { |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 368 | temp = r->positions[i] + r->advances[i]; |
| 369 | r->positions[i] = temp & r->size_mask; |
| 370 | if ((temp >= r->size) && (i < (r->rotors - 1))) { |
| 371 | r->positions[(i+1)] = 1 + r->positions[(i+1)]; |
| 372 | } |
| 373 | i++; |
| 374 | } |
| 375 | } else { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 376 | while (i < r->rotors) { |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 377 | temp = r->positions[i] + r->advances[i]; |
| 378 | r->positions[i] = temp%r->size; |
| 379 | if ((temp >= r->size) && (i < (r->rotors - 1))) { |
| 380 | r->positions[(i+1)] = 1 + r->positions[(i+1)]; |
| 381 | } |
| 382 | i++; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 387 | /* Encrypt the character P with the current rotor machine */ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 388 | static unsigned char |
| 389 | RTR_e_char(r, p) |
| 390 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 391 | unsigned char p; |
| 392 | { |
| 393 | register int i=0; |
| 394 | register unsigned char tp=p; |
| 395 | if (r->size_mask) { |
| 396 | while (i < r->rotors) { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 397 | tp = r->e_rotor[(i*r->size) + |
| 398 | (((r->positions[i] ^ tp) & |
| 399 | r->size_mask))]; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 400 | i++; |
| 401 | } |
| 402 | } else { |
| 403 | while (i < r->rotors) { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 404 | tp = r->e_rotor[(i*r->size) + |
| 405 | (((r->positions[i] ^ tp) % |
| 406 | (unsigned int) r->size))]; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 407 | i++; |
| 408 | } |
| 409 | } |
| 410 | RTR_advance(r); |
| 411 | return ((unsigned char)tp); |
| 412 | } |
| 413 | |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 414 | /* Decrypt the character C with the current rotor machine */ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 415 | static unsigned char |
| 416 | RTR_d_char(r, c) |
| 417 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 418 | unsigned char c; |
| 419 | { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 420 | register int i = r->rotors - 1; |
| 421 | register unsigned char tc = c; |
| 422 | |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 423 | if (r->size_mask) { |
| 424 | while (0 <= i) { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 425 | tc = (r->positions[i] ^ |
| 426 | r->d_rotor[(i*r->size)+tc]) & r->size_mask; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 427 | i--; |
| 428 | } |
| 429 | } else { |
| 430 | while (0 <= i) { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 431 | tc = (r->positions[i] ^ |
| 432 | r->d_rotor[(i*r->size)+tc]) % |
| 433 | (unsigned int) r->size; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 434 | i--; |
| 435 | } |
| 436 | } |
| 437 | RTR_advance(r); |
| 438 | return(tc); |
| 439 | } |
| 440 | |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 441 | /* Perform a rotor encryption of the region from BEG to END by KEY */ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 442 | static void |
| 443 | RTR_e_region(r, beg, len, doinit) |
| 444 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 445 | unsigned char *beg; |
| 446 | int len; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 447 | int doinit; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 448 | { |
| 449 | register int i; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 450 | if (doinit || r->isinited == FALSE) |
| 451 | RTR_init(r); |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 452 | for (i = 0; i < len; i++) { |
| 453 | beg[i] = RTR_e_char(r, beg[i]); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 457 | /* Perform a rotor decryption of the region from BEG to END by KEY */ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 458 | static void |
| 459 | RTR_d_region(r, beg, len, doinit) |
| 460 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 461 | unsigned char *beg; |
| 462 | int len; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 463 | int doinit; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 464 | { |
| 465 | register int i; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 466 | if (doinit || r->isinited == FALSE) |
| 467 | RTR_init(r); |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 468 | for (i = 0; i < len; i++) { |
| 469 | beg[i] = RTR_d_char(r, beg[i]); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
| 473 | |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 474 | |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 475 | /* Rotor methods */ |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 476 | static void |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 477 | rotor_dealloc(xp) |
| 478 | Rotorobj *xp; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 479 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame^] | 480 | if (xp->e_rotor) |
| 481 | PyMem_DEL(xp->e_rotor); |
| 482 | if (xp->d_rotor) |
| 483 | PyMem_DEL(xp->d_rotor); |
| 484 | if (xp->positions) |
| 485 | PyMem_DEL(xp->positions); |
| 486 | if (xp->advances) |
| 487 | PyMem_DEL(xp->advances); |
| 488 | PyObject_Del(xp); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 491 | static PyObject * |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 492 | rotorobj_encrypt(self, args) |
| 493 | Rotorobj *self; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 494 | PyObject * args; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 495 | { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 496 | char *string = NULL; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 497 | int len = 0; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 498 | PyObject *rtn = NULL; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 499 | char *tmp; |
| 500 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 501 | if (!PyArg_Parse(args, "s#", &string, &len)) |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 502 | return NULL; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 503 | if (!(tmp = PyMem_NEW(char, len+5))) { |
Guido van Rossum | 7b1e974 | 1994-08-29 10:46:42 +0000 | [diff] [blame] | 504 | PyErr_NoMemory(); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 505 | return NULL; |
| 506 | } |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 507 | memset(tmp, '\0', len+1); |
| 508 | memcpy(tmp, string, len); |
| 509 | RTR_e_region(self, (unsigned char *)tmp, len, TRUE); |
| 510 | rtn = PyString_FromStringAndSize(tmp, len); |
| 511 | PyMem_DEL(tmp); |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 512 | return(rtn); |
| 513 | } |
| 514 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 515 | static PyObject * |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 516 | rotorobj_encrypt_more(self, args) |
| 517 | Rotorobj *self; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 518 | PyObject * args; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 519 | { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 520 | char *string = NULL; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 521 | int len = 0; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 522 | PyObject *rtn = NULL; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 523 | char *tmp; |
| 524 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 525 | if (!PyArg_Parse(args, "s#", &string, &len)) |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 526 | return NULL; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 527 | if (!(tmp = PyMem_NEW(char, len+5))) { |
Guido van Rossum | 7b1e974 | 1994-08-29 10:46:42 +0000 | [diff] [blame] | 528 | PyErr_NoMemory(); |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 529 | return NULL; |
| 530 | } |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 531 | memset(tmp, '\0', len+1); |
| 532 | memcpy(tmp, string, len); |
| 533 | RTR_e_region(self, (unsigned char *)tmp, len, FALSE); |
| 534 | rtn = PyString_FromStringAndSize(tmp, len); |
| 535 | PyMem_DEL(tmp); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 536 | return(rtn); |
| 537 | } |
| 538 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 539 | static PyObject * |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 540 | rotorobj_decrypt(self, args) |
| 541 | Rotorobj *self; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 542 | PyObject * args; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 543 | { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 544 | char *string = NULL; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 545 | int len = 0; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 546 | PyObject *rtn = NULL; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 547 | char *tmp; |
| 548 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 549 | if (!PyArg_Parse(args, "s#", &string, &len)) |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 550 | return NULL; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 551 | if (!(tmp = PyMem_NEW(char, len+5))) { |
Guido van Rossum | 7b1e974 | 1994-08-29 10:46:42 +0000 | [diff] [blame] | 552 | PyErr_NoMemory(); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 553 | return NULL; |
| 554 | } |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 555 | memset(tmp, '\0', len+1); |
| 556 | memcpy(tmp, string, len); |
| 557 | RTR_d_region(self, (unsigned char *)tmp, len, TRUE); |
| 558 | rtn = PyString_FromStringAndSize(tmp, len); |
| 559 | PyMem_DEL(tmp); |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 560 | return(rtn); |
| 561 | } |
| 562 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 563 | static PyObject * |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 564 | rotorobj_decrypt_more(self, args) |
| 565 | Rotorobj *self; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 566 | PyObject * args; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 567 | { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 568 | char *string = NULL; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 569 | int len = 0; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 570 | PyObject *rtn = NULL; |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 571 | char *tmp; |
| 572 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 573 | if (!PyArg_Parse(args, "s#", &string, &len)) |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 574 | return NULL; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 575 | if (!(tmp = PyMem_NEW(char, len+5))) { |
Guido van Rossum | 7b1e974 | 1994-08-29 10:46:42 +0000 | [diff] [blame] | 576 | PyErr_NoMemory(); |
Guido van Rossum | 7b2c03f | 1992-08-02 09:00:06 +0000 | [diff] [blame] | 577 | return NULL; |
| 578 | } |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 579 | memset(tmp, '\0', len+1); |
| 580 | memcpy(tmp, string, len); |
| 581 | RTR_d_region(self, (unsigned char *)tmp, len, FALSE); |
| 582 | rtn = PyString_FromStringAndSize(tmp, len); |
| 583 | PyMem_DEL(tmp); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 584 | return(rtn); |
| 585 | } |
| 586 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 587 | static PyObject * |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 588 | rotorobj_setkey(self, args) |
| 589 | Rotorobj *self; |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 590 | PyObject * args; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 591 | { |
Barry Warsaw | 9e3fceb | 1997-01-02 20:36:36 +0000 | [diff] [blame] | 592 | char *key; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 593 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 594 | if (!PyArg_ParseTuple(args, "s:setkey", &key)) |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 595 | return NULL; |
| 596 | |
Barry Warsaw | 9e3fceb | 1997-01-02 20:36:36 +0000 | [diff] [blame] | 597 | set_key(self, key); |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 598 | Py_INCREF(Py_None); |
| 599 | return Py_None; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 602 | static struct PyMethodDef |
| 603 | rotorobj_methods[] = { |
| 604 | {"encrypt", (PyCFunction)rotorobj_encrypt}, |
| 605 | {"encryptmore", (PyCFunction)rotorobj_encrypt_more}, |
| 606 | {"decrypt", (PyCFunction)rotorobj_decrypt}, |
| 607 | {"decryptmore", (PyCFunction)rotorobj_decrypt_more}, |
| 608 | {"setkey", (PyCFunction)rotorobj_setkey, 1}, |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 609 | {NULL, NULL} /* sentinel */ |
| 610 | }; |
| 611 | |
| 612 | |
| 613 | /* Return a rotor object's named attribute. */ |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 614 | static PyObject * |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 615 | rotorobj_getattr(s, name) |
| 616 | Rotorobj *s; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 617 | char *name; |
| 618 | { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 619 | return Py_FindMethod(rotorobj_methods, (PyObject*)s, name); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 622 | |
| 623 | statichere PyTypeObject Rotor_Type = { |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 624 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 625 | 0, /*ob_size*/ |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 626 | "rotor", /*tp_name*/ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 627 | sizeof(Rotorobj), /*tp_size*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 628 | 0, /*tp_itemsize*/ |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 629 | /* methods */ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 630 | (destructor)rotor_dealloc, /*tp_dealloc*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 631 | 0, /*tp_print*/ |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 632 | (getattrfunc)rotorobj_getattr, /*tp_getattr*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 633 | 0, /*tp_setattr*/ |
| 634 | 0, /*tp_compare*/ |
| 635 | 0, /*tp_repr*/ |
Guido van Rossum | 7b1e974 | 1994-08-29 10:46:42 +0000 | [diff] [blame] | 636 | 0, /*tp_hash*/ |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 637 | }; |
| 638 | |
| 639 | |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 640 | static PyObject * |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 641 | rotor_rotor(self, args) |
Guido van Rossum | f6971e2 | 1994-08-30 12:25:20 +0000 | [diff] [blame] | 642 | PyObject * self; |
| 643 | PyObject * args; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 644 | { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 645 | Rotorobj *r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 646 | char *string; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 647 | int len; |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 648 | int num_rotors = 6; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 649 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 650 | if (!PyArg_ParseTuple(args, "s#|i:newrotor", &string, &len, &num_rotors)) |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 651 | return NULL; |
| 652 | |
| 653 | r = rotorobj_new(num_rotors, string); |
| 654 | return (PyObject *)r; |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 657 | |
Barry Warsaw | 47d3500 | 1997-01-16 16:49:44 +0000 | [diff] [blame] | 658 | |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 659 | static struct PyMethodDef |
| 660 | rotor_methods[] = { |
| 661 | {"newrotor", rotor_rotor, 1}, |
| 662 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 663 | }; |
| 664 | |
| 665 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 666 | DL_EXPORT(void) |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 667 | initrotor() |
| 668 | { |
Barry Warsaw | aeb207c | 1996-12-23 23:36:24 +0000 | [diff] [blame] | 669 | (void)Py_InitModule("rotor", rotor_methods); |
Guido van Rossum | 5009820 | 1992-07-31 15:10:13 +0000 | [diff] [blame] | 670 | } |