yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | /* chacha.h |
| 2 | * |
| 3 | * Copyright (C) 2006-2021 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 2 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 * 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 | #ifdef HAVE_INTEL_AVX1 |
| 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 | word32 over[CHACHA_CHUNK_WORDS]; |
| 87 | #endif |
| 88 | } ChaCha; |
| 89 | |
| 90 | /** |
| 91 | * IV(nonce) changes with each record |
| 92 | * counter is for what value the block counter should start ... usually 0 |
| 93 | */ |
| 94 | WOLFSSL_API int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter); |
| 95 | |
| 96 | WOLFSSL_API int wc_Chacha_Process(ChaCha* ctx, byte* cipher, const byte* plain, |
| 97 | word32 msglen); |
| 98 | |
| 99 | WOLFSSL_LOCAL void wc_Chacha_purge_current_block(ChaCha* ctx); |
| 100 | |
| 101 | WOLFSSL_API int wc_Chacha_SetKey(ChaCha* ctx, const byte* key, word32 keySz); |
| 102 | |
| 103 | #ifdef HAVE_XCHACHA |
| 104 | WOLFSSL_API int wc_XChacha_SetKey(ChaCha *ctx, const byte *key, word32 keySz, |
| 105 | const byte *nonce, word32 nonceSz, |
| 106 | word32 counter); |
| 107 | #endif |
| 108 | |
| 109 | #ifdef __cplusplus |
| 110 | } /* extern "C" */ |
| 111 | #endif |
| 112 | |
| 113 | #endif /* HAVE_CHACHA */ |
| 114 | #endif /* WOLF_CRYPT_CHACHA_H */ |
| 115 | |