зеркало из https://github.com/mozilla/pjs.git
This is the last trunk version of this file. The file will be moved
to nss/lib/freebl. This version is identical to the first version of this file in nss/lib/freebl. Bug 303316. r=wtchang. Modified Files: alghmac.c alghmac.h rawhash.c
This commit is contained in:
Родитель
27ad9e4d19
Коммит
af23fd0e10
|
@ -1,166 +0,0 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is the Netscape security libraries.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 1994-2000
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "sechash.h"
|
||||
#include "secport.h"
|
||||
#include "alghmac.h"
|
||||
#include "secerr.h"
|
||||
|
||||
#define HMAC_PAD_SIZE 64
|
||||
|
||||
struct HMACContextStr {
|
||||
void *hash;
|
||||
const SECHashObject *hashobj;
|
||||
unsigned char ipad[HMAC_PAD_SIZE];
|
||||
unsigned char opad[HMAC_PAD_SIZE];
|
||||
};
|
||||
|
||||
void
|
||||
HMAC_Destroy(HMACContext *cx)
|
||||
{
|
||||
if (cx == NULL)
|
||||
return;
|
||||
|
||||
if (cx->hash != NULL)
|
||||
cx->hashobj->destroy(cx->hash, PR_TRUE);
|
||||
PORT_ZFree(cx, sizeof(HMACContext));
|
||||
}
|
||||
|
||||
HMACContext *
|
||||
HMAC_Create(const SECHashObject *hash_obj, const unsigned char *secret,
|
||||
unsigned int secret_len, PRBool isFIPS)
|
||||
{
|
||||
HMACContext *cx;
|
||||
unsigned int i;
|
||||
unsigned char hashed_secret[HASH_LENGTH_MAX];
|
||||
|
||||
/* required by FIPS 198 Section 3 */
|
||||
if (isFIPS && secret_len < hash_obj->length/2) {
|
||||
PORT_SetError(SEC_ERROR_INVALID_ARGS);
|
||||
return NULL;
|
||||
}
|
||||
cx = (HMACContext*)PORT_ZAlloc(sizeof(HMACContext));
|
||||
if (cx == NULL)
|
||||
return NULL;
|
||||
cx->hashobj = hash_obj;
|
||||
|
||||
cx->hash = cx->hashobj->create();
|
||||
if (cx->hash == NULL)
|
||||
goto loser;
|
||||
|
||||
if (secret_len > HMAC_PAD_SIZE) {
|
||||
cx->hashobj->begin( cx->hash);
|
||||
cx->hashobj->update(cx->hash, secret, secret_len);
|
||||
PORT_Assert(cx->hashobj->length <= sizeof hashed_secret);
|
||||
cx->hashobj->end( cx->hash, hashed_secret, &secret_len,
|
||||
sizeof hashed_secret);
|
||||
if (secret_len != cx->hashobj->length)
|
||||
goto loser;
|
||||
secret = (const unsigned char *)&hashed_secret[0];
|
||||
}
|
||||
|
||||
PORT_Memset(cx->ipad, 0x36, sizeof cx->ipad);
|
||||
PORT_Memset(cx->opad, 0x5c, sizeof cx->opad);
|
||||
|
||||
/* fold secret into padding */
|
||||
for (i = 0; i < secret_len; i++) {
|
||||
cx->ipad[i] ^= secret[i];
|
||||
cx->opad[i] ^= secret[i];
|
||||
}
|
||||
PORT_Memset(hashed_secret, 0, sizeof hashed_secret);
|
||||
return cx;
|
||||
|
||||
loser:
|
||||
PORT_Memset(hashed_secret, 0, sizeof hashed_secret);
|
||||
HMAC_Destroy(cx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
HMAC_Begin(HMACContext *cx)
|
||||
{
|
||||
/* start inner hash */
|
||||
cx->hashobj->begin(cx->hash);
|
||||
cx->hashobj->update(cx->hash, cx->ipad, sizeof(cx->ipad));
|
||||
}
|
||||
|
||||
void
|
||||
HMAC_Update(HMACContext *cx, const unsigned char *data, unsigned int data_len)
|
||||
{
|
||||
cx->hashobj->update(cx->hash, data, data_len);
|
||||
}
|
||||
|
||||
SECStatus
|
||||
HMAC_Finish(HMACContext *cx, unsigned char *result, unsigned int *result_len,
|
||||
unsigned int max_result_len)
|
||||
{
|
||||
if (max_result_len < cx->hashobj->length) {
|
||||
PORT_SetError(SEC_ERROR_INVALID_ARGS);
|
||||
return SECFailure;
|
||||
}
|
||||
|
||||
cx->hashobj->end(cx->hash, result, result_len, max_result_len);
|
||||
if (*result_len != cx->hashobj->length)
|
||||
return SECFailure;
|
||||
|
||||
cx->hashobj->begin(cx->hash);
|
||||
cx->hashobj->update(cx->hash, cx->opad, sizeof(cx->opad));
|
||||
cx->hashobj->update(cx->hash, result, *result_len);
|
||||
cx->hashobj->end(cx->hash, result, result_len, max_result_len);
|
||||
return SECSuccess;
|
||||
}
|
||||
|
||||
HMACContext *
|
||||
HMAC_Clone(HMACContext *cx)
|
||||
{
|
||||
HMACContext *newcx;
|
||||
|
||||
newcx = (HMACContext*)PORT_ZAlloc(sizeof(HMACContext));
|
||||
if (newcx == NULL)
|
||||
goto loser;
|
||||
|
||||
newcx->hashobj = cx->hashobj;
|
||||
newcx->hash = cx->hashobj->clone(cx->hash);
|
||||
if (newcx->hash == NULL)
|
||||
goto loser;
|
||||
PORT_Memcpy(newcx->ipad, cx->ipad, sizeof(cx->ipad));
|
||||
PORT_Memcpy(newcx->opad, cx->opad, sizeof(cx->opad));
|
||||
return newcx;
|
||||
|
||||
loser:
|
||||
HMAC_Destroy(newcx);
|
||||
return NULL;
|
||||
}
|
|
@ -1,91 +0,0 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is the Netscape security libraries.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 1994-2000
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#ifndef _ALGHMAC_H_
|
||||
#define _ALGHMAC_H_
|
||||
|
||||
typedef struct HMACContextStr HMACContext;
|
||||
|
||||
SEC_BEGIN_PROTOS
|
||||
|
||||
/* destroy HMAC context */
|
||||
extern void
|
||||
HMAC_Destroy(HMACContext *cx);
|
||||
|
||||
/* create HMAC context
|
||||
* hashObj hash object from SECRawHashObjects[]
|
||||
* secret the secret with which the HMAC is performed.
|
||||
* secret_len the length of the secret.
|
||||
* isFIPS true if conforming to FIPS 198.
|
||||
*
|
||||
* NULL is returned if an error occurs.
|
||||
*/
|
||||
extern HMACContext *
|
||||
HMAC_Create(const SECHashObject *hashObj, const unsigned char *secret,
|
||||
unsigned int secret_len, PRBool isFIPS);
|
||||
|
||||
/* reset HMAC for a fresh round */
|
||||
extern void
|
||||
HMAC_Begin(HMACContext *cx);
|
||||
|
||||
/* update HMAC
|
||||
* cx HMAC Context
|
||||
* data the data to perform HMAC on
|
||||
* data_len the length of the data to process
|
||||
*/
|
||||
extern void
|
||||
HMAC_Update(HMACContext *cx, const unsigned char *data, unsigned int data_len);
|
||||
|
||||
/* Finish HMAC -- place the results within result
|
||||
* cx HMAC context
|
||||
* result buffer for resulting hmac'd data
|
||||
* result_len where the resultant hmac length is stored
|
||||
* max_result_len maximum possible length that can be stored in result
|
||||
*/
|
||||
extern SECStatus
|
||||
HMAC_Finish(HMACContext *cx, unsigned char *result, unsigned int *result_len,
|
||||
unsigned int max_result_len);
|
||||
|
||||
/* clone a copy of the HMAC state. this is usefult when you would
|
||||
* need to keep a running hmac but also need to extract portions
|
||||
* partway through the process.
|
||||
*/
|
||||
extern HMACContext *
|
||||
HMAC_Clone(HMACContext *cx);
|
||||
|
||||
SEC_END_PROTOS
|
||||
|
||||
#endif
|
|
@ -1,138 +0,0 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is the Netscape security libraries.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 1994-2000
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "nspr.h"
|
||||
#include "sechash.h"
|
||||
#include "blapi.h" /* below the line */
|
||||
|
||||
|
||||
static void *
|
||||
null_hash_new_context(void)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void *
|
||||
null_hash_clone_context(void *v)
|
||||
{
|
||||
PORT_Assert(v == NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
null_hash_begin(void *v)
|
||||
{
|
||||
}
|
||||
|
||||
static void
|
||||
null_hash_update(void *v, const unsigned char *input, unsigned int length)
|
||||
{
|
||||
}
|
||||
|
||||
static void
|
||||
null_hash_end(void *v, unsigned char *output, unsigned int *outLen,
|
||||
unsigned int maxOut)
|
||||
{
|
||||
*outLen = 0;
|
||||
}
|
||||
|
||||
static void
|
||||
null_hash_destroy_context(void *v, PRBool b)
|
||||
{
|
||||
PORT_Assert(v == NULL);
|
||||
}
|
||||
|
||||
|
||||
const SECHashObject SECRawHashObjects[] = {
|
||||
{ 0,
|
||||
(void * (*)(void)) null_hash_new_context,
|
||||
(void * (*)(void *)) null_hash_clone_context,
|
||||
(void (*)(void *, PRBool)) null_hash_destroy_context,
|
||||
(void (*)(void *)) null_hash_begin,
|
||||
(void (*)(void *, const unsigned char *, unsigned int)) null_hash_update,
|
||||
(void (*)(void *, unsigned char *, unsigned int *,
|
||||
unsigned int)) null_hash_end
|
||||
},
|
||||
{ MD2_LENGTH,
|
||||
(void * (*)(void)) MD2_NewContext,
|
||||
(void * (*)(void *)) null_hash_clone_context,
|
||||
(void (*)(void *, PRBool)) MD2_DestroyContext,
|
||||
(void (*)(void *)) MD2_Begin,
|
||||
(void (*)(void *, const unsigned char *, unsigned int)) MD2_Update,
|
||||
(void (*)(void *, unsigned char *, unsigned int *, unsigned int)) MD2_End
|
||||
},
|
||||
{ MD5_LENGTH,
|
||||
(void * (*)(void)) MD5_NewContext,
|
||||
(void * (*)(void *)) null_hash_clone_context,
|
||||
(void (*)(void *, PRBool)) MD5_DestroyContext,
|
||||
(void (*)(void *)) MD5_Begin,
|
||||
(void (*)(void *, const unsigned char *, unsigned int)) MD5_Update,
|
||||
(void (*)(void *, unsigned char *, unsigned int *, unsigned int)) MD5_End
|
||||
},
|
||||
{ SHA1_LENGTH,
|
||||
(void * (*)(void)) SHA1_NewContext,
|
||||
(void * (*)(void *)) null_hash_clone_context,
|
||||
(void (*)(void *, PRBool)) SHA1_DestroyContext,
|
||||
(void (*)(void *)) SHA1_Begin,
|
||||
(void (*)(void *, const unsigned char *, unsigned int)) SHA1_Update,
|
||||
(void (*)(void *, unsigned char *, unsigned int *, unsigned int)) SHA1_End
|
||||
},
|
||||
{ SHA256_LENGTH,
|
||||
(void * (*)(void)) SHA256_NewContext,
|
||||
(void * (*)(void *)) null_hash_clone_context,
|
||||
(void (*)(void *, PRBool)) SHA256_DestroyContext,
|
||||
(void (*)(void *)) SHA256_Begin,
|
||||
(void (*)(void *, const unsigned char *, unsigned int)) SHA256_Update,
|
||||
(void (*)(void *, unsigned char *, unsigned int *, unsigned int)) SHA256_End
|
||||
},
|
||||
{ SHA384_LENGTH,
|
||||
(void * (*)(void)) SHA384_NewContext,
|
||||
(void * (*)(void *)) null_hash_clone_context,
|
||||
(void (*)(void *, PRBool)) SHA384_DestroyContext,
|
||||
(void (*)(void *)) SHA384_Begin,
|
||||
(void (*)(void *, const unsigned char *, unsigned int)) SHA384_Update,
|
||||
(void (*)(void *, unsigned char *, unsigned int *, unsigned int)) SHA384_End
|
||||
},
|
||||
{ SHA512_LENGTH,
|
||||
(void * (*)(void)) SHA512_NewContext,
|
||||
(void * (*)(void *)) null_hash_clone_context,
|
||||
(void (*)(void *, PRBool)) SHA512_DestroyContext,
|
||||
(void (*)(void *)) SHA512_Begin,
|
||||
(void (*)(void *, const unsigned char *, unsigned int)) SHA512_Update,
|
||||
(void (*)(void *, unsigned char *, unsigned int *, unsigned int)) SHA512_End
|
||||
},
|
||||
};
|
||||
|
Загрузка…
Ссылка в новой задаче