"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.4.1/libcanlock/t/hmactest.c" (28 Aug 2013, 3135 Bytes) of package /linux/misc/tin-2.4.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "hmactest.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * hmac test program
    3  */
    4 
    5 #include <stdio.h>
    6 #include <string.h>
    7 #include "sha1.h"
    8 #include "hmac_sha1.h"
    9 
   10 int
   11 main(void)
   12 {
   13     unsigned char
   14         *hmachash,
   15         key1[] = "Jefe",
   16         message1[] = "what do ya want for nothing?",
   17         key2[20],
   18         message2[] = "Hi There",
   19         key3[80],
   20         message3[] = "Test Using Larger Than Block-Size Key - Hash Key First",
   21         key4[80],
   22         message4[] = "Test Using Larger Than Block-Size Key and Larger "
   23                      "Than One Block-Size Data",
   24         key5[] = {  0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   25                     0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
   26                     0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
   27                     0x19, 0x00
   28                  },
   29         message5[50];
   30     int
   31         i;
   32 
   33 
   34     for (i = 0; i < 20; i++)
   35         key2[i] = 0x0b;
   36 
   37     for (i = 0; i < 80; i++)
   38         key3[i] = 0xaa;
   39 
   40     for (i = 0; i < 80; i++)
   41         key4[i] = 0xaa;
   42 
   43     for (i = 0; i < 50; i++)
   44         message5[i] = 0xcd;
   45 
   46     printf("Key: %s\n", key1);
   47     printf("Msg: %s\n", message1);
   48     hmachash = hmac_sha1(key1, strlen((char *) key1),
   49                          message1, strlen((char *)message1));
   50     printf("Expected SHA Digest: %s\n",
   51            "0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79");
   52     printf("  Actual SHA Digest: 0x");
   53     for (i = 0; i < SHA_DIGESTSIZE; i++)
   54         printf("%02x", hmachash[i]);
   55     putchar('\n');
   56 
   57 
   58 /********/
   59 
   60     printf("\nKey: 0x0b, len 20 for SHA, 16 for MD5\n");
   61     printf("Msg: %s\n", message2);
   62 
   63 
   64     hmachash = hmac_sha1(key2, 20, message2, strlen((char *) message2));
   65     printf("Expected SHA Digest: %s\n",
   66            "0xb617318655057264e28bc0b6fb378c8ef146be00");
   67     printf("  Actual SHA Digest: 0x");
   68     for (i = 0; i < SHA_DIGESTSIZE; i++)
   69         printf("%02x", hmachash[i]);
   70     putchar('\n');
   71 
   72 /********/
   73 
   74     printf("\nKey: 0xaa repeated 80 times\n");
   75     printf("Msg: %s\n", message3);
   76 
   77     hmachash = hmac_sha1(key3, 80, message3, strlen((char *) message3));
   78     printf("Expected SHA Digest: %s\n",
   79            "0xaa4ae5e15272d00e95705637ce8a3b55ed402112");
   80     printf("  Actual SHA Digest: 0x");
   81     for (i = 0; i < SHA_DIGESTSIZE; i++)
   82         printf("%02x", hmachash[i]);
   83     putchar('\n');
   84 
   85 /********/
   86 
   87     printf("\nKey: 0xaa repeated 80 times\n");
   88     printf("Msg: %s\n", message4);
   89 
   90     hmachash = hmac_sha1(key4, 80, message4, strlen((char *) message4));
   91     printf("Expected SHA Digest: %s\n",
   92            "0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91");
   93     printf("  Actual SHA Digest: 0x");
   94     for (i = 0; i < SHA_DIGESTSIZE; i++)
   95         printf("%02x", hmachash[i]);
   96     putchar('\n');
   97 
   98 /********/
   99 
  100     printf("\nKey: 0x");
  101     for (i = 0; i < 25; i++)
  102         printf("%02x", key5[i]);
  103 
  104     printf("\nMsg: 0xcd repeated 50 times\n");
  105 
  106     hmachash = hmac_sha1(key5, 25, message5, 50);
  107     printf("Expected SHA Digest: %s\n",
  108            "0x4c9007f4026250c6bc8414f9bf50c86c2d7235da");
  109     printf("  Actual SHA Digest: 0x");
  110     for (i = 0; i < SHA_DIGESTSIZE; i++)
  111         printf("%02x", hmachash[i]);
  112     putchar('\n');
  113 
  114     return 0;
  115 }