This supports HMAC-MD5 and HMAC-SHA1 for now, other hashes are simple to add.
Unified · Split
| @@ -13,6 +13,9 @@ libavutil: 2012-10-22 · 12 unmodified lines | |||
| 13 | 13 | ||
| 14 | API changes, most recent first: | 14 | API changes, most recent first: |
| 15 | 15 | ||
| 16 | 2013-01-xx - xxxxxxx - lavu 52.5.0 - hmac.h | ||
| 17 | Add AVHMAC. | ||
| 18 | |||
| 16 | 2013-01-13 - xxxxxxx - lavc 54.36.0 - vdpau.h | 19 | 2013-01-13 - xxxxxxx - lavc 54.36.0 - vdpau.h |
| 17 | Add AVVDPAUContext struct for VDPAU hardware-accelerated decoding. | 20 | Add AVVDPAUContext struct for VDPAU hardware-accelerated decoding. |
| 18 | 21 | ||
| @@ -19,6 +19,7 @@ HEADERS = adler32.h \ · 18 unmodified lines | |||
| 19 | eval.h \ | 19 | eval.h \ |
| 20 | fifo.h \ | 20 | fifo.h \ |
| 21 | file.h \ | 21 | file.h \ |
| 22 | hmac.h \ | ||
| 22 | imgutils.h \ | 23 | imgutils.h \ |
| 23 | intfloat.h \ | 24 | intfloat.h \ |
| 24 | intfloat_readwrite.h \ | 25 | intfloat_readwrite.h \ |
| @@ -66,6 +67,7 @@ OBJS = adler32.o \ · 41 unmodified lines | |||
| 66 | fifo.o \ | 67 | fifo.o \ |
| 67 | file.o \ | 68 | file.o \ |
| 68 | float_dsp.o \ | 69 | float_dsp.o \ |
| 70 | hmac.o \ | ||
| 69 | imgutils.o \ | 71 | imgutils.o \ |
| 70 | intfloat_readwrite.o \ | 72 | intfloat_readwrite.o \ |
| 71 | intmath.o \ | 73 | intmath.o \ |
| @@ -0,0 +1,138 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2012 Martin Storsjo | ||
| 3 | * | ||
| 4 | * This file is part of Libav. | ||
| 5 | * | ||
| 6 | * Libav is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * Libav is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with Libav; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include <string.h> | ||
| 22 | |||
| 23 | #include "hmac.h" | ||
| 24 | #include "md5.h" | ||
| 25 | #include "sha.h" | ||
| 26 | #include "mem.h" | ||
| 27 | |||
| 28 | #define MAX_HASHLEN 20 | ||
| 29 | #define MAX_BLOCKLEN 64 | ||
| 30 | |||
| 31 | struct AVHMAC { | ||
| 32 | void *hash; | ||
| 33 | int blocklen, hashlen; | ||
| 34 | void (*final)(void*, uint8_t*); | ||
| 35 | void (*update)(void*, const uint8_t*, int len); | ||
| 36 | void (*init)(void*); | ||
| 37 | uint8_t key[MAX_BLOCKLEN]; | ||
| 38 | int keylen; | ||
| 39 | }; | ||
| 40 | |||
| 41 | static void sha1_init(void *ctx) | ||
| 42 | { | ||
| 43 | av_sha_init(ctx, 160); | ||
| 44 | } | ||
| 45 | |||
| 46 | AVHMAC *av_hmac_alloc(enum AVHMACType type) | ||
| 47 | { | ||
| 48 | AVHMAC *c = av_mallocz(sizeof(*c)); | ||
| 49 | if (!c) | ||
| 50 | return NULL; | ||
| 51 | switch (type) { | ||
| 52 | case AV_HMAC_MD5: | ||
| 53 | c->blocklen = 64; | ||
| 54 | c->hashlen = 16; | ||
| 55 | c->init = av_md5_init; | ||
| 56 | c->update = av_md5_update; | ||
| 57 | c->final = av_md5_final; | ||
| 58 | c->hash = av_md5_alloc(); | ||
| 59 | break; | ||
| 60 | case AV_HMAC_SHA1: | ||
| 61 | c->blocklen = 64; | ||
| 62 | c->hashlen = 20; | ||
| 63 | c->init = sha1_init; | ||
| 64 | c->update = av_sha_update; | ||
| 65 | c->final = av_sha_final; | ||
| 66 | c->hash = av_sha_alloc(); | ||
| 67 | break; | ||
| 68 | default: | ||
| 69 | av_free(c); | ||
| 70 | return NULL; | ||
| 71 | } | ||
| 72 | if (!c->hash) { | ||
| 73 | av_free(c); | ||
| 74 | return NULL; | ||
| 75 | } | ||
| 76 | return c; | ||
| 77 | } | ||
| 78 | |||
| 79 | void av_hmac_free(AVHMAC *c) | ||
| 80 | { | ||
| 81 | if (!c) | ||
| 82 | return; | ||
| 83 | av_free(c->hash); | ||
| 84 | av_free(c); | ||
| 85 | } | ||
| 86 | |||
| 87 | void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen) | ||
| 88 | { | ||
| 89 | int i; | ||
| 90 | uint8_t block[MAX_BLOCKLEN]; | ||
| 91 | if (keylen > c->blocklen) { | ||
| 92 | c->init(c->hash); | ||
| 93 | c->update(c->hash, key, keylen); | ||
| 94 | c->final(c->hash, c->key); | ||
| 95 | c->keylen = c->hashlen; | ||
| 96 | } else { | ||
| 97 | memcpy(c->key, key, keylen); | ||
| 98 | c->keylen = keylen; | ||
| 99 | } | ||
| 100 | c->init(c->hash); | ||
| 101 | for (i = 0; i < c->keylen; i++) | ||
| 102 | block[i] = c->key[i] ^ 0x36; | ||
| 103 | for (i = c->keylen; i < c->blocklen; i++) | ||
| 104 | block[i] = 0x36; | ||
| 105 | c->update(c->hash, block, c->blocklen); | ||
| 106 | } | ||
| 107 | |||
| 108 | void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len) | ||
| 109 | { | ||
| 110 | c->update(c->hash, data, len); | ||
| 111 | } | ||
| 112 | |||
| 113 | int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen) | ||
| 114 | { | ||
| 115 | uint8_t block[MAX_BLOCKLEN]; | ||
| 116 | int i; | ||
| 117 | if (outlen < c->hashlen) | ||
| 118 | return AVERROR(EINVAL); | ||
| 119 | c->final(c->hash, out); | ||
| 120 | c->init(c->hash); | ||
| 121 | for (i = 0; i < c->keylen; i++) | ||
| 122 | block[i] = c->key[i] ^ 0x5C; | ||
| 123 | for (i = c->keylen; i < c->blocklen; i++) | ||
| 124 | block[i] = 0x5C; | ||
| 125 | c->update(c->hash, block, c->blocklen); | ||
| 126 | c->update(c->hash, out, c->hashlen); | ||
| 127 | c->final(c->hash, out); | ||
| 128 | return c->hashlen; | ||
| 129 | } | ||
| 130 | |||
| 131 | int av_hmac_calc(AVHMAC *c, const uint8_t *data, unsigned int len, | ||
| 132 | const uint8_t *key, unsigned int keylen, | ||
| 133 | uint8_t *out, unsigned int outlen) | ||
| 134 | { | ||
| 135 | av_hmac_init(c, key, keylen); | ||
| 136 | av_hmac_update(c, data, len); | ||
| 137 | return av_hmac_final(c, out, outlen); | ||
| 138 | } | ||
| @@ -0,0 +1,95 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2012 Martin Storsjo | ||
| 3 | * | ||
| 4 | * This file is part of Libav. | ||
| 5 | * | ||
| 6 | * Libav is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * Libav is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with Libav; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | #ifndef AVUTIL_HMAC_H | ||
| 22 | #define AVUTIL_HMAC_H | ||
| 23 | |||
| 24 | #include <stdint.h> | ||
| 25 | |||
| 26 | /** | ||
| 27 | * @defgroup lavu_hmac HMAC | ||
| 28 | * @ingroup lavu_crypto | ||
| 29 | * @{ | ||
| 30 | */ | ||
| 31 | |||
| 32 | enum AVHMACType { | ||
| 33 | AV_HMAC_MD5, | ||
| 34 | AV_HMAC_SHA1, | ||
| 35 | }; | ||
| 36 | |||
| 37 | typedef struct AVHMAC AVHMAC; | ||
| 38 | |||
| 39 | /** | ||
| 40 | * Allocate an AVHMAC context. | ||
| 41 | * @param type The hash function used for the HMAC. | ||
| 42 | */ | ||
| 43 | AVHMAC *av_hmac_alloc(enum AVHMACType type); | ||
| 44 | |||
| 45 | /** | ||
| 46 | * Free an AVHMAC context. | ||
| 47 | * @param ctx The context to free, may be NULL | ||
| 48 | */ | ||
| 49 | void av_hmac_free(AVHMAC *ctx); | ||
| 50 | |||
| 51 | /** | ||
| 52 | * Initialize an AVHMAC context with an authentication key. | ||
| 53 | * @param ctx The HMAC context | ||
| 54 | * @param key The authentication key | ||
| 55 | * @param keylen The length of the key, in bytes | ||
| 56 | */ | ||
| 57 | void av_hmac_init(AVHMAC *ctx, const uint8_t *key, unsigned int keylen); | ||
| 58 | |||
| 59 | /** | ||
| 60 | * Hash data with the HMAC. | ||
| 61 | * @param ctx The HMAC context | ||
| 62 | * @param data The data to hash | ||
| 63 | * @param len The length of the data, in bytes | ||
| 64 | */ | ||
| 65 | void av_hmac_update(AVHMAC *ctx, const uint8_t *data, unsigned int len); | ||
| 66 | |||
| 67 | /** | ||
| 68 | * Finish hashing and output the HMAC digest. | ||
| 69 | * @param ctx The HMAC context | ||
| 70 | * @param out The output buffer to write the digest into | ||
| 71 | * @param outlen The length of the out buffer, in bytes | ||
| 72 | * @return The number of bytes written to out, or a negative error code. | ||
| 73 | */ | ||
| 74 | int av_hmac_final(AVHMAC *ctx, uint8_t *out, unsigned int outlen); | ||
| 75 | |||
| 76 | /** | ||
| 77 | * Hash an array of data with a key. | ||
| 78 | * @param ctx The HMAC context | ||
| 79 | * @param data The data to hash | ||
| 80 | * @param len The length of the data, in bytes | ||
| 81 | * @param key The authentication key | ||
| 82 | * @param keylen The length of the key, in bytes | ||
| 83 | * @param out The output buffer to write the digest into | ||
| 84 | * @param outlen The length of the out buffer, in bytes | ||
| 85 | * @return The number of bytes written to out, or a negative error code. | ||
| 86 | */ | ||
| 87 | int av_hmac_calc(AVHMAC *ctx, const uint8_t *data, unsigned int len, | ||
| 88 | const uint8_t *key, unsigned int keylen, | ||
| 89 | uint8_t *out, unsigned int outlen); | ||
| 90 | |||
| 91 | /** | ||
| 92 | * @} | ||
| 93 | */ | ||
| 94 | |||
| 95 | #endif /* AVUTIL_HMAC_H */ | ||
| @@ -37,7 +37,7 @@ · 36 unmodified lines | |||
| 37 | */ | 37 | */ |
| 38 | 38 | ||
| 39 | #define LIBAVUTIL_VERSION_MAJOR 52 | 39 | #define LIBAVUTIL_VERSION_MAJOR 52 |
| 40 | #define LIBAVUTIL_VERSION_MINOR 4 | 40 | #define LIBAVUTIL_VERSION_MINOR 5 |
| 41 | #define LIBAVUTIL_VERSION_MICRO 0 | 41 | #define LIBAVUTIL_VERSION_MICRO 0 |
| 42 | 42 | ||
| 43 | #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ | 43 | #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ |