Skip to content

About

A lightweight, portable C library implementing ChaCha20, XChaCha20, ChaCha20-Poly1305, XChaCha20-Poly1305, IETF & Bernstein variants, N rounds, BLAKE2s/b, BLAKE3.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

ChaChaSlide

A lightweight, portable C library implementing ChaCha20, XChaCha20, ChaCha20-Poly1305, XChaCha20-Poly1305, IETF & Bernstein variants, N rounds, BLAKE2s/b, BLAKE3.

The library is optimized for code size while matching or beating throughput of comparable crypto libraries at -O3. For reference, ChaCha20-Poly1305 compiles to just 2.2KB at -Os and ~5.1KB at -O3 on Windows.

Table of Contents

Features

Features can be toggled in ccs_config.h.

Category Feature
Stream Ciphers IETF ChaCha20 (96-bit nonce, 32-bit counter) [1]
D.J. Bernstein ChaCha20 (64-bit nonce, 64-bit counter) [2]
XChaCha20 (192-bit nonce, 64-bit counter) [3]
AEAD Modes IETF ChaCha20-Poly1305 [1]
D.J. Bernstein ChaCha20-Poly1305 [4] (compatible with LibSodium)
XChaCha20-Poly1305 [3], [4]
One Time Authenticators Poly1305 [1]
Hash Functions BLAKE2s [5]
BLAKE2b [5]
BLAKE3 [6]

All (X)ChaCha and (X)ChaCha-Poly1305 functions support custom number of rounds through their ChaChaN APIs, allowing for ChaCha12 or ChaCha8. ChaChaN supports custom counter start.

Getting Started

Code Examples

CMake

  1. Add this project using add_subdirectory(path/to/ChaChaSlide) (CMake 3.16+, GCC/Clang).
  2. Link your application to either ccs-size or ccs-speed using target_link_libraries().
  3. Configure include/ccs_config.h for the features you want to keep.
  4. See /example for usage.

1 Tests and benchmarks are off by default. Enable BUILD_TESTING for tests or BUILD_BENCHMARKS for the benchmark runner.

2 Library level LTO is enabled when supported. Set CMAKE_INTERPROCEDURAL_OPTIMIZATION=OFF to disable it.

BLAKE3 Memory Configuration (If using BLAKE3)

Details

Set CCS_B3_MAX in include/ccs_config.h to choose the maximum message size BLAKE3 can hash: 2^CCS_B3_MAX bytes. This determines the size of ccs_blake3_ctx_t. The default is 64.

CCS_B3_MAX Maximum message size sizeof(ccs_blake3_ctx_t)
32 2^32 bytes (4 GiB) 844 bytes
40 2^40 bytes (1 TiB) 1,100 bytes
48 2^48 bytes (256 TiB) 1,356 bytes
56 2^56 bytes (64 PiB) 1,612 bytes
64 2^64 bytes (16 EiB) 1,868 bytes

Code Size

Bytes per feature, including dependencies. Rows are not additive due to feature overlap.1

Feature-Os-O3
Windows x86-64Linux x86-64Cortex-M0+2Cortex-M32AArch64AVR ATmega328P2Windows x86-64Linux x86-64Cortex-M0+2Cortex-M32AArch64AVR ATmega328P2
ChaCha20 IETF5605045164646281,5181,7121,4591,5681,3041,5485,756
ChaCha20 DJB5444955164766121,7681,7121,5551,5761,2921,4886,204
XChaCha207847667646768682,2422,5122,3232,3401,9802,32010,180
ChaCha20-Poly1305 IETF2,1761,9572,1761,8242,1844,0565,0724,7985,6244,2684,20015,266
ChaCha20-Poly1305 DJB2,0641,8342,1441,7922,1044,1004,9764,4785,5164,2603,99215,178
XChaCha20-Poly13052,4162,2142,4922,0642,5004,6285,7445,5986,7964,9805,05219,586
Poly13051,0248831,2628981,0041,8522,7362,3353,4402,6722,0207,862
BLAKE2s1,1521,0671,0428481,2362,0305,4405,5936,3725,3565,82026,868
BLAKE2b1,3121,2201,5261,2761,3243,7727,2487,32122,45616,9766,952-3
BLAKE341,9201,8621,5281,3741,9323,5286,3686,5467,0445,3566,04025,500
HChaCha204964344604165561,2387046661,0167128363,582
Default suite58,1607,7127,4806,4448,54016,26828,51228,28645,68035,22027,516231,2503

