Beispiel #1
0
def test_init(context):
    s_expr = SExpression(b'(test (a "123"))')
    assert repr(s_expr) == "SExpression(b'(test \\n (a \"123\")\\n )\\n')"
    s_expr = SExpression(b'(test (a %d)(b %s)(c %b))', 10, 'Hello World', 3, b'123')
    assert repr(s_expr) == "SExpression(b'(test \\n (a \"10\")\\n (b \"Hello World\")\\n (c \"123\")\\n )\\n')"
    s_expr2 = SExpression(s_expr.sexp)
    assert repr(s_expr2) == "SExpression(b'(test \\n (a \"10\")\\n (b \"Hello World\")\\n (c \"123\")\\n )\\n')"
Beispiel #2
0
def test_extract(context):
    s_expr = SExpression(b'(test (a "123") (b "-123") (longitem "Ohai world"))')
    ret = s_expr.extract('a')
    assert ret['a'] == 3224115
    ret = s_expr.extract('-b', b'test')
    assert ret['b'] == 758198835
    ret = s_expr.extract("/'longitem'")
    assert isinstance(ret["'longitem'"], MPIopaque) == True
    assert ret["'longitem'"] == b'Ohai world'
Beispiel #3
0
def test_encryption(context, private, public):
    input_data = SExpression(b'(data (flags)(value %s))', b"Hello World!\n")
    encrypted_data = SExpression(
        b'(enc-val (rsa (a #0081C38D19B9DB672738EBA125714C3765C6292F8EA6BD988E3663104D616F183E7F6C96C5422D13032BC4FEA2415533C35F065E13E741BB2E31FE057D943FD92157F3E09BB17CE353929231DE376483C231313694451010C27B84AD8F171EDCFA51A493B9655DF59EEDC591FFDA4C25424FEA034BD60ECC9D433AE7E67362C292309BE5226997F93E2123FB2F9AF066E0D74F8A3F6FCC1511D4B80DEC92DEB95CBD16C7A9E1EA05F5252B7A5D1954600D532B9464E830579392EFD8979320CB8DBE02932681A7BD149E06F04A58F50B6E791E4E7E8C318EA24821327AF3F96C1B8CE78039E9BB2CBF302A100987B7CBDE842D199CAF9BD059432E45C28D3C743F31FBD84192F720329DF2E55F3005271C47BCACAFB2BAE4F886391E5C9CA83AF0758B7B1C48C2CEB1DDE2D3D227A3F811ADEEE79F3607B7E0230ACE8770602013DF06B32C33A9407E7551D1B7C801EC23237E3175E397F28F21BD611FB68B6EE214F367AE52C78D4A6515352DB620B102B687DFC107AD605022EA7E8A540A96457403B1EC4DE8A783C6F13877BC869DB7DB746316697A7824BEC1F330ABCFD1C2FE7BFB41BC7BD0307FD78EE9DD111984D77704A8801C06880D2BC771883FE7C2DB6CCA9FB4812483B4262A2D1FDE23458511BACC052A219870B6E248D94DED6968C8B0EC9335523A8BB4CD89BECB11FDE3747BAF13C29BDC16421A19988D1CB3#)))'
    )
    decrypted_data = SExpression(b'(value %s)', b"Hello World!\n")
    assert public.encrypt(input_data) == encrypted_data
    assert private.decrypt(encrypted_data) == decrypted_data
    with pytest.raises(NotImplementedError):
        private.encrypt(input_data)
        public.decrypt(encrypted_data)
Beispiel #4
0
def test_fromdict(context):
    dict_a = {'a': MPIint(1)}
    dict_b = {'a': "123"}
    dict_c = {'a': "Hello World"}
    dict_d = {'a': {'b': "Test", 'c': 123}}
    assert str(SExpression.from_dict(dict_a)) == str(SExpression(b'(a %m)', MPIint(1)))
    assert str(SExpression.from_dict(dict_b)) == str(SExpression(b'(a %s)', "123"))
    assert str(SExpression.from_dict(dict_c)) == str(SExpression(b'(a %b)', 11, "Hello World"))
    assert str(SExpression.from_dict(dict_d)) == str(SExpression(b'(a (b %b)(c %d))', 4, "Test", 123))
