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