gnunet-android

GNUnet for Android
Log | Files | Refs | README

chacha.h (3580B)


      1 /* chacha.h
      2  *
      3  * Copyright (C) 2006-2025 wolfSSL Inc.
      4  *
      5  * This file is part of wolfSSL.
      6  *
      7  * wolfSSL is free software; you can redistribute it and/or modify
      8  * it under the terms of the GNU General Public License as published by
      9  * the Free Software Foundation; either version 3 of the License, or
     10  * (at your option) any later version.
     11  *
     12  * wolfSSL is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  * GNU General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU General Public License
     18  * along with this program; if not, write to the Free Software
     19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
     20  */
     21 /*
     22 
     23 DESCRIPTION
     24 This library contains implementation for the ChaCha20 stream cipher.
     25 
     26 */
     27 /*!
     28     \file wolfssl/wolfcrypt/chacha.h
     29 */
     30 
     31 
     32 #ifndef WOLF_CRYPT_CHACHA_H
     33 #define WOLF_CRYPT_CHACHA_H
     34 
     35 #include <wolfssl/wolfcrypt/types.h>
     36 
     37 #ifdef HAVE_CHACHA
     38 
     39 #ifdef __cplusplus
     40     extern "C" {
     41 #endif
     42 
     43 /*
     44 Initialization vector starts at 13 with zero being the index origin of a matrix.
     45 Block counter is located at index 12.
     46   0   1   2   3
     47   4   5   6   7
     48   8   9   10  11
     49   12  13  14  15
     50 */
     51 #define CHACHA_MATRIX_CNT_IV 12
     52 
     53 /* Size of the IV */
     54 #define CHACHA_IV_WORDS    3
     55 
     56 /* Size of IV in bytes*/
     57 #define CHACHA_IV_BYTES 12
     58 #ifdef HAVE_XCHACHA
     59 #define XCHACHA_NONCE_BYTES 24
     60 #endif
     61 
     62 /* Size of ChaCha chunks */
     63 #define CHACHA_CHUNK_WORDS 16
     64 #define CHACHA_CHUNK_BYTES (CHACHA_CHUNK_WORDS * (word32)sizeof(word32))
     65 
     66 #ifdef WOLFSSL_X86_64_BUILD
     67 #if defined(USE_INTEL_SPEEDUP) && !defined(NO_CHACHA_ASM)
     68     #define USE_INTEL_CHACHA_SPEEDUP
     69     #define HAVE_INTEL_AVX1
     70 #endif
     71 #endif
     72 
     73 enum {
     74     CHACHA_ENC_TYPE = WC_CIPHER_CHACHA,    /* cipher unique type */
     75     CHACHA_MAX_KEY_SZ = 32
     76 };
     77 
     78 typedef struct ChaCha {
     79     word32 X[CHACHA_CHUNK_WORDS];           /* state of cipher */
     80 #if defined(USE_INTEL_CHACHA_SPEEDUP)
     81     /* vpshufd reads 16 bytes but we only use bottom 4. */
     82     byte extra[12];
     83 #endif
     84     word32 left;                            /* number of bytes leftover */
     85 #if defined(USE_INTEL_CHACHA_SPEEDUP) || defined(WOLFSSL_ARMASM) || \
     86     defined(WOLFSSL_RISCV_ASM)
     87     word32 over[CHACHA_CHUNK_WORDS];
     88 #endif
     89 } ChaCha;
     90 
     91 /**
     92   * IV(nonce) changes with each record
     93   * counter is for what value the block counter should start ... usually 0
     94   */
     95 WOLFSSL_API int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter);
     96 
     97 WOLFSSL_API int wc_Chacha_Process(ChaCha* ctx, byte* cipher, const byte* plain,
     98                               word32 msglen);
     99 
    100 WOLFSSL_API int wc_Chacha_SetKey(ChaCha* ctx, const byte* key, word32 keySz);
    101 
    102 #ifdef HAVE_XCHACHA
    103 WOLFSSL_LOCAL void wc_Chacha_purge_current_block(ChaCha* ctx);
    104 
    105 WOLFSSL_API int wc_XChacha_SetKey(ChaCha *ctx, const byte *key, word32 keySz,
    106                                   const byte *nonce, word32 nonceSz,
    107                                   word32 counter);
    108 #endif
    109 
    110 #if defined(WOLFSSL_ARMASM)
    111 
    112 #ifndef __aarch64__
    113 void wc_chacha_setiv(word32* x, const byte* iv, word32 counter);
    114 void wc_chacha_setkey(word32* x, const byte* key, word32 keySz);
    115 #endif
    116 
    117 #if defined(WOLFSSL_ARMASM_NO_NEON) || defined(WOLFSSL_ARMASM_THUMB2)
    118 void wc_chacha_use_over(byte* over, byte* output, const byte* input,
    119     word32 len);
    120 void wc_chacha_crypt_bytes(ChaCha* ctx, byte* c, const byte* m, word32 len);
    121 #endif
    122 
    123 #endif
    124 
    125 
    126 #ifdef __cplusplus
    127     } /* extern "C" */
    128 #endif
    129 
    130 #endif /* HAVE_CHACHA */
    131 #endif /* WOLF_CRYPT_CHACHA_H */
    132