1 #include "crypto_stream_chacha20.h" 2 #include "private/common.h" 3 #include "private/implementations.h" 4 #include "randombytes.h" 5 #include "runtime.h" 6 #include "stream_chacha20.h" 7 8 #include "ref/chacha20_ref.h" 9 #if defined(HAVE_AVX2INTRIN_H) && defined(HAVE_EMMINTRIN_H) && \ 10 defined(HAVE_TMMINTRIN_H) && defined(HAVE_SMMINTRIN_H) 11 # include "dolbeau/chacha20_dolbeau-avx2.h" 12 #endif 13 #if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) 14 # include "dolbeau/chacha20_dolbeau-ssse3.h" 15 #endif 16 17 static const crypto_stream_chacha20_implementation *implementation = 18 &crypto_stream_chacha20_ref_implementation; 19 20 size_t 21 crypto_stream_chacha20_keybytes(void) { 22 return crypto_stream_chacha20_KEYBYTES; 23 } 24 25 size_t 26 crypto_stream_chacha20_noncebytes(void) { 27 return crypto_stream_chacha20_NONCEBYTES; 28 } 29 30 size_t 31 crypto_stream_chacha20_messagebytes_max(void) 32 { 33 return crypto_stream_chacha20_MESSAGEBYTES_MAX; 34 } 35 36 size_t 37 crypto_stream_chacha20_ietf_keybytes(void) { 38 return crypto_stream_chacha20_ietf_KEYBYTES; 39 } 40 41 size_t 42 crypto_stream_chacha20_ietf_noncebytes(void) { 43 return crypto_stream_chacha20_ietf_NONCEBYTES; 44 } 45 46 size_t 47 crypto_stream_chacha20_ietf_messagebytes_max(void) 48 { 49 return crypto_stream_chacha20_ietf_MESSAGEBYTES_MAX; 50 } 51 52 int 53 crypto_stream_chacha20(unsigned char *c, unsigned long long clen, 54 const unsigned char *n, const unsigned char *k) 55 { 56 return implementation->stream(c, clen, n, k); 57 } 58 59 int 60 crypto_stream_chacha20_ietf(unsigned char *c, unsigned long long clen, 61 const unsigned char *n, const unsigned char *k) 62 { 63 return implementation->stream_ietf(c, clen, n, k); 64 } 65 66 int 67 crypto_stream_chacha20_xor_ic(unsigned char *c, const unsigned char *m, 68 unsigned long long mlen, 69 const unsigned char *n, uint64_t ic, 70 const unsigned char *k) 71 { 72 return implementation->stream_xor_ic(c, m, mlen, n, ic, k); 73 } 74 75 int 76 crypto_stream_chacha20_ietf_xor_ic(unsigned char *c, const unsigned char *m, 77 unsigned long long mlen, 78 const unsigned char *n, uint32_t ic, 79 const unsigned char *k) 80 { 81 return implementation->stream_ietf_xor_ic(c, m, mlen, n, ic, k); 82 } 83 84 int 85 crypto_stream_chacha20_xor(unsigned char *c, const unsigned char *m, 86 unsigned long long mlen, const unsigned char *n, 87 const unsigned char *k) 88 { 89 return implementation->stream_xor_ic(c, m, mlen, n, 0U, k); 90 } 91 92 int 93 crypto_stream_chacha20_ietf_xor(unsigned char *c, const unsigned char *m, 94 unsigned long long mlen, const unsigned char *n, 95 const unsigned char *k) 96 { 97 return implementation->stream_ietf_xor_ic(c, m, mlen, n, 0U, k); 98 } 99 100 void 101 crypto_stream_chacha20_ietf_keygen(unsigned char k[crypto_stream_chacha20_ietf_KEYBYTES]) 102 { 103 randombytes_buf(k, crypto_stream_chacha20_ietf_KEYBYTES); 104 } 105 106 void 107 crypto_stream_chacha20_keygen(unsigned char k[crypto_stream_chacha20_KEYBYTES]) 108 { 109 randombytes_buf(k, crypto_stream_chacha20_KEYBYTES); 110 } 111 112 int 113 _crypto_stream_chacha20_pick_best_implementation(void) 114 { 115 implementation = &crypto_stream_chacha20_ref_implementation; 116 #if defined(HAVE_AVX2INTRIN_H) && defined(HAVE_EMMINTRIN_H) && \ 117 defined(HAVE_TMMINTRIN_H) && defined(HAVE_SMMINTRIN_H) 118 if (sodium_runtime_has_avx2()) { 119 implementation = &crypto_stream_chacha20_dolbeau_avx2_implementation; 120 return 0; 121 } 122 #endif 123 #if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) 124 if (sodium_runtime_has_ssse3()) { 125 implementation = &crypto_stream_chacha20_dolbeau_ssse3_implementation; 126 return 0; 127 } 128 #endif 129 return 0; 130 } 131