Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | e24852d | 2014-02-04 13:13:02 -0500 | [diff] [blame] | 2 | Copyright (C) 2005, 2004, 2010, Erik Eliasson, Johan Bilien, Werner Dittmann |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | This library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with this library; if not, write to the Free Software |
| 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | |
| 18 | * In addition, as a special exception, the copyright holders give |
| 19 | * permission to link the code of portions of this program with the |
| 20 | * OpenSSL library under certain conditions as described in each |
| 21 | * individual source file, and distribute linked combinations |
| 22 | * including the two. |
| 23 | * You must obey the GNU General Public License in all respects |
| 24 | * for all of the code used other than OpenSSL. If you modify |
| 25 | * file(s) with this exception, you may extend this exception to your |
| 26 | * version of the file(s), but you are not obligated to do so. If you |
| 27 | * do not wish to do so, delete this exception statement from your |
| 28 | * version. If you delete this exception statement from all source |
| 29 | * files in the program, then also delete it here. |
| 30 | */ |
| 31 | |
| 32 | /* |
Alexandre Lision | e24852d | 2014-02-04 13:13:02 -0500 | [diff] [blame] | 33 | * Authors: Erik Eliasson <eliasson@it.kth.se> |
| 34 | * Johan Bilien <jobi@via.ecp.fr> |
| 35 | * Werner Dittmann |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | #include <stdint.h> |
| 39 | #include <openssl/hmac.h> |
| 40 | #include <crypto/hmac.h> |
| 41 | |
| 42 | void hmac_sha1(uint8_t * key, int32_t key_length, |
| 43 | const uint8_t* data, uint32_t data_length, |
| 44 | uint8_t* mac, int32_t* mac_length ) |
| 45 | { |
| 46 | HMAC(EVP_sha1(), key, key_length, |
| 47 | data, data_length, mac, |
| 48 | reinterpret_cast<uint32_t*>(mac_length) ); |
| 49 | } |
| 50 | |
| 51 | void hmac_sha1( uint8_t* key, int32_t key_length, |
| 52 | const uint8_t* data_chunks[], |
| 53 | uint32_t data_chunck_length[], |
| 54 | uint8_t* mac, int32_t* mac_length ) { |
| 55 | HMAC_CTX ctx; |
| 56 | HMAC_CTX_init(&ctx); |
| 57 | HMAC_Init_ex(&ctx, key, key_length, EVP_sha1(), NULL); |
| 58 | while (*data_chunks) { |
| 59 | HMAC_Update(&ctx, *data_chunks, *data_chunck_length); |
| 60 | data_chunks ++; |
| 61 | data_chunck_length ++; |
| 62 | } |
| 63 | HMAC_Final(&ctx, mac, reinterpret_cast<uint32_t*>(mac_length)); |
| 64 | HMAC_CTX_cleanup(&ctx); |
| 65 | } |
| 66 | |
| 67 | void* createSha1HmacContext(uint8_t* key, int32_t key_length) |
| 68 | { |
| 69 | HMAC_CTX* ctx = (HMAC_CTX*)malloc(sizeof(HMAC_CTX)); |
Alexandre Lision | e24852d | 2014-02-04 13:13:02 -0500 | [diff] [blame] | 70 | |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 71 | HMAC_CTX_init(ctx); |
| 72 | HMAC_Init_ex(ctx, key, key_length, EVP_sha1(), NULL); |
| 73 | return ctx; |
| 74 | } |
| 75 | |
| 76 | void hmacSha1Ctx(void* ctx, const uint8_t* data, uint32_t data_length, |
| 77 | uint8_t* mac, int32_t* mac_length) |
| 78 | { |
| 79 | HMAC_CTX* pctx = (HMAC_CTX*)ctx; |
| 80 | |
| 81 | HMAC_Init_ex(pctx, NULL, 0, NULL, NULL ); |
| 82 | HMAC_Update(pctx, data, data_length ); |
| 83 | HMAC_Final(pctx, mac, reinterpret_cast<uint32_t*>(mac_length) ); |
| 84 | } |
| 85 | |
| 86 | void hmacSha1Ctx(void* ctx, const uint8_t* data[], uint32_t data_length[], |
| 87 | uint8_t* mac, int32_t* mac_length ) |
| 88 | { |
| 89 | HMAC_CTX* pctx = (HMAC_CTX*)ctx; |
| 90 | |
| 91 | HMAC_Init_ex(pctx, NULL, 0, NULL, NULL ); |
| 92 | while (*data) { |
| 93 | HMAC_Update(pctx, *data, *data_length); |
| 94 | data++; |
| 95 | data_length++; |
| 96 | } |
| 97 | HMAC_Final(pctx, mac, reinterpret_cast<uint32_t*>(mac_length) ); |
| 98 | } |
| 99 | |
| 100 | void freeSha1HmacContext(void* ctx) |
| 101 | { |
| 102 | if (ctx) { |
| 103 | HMAC_CTX_cleanup((HMAC_CTX*)ctx); |
| 104 | free(ctx); |
| 105 | } |
| 106 | } |