31 |
31 |
32 def append_PKCS7_padding(b): |
32 def append_PKCS7_padding(b): |
33 """ |
33 """ |
34 Function to pad the given data to a multiple of 16-bytes by PKCS7 padding. |
34 Function to pad the given data to a multiple of 16-bytes by PKCS7 padding. |
35 |
35 |
36 @param b data to be padded (bytes) |
36 @param b data to be padded |
37 @return padded data (bytes) |
37 @type bytes |
|
38 @return padded data |
|
39 @rtype bytes |
38 """ |
40 """ |
39 numpads = 16 - (len(b) % 16) |
41 numpads = 16 - (len(b) % 16) |
40 return b + numpads * bytes(chr(numpads), encoding="ascii") |
42 return b + numpads * bytes(chr(numpads), encoding="ascii") |
41 |
43 |
42 |
44 |
43 def strip_PKCS7_padding(b): |
45 def strip_PKCS7_padding(b): |
44 """ |
46 """ |
45 Function to strip off PKCS7 padding. |
47 Function to strip off PKCS7 padding. |
46 |
48 |
47 @param b data to be stripped (bytes) |
49 @param b data to be stripped |
48 @return stripped data (bytes) |
50 @type bytes |
|
51 @return stripped data |
|
52 @rtype bytes |
49 @exception ValueError data padding is invalid |
53 @exception ValueError data padding is invalid |
50 """ |
54 """ |
51 if len(b) % 16 or not b: |
55 if len(b) % 16 or not b: |
52 raise ValueError("Data of len {0} can't be PCKS7-padded".format(len(b))) |
56 raise ValueError("Data of len {0} can't be PCKS7-padded".format(len(b))) |
53 numpads = b[-1] |
57 numpads = b[-1] |
849 |
853 |
850 def __getSBoxValue(self, num): |
854 def __getSBoxValue(self, num): |
851 """ |
855 """ |
852 Private method to retrieve a given S-Box value. |
856 Private method to retrieve a given S-Box value. |
853 |
857 |
854 @param num position of the value (integer) |
858 @param num position of the value |
855 @return value of the S-Box (integer) |
859 @type int |
|
860 @return value of the S-Box |
|
861 @rtype int |
856 """ |
862 """ |
857 return self.sbox[num] |
863 return self.sbox[num] |
858 |
864 |
859 def __getSBoxInvert(self, num): |
865 def __getSBoxInvert(self, num): |
860 """ |
866 """ |
861 Private method to retrieve a given Inverted S-Box value. |
867 Private method to retrieve a given Inverted S-Box value. |
862 |
868 |
863 @param num position of the value (integer) |
869 @param num position of the value |
864 @return value of the Inverted S-Box (integer) |
870 @type int |
|
871 @return value of the Inverted S-Box |
|
872 @rtype int |
865 """ |
873 """ |
866 return self.rsbox[num] |
874 return self.rsbox[num] |
867 |
875 |
868 def __rotate(self, data): |
876 def __rotate(self, data): |
869 """ |
877 """ |
870 Private method performing Rijndael's key schedule rotate operation. |
878 Private method performing Rijndael's key schedule rotate operation. |
871 |
879 |
872 Rotate the data word eight bits to the left: eg, |
880 Rotate the data word eight bits to the left: eg, |
873 rotate(1d2c3a4f) == 2c3a4f1d. |
881 rotate(1d2c3a4f) == 2c3a4f1d. |
874 |
882 |
875 @param data data of size 4 (bytearray) |
883 @param data data of size 4 |
876 @return rotated data (bytearray) |
884 @type bytearray |
|
885 @return rotated data |
|
886 @rtype bytearray |
877 """ |
887 """ |
878 return data[1:] + data[:1] |
888 return data[1:] + data[:1] |
879 |
889 |
880 def __getRconValue(self, num): |
890 def __getRconValue(self, num): |
881 """ |
891 """ |
882 Private method to retrieve a given Rcon value. |
892 Private method to retrieve a given Rcon value. |
883 |
893 |
884 @param num position of the value (integer) |
894 @param num position of the value |
885 @return Rcon value (integer) |
895 @type int |
|
896 @return Rcon value |
|
897 @rtype int |
886 """ |
898 """ |
887 return self.Rcon[num] |
899 return self.Rcon[num] |
888 |
900 |
889 def __core(self, data, iteration): |
901 def __core(self, data, iteration): |
890 """ |
902 """ |
891 Private method performing the key schedule core operation. |
903 Private method performing the key schedule core operation. |
892 |
904 |
893 @param data data to operate on (bytearray) |
905 @param data data to operate on |
894 @param iteration iteration counter (integer) |
906 @type bytearray |
895 @return modified data (bytearray) |
907 @param iteration iteration counter |
|
908 @type int |
|
909 @return modified data |
|
910 @rtype bytearray |
896 """ |
911 """ |
897 # rotate the 32-bit word 8 bits to the left |
912 # rotate the 32-bit word 8 bits to the left |
898 data = self.__rotate(data) |
913 data = self.__rotate(data) |
899 # apply S-Box substitution on all 4 parts of the 32-bit word |
914 # apply S-Box substitution on all 4 parts of the 32-bit word |
900 for i in range(4): |
915 for i in range(4): |
908 """ |
923 """ |
909 Private method performing Rijndael's key expansion. |
924 Private method performing Rijndael's key expansion. |
910 |
925 |
911 Expands a 128, 192 or 256 bit key into a 176, 208 or 240 bit key. |
926 Expands a 128, 192 or 256 bit key into a 176, 208 or 240 bit key. |
912 |
927 |
913 @param key key to be expanded (bytes or bytearray) |
928 @param key key to be expanded |
|
929 @type bytes or bytearray |
914 @param size size of the key in bytes (16, 24 or 32) |
930 @param size size of the key in bytes (16, 24 or 32) |
915 @param expandedKeySize size of the expanded key (integer) |
931 @type int |
916 @return expanded key (bytearray) |
932 @param expandedKeySize size of the expanded key |
|
933 @type int |
|
934 @return expanded key |
|
935 @rtype bytearray |
917 """ |
936 """ |
918 # current expanded keySize, in bytes |
937 # current expanded keySize, in bytes |
919 currentSize = 0 |
938 currentSize = 0 |
920 rconIteration = 1 |
939 rconIteration = 1 |
921 expandedKey = bytearray(expandedKeySize) |
940 expandedKey = bytearray(expandedKeySize) |
950 |
969 |
951 def __addRoundKey(self, state, roundKey): |
970 def __addRoundKey(self, state, roundKey): |
952 """ |
971 """ |
953 Private method to add (XORs) the round key to the state. |
972 Private method to add (XORs) the round key to the state. |
954 |
973 |
955 @param state state to be changed (bytearray) |
974 @param state state to be changed |
956 @param roundKey key to be used for the modification (bytearray) |
975 @type bytearray |
957 @return modified state (bytearray) |
976 @param roundKey key to be used for the modification |
|
977 @type bytearray |
|
978 @return modified state |
|
979 @rtype bytearray |
958 """ |
980 """ |
959 buf = state[:] |
981 buf = state[:] |
960 for i in range(16): |
982 for i in range(16): |
961 buf[i] ^= roundKey[i] |
983 buf[i] ^= roundKey[i] |
962 return buf |
984 return buf |
963 |
985 |
964 def __createRoundKey(self, expandedKey, roundKeyPointer): |
986 def __createRoundKey(self, expandedKey, roundKeyPointer): |
965 """ |
987 """ |
966 Private method to create a round key. |
988 Private method to create a round key. |
967 |
989 |
968 @param expandedKey expanded key to be used (bytearray) |
990 @param expandedKey expanded key to be used |
969 @param roundKeyPointer position within the expanded key (integer) |
991 @type bytearray |
970 @return round key (bytearray) |
992 @param roundKeyPointer position within the expanded key |
|
993 @type int |
|
994 @return round key |
|
995 @rtype bytearray |
971 """ |
996 """ |
972 roundKey = bytearray(16) |
997 roundKey = bytearray(16) |
973 for i in range(4): |
998 for i in range(4): |
974 for j in range(4): |
999 for j in range(4): |
975 roundKey[j * 4 + i] = expandedKey[roundKeyPointer + i * 4 + j] |
1000 roundKey[j * 4 + i] = expandedKey[roundKeyPointer + i * 4 + j] |
1000 def __subBytes(self, state, isInv): |
1028 def __subBytes(self, state, isInv): |
1001 """ |
1029 """ |
1002 Private method to substitute all the values from the state with the |
1030 Private method to substitute all the values from the state with the |
1003 value in the SBox using the state value as index for the SBox. |
1031 value in the SBox using the state value as index for the SBox. |
1004 |
1032 |
1005 @param state state to be worked on (bytearray) |
1033 @param state state to be worked on |
1006 @param isInv flag indicating an inverse operation (boolean) |
1034 @type bytearray |
1007 @return modified state (bytearray) |
1035 @param isInv flag indicating an inverse operation |
|
1036 @type bool |
|
1037 @return modified state |
|
1038 @rtype bytearray |
1008 """ |
1039 """ |
1009 state = state[:] |
1040 state = state[:] |
1010 getter = self.__getSBoxInvert if isInv else self.__getSBoxValue |
1041 getter = self.__getSBoxInvert if isInv else self.__getSBoxValue |
1011 for i in range(16): |
1042 for i in range(16): |
1012 state[i] = getter(state[i]) |
1043 state[i] = getter(state[i]) |
1015 def __shiftRows(self, state, isInv): |
1046 def __shiftRows(self, state, isInv): |
1016 """ |
1047 """ |
1017 Private method to iterate over the 4 rows and call __shiftRow() with |
1048 Private method to iterate over the 4 rows and call __shiftRow() with |
1018 that row. |
1049 that row. |
1019 |
1050 |
1020 @param state state to be worked on (bytearray) |
1051 @param state state to be worked on |
1021 @param isInv flag indicating an inverse operation (boolean) |
1052 @type bytearray |
1022 @return modified state (bytearray) |
1053 @param isInv flag indicating an inverse operation |
|
1054 @type bool |
|
1055 @return modified state |
|
1056 @rtype bytearray |
1023 """ |
1057 """ |
1024 state = state[:] |
1058 state = state[:] |
1025 for i in range(4): |
1059 for i in range(4): |
1026 state = self.__shiftRow(state, i * 4, i, isInv) |
1060 state = self.__shiftRow(state, i * 4, i, isInv) |
1027 return state |
1061 return state |
1028 |
1062 |
1029 def __shiftRow(self, state, statePointer, nbr, isInv): |
1063 def __shiftRow(self, state, statePointer, nbr, isInv): |
1030 """ |
1064 """ |
1031 Private method to shift the bytes of a row to the left. |
1065 Private method to shift the bytes of a row to the left. |
1032 |
1066 |
1033 @param state state to be worked on (bytearray) |
1067 @param state state to be worked on |
1034 @param statePointer index into the state (integer) |
1068 @type bytearray |
1035 @param nbr number of positions to shift (integer) |
1069 @param statePointer index into the state |
1036 @param isInv flag indicating an inverse operation (boolean) |
1070 @type int |
1037 @return modified state (bytearray) |
1071 @param nbr number of positions to shift |
|
1072 @type int |
|
1073 @param isInv flag indicating an inverse operation |
|
1074 @type bool |
|
1075 @return modified state |
|
1076 @rtype bytearray |
1038 """ |
1077 """ |
1039 state = state[:] |
1078 state = state[:] |
1040 for _ in range(nbr): |
1079 for _ in range(nbr): |
1041 if isInv: |
1080 if isInv: |
1042 state[statePointer : statePointer + 4] = ( |
1081 state[statePointer : statePointer + 4] = ( |
1052 |
1091 |
1053 def __mixColumns(self, state, isInv): |
1092 def __mixColumns(self, state, isInv): |
1054 """ |
1093 """ |
1055 Private method to perform a galois multiplication of the 4x4 matrix. |
1094 Private method to perform a galois multiplication of the 4x4 matrix. |
1056 |
1095 |
1057 @param state state to be worked on (bytearray) |
1096 @param state state to be worked on |
1058 @param isInv flag indicating an inverse operation (boolean) |
1097 @type bytearray |
1059 @return modified state (bytearray) |
1098 @param isInv flag indicating an inverse operation |
|
1099 @type bool |
|
1100 @return modified state |
|
1101 @rtype bytearray |
1060 """ |
1102 """ |
1061 state = state[:] |
1103 state = state[:] |
1062 # iterate over the 4 columns |
1104 # iterate over the 4 columns |
1063 for i in range(4): |
1105 for i in range(4): |
1064 # construct one column by slicing over the 4 rows |
1106 # construct one column by slicing over the 4 rows |
1074 def __mixColumn(self, column, isInv): |
1116 def __mixColumn(self, column, isInv): |
1075 """ |
1117 """ |
1076 Private method to perform a galois multiplication of 1 column the |
1118 Private method to perform a galois multiplication of 1 column the |
1077 4x4 matrix. |
1119 4x4 matrix. |
1078 |
1120 |
1079 @param column column to be worked on (bytearray) |
1121 @param column column to be worked on |
1080 @param isInv flag indicating an inverse operation (boolean) |
1122 @type bytearray |
1081 @return modified column (bytearray) |
1123 @param isInv flag indicating an inverse operation |
|
1124 @type bool |
|
1125 @return modified column |
|
1126 @rtype bytearray |
1082 """ |
1127 """ |
1083 column = column[:] |
1128 column = column[:] |
1084 mult = [14, 9, 13, 11] if isInv else [2, 1, 1, 3] |
1129 mult = [14, 9, 13, 11] if isInv else [2, 1, 1, 3] |
1085 cpy = column[:] |
1130 cpy = column[:] |
1086 g = self.__galois_multiplication |
1131 g = self.__galois_multiplication |
1114 def __aes_round(self, state, roundKey): |
1159 def __aes_round(self, state, roundKey): |
1115 """ |
1160 """ |
1116 Private method to apply the 4 operations of the forward round in |
1161 Private method to apply the 4 operations of the forward round in |
1117 sequence. |
1162 sequence. |
1118 |
1163 |
1119 @param state state to be worked on (bytearray) |
1164 @param state state to be worked on |
1120 @param roundKey round key to be used (bytearray) |
1165 @type bytearray |
1121 @return modified state (bytearray) |
1166 @param roundKey round key to be used |
|
1167 @type bytearray |
|
1168 @return modified state |
|
1169 @rtype bytearray |
1122 """ |
1170 """ |
1123 state = self.__subBytes(state, False) |
1171 state = self.__subBytes(state, False) |
1124 state = self.__shiftRows(state, False) |
1172 state = self.__shiftRows(state, False) |
1125 state = self.__mixColumns(state, False) |
1173 state = self.__mixColumns(state, False) |
1126 state = self.__addRoundKey(state, roundKey) |
1174 state = self.__addRoundKey(state, roundKey) |
1129 def __aes_invRound(self, state, roundKey): |
1177 def __aes_invRound(self, state, roundKey): |
1130 """ |
1178 """ |
1131 Private method to apply the 4 operations of the inverse round in |
1179 Private method to apply the 4 operations of the inverse round in |
1132 sequence. |
1180 sequence. |
1133 |
1181 |
1134 @param state state to be worked on (bytearray) |
1182 @param state state to be worked on |
1135 @param roundKey round key to be used (bytearray) |
1183 @type bytearray |
1136 @return modified state (bytearray) |
1184 @param roundKey round key to be used |
|
1185 @type bytearray |
|
1186 @return modified state |
|
1187 @rtype bytearray |
1137 """ |
1188 """ |
1138 state = self.__shiftRows(state, True) |
1189 state = self.__shiftRows(state, True) |
1139 state = self.__subBytes(state, True) |
1190 state = self.__subBytes(state, True) |
1140 state = self.__addRoundKey(state, roundKey) |
1191 state = self.__addRoundKey(state, roundKey) |
1141 state = self.__mixColumns(state, True) |
1192 state = self.__mixColumns(state, True) |
1147 |
1198 |
1148 Perform the initial operations, the standard round, and the |
1199 Perform the initial operations, the standard round, and the |
1149 final operations of the forward AES, creating a round key for |
1200 final operations of the forward AES, creating a round key for |
1150 each round. |
1201 each round. |
1151 |
1202 |
1152 @param state state to be worked on (bytearray) |
1203 @param state state to be worked on |
1153 @param expandedKey expanded key to be used (bytearray) |
1204 @type bytearray |
1154 @param nbrRounds number of rounds to be done (integer) |
1205 @param expandedKey expanded key to be used |
1155 @return modified state (bytearray) |
1206 @type bytearray |
|
1207 @param nbrRounds number of rounds to be done |
|
1208 @type int |
|
1209 @return modified state |
|
1210 @rtype bytearray |
1156 """ |
1211 """ |
1157 state = self.__addRoundKey(state, self.__createRoundKey(expandedKey, 0)) |
1212 state = self.__addRoundKey(state, self.__createRoundKey(expandedKey, 0)) |
1158 i = 1 |
1213 i = 1 |
1159 while i < nbrRounds: |
1214 while i < nbrRounds: |
1160 state = self.__aes_round(state, self.__createRoundKey(expandedKey, 16 * i)) |
1215 state = self.__aes_round(state, self.__createRoundKey(expandedKey, 16 * i)) |
1172 |
1227 |
1173 Perform the initial operations, the standard round, and the |
1228 Perform the initial operations, the standard round, and the |
1174 final operations of the inverse AES, creating a round key for |
1229 final operations of the inverse AES, creating a round key for |
1175 each round. |
1230 each round. |
1176 |
1231 |
1177 @param state state to be worked on (bytearray) |
1232 @param state state to be worked on |
1178 @param expandedKey expanded key to be used (bytearray) |
1233 @type bytearray |
1179 @param nbrRounds number of rounds to be done (integer) |
1234 @param expandedKey expanded key to be used |
1180 @return modified state (bytearray) |
1235 @type bytearray |
|
1236 @param nbrRounds number of rounds to be done |
|
1237 @type int |
|
1238 @return modified state |
|
1239 @rtype bytearray |
1181 """ |
1240 """ |
1182 state = self.__addRoundKey( |
1241 state = self.__addRoundKey( |
1183 state, self.__createRoundKey(expandedKey, 16 * nbrRounds) |
1242 state, self.__createRoundKey(expandedKey, 16 * nbrRounds) |
1184 ) |
1243 ) |
1185 i = nbrRounds - 1 |
1244 i = nbrRounds - 1 |
1196 def encrypt(self, iput, key, size): |
1255 def encrypt(self, iput, key, size): |
1197 """ |
1256 """ |
1198 Public method to encrypt a 128 bit input block against the given key |
1257 Public method to encrypt a 128 bit input block against the given key |
1199 of size specified. |
1258 of size specified. |
1200 |
1259 |
1201 @param iput input data (bytearray) |
1260 @param iput input data |
1202 @param key key to be used (bytes or bytearray) |
1261 @type bytearray |
|
1262 @param key key to be used |
|
1263 @type bytes or bytearray |
1203 @param size key size (16, 24 or 32) |
1264 @param size key size (16, 24 or 32) |
1204 @return encrypted data (bytes) |
1265 @type int |
|
1266 @return encrypted data |
|
1267 @rtype bytes |
1205 @exception ValueError key size is invalid |
1268 @exception ValueError key size is invalid |
1206 """ |
1269 """ |
1207 if size not in self.KeySize.values(): |
1270 if size not in self.KeySize.values(): |
1208 raise ValueError("Wrong key size given ({0}).".format(size)) |
1271 raise ValueError("Wrong key size given ({0}).".format(size)) |
1209 |
1272 |
1254 def decrypt(self, iput, key, size): |
1317 def decrypt(self, iput, key, size): |
1255 """ |
1318 """ |
1256 Public method to decrypt a 128 bit input block against the given key |
1319 Public method to decrypt a 128 bit input block against the given key |
1257 of size specified. |
1320 of size specified. |
1258 |
1321 |
1259 @param iput input data (bytearray) |
1322 @param iput input data |
1260 @param key key to be used (bytes or bytearray) |
1323 @type bytearray |
|
1324 @param key key to be used |
|
1325 @type bytes or bytearray |
1261 @param size key size (16, 24 or 32) |
1326 @param size key size (16, 24 or 32) |
1262 @return decrypted data (bytes) |
1327 @type int |
|
1328 @return decrypted data |
|
1329 @rtype bytes |
1263 @exception ValueError key size is invalid |
1330 @exception ValueError key size is invalid |
1264 """ |
1331 """ |
1265 if size not in self.KeySize.values(): |
1332 if size not in self.KeySize.values(): |
1266 raise ValueError("Wrong key size given ({0}).".format(size)) |
1333 raise ValueError("Wrong key size given ({0}).".format(size)) |
1267 |
1334 |
1322 |
1389 |
1323 def __extractBytes(self, inputData, start, end, mode): |
1390 def __extractBytes(self, inputData, start, end, mode): |
1324 """ |
1391 """ |
1325 Private method to extract a range of bytes from the input. |
1392 Private method to extract a range of bytes from the input. |
1326 |
1393 |
1327 @param inputData input data (bytes) |
1394 @param inputData input data |
1328 @param start start index (integer) |
1395 @type bytes |
1329 @param end end index (integer) |
1396 @param start start index |
|
1397 @type int |
|
1398 @param end end index |
|
1399 @type int |
1330 @param mode mode of operation (0, 1, 2) |
1400 @param mode mode of operation (0, 1, 2) |
1331 @return extracted bytes (bytearray) |
1401 @type int |
|
1402 @return extracted bytes |
|
1403 @rtype bytearray |
1332 """ |
1404 """ |
1333 if end - start > 16: |
1405 if end - start > 16: |
1334 end = start + 16 |
1406 end = start + 16 |
1335 ar = bytearray(16) if mode == self.ModeOfOperation["CBC"] else bytearray() |
1407 ar = bytearray(16) if mode == self.ModeOfOperation["CBC"] else bytearray() |
1336 |
1408 |
1346 |
1418 |
1347 def encrypt(self, inputData, mode, key, size, IV): |
1419 def encrypt(self, inputData, mode, key, size, IV): |
1348 """ |
1420 """ |
1349 Public method to perform the encryption operation. |
1421 Public method to perform the encryption operation. |
1350 |
1422 |
1351 @param inputData data to be encrypted (bytes) |
1423 @param inputData data to be encrypted |
|
1424 @type bytes |
1352 @param mode mode of operation (0, 1 or 2) |
1425 @param mode mode of operation (0, 1 or 2) |
1353 @param key key to be used (bytes) |
1426 @type int |
|
1427 @param key key to be used |
|
1428 @type bytes |
1354 @param size length of the key (16, 24 or 32) |
1429 @param size length of the key (16, 24 or 32) |
1355 @param IV initialisation vector (bytearray) |
1430 @type int |
|
1431 @param IV initialisation vector |
|
1432 @type bytearray |
1356 @return tuple with mode of operation, length of the input data and |
1433 @return tuple with mode of operation, length of the input data and |
1357 the encrypted data (integer, integer, bytes) |
1434 the encrypted data |
|
1435 @rtype tuple of (int, int, bytes) |
1358 @exception ValueError key size is invalid or decrypted data is invalid |
1436 @exception ValueError key size is invalid or decrypted data is invalid |
1359 """ |
1437 """ |
1360 if len(key) % size: |
1438 if len(key) % size: |
1361 raise ValueError("Illegal size ({0}) for key '{1}'.".format(size, key)) |
1439 raise ValueError("Illegal size ({0}) for key '{1}'.".format(size, key)) |
1362 if len(IV) % 16: |
1440 if len(IV) % 16: |
1434 # IV - the 128 bit number array Initilization Vector |
1512 # IV - the 128 bit number array Initilization Vector |
1435 def decrypt(self, cipherIn, originalsize, mode, key, size, IV): |
1513 def decrypt(self, cipherIn, originalsize, mode, key, size, IV): |
1436 """ |
1514 """ |
1437 Public method to perform the decryption operation. |
1515 Public method to perform the decryption operation. |
1438 |
1516 |
1439 @param cipherIn data to be decrypted (bytes) |
1517 @param cipherIn data to be decrypted |
|
1518 @type bytes |
1440 @param originalsize unencrypted string length (required for CBC) |
1519 @param originalsize unencrypted string length (required for CBC) |
1441 (integer) |
1520 @type int |
1442 @param mode mode of operation (0, 1 or 2) |
1521 @param mode mode of operation (0, 1 or 2) |
1443 @param key key to be used (bytes) |
1522 @type int |
|
1523 @param key key to be used |
|
1524 @type bytes |
1444 @param size length of the key (16, 24 or 32) |
1525 @param size length of the key (16, 24 or 32) |
1445 @param IV initialisation vector (bytearray) |
1526 @type int |
1446 @return decrypted data (bytes) |
1527 @param IV initialisation vector |
|
1528 @type bytearray |
|
1529 @return decrypted data |
|
1530 @rtype bytes |
1447 @exception ValueError key size is invalid or decrypted data is invalid |
1531 @exception ValueError key size is invalid or decrypted data is invalid |
1448 """ |
1532 """ |
1449 if len(key) % size: |
1533 if len(key) % size: |
1450 raise ValueError("Illegal size ({0}) for key '{1}'.".format(size, key)) |
1534 raise ValueError("Illegal size ({0}) for key '{1}'.".format(size, key)) |
1451 if len(IV) % 16: |
1535 if len(IV) % 16: |
1522 |
1606 |
1523 def encryptData(key, data, mode=AESModeOfOperation.ModeOfOperation["CBC"]): |
1607 def encryptData(key, data, mode=AESModeOfOperation.ModeOfOperation["CBC"]): |
1524 """ |
1608 """ |
1525 Module function to encrypt the given data with the given key. |
1609 Module function to encrypt the given data with the given key. |
1526 |
1610 |
1527 @param key key to be used for encryption (bytes) |
1611 @param key key to be used for encryption |
1528 @param data data to be encrypted (bytes) |
1612 @type bytes |
|
1613 @param data data to be encrypted |
|
1614 @type bytes |
1529 @param mode mode of operations (0, 1 or 2) |
1615 @param mode mode of operations (0, 1 or 2) |
1530 @return encrypted data prepended with the initialization vector (bytes) |
1616 @type int |
|
1617 @return encrypted data prepended with the initialization vector |
|
1618 @rtype bytes |
1531 @exception ValueError raised to indicate an invalid key size |
1619 @exception ValueError raised to indicate an invalid key size |
1532 """ |
1620 """ |
1533 key = bytearray(key) |
1621 key = bytearray(key) |
1534 if mode == AESModeOfOperation.ModeOfOperation["CBC"]: |
1622 if mode == AESModeOfOperation.ModeOfOperation["CBC"]: |
1535 data = append_PKCS7_padding(data) |
1623 data = append_PKCS7_padding(data) |
1548 |
1636 |
1549 def decryptData(key, data, mode=AESModeOfOperation.ModeOfOperation["CBC"]): |
1637 def decryptData(key, data, mode=AESModeOfOperation.ModeOfOperation["CBC"]): |
1550 """ |
1638 """ |
1551 Module function to decrypt the given data with the given key. |
1639 Module function to decrypt the given data with the given key. |
1552 |
1640 |
1553 @param key key to be used for decryption (bytes) |
1641 @param key key to be used for decryption |
|
1642 @type bytes |
1554 @param data data to be decrypted (with initialization vector prepended) |
1643 @param data data to be decrypted (with initialization vector prepended) |
1555 (bytes) |
1644 @type bytes |
1556 @param mode mode of operations (0, 1 or 2) |
1645 @param mode mode of operations (0, 1 or 2) |
1557 @return decrypted data (bytes) |
1646 @type int |
|
1647 @return decrypted data |
|
1648 @rtype bytes |
1558 @exception ValueError raised to indicate an invalid key size |
1649 @exception ValueError raised to indicate an invalid key size |
1559 """ |
1650 """ |
1560 key = bytearray(key) |
1651 key = bytearray(key) |
1561 keysize = len(key) |
1652 keysize = len(key) |
1562 if keysize not in AES.KeySize.values(): |
1653 if keysize not in AES.KeySize.values(): |