modified: myjupyterlab.sh
[GalaxyCodeBases.git] / etc / Windows / py-kms / kmsRequestV6.py
blob89cf6cfa0e21d7a26283ce9d542c29eae3706426
1 import aes
2 import binascii
3 import hashlib
4 import hmac
5 import random
6 import struct
7 from kmsBase import kmsBase
8 from kmsRequestV5 import kmsRequestV5
9 from structure import Structure
11 class kmsRequestV6(kmsRequestV5):
12 class DecryptedResponse(Structure):
13 class Message(Structure):
14 commonHdr = ()
15 structure = (
16 ('response', ':', kmsBase.kmsResponseStruct),
17 ('keys', '16s'),
18 ('hash', '32s'),
19 ('hwid', '8s'),
20 ('xorSalts', '16s'),
23 commonHdr = ()
24 structure = (
25 ('message', ':', Message),
26 ('hmac', '16s'),
29 key = bytearray([ 0xA9, 0x4A, 0x41, 0x95, 0xE2, 0x01, 0x43, 0x2D, 0x9B, 0xCB, 0x46, 0x04, 0x05, 0xD8, 0x4A, 0x21 ])
31 v6 = True
33 ver = 6
35 def encryptResponse(self, request, decrypted, response):
36 randomSalt = self.getRandomSalt()
37 sha256 = hashlib.sha256()
38 sha256.update(str(randomSalt))
39 result = sha256.digest()
41 SaltC = bytearray(request['message']['salt'])
42 DSaltC = bytearray(decrypted['salt'])
44 randomStuff = bytearray(16)
45 for i in range(0,16):
46 randomStuff[i] = (SaltC[i] ^ DSaltC[i] ^ randomSalt[i]) & 0xff
48 # XorSalts
49 XorSalts = bytearray(16)
50 for i in range (0, 16):
51 XorSalts[i] = (SaltC[i] ^ DSaltC[i]) & 0xff
53 message = self.DecryptedResponse.Message()
54 message['response'] = response
55 message['keys'] = str(randomStuff)
56 message['hash'] = result
57 message['xorSalts'] = str(XorSalts)
58 message['hwid'] = self.config['hwid']
60 # SaltS
61 SaltS = self.getRandomSalt()
63 moo = aes.AESModeOfOperation()
64 moo.aes.v6 = True
65 d = moo.decrypt(SaltS, 16, moo.modeOfOperation["CBC"], self.key, moo.aes.keySize["SIZE_128"], SaltS)
67 # DSaltS
68 DSaltS = bytearray(d)
70 # HMacMsg
71 HMacMsg = bytearray(16)
72 for i in range (0, 16):
73 HMacMsg[i] = (SaltS[i] ^ DSaltS[i]) & 0xff
74 HMacMsg.extend(str(message))
76 # HMacKey
77 requestTime = decrypted['request']['requestTime']
78 HMacKey = self.getMACKey(requestTime)
79 HMac = hmac.new(HMacKey, str(HMacMsg), hashlib.sha256)
80 digest = HMac.digest()
82 responsedata = self.DecryptedResponse()
83 responsedata['message'] = message
84 responsedata['hmac'] = digest[16:]
86 padded = aes.append_PKCS7_padding(str(responsedata))
87 mode, orig_len, crypted = moo.encrypt(str(padded), moo.modeOfOperation["CBC"], self.key, moo.aes.keySize["SIZE_128"], SaltS)
89 return str(SaltS), str(bytearray(crypted))
91 def getMACKey(self, t):
92 c1 = 0x00000022816889BD
93 c2 = 0x000000208CBAB5ED
94 c3 = 0x3156CD5AC628477A
96 i1 = (t / c1) & 0xFFFFFFFFFFFFFFFF
97 i2 = (i1 * c2) & 0xFFFFFFFFFFFFFFFF
98 seed = (i2 + c3) & 0xFFFFFFFFFFFFFFFF
100 sha256 = hashlib.sha256()
101 sha256.update(struct.pack("<Q", seed))
102 digest = sha256.digest()
104 return digest[16:]