1 Full-API linked code, constants, and initialized data. Excludes runtime startup, RAM, and debug metadata. GCC 16.1 (Windows), 13.4 (Cortex-M), 14.2 (others). Uses LTO within the library.

2 CCS_POLY=2 on Cortex-M, 3 on AVR, 1 elsewhere.

3 Exceeds the ATmega328P's 32 KiB flash.

4 BLAKE3 includes hash, keyed hash, KDF, and XOF.

5 Default suite includes all features.

Benchmarks

Ryzen 7 9800X3D

Windows 11 x86-64. GCC 16.1. LTO. MiB/s.

Operation1 MiB64 KiB1 KiB16 B
-O3-Os-O3-Os-O3-Os-O3-Os
ChaCha20 IETF1,244.1474.11,244.3473.81,154.3450.4137.279.1
ChaCha20 DJB1,238.6473.11,239.0472.71,140.8450.2136.278.6
XChaCha201,238.4472.91,238.7469.71,061.5422.086.045.3
ChaCha20-Poly1305 encrypt926.0414.5925.7413.5798.5360.970.736.5
ChaCha20-Poly1305 DJB encrypt924.1412.6922.9410.2790.7359.768.936.6
XChaCha20-Poly1305 encrypt924.0414.0922.1411.9756.4345.153.727.5
Poly13053,625.03,318.43,615.43,315.82,915.12,491.2221.0161.2
BLAKE2s-2561,026.9408.41,023.4409.0938.1388.6108.357.2
BLAKE2b-5121,739.2665.71,734.9666.11,365.8573.882.539.1
BLAKE3-2561,289.2514.51,290.6515.11,188.0501.6101.652.2

STM32C031

Inexpensive barebones ARM Cortex-M0+ microcontroller.

Pending

ATMEGA328P

Legacy 8-bit Microcontroller on an Arduino Uno.

Pending

Comparisons

Monocypher 4.0.3 · libsodium 1.0.22 · wolfCrypt 5.9.4.

Handwritten SIMD/assembly backends disabled for raw portable comparison.

Windows 11 x86-64. GCC 16.1. LTO. MiB/s.

Code Size

Added bytes when statically linked, excluding the common baseline.1

Requested operations-Os-O3
CCSMonocypherlibsodiumwolfCryptCCSMonocypherlibsodiumwolfCrypt
ChaCha20 IETF4641,3563,1681,1721,5403,9204,5763,940
ChaCha20 DJB4481,2883,184-1,6363,8564,576-
XChaCha206721,5283,7282,1402,3724,1765,1206,820
ChaCha20-Poly1305 IETF2,1963,4365,8364,0005,0367,65210,64411,528
ChaCha20-Poly1305 DJB2,084-5,660-4,976-10,468-
XChaCha20-Poly13052,4563,4806,4124,9645,8767,89611,34812,620
Poly13058801,0682,9761,4802,3761,7206,0802,552
BLAKE2s1,288--8,0166,552--14,304
BLAKE2b1,4246,8768,41610,1048,5048,7769,50412,632
BLAKE31,868---5,468---
Common suite23,80410,46813,02014,35613,82416,85218,52023,644

"-" = Unsupported

1 AEAD includes encrypt/decrypt; BLAKE2 and BLAKE3 include keyed/unkeyed hashing.

2 Common suite: IETF ChaCha20, IETF AEAD, Poly1305, and BLAKE2b.

Comparison Benchmarks

Handwritten SIMD and assembly disabled for raw comparison. Includes key setup and finalization. AEAD uses empty AD.

Notably, CCS performs secret zeroing, the others do not.

-Os, MiB/s

