tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

rawhash.c (8444B)


      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 "nspr.h"
     10 #include "hasht.h"
     11 #include "blapi.h" /* below the line */
     12 #include "secerr.h"
     13 
     14 #define RawHashBase(ctxtype, mmm)                                                        \
     15    static void *                                                                        \
     16        RawHash_##mmm##_NewContext(void)                                                 \
     17    {                                                                                    \
     18        ctxtype *ctx = mmm##_NewContext();                                               \
     19        return ctx;                                                                      \
     20    }                                                                                    \
     21    static void                                                                          \
     22        RawHash_##mmm##_Begin(void *vctx)                                                \
     23    {                                                                                    \
     24        ctxtype *ctx = vctx;                                                             \
     25        mmm##_Begin(ctx);                                                                \
     26    }                                                                                    \
     27    static void                                                                          \
     28        RawHash_##mmm##_Update(void *vctx, const unsigned char *input, unsigned int len) \
     29    {                                                                                    \
     30        ctxtype *ctx = vctx;                                                             \
     31        mmm##_Update(ctx, input, len);                                                   \
     32    }                                                                                    \
     33    static void                                                                          \
     34        RawHash_##mmm##_End(void *vctx, unsigned char *digest,                           \
     35                            unsigned int *len, unsigned int maxLen)                      \
     36    {                                                                                    \
     37        ctxtype *ctx = vctx;                                                             \
     38        mmm##_End(ctx, digest, len, maxLen);                                             \
     39    }                                                                                    \
     40    static void                                                                          \
     41        RawHash_##mmm##_DestroyContext(void *vctx, PRBool freeit)                        \
     42    {                                                                                    \
     43        ctxtype *ctx = vctx;                                                             \
     44        mmm##_DestroyContext(ctx, freeit);                                               \
     45    }
     46 
     47 RawHashBase(MD2Context, MD2);
     48 RawHashBase(MD5Context, MD5);
     49 RawHashBase(SHA1Context, SHA1);
     50 RawHashBase(SHA224Context, SHA224);
     51 RawHashBase(SHA256Context, SHA256);
     52 RawHashBase(SHA384Context, SHA384);
     53 RawHashBase(SHA512Context, SHA512);
     54 RawHashBase(SHA3_224Context, SHA3_224);
     55 RawHashBase(SHA3_256Context, SHA3_256);
     56 RawHashBase(SHA3_384Context, SHA3_384);
     57 RawHashBase(SHA3_512Context, SHA3_512);
     58 
     59 #define RawHashExtra(ctxtype, mmm)                                     \
     60    static void                                                        \
     61        RawHash_##mmm##_EndRaw(void *vctx, unsigned char *digest,      \
     62                               unsigned int *len, unsigned int maxLen) \
     63    {                                                                  \
     64        ctxtype *ctx = vctx;                                           \
     65        mmm##_EndRaw(ctx, digest, len, maxLen);                        \
     66    }
     67 
     68 RawHashExtra(MD5Context, MD5);
     69 RawHashExtra(SHA1Context, SHA1);
     70 RawHashExtra(SHA224Context, SHA224);
     71 RawHashExtra(SHA256Context, SHA256);
     72 RawHashExtra(SHA384Context, SHA384);
     73 RawHashExtra(SHA512Context, SHA512);
     74 
     75 static void *
     76 null_hash_new_context(void)
     77 {
     78    return NULL;
     79 }
     80 
     81 static void *
     82 null_hash_clone_context(void *v)
     83 {
     84    PORT_Assert(v == NULL);
     85    return NULL;
     86 }
     87 
     88 static void
     89 null_hash_begin(void *v)
     90 {
     91 }
     92 
     93 static void
     94 null_hash_update(void *v, const unsigned char *input, unsigned int length)
     95 {
     96 }
     97 
     98 static void
     99 null_hash_end(void *v, unsigned char *output, unsigned int *outLen,
    100              unsigned int maxOut)
    101 {
    102    *outLen = 0;
    103 }
    104 
    105 static void
    106 null_hash_destroy_context(void *v, PRBool b)
    107 {
    108    PORT_Assert(v == NULL);
    109 }
    110 
    111 const SECHashObject SECRawHashObjects[] = {
    112    { 0,
    113      null_hash_new_context,
    114      null_hash_clone_context,
    115      null_hash_destroy_context,
    116      null_hash_begin,
    117      null_hash_update,
    118      null_hash_end,
    119      0,
    120      HASH_AlgNULL,
    121      null_hash_end },
    122    {
    123        MD2_LENGTH,
    124        RawHash_MD2_NewContext,
    125        null_hash_clone_context,
    126        RawHash_MD2_DestroyContext,
    127        RawHash_MD2_Begin,
    128        RawHash_MD2_Update,
    129        RawHash_MD2_End,
    130        MD2_BLOCK_LENGTH,
    131        HASH_AlgMD2,
    132        NULL /* end_raw */
    133    },
    134    { MD5_LENGTH,
    135      RawHash_MD5_NewContext,
    136      null_hash_clone_context,
    137      RawHash_MD5_DestroyContext,
    138      RawHash_MD5_Begin,
    139      RawHash_MD5_Update,
    140      RawHash_MD5_End,
    141      MD5_BLOCK_LENGTH,
    142      HASH_AlgMD5,
    143      RawHash_MD5_EndRaw },
    144    { SHA1_LENGTH,
    145      RawHash_SHA1_NewContext,
    146      null_hash_clone_context,
    147      RawHash_SHA1_DestroyContext,
    148      RawHash_SHA1_Begin,
    149      RawHash_SHA1_Update,
    150      RawHash_SHA1_End,
    151      SHA1_BLOCK_LENGTH,
    152      HASH_AlgSHA1,
    153      RawHash_SHA1_EndRaw },
    154    { SHA256_LENGTH,
    155      RawHash_SHA256_NewContext,
    156      null_hash_clone_context,
    157      RawHash_SHA256_DestroyContext,
    158      RawHash_SHA256_Begin,
    159      RawHash_SHA256_Update,
    160      RawHash_SHA256_End,
    161      SHA256_BLOCK_LENGTH,
    162      HASH_AlgSHA256,
    163      RawHash_SHA256_EndRaw },
    164    { SHA384_LENGTH,
    165      RawHash_SHA384_NewContext,
    166      null_hash_clone_context,
    167      RawHash_SHA384_DestroyContext,
    168      RawHash_SHA384_Begin,
    169      RawHash_SHA384_Update,
    170      RawHash_SHA384_End,
    171      SHA384_BLOCK_LENGTH,
    172      HASH_AlgSHA384,
    173      RawHash_SHA384_EndRaw },
    174    { SHA512_LENGTH,
    175      RawHash_SHA512_NewContext,
    176      null_hash_clone_context,
    177      RawHash_SHA512_DestroyContext,
    178      RawHash_SHA512_Begin,
    179      RawHash_SHA512_Update,
    180      RawHash_SHA512_End,
    181      SHA512_BLOCK_LENGTH,
    182      HASH_AlgSHA512,
    183      RawHash_SHA512_EndRaw },
    184    { SHA224_LENGTH,
    185      RawHash_SHA224_NewContext,
    186      null_hash_clone_context,
    187      RawHash_SHA224_DestroyContext,
    188      RawHash_SHA224_Begin,
    189      RawHash_SHA224_Update,
    190      RawHash_SHA224_End,
    191      SHA224_BLOCK_LENGTH,
    192      HASH_AlgSHA224,
    193      RawHash_SHA224_EndRaw },
    194    { SHA3_224_LENGTH,
    195      RawHash_SHA3_224_NewContext,
    196      null_hash_clone_context,
    197      RawHash_SHA3_224_DestroyContext,
    198      RawHash_SHA3_224_Begin,
    199      RawHash_SHA3_224_Update,
    200      RawHash_SHA3_224_End,
    201      SHA3_224_BLOCK_LENGTH,
    202      HASH_AlgSHA3_224,
    203      NULL },
    204    { SHA3_256_LENGTH,
    205      RawHash_SHA3_256_NewContext,
    206      null_hash_clone_context,
    207      RawHash_SHA3_256_DestroyContext,
    208      RawHash_SHA3_256_Begin,
    209      RawHash_SHA3_256_Update,
    210      RawHash_SHA3_256_End,
    211      SHA3_256_BLOCK_LENGTH,
    212      HASH_AlgSHA3_256,
    213      NULL },
    214    { SHA3_384_LENGTH,
    215      RawHash_SHA3_384_NewContext,
    216      null_hash_clone_context,
    217      RawHash_SHA3_384_DestroyContext,
    218      RawHash_SHA3_384_Begin,
    219      RawHash_SHA3_384_Update,
    220      RawHash_SHA3_384_End,
    221      SHA3_384_BLOCK_LENGTH,
    222      HASH_AlgSHA3_384,
    223      NULL },
    224    { SHA3_512_LENGTH,
    225      RawHash_SHA3_512_NewContext,
    226      null_hash_clone_context,
    227      RawHash_SHA3_512_DestroyContext,
    228      RawHash_SHA3_512_Begin,
    229      RawHash_SHA3_512_Update,
    230      RawHash_SHA3_512_End,
    231      SHA3_512_BLOCK_LENGTH,
    232      HASH_AlgSHA3_512,
    233      NULL },
    234 };
    235 
    236 const SECHashObject *
    237 HASH_GetRawHashObject(HASH_HashType hashType)
    238 {
    239    if (hashType <= HASH_AlgNULL || hashType >= HASH_AlgTOTAL) {
    240        PORT_SetError(SEC_ERROR_INVALID_ARGS);
    241        return NULL;
    242    }
    243    return &SECRawHashObjects[hashType];
    244 }