Beispiel #5
0
def public():
    return Key(SExpression(b"""
(public-key 
  (rsa 
    (n #00D3D01AD42539298A201D311F3145D87A6BA22851702D91633731AAD6491771CB2BEBB5695DD3628AB21E76EBEF0D718E281AF6E963B3CF9D25C0384052CCC4B97AF75A2B11821F3D2EAAF3C468F65178EEA5ABF5E84EEBB40AC00C78BF6C8D72C7071871FBDC8E26767DCFF88F8362657FF3046235C50F67E1AC2CEFF3699D13F13A9649FB7297BC0F6688C696766E0C6E0143211EFCD546988177A39F71A243D6B352220D9C5FEFF9548CF8DE6111023B35C32EAFAC884EB1B4B4EEB17F7D2A4CA01D1B8413BFDB5FCEA09865D756F4C2D24B3B811D918D4B55D3CE34AF8CA648D69ACBE717343278798A3D01E6E5A174A491A48272E72ACBC3159427079BF1493E303C0F1BDCBBA032505071079761C667CF59ADE988CFEC50191180B9F6FC954353A71E86FFB8458B00F21B47F6F13A302587BE0CF51CC92AA610122F1D17308450F033075567D8C5CF2D0F9DA4B9CBA4F85DB0E6EDFE6BE5C6BE8E31B6E07CA205FA8883F19D1DE0D72B3297F3FD53CEFDBBBFBC9F42142DF077126251E57CF06D6CD47762AF930F1F3AC1E07FD01F0A6994C2F177BFD35BB7E8339506D186B92B85AB0692EB82CDA09A46F4CC3158A2BCC971E317F1DDF9B3298A46BDE9E6F379F903F0754372C03463AEC2D96554FAB0F8E756FFC2FF3EB6094C1D48ABDCB653EC42CB3832A2A363E5D96AF3CA7C2412256F2A8DA92AE615CEE00518EB#)
    (e #010001#)
  )
)"""))
Beispiel #6
0
def test_getitem(context):
    s_expr = SExpression(b'(a "hello World")')
    #assert print(s_expr['a']) == print("(hello World)")
    #assert print(s_expr[0]) == print("(a)")
    #assert print(s_expr[0:1]) == print(b'(a "hello World")')
    with pytest.raises(IndexError):
        s_expr[4]
        s_expr[-1]
        s_expr[3:5]
Beispiel #7
0
def test_getkey(context):
    sexp = SExpression(
        b'(public-key (ecc (curve "NIST P-256")(q #0442B927242237639A36CE9221B340DB1A9AB76DF2FE3E171277F6A4023DED146EE86525E38CCECFF3FB8D152CC6334F70D23A525175C1BCBDDE6E023B2228770E#)))'
    )
    ec = ECurve(keyparam=sexp)

    with pytest.raises(errors.GcryptException):
        priv = ec.key(mode=u'SECKEY')
    pub = ec.key(mode=u'PUBKEY')

    assert pub.type == u'public'
