@@ -5325,6 +5325,170 @@ class SendfileUsingSendfileTest(SendfileUsingSendTest):
53255325 def meth_from_sock (self , sock ):
53265326 return getattr (sock , "_sendfile_use_sendfile" )
53275327
5328+ @unittest .skipUnless (hasattr (socket , "AF_ALG" ), 'AF_ALG required' )
5329+ class LinuxKernelCryptoAPI (unittest .TestCase ):
5330+ # tests for AF_ALG
5331+ def create_alg (self , typ , name ):
5332+ sock = socket .socket (socket .AF_ALG , socket .SOCK_SEQPACKET , 0 )
5333+ sock .bind ((typ , name ))
5334+ return sock
5335+
5336+ def test_sha256 (self ):
5337+ expected = bytes .fromhex ("ba7816bf8f01cfea414140de5dae2223b00361a396"
5338+ "177a9cb410ff61f20015ad" )
5339+ with self .create_alg ('hash' , 'sha256' ) as algo :
5340+ op , _ = algo .accept ()
5341+ with op :
5342+ op .sendall (b"abc" )
5343+ self .assertEqual (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 (op .recv (512 ), expected )
5352+
5353+ def test_hmac_sha1 (self ):
5354+ expected = bytes .fromhex ("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 (op .recv (512 ), expected )
5361+
5362+ def test_aes_cbc (self ):
5363+ key = bytes .fromhex ('06a9214036b8a15b512e03d534120006' )
5364+ iv = bytes .fromhex ('3dafba429d9eb430b422da802c9fac41' )
5365+ msg = b"Single block msg"
5366+ ciphertext = bytes .fromhex ('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 )
5376+
5377+ op , _ = algo .accept ()
5378+ with op :
5379+ op .sendmsg_afalg ([ciphertext ],
5380+ op = socket .ALG_OP_DECRYPT , iv = iv )
5381+ self .assertEqual (op .recv (msglen ), msg )
5382+
5383+ # long message
5384+ multiplier = 1024
5385+ longmsg = [msg ] * multiplier
5386+ op , _ = algo .accept ()
5387+ with op :
5388+ op .sendmsg_afalg (longmsg ,
5389+ op = socket .ALG_OP_ENCRYPT , iv = iv )
5390+ enc = op .recv (msglen * multiplier )
5391+ self .assertEqual (len (enc ), msglen * multiplier )
5392+ self .assertTrue (enc [:msglen ], ciphertext )
5393+
5394+ op , _ = algo .accept ()
5395+ with op :
5396+ op .sendmsg_afalg ([enc ],
5397+ op = socket .ALG_OP_DECRYPT , iv = iv )
5398+ dec = op .recv (msglen * multiplier )
5399+ self .assertEqual (len (dec ), msglen * multiplier )
5400+ self .assertEqual (dec , msg * multiplier )
5401+
5402+ @support .requires_linux_version (3 , 19 )
5403+ def test_aead_aes_gcm (self ):
5404+ key = bytes .fromhex ('c939cc13397c1d37de6ae0e1cb7c423c' )
5405+ iv = bytes .fromhex ('b3d8cc017cbb89b39e0f67e2' )
5406+ plain = bytes .fromhex ('c3b3c41f113a31b73d9a5cd432103069' )
5407+ assoc = bytes .fromhex ('24825602bd12a984e0092d3e448eda5f' )
5408+ expected_ct = bytes .fromhex ('93fe7d9e9bfd10348a5606e5cafa7354' )
5409+ expected_tag = bytes .fromhex ('0032a1dc85f1c9786925a2e71d8272dd' )
5410+
5411+ taglen = len (expected_tag )
5412+ assoclen = len (assoc )
5413+
5414+ with self .create_alg ('aead' , 'gcm(aes)' ) as algo :
5415+ algo .setsockopt (socket .SOL_ALG , socket .ALG_SET_KEY , key )
5416+ algo .setsockopt (socket .SOL_ALG , socket .ALG_SET_AEAD_AUTHSIZE ,
5417+ None , taglen )
5418+
5419+ # send assoc, plain and tag buffer in separate steps
5420+ op , _ = algo .accept ()
5421+ with op :
5422+ op .sendmsg_afalg (op = socket .ALG_OP_ENCRYPT , iv = iv ,
5423+ assoclen = assoclen , flags = socket .MSG_MORE )
5424+ op .sendall (assoc , socket .MSG_MORE )
5425+ op .sendall (plain , socket .MSG_MORE )
5426+ op .sendall (b'\x00 ' * taglen )
5427+ res = op .recv (assoclen + len (plain ) + taglen )
5428+ self .assertEqual (expected_ct , res [assoclen :- taglen ])
5429+ self .assertEqual (expected_tag , res [- taglen :])
5430+
5431+ # now with msg
5432+ op , _ = algo .accept ()
5433+ with op :
5434+ msg = assoc + plain + b'\x00 ' * taglen
5435+ op .sendmsg_afalg ([msg ], op = socket .ALG_OP_ENCRYPT , iv = iv ,
5436+ assoclen = assoclen )
5437+ res = op .recv (assoclen + len (plain ) + taglen )
5438+ self .assertEqual (expected_ct , res [assoclen :- taglen ])
5439+ self .assertEqual (expected_tag , res [- taglen :])
5440+
5441+ # create anc data manually
5442+ pack_uint32 = struct .Struct ('I' ).pack
5443+ op , _ = algo .accept ()
5444+ with op :
5445+ msg = assoc + plain + b'\x00 ' * taglen
5446+ op .sendmsg (
5447+ [msg ],
5448+ ([socket .SOL_ALG , socket .ALG_SET_OP , pack_uint32 (socket .ALG_OP_ENCRYPT )],
5449+ [socket .SOL_ALG , socket .ALG_SET_IV , pack_uint32 (len (iv )) + iv ],
5450+ [socket .SOL_ALG , socket .ALG_SET_AEAD_ASSOCLEN , pack_uint32 (assoclen )],
5451+ )
5452+ )
5453+ res = op .recv (len (msg ))
5454+ self .assertEqual (expected_ct , res [assoclen :- taglen ])
5455+ self .assertEqual (expected_tag , res [- taglen :])
5456+
5457+ # decrypt and verify
5458+ op , _ = algo .accept ()
5459+ with op :
5460+ msg = assoc + expected_ct + expected_tag
5461+ op .sendmsg_afalg ([msg ], op = socket .ALG_OP_DECRYPT , iv = iv ,
5462+ assoclen = assoclen )
5463+ res = op .recv (len (msg ))
5464+ self .assertEqual (plain , res [assoclen :- taglen ])
5465+
5466+ def test_drbg_pr_sha256 (self ):
5467+ # deterministic random bit generator, prediction resistance, sha256
5468+ with self .create_alg ('rng' , 'drbg_pr_sha256' ) as algo :
5469+ extra_seed = os .urandom (32 )
5470+ algo .setsockopt (socket .SOL_ALG , socket .ALG_SET_KEY , extra_seed )
5471+ op , _ = algo .accept ()
5472+ with op :
5473+ rn = op .recv (32 )
5474+ self .assertEqual (len (rn ), 32 )
5475+
5476+ def test_sendmsg_afalg_args (self ):
5477+ sock = socket .socket (socket .AF_ALG , socket .SOCK_SEQPACKET , 0 )
5478+ with self .assertRaises (TypeError ):
5479+ sock .sendmsg_afalg ()
5480+
5481+ with self .assertRaises (TypeError ):
5482+ sock .sendmsg_afalg (op = None )
5483+
5484+ with self .assertRaises (TypeError ):
5485+ sock .sendmsg_afalg (1 )
5486+
5487+ with self .assertRaises (TypeError ):
5488+ sock .sendmsg_afalg (op = socket .ALG_OP_ENCRYPT , assoclen = None )
5489+
5490+ with self .assertRaises (TypeError ):
5491+ sock .sendmsg_afalg (op = socket .ALG_OP_ENCRYPT , assoclen = - 1 )
53285492
53295493def test_main ():
53305494 tests = [GeneralModuleTests , BasicTCPTest , TCPCloserTest , TCPTimeoutTest ,
@@ -5352,6 +5516,7 @@ def test_main():
53525516 tests .extend ([TIPCTest , TIPCThreadableTest ])
53535517 tests .extend ([BasicCANTest , CANTest ])
53545518 tests .extend ([BasicRDSTest , RDSTest ])
5519+ tests .append (LinuxKernelCryptoAPI )
53555520 tests .extend ([
53565521 CmsgMacroTests ,
53575522 SendmsgUDPTest ,
0 commit comments