Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | 907ed2e | 2014-02-04 10:33:09 -0500 | [diff] [blame] | 2 | Copyright (C) 2006-2013 Werner Dittmann |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 3 | |
| 4 | This program is free software: you can redistribute it and/or modify |
Alexandre Lision | 907ed2e | 2014-02-04 10:33:09 -0500 | [diff] [blame] | 5 | it under the terms of the GNU Lesser General Public License as published by |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 6 | the Free Software Foundation, either version 3 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program 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 |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | /* |
| 19 | * Authors: Erik Eliasson <eliasson@it.kth.se> |
| 20 | * Johan Bilien <jobi@via.ecp.fr> |
| 21 | */ |
| 22 | |
| 23 | #include <gcrypt.h> |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 24 | #include <crypto/hmac256.h> |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 25 | |
| 26 | void hmac_sha256(uint8_t* key, uint32_t keyLength, |
| 27 | uint8_t* data, int32_t dataLength, |
| 28 | uint8_t* mac, uint32_t* macLength) |
| 29 | { |
| 30 | gcry_md_hd_t hd; |
| 31 | gcry_error_t err = 0; |
| 32 | |
| 33 | err = gcry_md_open(&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC); |
| 34 | gcry_md_setkey(hd, key, keyLength); |
| 35 | |
| 36 | gcry_md_write (hd, data, dataLength); |
| 37 | |
| 38 | uint8_t* p = gcry_md_read (hd, GCRY_MD_SHA256); |
| 39 | memcpy(mac, p, SHA256_DIGEST_LENGTH); |
| 40 | if (macLength != NULL) { |
| 41 | *macLength = SHA256_DIGEST_LENGTH; |
| 42 | } |
| 43 | gcry_md_close (hd); |
| 44 | } |
| 45 | |
| 46 | void hmac_sha256( uint8_t* key, uint32_t keyLength, |
| 47 | uint8_t* dataChunks[], |
| 48 | uint32_t dataChunkLength[], |
| 49 | uint8_t* mac, uint32_t* macLength ) |
| 50 | { |
| 51 | gcry_md_hd_t hd; |
| 52 | gcry_error_t err = 0; |
| 53 | |
| 54 | err = gcry_md_open(&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC); |
| 55 | gcry_md_setkey(hd, key, keyLength); |
| 56 | |
| 57 | while (*dataChunks) { |
| 58 | gcry_md_write (hd, *dataChunks, (uint32_t)(*dataChunkLength)); |
| 59 | dataChunks++; |
| 60 | dataChunkLength++; |
| 61 | } |
| 62 | uint8_t* p = gcry_md_read (hd, GCRY_MD_SHA256); |
| 63 | memcpy(mac, p, SHA256_DIGEST_LENGTH); |
| 64 | if (macLength != NULL) { |
| 65 | *macLength = SHA256_DIGEST_LENGTH; |
| 66 | } |
| 67 | gcry_md_close (hd); |
| 68 | } |