OLD | NEW |
(Empty) | |
| 1 # |
| 2 # pubkey.py : Internal functions for public key operations |
| 3 # |
| 4 # Part of the Python Cryptography Toolkit |
| 5 # |
| 6 # Written by Andrew Kuchling, Paul Swartz, and others |
| 7 # |
| 8 # =================================================================== |
| 9 # The contents of this file are dedicated to the public domain. To |
| 10 # the extent that dedication to the public domain is not available, |
| 11 # everyone is granted a worldwide, perpetual, royalty-free, |
| 12 # non-exclusive license to exercise all rights associated with the |
| 13 # contents of this file for any purpose whatsoever. |
| 14 # No rights are reserved. |
| 15 # |
| 16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 # SOFTWARE. |
| 24 # =================================================================== |
| 25 # |
| 26 |
| 27 __revision__ = "$Id$" |
| 28 |
| 29 import types, warnings |
| 30 from Crypto.Util.number import * |
| 31 |
| 32 # Basic public key class |
| 33 class pubkey: |
| 34 """An abstract class for a public key object. |
| 35 |
| 36 :undocumented: __getstate__, __setstate__, __eq__, __ne__, validate |
| 37 """ |
| 38 def __init__(self): |
| 39 pass |
| 40 |
| 41 def __getstate__(self): |
| 42 """To keep key objects platform-independent, the key data is |
| 43 converted to standard Python long integers before being |
| 44 written out. It will then be reconverted as necessary on |
| 45 restoration.""" |
| 46 d=self.__dict__ |
| 47 for key in self.keydata: |
| 48 if d.has_key(key): d[key]=long(d[key]) |
| 49 return d |
| 50 |
| 51 def __setstate__(self, d): |
| 52 """On unpickling a key object, the key data is converted to the big |
| 53 number representation being used, whether that is Python long |
| 54 integers, MPZ objects, or whatever.""" |
| 55 for key in self.keydata: |
| 56 if d.has_key(key): self.__dict__[key]=bignum(d[key]) |
| 57 |
| 58 def encrypt(self, plaintext, K): |
| 59 """Encrypt a piece of data. |
| 60 |
| 61 :Parameter plaintext: The piece of data to encrypt. |
| 62 :Type plaintext: byte string or long |
| 63 |
| 64 :Parameter K: A random parameter required by some algorithms |
| 65 :Type K: byte string or long |
| 66 |
| 67 :Return: A tuple with two items. Each item is of the same type as the |
| 68 plaintext (string or long). |
| 69 """ |
| 70 wasString=0 |
| 71 if isinstance(plaintext, types.StringType): |
| 72 plaintext=bytes_to_long(plaintext) ; wasString=1 |
| 73 if isinstance(K, types.StringType): |
| 74 K=bytes_to_long(K) |
| 75 ciphertext=self._encrypt(plaintext, K) |
| 76 if wasString: return tuple(map(long_to_bytes, ciphertext)) |
| 77 else: return ciphertext |
| 78 |
| 79 def decrypt(self, ciphertext): |
| 80 """Decrypt a piece of data. |
| 81 |
| 82 :Parameter ciphertext: The piece of data to decrypt. |
| 83 :Type ciphertext: byte string, long or a 2-item tuple as returned by `en
crypt` |
| 84 |
| 85 :Return: A byte string if ciphertext was a byte string or a tuple |
| 86 of byte strings. A long otherwise. |
| 87 """ |
| 88 wasString=0 |
| 89 if not isinstance(ciphertext, types.TupleType): |
| 90 ciphertext=(ciphertext,) |
| 91 if isinstance(ciphertext[0], types.StringType): |
| 92 ciphertext=tuple(map(bytes_to_long, ciphertext)) ; wasString=1 |
| 93 plaintext=self._decrypt(ciphertext) |
| 94 if wasString: return long_to_bytes(plaintext) |
| 95 else: return plaintext |
| 96 |
| 97 def sign(self, M, K): |
| 98 """Sign a piece of data. |
| 99 |
| 100 :Parameter M: The piece of data to encrypt. |
| 101 :Type M: byte string or long |
| 102 |
| 103 :Parameter K: A random parameter required by some algorithms |
| 104 :Type K: byte string or long |
| 105 |
| 106 :Return: A tuple with two items. |
| 107 """ |
| 108 if (not self.has_private()): |
| 109 raise TypeError('Private key not available in this object') |
| 110 if isinstance(M, types.StringType): M=bytes_to_long(M) |
| 111 if isinstance(K, types.StringType): K=bytes_to_long(K) |
| 112 return self._sign(M, K) |
| 113 |
| 114 def verify (self, M, signature): |
| 115 """Verify the validity of a signature. |
| 116 |
| 117 :Parameter M: The expected message. |
| 118 :Type M: byte string or long |
| 119 |
| 120 :Parameter signature: The signature to verify. |
| 121 :Type signature: tuple with two items, as return by `sign` |
| 122 |
| 123 :Return: True if the signature is correct, False otherwise. |
| 124 """ |
| 125 if isinstance(M, types.StringType): M=bytes_to_long(M) |
| 126 return self._verify(M, signature) |
| 127 |
| 128 # alias to compensate for the old validate() name |
| 129 def validate (self, M, signature): |
| 130 warnings.warn("validate() method name is obsolete; use verify()", |
| 131 DeprecationWarning) |
| 132 |
| 133 def blind(self, M, B): |
| 134 """Blind a message to prevent certain side-channel attacks. |
| 135 |
| 136 :Parameter M: The message to blind. |
| 137 :Type M: byte string or long |
| 138 |
| 139 :Parameter B: Blinding factor. |
| 140 :Type B: byte string or long |
| 141 |
| 142 :Return: A byte string if M was so. A long otherwise. |
| 143 """ |
| 144 wasString=0 |
| 145 if isinstance(M, types.StringType): |
| 146 M=bytes_to_long(M) ; wasString=1 |
| 147 if isinstance(B, types.StringType): B=bytes_to_long(B) |
| 148 blindedmessage=self._blind(M, B) |
| 149 if wasString: return long_to_bytes(blindedmessage) |
| 150 else: return blindedmessage |
| 151 |
| 152 def unblind(self, M, B): |
| 153 """Unblind a message after cryptographic processing. |
| 154 |
| 155 :Parameter M: The encoded message to unblind. |
| 156 :Type M: byte string or long |
| 157 |
| 158 :Parameter B: Blinding factor. |
| 159 :Type B: byte string or long |
| 160 """ |
| 161 wasString=0 |
| 162 if isinstance(M, types.StringType): |
| 163 M=bytes_to_long(M) ; wasString=1 |
| 164 if isinstance(B, types.StringType): B=bytes_to_long(B) |
| 165 unblindedmessage=self._unblind(M, B) |
| 166 if wasString: return long_to_bytes(unblindedmessage) |
| 167 else: return unblindedmessage |
| 168 |
| 169 |
| 170 # The following methods will usually be left alone, except for |
| 171 # signature-only algorithms. They both return Boolean values |
| 172 # recording whether this key's algorithm can sign and encrypt. |
| 173 def can_sign (self): |
| 174 """Tell if the algorithm can deal with cryptographic signatures. |
| 175 |
| 176 This property concerns the *algorithm*, not the key itself. |
| 177 It may happen that this particular key object hasn't got |
| 178 the private information required to generate a signature. |
| 179 |
| 180 :Return: boolean |
| 181 """ |
| 182 return 1 |
| 183 |
| 184 def can_encrypt (self): |
| 185 """Tell if the algorithm can deal with data encryption. |
| 186 |
| 187 This property concerns the *algorithm*, not the key itself. |
| 188 It may happen that this particular key object hasn't got |
| 189 the private information required to decrypt data. |
| 190 |
| 191 :Return: boolean |
| 192 """ |
| 193 return 1 |
| 194 |
| 195 def can_blind (self): |
| 196 """Tell if the algorithm can deal with data blinding. |
| 197 |
| 198 This property concerns the *algorithm*, not the key itself. |
| 199 It may happen that this particular key object hasn't got |
| 200 the private information required carry out blinding. |
| 201 |
| 202 :Return: boolean |
| 203 """ |
| 204 return 0 |
| 205 |
| 206 # The following methods will certainly be overridden by |
| 207 # subclasses. |
| 208 |
| 209 def size (self): |
| 210 """Tell the maximum number of bits that can be handled by this key. |
| 211 |
| 212 :Return: int |
| 213 """ |
| 214 return 0 |
| 215 |
| 216 def has_private (self): |
| 217 """Tell if the key object contains private components. |
| 218 |
| 219 :Return: bool |
| 220 """ |
| 221 return 0 |
| 222 |
| 223 def publickey (self): |
| 224 """Construct a new key carrying only the public information. |
| 225 |
| 226 :Return: A new `pubkey` object. |
| 227 """ |
| 228 return self |
| 229 |
| 230 def __eq__ (self, other): |
| 231 """__eq__(other): 0, 1 |
| 232 Compare us to other for equality. |
| 233 """ |
| 234 return self.__getstate__() == other.__getstate__() |
| 235 |
| 236 def __ne__ (self, other): |
| 237 """__ne__(other): 0, 1 |
| 238 Compare us to other for inequality. |
| 239 """ |
| 240 return not self.__eq__(other) |
OLD | NEW |