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