Beispiel #8
0
def private():
    return Key(SExpression(b"""
(private-key 
  (rsa
    (n #00D3D01AD42539298A201D311F3145D87A6BA22851702D91633731AAD6491771CB2BEBB5695DD3628AB21E76EBEF0D718E281AF6E963B3CF9D25C0384052CCC4B97AF75A2B11821F3D2EAAF3C468F65178EEA5ABF5E84EEBB40AC00C78BF6C8D72C7071871FBDC8E26767DCFF88F8362657FF3046235C50F67E1AC2CEFF3699D13F13A9649FB7297BC0F6688C696766E0C6E0143211EFCD546988177A39F71A243D6B352220D9C5FEFF9548CF8DE6111023B35C32EAFAC884EB1B4B4EEB17F7D2A4CA01D1B8413BFDB5FCEA09865D756F4C2D24B3B811D918D4B55D3CE34AF8CA648D69ACBE717343278798A3D01E6E5A174A491A48272E72ACBC3159427079BF1493E303C0F1BDCBBA032505071079761C667CF59ADE988CFEC50191180B9F6FC954353A71E86FFB8458B00F21B47F6F13A302587BE0CF51CC92AA610122F1D17308450F033075567D8C5CF2D0F9DA4B9CBA4F85DB0E6EDFE6BE5C6BE8E31B6E07CA205FA8883F19D1DE0D72B3297F3FD53CEFDBBBFBC9F42142DF077126251E57CF06D6CD47762AF930F1F3AC1E07FD01F0A6994C2F177BFD35BB7E8339506D186B92B85AB0692EB82CDA09A46F4CC3158A2BCC971E317F1DDF9B3298A46BDE9E6F379F903F0754372C03463AEC2D96554FAB0F8E756FFC2FF3EB6094C1D48ABDCB653EC42CB3832A2A363E5D96AF3CA7C2412256F2A8DA92AE615CEE00518EB#)
    (e #010001#)
    (d #113EE8704D779202450B1B6CC6BE4119F699855EDC1B59B3125360ED8195FF15E3C59547B6F3F71A03D6A7281E96A1A04973A896E81F43D02FBAAED9CDE40E406BDAA7E4C9D0266AFCD9BE6F477C86B5A9F8A33FAA62829E6E21A4E24FF3AEB0A1B3DA31ADA80F8AE22AEC3BF8B361D791CB5F97FF78C4005947838125F87B699C4280EA15270763B07D50B59A9A99EB3929B12589385CC51AB5AFE00AA35DEAD818519FA42C0F43A2B157A90BB22B885AF9CDA2877B2CBDFB8A13113C327D266C86BCD7F8B102F6019EF9446F4651362C86A2C97D81C2D0164A2EACA36D87AE225016DA733E5869A39581C3E5DBBEE69E70A7CB1533F5CE6E38BC110BFC7A9DCCC273D9BA3CFC883A1E60A029174B4C053BCC308DE3CBB87B3DA0A7D59F5E9977828C818B2080C70076906ED5C1419CF2B2D9FA6711BAA1B34BFC1FACD3D8BBBD98DE1DADD3AE032C3714A46FB475DDC7887CA00A23E343F8F3744ECD0C1882317A9C44C17063C62D31598190942F8411E3F31A428D51996028EA97B9BD8141B9279BCB07952F406708971597CF9C72C95B07F4694B27A2BC8A98EB813EB2BBBD0DB1CF2CEE6256832C4B1A13E5F1F8907F568DEB491B21D7384BAD2E3C86B2789379129D779EE0A06944EADCFEA4E1CA8C0A9F83820D6CA4E48D7382E83A0E129D02A8ACBFCEC2F4E1124F39A07A5F5E7BD7726E2B06F7C9C0EC9C7371AEC1#)
    (p #00E31EBA29F18D0317D9CD973DF9AC413AC9FBAA801D35C4585F1EDBF043B1C855CA6224CE9DF88AD467AD50950ABE121EE9E43A240A005972D3DA54414B4974FC712B4F6BA5698564C1934D066837BEB23C49ADE82C92AE5829F68024F0F8314CCBEF198D773FDE184CA2ABBBA165447320581EBF9BE14CEFD7C98E708F83C15E2CDEA0A8D591AF48E47B90B0C1555DB823417C07741D0F146531333E6520D909CA8C8E2543EF24125481E3984B66C1598D87B91AFFFF9095B231C89096703B208D103DFFD47EDC97E3DEE8E81C90B5B2C8AD2863EB8319F3484759A08A8901D541542101263D1CD08AC4E0F76F44E6140AE595CFBA20DF706E201E6AE251248D#)
    (q #00EEBF189AD7C4422B52EF25604C033BE4F2117F38441D9F084C0922C4D2C15BE69868797DF0E9FFDA364234226DCCB4EE823383E49CD29249B8C87AB318DCF23301E8C17DD6DBCE25E9083F955DA9F2F8B75B1763274255888085DC2D50B539AA3D5376A09B92E3FED26AC0FB2C5D48AAC1DB307029A2519DC2FFB17162C32B9FC2C3A6421B49EE3D501CAAA4EBF3B55C681F9B0F5BE6673C4F349E7830FC9FA7816204C56797A8F3C873BCB3BBD773B1AD8E960B3CF8548E5647F826DEA587D2FB2F887163268B9BDB2ABAF8FC0DF8B0A80A80BC38C72FF2E9F48FD61FD1106B4CF056B93EB0DD751BE5D7BD192DEBF1E43B83B5C60C1EED17B39F3D9B89A157#)
    (u #0085C72E87702CF7D0C84A9C4753F1D3FA07798D3E0AF8C617F186E7A756980E46FBC64656C347BB83F31AAA14236DABA5D034BE373CA5D851428D80618D7AC726CC11D8F420DB2ABB86310D01EE42B4AE1A49939DB76F4D1CD6BB3441308F204882BCE5A957449C8663B9B182831A8472FFE2ECD913E8D44DE9FC2BC5933D6169E03C58CE17A7F65AD68193E67E80A29209D746617CB2A64CF5506E9058D9330EEAA36A87D76FB3D28415C28019903DCE81A34E897D1B4AB7FD0F8E122EB32E945359F7D2397739FFA4953E83AF8E4B60B2605717BC85F6A2B133B12A23A28E443D14FA5DE5D9482A63F562202C487B97F48A40DD3A41C7D2AD1DF472A1EA4F02#)
 )
)"""))
Beispiel #9
0
def generate_keys():  
    
    #sexp =  SExpression(b"""(public-key ( algo "rsa") )""")
    sexp =  SExpression(b"""(private-key (algo "rsa")) (private-key (algo "rsa")) """)
    print (sexp)
    
    (priv, pub) = create_keys(keyparam=sexp, algo="rsx")
    #(priv, pub) = (1, 2)
    #assert isinstance(priv, Key)
    #assert isinstance(pub, Key)
    #print(priv, pub)
    return (priv, pub)
