home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1997 December / Internet_Info_CD-ROM_Walnut_Creek_December_1997.iso / rfc / rfc2202 < prev    next >
Text File  |  1997-09-23  |  12KB  |  508 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Network Working Group                                          P. Cheng
  8. Request for Comments: 2202                                          IBM
  9. Category: Informational                                        R. Glenn
  10.                                                                    NIST
  11.                                                          September 1997
  12.  
  13.  
  14.                  Test Cases for HMAC-MD5 and HMAC-SHA-1
  15.  
  16. Status of This Memo
  17.  
  18.    This memo provides information for the Internet community.  This memo
  19.    does not specify an Internet standard of any kind.  Distribution of
  20.    this memo is unlimited.
  21.  
  22. Abstract
  23.  
  24.    This document provides two sets of test cases for HMAC-MD5 and HMAC-
  25.    SHA-1, respectively. HMAC-MD5 and HMAC-SHA-1 are two constructs of
  26.    the HMAC [HMAC] message authentication function using the MD5 [MD5]
  27.    hash function and the SHA-1 [SHA] hash function. Both constructs are
  28.    used by IPSEC [OG,CG] and other protocols to authenticate messages.
  29.    The test cases and results provided in this document are meant to be
  30.    used as a conformance test for HMAC-MD5 and HMAC-SHA-1
  31.    implementations.
  32.  
  33. 1. Introduction
  34.  
  35.    The general method for constructing a HMAC message authentication
  36.    function using a particular hash function is described in section 2
  37.    of [HMAC]. We will not repeat the description here. Section 5 of
  38.    [HMAC] also discusses truncating the output of HMAC; the rule is that
  39.    we should keep the more significant bits (the bits in the left,
  40.    assuming a network byte order (big-endian)).
  41.  
  42.    In sections 2 and 3 we provide test cases for HMAC-MD5 and HMAC-SHA-
  43.    1, respectively. Each case includes the key, the data, and the
  44.    result.  The values of keys and data are either hexadecimal numbers
  45.    (prefixed by "0x") or ASCII character strings in double quotes. If a
  46.    value is an ASCII character string, then the HMAC computation for the
  47.    corresponding test case DOES NOT include the trailing null character
  48.    ('\0') in the string.
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. Cheng & Glenn                Informational                      [Page 1]
  59.  
  60. RFC 2202         Test Cases for HMAC-MD5 and HMAC-SHA-1   September 1997
  61.  
  62.  
  63.    The C source code of the functions used to generate HMAC-SHA-1
  64.    results is listed in the Appendix. Note that these functions are
  65.    meant to be simple and easy to understand; they are not optimized in
  66.    any way. The C source code for computing HMAC-MD5 can be found in
  67.    [MD5]; or you can do a simple modification to HMAC-SHA-1 code to get
  68.    HMAC-MD5 code, as explained in the Appendix.
  69.  
  70.    The test cases in this document are cross-verified by three
  71.    independent implementations, one from NIST and two from IBM Research.
  72.    One IBM implementation uses optimized code that is very different
  73.    from the code in the Appendix. An implemenation that concurs with the
  74.    results provided in this document should be interoperable with other
  75.    similar implemenations.  We do not claim that such an implementation
  76.    is absolutely correct with respect to the HMAC definition in [HMAC].
  77.  
  78. 2. Test Cases for HMAC-MD5
  79.  
  80. test_case =     1
  81. key =           0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
  82. key_len =       16
  83. data =          "Hi There"
  84. data_len =      8
  85. digest =        0x9294727a3638bb1c13f48ef8158bfc9d
  86.  
  87. test_case =     2
  88. key =           "Jefe"
  89. key_len =       4
  90. data =          "what do ya want for nothing?"
  91. data_len =      28
  92. digest =        0x750c783e6ab0b503eaa86e310a5db738
  93.  
  94. test_case =     3
  95. key =           0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  96. key_len         16
  97. data =          0xdd repeated 50 times
  98. data_len =      50
  99. digest =        0x56be34521d144c88dbb8c733f0e8b3f6
  100.  
  101. test_case =     4
  102. key =           0x0102030405060708090a0b0c0d0e0f10111213141516171819
  103. key_len         25
  104. data =          0xcd repeated 50 times
  105. data_len =      50
  106. digest =        0x697eaf0aca3a3aea3a75164746ffaa79
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. Cheng & Glenn                Informational                      [Page 2]
  115.  
  116. RFC 2202         Test Cases for HMAC-MD5 and HMAC-SHA-1   September 1997
  117.  
  118.  
  119. test_case =     5
  120. key =           0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
  121. key_len =       16
  122. data =          "Test With Truncation"
  123. data_len =      20
  124. digest =        0x56461ef2342edc00f9bab995690efd4c
  125. digest-96       0x56461ef2342edc00f9bab995
  126.  
  127. test_case =     6
  128. key =           0xaa repeated 80 times
  129. key_len =       80
  130. data =          "Test Using Larger Than Block-Size Key - Hash Key First"
  131. data_len =      54
  132. digest =        0x6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd
  133.  
  134. test_case =     7
  135. key =           0xaa repeated 80 times
  136. key_len =       80
  137. data =          "Test Using Larger Than Block-Size Key and Larger
  138.                 Than One Block-Size Data"
  139. data_len =      73
  140. digest =        0x6f630fad67cda0ee1fb1f562db3aa53e
  141.  
  142. 3. Test Cases for HMAC-SHA-1
  143.  
  144. test_case =     1
  145. key =           0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
  146. key_len =       20
  147. data =          "Hi There"
  148. data_len =      8
  149. digest =        0xb617318655057264e28bc0b6fb378c8ef146be00
  150.  
  151. test_case =     2
  152. key =           "Jefe"
  153. key_len =       4
  154. data =          "what do ya want for nothing?"
  155. data_len =      28
  156. digest =        0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79
  157.  
  158. test_case =     3
  159. key =           0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  160. key_len =       20
  161. data =          0xdd repeated 50 times
  162. data_len =      50
  163. digest =        0x125d7342b9ac11cd91a39af48aa17b4f63f175d3
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170. Cheng & Glenn                Informational                      [Page 3]
  171.  
  172. RFC 2202         Test Cases for HMAC-MD5 and HMAC-SHA-1   September 1997
  173.  
  174.  
  175. test_case =     4
  176. key =           0x0102030405060708090a0b0c0d0e0f10111213141516171819
  177. key_len =       25
  178. data =          0xcd repeated 50 times
  179. data_len =      50
  180. digest =        0x4c9007f4026250c6bc8414f9bf50c86c2d7235dane 7
  181. test_case =     5
  182. key =           0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
  183. key_len =       20
  184. data =          "Test With Truncation"
  185. data_len =      20
  186. digest =        0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04
  187. digest-96 =     0x4c1a03424b55e07fe7f27be1
  188.  
  189. test_case =     6
  190. key =           0xaa repeated 80 times
  191. key_len =       80
  192. data =          "Test Using Larger Than Block-Size Key - Hash Key First"
  193. data_len =      54
  194. digest =        0xaa4ae5e15272d00e95705637ce8a3b55ed402112
  195.  
  196. test_case =     7
  197. key =           0xaa repeated 80 times
  198. key_len =       80
  199. data =          "Test Using Larger Than Block-Size Key and Larger
  200.                 Than One Block-Size Data"
  201. data_len =      73
  202. digest =        0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91
  203. data_len =      20
  204. digest =        0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04
  205. digest-96 =     0x4c1a03424b55e07fe7f27be1
  206.  
  207. test_case =     6
  208. key =           0xaa repeated 80 times
  209. key_len =       80
  210. data =          "Test Using Larger Than Block-Size Key - Hash Key
  211. First"
  212. data_len =      54
  213. digest =        0xaa4ae5e15272d00e95705637ce8a3b55ed402112
  214.  
  215. test_case =     7
  216. key =           0xaa repeated 80 times
  217. key_len =       80
  218. data =          "Test Using Larger Than Block-Size Key and Larger
  219.                 Than One Block-Size Data"
  220. data_len =      73
  221. digest =        0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91
  222.  
  223.  
  224.  
  225.  
  226. Cheng & Glenn                Informational                      [Page 4]
  227.  
  228. RFC 2202         Test Cases for HMAC-MD5 and HMAC-SHA-1   September 1997
  229.  
  230.  
  231. 4. Security Considerations
  232.  
  233.    This docuemnt raises no security issues. Discussion on the strength
  234.    of the HMAC construction can be found in [HMAC].
  235.  
  236. References
  237.  
  238.    [HMAC]    Krawczyk, H., Bellare, M., and R. Canetti,
  239.              "HMAC: Keyed-Hashing for Message Authentication",
  240.              RFC 2104, February 1997.
  241.  
  242.    [MD5]     Rivest, R., "The MD5 Message-Digest Algorithm",
  243.              RFC 1321, April 1992.
  244.  
  245.    [SHA]     NIST, FIPS PUB 180-1: Secure Hash Standard, April 1995.
  246.  
  247.    [OG]      Oehler, M., and R. Glenn,
  248.              "HMAC-MD5 IP Authentication with Replay Prevention",
  249.              RFC 2085, February 1997.
  250.  
  251.    [CG]      Chang, S., and R. Glenn,
  252.              "HMAC-SHA IP Authentication with Replay Prevention",
  253.              Work in Progress.
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282. Cheng & Glenn                Informational                      [Page 5]
  283.  
  284. RFC 2202         Test Cases for HMAC-MD5 and HMAC-SHA-1   September 1997
  285.  
  286.  
  287. Authors' Addresses
  288.  
  289.    Pau-Chen Cheng
  290.    IBM T.J. Watson Research Center
  291.    P.O.Box 704
  292.    Yorktown Heights, NY 10598
  293.  
  294.    EMail: pau@watson.ibm.com
  295.  
  296.  
  297.    Robert Glenn
  298.    NIST
  299.    Building 820, Room 455
  300.    Gaithersburg, MD 20899
  301.  
  302.    EMail: rob.glenn@nist.gov
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338. Cheng & Glenn                Informational                      [Page 6]
  339.  
  340. RFC 2202         Test Cases for HMAC-MD5 and HMAC-SHA-1   September 1997
  341.  
  342.  
  343. Appendix
  344.  
  345.    This appendix contains the C reference code which implements HMAC-
  346.    SHA-1 using an existing SHA-1 library.  It assumes that the SHA-1
  347.    library has similar API's as those of the MD5 code described in RFC
  348.    1321.  The code for HMAC-MD5 is similar, just replace the strings
  349.    "SHA" and "sha" with "MD5" and "md5". HMAC-MD5 code is also listed in
  350.    RFC 2104.
  351.  
  352.    #ifndef SHA_DIGESTSIZE
  353.    #define SHA_DIGESTSIZE  20
  354.    #endif
  355.  
  356.    #ifndef SHA_BLOCKSIZE
  357.    #define SHA_BLOCKSIZE   64
  358.    #endif
  359.  
  360.    #ifndef MD5_DIGESTSIZE
  361.    #define MD5_DIGESTSIZE  16
  362.    #endif
  363.  
  364.    #ifndef MD5_BLOCKSIZE
  365.    #define MD5_BLOCKSIZE   64
  366.    #endif
  367.  
  368.    /* Function to print the digest */
  369.    void
  370.    pr_sha(FILE* fp, char* s, int t)
  371.    {
  372.            int     i ;
  373.  
  374.            fprintf(fp, "0x") ;
  375.            for (i = 0 ; i < t ; i++)
  376.                    fprintf(fp, "%02x", s[i]) ;
  377.            fprintf(fp, "0) ;
  378.    }
  379.  
  380.    void truncate
  381.    (
  382.     char*   d1,   /* data to be truncated */
  383.     char*   d2,   /* truncated data */
  384.     int     len   /* length in bytes to keep */
  385.    )
  386.    {
  387.            int     i ;
  388.            for (i = 0 ; i < len ; i++) d2[i] = d1[i];
  389.    }
  390.  
  391.  
  392.  
  393.  
  394. Cheng & Glenn                Informational                      [Page 7]
  395.  
  396. RFC 2202         Test Cases for HMAC-MD5 and HMAC-SHA-1   September 1997
  397.  
  398.  
  399.    /* Function to compute the digest */
  400.    void
  401.    hmac_sha
  402.    (
  403.     char*    k,     /* secret key */
  404.     int      lk,    /* length of the key in bytes */
  405.     char*    d,     /* data */
  406.     int      ld,    /* length of data in bytes */
  407.     char*    out,   /* output buffer, at least "t" bytes */
  408.     int      t
  409.    )
  410.    {
  411.            SHA_CTX ictx, octx ;
  412.            char    isha[SHA_DIGESTSIZE], osha[SHA_DIGESTSIZE] ;
  413.            char    key[SHA_DIGESTSIZE] ;
  414.            char    buf[SHA_BLOCKSIZE] ;
  415.            int     i ;
  416.  
  417.            if (lk > SHA_BLOCKSIZE) {
  418.  
  419.                    SHA_CTX         tctx ;
  420.  
  421.                    SHAInit(&tctx) ;
  422.                    SHAUpdate(&tctx, k, lk) ;
  423.                    SHAFinal(key, &tctx) ;
  424.  
  425.                    k = key ;
  426.                    lk = SHA_DIGESTSIZE ;
  427.            }
  428.  
  429.            /**** Inner Digest ****/
  430.  
  431.            SHAInit(&ictx) ;
  432.  
  433.            /* Pad the key for inner digest */
  434.            for (i = 0 ; i < lk ; ++i) buf[i] = k[i] ^ 0x36 ;
  435.            for (i = lk ; i < SHA_BLOCKSIZE ; ++i) buf[i] = 0x36 ;
  436.  
  437.            SHAUpdate(&ictx, buf, SHA_BLOCKSIZE) ;
  438.            SHAUpdate(&ictx, d, ld) ;
  439.  
  440.            SHAFinal(isha, &ictx) ;
  441.  
  442.            /**** Outter Digest ****/
  443.  
  444.            SHAInit(&octx) ;
  445.  
  446.            /* Pad the key for outter digest */
  447.  
  448.  
  449.  
  450. Cheng & Glenn                Informational                      [Page 8]
  451.  
  452. RFC 2202         Test Cases for HMAC-MD5 and HMAC-SHA-1   September 1997
  453.  
  454.  
  455.            for (i = 0 ; i < lk ; ++i) buf[i] = k[i] ^ 0x5C ;
  456.            for (i = lk ; i < SHA_BLOCKSIZE ; ++i) buf[i] = 0x5C ;
  457.  
  458.            SHAUpdate(&octx, buf, SHA_BLOCKSIZE) ;
  459.            SHAUpdate(&octx, isha, SHA_DIGESTSIZE) ;
  460.  
  461.            SHAFinal(osha, &octx) ;
  462.  
  463.            /* truncate and print the results */
  464.            t = t > SHA_DIGESTSIZE ? SHA_DIGESTSIZE : t ;
  465.            truncate(osha, out, t) ;
  466.            pr_sha(stdout, out, t) ;
  467.  
  468.    }
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506. Cheng & Glenn                Informational                      [Page 9]
  507.  
  508.