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