| 1 |
;; |
| 2 |
;; test for hmac module |
| 3 |
;; |
| 4 |
|
| 5 |
(use gauche.test) |
| 6 |
(test-start "hmac") |
| 7 |
(test-section "hmac") |
| 8 |
|
| 9 |
(use gauche.uvector) |
| 10 |
(use rfc.md5) |
| 11 |
(use rfc.sha) |
| 12 |
(use rfc.base64) |
| 13 |
|
| 14 |
(use rfc.hmac) |
| 15 |
(test-module 'rfc.hmac) |
| 16 |
|
| 17 |
(define hmac (make-hmac <md5> (make-byte-string 16 #x0b))) |
| 18 |
(hmac-update! hmac "Hi ") |
| 19 |
(hmac-update! hmac "There") |
| 20 |
(test* "hmac-final!" "9294727a3638bb1c13f48ef8158bfc9d" |
| 21 |
(hmac-final! hmac 'hex)) |
| 22 |
|
| 23 |
(define *hmac-test-keys* |
| 24 |
'("01234567890123456789" |
| 25 |
"0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij" |
| 26 |
"0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij")) |
| 27 |
(define *hmac-test-message* "Hi, There") |
| 28 |
|
| 29 |
#| |
| 30 |
The following test data can be obtained by the following snippet: |
| 31 |
|
| 32 |
(use gauche.process) |
| 33 |
|
| 34 |
(begin |
| 35 |
(with-output-to-file "tmp.o" (^[] (display *hmac-test-message*))) |
| 36 |
(print "(define *hmac-test-results* `(") |
| 37 |
(dolist [class '(md5 sha1 sha224 sha256 sha384 sha512)] |
| 38 |
(print "(,<" class "> ") |
| 39 |
(dolist [key *hmac-test-keys*] |
| 40 |
(write (regexp-replace |
| 41 |
#/^[^=]*=\s*/ |
| 42 |
(process-output->string |
| 43 |
`(openssl dgst ,#"-~class" -hmac ~key "tmp.o")) |
| 44 |
"")) |
| 45 |
(print)) |
| 46 |
(print ")")) |
| 47 |
(print "))") |
| 48 |
(sys-unlink "tmp.o")) |
| 49 |
|# |
| 50 |
|
| 51 |
(define *hmac-test-results* `( |
| 52 |
(,<md5> |
| 53 |
"32fad29f907cdf38becd0746dbfb5c8c" |
| 54 |
"c93e25f979c2698af603be3cdc3bde61" |
| 55 |
"1ba67a2a0394f3179ea8dc5dfd80c182" |
| 56 |
) |
| 57 |
(,<sha1> |
| 58 |
"7a24d5f70eb816abf08ddbff3839810dab31f899" |
| 59 |
"f80dae80d19cf4557397e4752a676065f03a9c7e" |
| 60 |
"6ef42ff9c975dc8e454ab8204f2f7b72cfda03fd" |
| 61 |
) |
| 62 |
(,<sha224> |
| 63 |
"349641db8a8cebb0792f922324dcbc9ffbd889b782f7b9d4f48fc3e0" |
| 64 |
"df2f9f9085a92393d8559f34127c8d5828e0fa1805a0d28aeddbf3ba" |
| 65 |
"7d32f9d373095e6a62d077d84673e2077e1afa2c88c08181c85b9ee9" |
| 66 |
) |
| 67 |
(,<sha256> |
| 68 |
"c54b248f72bd92455f866cd77b171a3dc74f6b2c1b1f19a51375f723506c1f04" |
| 69 |
"68ff224de29d79ca6bbc273777de97ecf8ee57d1ac9360d365887dc6ab693218" |
| 70 |
"e8da3ca91a29a996bf3bb1b140c5d55a3bda661ac54fae0ceca63b74a4254896" |
| 71 |
) |
| 72 |
(,<sha384> |
| 73 |
"8cf00284dd899f69e9a173dc142e0f53e2c8629a523ae8b16be5415c3f236379cb5bfc512df29caea4b28cf27d8a34e6" |
| 74 |
"3765c9af8409f6fbc00a25560c0ea611bf2b78623a29c2c53f538c402fa2bc3fa0554a8b9ee0246be6dd775688c6531f" |
| 75 |
"e83a53cff39e6274cf3bc271da01f3010e4cbd4ac4caf2c5d0e04072b503f8b3b9f0730027e31fa1f058c6a588273a65" |
| 76 |
) |
| 77 |
(,<sha512> |
| 78 |
"9324878a0be9ca269a6949ce9c21703443afc2fb440c4b5d3d5da2419fffe69dad7e005df799cff29c7506f1e43d77a4fe759a0261ffb675c9e44cdaf79b3659" |
| 79 |
"0f6fad711fedc681106dce5f38be92229d344ce7757ff77a269df5ef100566238cc7b4220ef21c835caf5613e0400a840a7b33315c013158d25b56f4fbecd4e3" |
| 80 |
"ba9327d25e066a5d9d486cc53c054a24da2f1848f45168c20f773e339c8c8b8a66e13e1fe8772ca5007f3857e42c06e996e2e02cdbf6b4f31b69df3d51ec470d" |
| 81 |
) |
| 82 |
)) |
| 83 |
|
| 84 |
(dolist [tests *hmac-test-results*] |
| 85 |
(let ([class (car tests)] |
| 86 |
[hashes (cdr tests)] |
| 87 |
[keys *hmac-test-keys*]) |
| 88 |
(for-each |
| 89 |
(^[hash key] |
| 90 |
(test* #"hmac-message ~(class-name class) keylen=~(string-length key)" |
| 91 |
hash |
| 92 |
(base16-encode-message |
| 93 |
(hmac-message class key *hmac-test-message*) |
| 94 |
:lowercase #t)) |
| 95 |
|
| 96 |
(test* #"hmac-verify ~(class-name class) keylen=~(string-length key)" |
| 97 |
#t |
| 98 |
(hmac-verify class |
| 99 |
(base16-decode-string-to <u8vector> hash) |
| 100 |
key *hmac-test-message*)) |
| 101 |
(test* #"hmac-verify ~(class-name class) keylen=~(string-length key)" |
| 102 |
#f |
| 103 |
(hmac-verify class |
| 104 |
(u8vector-drop |
| 105 |
(base16-decode-string-to <u8vector> hash) |
| 106 |
1) |
| 107 |
key *hmac-test-message*)) |
| 108 |
|
| 109 |
;; deprecated API; just kept to check regression |
| 110 |
(test* #"hmac-digest-string ~(class-name class) keylen=~(string-length key)" |
| 111 |
hash |
| 112 |
(digest-hexify (hmac-digest-string *hmac-test-message* |
| 113 |
:key key |
| 114 |
:hasher class)))) |
| 115 |
hashes |
| 116 |
keys))) |
| 117 |
|
| 118 |
(test-end) |