1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifdef FREEBL_NO_DEPEND
6 #include "stubs.h"
7 #endif
8 
9 #include "secport.h"
10 #include "hasht.h"
11 #include "blapit.h"
12 #include "alghmac.h"
13 #include "secerr.h"
14 
15 #define HMAC_PAD_SIZE HASH_BLOCK_LENGTH_MAX
16 
17 struct HMACContextStr {
18     void *hash;
19     const SECHashObject *hashobj;
20     PRBool wasAllocated;
21     unsigned char ipad[HMAC_PAD_SIZE];
22     unsigned char opad[HMAC_PAD_SIZE];
23 };
24 
25 void
HMAC_Destroy(HMACContext * cx,PRBool freeit)26 HMAC_Destroy(HMACContext *cx, PRBool freeit)
27 {
28     if (cx == NULL)
29         return;
30 
31     PORT_Assert(!freeit == !cx->wasAllocated);
32     if (cx->hash != NULL) {
33         cx->hashobj->destroy(cx->hash, PR_TRUE);
34         PORT_Memset(cx, 0, sizeof *cx);
35     }
36     if (freeit)
37         PORT_Free(cx);
38 }
39 
40 static SECStatus
hmac_initKey(HMACContext * cx,const unsigned char * secret,unsigned int secret_len,PRBool isFIPS)41 hmac_initKey(HMACContext *cx, const unsigned char *secret,
42              unsigned int secret_len, PRBool isFIPS)
43 {
44     unsigned int i;
45     unsigned char hashed_secret[HASH_LENGTH_MAX];
46 
47     /* required by FIPS 198 Section 3 */
48     if (isFIPS && secret_len < cx->hashobj->length / 2) {
49         PORT_SetError(SEC_ERROR_INVALID_ARGS);
50         return SECFailure;
51     }
52 
53     if (secret_len > cx->hashobj->blocklength) {
54         cx->hashobj->begin(cx->hash);
55         cx->hashobj->update(cx->hash, secret, secret_len);
56         PORT_Assert(cx->hashobj->length <= sizeof hashed_secret);
57         cx->hashobj->end(cx->hash, hashed_secret, &secret_len,
58                          sizeof hashed_secret);
59         if (secret_len != cx->hashobj->length) {
60             PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
61             goto loser;
62         }
63         secret = (const unsigned char *)&hashed_secret[0];
64     }
65 
66     PORT_Memset(cx->ipad, 0x36, cx->hashobj->blocklength);
67     PORT_Memset(cx->opad, 0x5c, cx->hashobj->blocklength);
68 
69     /* fold secret into padding */
70     for (i = 0; i < secret_len; i++) {
71         cx->ipad[i] ^= secret[i];
72         cx->opad[i] ^= secret[i];
73     }
74     PORT_Memset(hashed_secret, 0, sizeof hashed_secret);
75     return SECSuccess;
76 
77 loser:
78     PORT_Memset(hashed_secret, 0, sizeof hashed_secret);
79     return SECFailure;
80 }
81 
82 SECStatus
HMAC_Init(HMACContext * cx,const SECHashObject * hash_obj,const unsigned char * secret,unsigned int secret_len,PRBool isFIPS)83 HMAC_Init(HMACContext *cx, const SECHashObject *hash_obj,
84           const unsigned char *secret, unsigned int secret_len, PRBool isFIPS)
85 {
86     SECStatus rv;
87 
88     if (cx == NULL) {
89         PORT_SetError(SEC_ERROR_INVALID_ARGS);
90         return SECFailure;
91     }
92     cx->wasAllocated = PR_FALSE;
93     cx->hashobj = hash_obj;
94     cx->hash = cx->hashobj->create();
95     if (cx->hash == NULL)
96         goto loser;
97 
98     rv = hmac_initKey(cx, secret, secret_len, isFIPS);
99     if (rv != SECSuccess)
100         goto loser;
101 
102     return rv;
103 loser:
104     if (cx->hash != NULL)
105         cx->hashobj->destroy(cx->hash, PR_TRUE);
106     return SECFailure;
107 }
108 
109 HMACContext *
HMAC_Create(const SECHashObject * hash_obj,const unsigned char * secret,unsigned int secret_len,PRBool isFIPS)110 HMAC_Create(const SECHashObject *hash_obj, const unsigned char *secret,
111             unsigned int secret_len, PRBool isFIPS)
112 {
113     SECStatus rv;
114     HMACContext *cx = PORT_ZNew(HMACContext);
115     if (cx == NULL)
116         return NULL;
117     rv = HMAC_Init(cx, hash_obj, secret, secret_len, isFIPS);
118     cx->wasAllocated = PR_TRUE;
119     if (rv != SECSuccess) {
120         PORT_Free(cx); /* contains no secret info */
121         cx = NULL;
122     }
123     return cx;
124 }
125 
126 /* this allows us to reuse an existing HMACContext with a new key and
127  * Hash function */
128 SECStatus
HMAC_ReInit(HMACContext * cx,const SECHashObject * hash_obj,const unsigned char * secret,unsigned int secret_len,PRBool isFIPS)129 HMAC_ReInit(HMACContext *cx, const SECHashObject *hash_obj,
130             const unsigned char *secret, unsigned int secret_len, PRBool isFIPS)
131 {
132     PRBool wasAllocated;
133     SECStatus rv;
134 
135     /* if we are using the same hash, keep the hash contexts and only
136      * init the key */
137     if ((cx->hashobj == hash_obj) && (cx->hash != NULL)) {
138         return hmac_initKey(cx, secret, secret_len, isFIPS);
139     }
140     /* otherwise we destroy the contents of the context and
141      * initalize it from scratch. We need to preseve the current state
142      * of wasAllocated to the final destroy works correctly */
143     wasAllocated = cx->wasAllocated;
144     cx->wasAllocated = PR_FALSE;
145     HMAC_Destroy(cx, PR_FALSE);
146     rv = HMAC_Init(cx, hash_obj, secret, secret_len, isFIPS);
147     if (rv != SECSuccess) {
148         return rv;
149     }
150     cx->wasAllocated = wasAllocated;
151     return SECSuccess;
152 }
153 
154 void
HMAC_Begin(HMACContext * cx)155 HMAC_Begin(HMACContext *cx)
156 {
157     /* start inner hash */
158     cx->hashobj->begin(cx->hash);
159     cx->hashobj->update(cx->hash, cx->ipad, cx->hashobj->blocklength);
160 }
161 
162 void
HMAC_Update(HMACContext * cx,const unsigned char * data,unsigned int data_len)163 HMAC_Update(HMACContext *cx, const unsigned char *data, unsigned int data_len)
164 {
165     cx->hashobj->update(cx->hash, data, data_len);
166 }
167 
168 SECStatus
HMAC_Finish(HMACContext * cx,unsigned char * result,unsigned int * result_len,unsigned int max_result_len)169 HMAC_Finish(HMACContext *cx, unsigned char *result, unsigned int *result_len,
170             unsigned int max_result_len)
171 {
172     if (max_result_len < cx->hashobj->length) {
173         PORT_SetError(SEC_ERROR_INVALID_ARGS);
174         return SECFailure;
175     }
176 
177     cx->hashobj->end(cx->hash, result, result_len, max_result_len);
178     if (*result_len != cx->hashobj->length)
179         return SECFailure;
180 
181     cx->hashobj->begin(cx->hash);
182     cx->hashobj->update(cx->hash, cx->opad, cx->hashobj->blocklength);
183     cx->hashobj->update(cx->hash, result, *result_len);
184     cx->hashobj->end(cx->hash, result, result_len, max_result_len);
185     return SECSuccess;
186 }
187 
188 HMACContext *
HMAC_Clone(HMACContext * cx)189 HMAC_Clone(HMACContext *cx)
190 {
191     HMACContext *newcx;
192 
193     newcx = (HMACContext *)PORT_ZAlloc(sizeof(HMACContext));
194     if (newcx == NULL)
195         goto loser;
196 
197     newcx->wasAllocated = PR_TRUE;
198     newcx->hashobj = cx->hashobj;
199     newcx->hash = cx->hashobj->clone(cx->hash);
200     if (newcx->hash == NULL)
201         goto loser;
202     PORT_Memcpy(newcx->ipad, cx->ipad, cx->hashobj->blocklength);
203     PORT_Memcpy(newcx->opad, cx->opad, cx->hashobj->blocklength);
204     return newcx;
205 
206 loser:
207     HMAC_Destroy(newcx, PR_TRUE);
208     return NULL;
209 }
210