Operation1 MiB64 KiB16 B
CCSMonocypherlibsodiumwolfCryptCCSMonocypherlibsodiumwolfCryptCCSMonocypherlibsodiumwolfCrypt
ChaCha20 IETF472.7937.11,168.6989.9471.7956.11,175.61,002.6108.9131.8196.7227.0
ChaCha20 DJB471.9942.31,171.4-469.9956.81,177.7-108.6131.5198.1-
XChaCha20472.3932.01,171.3982.5466.8954.01,176.2997.353.475.8121.873.6
ChaCha20-Poly1305 encrypt412.8729.6868.7760.4409.0740.2876.3762.842.553.258.461.8
ChaCha20-Poly1305 decrypt413.0723.9870.6757.6409.2737.6875.3762.341.751.053.559.9
ChaCha20-Poly1305 DJB encrypt413.1-871.6-408.7-876.6-41.3-56.9-
ChaCha20-Poly1305 DJB decrypt412.7-872.6-408.3-873.7-40.7-53.9-
XChaCha20-Poly1305 encrypt413.2736.4867.2761.4408.5739.1874.4768.330.641.448.634.6
XChaCha20-Poly1305 decrypt412.6732.4873.1763.1408.5736.9875.8770.030.339.945.033.9
Poly13053,301.23,289.53,445.53,200.13,299.73,299.23,443.13,234.8383.5421.2524.4882.9
BLAKE2s-256408.4--632.9408.6--644.970.8--119.8
BLAKE2s-256 keyed408.0--638.3408.5--644.641.9--64.8
BLAKE2b-512665.41,273.91,387.91,003.6664.81,292.41,443.51,034.144.293.691.296.7
BLAKE2b-512 keyed665.71,272.41,395.5999.7662.71,287.01,435.81,031.328.651.153.949.9
BLAKE3-256515.0---514.8---65.7---
BLAKE3-256 keyed515.3---515.8---65.5---

-O3, MiB/s

Operation1 MiB64 KiB16 B
CCSMonocypherlibsodiumwolfCryptCCSMonocypherlibsodiumwolfCryptCCSMonocypherlibsodiumwolfCrypt
ChaCha20 IETF1,243.51,165.51,206.31,132.51,243.91,198.41,226.51,142.4231.3198.5222.7238.9
ChaCha20 DJB1,234.51,182.31,216.4-1,240.41,199.71,224.9-231.8199.9225.5-
XChaCha201,237.61,171.51,219.91,108.61,238.31,197.61,224.41,141.4115.1109.2128.6118.9
ChaCha20-Poly1305 encrypt925.7893.5902.5853.5925.8899.0903.9861.089.887.661.395.1
ChaCha20-Poly1305 decrypt925.3888.1885.7849.2926.3899.0903.6860.890.286.357.191.8
ChaCha20-Poly1305 DJB encrypt921.0-904.5-922.6-902.5-86.0-61.8-
ChaCha20-Poly1305 DJB decrypt918.5-901.3-923.6-902.8-86.0-56.3-
XChaCha20-Poly1305 encrypt923.6893.3903.5848.1922.5898.1903.4859.064.363.449.155.0
XChaCha20-Poly1305 decrypt923.3890.5900.9852.6922.0898.2900.8858.763.961.847.054.0
Poly13053,628.63,599.93,459.83,463.13,624.93,604.53,470.63,506.2640.6845.3532.11,066.6
BLAKE2s-2561,038.2--1,056.01,039.4--1,136.6173.7--209.7
BLAKE2s-256 keyed1,038.3--1,120.61,038.9--1,134.2101.7--100.1
BLAKE2b-5121,735.61,886.01,734.61,131.61,737.41,897.71,742.21,155.0120.8168.799.4123.0
BLAKE2b-512 keyed1,736.51,872.91,735.41,139.81,735.51,894.51,735.91,155.177.191.661.360.5
BLAKE3-2561,289.5---1,285.7---146.8---
BLAKE3-256 keyed1,292.1---1,291.1---147.2---

"-" = Unsupported

Security

ChaChaSlide is not formally audited. Its implementations are validated against a large set of test vectors. All functions are constant time (on tested platforms with GCC), with the exception of Poly1305, which depends on the selected multiply implementation and CPU architecture (read below). While custom round counts are supported, this is intended for ChaCha8 and ChaCha12, which have been the subject of cryptanalysis. Using custom rounds is an experiment that this library supports.

