tor-browser

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

shake.c (3173B)


      1 #ifdef FREEBL_NO_DEPEND
      2 #include "stubs.h"
      3 #endif
      4 
      5 #include "prtypes.h" /* for PRUintXX */
      6 #include "secport.h" /* for PORT_XXX */
      7 #include "blapi.h"
      8 #include "blapii.h"
      9 #include "blapit.h"
     10 #include "secerr.h"
     11 #include "Hacl_Hash_SHA3.h"
     12 
     13 struct SHAKEContextStr {
     14    Hacl_Streaming_Keccak_state *st;
     15 };
     16 
     17 SHAKE_128Context *
     18 SHAKE_128_NewContext()
     19 {
     20    SHAKE_128Context *ctx = PORT_New(SHAKE_128Context);
     21    ctx->st = Hacl_Streaming_Keccak_malloc(Spec_Hash_Definitions_Shake128);
     22    return ctx;
     23 }
     24 
     25 SHAKE_256Context *
     26 SHAKE_256_NewContext()
     27 {
     28    SHAKE_256Context *ctx = PORT_New(SHAKE_256Context);
     29    ctx->st = Hacl_Streaming_Keccak_malloc(Spec_Hash_Definitions_Shake256);
     30    return ctx;
     31 }
     32 
     33 void
     34 SHAKE_128_DestroyContext(SHAKE_128Context *ctx, PRBool freeit)
     35 {
     36    Hacl_Streaming_Keccak_reset(ctx->st);
     37    if (freeit) {
     38        Hacl_Streaming_Keccak_free(ctx->st);
     39        PORT_Free(ctx);
     40    }
     41 }
     42 
     43 void
     44 SHAKE_256_DestroyContext(SHAKE_256Context *ctx, PRBool freeit)
     45 {
     46    Hacl_Streaming_Keccak_reset(ctx->st);
     47    if (freeit) {
     48        Hacl_Streaming_Keccak_free(ctx->st);
     49        PORT_Free(ctx);
     50    }
     51 }
     52 
     53 void
     54 SHAKE_128_Begin(SHAKE_128Context *ctx)
     55 {
     56    Hacl_Streaming_Keccak_reset(ctx->st);
     57 }
     58 
     59 void
     60 SHAKE_256_Begin(SHAKE_256Context *ctx)
     61 {
     62    Hacl_Streaming_Keccak_reset(ctx->st);
     63 }
     64 
     65 void
     66 SHAKE_128_Absorb(SHAKE_128Context *ctx, const unsigned char *input,
     67                 unsigned int inputLen)
     68 {
     69    Hacl_Streaming_Keccak_update(ctx->st, (uint8_t *)input, inputLen);
     70 }
     71 
     72 void
     73 SHAKE_256_Absorb(SHAKE_256Context *ctx, const unsigned char *input,
     74                 unsigned int inputLen)
     75 {
     76    Hacl_Streaming_Keccak_update(ctx->st, (uint8_t *)input, inputLen);
     77 }
     78 
     79 void
     80 SHAKE_128_SqueezeEnd(SHAKE_128Context *ctx, unsigned char *digest,
     81                     unsigned int digestLen)
     82 {
     83    Hacl_Streaming_Keccak_squeeze(ctx->st, digest, digestLen);
     84 }
     85 
     86 void
     87 SHAKE_256_SqueezeEnd(SHAKE_256Context *ctx, unsigned char *digest,
     88                     unsigned int digestLen)
     89 {
     90    Hacl_Streaming_Keccak_squeeze(ctx->st, digest, digestLen);
     91 }
     92 
     93 SECStatus
     94 SHAKE_128_HashBuf(unsigned char *dest, PRUint32 dest_length,
     95                  const unsigned char *src, PRUint32 src_length)
     96 {
     97    SHAKE_128Context *ctx = SHAKE_128_NewContext();
     98    SHAKE_128_Begin(ctx);
     99    SHAKE_128_Absorb(ctx, src, src_length);
    100    SHAKE_128_SqueezeEnd(ctx, dest, dest_length);
    101    SHAKE_128_DestroyContext(ctx, true);
    102    return SECSuccess;
    103 }
    104 
    105 SECStatus
    106 SHAKE_256_HashBuf(unsigned char *dest, PRUint32 dest_length,
    107                  const unsigned char *src, PRUint32 src_length)
    108 {
    109    SHAKE_256Context *ctx = SHAKE_256_NewContext();
    110    SHAKE_256_Begin(ctx);
    111    SHAKE_256_Absorb(ctx, src, src_length);
    112    SHAKE_256_SqueezeEnd(ctx, dest, dest_length);
    113    SHAKE_256_DestroyContext(ctx, true);
    114    return SECSuccess;
    115 }
    116 
    117 SECStatus
    118 SHAKE_128_Hash(unsigned char *dest, unsigned int dest_length, const char *src)
    119 {
    120    return SHAKE_128_HashBuf(dest, dest_length, (const unsigned char *)src, PORT_Strlen(src));
    121 }
    122 
    123 SECStatus
    124 SHAKE_256_Hash(unsigned char *dest, unsigned int dest_length, const char *src)
    125 {
    126    return SHAKE_256_HashBuf(dest, dest_length, (const unsigned char *)src, PORT_Strlen(src));
    127 }