/* * Copyright (C) 2015 Southern Storm Software, Pty Ltd. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ /** \file crypto.dox \page crypto Cryptographic Library \section crypto_algorithms Supported Algorithms \li Block ciphers: AES128, AES192, AES256, Speck \li Block cipher modes: CTR, CFB, CBC, OFB, GCM \li Stream ciphers: ChaCha \li Authenticated encryption with associated data (AEAD): ChaChaPoly, EAX, GCM \li Hash algorithms: SHA1, SHA256, SHA512, SHA3_256, SHA3_512, BLAKE2s, BLAKE2b (regular and HMAC modes) \li Message authenticators: Poly1305, GHASH \li Public key algorithms: Curve25519, Ed25519 \li Random number generation: \link RNGClass RNG\endlink, TransistorNoiseSource, RingOscillatorNoiseSource All cryptographic algorithms have been optimized for 8-bit Arduino platforms like the Uno. Memory usage is also reduced, particularly for SHA1, SHA256, and SHA512 which save 256, 192, and 512 bytes respectively over traditional implementations. For all algorithms, static sbox tables and the like are placed into program memory to further reduce data memory usage. ChaCha with 20 rounds and 256-bit keys is the recommended symmetric encryption algorithm because it is twice as fast as AES128, constant-time, and much more secure. AES128, AES192, and AES256 are provided for use in applications where compatibility with other systems is desirable. If code size is an issue for your application (for example on very low end Arduino variants), then Speck on AVR is less than half the code size of ChaCha, at the cost of more data memory for the state and longer key setup times. The SpeckLowMemory class is even smaller at the cost of some performance when encrypting. BLAKE2s and BLAKE2b are variations on the ChaCha stream cipher, designed for hashing, with 256-bit and 512-bit hash outputs respectively. They are intended as high performance replacements for SHA256 and SHA512 for when speed is critical but exact bit-compatibility of hash values is not. \section crypto_other Examples and other topics \li \ref crypto_rng "Generating random numbers" \section crypto_performance Performance \subsection crypto_performance_avr Performance on AVR All figures are for the Arduino Uno running at 16 MHz. Figures for the Ardunino Mega 2560 running at 16 MHz are similar:
Encryption AlgorithmEncryption (per byte)Decryption (per byte)Key SetupState Size (bytes)
AES128 (ECB mode)33.28us63.18us160.00us181
AES192 (ECB mode)39.94us76.48us166.54us213
AES256 (ECB mode)46.61us89.78us227.97us245
ChaCha (20 rounds)14.87us14.88us43.74us132
ChaCha (12 rounds)10.38us10.38us43.74us132
ChaCha (8 rounds)8.13us8.14us43.74us132
Speck (128-bit key, ECB mode)10.72us11.09us287.02us275
Speck (192-bit key, ECB mode)11.03us11.42us298.21us275
Speck (256-bit key, ECB mode)11.35us11.74us309.66us275
SpeckLowMemory (128-bit key, ECB mode)35.25us 10.22us35
SpeckLowMemory (192-bit key, ECB mode)36.56us 13.62us35
SpeckLowMemory (256-bit key, ECB mode)37.87us 16.89us35
AEAD AlgorithmEncryption (per byte)Decryption (per byte)Key SetupState Size (bytes)
ChaChaPoly41.23us41.23us902.55us255
GCM<AES128>183.25us182.80us1272.73us284
GCM<AES192>189.92us189.47us1492.60us316
GCM<AES256>196.59us196.13us1767.33us348
EAX<AES128>71.14us71.14us1329.44us268
EAX<Speck> (128-bit key)26.01us26.01us735.46us362
EAX<SpeckLowMemory> (128-bit key)75.08us75.07us1243.66us122
Hash AlgorithmHashing (per byte)Finalization State Size (bytes)
SHA121.90us1423.28us 95
SHA25643.85us2841.04us 107
SHA512122.82us15953.42us 211
SHA3_25660.69us8180.24us 205
SHA3_512113.88us8196.34us 205
BLAKE2s20.65us1335.25us 107
BLAKE2b65.22us8375.36us 211
Authentication AlgorithmHashing (per byte)FinalizationKey SetupState Size (bytes)
SHA1 (HMAC mode)21.90us4296.33us1420.24us95
SHA256 (HMAC mode)43.85us8552.61us2836.49us107
BLAKE2s (HMAC mode)20.65us4055.56us1350.00us107
Poly130526.29us486.15us17.26us87
GHASH148.14us17.09us21.87us33
Public Key OperationTime (per operation)Comment
Curve25519::eval()3119msRaw curve evaluation
Curve25519::dh1()3121msFirst half of Diffie-Hellman key agreement
Curve25519::dh2()3120msSecond half of Diffie-Hellman key agreement
Ed25519::sign()5688msDigital signature generation
Ed25519::verify()9030msDigital signature verification
Ed25519::derivePublicKey()5642msDerive a public key from a private key
Where a cipher supports more than one key size (such as ChaCha), the values are typically almost identical for 128-bit and 256-bit keys so only the maximum is shown above. \subsection crypto_performance_arm Performance on ARM All figures are for the Arduino Due running at 84 MHz:
Encryption AlgorithmEncryption (per byte)Decryption (per byte)Key SetupState Size (bytes)
AES128 (ECB mode)5.71us10.41us34.73us188
AES192 (ECB mode)6.87us12.57us36.51us220
AES256 (ECB mode)8.04us14.7249.96us252
ChaCha (20 rounds)0.87us0.88us4.96us136
ChaCha (12 rounds)0.70us0.71us4.96us136
ChaCha (8 rounds)0.62us0.62us4.96us136
Speck (128-bit key, ECB mode)0.97us0.96us36.80us288
Speck (192-bit key, ECB mode)1.00us0.98us38.14us288
Speck (256-bit key, ECB mode)1.03us1.01us39.31us288
SpeckLowMemory (128-bit key, ECB mode)2.72us 1.47us48
SpeckLowMemory (192-bit key, ECB mode)2.81us 1.54us48
SpeckLowMemory (256-bit key, ECB mode)2.90us 1.83us48
AEAD AlgorithmEncryption (per byte)Decryption (per byte)Key SetupState Size (bytes)
ChaChaPoly1.66us1.66us45.02us280
GCM<AES128>10.29us10.29us223.82us312
GCM<AES192>11.50us11.51us265.62us344
GCM<AES256>12.67us12.67us313.06us376
EAX<AES128>12.29us12.29us236.47us280
EAX<Speck> (128-bit key)2.65us2.65us79.46us384
EAX<SpeckLowMemory> (128-bit key)6.29us6.29us106.60us144
Hash AlgorithmHashing (per byte)Finalization State Size (bytes)
SHA10.94us62.55us 112
SHA2561.15us76.60us 120
SHA5122.87us370.37us 224
SHA3_2565.64us735.29us 224
SHA3_51210.42us735.49us 224
BLAKE2s0.72us48.24us 120
BLAKE2b1.29us165.28us 224
Authentication AlgorithmHashing (per byte)FinalizationKey SetupState Size (bytes)
SHA1 (HMAC mode)0.94us193.92us65.09us112
SHA256 (HMAC mode)1.15us238.98us80.44us120
BLAKE2s (HMAC mode)0.72us157.75us57.18us120
Poly13050.85us19.25us2.35us96
GHASH4.37us1.50us4.37us36
Public Key OperationTime (per operation)Comment
Curve25519::eval()103msRaw curve evaluation
Curve25519::dh1()103msFirst half of Diffie-Hellman key agreement
Curve25519::dh2()104msSecond half of Diffie-Hellman key agreement
Ed25519::sign()195msDigital signature generation
Ed25519::verify()306msDigital signature verification
Ed25519::derivePublicKey()194msDerive a public key from a private key
*/