About Constant Time Poly1305 Poly1305 relies on a 32->64 multiply, which may not be constant time in hardware or are implemented with variable time software helpers depending on the CPU architecture. This may expose Poly1305 to a side channel timing attacks.

ChaChaSlide has 3 implementations of the Poly1305 multiply to select, which will compile to constant time code depending on the platform and implementation used. Performance will be reduced.

Select CCS_POLY in include/ccs_config.h:

  • 0: Off Poly1305 is not exposed. However, using AEAD modes (ChaCha20-Poly1305) will default to the Generic implementation.
  • 1: Generic: Normal multiplication. Fastest choice on all platforms. Use on platforms that have constant time 32->64 multipliers.
  • 2: 32->32: Forces 15-bit multiplies and will work on platforms with 32->32 multipliers. Best for inexpensive ARM Cortex-M microcontrollers (M0/M0+/M3).
  • 3: 8-bit: Optimized for 8-bit architectures with 16-bit accumulators. E.g. AVR, PIC.
Platform Recommended CCS_POLY selection
Modern x86-64 1
Cortex-M0/M0+/M1/M3/M23 2
Cortex-M4/M7 1
Other Cortex and AArch64 1 or 2
AVR with hardware multiply 3
PIC18 3
Other without hardware multiply Conditional
RISC-V Conditional

Monocypher, wolfCrypt and LibSodium do not guarantee constant time Poly1305 if the CPU does not have a constant time multiplier. The reduced speed of a constant time ChaChaSlide implementation is not directly comparable to a non constant time benchmark of a comparable library due to the potential side channel vulnerability.

FAQ

  • Can we go smaller: Yes! We can also go substantially faster at -Os at the cost of code size. ChaChaSlide also prevents some footguns (e.g. allowing ChaCha counter overflow).
  • Can we go faster: A little bit. ChaChaSlide pushes the limits on my hardware, and the speedups are architecture dependant (e.g. taking advantage of 64-bit operations), and might displace code designed for -Os (e.g. loop unrolling and array-less computation).
  • Why not create two sources for size and speed optimized code: This is on the table, but one codebase keeps the project simpler.
  • Why include BLAKE2/3: BLAKE is based on the core ChaCha ARX function and is compact. This is the ChaCha"Slide".
  • Is the project finished: The library is fully functional.

References

[1] Y. Nir and A. Langley, “ChaCha20 and Poly1305 for IETF protocols,” RFC 8439, Jun. 2018, doi: 10.17487/RFC8439.

[2] D. J. Bernstein, “ChaCha, a variant of Salsa20,” Jan. 28, 2008. [Online]. Available: https://cr.yp.to/chacha/chacha-20080128.pdf

[3] S. Arciszewski, “XChaCha: eXtended-nonce ChaCha and AEAD_XChaCha20_Poly1305,” Internet-Draft draft-irtf-cfrg-xchacha-03, Jan. 2020, expired. [Online]. Available: https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha-03

[4] The libsodium Project, “ChaCha20-Poly1305,” The Sodium Crypto Library. Accessed: Nov. 4, 2026. [Online]. Available: https://doc.libsodium.org/secret-key_cryptography/aead/chacha20-poly1305

[5] M.-J. O. Saarinen and J.-P. Aumasson, “The BLAKE2 cryptographic hash and message authentication code (MAC),” RFC 7693, Nov. 2015, doi: 10.17487/RFC7693.

[6] J. O’Connor, J.-P. Aumasson, S. Neves, and Z. Wilcox-O’Hearn, “BLAKE3: one function, fast everywhere,” 2020. [Online]. Available: https://github.com/BLAKE3-team/BLAKE3-specs/blob/master/blake3.pdf

Changes

1.0.1 Add BLAKE3. BLAKE2 speed improvements.
1.0.0 Initial Release

About

A lightweight, portable C library implementing ChaCha20, XChaCha20, ChaCha20-Poly1305, XChaCha20-Poly1305, IETF & Bernstein variants, N rounds, BLAKE2s/b, BLAKE3.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Used by

Contributors

Languages