Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(17)

Side by Side Diff: Lib/test/test_socket.py

Issue 27744: Add AF_ALG (Linux Kernel crypto) to socket module
Patch Set: Created 3 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 import unittest 1 import unittest
2 from test import support 2 from test import support
3 3
4 import errno 4 import errno
5 import io 5 import io
6 import itertools 6 import itertools
7 import socket 7 import socket
8 import select 8 import select
9 import tempfile 9 import tempfile
10 import time 10 import time
11 import traceback 11 import traceback
12 import queue 12 import queue
13 import sys 13 import sys
14 import os 14 import os
15 import array 15 import array
16 import contextlib 16 import contextlib
17 from weakref import proxy 17 from weakref import proxy
18 import signal 18 import signal
19 import math 19 import math
20 import pickle 20 import pickle
21 import struct 21 import struct
22 import random 22 import random
23 import string 23 import string
24 from binascii import hexlify, unhexlify
24 try: 25 try:
25 import multiprocessing 26 import multiprocessing
26 except ImportError: 27 except ImportError:
27 multiprocessing = False 28 multiprocessing = False
28 try: 29 try:
29 import fcntl 30 import fcntl
30 except ImportError: 31 except ImportError:
31 fcntl = None 32 fcntl = None
32 33
33 HOST = support.HOST 34 HOST = support.HOST
(...skipping 5284 matching lines...) Expand 10 before | Expand all | Expand 10 after
5318 @unittest.skipUnless(thread, 'Threading required for this test.') 5319 @unittest.skipUnless(thread, 'Threading required for this test.')
5319 @unittest.skipUnless(hasattr(os, "sendfile"), 5320 @unittest.skipUnless(hasattr(os, "sendfile"),
5320 'os.sendfile() required for this test.') 5321 'os.sendfile() required for this test.')
5321 class SendfileUsingSendfileTest(SendfileUsingSendTest): 5322 class SendfileUsingSendfileTest(SendfileUsingSendTest):
5322 """ 5323 """
5323 Test the sendfile() implementation of socket.sendfile(). 5324 Test the sendfile() implementation of socket.sendfile().
5324 """ 5325 """
5325 def meth_from_sock(self, sock): 5326 def meth_from_sock(self, sock):
5326 return getattr(sock, "_sendfile_use_sendfile") 5327 return getattr(sock, "_sendfile_use_sendfile")
5327 5328
5329 @unittest.skipUnless(hasattr(socket, "AF_ALG"), 'AF_ALG required')
5330 class LinuxKernelCryptoAPI(unittest.TestCase):
5331 # tests for AF_ALG
5332 def create_alg(self, typ, name):
5333 sock = socket.socket(socket.AF_ALG, socket.SOCK_SEQPACKET, 0)
5334 sock.bind((typ, name))
5335 return sock
5336
5337 def test_sha256(self):
5338 expected = b"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20 015ad"
5339 with self.create_alg('hash', 'sha256') as algo:
5340 op, _ = algo.accept()
5341 with op:
5342 op.sendall(b"abc")
5343 self.assertEqual(hexlify(op.recv(512)), expected)
5344
5345 op, _ = algo.accept()
5346 with op:
5347 op.send(b'a', socket.MSG_MORE)
5348 op.send(b'b', socket.MSG_MORE)
5349 op.send(b'c', socket.MSG_MORE)
5350 op.send(b'')
5351 self.assertEqual(hexlify(op.recv(512)), expected)
5352
5353 def test_hmac_sha1(self):
5354 expected = b"effcdf6ae5eb2fa2d27416d5f184df9c259a7c79"
5355 with self.create_alg('hash', 'hmac(sha1)') as algo:
5356 algo.setsockopt(socket.SOL_ALG, socket.ALG_SET_KEY, b"Jefe")
5357 op, _ = algo.accept()
5358 with op:
5359 op.sendall(b"what do ya want for nothing?")
5360 self.assertEqual(hexlify(op.recv(512)), expected)
5361
5362 def test_aes_cbc(self):
5363 key = unhexlify('06a9214036b8a15b512e03d534120006')
5364 iv = unhexlify('3dafba429d9eb430b422da802c9fac41')
5365 msg = b"Single block msg"
5366 ciphertext = unhexlify('e353779c1079aeb82708942dbe77181a')
5367 msglen = len(msg)
5368 with self.create_alg('skcipher', 'cbc(aes)') as algo:
5369 algo.setsockopt(socket.SOL_ALG, socket.ALG_SET_KEY, key)
5370 op, _ = algo.accept()
5371 with op:
5372 op.sendmsg_afalg(op=socket.ALG_OP_ENCRYPT, iv=iv,
5373 flags=socket.MSG_MORE)
5374 op.sendall(msg)
5375 self.assertEqual(op.recv(msglen), ciphertext)
haypo 2016/08/30 16:56:07 please add an empty line line for readability
5376 op, _ = algo.accept()
5377 with op:
5378 op.sendmsg_afalg([ciphertext],
5379 op=socket.ALG_OP_DECRYPT, iv=iv)
5380 self.assertEqual(op.recv(msglen), msg)
5381
5382 multiplier = 1024
5383 longmsg = [msg] * multiplier
5384 op, _ = algo.accept()
5385 with op:
5386 op.sendmsg_afalg(longmsg,
5387 op=socket.ALG_OP_ENCRYPT, iv=iv)
5388 enc = op.recv(msglen * multiplier)
5389 self.assertEqual(len(enc), msglen * multiplier)
5390 self.assertTrue(enc[:msglen], ciphertext)
5391
5392 op, _ = algo.accept()
5393 with op:
5394 op.sendmsg_afalg([enc],
5395 op=socket.ALG_OP_DECRYPT, iv=iv)
5396 dec = op.recv(msglen * multiplier)
5397 self.assertEqual(len(dec), msglen * multiplier)
5398 self.assertEqual(dec, msg * multiplier)
5399
5400 def test_aead_aes_gcm(self):
5401 key = unhexlify('c939cc13397c1d37de6ae0e1cb7c423c')
5402 iv = unhexlify('b3d8cc017cbb89b39e0f67e2')
5403 plain = unhexlify('c3b3c41f113a31b73d9a5cd432103069')
5404 assoc = unhexlify('24825602bd12a984e0092d3e448eda5f')
5405 expected_ct = unhexlify('93fe7d9e9bfd10348a5606e5cafa7354')
5406 expected_tag = unhexlify('0032a1dc85f1c9786925a2e71d8272dd')
5407
5408 taglen = len(expected_tag)
5409 assoclen = len(assoc)
5410
5411 with self.create_alg('aead', 'gcm(aes)') as algo:
5412 algo.setsockopt(socket.SOL_ALG, socket.ALG_SET_KEY, key)
5413 algo.setsockopt(socket.SOL_ALG, socket.ALG_SET_AEAD_AUTHSIZE,
5414 None, taglen)
haypo 2016/08/30 16:56:07 According to socketmodule.c, _AHEAD_ constants are
5415
5416 # send assoc, plain and tag buffer in separate steps
5417 op, _ = algo.accept()
5418 with op:
5419 op.sendmsg_afalg(op=socket.ALG_OP_ENCRYPT, iv=iv,
5420 assoclen=assoclen, flags=socket.MSG_MORE)
5421 op.sendall(assoc, socket.MSG_MORE)
5422 op.sendall(plain, socket.MSG_MORE)
5423 op.sendall(b'\x00' * taglen)
5424 res = op.recv(assoclen + len(plain) + taglen)
5425 self.assertEqual(expected_ct, res[assoclen:-taglen])
5426 self.assertEqual(expected_tag, res[-taglen:])
5427
5428 # now with msg
5429 op, _ = algo.accept()
5430 with op:
5431 msg = assoc + plain + b'\x00' * taglen
5432 op.sendmsg_afalg([msg], op=socket.ALG_OP_ENCRYPT, iv=iv,
5433 assoclen=assoclen)
5434 res = op.recv(assoclen + len(plain) + taglen)
5435 self.assertEqual(expected_ct, res[assoclen:-taglen])
5436 self.assertEqual(expected_tag, res[-taglen:])
5437
5438 # create anc data manually
5439 pack_uint32 = struct.Struct('I').pack
5440 op, _ = algo.accept()
5441 with op:
5442 msg = assoc + plain + b'\x00' * taglen
5443 op.sendmsg(
5444 [msg],
5445 ([socket.SOL_ALG, socket.ALG_SET_OP, pack_uint32(socket.ALG_ OP_ENCRYPT)],
5446 [socket.SOL_ALG, socket.ALG_SET_IV, pack_uint32(len(iv)) + iv],
5447 [socket.SOL_ALG, socket.ALG_SET_AEAD_ASSOCLEN, pack_uint32( assoclen)],
5448 )
5449 )
5450 res = op.recv(len(msg))
5451 self.assertEqual(expected_ct, res[assoclen:-taglen])
5452 self.assertEqual(expected_tag, res[-taglen:])
5453
5454 # decrypt and verify
5455 op, _ = algo.accept()
5456 with op:
5457 msg = assoc + expected_ct + expected_tag
5458 op.sendmsg_afalg([msg], op=socket.ALG_OP_DECRYPT, iv=iv,
5459 assoclen=assoclen)
5460 res = op.recv(len(msg))
5461 self.assertEqual(plain, res[assoclen:-taglen])
5462
5463 def test_drbg_pr_sha256(self):
5464 # deterministic random bit generator, prediction resistance, sha256
5465 with self.create_alg('rng', 'drbg_pr_sha256') as algo:
5466 extra_seed = os.urandom(32)
5467 algo.setsockopt(socket.SOL_ALG, socket.ALG_SET_KEY, extra_seed)
5468 op, _ = algo.accept()
5469 with op:
5470 rn = op.recv(32)
5471 self.assertEqual(len(rn), 32)
5472
5473 def test_sendmsg_afalg_args(self):
5474 sock = socket.socket(socket.AF_ALG, socket.SOCK_SEQPACKET, 0)
5475 with self.assertRaises(TypeError):
5476 sock.sendmsg_afalg()
5477
5478 with self.assertRaises(TypeError):
5479 sock.sendmsg_afalg(op=None)
5480
5481 with self.assertRaises(TypeError):
5482 sock.sendmsg_afalg(1)
5483
5484 with self.assertRaises(TypeError):
5485 sock.sendmsg_afalg(op=socket.ALG_OP_ENCRYPT, assoclen=None)
5486
5487 with self.assertRaises(TypeError):
5488 sock.sendmsg_afalg(op=socket.ALG_OP_ENCRYPT, assoclen=-1)
5328 5489
5329 def test_main(): 5490 def test_main():
5330 tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest, 5491 tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest,
5331 TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest, UDPTimeo utTest ] 5492 TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest, UDPTimeo utTest ]
5332 5493
5333 tests.extend([ 5494 tests.extend([
5334 NonBlockingTCPTests, 5495 NonBlockingTCPTests,
5335 FileObjectClassTestCase, 5496 FileObjectClassTestCase,
5336 UnbufferedFileObjectClassTestCase, 5497 UnbufferedFileObjectClassTestCase,
5337 LineBufferedFileObjectClassTestCase, 5498 LineBufferedFileObjectClassTestCase,
5338 SmallBufferedFileObjectClassTestCase, 5499 SmallBufferedFileObjectClassTestCase,
5339 UnicodeReadFileObjectClassTestCase, 5500 UnicodeReadFileObjectClassTestCase,
5340 UnicodeWriteFileObjectClassTestCase, 5501 UnicodeWriteFileObjectClassTestCase,
5341 UnicodeReadWriteFileObjectClassTestCase, 5502 UnicodeReadWriteFileObjectClassTestCase,
5342 NetworkConnectionNoServer, 5503 NetworkConnectionNoServer,
5343 NetworkConnectionAttributesTest, 5504 NetworkConnectionAttributesTest,
5344 NetworkConnectionBehaviourTest, 5505 NetworkConnectionBehaviourTest,
5345 ContextManagersTest, 5506 ContextManagersTest,
5346 InheritanceTest, 5507 InheritanceTest,
5347 NonblockConstantTest 5508 NonblockConstantTest
5348 ]) 5509 ])
5349 tests.append(BasicSocketPairTest) 5510 tests.append(BasicSocketPairTest)
5350 tests.append(TestUnixDomain) 5511 tests.append(TestUnixDomain)
5351 tests.append(TestLinuxAbstractNamespace) 5512 tests.append(TestLinuxAbstractNamespace)
5352 tests.extend([TIPCTest, TIPCThreadableTest]) 5513 tests.extend([TIPCTest, TIPCThreadableTest])
5353 tests.extend([BasicCANTest, CANTest]) 5514 tests.extend([BasicCANTest, CANTest])
5354 tests.extend([BasicRDSTest, RDSTest]) 5515 tests.extend([BasicRDSTest, RDSTest])
5516 tests.append(LinuxKernelCryptoAPI)
5355 tests.extend([ 5517 tests.extend([
5356 CmsgMacroTests, 5518 CmsgMacroTests,
5357 SendmsgUDPTest, 5519 SendmsgUDPTest,
5358 RecvmsgUDPTest, 5520 RecvmsgUDPTest,
5359 RecvmsgIntoUDPTest, 5521 RecvmsgIntoUDPTest,
5360 SendmsgUDP6Test, 5522 SendmsgUDP6Test,
5361 RecvmsgUDP6Test, 5523 RecvmsgUDP6Test,
5362 RecvmsgRFC3542AncillaryUDP6Test, 5524 RecvmsgRFC3542AncillaryUDP6Test,
5363 RecvmsgIntoRFC3542AncillaryUDP6Test, 5525 RecvmsgIntoRFC3542AncillaryUDP6Test,
5364 RecvmsgIntoUDP6Test, 5526 RecvmsgIntoUDP6Test,
(...skipping 15 matching lines...) Expand all
5380 SendfileUsingSendTest, 5542 SendfileUsingSendTest,
5381 SendfileUsingSendfileTest, 5543 SendfileUsingSendfileTest,
5382 ]) 5544 ])
5383 5545
5384 thread_info = support.threading_setup() 5546 thread_info = support.threading_setup()
5385 support.run_unittest(*tests) 5547 support.run_unittest(*tests)
5386 support.threading_cleanup(*thread_info) 5548 support.threading_cleanup(*thread_info)
5387 5549
5388 if __name__ == "__main__": 5550 if __name__ == "__main__":
5389 test_main() 5551 test_main()
OLDNEW

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+