Beispiel #10
0
def test_sign(context, private, public):
    hashing = sha256()
    hashing.update(b"Test data inserted")
    data = hashing.digest()
    with pytest.raises(NotImplementedError):
        public.makesign(data)

    sig = private.makesign(data)
    assert sig == SExpression(
        b'(sig-val \n (rsa \n  (s #0DB6FD3B8915538E0C3619B32F47BBB2990E36F3E0ACBBA80E58BF317079928575EB06ED86247FD988A25F906BC4547983422DF30ACB64DE416D1144EEAEC08AD8DCDBA5E7A21956CECE6549CBBFA5B1D8E80994EAAEA266C4FEEBD7A613BDA1636487FA4F3FED102BF79779B8B6056F02D76F79BE96F64B555B578A0D8816535C2F6915D88ADE601FDD5D03862CB8240C4DA4A66AAE510B995EB5C76D6B62867FBB27D1BBEFE4A5BA77D0357035873F189B2FFE718F3BD164ACCCB5355D316C1A80B8BFF324AAB148854D4E1F40DF224DF7DFD7ADA056D343A0B81F9E6DCA6AD9C92924BD36C6C2D7688C94EC8DB45F3B3E38BFCA1B7475E29BF9442DCDEC154AD816AFEC1F7EE23A9228E40DB20248951FE2CE6E4AF9C546C7C108A862B58C99BD70AE72D73A8BCA00CC8A4501DC7FF5EEA7533BC065454FDFB270242DE7981B050DAFBE682A9F498F445AB6EA1ED20B1AF5BFBEE646214DF2CC214E6D3B6E59925600CDED856A49C0F5899669107A5D957E675BC94A53C32E476CE64B0DB77DDB4320179B5D36682F017FE9D8FA5D18A9C9955210F60AA4A18737AECAF9CA2DC9D938A1F73ECF274276FE3F8B9BC4879B75617F2E7AD8485FB71C7B10B2F0DCE6F2F0CE3FF1303145B7D11EE281B6A18A6F02B8E787A4956046BBE5AC5E47BF695712BAE9FC32A8881CB53D8881973B6C70AA648289A5DDB98EFF5C14A061#)\n  )\n )\n'
    )
    assert public.verify(sig, data) == True
    with pytest.raises(NotImplementedError):
        private.verify(sig, data)
Beispiel #11
0
def test_contains(context):
    s_expr = SExpression(b'(test (a "123") (b "-123") (longitem "Ohai world"))')
    assert ('test' in s_expr) == True
    assert ('yadayada' in s_expr) == False
Beispiel #12
0
def test_carcdr(context):
    s_expr = SExpression(b'(tests (test1 "123") (test2 "456"))')
    assert str(s_expr.car) == "SExpression(b'(tests)\\n')"
    assert str(s_expr.cdr) == "SExpression(b'(\\n (test1 \"123\")\\n (test2 \"456\")\\n )\\n')"
    assert str(s_expr.cdr.car.car) == "SExpression(b'(test1)\\n')"
Beispiel #13
0
def test_length(context):
    s_expr = SExpression(b'(test (a "123")(b "-123"))')
    assert len(s_expr) == 3
Beispiel #14
0
def test_keys(context):
    s_expr = SExpression(b'(test (flags) (a "123") (b "-123") (longitem "Ohai world"))')
    assert s_expr.keys() == [b'test', b'flags', b'a', b'b', b'longitem'] 
    assert s_expr['test'].keys() == [b'flags', b'a', b'b', b'longitem']
    s_expr = SExpression(b'(a "123")')
    assert s_expr.keys() == [b'a']
Beispiel #15
0
def test_delitem(context):
    s_expr = SExpression(b'(test (a "123")(b "-123"))')
    del(s_expr['a'])
    assert repr(s_expr) == "SExpression(b'(test \\n (b -123)\\n )\\n')"
Beispiel #16
0
def test_setitem(context):
    s_expr = SExpression(b'(test (a "123") (b "-123") (longitem "Ohai world"))')
    s_expr['jumbo'] = 'Jumbo humbo jumbo!'
    assert repr(s_expr['jumbo']) == "SExpression(b'(\"Jumbo humbo jumbo!\")\\n')"
    with pytest.raises(Exception):
        s_expr['jumbo'] = 'Jumbo humbo jumbo-to!'
Beispiel #17
0
def test_iterate(context):
    s_expr = SExpression(b'(test (inside (a "123") (b "-123")) (longitem "Ohai world"))')
    iter_expr = [sexp for sexp in s_expr]
    assert iter_expr == [SExpression(b'(test)'), SExpression(b'(inside (a "123") (b "-123"))'), SExpression(b'(longitem "Ohai world")')]