A simple CyaSSL-based HMAC-MD5 implementation. Licensed under GPL v2.

Dependents:   RFrec_full RFtrans_full

The output will be base64-encoded, with trailing "==", like this:

j62o/jZsAZD9i9m+32lIuQ==

Example

#include "mbed.h"
#include "hmac_md5.h"

Serial pc(USBTX, USBRX); // tx, rx

void main(void)
{
    
    const char * key = "MySecretKey";
    
    const char * text = "message to be signed";
    
    char output[26];
    
    HMAC_MD5(key, text, output);

    printf("result = %s\n", output);

    while(true){}
}
Committer:
igrokhotkov
Date:
Wed Feb 06 20:35:03 2013 +0000
Revision:
0:83f3dcfa5c8f
initial revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
igrokhotkov 0:83f3dcfa5c8f 1 /* hmac.h
igrokhotkov 0:83f3dcfa5c8f 2 *
igrokhotkov 0:83f3dcfa5c8f 3 * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
igrokhotkov 0:83f3dcfa5c8f 4 *
igrokhotkov 0:83f3dcfa5c8f 5 * This file is part of CyaSSL.
igrokhotkov 0:83f3dcfa5c8f 6 *
igrokhotkov 0:83f3dcfa5c8f 7 * CyaSSL is free software; you can redistribute it and/or modify
igrokhotkov 0:83f3dcfa5c8f 8 * it under the terms of the GNU General Public License as published by
igrokhotkov 0:83f3dcfa5c8f 9 * the Free Software Foundation; either version 2 of the License, or
igrokhotkov 0:83f3dcfa5c8f 10 * (at your option) any later version.
igrokhotkov 0:83f3dcfa5c8f 11 *
igrokhotkov 0:83f3dcfa5c8f 12 * CyaSSL is distributed in the hope that it will be useful,
igrokhotkov 0:83f3dcfa5c8f 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
igrokhotkov 0:83f3dcfa5c8f 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
igrokhotkov 0:83f3dcfa5c8f 15 * GNU General Public License for more details.
igrokhotkov 0:83f3dcfa5c8f 16 *
igrokhotkov 0:83f3dcfa5c8f 17 * You should have received a copy of the GNU General Public License
igrokhotkov 0:83f3dcfa5c8f 18 * along with this program; if not, write to the Free Software
igrokhotkov 0:83f3dcfa5c8f 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
igrokhotkov 0:83f3dcfa5c8f 20 */
igrokhotkov 0:83f3dcfa5c8f 21
igrokhotkov 0:83f3dcfa5c8f 22
igrokhotkov 0:83f3dcfa5c8f 23 #ifndef NO_HMAC
igrokhotkov 0:83f3dcfa5c8f 24
igrokhotkov 0:83f3dcfa5c8f 25 #ifndef CTAO_CRYPT_HMAC_H
igrokhotkov 0:83f3dcfa5c8f 26 #define CTAO_CRYPT_HMAC_H
igrokhotkov 0:83f3dcfa5c8f 27
igrokhotkov 0:83f3dcfa5c8f 28 #define NO_SHA256
igrokhotkov 0:83f3dcfa5c8f 29 #define NO_SHA
igrokhotkov 0:83f3dcfa5c8f 30
igrokhotkov 0:83f3dcfa5c8f 31 #ifndef NO_MD5
igrokhotkov 0:83f3dcfa5c8f 32 #include "md5.h"
igrokhotkov 0:83f3dcfa5c8f 33 #endif
igrokhotkov 0:83f3dcfa5c8f 34
igrokhotkov 0:83f3dcfa5c8f 35 #ifndef NO_SHA
igrokhotkov 0:83f3dcfa5c8f 36 #include <cyassl/ctaocrypt/sha.h>
igrokhotkov 0:83f3dcfa5c8f 37 #endif
igrokhotkov 0:83f3dcfa5c8f 38
igrokhotkov 0:83f3dcfa5c8f 39
igrokhotkov 0:83f3dcfa5c8f 40 #ifndef NO_SHA256
igrokhotkov 0:83f3dcfa5c8f 41 #include <cyassl/ctaocrypt/sha256.h>
igrokhotkov 0:83f3dcfa5c8f 42 #endif
igrokhotkov 0:83f3dcfa5c8f 43
igrokhotkov 0:83f3dcfa5c8f 44 #ifdef CYASSL_SHA384
igrokhotkov 0:83f3dcfa5c8f 45 #include <cyassl/ctaocrypt/sha512.h>
igrokhotkov 0:83f3dcfa5c8f 46 #endif
igrokhotkov 0:83f3dcfa5c8f 47
igrokhotkov 0:83f3dcfa5c8f 48 #ifdef HAVE_CAVIUM
igrokhotkov 0:83f3dcfa5c8f 49 #include <cyassl/ctaocrypt/logging.h>
igrokhotkov 0:83f3dcfa5c8f 50 #include "cavium_common.h"
igrokhotkov 0:83f3dcfa5c8f 51 #endif
igrokhotkov 0:83f3dcfa5c8f 52
igrokhotkov 0:83f3dcfa5c8f 53 #ifdef __cplusplus
igrokhotkov 0:83f3dcfa5c8f 54 extern "C" {
igrokhotkov 0:83f3dcfa5c8f 55 #endif
igrokhotkov 0:83f3dcfa5c8f 56
igrokhotkov 0:83f3dcfa5c8f 57
igrokhotkov 0:83f3dcfa5c8f 58 #define CYASSL_HMAC_CAVIUM_MAGIC 0xBEEF0005
igrokhotkov 0:83f3dcfa5c8f 59
igrokhotkov 0:83f3dcfa5c8f 60 enum {
igrokhotkov 0:83f3dcfa5c8f 61 IPAD = 0x36,
igrokhotkov 0:83f3dcfa5c8f 62 OPAD = 0x5C,
igrokhotkov 0:83f3dcfa5c8f 63 #ifdef NO_MD5
igrokhotkov 0:83f3dcfa5c8f 64 MD5 = 0,
igrokhotkov 0:83f3dcfa5c8f 65 #endif
igrokhotkov 0:83f3dcfa5c8f 66 #if defined(CYASSL_SHA384)
igrokhotkov 0:83f3dcfa5c8f 67 INNER_HASH_SIZE = SHA384_DIGEST_SIZE,
igrokhotkov 0:83f3dcfa5c8f 68 HMAC_BLOCK_SIZE = SHA384_BLOCK_SIZE
igrokhotkov 0:83f3dcfa5c8f 69 #elif !defined(NO_SHA256)
igrokhotkov 0:83f3dcfa5c8f 70 INNER_HASH_SIZE = SHA256_DIGEST_SIZE,
igrokhotkov 0:83f3dcfa5c8f 71 HMAC_BLOCK_SIZE = SHA256_BLOCK_SIZE,
igrokhotkov 0:83f3dcfa5c8f 72 SHA384 = 5
igrokhotkov 0:83f3dcfa5c8f 73 #elif !defined(NO_SHA)
igrokhotkov 0:83f3dcfa5c8f 74 INNER_HASH_SIZE = SHA_DIGEST_SIZE,
igrokhotkov 0:83f3dcfa5c8f 75 HMAC_BLOCK_SIZE = SHA_BLOCK_SIZE,
igrokhotkov 0:83f3dcfa5c8f 76 SHA256 = 2, /* hash type unique */
igrokhotkov 0:83f3dcfa5c8f 77 SHA384 = 5
igrokhotkov 0:83f3dcfa5c8f 78 #else
igrokhotkov 0:83f3dcfa5c8f 79 INNER_HASH_SIZE = MD5_DIGEST_SIZE,
igrokhotkov 0:83f3dcfa5c8f 80 HMAC_BLOCK_SIZE = MD5_BLOCK_SIZE,
igrokhotkov 0:83f3dcfa5c8f 81 SHA = 1,
igrokhotkov 0:83f3dcfa5c8f 82 SHA256 = 2, /* hash type unique */
igrokhotkov 0:83f3dcfa5c8f 83 SHA384 = 5
igrokhotkov 0:83f3dcfa5c8f 84 #endif
igrokhotkov 0:83f3dcfa5c8f 85 };
igrokhotkov 0:83f3dcfa5c8f 86
igrokhotkov 0:83f3dcfa5c8f 87
igrokhotkov 0:83f3dcfa5c8f 88 /* hash union */
igrokhotkov 0:83f3dcfa5c8f 89 typedef union {
igrokhotkov 0:83f3dcfa5c8f 90 #ifndef NO_MD5
igrokhotkov 0:83f3dcfa5c8f 91 Md5 md5;
igrokhotkov 0:83f3dcfa5c8f 92 #endif
igrokhotkov 0:83f3dcfa5c8f 93 #ifndef NO_SHA
igrokhotkov 0:83f3dcfa5c8f 94 Sha sha;
igrokhotkov 0:83f3dcfa5c8f 95 #endif
igrokhotkov 0:83f3dcfa5c8f 96 #ifndef NO_SHA256
igrokhotkov 0:83f3dcfa5c8f 97 Sha256 sha256;
igrokhotkov 0:83f3dcfa5c8f 98 #endif
igrokhotkov 0:83f3dcfa5c8f 99 #ifdef CYASSL_SHA384
igrokhotkov 0:83f3dcfa5c8f 100 Sha384 sha384;
igrokhotkov 0:83f3dcfa5c8f 101 #endif
igrokhotkov 0:83f3dcfa5c8f 102 } Hash;
igrokhotkov 0:83f3dcfa5c8f 103
igrokhotkov 0:83f3dcfa5c8f 104 /* Hmac digest */
igrokhotkov 0:83f3dcfa5c8f 105 typedef struct Hmac {
igrokhotkov 0:83f3dcfa5c8f 106 Hash hash;
igrokhotkov 0:83f3dcfa5c8f 107 word32 ipad[HMAC_BLOCK_SIZE / sizeof(word32)]; /* same block size all*/
igrokhotkov 0:83f3dcfa5c8f 108 word32 opad[HMAC_BLOCK_SIZE / sizeof(word32)];
igrokhotkov 0:83f3dcfa5c8f 109 word32 innerHash[INNER_HASH_SIZE / sizeof(word32)]; /* max size */
igrokhotkov 0:83f3dcfa5c8f 110 byte macType; /* md5 sha or sha256 */
igrokhotkov 0:83f3dcfa5c8f 111 byte innerHashKeyed; /* keyed flag */
igrokhotkov 0:83f3dcfa5c8f 112 #ifdef HAVE_CAVIUM
igrokhotkov 0:83f3dcfa5c8f 113 word16 keyLen; /* hmac key length */
igrokhotkov 0:83f3dcfa5c8f 114 word16 dataLen;
igrokhotkov 0:83f3dcfa5c8f 115 HashType type; /* hmac key type */
igrokhotkov 0:83f3dcfa5c8f 116 int devId; /* nitrox device id */
igrokhotkov 0:83f3dcfa5c8f 117 word32 magic; /* using cavium magic */
igrokhotkov 0:83f3dcfa5c8f 118 word64 contextHandle; /* nitrox context memory handle */
igrokhotkov 0:83f3dcfa5c8f 119 byte* data; /* buffered input data for one call */
igrokhotkov 0:83f3dcfa5c8f 120 #endif
igrokhotkov 0:83f3dcfa5c8f 121 } Hmac;
igrokhotkov 0:83f3dcfa5c8f 122
igrokhotkov 0:83f3dcfa5c8f 123
igrokhotkov 0:83f3dcfa5c8f 124 /* does init */
igrokhotkov 0:83f3dcfa5c8f 125 CYASSL_API void HmacSetKey(Hmac*, int type, const byte* key, word32 keySz);
igrokhotkov 0:83f3dcfa5c8f 126 CYASSL_API void HmacUpdate(Hmac*, const byte*, word32);
igrokhotkov 0:83f3dcfa5c8f 127 CYASSL_API void HmacFinal(Hmac*, byte*);
igrokhotkov 0:83f3dcfa5c8f 128
igrokhotkov 0:83f3dcfa5c8f 129 #ifdef HAVE_CAVIUM
igrokhotkov 0:83f3dcfa5c8f 130 CYASSL_API int HmacInitCavium(Hmac*, int);
igrokhotkov 0:83f3dcfa5c8f 131 CYASSL_API void HmacFreeCavium(Hmac*);
igrokhotkov 0:83f3dcfa5c8f 132 #endif
igrokhotkov 0:83f3dcfa5c8f 133
igrokhotkov 0:83f3dcfa5c8f 134
igrokhotkov 0:83f3dcfa5c8f 135 #ifdef __cplusplus
igrokhotkov 0:83f3dcfa5c8f 136 } /* extern "C" */
igrokhotkov 0:83f3dcfa5c8f 137 #endif
igrokhotkov 0:83f3dcfa5c8f 138
igrokhotkov 0:83f3dcfa5c8f 139 #endif /* CTAO_CRYPT_HMAC_H */
igrokhotkov 0:83f3dcfa5c8f 140
igrokhotkov 0:83f3dcfa5c8f 141 #endif /* NO_HMAC */