Go to the documentation of this file.
32 #define TRACE_LEVEL SSH_TRACE_LEVEL
41 #if (SSH_SUPPORT == ENABLED)
57 const char_t *macAlgo, uint8_t
x)
73 #if (SSH_STREAM_CIPHER_SUPPORT == ENABLED)
110 encryptionEngine->
hmacContext = &connection->hmacContext;
114 #if (SSH_CBC_CIPHER_SUPPORT == ENABLED || SSH_CTR_CIPHER_SUPPORT == ENABLED)
148 encryptionEngine->
hmacContext = &connection->hmacContext;
152 #if (SSH_GCM_CIPHER_SUPPORT == ENABLED || SSH_RFC5647_SUPPORT == ENABLED)
187 #if (SSH_CHACHA20_POLY1305_SUPPORT == ENABLED)
227 #if (SSH_GCM_CIPHER_SUPPORT == ENABLED || SSH_RFC5647_SUPPORT == ENABLED)
268 #if (SSH_RC4_SUPPORT == ENABLED && SSH_STREAM_CIPHER_SUPPORT == ENABLED)
279 #if (SSH_RC4_128_SUPPORT == ENABLED && SSH_STREAM_CIPHER_SUPPORT == ENABLED)
290 #if (SSH_RC4_256_SUPPORT == ENABLED && SSH_STREAM_CIPHER_SUPPORT == ENABLED)
301 #if (SSH_CAST128_SUPPORT == ENABLED && SSH_CBC_CIPHER_SUPPORT == ENABLED)
313 #if (SSH_CAST128_SUPPORT == ENABLED && SSH_CTR_CIPHER_SUPPORT == ENABLED)
325 #if (SSH_IDEA_SUPPORT == ENABLED && SSH_CBC_CIPHER_SUPPORT == ENABLED)
336 #if (SSH_IDEA_SUPPORT == ENABLED && SSH_CTR_CIPHER_SUPPORT == ENABLED)
347 #if (SSH_BLOWFISH_SUPPORT == ENABLED && SSH_CBC_CIPHER_SUPPORT == ENABLED)
359 #if (SSH_BLOWFISH_SUPPORT == ENABLED && SSH_CTR_CIPHER_SUPPORT == ENABLED)
371 #if (SSH_3DES_SUPPORT == ENABLED && SSH_CBC_CIPHER_SUPPORT == ENABLED)
383 #if (SSH_3DES_SUPPORT == ENABLED && SSH_CTR_CIPHER_SUPPORT == ENABLED)
395 #if (SSH_AES_128_SUPPORT == ENABLED && SSH_CBC_CIPHER_SUPPORT == ENABLED)
407 #if (SSH_AES_192_SUPPORT == ENABLED && SSH_CBC_CIPHER_SUPPORT == ENABLED)
419 #if (SSH_AES_256_SUPPORT == ENABLED && SSH_CBC_CIPHER_SUPPORT == ENABLED)
431 #if (SSH_AES_128_SUPPORT == ENABLED && SSH_CTR_CIPHER_SUPPORT == ENABLED)
443 #if (SSH_AES_192_SUPPORT == ENABLED && SSH_CTR_CIPHER_SUPPORT == ENABLED)
455 #if (SSH_AES_256_SUPPORT == ENABLED && SSH_CTR_CIPHER_SUPPORT == ENABLED)
467 #if (SSH_TWOFISH_128_SUPPORT == ENABLED && SSH_CBC_CIPHER_SUPPORT == ENABLED)
478 #if (SSH_TWOFISH_192_SUPPORT == ENABLED && SSH_CBC_CIPHER_SUPPORT == ENABLED)
489 #if (SSH_TWOFISH_256_SUPPORT == ENABLED && SSH_CBC_CIPHER_SUPPORT == ENABLED)
501 #if (SSH_TWOFISH_128_SUPPORT == ENABLED && SSH_CTR_CIPHER_SUPPORT == ENABLED)
512 #if (SSH_TWOFISH_192_SUPPORT == ENABLED && SSH_CTR_CIPHER_SUPPORT == ENABLED)
523 #if (SSH_TWOFISH_256_SUPPORT == ENABLED && SSH_CTR_CIPHER_SUPPORT == ENABLED)
534 #if (SSH_SERPENT_128_SUPPORT == ENABLED && SSH_CBC_CIPHER_SUPPORT == ENABLED)
545 #if (SSH_SERPENT_192_SUPPORT == ENABLED && SSH_CBC_CIPHER_SUPPORT == ENABLED)
556 #if (SSH_SERPENT_256_SUPPORT == ENABLED && SSH_CBC_CIPHER_SUPPORT == ENABLED)
567 #if (SSH_SERPENT_128_SUPPORT == ENABLED && SSH_CTR_CIPHER_SUPPORT == ENABLED)
578 #if (SSH_SERPENT_192_SUPPORT == ENABLED && SSH_CTR_CIPHER_SUPPORT == ENABLED)
589 #if (SSH_SERPENT_256_SUPPORT == ENABLED && SSH_CTR_CIPHER_SUPPORT == ENABLED)
600 #if (SSH_CAMELLIA_128_SUPPORT == ENABLED && SSH_CBC_CIPHER_SUPPORT == ENABLED)
611 #if (SSH_CAMELLIA_192_SUPPORT == ENABLED && SSH_CBC_CIPHER_SUPPORT == ENABLED)
622 #if (SSH_CAMELLIA_256_SUPPORT == ENABLED && SSH_CBC_CIPHER_SUPPORT == ENABLED)
633 #if (SSH_CAMELLIA_128_SUPPORT == ENABLED && SSH_CTR_CIPHER_SUPPORT == ENABLED)
644 #if (SSH_CAMELLIA_192_SUPPORT == ENABLED && SSH_CTR_CIPHER_SUPPORT == ENABLED)
655 #if (SSH_CAMELLIA_256_SUPPORT == ENABLED && SSH_CTR_CIPHER_SUPPORT == ENABLED)
666 #if (SSH_SEED_SUPPORT == ENABLED && SSH_CBC_CIPHER_SUPPORT == ENABLED)
677 #if (SSH_AES_128_SUPPORT == ENABLED && SSH_GCM_CIPHER_SUPPORT == ENABLED)
688 #if (SSH_AES_256_SUPPORT == ENABLED && SSH_GCM_CIPHER_SUPPORT == ENABLED)
699 #if (SSH_AES_128_SUPPORT == ENABLED && SSH_RFC5647_SUPPORT == ENABLED)
710 #if (SSH_AES_256_SUPPORT == ENABLED && SSH_RFC5647_SUPPORT == ENABLED)
721 #if (SSH_CAMELLIA_128_SUPPORT == ENABLED && SSH_RFC5647_SUPPORT == ENABLED)
732 #if (SSH_CAMELLIA_256_SUPPORT == ENABLED && SSH_RFC5647_SUPPORT == ENABLED)
743 #if (SSH_CHACHA20_POLY1305_SUPPORT == ENABLED)
782 #if (SSH_AES_128_SUPPORT == ENABLED && SSH_GCM_CIPHER_SUPPORT == ENABLED)
788 encryptionEngine->
macSize = 16;
793 #if (SSH_AES_256_SUPPORT == ENABLED && SSH_GCM_CIPHER_SUPPORT == ENABLED)
799 encryptionEngine->
macSize = 16;
804 #if (SSH_AES_128_SUPPORT == ENABLED && SSH_RFC5647_SUPPORT == ENABLED)
810 encryptionEngine->
macSize = 16;
815 #if (SSH_AES_256_SUPPORT == ENABLED && SSH_RFC5647_SUPPORT == ENABLED)
821 encryptionEngine->
macSize = 16;
826 #if (SSH_CAMELLIA_128_SUPPORT == ENABLED && SSH_RFC5647_SUPPORT == ENABLED)
832 encryptionEngine->
macSize = 16;
837 #if (SSH_CAMELLIA_256_SUPPORT == ENABLED && SSH_RFC5647_SUPPORT == ENABLED)
843 encryptionEngine->
macSize = 16;
848 #if (SSH_CHACHA20_POLY1305_SUPPORT == ENABLED)
854 encryptionEngine->
macSize = 16;
859 #if (SSH_HMAC_SUPPORT == ENABLED && SSH_MD5_SUPPORT == ENABLED)
870 #if (SSH_HMAC_SUPPORT == ENABLED && SSH_MD5_SUPPORT == ENABLED && \
871 SSH_ETM_SUPPORT == ENABLED)
882 #if (SSH_HMAC_SUPPORT == ENABLED && SSH_MD5_96_SUPPORT == ENABLED)
888 encryptionEngine->
macSize = 12;
893 #if (SSH_HMAC_SUPPORT == ENABLED && SSH_MD5_96_SUPPORT == ENABLED && \
894 SSH_ETM_SUPPORT == ENABLED)
900 encryptionEngine->
macSize = 12;
905 #if (SSH_HMAC_SUPPORT == ENABLED && SSH_RIPEMD160_SUPPORT == ENABLED)
917 #if (SSH_HMAC_SUPPORT == ENABLED && SSH_RIPEMD160_SUPPORT == ENABLED && \
918 SSH_ETM_SUPPORT == ENABLED)
929 #if (SSH_HMAC_SUPPORT == ENABLED && SSH_SHA1_SUPPORT == ENABLED)
940 #if (SSH_HMAC_SUPPORT == ENABLED && SSH_SHA1_SUPPORT == ENABLED && \
941 SSH_ETM_SUPPORT == ENABLED)
952 #if (SSH_HMAC_SUPPORT == ENABLED && SSH_SHA1_96_SUPPORT == ENABLED)
958 encryptionEngine->
macSize = 12;
963 #if (SSH_HMAC_SUPPORT == ENABLED && SSH_SHA1_96_SUPPORT == ENABLED && \
964 SSH_ETM_SUPPORT == ENABLED)
970 encryptionEngine->
macSize = 12;
975 #if (SSH_HMAC_SUPPORT == ENABLED && SSH_SHA256_SUPPORT == ENABLED)
986 #if (SSH_HMAC_SUPPORT == ENABLED && SSH_SHA256_SUPPORT == ENABLED && \
987 SSH_ETM_SUPPORT == ENABLED)
998 #if (SSH_HMAC_SUPPORT == ENABLED && SSH_SHA512_SUPPORT == ENABLED)
1009 #if (SSH_HMAC_SUPPORT == ENABLED && SSH_SHA512_SUPPORT == ENABLED && \
1010 SSH_ETM_SUPPORT == ENABLED)
1053 hashAlgo = connection->hashAlgo;
1056 if(hashAlgo != NULL)
1062 if(hashContext != NULL)
1065 hashAlgo->
init(hashContext);
1066 hashAlgo->
update(hashContext, connection->k, connection->kLen);
1067 hashAlgo->
update(hashContext, connection->h, connection->hLen);
1068 hashAlgo->
update(hashContext, &
x,
sizeof(
x));
1069 hashAlgo->
update(hashContext, connection->sessionId, connection->sessionIdLen);
1070 hashAlgo->
final(hashContext, NULL);
1075 output[
n] = hashContext->
digest[
n];
1081 while(
n < outputLen)
1084 hashAlgo->
init(hashContext);
1085 hashAlgo->
update(hashContext, connection->k, connection->kLen);
1086 hashAlgo->
update(hashContext, connection->h, connection->hLen);
1087 hashAlgo->
update(hashContext, output,
n);
1088 hashAlgo->
final(hashContext, NULL);
1091 for(i = 0; i < hashAlgo->
digestSize &&
n < outputLen; i++,
n++)
1093 output[
n] = hashContext->
digest[i];
1129 const uint8_t *key,
size_t keyLen)
1131 #if (SSH_KEY_LOG_SUPPORT == ENABLED)
1135 context = connection->context;
1138 if(context->keyLogCallback != NULL)
1155 n +=
osSprintf(buffer +
n,
"%02" PRIX8, connection->cookie[i]);
1163 for(i = 0; i < keyLen; i++)
1173 context->keyLogCallback(connection, buffer);
#define TWOFISH_CIPHER_ALGO
Generic hash algorithm context.
void sshFreeEncryptionEngine(SshEncryptionEngine *encryptionEngine)
Release encryption engine.
uint8_t macKey[SSH_MAX_HASH_DIGEST_SIZE]
Integrity key.
#define BLOWFISH_CIPHER_ALGO
uint8_t iv[SSH_MAX_CIPHER_BLOCK_SIZE]
Initialization vector.
void sshDumpKey(SshConnection *connection, const char_t *label, const uint8_t *key, size_t keyLen)
Dump secret key (for debugging purpose only)
const HashAlgo * hashAlgo
Hash algorithm for MAC operations.
GcmContext gcmContext
GCM context.
size_t macSize
Size of the MAC tag, in bytes.
__weak_func error_t gcmInit(GcmContext *context, const CipherAlgo *cipherAlgo, void *cipherContext)
Initialize GCM context.
#define SERPENT_CIPHER_ALGO
#define SSH_MAX_CIPHER_BLOCK_SIZE
CipherAlgoEncryptStream encryptStream
@ ERROR_UNSUPPORTED_CIPHER_MODE
@ ERROR_UNSUPPORTED_HASH_ALGO
CipherMode cipherMode
Cipher mode of operation.
#define osSprintf(dest,...)
bool_t sshCompareAlgo(const char_t *name1, const char_t *name2)
Compare algorithm names.
@ ERROR_FAILURE
Generic error code.
#define RIPEMD160_DIGEST_SIZE
const CipherAlgo * cipherAlgo
Cipher algorithm.
uint8_t encKey[SSH_MAX_ENC_KEY_SIZE]
Encryption key.
uint8_t digest[MAX_HASH_DIGEST_SIZE]
bool_t etm
Encrypt-then-MAC.
error_t sshInitEncryptionEngine(SshConnection *connection, SshEncryptionEngine *encryptionEngine, const char_t *encAlgo, const char_t *macAlgo, uint8_t x)
Initialize encryption engine.
CipherContext cipherContext
Cipher context.
#define CAMELLIA_CIPHER_ALGO
error_t sshSelectCipherAlgo(SshEncryptionEngine *encryptionEngine, const char_t *encAlgo)
Select the relevant cipher algorithm.
#define RIPEMD160_HASH_ALGO
#define SSH_MAX_HASH_DIGEST_SIZE
error_t sshSelectHashAlgo(SshEncryptionEngine *encryptionEngine, const char_t *encAlgo, const char_t *macAlgo)
Select the relevant hash algorithm.
#define SSH_MAX_ENC_KEY_SIZE
@ ERROR_UNSUPPORTED_CIPHER_ALGO
error_t sshDeriveKey(SshConnection *connection, uint8_t x, uint8_t *output, size_t outputLen)
Key derivation function.
#define CAST128_CIPHER_ALGO
HmacContext * hmacContext
HMAC context.
Common interface for hash algorithms.
#define sshAllocMem(size)
@ CIPHER_MODE_CHACHA20_POLY1305
#define osMemset(p, value, length)
#define SHA256_DIGEST_SIZE
#define SHA512_DIGEST_SIZE
size_t encKeyLen
Length of the encryption key, in bytes.