3 ### Setup for Catacomb/Python bindings
5 ### (c) 2004 Straylight/Edgeware
8 ###----- Licensing notice ---------------------------------------------------
10 ### This file is part of the Python interface to Catacomb.
12 ### Catacomb/Python is free software; you can redistribute it and/or modify
13 ### it under the terms of the GNU General Public License as published by
14 ### the Free Software Foundation; either version 2 of the License, or
15 ### (at your option) any later version.
17 ### Catacomb/Python is distributed in the hope that it will be useful,
18 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ### GNU General Public License for more details.
22 ### You should have received a copy of the GNU General Public License
23 ### along with Catacomb/Python; if not, write to the Free Software Foundation,
24 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 from __future__
import with_statement
28 from binascii
import hexlify
as _hexify
, unhexlify
as _unhexify
29 from contextlib
import contextmanager
as _ctxmgr
30 try: import DLFCN
as _dlfcn
31 except ImportError: _dlfcn
= None
33 from struct
import pack
as _pack
35 import types
as _types
37 ###--------------------------------------------------------------------------
38 ### Import the main C extension module.
41 _dlflags
= _odlflags
= _sys
.getdlopenflags()
42 except AttributeError:
43 _dlflags
= _odlflags
= -1
45 ## Set the `deep binding' flag. Python has its own different MD5
46 ## implementation, and some distributions export `md5_init' and friends so
47 ## they override our versions, which doesn't end well. Figure out how to
48 ## turn this flag on so we don't have the problem.
50 try: _dlflags |
= _dlfcn
.RTLD_DEEPBIND
51 except AttributeError:
52 try: _dlflags |
= _os
.RTLD_DEEPBIND
53 except AttributeError:
54 if _os
.uname()[0] == 'Linux': _dlflags |
= 8 # magic knowledge
55 else: pass # can't do this.
56 _sys
.setdlopenflags(_dlflags
)
61 _sys
.setdlopenflags(_odlflags
)
63 del _dlflags
, _odlflags
65 ###--------------------------------------------------------------------------
68 ## For the benefit of the default keyreporter, we need the program name.
69 _base
._ego(_sys
.argv
[0])
71 ## Register our module.
72 _base
._set_home_module(_sys
.modules
[__name__
])
73 def default_lostexchook(why
, ty
, val
, tb
):
74 """`catacomb.lostexchook(WHY, TY, VAL, TB)' reports lost exceptions."""
75 _sys
.stderr
.write("\n\n!!! LOST EXCEPTION: %s\n" % why
)
76 _sys
.excepthook(ty
, val
, tb
)
77 _sys
.stderr
.write("\n")
78 lostexchook
= default_lostexchook
80 ## How to fix a name back into the right identifier. Alas, the rules are not
84 ## Hyphens consistently become underscores.
85 name
= name
.replace('-', '_')
87 ## But slashes might become underscores or just vanish.
88 if name
.startswith('salsa20'): name
= name
.replace('/', '')
89 else: name
= name
.replace('/', '_')
94 ## Initialize the module. Drag in the static methods of the various
95 ## classes; create names for the various known crypto algorithms.
102 for i
in ['ByteString',
104 'ECPt', 'ECPtCurve', 'ECCurve', 'ECInfo',
105 'DHInfo', 'BinDHInfo', 'RSAPriv', 'BBSPriv',
106 'PrimeFilter', 'RabinMiller',
114 setattr(c
, j
[plen
:], classmethod(b
[j
]))
115 for i
in [gcciphers
, gchashes
, gcmacs
, gcprps
]:
116 for c
in i
.itervalues():
117 d
[_fixname(c
.name
)] = c
118 for c
in gccrands
.itervalues():
119 d
[_fixname(c
.name
+ 'rand')] = c
122 ## A handy function for our work: add the methods of a named class to an
123 ## existing class. This is how we write the Python-implemented parts of our
126 for i
in cc
.__dict__
:
128 if type(a
) is _types
.MethodType
:
130 elif type(a
) not in (_types
.FunctionType
, staticmethod, classmethod):
134 ## Parsing functions tend to return the object parsed and the remainder of
135 ## the input. This checks that the remainder is input and, if so, returns
140 raise SyntaxError, 'junk at end of string'
143 ## Some pretty-printing utilities.
144 PRINT_SECRETS
= False
145 def _clsname(me
): return type(me
).__name__
146 def _repr_secret(thing
, secretp
= True):
147 if not secretp
or PRINT_SECRETS
: return repr(thing
)
148 else: return '#<SECRET>'
149 def _pp_str(me
, pp
, cyclep
): pp
.text(cyclep
and '...' or str(me
))
150 def _pp_secret(pp
, thing
, secretp
= True):
151 if not secretp
or PRINT_SECRETS
: pp
.pretty(thing
)
152 else: pp
.text('#<SECRET>')
153 def _pp_bgroup(pp
, text
):
155 pp
.begin_group(ind
, text
)
157 def _pp_bgroup_tyname(pp
, obj
, open = '('):
158 return _pp_bgroup(pp
, _clsname(obj
) + open)
159 def _pp_kv(pp
, k
, v
, secretp
= False):
160 ind
= _pp_bgroup(pp
, k
+ ' = ')
161 _pp_secret(pp
, v
, secretp
)
162 pp
.end_group(ind
, '')
163 def _pp_commas(pp
, printfn
, items
):
166 if firstp
: firstp
= False
167 else: pp
.text(','); pp
.breakable()
169 def _pp_dict(pp
, items
):
179 _pp_commas(pp
, p
, items
)
181 ###--------------------------------------------------------------------------
186 return ByteString(_unhexify(x
))
187 fromhex
= staticmethod(fromhex
)
191 return 'bytes(%r)' %
hex(me
)
192 _augment(ByteString
, _tmp
)
193 ByteString
.__hash__
= str.__hash__
194 bytes
= ByteString
.fromhex
196 ###--------------------------------------------------------------------------
202 return ctstreq(h
, hh
)
203 _augment(GHash
, _tmp
)
204 _augment(Poly1305Hash
, _tmp
)
206 class _HashBase (object):
207 ## The standard hash methods. Assume that `hash' is defined and returns
209 def hashu8(me
, n
): return me
.hash(_pack('B', n
))
210 def hashu16l(me
, n
): return me
.hash(_pack('<H', n
))
211 def hashu16b(me
, n
): return me
.hash(_pack('>H', n
))
213 def hashu32l(me
, n
): return me
.hash(_pack('<L', n
))
214 def hashu32b(me
, n
): return me
.hash(_pack('>L', n
))
216 def hashu64l(me
, n
): return me
.hash(_pack('<Q', n
))
217 def hashu64b(me
, n
): return me
.hash(_pack('>Q', n
))
219 def hashbuf8(me
, s
): return me
.hashu8(len(s
)).hash(s
)
220 def hashbuf16l(me
, s
): return me
.hashu16l(len(s
)).hash(s
)
221 def hashbuf16b(me
, s
): return me
.hashu16b(len(s
)).hash(s
)
222 hashbuf16
= hashbuf16b
223 def hashbuf32l(me
, s
): return me
.hashu32l(len(s
)).hash(s
)
224 def hashbuf32b(me
, s
): return me
.hashu32b(len(s
)).hash(s
)
225 hashbuf32
= hashbuf32b
226 def hashbuf64l(me
, s
): return me
.hashu64l(len(s
)).hash(s
)
227 def hashbuf64b(me
, s
): return me
.hashu64b(len(s
)).hash(s
)
228 hashbuf64
= hashbuf64b
229 def hashstrz(me
, s
): return me
.hash(s
).hashu8(0)
231 class _ShakeBase (_HashBase
):
233 ## Python gets really confused if I try to augment `__new__' on native
234 ## classes, so wrap and delegate. Sorry.
235 def __init__(me
, perso
= '', *args
, **kw
):
236 super(_ShakeBase
, me
).__init__(*args
, **kw
)
237 me
._h
= me
._SHAKE(perso
= perso
, func
= me
._FUNC
)
239 ## Delegate methods...
240 def copy(me
): new
= me
.__class__(); new
._copy(me
)
241 def _copy(me
, other
): me
._h
= other
._h
.copy()
242 def hash(me
, m
): me
._h
.hash(m
); return me
243 def xof(me
): me
._h
.xof(); return me
244 def get(me
, n
): return me
._h
.get(n
)
245 def mask(me
, m
): return me
._h
.mask(m
)
246 def done(me
, n
): return me
._h
.done(n
)
247 def check(me
, h
): return ctstreq(h
, me
.done(len(h
)))
249 def state(me
): return me
._h
.state
251 def buffered(me
): return me
._h
.buffered
253 def rate(me
): return me
._h
.rate
257 return ctstreq(h
, me
.done(len(h
)))
260 return me
.hashu8(len(nn
)).hash(nn
)
263 return me
.hash(nn
).hashu8(len(nn
))
264 def stringenc(me
, str):
265 return me
.leftenc(8*len(str)).hash(str)
266 def bytepad_before(me
):
267 return me
.leftenc(me
.rate
)
268 def bytepad_after(me
):
269 if me
.buffered
: me
.hash(me
._Z
[:me
.rate
- me
.buffered
])
276 _augment(Shake
, _tmp
)
277 _augment(_ShakeBase
, _tmp
)
278 Shake
._Z
= _ShakeBase
._Z
= ByteString(200*'\0')
280 class KMAC (_ShakeBase
):
282 def __init__(me
, k
, *arg
, **kw
):
283 super(KMAC
, me
).__init__(*arg
, **kw
)
284 with me
.bytepad(): me
.stringenc(k
)
285 def done(me
, n
= -1):
286 if n
< 0: n
= me
._TAGSZ
288 return super(KMAC
, me
).done(n
)
291 return super(KMAC
, me
).xof()
293 class KMAC128 (KMAC
): _SHAKE
= Shake128
; _TAGSZ
= 16
294 class KMAC256 (KMAC
): _SHAKE
= Shake256
; _TAGSZ
= 32
296 ###--------------------------------------------------------------------------
297 ### NaCl `secretbox'.
299 def secret_box(k
, n
, m
):
300 E
= xsalsa20(k
).setiv(n
)
301 r
= E
.enczero(poly1305
.keysz
.default
)
302 s
= E
.enczero(poly1305
.masksz
)
304 t
= poly1305(r
)(s
).hash(y
).done()
305 return ByteString(t
+ y
)
307 def secret_unbox(k
, n
, c
):
308 E
= xsalsa20(k
).setiv(n
)
309 r
= E
.enczero(poly1305
.keysz
.default
)
310 s
= E
.enczero(poly1305
.masksz
)
311 y
= c
[poly1305
.tagsz
:]
312 if not poly1305(r
)(s
).hash(y
).check(c
[0:poly1305
.tagsz
]):
313 raise ValueError, 'decryption failed'
314 return E
.decrypt(c
[poly1305
.tagsz
:])
316 ###--------------------------------------------------------------------------
317 ### Multiprecision integers and binary polynomials.
320 if isinstance(x
, BaseRat
): return x
._n
, x
._d
322 class BaseRat (object):
323 """Base class implementing fields of fractions over Euclidean domains."""
324 def __new__(cls
, a
, b
):
325 a
, b
= cls
.RING(a
), cls
.RING(b
)
329 me
= super(BaseRat
, cls
).__new__(cls
)
334 def numer(me
): return me
._n
336 def denom(me
): return me
._d
337 def __str__(me
): return '%s/%s' %
(me
._n
, me
._d
)
338 def __repr__(me
): return '%s(%s, %s)' %
(_clsname(me
), me
._n
, me
._d
)
339 _repr_pretty_
= _pp_str
341 def __add__(me
, you
):
342 n
, d
= _split_rat(you
)
343 return type(me
)(me
._n
*d
+ n
*me
._d
, d
*me
._d
)
345 def __sub__(me
, you
):
346 n
, d
= _split_rat(you
)
347 return type(me
)(me
._n
*d
- n
*me
._d
, d
*me
._d
)
348 def __rsub__(me
, you
):
349 n
, d
= _split_rat(you
)
350 return type(me
)(n
*me
._d
- me
._n
*d
, d
*me
._d
)
351 def __mul__(me
, you
):
352 n
, d
= _split_rat(you
)
353 return type(me
)(me
._n
*n
, me
._d
*d
)
355 def __truediv__(me
, you
):
356 n
, d
= _split_rat(you
)
357 return type(me
)(me
._n
*d
, me
._d
*n
)
358 def __rtruediv__(me
, you
):
359 n
, d
= _split_rat(you
)
360 return type(me
)(me
._d
*n
, me
._n
*d
)
361 __div__
= __truediv__
362 __rdiv__
= __rtruediv__
363 def __cmp__(me
, you
):
364 n
, d
= _split_rat(you
)
365 return cmp(me
._n
*d
, n
*me
._d
)
366 def __rcmp__(me
, you
):
367 n
, d
= _split_rat(you
)
368 return cmp(n
*me
._d
, me
._n
*d
)
370 class IntRat (BaseRat
):
373 class GFRat (BaseRat
):
377 def negp(x
): return x
< 0
378 def posp(x
): return x
> 0
379 def zerop(x
): return x
== 0
380 def oddp(x
): return x
.testbit(0)
381 def evenp(x
): return not x
.testbit(0)
382 def mont(x
): return MPMont(x
)
383 def barrett(x
): return MPBarrett(x
)
384 def reduce(x
): return MPReduce(x
)
385 def __truediv__(me
, you
): return IntRat(me
, you
)
386 def __rtruediv__(me
, you
): return IntRat(you
, me
)
387 __div__
= __truediv__
388 __rdiv__
= __rtruediv__
389 _repr_pretty_
= _pp_str
393 def zerop(x
): return x
== 0
394 def reduce(x
): return GFReduce(x
)
395 def trace(x
, y
): return x
.reduce().trace(y
)
396 def halftrace(x
, y
): return x
.reduce().halftrace(y
)
397 def modsqrt(x
, y
): return x
.reduce().sqrt(y
)
398 def quadsolve(x
, y
): return x
.reduce().quadsolve(y
)
399 def __truediv__(me
, you
): return GFRat(me
, you
)
400 def __rtruediv__(me
, you
): return GFRat(you
, me
)
401 __div__
= __truediv__
402 __rdiv__
= __rtruediv__
403 _repr_pretty_
= _pp_str
408 'product(ITERABLE) or product(I, ...) -> PRODUCT'
409 return MPMul(*arg
).done()
410 product
= staticmethod(product
)
411 _augment(MPMul
, _tmp
)
413 ###--------------------------------------------------------------------------
417 def fromstring(str): return _checkend(Field
.parse(str))
418 fromstring
= staticmethod(fromstring
)
419 _augment(Field
, _tmp
)
422 def __repr__(me
): return '%s(%sL)' %
(_clsname(me
), me
.p
)
423 def __hash__(me
): return 0x114401de ^
hash(me
.p
)
424 def _repr_pretty_(me
, pp
, cyclep
):
425 ind
= _pp_bgroup_tyname(pp
, me
)
426 if cyclep
: pp
.text('...')
427 else: pp
.pretty(me
.p
)
428 pp
.end_group(ind
, ')')
429 def ec(me
, a
, b
): return ECPrimeProjCurve(me
, a
, b
)
430 _augment(PrimeField
, _tmp
)
433 def __repr__(me
): return '%s(%#xL)' %
(_clsname(me
), me
.p
)
434 def ec(me
, a
, b
): return ECBinProjCurve(me
, a
, b
)
435 def _repr_pretty_(me
, pp
, cyclep
):
436 ind
= _pp_bgroup_tyname(pp
, me
)
437 if cyclep
: pp
.text('...')
438 else: pp
.text('%#x' % me
.p
)
439 pp
.end_group(ind
, ')')
440 _augment(BinField
, _tmp
)
443 def __hash__(me
): return 0x23e4701c ^
hash(me
.p
)
444 _augment(BinPolyField
, _tmp
)
450 h ^
= 2*hash(me
.beta
) & 0xffffffff
452 _augment(BinNormField
, _tmp
)
455 def __str__(me
): return str(me
.value
)
456 def __repr__(me
): return '%s(%s)' %
(repr(me
.field
), repr(me
.value
))
457 _repr_pretty_
= _pp_str
460 ###--------------------------------------------------------------------------
465 return '%s(%r, %s, %s)' %
(_clsname(me
), me
.field
, me
.a
, me
.b
)
466 def _repr_pretty_(me
, pp
, cyclep
):
467 ind
= _pp_bgroup_tyname(pp
, me
)
471 pp
.pretty(me
.field
); pp
.text(','); pp
.breakable()
472 pp
.pretty(me
.a
); pp
.text(','); pp
.breakable()
474 pp
.end_group(ind
, ')')
476 return ecpt
.frombuf(me
, s
)
478 return ecpt
.fromraw(me
, s
)
481 _augment(ECCurve
, _tmp
)
487 h ^
= 2*hash(me
.a
) ^
0xffffffff
488 h ^
= 5*hash(me
.b
) ^
0xffffffff
490 _augment(ECPrimeCurve
, _tmp
)
496 h ^
= 2*hash(me
.a
) ^
0xffffffff
497 h ^
= 5*hash(me
.b
) ^
0xffffffff
499 _augment(ECBinCurve
, _tmp
)
503 if not me
: return '%s()' %
_clsname(me
)
504 return '%s(%s, %s)' %
(_clsname(me
), me
.ix
, me
.iy
)
506 if not me
: return 'inf'
507 return '(%s, %s)' %
(me
.ix
, me
.iy
)
508 def _repr_pretty_(me
, pp
, cyclep
):
514 ind
= _pp_bgroup(pp
, '(')
515 pp
.pretty(me
.ix
); pp
.text(','); pp
.breakable()
517 pp
.end_group(ind
, ')')
522 return '%s(curve = %r, G = %r, r = %s, h = %s)' % \
523 (_clsname(me
), me
.curve
, me
.G
, me
.r
, me
.h
)
524 def _repr_pretty_(me
, pp
, cyclep
):
525 ind
= _pp_bgroup_tyname(pp
, me
)
529 _pp_kv(pp
, 'curve', me
.curve
); pp
.text(','); pp
.breakable()
530 _pp_kv(pp
, 'G', me
.G
); pp
.text(','); pp
.breakable()
531 _pp_kv(pp
, 'r', me
.r
); pp
.text(','); pp
.breakable()
532 _pp_kv(pp
, 'h', me
.h
)
533 pp
.end_group(ind
, ')')
537 h ^
= 2*hash(me
.G
) & 0xffffffff
541 _augment(ECInfo
, _tmp
)
545 if not me
: return '%r()' %
(me
.curve
)
546 return '%r(%s, %s)' %
(me
.curve
, me
.x
, me
.y
)
548 if not me
: return 'inf'
549 return '(%s, %s)' %
(me
.x
, me
.y
)
550 def _repr_pretty_(me
, pp
, cyclep
):
556 ind
= _pp_bgroup(pp
, '(')
557 pp
.pretty(me
.x
); pp
.text(','); pp
.breakable()
559 pp
.end_group(ind
, ')')
560 _augment(ECPtCurve
, _tmp
)
562 ###--------------------------------------------------------------------------
566 def __repr__(me
): return '%s(%d)' %
(_clsname(me
), me
.default
)
567 def check(me
, sz
): return True
568 def best(me
, sz
): return sz
569 _augment(KeySZAny
, _tmp
)
573 return '%s(%d, %d, %d, %d)' % \
574 (_clsname(me
), me
.default
, me
.min, me
.max, me
.mod
)
575 def _repr_pretty_(me
, pp
, cyclep
):
576 ind
= _pp_bgroup_tyname(pp
, me
)
580 pp
.pretty(me
.default
); pp
.text(','); pp
.breakable()
581 pp
.pretty(me
.min); pp
.text(','); pp
.breakable()
582 pp
.pretty(me
.max); pp
.text(','); pp
.breakable()
584 pp
.end_group(ind
, ')')
585 def check(me
, sz
): return me
.min <= sz
<= me
.max and sz % me
.mod
== 0
587 if sz
< me
.min: raise ValueError, 'key too small'
588 elif sz
> me
.max: return me
.max
589 else: return sz
- (sz % me
.mod
)
590 _augment(KeySZRange
, _tmp
)
593 def __repr__(me
): return '%s(%d, %s)' %
(_clsname(me
), me
.default
, me
.set)
594 def _repr_pretty_(me
, pp
, cyclep
):
595 ind
= _pp_bgroup_tyname(pp
, me
)
599 pp
.pretty(me
.default
); pp
.text(','); pp
.breakable()
600 ind1
= _pp_bgroup(pp
, '{')
601 _pp_commas(pp
, pp
.pretty
, me
.set)
602 pp
.end_group(ind1
, '}')
603 pp
.end_group(ind
, ')')
604 def check(me
, sz
): return sz
in me
.set
608 if found
< i
<= sz
: found
= i
609 if found
< 0: raise ValueError, 'key too small'
611 _augment(KeySZSet
, _tmp
)
613 ###--------------------------------------------------------------------------
614 ### Key data objects.
617 def __repr__(me
): return '%s(%r)' %
(_clsname(me
), me
.name
)
618 _augment(KeyFile
, _tmp
)
621 def __repr__(me
): return '%s(%r)' %
(_clsname(me
), me
.fulltag
)
626 return '%s({%s})' %
(_clsname(me
),
627 ', '.join(['%r: %r' % kv
for kv
in me
.iteritems()]))
628 def _repr_pretty_(me
, pp
, cyclep
):
629 ind
= _pp_bgroup_tyname(pp
, me
)
630 if cyclep
: pp
.text('...')
631 else: _pp_dict(pp
, me
.iteritems())
632 pp
.end_group(ind
, ')')
633 _augment(KeyAttributes
, _tmp
)
637 return '%s(%s, %r)' %
(_clsname(me
),
638 _repr_secret(me
._guts(),
639 not (me
.flags
& KF_NONSECRET
)),
640 me
.writeflags(me
.flags
))
641 def _repr_pretty_(me
, pp
, cyclep
):
642 ind
= _pp_bgroup_tyname(pp
, me
)
646 _pp_secret(pp
, me
._guts(), not (me
.flags
& KF_NONSECRET
))
647 pp
.text(','); pp
.breakable()
648 pp
.pretty(me
.writeflags(me
.flags
))
649 pp
.end_group(ind
, ')')
650 _augment(KeyData
, _tmp
)
653 def _guts(me
): return me
.bin
654 _augment(KeyDataBinary
, _tmp
)
657 def _guts(me
): return me
.ct
658 _augment(KeyDataEncrypted
, _tmp
)
661 def _guts(me
): return me
.mp
662 _augment(KeyDataMP
, _tmp
)
665 def _guts(me
): return me
.str
666 _augment(KeyDataString
, _tmp
)
669 def _guts(me
): return me
.ecpt
670 _augment(KeyDataECPt
, _tmp
)
674 return '%s({%s})' %
(_clsname(me
),
675 ', '.join(['%r: %r' % kv
for kv
in me
.iteritems()]))
676 def _repr_pretty_(me
, pp
, cyclep
):
677 ind
= _pp_bgroup_tyname(pp
, me
, '({ ')
678 if cyclep
: pp
.text('...')
679 else: _pp_dict(pp
, me
.iteritems())
680 pp
.end_group(ind
, ' })')
681 _augment(KeyDataStructured
, _tmp
)
683 ###--------------------------------------------------------------------------
688 return '%s(p = %s, r = %s, g = %s)' %
(_clsname(me
), me
.p
, me
.r
, me
.g
)
689 def _repr_pretty_(me
, pp
, cyclep
):
690 ind
= _pp_bgroup_tyname(pp
, me
)
694 _pp_kv(pp
, 'p', me
.p
); pp
.text(','); pp
.breakable()
695 _pp_kv(pp
, 'r', me
.r
); pp
.text(','); pp
.breakable()
696 _pp_kv(pp
, 'g', me
.g
)
697 pp
.end_group(ind
, ')')
698 _augment(FGInfo
, _tmp
)
701 def group(me
): return PrimeGroup(me
)
702 _augment(DHInfo
, _tmp
)
705 def group(me
): return BinGroup(me
)
706 _augment(BinDHInfo
, _tmp
)
710 return '%s(%r)' %
(_clsname(me
), me
.info
)
711 def _repr_pretty_(me
, pp
, cyclep
):
712 ind
= _pp_bgroup_tyname(pp
, me
)
713 if cyclep
: pp
.text('...')
714 else: pp
.pretty(me
.info
)
715 pp
.end_group(ind
, ')')
716 _augment(Group
, _tmp
)
723 h ^
= 2*hash(info
.r
) & 0xffffffff
724 h ^
= 5*hash(info
.g
) & 0xffffffff
726 def _get_geval(me
, x
): return MP(x
)
727 _augment(PrimeGroup
, _tmp
)
734 h ^
= 2*hash(info
.r
) & 0xffffffff
735 h ^
= 5*hash(info
.g
) & 0xffffffff
737 def _get_geval(me
, x
): return GF(x
)
738 _augment(BinGroup
, _tmp
)
741 def __hash__(me
): return 0x0ec23dab ^
hash(me
.info
)
742 def _get_geval(me
, x
): return x
.toec()
743 _augment(ECGroup
, _tmp
)
747 return '%r(%r)' %
(me
.group
, str(me
))
748 def _repr_pretty_(me
, pp
, cyclep
):
749 pp
.pretty(type(me
)._get_geval(me
))
752 ###--------------------------------------------------------------------------
753 ### RSA encoding techniques.
755 class PKCS1Crypt (object):
756 def __init__(me
, ep
= '', rng
= rand
):
759 def encode(me
, msg
, nbits
):
760 return _base
._p1crypt_encode(msg
, nbits
, me
.ep
, me
.rng
)
761 def decode(me
, ct
, nbits
):
762 return _base
._p1crypt_decode(ct
, nbits
, me
.ep
, me
.rng
)
764 class PKCS1Sig (object):
765 def __init__(me
, ep
= '', rng
= rand
):
768 def encode(me
, msg
, nbits
):
769 return _base
._p1sig_encode(msg
, nbits
, me
.ep
, me
.rng
)
770 def decode(me
, msg
, sig
, nbits
):
771 return _base
._p1sig_decode(msg
, sig
, nbits
, me
.ep
, me
.rng
)
774 def __init__(me
, mgf
= sha_mgf
, hash = sha
, ep
= '', rng
= rand
):
779 def encode(me
, msg
, nbits
):
780 return _base
._oaep_encode(msg
, nbits
, me
.mgf
, me
.hash, me
.ep
, me
.rng
)
781 def decode(me
, ct
, nbits
):
782 return _base
._oaep_decode(ct
, nbits
, me
.mgf
, me
.hash, me
.ep
, me
.rng
)
785 def __init__(me
, mgf
= sha_mgf
, hash = sha
, saltsz
= None, rng
= rand
):
792 def encode(me
, msg
, nbits
):
793 return _base
._pss_encode(msg
, nbits
, me
.mgf
, me
.hash, me
.saltsz
, me
.rng
)
794 def decode(me
, msg
, sig
, nbits
):
795 return _base
._pss_decode(msg
, sig
, nbits
,
796 me
.mgf
, me
.hash, me
.saltsz
, me
.rng
)
799 def encrypt(me
, msg
, enc
):
800 return me
.pubop(enc
.encode(msg
, me
.n
.nbits
))
801 def verify(me
, msg
, sig
, enc
):
802 if msg
is None: return enc
.decode(msg
, me
.pubop(sig
), me
.n
.nbits
)
804 x
= enc
.decode(msg
, me
.pubop(sig
), me
.n
.nbits
)
805 return x
is None or x
== msg
809 return '%s(n = %r, e = %r)' %
(_clsname(me
), me
.n
, me
.e
)
810 def _repr_pretty_(me
, pp
, cyclep
):
811 ind
= _pp_bgroup_tyname(pp
, me
)
815 _pp_kv(pp
, 'n', me
.n
); pp
.text(','); pp
.breakable()
816 _pp_kv(pp
, 'e', me
.e
)
817 pp
.end_group(ind
, ')')
818 _augment(RSAPub
, _tmp
)
821 def decrypt(me
, ct
, enc
): return enc
.decode(me
.privop(ct
), me
.n
.nbits
)
822 def sign(me
, msg
, enc
): return me
.privop(enc
.encode(msg
, me
.n
.nbits
))
824 return '%s(n = %r, e = %r, d = %s, ' \
825 'p = %s, q = %s, dp = %s, dq = %s, q_inv = %s)' % \
826 (_clsname(me
), me
.n
, me
.e
,
827 _repr_secret(me
.d
), _repr_secret(me
.p
), _repr_secret(me
.q
),
828 _repr_secret(me
.dp
), _repr_secret(me
.dq
), _repr_secret(me
.q_inv
))
829 def _repr_pretty_(me
, pp
, cyclep
):
830 ind
= _pp_bgroup_tyname(pp
, me
)
834 _pp_kv(pp
, 'n', me
.n
); pp
.text(','); pp
.breakable()
835 _pp_kv(pp
, 'e', me
.e
); pp
.text(','); pp
.breakable()
836 _pp_kv(pp
, 'd', me
.d
, secretp
= True); pp
.text(','); pp
.breakable()
837 _pp_kv(pp
, 'p', me
.p
, secretp
= True); pp
.text(','); pp
.breakable()
838 _pp_kv(pp
, 'q', me
.q
, secretp
= True); pp
.text(','); pp
.breakable()
839 _pp_kv(pp
, 'dp', me
.dp
, secretp
= True); pp
.text(','); pp
.breakable()
840 _pp_kv(pp
, 'dq', me
.dq
, secretp
= True); pp
.text(','); pp
.breakable()
841 _pp_kv(pp
, 'q_inv', me
.q_inv
, secretp
= True)
842 pp
.end_group(ind
, ')')
843 _augment(RSAPriv
, _tmp
)
845 ###--------------------------------------------------------------------------
846 ### DSA and related schemes.
849 def __repr__(me
): return '%s(G = %r, p = %r)' %
(_clsname(me
), me
.G
, me
.p
)
850 def _repr_pretty_(me
, pp
, cyclep
):
851 ind
= _pp_bgroup_tyname(pp
, me
)
855 _pp_kv(pp
, 'G', me
.G
); pp
.text(','); pp
.breakable()
856 _pp_kv(pp
, 'p', me
.p
)
857 pp
.end_group(ind
, ')')
858 _augment(DSAPub
, _tmp
)
859 _augment(KCDSAPub
, _tmp
)
862 def __repr__(me
): return '%s(G = %r, u = %s, p = %r)' % \
863 (_clsname(me
), me
.G
, _repr_secret(me
.u
), me
.p
)
864 def _repr_pretty_(me
, pp
, cyclep
):
865 ind
= _pp_bgroup_tyname(pp
, me
)
869 _pp_kv(pp
, 'G', me
.G
); pp
.text(','); pp
.breakable()
870 _pp_kv(pp
, 'u', me
.u
, True); pp
.text(','); pp
.breakable()
871 _pp_kv(pp
, 'p', me
.p
)
872 pp
.end_group(ind
, ')')
873 _augment(DSAPriv
, _tmp
)
874 _augment(KCDSAPriv
, _tmp
)
876 ###--------------------------------------------------------------------------
877 ### Bernstein's elliptic curve crypto and related schemes.
879 X25519_BASE
= MP(9).storel(32)
880 X448_BASE
= MP(5).storel(56)
882 Z128
= ByteString
.zero(16)
884 class _BasePub (object):
885 def __init__(me
, pub
, *args
, **kw
):
886 if not me
._PUBSZ
.check(len(pub
)): raise ValueError, 'bad public key'
887 super(_BasePub
, me
).__init__(*args
, **kw
)
889 def __repr__(me
): return '%s(pub = %r)' %
(_clsname(me
), me
.pub
)
890 def _pp(me
, pp
): _pp_kv(pp
, 'pub', me
.pub
)
891 def _repr_pretty_(me
, pp
, cyclep
):
892 ind
= _pp_bgroup_tyname(pp
, me
)
893 if cyclep
: pp
.text('...')
895 pp
.end_group(ind
, ')')
897 class _BasePriv (object):
898 def __init__(me
, priv
, pub
= None, *args
, **kw
):
899 if not me
._KEYSZ
.check(len(priv
)): raise ValueError, 'bad private key'
900 if pub
is None: pub
= me
._pubkey(priv
)
901 super(_BasePriv
, me
).__init__(pub
= pub
, *args
, **kw
)
904 def generate(cls
, rng
= rand
):
905 return cls(rng
.block(cls
._KEYSZ
.default
))
907 return '%s(priv = %d, pub = %r)' % \
908 (_clsname(me
), _repr_secret(me
.priv
), me
.pub
)
910 _pp_kv(pp
, 'priv', me
.priv
, secretp
= True); pp
.text(','); pp
.breakable()
911 super(_BasePriv
, me
)._pp(pp
)
913 class _XDHPub (_BasePub
): pass
915 class _XDHPriv (_BasePriv
):
916 def _pubkey(me
, priv
): return me
._op(priv
, me
._BASE
)
917 def agree(me
, you
): return me
._op(me
.priv
, you
.pub
)
918 def boxkey(me
, recip
): return me
._hashkey(me
.agree(recip
))
919 def box(me
, recip
, n
, m
): return secret_box(me
.boxkey(recip
), n
, m
)
920 def unbox(me
, recip
, n
, c
): return secret_unbox(me
.boxkey(recip
), n
, c
)
922 class X25519Pub (_XDHPub
):
923 _PUBSZ
= KeySZSet(X25519_PUBSZ
)
926 class X25519Priv (_XDHPriv
, X25519Pub
):
927 _KEYSZ
= KeySZSet(X25519_KEYSZ
)
928 def _op(me
, k
, X
): return x25519(k
, X
)
929 def _hashkey(me
, z
): return hsalsa20_prf(z
, Z128
)
931 class X448Pub (_XDHPub
):
932 _PUBSZ
= KeySZSet(X448_PUBSZ
)
935 class X448Priv (_XDHPriv
, X448Pub
):
936 _KEYSZ
= KeySZSet(X448_KEYSZ
)
937 def _op(me
, k
, X
): return x448(k
, X
)
938 def _hashkey(me
, z
): return Shake256().hash(z
).done(salsa20
.keysz
.default
)
940 class _EdDSAPub (_BasePub
):
941 def beginhash(me
): return me
._HASH()
942 def endhash(me
, h
): return h
.done()
944 class _EdDSAPriv (_BasePriv
, _EdDSAPub
):
947 class Ed25519Pub (_EdDSAPub
):
948 _PUBSZ
= KeySZSet(ED25519_PUBSZ
)
950 def verify(me
, msg
, sig
, **kw
):
951 return ed25519_verify(me
.pub
, msg
, sig
, **kw
)
953 class Ed25519Priv (_EdDSAPriv
, Ed25519Pub
):
954 _KEYSZ
= KeySZAny(ED25519_KEYSZ
)
955 def _pubkey(me
, priv
): return ed25519_pubkey(priv
)
956 def sign(me
, msg
, **kw
):
957 return ed25519_sign(me
.priv
, msg
, pub
= me
.pub
, **kw
)
959 class Ed448Pub (_EdDSAPub
):
960 _PUBSZ
= KeySZSet(ED448_PUBSZ
)
962 def verify(me
, msg
, sig
, **kw
):
963 return ed448_verify(me
.pub
, msg
, sig
, **kw
)
965 class Ed448Priv (_EdDSAPriv
, Ed448Pub
):
966 _KEYSZ
= KeySZAny(ED448_KEYSZ
)
967 def _pubkey(me
, priv
): return ed448_pubkey(priv
)
968 def sign(me
, msg
, **kw
):
969 return ed448_sign(me
.priv
, msg
, pub
= me
.pub
, **kw
)
971 ###--------------------------------------------------------------------------
972 ### Built-in named curves and prime groups.
974 class _groupmap (object):
975 def __init__(me
, map, nth
):
978 me
._n
= max(map.values()) + 1
981 return '{%s}' %
', '.join(['%r: %r' % kv
for kv
in me
.iteritems()])
982 def _repr_pretty_(me
, pp
, cyclep
):
983 ind
= _pp_bgroup(pp
, '{ ')
984 if cyclep
: pp
.text('...')
985 else: _pp_dict(pp
, me
.iteritems())
986 pp
.end_group(ind
, ' }')
989 def __contains__(me
, k
):
991 def __getitem__(me
, k
):
996 def __setitem__(me
, k
, v
):
997 raise TypeError, "immutable object"
1009 return [k
for k
in me
]
1011 return [me
[k
] for k
in me
]
1013 return [(k
, me
[k
]) for k
in me
]
1014 eccurves
= _groupmap(_base
._eccurves
, ECInfo
._curven
)
1015 primegroups
= _groupmap(_base
._pgroups
, DHInfo
._groupn
)
1016 bingroups
= _groupmap(_base
._bingroups
, BinDHInfo
._groupn
)
1018 ###--------------------------------------------------------------------------
1019 ### Prime number generation.
1021 class PrimeGenEventHandler (object):
1022 def pg_begin(me
, ev
):
1023 return me
.pg_try(ev
)
1024 def pg_done(me
, ev
):
1026 def pg_abort(me
, ev
):
1028 def pg_fail(me
, ev
):
1030 def pg_pass(me
, ev
):
1033 class SophieGermainStepJump (object):
1034 def pg_begin(me
, ev
):
1035 me
.lf
= PrimeFilter(ev
.x
)
1036 me
.hf
= me
.lf
.muladd(2, 1)
1042 while me
.lf
.status
== PGEN_FAIL
or me
.hf
.status
== PGEN_FAIL
:
1044 if me
.lf
.status
== PGEN_ABORT
or me
.hf
.status
== PGEN_ABORT
:
1047 if me
.lf
.status
== PGEN_DONE
and me
.hf
.status
== PGEN_DONE
:
1050 def pg_done(me
, ev
):
1054 class SophieGermainStepper (SophieGermainStepJump
):
1055 def __init__(me
, step
):
1059 me
.lf
.step(me
.lstep
)
1060 me
.hf
.step(me
.hstep
)
1062 class SophieGermainJumper (SophieGermainStepJump
):
1063 def __init__(me
, jump
):
1064 me
.ljump
= PrimeFilter(jump
);
1065 me
.hjump
= me
.ljump
.muladd(2, 0)
1067 me
.lf
.jump(me
.ljump
)
1068 me
.hf
.jump(me
.hjump
)
1069 def pg_done(me
, ev
):
1072 SophieGermainStepJump
.pg_done(me
, ev
)
1074 class SophieGermainTester (object):
1077 def pg_begin(me
, ev
):
1078 me
.lr
= RabinMiller(ev
.x
)
1079 me
.hr
= RabinMiller(2 * ev
.x
+ 1)
1081 lst
= me
.lr
.test(ev
.rng
.range(me
.lr
.x
))
1082 if lst
!= PGEN_PASS
and lst
!= PGEN_DONE
:
1084 rst
= me
.hr
.test(ev
.rng
.range(me
.hr
.x
))
1085 if rst
!= PGEN_PASS
and rst
!= PGEN_DONE
:
1087 if lst
== PGEN_DONE
and rst
== PGEN_DONE
:
1090 def pg_done(me
, ev
):
1094 class PrimitiveStepper (PrimeGenEventHandler
):
1100 def pg_begin(me
, ev
):
1101 me
.i
= iter(smallprimes
)
1102 return me
.pg_try(ev
)
1104 class PrimitiveTester (PrimeGenEventHandler
):
1105 def __init__(me
, mod
, hh
= [], exp
= None):
1106 me
.mod
= MPMont(mod
)
1111 if me
.exp
is not None:
1112 x
= me
.mod
.exp(x
, me
.exp
)
1113 if x
== 1: return PGEN_FAIL
1115 if me
.mod
.exp(x
, h
) == 1: return PGEN_FAIL
1119 class SimulStepper (PrimeGenEventHandler
):
1120 def __init__(me
, mul
= 2, add
= 1, step
= 2):
1124 def _stepfn(me
, step
):
1126 raise ValueError, 'step must be positive'
1128 return lambda f
: f
.step(step
)
1129 j
= PrimeFilter(step
)
1130 return lambda f
: f
.jump(j
)
1131 def pg_begin(me
, ev
):
1133 me
.lf
= PrimeFilter(x
)
1134 me
.hf
= PrimeFilter(x
* me
.mul
+ me
.add
)
1135 me
.lstep
= me
._stepfn(me
.step
)
1136 me
.hstep
= me
._stepfn(me
.step
* me
.mul
)
1137 SimulStepper
._cont(me
, ev
)
1145 while me
.lf
.status
== PGEN_FAIL
or me
.hf
.status
== PGEN_FAIL
:
1147 if me
.lf
.status
== PGEN_ABORT
or me
.hf
.status
== PGEN_ABORT
:
1150 if me
.lf
.status
== PGEN_DONE
and me
.hf
.status
== PGEN_DONE
:
1153 def pg_done(me
, ev
):
1159 class SimulTester (PrimeGenEventHandler
):
1160 def __init__(me
, mul
= 2, add
= 1):
1163 def pg_begin(me
, ev
):
1165 me
.lr
= RabinMiller(x
)
1166 me
.hr
= RabinMiller(x
* me
.mul
+ me
.add
)
1168 lst
= me
.lr
.test(ev
.rng
.range(me
.lr
.x
))
1169 if lst
!= PGEN_PASS
and lst
!= PGEN_DONE
:
1171 rst
= me
.hr
.test(ev
.rng
.range(me
.hr
.x
))
1172 if rst
!= PGEN_PASS
and rst
!= PGEN_DONE
:
1174 if lst
== PGEN_DONE
and rst
== PGEN_DONE
:
1177 def pg_done(me
, ev
):
1181 def sgprime(start
, step
= 2, name
= 'p', event
= pgen_nullev
, nsteps
= 0):
1183 return pgen(start
, name
, SimulStepper(step
= step
), SimulTester(), event
,
1184 nsteps
, RabinMiller
.iters(start
.nbits
))
1186 def findprimitive(mod
, hh
= [], exp
= None, name
= 'g', event
= pgen_nullev
):
1187 return pgen(0, name
, PrimitiveStepper(), PrimitiveTester(mod
, hh
, exp
),
1190 def kcdsaprime(pbits
, qbits
, rng
= rand
,
1191 event
= pgen_nullev
, name
= 'p', nsteps
= 0):
1192 hbits
= pbits
- qbits
1193 h
= pgen(rng
.mp(hbits
, 1), name
+ ' [h]',
1194 PrimeGenStepper(2), PrimeGenTester(),
1195 event
, nsteps
, RabinMiller
.iters(hbits
))
1196 q
= pgen(rng
.mp(qbits
, 1), name
, SimulStepper(2 * h
, 1, 2),
1197 SimulTester(2 * h
, 1), event
, nsteps
, RabinMiller
.iters(qbits
))
1201 #----- That's all, folks ----------------------------------------------------