43 """ |
43 """ |
44 Function to strip off PKCS7 padding. |
44 Function to strip off PKCS7 padding. |
45 |
45 |
46 @param b data to be stripped (bytes) |
46 @param b data to be stripped (bytes) |
47 @return stripped data (bytes) |
47 @return stripped data (bytes) |
|
48 @exception ValueError data padding is invalid |
48 """ |
49 """ |
49 if len(b) % 16 or not b: |
50 if len(b) % 16 or not b: |
50 raise ValueError( |
51 raise ValueError( |
51 "Data of len {0} can't be PCKS7-padded".format(len(b))) |
52 "Data of len {0} can't be PCKS7-padded".format(len(b))) |
52 numpads = b[-1] |
53 numpads = b[-1] |
169 |
170 |
170 Rotate the data word eight bits to the left: eg, |
171 Rotate the data word eight bits to the left: eg, |
171 rotate(1d2c3a4f) == 2c3a4f1d. |
172 rotate(1d2c3a4f) == 2c3a4f1d. |
172 |
173 |
173 @param data data of size 4 (bytearray) |
174 @param data data of size 4 (bytearray) |
|
175 @return rotated data (bytearray) |
174 """ |
176 """ |
175 return data[1:] + data[:1] |
177 return data[1:] + data[:1] |
176 |
178 |
177 def __getRconValue(self, num): |
179 def __getRconValue(self, num): |
178 """ |
180 """ |
487 |
489 |
488 @param iput input data (bytearray) |
490 @param iput input data (bytearray) |
489 @param key key to be used (bytes or bytearray) |
491 @param key key to be used (bytes or bytearray) |
490 @param size key size (16, 24 or 32) |
492 @param size key size (16, 24 or 32) |
491 @return encrypted data (bytes) |
493 @return encrypted data (bytes) |
|
494 @exception ValueError key size is invalid |
492 """ |
495 """ |
493 output = bytearray(16) |
496 output = bytearray(16) |
494 # the number of rounds |
497 # the number of rounds |
495 nbrRounds = 0 |
498 nbrRounds = 0 |
496 # the 128 bit block to encode |
499 # the 128 bit block to encode |
543 |
546 |
544 @param iput input data (bytearray) |
547 @param iput input data (bytearray) |
545 @param key key to be used (bytes or bytearray) |
548 @param key key to be used (bytes or bytearray) |
546 @param size key size (16, 24 or 32) |
549 @param size key size (16, 24 or 32) |
547 @return decrypted data (bytes) |
550 @return decrypted data (bytes) |
|
551 @exception ValueError key size is invalid |
548 """ |
552 """ |
549 output = bytearray(16) |
553 output = bytearray(16) |
550 # the number of rounds |
554 # the number of rounds |
551 nbrRounds = 0 |
555 nbrRounds = 0 |
552 # the 128 bit block to decode |
556 # the 128 bit block to decode |
637 @param key key to be used (bytes) |
641 @param key key to be used (bytes) |
638 @param size length of the key (16, 24 or 32) |
642 @param size length of the key (16, 24 or 32) |
639 @param IV initialisation vector (bytearray) |
643 @param IV initialisation vector (bytearray) |
640 @return tuple with mode of operation, length of the input and |
644 @return tuple with mode of operation, length of the input and |
641 the encrypted data (integer, integer, bytes) |
645 the encrypted data (integer, integer, bytes) |
|
646 @exception ValueError key size is invalid or decrypted data is invalid |
642 """ |
647 """ |
643 if len(key) % size: |
648 if len(key) % size: |
644 raise ValueError("Illegal size ({0}) for key '{1}'.".format( |
649 raise ValueError("Illegal size ({0}) for key '{1}'.".format( |
645 size, key)) |
650 size, key)) |
646 if len(IV) % 16: |
651 if len(IV) % 16: |
720 # IV - the 128 bit number array Initilization Vector |
725 # IV - the 128 bit number array Initilization Vector |
721 def decrypt(self, cipherIn, originalsize, mode, key, size, IV): |
726 def decrypt(self, cipherIn, originalsize, mode, key, size, IV): |
722 """ |
727 """ |
723 Public method to perform the decryption operation. |
728 Public method to perform the decryption operation. |
724 |
729 |
725 @param input data to be encrypted (bytes) |
730 @param cipherIn data to be decrypted (bytes) |
726 @param originalsize unencrypted string length (required for CBC) |
731 @param originalsize unencrypted string length (required for CBC) |
727 (integer) |
732 (integer) |
728 @param mode mode of operation (0, 1 or 2) |
733 @param mode mode of operation (0, 1 or 2) |
729 @param key key to be used (bytes) |
734 @param key key to be used (bytes) |
730 @param size length of the key (16, 24 or 32) |
735 @param size length of the key (16, 24 or 32) |
731 @param IV initialisation vector (bytearray) |
736 @param IV initialisation vector (bytearray) |
732 @return decrypted data (bytes) |
737 @return decrypted data (bytes) |
|
738 @exception ValueError key size is invalid or decrypted data is invalid |
733 """ |
739 """ |
734 if len(key) % size: |
740 if len(key) % size: |
735 raise ValueError("Illegal size ({0}) for key '{1}'.".format( |
741 raise ValueError("Illegal size ({0}) for key '{1}'.".format( |
736 size, key)) |
742 size, key)) |
737 if len(IV) % 16: |
743 if len(IV) % 16: |
838 @param key key to be used for decryption (bytes) |
844 @param key key to be used for decryption (bytes) |
839 @param data data to be decrypted (with initialization vector prepended) |
845 @param data data to be decrypted (with initialization vector prepended) |
840 (bytes) |
846 (bytes) |
841 @param mode mode of operations (0, 1 or 2) |
847 @param mode mode of operations (0, 1 or 2) |
842 @return decrypted data (bytes) |
848 @return decrypted data (bytes) |
843 @exception ValueError key size is invalid or decrypted data is invalid |
|
844 """ |
849 """ |
845 key = bytearray(key) |
850 key = bytearray(key) |
846 keysize = len(key) |
851 keysize = len(key) |
847 assert keysize in AES.KeySize.values(), 'invalid key size: %s' % keysize |
852 assert keysize in AES.KeySize.values(), 'invalid key size: %s' % keysize |
848 # iv is first 16 bytes |
853 # iv is first 16 bytes |