local testimony = require("testimony") local litls = require("litls.core") local hex = litls.hex_encode local unhex = litls.hex_decode local testify = testimony.new("== LITLS HMAC ==") -- RFC 4231 test vectors for HMAC-SHA256 testify:that("HMAC-SHA256 test case 1", function() local key = unhex("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b") local data = "Hi There" testimony.assert_equal( "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7", hex(litls.hmac_sha256(key, data)) ) end) testify:that("HMAC-SHA256 test case 2 (Jefe)", function() local key = "Jefe" local data = "what do ya want for nothing?" testimony.assert_equal( "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843", hex(litls.hmac_sha256(key, data)) ) end) testify:that("HMAC-SHA256 test case 3", function() local key = unhex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") local data = unhex("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd") testimony.assert_equal( "ebb08af3c341bf3f70c518e4d514935d7d4d983a12af6bf87174d5b27d3af446", hex(litls.hmac_sha256(key, data)) ) end) testify:that("HMAC-SHA256 test case 4", function() local key = unhex("0102030405060708090a0b0c0d0e0f10111213141516171819") local data = unhex( "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd" ) testimony.assert_equal( "344249be755ca920363e31fb79cbc9876e7c0c9c009804078c8a7809f06dab49", hex(litls.hmac_sha256(key, data)) ) end) testify:that("HMAC-SHA256 test case 6 (long key)", function() -- Key larger than block size (131 bytes) local key = unhex(("aa"):rep(131)) local data = "Test Using Larger Than Block-Size Key - Hash Key First" testimony.assert_equal( "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54", hex(litls.hmac_sha256(key, data)) ) end) testify:that("HMAC-SHA256 test case 7 (long key + long data)", function() local key = unhex(("aa"):rep(131)) local data = "This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm." testimony.assert_equal( "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2", hex(litls.hmac_sha256(key, data)) ) end) -- RFC 4231 HMAC-SHA384 test vectors testify:that("HMAC-SHA384 test case 1", function() local key = unhex("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b") local data = "Hi There" testimony.assert_equal( "afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c7cebc59cfaea9ea9076ede7f4af152e8b2fa9cb6", hex(litls.hmac_sha384(key, data)) ) end) testify:that("HMAC-SHA384 test case 2 (Jefe)", function() local key = "Jefe" local data = "what do ya want for nothing?" testimony.assert_equal( "af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e8e2240ca5e69e2c78b3239ecfab21649", hex(litls.hmac_sha384(key, data)) ) end) testify:conclude()