sechash.c
上传用户:lyxiangda
上传日期:2007-01-12
资源大小:3042k
文件大小:6k
源码类别:

CA认证

开发平台:

WINDOWS

  1. /*
  2.  * The contents of this file are subject to the Mozilla Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/MPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * The Original Code is the Netscape security libraries.
  13.  * 
  14.  * The Initial Developer of the Original Code is Netscape
  15.  * Communications Corporation.  Portions created by Netscape are 
  16.  * Copyright (C) 1994-2000 Netscape Communications Corporation.  All
  17.  * Rights Reserved.
  18.  * 
  19.  * Contributor(s):
  20.  * 
  21.  * Alternatively, the contents of this file may be used under the
  22.  * terms of the GNU General Public License Version 2 or later (the
  23.  * "GPL"), in which case the provisions of the GPL are applicable 
  24.  * instead of those above.  If you wish to allow use of your 
  25.  * version of this file only under the terms of the GPL and not to
  26.  * allow others to use your version of this file under the MPL,
  27.  * indicate your decision by deleting the provisions above and
  28.  * replace them with the notice and other provisions required by
  29.  * the GPL.  If you do not delete the provisions above, a recipient
  30.  * may use your version of this file under either the MPL or the
  31.  * GPL.
  32.  */
  33. #include "sechash.h"
  34. #include "secoidt.h"
  35. #include "blapi.h"
  36. #include "pk11func.h" /* for the PK11_ calls below. */
  37. static void *
  38. null_hash_new_context(void)
  39. {
  40.     return NULL;
  41. }
  42. static void *
  43. null_hash_clone_context(void *v)
  44. {
  45.     PORT_Assert(v == NULL);
  46.     return NULL;
  47. }
  48. static void
  49. null_hash_begin(void *v)
  50. {
  51. }
  52. static void
  53. null_hash_update(void *v, const unsigned char *input, unsigned int length)
  54. {
  55. }
  56. static void
  57. null_hash_end(void *v, unsigned char *output, unsigned int *outLen,
  58.       unsigned int maxOut)
  59. {
  60.     *outLen = 0;
  61. }
  62. static void
  63. null_hash_destroy_context(void *v, PRBool b)
  64. {
  65.     PORT_Assert(v == NULL);
  66. }
  67. static void *
  68. md2_NewContext(void) {
  69. return (void *) PK11_CreateDigestContext(SEC_OID_MD2);
  70. }
  71. static void *
  72. md5_NewContext(void) {
  73. return (void *) PK11_CreateDigestContext(SEC_OID_MD5);
  74. }
  75. static void *
  76. sha1_NewContext(void) {
  77. return (void *) PK11_CreateDigestContext(SEC_OID_SHA1);
  78. }
  79. SECHashObject SECHashObjects[] = {
  80.   { 0,
  81.     (void * (*)(void)) null_hash_new_context,
  82.     (void * (*)(void *)) null_hash_clone_context,
  83.     (void (*)(void *, PRBool)) null_hash_destroy_context,
  84.     (void (*)(void *)) null_hash_begin,
  85.     (void (*)(void *, const unsigned char *, unsigned int)) null_hash_update,
  86.     (void (*)(void *, unsigned char *, unsigned int *,
  87.       unsigned int)) null_hash_end
  88.   },
  89.   { MD2_LENGTH,
  90.     (void * (*)(void)) md2_NewContext,
  91.     (void * (*)(void *)) PK11_CloneContext,
  92.     (void (*)(void *, PRBool)) PK11_DestroyContext,
  93.     (void (*)(void *)) PK11_DigestBegin,
  94.     (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp,
  95.     (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) 
  96. PK11_DigestFinal
  97.   },
  98.   { MD5_LENGTH,
  99.     (void * (*)(void)) md5_NewContext,
  100.     (void * (*)(void *)) PK11_CloneContext,
  101.     (void (*)(void *, PRBool)) PK11_DestroyContext,
  102.     (void (*)(void *)) PK11_DigestBegin,
  103.     (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp,
  104.     (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) 
  105. PK11_DigestFinal
  106.   },
  107.   { SHA1_LENGTH,
  108.     (void * (*)(void)) sha1_NewContext,
  109.     (void * (*)(void *)) PK11_CloneContext,
  110.     (void (*)(void *, PRBool)) PK11_DestroyContext,
  111.     (void (*)(void *)) PK11_DigestBegin,
  112.     (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp,
  113.     (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) 
  114. PK11_DigestFinal
  115.   },
  116. };
  117. unsigned int
  118. HASH_ResultLen(HASH_HashType type)
  119. {
  120.     if ( ( type < HASH_AlgNULL ) || ( type >= HASH_AlgTOTAL ) ) {
  121. return(0);
  122.     }
  123.     
  124.     return(SECHashObjects[type].length);
  125. }
  126. unsigned int
  127. HASH_ResultLenContext(HASHContext *context)
  128. {
  129.     return(context->hashobj->length);
  130. }
  131. SECStatus
  132. HASH_HashBuf(HASH_HashType type,
  133.      unsigned char *dest,
  134.      unsigned char *src,
  135.      uint32 src_len)
  136. {
  137.     HASHContext *cx;
  138.     unsigned int part;
  139.     
  140.     if ( ( type < HASH_AlgNULL ) || ( type >= HASH_AlgTOTAL ) ) {
  141. return(SECFailure);
  142.     }
  143.     
  144.     cx = HASH_Create(type);
  145.     if ( cx == NULL ) {
  146. return(SECFailure);
  147.     }
  148.     HASH_Begin(cx);
  149.     HASH_Update(cx, src, src_len);
  150.     HASH_End(cx, dest, &part, HASH_ResultLenContext(cx));
  151.     HASH_Destroy(cx);
  152.     return(SECSuccess);
  153. }
  154. HASHContext *
  155. HASH_Create(HASH_HashType type)
  156. {
  157.     void *hash_context = NULL;
  158.     HASHContext *ret = NULL;
  159.     
  160.     if ( ( type < HASH_AlgNULL ) || ( type >= HASH_AlgTOTAL ) ) {
  161. return(NULL);
  162.     }
  163.     
  164.     hash_context = (* SECHashObjects[type].create)();
  165.     if ( hash_context == NULL ) {
  166. goto loser;
  167.     }
  168.     ret = (HASHContext *)PORT_Alloc(sizeof(HASHContext));
  169.     if ( ret == NULL ) {
  170. goto loser;
  171.     }
  172.     ret->hash_context = hash_context;
  173.     ret->hashobj = &SECHashObjects[type];
  174.     
  175.     return(ret);
  176.     
  177. loser:
  178.     if ( hash_context != NULL ) {
  179. (* SECHashObjects[type].destroy)(hash_context, PR_TRUE);
  180.     }
  181.     
  182.     return(NULL);
  183. }
  184. HASHContext *
  185. HASH_Clone(HASHContext *context)
  186. {
  187.     void *hash_context = NULL;
  188.     HASHContext *ret = NULL;
  189.     
  190.     hash_context = (* context->hashobj->clone)(context->hash_context);
  191.     if ( hash_context == NULL ) {
  192. goto loser;
  193.     }
  194.     ret = (HASHContext *)PORT_Alloc(sizeof(HASHContext));
  195.     if ( ret == NULL ) {
  196. goto loser;
  197.     }
  198.     ret->hash_context = hash_context;
  199.     ret->hashobj = context->hashobj;
  200.     
  201.     return(ret);
  202.     
  203. loser:
  204.     if ( hash_context != NULL ) {
  205. (* context->hashobj->destroy)(hash_context, PR_TRUE);
  206.     }
  207.     
  208.     return(NULL);
  209. }
  210. void
  211. HASH_Destroy(HASHContext *context)
  212. {
  213.     (* context->hashobj->destroy)(context->hash_context, PR_TRUE);
  214.     PORT_Free(context);
  215.     return;
  216. }
  217. void
  218. HASH_Begin(HASHContext *context)
  219. {
  220.     (* context->hashobj->begin)(context->hash_context);
  221.     return;
  222. }
  223. void
  224. HASH_Update(HASHContext *context,
  225.     const unsigned char *src,
  226.     unsigned int len)
  227. {
  228.     (* context->hashobj->update)(context->hash_context, src, len);
  229.     return;
  230. }
  231. void
  232. HASH_End(HASHContext *context,
  233.  unsigned char *result,
  234.  unsigned int *result_len,
  235.  unsigned int max_result_len)
  236. {
  237.     (* context->hashobj->end)(context->hash_context, result, result_len,
  238.       max_result_len);
  239.     return;
  240. }