|
4 | 4 | import struct
|
5 | 5 | import logging
|
6 | 6 | import sys
|
| 7 | +import encrypt |
7 | 8 |
|
8 | 9 | slow_xor = False
|
9 | 10 | imported = False
|
@@ -72,37 +73,55 @@ def update(self, data):
|
72 | 73 | cur_data = data[:remain]
|
73 | 74 | cur_data_len = len(cur_data)
|
74 | 75 | cur_stream = self._stream[self._pos:self._pos + cur_data_len]
|
75 |
| - self._pos = (self._pos + cur_data_len) % BLOCK_SIZE |
| 76 | + self._pos = self._pos + cur_data_len |
76 | 77 | data = data[remain:]
|
77 | 78 |
|
78 | 79 | results.append(numpy_xor(cur_data, cur_stream))
|
79 | 80 |
|
| 81 | + if self._pos >= BLOCK_SIZE: |
| 82 | + self._next_stream() |
| 83 | + self._pos -= BLOCK_SIZE |
| 84 | + assert self._pos == 0 |
80 | 85 | if not data:
|
81 | 86 | break
|
82 |
| - self._next_stream() |
83 | 87 | return ''.join(results)
|
84 | 88 |
|
85 | 89 |
|
86 | 90 | def test():
|
87 | 91 | from os import urandom
|
88 | 92 | import random
|
89 | 93 |
|
90 |
| - rounds = 1 * 10 |
| 94 | + rounds = 1 * 1024 |
91 | 95 | plain = urandom(BLOCK_SIZE * rounds)
|
| 96 | + import M2Crypto.EVP |
| 97 | + cipher = M2Crypto.EVP.Cipher('aes_128_cfb', 'k' * 32, 'i' * 16, 1, |
| 98 | + key_as_bytes=0, d='md5', salt=None, i=1, |
| 99 | + padding=1) |
| 100 | + decipher = M2Crypto.EVP.Cipher('aes_128_cfb', 'k' * 32, 'i' * 16, 0, |
| 101 | + key_as_bytes=0, d='md5', salt=None, i=1, |
| 102 | + padding=1) |
| 103 | + |
92 | 104 | cipher = Salsa20Cipher('salsa20-ctr', 'k' * 32, 'i' * 8, 1)
|
93 | 105 | decipher = Salsa20Cipher('salsa20-ctr', 'k' * 32, 'i' * 8, 1)
|
94 | 106 | results = []
|
95 | 107 | pos = 0
|
96 | 108 | print 'start'
|
97 | 109 | start = time.time()
|
98 | 110 | while pos < len(plain):
|
99 |
| - l = random.randint(10000, 32768) |
| 111 | + l = random.randint(100, 16384) |
100 | 112 | c = cipher.update(plain[pos:pos + l])
|
101 |
| - results.append(decipher.update(c)) |
| 113 | + results.append(c) |
| 114 | + pos += l |
| 115 | + pos = 0 |
| 116 | + c = ''.join(results) |
| 117 | + results = [] |
| 118 | + while pos < len(plain): |
| 119 | + l = random.randint(100, 16384) |
| 120 | + results.append(decipher.update(c[pos:pos + l])) |
102 | 121 | pos += l
|
103 |
| - assert ''.join(results) == plain |
104 | 122 | end = time.time()
|
105 | 123 | print BLOCK_SIZE * rounds / (end - start)
|
| 124 | + assert ''.join(results) == plain |
106 | 125 |
|
107 | 126 |
|
108 | 127 | if __name__ == '__main__':
|
|
0 commit comments