"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.4.1/libcanlock/src/hmac_sha1.c" (9 Feb 2016, 3826 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 "hmac_sha1.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.3.2_vs_2.3.3.

    1 /*
    2  * COPYRIGHT AND PERMISSION NOTICE
    3  * 
    4  * Copyright (c) 2003 G.J. Andruk
    5  * 
    6  * All rights reserved.
    7  * 
    8  * Permission is hereby granted, free of charge, to any person obtaining
    9  * a copy of this software and associated documentation files (the
   10  * "Software"), to deal in the Software without restriction, including
   11  * without limitation the rights to use, copy, modify, merge, publish,
   12  * distribute, and/or sell copies of the Software, and to permit persons
   13  * to whom the Software is furnished to do so, provided that the above
   14  * copyright notice(s) and this permission notice appear in all copies of
   15  * the Software and that both the above copyright notice(s) and this
   16  * permission notice appear in supporting documentation.
   17  * 
   18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
   21  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
   22  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY
   23  * SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
   24  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
   25  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
   26  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   27  * 
   28  * Except as contained in this notice, the name of a copyright holder
   29  * shall not be used in advertising or otherwise to promote the sale, use
   30  * or other dealings in this Software without prior written authorization
   31  * of the copyright holder.
   32  */
   33 
   34 #include <stdio.h>
   35 #include <stdlib.h>
   36 #include <string.h>
   37 #include "sha1.h"
   38 #include "hmac_sha1.h"
   39 
   40 #define ipad 0x36
   41 #define opad 0x5c
   42 
   43 /* 
   44  * Encode a string using HMAC - see RFC-2104 for details.
   45  * Returns the MAC, or NULL on error.
   46  * Caller needs to free() non-NULL return values..
   47  */
   48 unsigned char *
   49 hmac_sha1(
   50             const unsigned char *K,   /* key */
   51             int Klen,           /* and it size */
   52             const unsigned char *T,   /* text to encode */
   53             int Tlen            /* and its size */
   54         )
   55 {
   56     SHA_CTX
   57         hash_ctx;
   58     unsigned char
   59         keyin[SHA_DATASIZE],
   60         *step2,
   61         step4[SHA_DIGESTSIZE],
   62         step5[SHA_DATASIZE + SHA_DIGESTSIZE],
   63         *hmac_out,
   64         *c;
   65     int
   66         i,
   67         j;
   68 
   69     if (sha_init(&hash_ctx))
   70         return NULL;
   71 
   72     /* If the key is bigger than SHA_DATASIZE we need to hash it. */
   73     if (Klen > SHA_DATASIZE) {
   74         if (sha_update(&hash_ctx, K, Klen))
   75             return NULL;
   76         if (sha_digest(&hash_ctx, keyin))
   77             return NULL;
   78         Klen = SHA_DIGESTSIZE;
   79     }
   80     else
   81         memcpy(keyin, K, Klen);
   82 
   83     if ((step2 = (unsigned char *) malloc(Tlen + SHA_DATASIZE)) == NULL)
   84         return NULL;
   85 
   86     c = keyin;
   87     for (i = 0; i < Klen; i++) {
   88         step2[i] = *c ^ ipad;
   89         step5[i] = *c ^ opad;
   90         c++;
   91     }
   92     for (j = i; j < SHA_DATASIZE; j++) {
   93         step2[j] = ipad;
   94         step5[j] = opad;
   95     }
   96     
   97     memcpy(&step2[SHA_DATASIZE], T, Tlen);
   98 
   99     if (sha_init(&hash_ctx)) {
  100         free(step2);
  101         return NULL;
  102     }
  103     if (sha_update(&hash_ctx, step2, SHA_DATASIZE + Tlen)) {
  104         free(step2);
  105         return NULL;
  106     }
  107     free(step2);
  108     if (sha_digest(&hash_ctx, step4))
  109         return NULL;
  110 
  111     memcpy(&step5[SHA_DATASIZE], step4, SHA_DIGESTSIZE);
  112 
  113     hmac_out = (unsigned char *) malloc(SHA_DIGESTSIZE);
  114     if (!hmac_out)
  115         return NULL;
  116 
  117     if (sha_init(&hash_ctx)) {
  118         free(hmac_out);
  119         return NULL;
  120     }
  121     if (sha_update(&hash_ctx, step5, SHA_DATASIZE + SHA_DIGESTSIZE)) {
  122         free(hmac_out);
  123         return NULL;
  124     }
  125     if (sha_digest(&hash_ctx, hmac_out)) {
  126         free(hmac_out);
  127         return NULL;
  128     }
  129 
  130     return hmac_out;
  131 }