]> git.sesse.net Git - ffmpeg/blob - libavutil/hmac.c
avio: add a destructor for AVIOContext
[ffmpeg] / libavutil / hmac.c
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 <stddef.h>
22 #include <stdint.h>
23 #include <string.h>
24
25 #include "attributes.h"
26 #include "hmac.h"
27 #include "md5.h"
28 #include "sha.h"
29 #include "mem.h"
30 #include "version.h"
31
32 #define MAX_HASHLEN 32
33 #define MAX_BLOCKLEN 64
34
35 typedef void (*hmac_final)(void *ctx, uint8_t *dst);
36 #if FF_API_CRYPTO_SIZE_T
37 typedef void (*hmac_update)(void *ctx, const uint8_t *src, int len);
38 #else
39 typedef void (*hmac_update)(void *ctx, const uint8_t *src, size_t len);
40 #endif
41 typedef void (*hmac_init)(void *ctx);
42
43 struct AVHMAC {
44     void *hash;
45     int blocklen, hashlen;
46     hmac_final  final;
47     hmac_update update;
48     hmac_init   init;
49     uint8_t key[MAX_BLOCKLEN];
50     int keylen;
51 };
52
53 #define DEFINE_SHA(bits)                           \
54 static av_cold void sha ## bits ##_init(void *ctx) \
55 {                                                  \
56     av_sha_init(ctx, bits);                        \
57 }
58
59 DEFINE_SHA(160)
60 DEFINE_SHA(224)
61 DEFINE_SHA(256)
62
63 AVHMAC *av_hmac_alloc(enum AVHMACType type)
64 {
65     AVHMAC *c = av_mallocz(sizeof(*c));
66     if (!c)
67         return NULL;
68     switch (type) {
69     case AV_HMAC_MD5:
70         c->blocklen = 64;
71         c->hashlen  = 16;
72         c->init     = (hmac_init) av_md5_init;
73         c->update   = (hmac_update) av_md5_update;
74         c->final    = (hmac_final) av_md5_final;
75         c->hash     = av_md5_alloc();
76         break;
77     case AV_HMAC_SHA1:
78         c->blocklen = 64;
79         c->hashlen  = 20;
80         c->init     = sha160_init;
81         c->update   = (hmac_update) av_sha_update;
82         c->final    = (hmac_final) av_sha_final;
83         c->hash     = av_sha_alloc();
84         break;
85     case AV_HMAC_SHA224:
86         c->blocklen = 64;
87         c->hashlen  = 28;
88         c->init     = sha224_init;
89         c->update   = (hmac_update) av_sha_update;
90         c->final    = (hmac_final) av_sha_final;
91         c->hash     = av_sha_alloc();
92         break;
93     case AV_HMAC_SHA256:
94         c->blocklen = 64;
95         c->hashlen  = 32;
96         c->init     = sha256_init;
97         c->update   = (hmac_update) av_sha_update;
98         c->final    = (hmac_final) av_sha_final;
99         c->hash     = av_sha_alloc();
100         break;
101     default:
102         av_free(c);
103         return NULL;
104     }
105     if (!c->hash) {
106         av_free(c);
107         return NULL;
108     }
109     return c;
110 }
111
112 void av_hmac_free(AVHMAC *c)
113 {
114     if (!c)
115         return;
116     av_free(c->hash);
117     av_free(c);
118 }
119
120 void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
121 {
122     int i;
123     uint8_t block[MAX_BLOCKLEN];
124     if (keylen > c->blocklen) {
125         c->init(c->hash);
126         c->update(c->hash, key, keylen);
127         c->final(c->hash, c->key);
128         c->keylen = c->hashlen;
129     } else {
130         memcpy(c->key, key, keylen);
131         c->keylen = keylen;
132     }
133     c->init(c->hash);
134     for (i = 0; i < c->keylen; i++)
135         block[i] = c->key[i] ^ 0x36;
136     for (i = c->keylen; i < c->blocklen; i++)
137         block[i] = 0x36;
138     c->update(c->hash, block, c->blocklen);
139 }
140
141 void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
142 {
143     c->update(c->hash, data, len);
144 }
145
146 int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
147 {
148     uint8_t block[MAX_BLOCKLEN];
149     int i;
150     if (outlen < c->hashlen)
151         return AVERROR(EINVAL);
152     c->final(c->hash, out);
153     c->init(c->hash);
154     for (i = 0; i < c->keylen; i++)
155         block[i] = c->key[i] ^ 0x5C;
156     for (i = c->keylen; i < c->blocklen; i++)
157         block[i] = 0x5C;
158     c->update(c->hash, block, c->blocklen);
159     c->update(c->hash, out, c->hashlen);
160     c->final(c->hash, out);
161     return c->hashlen;
162 }
163
164 int av_hmac_calc(AVHMAC *c, const uint8_t *data, unsigned int len,
165                  const uint8_t *key, unsigned int keylen,
166                  uint8_t *out, unsigned int outlen)
167 {
168     av_hmac_init(c, key, keylen);
169     av_hmac_update(c, data, len);
170     return av_hmac_final(c, out, outlen);
171 }