A new version of CryptoCoreTests was added to project The Inbox:
http://source.squeak.org/inbox/CryptoCoreTests-rww.1.mcz ==================== Summary ==================== Name: CryptoCoreTests-rww.1 Author: rww Time: 28 August 2010, 6:55:03.055 am UUID: 22ab3906-cbe9-574d-89e2-c0704d987e81 Ancestors: initial submission ==================== Snapshot ==================== SystemOrganization addCategory: #CryptoCoreTests! TestCase subclass: #CryptoDESTest instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'CryptoCoreTests'! ----- Method: CryptoDESTest>>testDES (in category 'as yet unclassified') ----- testDES | plain key d | plain := #(16r01 16r23 16r45 16r67 16r89 16rAB 16rCD 16rE7) asByteArray. key := #(16r01 16r23 16r45 16r67 16r89 16rAB 16rCD 16rEF) asByteArray. d := DES key: key. d encryptBlock: plain. self assert: plain = #(16rC9 16r57 16r44 16r25 16r6A 16r5E 16rD3 16r1D) asByteArray! ----- Method: CryptoDESTest>>testDES2 (in category 'as yet unclassified') ----- testDES2 | plain key d | plain := 'squeaker' asByteArray. key := 'hacking!!' asByteArray. d := DES key: key. d encryptBlock: plain. self assert: plain = #[64 222 70 13 59 145 76 186]! ----- Method: CryptoDESTest>>testDES3 (in category 'as yet unclassified') ----- testDES3 | plain plainCopy key d | plain := 'squeaker'. key := 'hacking!!'. plainCopy := plain copy. d := DES key: key. d encryptBlock: plainCopy. self assert: plainCopy asByteArray = #[64 222 70 13 59 145 76 186]. ! ----- Method: CryptoDESTest>>testDESCBC (in category 'as yet unclassified') ----- testDESCBC | plain key encryptor decryptor vector | plain := #(16r01 16r23 16r45 16r67 16r89 16rAB 16rCD 16rE7) asByteArray. key := #(16r01 16r23 16r45 16r67 16r89 16rAB 16rCD 16rEF) asByteArray. vector := #(16r00 16r11 16r22 16r33 16r44 16r55 16r66 16r77) asByteArray. encryptor := (DES key: key) cbc. encryptor initialVector: vector copy. decryptor := (DES key: key) cbc. decryptor initialVector: vector copy. encryptor encryptBlock: plain. decryptor decryptBlock: plain. encryptor encryptBlock: plain. decryptor decryptBlock: plain. self assert: plain = #(16r01 16r23 16r45 16r67 16r89 16rAB 16rCD 16rE7) asByteArray.! ----- Method: CryptoDESTest>>testDESFormal (in category 'as yet unclassified') ----- testDESFormal | x d | x := #(16r94 16r74 16rB8 16rE8 16rC7 16r3B 16rCA 16r7D) asByteArray. 1 to: 16 do: [:i | d := DES key: x. (i - 1) even ifTrue: [d encryptBlock: x] ifFalse: [d decryptBlock: x] ]. self assert: x = #(16r1B 16r1A 16r2D 16rDB 16r4C 16r64 16r24 16r38) asByteArray! ----- Method: CryptoDESTest>>testTripleDES (in category 'as yet unclassified') ----- testTripleDES | plain key d | plain := #(16r01 16r23 16r45 16r67 16r89 16rAB 16rCD 16rE7) asByteArray. key := #(16r01 16r23 16r45 16r67 16r89 16rAB 16rCD 16rEF 16r01 16r23 16r45 16r67 16r89 16rAB 16rCD 16rEF 16r01 16r23 16r45 16r67 16r89 16rAB 16rCD 16rEF) asByteArray. d := TripleDES key: key. d encryptBlock: plain. self assert: plain = #(16rC9 16r57 16r44 16r25 16r6A 16r5E 16rD3 16r1D) asByteArray. d decryptBlock: plain. self assert: plain = #(16r01 16r23 16r45 16r67 16r89 16rAB 16rCD 16rE7) asByteArray. ! ----- Method: CryptoDESTest>>testTripleDESCBC (in category 'as yet unclassified') ----- testTripleDESCBC | plain key encryptor decryptor vector | plain := #(16r01 16r23 16r45 16r67 16r89 16rAB 16rCD 16rE7) asByteArray. key := #(16r01 16r23 16r45 16r67 16r89 16rAB 16rCD 16rEF 16r01 16r23 16r45 16r67 16r89 16rAB 16rCD 16rEF 16r01 16r23 16r45 16r67 16r89 16rAB 16rCD 16rEF) asByteArray. vector := #(16r00 16r11 16r22 16r33 16r44 16r55 16r66 16r77) asByteArray. encryptor := (TripleDES key: key) cbc. encryptor initialVector: vector copy. decryptor := (TripleDES key: key) cbc. decryptor initialVector: vector copy. encryptor encryptBlock: plain. decryptor decryptBlock: plain. encryptor encryptBlock: plain. decryptor decryptBlock: plain. self assert: plain = #(16r01 16r23 16r45 16r67 16r89 16rAB 16rCD 16rE7) asByteArray.! TestCase subclass: #CryptoDSATest instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'CryptoCoreTests'! ----- Method: CryptoDSATest>>dsaPrivateKey (in category 'DSA') ----- dsaPrivateKey ^ DSAPrivateKey p: 8343811888543852523216773185009428259187948644369498021763210776677854991854533186365944349987509452133156416880596803846631577352387751880552969116768071 q: 1197175832754339660404549606408619548226315875117 g: 1433467472198821951822151391684734233265646022897503720591270330985699984763922266163182803556189497900262038518780931942996381297743579119123094520048965 x: 957348690772296812! ----- Method: CryptoDSATest>>dsaPublicKey (in category 'as yet unclassified') ----- dsaPublicKey ^ DSAPublicKey p: 8343811888543852523216773185009428259187948644369498021763210776677854991854533186365944349987509452133156416880596803846631577352387751880552969116768071 q: 1197175832754339660404549606408619548226315875117 g: 1433467472198821951822151391684734233265646022897503720591270330985699984763922266163182803556189497900262038518780931942996381297743579119123094520048965 y: 4645213122572190617807944614677917601101008235397095646475699959851618402406173485853587185431290863173614335452934961425661774118334228449202337038283799! ----- Method: CryptoDSATest>>testDSASigningAndVerifying (in category 'DSA') ----- testDSASigningAndVerifying | msg sig | msg := 'This is a test...'. sig := self dsaPrivateKey signMessage: msg. self assert: (self dsaPublicKey verify: sig isSignatureOf: msg)! TestCase subclass: #CryptoDiffieHellmanTest instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'CryptoCoreTests'! ----- Method: CryptoDiffieHellmanTest>>generator (in category 'testing') ----- generator ^ 19717436579274206947345122013064021491006648667525860934655329476450089156965164852462918009904002231332897658527803! ----- Method: CryptoDiffieHellmanTest>>prime (in category 'testing') ----- prime ^ 75736283208334870880874184828066736731062763491072315486250947429607562308928878772529680132938244784039544211105987! ----- Method: CryptoDiffieHellmanTest>>testDiffieHellman (in category 'testing') ----- testDiffieHellman | alice bob fromAlice fromBob k1 k2 | alice := DiffieHellman prime: self prime generator: self generator. bob := DiffieHellman prime: self prime generator: self generator. fromAlice := alice sendMessage. fromBob := bob sendMessage. k1 := alice receiveMessage: fromBob. k2 := bob receiveMessage: fromAlice. self assert: k1 = k2! ----- Method: CryptoDiffieHellmanTest>>testDiffieHellmanWithGeneration (in category 'testing') ----- testDiffieHellmanWithGeneration | alice bob fromAlice fromBob k1 k2 | alice := DiffieHellman bits: 64. "very low for speed" bob := DiffieHellman prime: alice prime generator: alice generator. fromAlice := alice sendMessage. fromBob := bob sendMessage. k1 := alice receiveMessage: fromBob. k2 := bob receiveMessage: fromAlice. self assert: k1 = k2! TestCase subclass: #CryptoElGamalTest instanceVariableNames: 'elGamalGenerators' classVariableNames: '' poolDictionaries: '' category: 'CryptoCoreTests'! ----- Method: CryptoElGamalTest>>elGamalGeneratorOfSize: (in category 'ElGamal') ----- elGamalGeneratorOfSize: bits ^ elGamalGenerators at: bits ifAbsent: [ "We should at least run the generator code to be sure it gets tested." ElGamalKeyGenerator new generateKeysOfSize: bits ]! ----- Method: CryptoElGamalTest>>elGamalSizes (in category 'ElGamal') ----- elGamalSizes " ^#(384 512 768 1024 2048 )" ^#(64 512 )! ----- Method: CryptoElGamalTest>>initialize (in category 'initialize') ----- initialize super initialize. self initializeElGamalGenerators ! ----- Method: CryptoElGamalTest>>initializeElGamalGenerators (in category 'ElGamal') ----- initializeElGamalGenerators elGamalGenerators := Dictionary new at: 512 put: (ElGamalKeyGenerator basicNew instVarAt: 1 put: 14997038101000471656908825146898513563002169840799091820711874590927961409550512572428509314042888054869079913003134043613271458211821248434060678863943999; instVarAt: 2 put: 5361564564837954391893723942228287597694468337177799740324910691768071826418893848377283607598491893249419038971685698869752393654611799189607985606751717; instVarAt: 3 put: 14403853607602337679914835868070068312873008366859520925460200717630345023960768832597791608212285651911616689566045479334218776261375380546818169284633222; yourself) ; at: 768 put: "time to generate (1.3GHz laptop): 0:00:15:35.71" (ElGamalKeyGenerator basicNew instVarAt: 1 put: 1627332758910669005349673899493430221060426291376421198672767104943990188121953540024154874632086014644358143872462042844845205724969954316173299451580831206047385225398963232807212824639418997262644394808208010952184597290928279083; instVarAt: 2 put: 637353925196181662716534281309417485695156757195851477182610451877569627941960958993724216664932866527382869766700707821847752783200389297904293272300703210689296849853090260886879923123065483201535107015232963789463552975559327338; instVarAt: 3 put: 1382119909913548250681949383374805537713040446907845318586396338232006123603639447180435936299472529495807027144078416474159199852128556879205262745414383327498575491661863562367989514634268303068691788866483656964224388150042511145; yourself) ; at: 1024 put: "time to generate (1.3GHz laptop): 0:00:47:36.288" (ElGamalKeyGenerator basicNew instVarAt: 1 put: 187962930112203130099304875066550992472989894755401650849423145019107775415120153753379890896898159468888909642301301426861539004261391697652587719733773666895578476166092219394431668865654035130702071975227577489091637769871178365952593486414574736105032572563196302943313478263258078003344616094907879494183; instVarAt: 2 put: 101379856798288932395208478694565744867478878485170411883016695448216014229040233878325777809530026322981840402000631493588538174303052403820192798556612765069387657111145929959798776355339656977979943366339321986473695320247202765836954753923663960196445984306386126700715069527679925061366367143594939752311; instVarAt: 3 put: 166515262641331249052409678632107998360295620903954709872386461965666190307310785117636796778342643935688990419372071274434257100649608137040102448736492114478048476296404763678373473030171410691968491068014395855534564710476510365843729427792397083999439079005119559363379365315966400331819925035641027782262; yourself) ; at: 2048 put: "time to generate (1.3GHz laptop): 0:10:12:51.318" (ElGamalKeyGenerator basicNew instVarAt: 1 put: 43744550524640778592149701468321500307659483711161614933780759317538835660018499584863359875319736195497834842011206606111677937058576081833326123611960958651716618573704725631301784031019401088209827585193009767033768284401492225440611202384860046570022594166194086374043414523106999827113761864480276549701342546190042502746170762523030838415181639559467384246646980630730354186842888000169398408845020528778902676590121957276525059882643422002341827009727652951788957728483337275699584610825200769579177116050586037396083453876647282696542617919089209621847548931160771918736412809101535195712578982414848324842023; instVarAt: 2 put: 10712249143584610517062034706393748750841448112842264168635103160726320592857516094777199394038060786699730059487045452474549294208077531478734497996197653738535125856484035290422114479816131356589211862230201414545738081276869984650096988089393906878268413233796553170847827164856623679160971724585707100834284358979484189497136931922824212488771387863590257584963981297527371077585758027526026146064280994391353033474013279212299950762699784082492767463217396754190025401868735342133341990021934718202416382949744384380039458404029702056002896172354530543264720995892165487343788849479311283598302664326567957332056; instVarAt: 3 put: 34071313526526550964319859214388665314022163381999037971182526093331137188777643466498971633059086483606132787656147890791281175830824595869636722657222797206435777085613341792023090529456287226683946127489180430863850983140873843765939491483118681168525418491145087283699270267894578719923555878333437301788432335456184779553157758814789690967079053314015566185742547007752259485986859847843898791493225888341829225652240920951193796829090836607954659113174671564171844005862397483897607655449822950579836313328217159525558966188520476621728360816330014688201932196096220643747293750747578885327851920080280020579421; yourself) ; yourself! ----- Method: CryptoElGamalTest>>performElGamalEncryption: (in category 'ElGamal') ----- performElGamalEncryption: bits | elgamal pub priv c element | elgamal := self elGamalGeneratorOfSize: bits. pub := elgamal publicKey. priv := elgamal privateKey. element := (SecureRandom picker nextBits: bits). c := pub encryptElement: element. self assert: (priv decryptElement: c) = element! ----- Method: CryptoElGamalTest>>performElGamalSignature: (in category 'ElGamal') ----- performElGamalSignature: bits | elgamal pub priv signature message | elgamal := self elGamalGeneratorOfSize: bits. pub := elgamal publicKey. priv := elgamal privateKey. message := SecureRandom picker nextBits: bits. signature := priv signMessage: message. self assert: (pub verifySignature: signature onMessage: message)! ----- Method: CryptoElGamalTest>>testElGamalEncryption (in category 'ElGamal') ----- testElGamalEncryption self elGamalSizes do: [:eachBits | self performElGamalEncryption: eachBits]! ----- Method: CryptoElGamalTest>>testElGamalSignature (in category 'ElGamal') ----- testElGamalSignature self elGamalSizes do: [:eachBits | self performElGamalSignature: eachBits]! TestCase subclass: #CryptoHashFunctionTest instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'CryptoCoreTests'! ----- Method: CryptoHashFunctionTest>>testHMAC (in category 'hash algorithms') ----- testHMAC self assert: (MD5 new hmac key: (ByteArray new: 16 withAll: 16r0B); digestMessage: 'Hi There' asByteArray) asInteger = 16r9294727A3638BB1C13F48EF8158BFC9D. self assert: (MD5 new hmac key: 'Jefe' asByteArray; digestMessage: 'what do ya want for nothing?' asByteArray) asInteger = 16r750C783E6AB0B503EAA86E310A5DB738. self assert: (MD5 new hmac key: (ByteArray new: 16 withAll: 16rAA); digestMessage: (ByteArray new: 50 withAll: 16rDD)) asInteger = 16r56BE34521D144C88DBB8C733F0E8B3F6! ----- Method: CryptoHashFunctionTest>>testHMACSHA1 (in category 'hash algorithms') ----- testHMACSHA1 self assert: ((SHA1 new hmac key: (ByteArray new: 16 withAll: 16r0B); digestMessage: 'Hi There' asByteArray) asByteArray) = #(16r67 16r5B 16rB 16r3A 16r1B 16r4D 16rDF 16r4E 16r12 16r48 16r72 16rDA 16r6C 16r2F 16r63 16r2B 16rFE 16rD9 16r57 16rE9) asByteArray! ----- Method: CryptoHashFunctionTest>>testLargeSHA1 (in category 'hash algorithms') ----- testLargeSHA1 "Here is the third example from the specification document (FIPS PUB 180-1). This example may take several minutes." self assert: (SHA1 hashMessage: (String new: 1000000 withAll: $a)) asInteger = 16r34AA973CD4C4DAA4F61EEB2BDBAD27316534016F! ----- Method: CryptoHashFunctionTest>>testMD5 (in category 'hash algorithms') ----- testMD5 self assert: (MD5 hashMessage: '') asInteger = 16rD41D8CD98F00B204E9800998ECF8427E. self assert: (MD5 hashMessage: 'a') asInteger = 16r0CC175B9C0F1B6A831C399E269772661. self assert: (MD5 hashMessage: 'abc') asInteger = 16r900150983CD24FB0D6963F7D28E17F72. self assert: (MD5 hashMessage: 'message digest') asInteger = 16rF96B697D7CB7938D525A2F31AAF161D0. self assert: (MD5 hashMessage: 'abcdefghijklmnopqrstuvwxyz') asInteger = 16rC3FCD3D76192E4007DFB496CCA67E13B. self assert: (MD5 hashMessage: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') asInteger = 16rD174AB98D277D9F5A5611C2C9F419D9F. self assert: (MD5 hashMessage: '12345678901234567890123456789012345678901234567890123456789012345678901234567890') asInteger = 16r57EDF4A22BE3C955AC49DA2E2107B67A! ----- Method: CryptoHashFunctionTest>>testMD5Implementations (in category 'hash algorithms') ----- testMD5Implementations "Check if the two implementations give the same results. Do nothing if the plugin is not available" | data | MD5 isPluginAvailable ifFalse: [ ^self ]. data := ''. 100 timesRepeat: [ | primitive nonPrimitive | primitive := MD5NonPrimitive basicNew hashMessage: data. nonPrimitive := MD5WithPrimitive basicNew hashMessage: data. self assert: primitive = nonPrimitive. data := data, 'Squeak' ]! ----- Method: CryptoHashFunctionTest>>testSHA1 (in category 'hash algorithms') ----- testSHA1 self assert: (SHA1 hashMessage: 'abc') asInteger = 16rA9993E364706816ABA3E25717850C26C9CD0D89D. self assert: (SHA1 hashMessage: 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') asInteger = 16r84983E441C3BD26EBAAE4AA1F95129E5E54670F1 ! ----- Method: CryptoHashFunctionTest>>testSHA256 (in category 'hash algorithms') ----- testSHA256 "these tests come from http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf" self assert: (SHA256 hashMessage: 'abc') asInteger = 16rBA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD. self assert: (SHA256 hashMessage: 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') asInteger = 16r248D6A61D20638B8E5C026930C3E6039A33CE45964FF2167F6ECEDD419DB06C1. SHA256 isPluginAvailable ifTrue: [ "takes a while to run without the plugin" self assert: (SHA256 hashMessage: (String new: 1000000 withAll: $a)) asInteger = 16rCDC76E5C9914FB9281A1C7E284D73E67F1809A48A497200E046D39CCC7112CD0 ]! ----- Method: CryptoHashFunctionTest>>testSHA256Implementations (in category 'hash algorithms') ----- testSHA256Implementations "Check if the two implementations give the same results. Do nothing if the plugin is not available" | data | SHA256 isPluginAvailable ifFalse: [ ^self ]. data := ''. 100 timesRepeat: [ | primitive nonPrimitive | primitive := SHA256NonPrimitive basicNew hashMessage: data. nonPrimitive := SHA256WithPrimitive basicNew hashMessage: data. self assert: primitive = nonPrimitive. data := data, 'Squeak' ]! TestCase subclass: #CryptoRC4Test instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'CryptoCoreTests'! ----- Method: CryptoRC4Test>>genericARC4TestKey:plaintext:ciphertext: (in category 'as yet unclassified') ----- genericARC4TestKey: key plaintext: plaintext ciphertext: ciphertext | vector rc4 | vector := plaintext copy. rc4 := ARC4 new. rc4 key: key. vector := rc4 encrypt: vector. self assert: vector = ciphertext! ----- Method: CryptoRC4Test>>testARC4 (in category 'as yet unclassified') ----- testARC4 self genericARC4TestKey: #(16r01 16r23 16r45 16r67 16r89 16rAB 16rCD 16rEF) plaintext: #(16r01 16r23 16r45 16r67 16r89 16rAB 16rCD 16rEF) ciphertext: #(16r75 16rB7 16r87 16r80 16r99 16rE0 16rC5 16r96). self genericARC4TestKey: #(16r01 16r23 16r45 16r67 16r89 16rAB 16rCD 16rEF) plaintext: #(16r00 16r00 16r00 16r00 16r00 16r00 16r00 16r00) ciphertext: #(16r74 16r94 16rC2 16rE7 16r10 16r4B 16r08 16r79). self genericARC4TestKey: #(16r00 16r00 16r00 16r00 16r00 16r00 16r00 16r00) plaintext: #(16r00 16r00 16r00 16r00 16r00 16r00 16r00 16r00) ciphertext: #(16rDE 16r18 16r89 16r41 16rA3 16r37 16r5D 16r3A). self genericARC4TestKey: #(16rEF 16r01 16r23 16r45) plaintext: #(16r00 16r00 16r00 16r00 16r00 16r00 16r00 16r00 16r00 16r00) ciphertext: #(16rD6 16rA1 16r41 16rA7 16rEC 16r3C 16r38 16rDF 16rBD 16r61)! TestCase subclass: #CryptoRSATest instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'CryptoCoreTests'! ----- Method: CryptoRSATest>>genericRSATest: (in category 'RSA') ----- genericRSATest: bits | generator public private msg c | generator := RSAKeyPairGenerator new bits: bits. public := generator publicKey. "this may take a while" private := generator privateKey. msg := 1234567890987654321. "the plaintext" c := public crypt: msg. "the ciphertext (print it and take a look)" self assert: (private crypt: c) = msg! ----- Method: CryptoRSATest>>testRSA128 (in category 'RSA') ----- testRSA128 self genericRSATest: 128! ----- Method: CryptoRSATest>>testRSA64 (in category 'RSA') ----- testRSA64 self genericRSATest: 64! TestCase subclass: #CryptoRandomTest instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'CryptoCoreTests'! ----- Method: CryptoRandomTest>>testRandomGenerators (in category 'others') ----- testRandomGenerators | random randomBytes | RandomGenerator allSubclassesDo: [ : each | random := each withGeneratedKey. 100 timesRepeat: [ self assert: ((random nextBits: 1) between: 0 and: 1) ]. 100 timesRepeat: [ self assert: (random nextBits: 1024) highBit <= 1024 ]. "Random enough to span all 10 within 100-elements?" self assert: ((1 to: 100) collect: [ : n | random nextInt: 10 ]) asSet asSortedCollection = (1 to: 10) asSortedCollection. randomBytes := random nextBytes: 100. self assert: randomBytes size = 100 ; assert: randomBytes asSet size > 1 "if this fails, I wanna see it!!" ]! TestCase subclass: #CryptoRijndaelCBCTest instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'CryptoCoreTests'! ----- Method: CryptoRijndaelCBCTest>>testByteArrayFromHexString (in category 'tests') ----- testByteArrayFromHexString "this test requres that the cryptography core method fromHexString be loaded the main purpose of the method fromHexString is to ensure leading zeros are not chopped off and that small case letters in hex strings are allowed" self assert: (ByteArray fromHexString: '16r000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f') storeString = '#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31]'. self assert: (ByteArray fromHexString: '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f') storeString = '#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31]'.! ----- Method: CryptoRijndaelCBCTest>>testRFC3602Case1 (in category 'tests') ----- testRFC3602Case1 "this test comes from RFC3602 http://www.faqs.org/rfcs/rfc3602.html Case #1: Encrypting 16 bytes (1 block) using AES-CBC with 128-bit key Key : 0x06a9214036b8a15b512e03d534120006 IV : 0x3dafba429d9eb430b422da802c9fac41 Plaintext : 'Single block msg' Ciphertext: 0xe353779c1079aeb82708942dbe77181a" | result | ((CBC on: (Rijndael new keySize: 16; key: (ByteArray fromHexString: '06A9214036B8A15B512E03D534120006'))) initialVector: (ByteArray fromHexString: '3DAFBA429D9EB430B422DA802C9FAC41')) encryptBlock: (result := 'Single block msg' asByteArray). self assert: result hex = 'E353779C1079AEB82708942DBE77181A'! ----- Method: CryptoRijndaelCBCTest>>testRFC3602Case2 (in category 'tests') ----- testRFC3602Case2 "this test comes from RFC3602 http://www.faqs.org/rfcs/rfc3602.html Case #2: Encrypting 32 bytes (2 blocks) using AES-CBC with 128-bit key Key : 0xc286696d887c9aa0611bbb3e2025a45a IV : 0x562e17996d093d28ddb3ba695a2e6f58 Plaintext : 0x000102030405060708090a0b0c0d0e0f 101112131415161718191a1b1c1d1e1f Ciphertext: 0xd296cd94c2cccf8a3a863028b5e1dc0a 7586602d253cfff91b8266bea6d61ab1" | result | result := ((CBC on: (Rijndael new keySize: 16; key: (ByteArray fromHexString: '16rC286696D887C9AA0611BBB3E2025A45A'))) initialVector: (ByteArray fromHexString: '16r562E17996D093D28DDB3BA695A2E6F58')) encrypt: (result := ByteArray fromHexString: '16r000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F'). self assert: result hex = 'D296CD94C2CCCF8A3A863028B5E1DC0A7586602D253CFFF91B8266BEA6D61AB1BCFD81022202366BDE6DD260A15841A1'! ----- Method: CryptoRijndaelCBCTest>>testRFC3602Case3 (in category 'tests') ----- testRFC3602Case3 "this test comes from RFC3602 http://www.faqs.org/rfcs/rfc3602.html Case #3: Encrypting 48 bytes (3 blocks) using AES-CBC with 128-bit key Key : 0x6c3ea0477630ce21a2ce334aa746c2cd IV : 0xc782dc4c098c66cbd9cd27d825682c81 Plaintext : This is a 48-byte message (exactly 3 AES blocks) Ciphertext: 0xd0a02b3836451753d493665d33f0e886 2dea54cdb293abc7506939276772f8d5 021c19216bad525c8579695d83ba2684" | result | result := ((CBC on: (Rijndael new keySize: 16; key: (ByteArray fromHexString: '6c3ea0477630ce21a2ce334aa746c2cd'))) initialVector: (ByteArray fromHexString: 'c782dc4c098c66cbd9cd27d825682c81')) encrypt: 'This is a 48-byte message (exactly 3 AES blocks)' asByteArray. self assert: result hex = 'D0A02B3836451753D493665D33F0E8862DEA54CDB293ABC7506939276772F8D5021C19216BAD525C8579695D83BA2684D248B3E0F2388C137102846EB06272FF'! ----- Method: CryptoRijndaelCBCTest>>testRFC3602Case4 (in category 'tests') ----- testRFC3602Case4 "this test comes from RFC3602 http://www.faqs.org/rfcs/rfc3602.html Case #4: Encrypting 64 bytes (4 blocks) using AES-CBC with 128-bit key Key : 0x56e47a38c5598974bc46903dba290349 IV : 0x8ce82eefbea0da3c44699ed7db51b7d9 Plaintext : 0xa0a1a2a3a4a5a6a7a8a9aaabacadaeaf b0b1b2b3b4b5b6b7b8b9babbbcbdbebf c0c1c2c3c4c5c6c7c8c9cacbcccdcecf d0d1d2d3d4d5d6d7d8d9dadbdcdddedf Ciphertext: 0xc30e32ffedc0774e6aff6af0869f71aa 0f3af07a9a31a9c684db207eb0ef8e4e 35907aa632c3ffdf868bb7b29d3d46ad 83ce9f9a102ee99d49a53e87f4c3da55 " | result | result := ((CBC on: (Rijndael new keySize: 16; key: (ByteArray fromHexString: '56e47a38c5598974bc46903dba290349'))) initialVector: (ByteArray fromHexString: '8ce82eefbea0da3c44699ed7db51b7d9')) encrypt: (ByteArray fromHexString: 'a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf'). self assert: result hex = 'C30E32FFEDC0774E6AFF6AF0869F71AA0F3AF07A9A31A9C684DB207EB0EF8E4E35907AA632C3FFDF868BB7B29D3D46AD83CE9F9A102EE99D49A53E87F4C3DA5578B8D04731041AA2D9787CA4A4FA3EEF'! TestCase subclass: #CryptoRijndaelTest instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'CryptoCoreTests'! ----- Method: CryptoRijndaelTest>>runBlockCipherModeTest: (in category 'block ciphers') ----- runBlockCipherModeTest: cipherModeSelector | key encipherer decipherer startMsg | key := #(190 219 189 251 27 38 8 9 135 25 167 8 238 107 29 251 136 65 107 0 37 215 45 130 84 87 13 206 8 55 74 82) asByteArray. encipherer := (Rijndael key: key) perform: cipherModeSelector. decipherer := (Rijndael key: key) perform: cipherModeSelector. "Stream ciphers do not require padding, therefore blocks can be less than the encipherer's blockSize." startMsg := encipherer isStreamCipher ifTrue: [ 900 ] ifFalse: [ 1000 ]. startMsg to: 1200 do: [ : n | | block originalBlock iv | block := ('some message', n printString) asByteArray. iv := ByteArray new: Rijndael blockSize. iv uint: Rijndael blockSize*8 at: 1 put: n. encipherer initialVector: iv copy. decipherer initialVector: iv copy. originalBlock := block copy. encipherer encryptBlock: block. self deny: block = originalBlock. self assert: block size = originalBlock size. decipherer decryptBlock: block. self assert: block = originalBlock ]! ----- Method: CryptoRijndaelTest>>runEncryptDecrypt: (in category 'as yet unclassified') ----- runEncryptDecrypt: blockCipherClass | msg key1 key2 enc1 enc2 dec1 dec2 ct2 ct1 | enc1 := blockCipherClass key: (key1 := SecureRandom picker nextBytes: 16). dec1 := blockCipherClass key: key1. enc2 := blockCipherClass key: (key2 := SecureRandom picker nextBytes: 16). dec2 := blockCipherClass key: key2. msg := 'hello how are you?' asByteArray. ct2 := enc2 encrypt: msg. self deny: msg = ct2. self assert: (dec2 decrypt: ct2) = msg. "ensure the cipher still works if its key is changed after creation, both for the encryptor instance and decryptor instance." enc1 key: key2. dec1 key: key2. ct1 := enc1 encrypt: msg. self assert: (dec1 decrypt: ct1) = msg! ----- Method: CryptoRijndaelTest>>testByteArrayPadding (in category 'as yet unclassified') ----- testByteArrayPadding | paddedHello fullBlockPad needsFullPad | paddedHello := #(104 101 108 108 111 11 11 11 11 11 11 11 11 11 11 11 ) asByteArray. self assert: paddedHello isProperlyPadded. self assert: ('hello' asByteArray padToMultipleOf: 16) = paddedHello. self assert: paddedHello unpaddedSize = 'hello' size. fullBlockPad := #(16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16) asByteArray. self assert: fullBlockPad asByteArray isProperlyPadded. needsFullPad := '1234567890123456' asByteArray padToMultipleOf: 16. self assert: needsFullPad size = 32. self assert: (needsFullPad copyFrom: 17 to: 32) = fullBlockPad. self assert: needsFullPad isProperlyPadded. self assert: needsFullPad unpaddedSize = 16! ----- Method: CryptoRijndaelTest>>testCBC (in category 'block ciphers') ----- testCBC self runBlockCipherModeTest: #cbc! ----- Method: CryptoRijndaelTest>>testCFB (in category 'block ciphers') ----- testCFB self runBlockCipherModeTest: #cfb! ----- Method: CryptoRijndaelTest>>testCTR (in category 'block ciphers') ----- testCTR self runBlockCipherModeTest: #ctr! ----- Method: CryptoRijndaelTest>>testEncryptDecrypt (in category 'others') ----- testEncryptDecrypt BlockCipherMode allSubclasses asArray do: [ : each | [ self runEncryptDecrypt: each ] on: Warning do: [ : warn | warn resume ] ]! ----- Method: CryptoRijndaelTest>>testOFB (in category 'block ciphers') ----- testOFB self runBlockCipherModeTest: #ofb! ----- Method: CryptoRijndaelTest>>testRijndael (in category 'block ciphers') ----- testRijndael | rijndael vector | rijndael := Rijndael new keySize: 16; key: (0 to: 15) asByteArray. vector := (0 to: 15) asByteArray. rijndael encryptBlock: vector. self assert: vector hex = '0A940BB5416EF045F1C39458C653EA5A'. rijndael decryptBlock: vector. self assert: vector hex = '000102030405060708090A0B0C0D0E0F'. rijndael := Rijndael new blockSize: 16; keySize: 24. rijndael key: (0 to: 16r17) asByteArray. vector := (0 to: 15) asByteArray. rijndael encryptBlock: vector. self assert: vector hex = '0060BFFE46834BB8DA5CF9A61FF220AE'. rijndael decryptBlock: vector. self assert: vector hex = '000102030405060708090A0B0C0D0E0F'. rijndael := Rijndael new blockSize: 16; keySize: 32. rijndael key: (0 to: 16r1F) asByteArray. vector := (0 to: 15) asByteArray. rijndael encryptBlock: vector. self assert: vector hex = '5A6E045708FB7196F02E553D02C3A692'. rijndael decryptBlock: vector. self assert: vector hex = '000102030405060708090A0B0C0D0E0F'! ----- Method: CryptoRijndaelTest>>testRijndael128128 (in category 'block ciphers') ----- testRijndael128128 | key vector cipher | key := #(16r2B 16r7E 16r15 16r16 16r28 16rAE 16rD2 16rA6 16rAB 16rF7 16r15 16r88 16r09 16rCF 16r4F 16r3C) asByteArray. vector := #(16r32 16r43 16rF6 16rA8 16r88 16r5A 16r30 16r8D 16r31 16r31 16r98 16rA2 16rE0 16r37 16r07 16r34) asByteArray. cipher := Rijndael new keySize: 16; key: key. cipher encryptBlock: vector. self assert: vector hex = '3925841D02DC09FBDC118597196A0B32'. cipher decryptBlock: vector. self assert: vector hex = '3243F6A8885A308D313198A2E0370734'! |
Free forum by Nabble | Edit this page |