I have a python application that creates a subprocess, opens a socket to communicate with it, then creates a multiprocessing.connection object over the socket. The connection object uses a shared key (randomly generated) and hmac to ensure that no other process will be allowed to communicate over the connection.
On Linux, this works perfectly. On windows, I get the error:
multiprocessing.AuthenticationError: digest received was wrong
The key is a string of randomly generated bits which is pickled before being sent to the subprocess by its stdin:
authkey = ''.join([chr(random.getrandbits(7)) for i in range(20)])
And I have checked carefully that the key matches on both ends of the connection this-a-way:
print "key:", ' '.join([str(ord(x)) for x in authkey])
The server is started with:
l = multiprocessing.connection.Listener(
('localhost', int(port)), authkey=authkey)
..and the client is started with:
c = multiprocessing.connection.Client(
('localhost', int(port)), authkey=authkey)
Both processes are running on the same machine, with the same version of python.
Stranger yet, I have found that if I fix the key (say, authkey=’test’), then I still get the AuthenticationError the first time I run the program, but not on subsequent runs.
The solution appears to be to use
os.urandomto generate the key rather than the method shown above. I do not know why this has any effect–in either case we’re passing random byte strings. The solution is also more correct from a security standpoint because getrandbits is not intended for cryptographic use.