### Compile and Install Dropbear Programs Source: https://github.com/mkj/dropbear/blob/main/INSTALL.md Compile and install specific Dropbear programs in a single command. This example compiles dropbear, dbclient, dropbearkey, dropbearconvert, and scp. ```bash make PROGRAMS="dropbear dbclient dropbearkey dropbearconvert scp" install ``` -------------------------------- ### Install LibTomCrypt with Extra Libraries and CFLAGS Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Installs the library and links test programs and shared libraries with extra specified libraries (EXTRALIBS) and custom CFLAGS. This example includes the TomsFastMath library and defines. ```makefile make install test timing CFLAGS="-DTFM_DESC -DUSE_TFM" EXTRALIBS=-ltfm ``` -------------------------------- ### Install LibTomCrypt with Custom Directories Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Installs the library and its components to custom directories specified by PREFIX and DATAPATH. This example demonstrates setting these variables during the make install command. ```makefile make PREFIX=/home/tom/project DATAPATH=/home/tom/project/docs install ``` -------------------------------- ### Install Dropbear Source: https://github.com/mkj/dropbear/blob/main/INSTALL.md Install the compiled Dropbear binaries. The default installation path is usually /usr/local/bin. ```bash make install ``` -------------------------------- ### RC2 Extended Setup Function Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Demonstrates the extended setup function for RC2, which allows configuring the effective key length in bits. ```c int rc2_setup_ex(const unsigned char *key, int keylen, int bits, int num_rounds, symmetric_key *skey); ``` -------------------------------- ### libtomcrypt Python ctypes Examples Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Demonstrates loading libtomcrypt, checking endianness and word size, getting struct sizes, and listing constants and structs/unions using Python's ctypes. ```python from ctypes import * # load the OSX shared/dynamic library LIB = CDLL('libtomcrypt.dylib') # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # print info about this library little = c_int() word32 = c_int() LIB.crypt_get_constant('ENDIAN_LITTLE', byref(little)) LIB.crypt_get_constant('ENDIAN_32BITWORD', byref(word32)) print('this lib was compiled for a %s endian %d-bit processor' % ('little' if little else 'big', 32 if word32 else 64)) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # print the size of the struct named "sha256_state" struct_size = c_int() # don't forget to add the '_struct' or '_union' suffix LIB.crypt_get_size('sha256_state_struct', byref(struct_size)) print('allocate %d bytes for sha256_state' % struct_size.value) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # print a list of all supported named constants list_size = c_int() # call with NULL to calc the min size needed for the list LIB.crypt_list_all_constants(None, byref(list_size)) # allocate required space names_list = c_buffer(list_size.value) # call again providing a pointer to where to write the list LIB.crypt_list_all_constants(names_list, byref(list_size)) print(names_list.value) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # print a list of all supported named structs and unions list_size = c_int() # call with NULL to calc the min size needed for the list LIB.crypt_list_all_sizes(None, byref(list_size)) # allocate required space names_list = c_buffer(list_size.value) # call again providing a pointer to where to write the list LIB.crypt_list_all_sizes(names_list, byref(list_size)) print(names_list.value) ``` -------------------------------- ### MD5 Hash Example Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Demonstrates how to hash a string using the MD5 algorithm. Includes initialization, processing, and finalization steps. ```c #include int main(void) { hash_state md; unsigned char *in = "hello world", out[16]; /* setup the hash */ md5_init(&md); /* add the message */ md5_process(&md, in, strlen(in)); /* get the hash in out[0..15] */ md5_done(&md, out); return 0; } ``` -------------------------------- ### Install Dropbear to a Temporary Directory Source: https://github.com/mkj/dropbear/blob/main/INSTALL.md Install Dropbear binaries to a temporary directory using the DESTDIR variable, useful for testing installations. ```bash make install DESTDIR=/same/temp/location ``` -------------------------------- ### Yarrow PRNG Setup and ECC Key Generation Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Example of setting up the Yarrow PRNG and then using it to generate a 192-bit ECC key. Requires Yarrow PRNG to be registered first. ```c #include int main(void) { ecc_key mykey; prng_state prng; int err; /* register yarrow */ if (register_prng(&yarrow_desc) == -1) { printf("Error registering Yarrow\n"); return -1; } /* setup the PRNG */ if ((err = rng_make_prng(128, find_prng("yarrow"), &prng, NULL)) != CRYPT_OK) { printf("Error setting up PRNG, %s\n", error_to_string(err)); return -1; } /* make a 192-bit ECC key */ if ((err = ecc_make_key(&prng, find_prng("yarrow"), 24, &mykey)) != CRYPT_OK) { printf("Error making key: %s\n", error_to_string(err)); return -1; } return 0; } ``` -------------------------------- ### Example: Using CHC with AES Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Demonstrates registering the AES cipher and the CHC hash descriptor, then binding AES to the CHC system. This setup allows the CHC hash to be used with other LibTomCrypt functions. ```c #include int main(void) { int err; /* register cipher and hash */ if (register_cipher(&aes_enc_desc) == -1) { printf("Could not register cipher\n"); return EXIT_FAILURE; } if (register_hash(&chc_desc) == -1) { printf("Could not register hash\n"); return EXIT_FAILURE; } /* start chc with AES */ if ((err = chc_register(find_cipher("aes"))) != CRYPT_OK) { printf("Error binding AES to CHC: %s\n", error_to_string(err)); } /* now you can use chc_hash in any LTC function * [aside from pkcs...] */ } ``` -------------------------------- ### HMAC SHA-1 Example Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Demonstrates how to initialize, process data, and finalize an HMAC-SHA1 computation. Ensure the hash algorithm is registered before use. ```c #include int main(void) { int idx, err; hmac_state hmac; unsigned char key[16], dst[MAXBLOCKSIZE]; unsigned long dstlen; /* register SHA-1 */ if (register_hash(&sha1_desc) == -1) { printf("Error registering SHA1\n"); return -1; } /* get index of SHA1 in hash descriptor table */ idx = find_hash("sha1"); /* we would make up our symmetric key in "key[]" here */ /* start the HMAC */ if ((err = hmac_init(&hmac, idx, key, 16)) != CRYPT_OK) { printf("Error setting up hmac: %s\n", error_to_string(err)); return -1; } /* process a few octets */ if((err = hmac_process(&hmac, "hello", 5) != CRYPT_OK) { printf("Error processing hmac: %s\n", error_to_string(err)); return -1; } /* get result (presumably to use it somehow...) */ dstlen = sizeof(dst); if ((err = hmac_done(&hmac, dst, &dstlen)) != CRYPT_OK) { printf("Error finishing hmac: %s\n", error_to_string(err)); return -1; } printf("The hmac is %lu bytes long\n", dstlen); /* return */ return 0; } ``` -------------------------------- ### MD5 Hash Memory Example Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Demonstrates how to register the MD5 hash, find its index, and then use hash_memory to hash a string. Error handling for registration and hashing is included. ```c #include int main(void) { int idx, err; unsigned long len; unsigned char out[MAXBLOCKSIZE]; /* register the hash */ if (register_hash(&md5_desc) == -1) { printf("Error registering MD5.\n"); return -1; } /* get the index of the hash */ idx = find_hash("md5"); /* call the hash */ len = sizeof(out); if ((err = hash_memory(idx, "hello world", 11, out, &len)) != CRYPT_OK) { printf("Error hashing data: %s\n", error_to_string(err)); return -1; } return 0; } ``` -------------------------------- ### OMAC Example with Rijndael Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Demonstrates how to initialize, process data with, and finalize an OMAC (One-Key MAC) using the Rijndael cipher. Ensure the cipher is registered before use. ```c #include int main(void) { int idx, err; omac_state omac; unsigned char key[16], dst[MAXBLOCKSIZE]; unsigned long dstlen; /* register Rijndael */ if (register_cipher(&rijndael_desc) == -1) { printf("Error registering Rijndael\n"); return -1; } /* get index of Rijndael in cipher descriptor table */ idx = find_cipher("rijndael"); /* we would make up our symmetric key in "key[]" here */ /* start the OMAC */ if ((err = omac_init(&omac, idx, key, 16)) != CRYPT_OK) { printf("Error setting up omac: %s\n", error_to_string(err)); return -1; } /* process a few octets */ if((err = omac_process(&omac, "hello", 5) != CRYPT_OK) { printf("Error processing omac: %s\n", error_to_string(err)); return -1; } /* get result (presumably to use it somehow...) */ dstlen = sizeof(dst); if ((err = omac_done(&omac, dst, &dstlen)) != CRYPT_OK) { printf("Error finishing omac: %s\n", error_to_string(err)); return -1; } printf("The omac is %lu bytes long\n", dstlen); /* return */ return 0; } ``` -------------------------------- ### Register and Unregister Cipher Example Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Demonstrates how to register a cipher (Rijndael) and then unregister it. Ensure the cipher descriptor is available. ```c #include int main(void) { int err; /* register the cipher */ if (register_cipher(&rijndael_desc) == -1) { printf("Error registering Rijndael\n"); return -1; } /* use Rijndael */ /* remove it */ if ((err = unregister_cipher(&rijndael_desc)) != CRYPT_OK) { printf("Error removing Rijndael: %s\n", error_to_string(err)); return -1; } return 0; } ``` -------------------------------- ### HMAC Initialization, Processing, and Done Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Demonstrates how to initialize an HMAC state, process data, and finalize to get the MAC. It includes registering a hash algorithm (SHA-1) and finding its index. ```APIDOC ## HMAC Operations ### Description Functions for initializing, processing, and finalizing HMAC computations. ### Functions - `hmac_init(hmac_state *hmac, int hash_idx, const unsigned char *key, unsigned long keylen)`: Initializes the HMAC state. - `hmac_process(hmac_state *hmac, const unsigned char *in, unsigned long inlen)`: Processes input data for HMAC. - `hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen)`: Finalizes the HMAC computation and outputs the MAC. ### Example Usage ```c #include int main() { int idx, err; hmac_state hmac; unsigned char key[16], dst[MAXBLOCKSIZE]; unsigned long dstlen; if (register_hash(&sha1_desc) == -1) { printf("Error registering SHA1\n"); return -1; } idx = find_hash("sha1"); if ((err = hmac_init(&hmac, idx, key, 16)) != CRYPT_OK) { printf("Error setting up hmac: %s\n", error_to_string(err)); return -1; } if((err = hmac_process(&hmac, "hello", 5) != CRYPT_OK) { printf("Error processing hmac: %s\n", error_to_string(err)); return -1; } dstlen = sizeof(dst); if ((err = hmac_done(&hmac, dst, &dstlen)) != CRYPT_OK) { printf("Error finishing hmac: %s\n", error_to_string(err)); return -1; } printf("The hmac is %lu bytes long\n", dstlen); return 0; } ``` ``` -------------------------------- ### Build and Install Static Library with CFLAGS Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Builds and installs the static version of LibTomCrypt, including the TomsFastMath descriptor, by specifying CFLAGS. This command ensures the necessary descriptor is compiled into the library. ```makefile make install CFLAGS="-DTFM_DESC" ``` -------------------------------- ### RC4 Setup Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Initializes the RC4 stream cipher state with a key. Key size can range from 5 to 256 bytes. ```c rc4_state st; err = rc4_stream_setup(&st, key, key_len); ``` -------------------------------- ### Radix to Binary Conversion Example Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Demonstrates how to use radix_to_bin to convert a hexadecimal string to binary. It first calls the function with a NULL buffer to determine the required size, allocates memory, and then performs the conversion. ```C #include int main(void) { const char *mpi = "AABBCCDD"; unsigned long l = 0; void* buf; int ret; ltc_mp = ltm_desc; if (radix_to_bin(mpi, 16, NULL, &l) != CRYPT_BUFFER_OVERFLOW) return EXIT_FAILURE; buf = malloc(l); ret = EXIT_SUCCESS; if (radix_to_bin(mpi, 16, buf, &l) != CRYPT_OK) ret = EXIT_FAILURE; free(buf); return ret; } ``` -------------------------------- ### RC4 PRNG Initialization and Usage Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Demonstrates how to initialize and use the RC4 pseudo-random number generator for encryption. Requires RC4 to be started, seeded with entropy, and then read from. ```c #include int main(void) { prng_state prng; unsigned char buf[32]; int err; if ((err = rc4_start(&prng)) != CRYPT_OK) { printf("RC4 init error: %s\n", error_to_string(err)); exit(-1); } /* use "key" as the key */ if ((err = rc4_add_entropy("key", 3, &prng)) != CRYPT_OK) { printf("RC4 add entropy error: %s\n", error_to_string(err)); exit(-1); } /* setup RC4 for use */ if ((err = rc4_ready(&prng)) != CRYPT_OK) { printf("RC4 ready error: %s\n", error_to_string(err)); exit(-1); } /* encrypt buffer */ strcpy(buf,"hello world"); if (rc4_read(buf, 11, &prng) != 11) { printf("RC4 read error\n"); exit(-1); } return 0; } ``` -------------------------------- ### Sober128 Setup Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Initializes the Sober128 stream cipher state with a key and a nonce. Both key and nonce must be multiples of 4 bytes. ```c sober128_state st; err = sober128_stream_setup(&st, key, 16); err = sober128_stream_setiv(&st, nonce, 12); ``` -------------------------------- ### OMAC Process Data Example Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Shows how to process data in chunks using OMAC. Multiple calls to omac_process with partial data yield the same result as a single call with concatenated data. ```c omac_process(&mystate, "hello", 5); omac_process(&mystate, " world", 6); ``` ```c omac_process(&mystate, "hello world", 11); ``` -------------------------------- ### EAX Mode Encryption Example Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Demonstrates the step-by-step encryption process using EAX mode, including context initialization, data encryption, and obtaining the authentication tag. Requires cipher registration. ```c #include int main(void) { int err; eax_state eax; unsigned char pt[64], ct[64], nonce[16], key[16], tag[16]; unsigned long taglen; if (register_cipher(&rijndael_desc) == -1) { printf("Error registering Rijndael"); return EXIT_FAILURE; } /* ... make up random nonce and key ... */ /* initialize context */ if ((err = eax_init( &eax, /* context */ find_cipher("rijndael"), /* cipher id */ nonce, /* the nonce */ 16, /* nonce is 16 bytes */ "TestApp", /* example header */ 7) /* header length */ ) != CRYPT_OK) { printf("Error eax_init: %s", error_to_string(err)); return EXIT_FAILURE; } /* now encrypt data, say in a loop or whatever */ if ((err = eax_encrypt( &eax, /* eax context */ pt, /* plaintext (source) */ ct, /* ciphertext (destination) */ sizeof(pt) /* size of plaintext */ ) != CRYPT_OK) { printf("Error eax_encrypt: %s", error_to_string(err)); return EXIT_FAILURE; } /* finish message and get authentication tag */ taglen = sizeof(tag); if ((err = eax_done( &eax, /* eax context */ tag, /* where to put tag */ &taglen /* length of tag space */ ) != CRYPT_OK) { printf("Error eax_done: %s", error_to_string(err)); return EXIT_FAILURE; } /* now we have the authentication tag in "tag" and * it's taglen bytes long */ } ``` -------------------------------- ### RSA Encryption and Decryption Example Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Demonstrates RSA encryption of data and subsequent decryption using a generated RSA key. Requires registering PRNG and hash algorithms. ```c #include int main(void) { int err, hash_idx, prng_idx, res; unsigned long l1, l2; unsigned char pt[16], pt2[16], out[1024]; rsa_key key; /* register prng/hash */ if (register_prng(&sprng_desc) == -1) { printf("Error registering sprng"); return EXIT_FAILURE; } /* register a math library (in this case TomsFastMath) ltc_mp = tfm_desc; if (register_hash(&sha1_desc) == -1) { printf("Error registering sha1"); return EXIT_FAILURE; } hash_idx = find_hash("sha1"); prng_idx = find_prng("sprng"); /* make an RSA-1024 key */ if ((err = rsa_make_key(NULL, /* PRNG state */ prng_idx, /* PRNG idx */ 1024/8, /* 1024-bit key */ 65537, /* we like e=65537 */ &key) /* where to store the key */ ) != CRYPT_OK) { printf("rsa_make_key %s", error_to_string(err)); return EXIT_FAILURE; } /* fill in pt[] with a key we want to send ... */ l1 = sizeof(out); if ((err = rsa_encrypt_key(pt, /* data we wish to encrypt */ 16, /* data is 16 bytes long */ out, /* where to store ciphertext */ &l1, /* length of ciphertext */ "TestApp", /* our lparam for this program */ 7, /* lparam is 7 bytes long */ NULL, /* PRNG state */ prng_idx, /* prng idx */ hash_idx, /* hash idx */ &key) /* our RSA key */ ) != CRYPT_OK) { printf("rsa_encrypt_key %s", error_to_string(err)); return EXIT_FAILURE; } /* now let's decrypt the encrypted key */ l2 = sizeof(pt2); if ((err = rsa_decrypt_key(out, /* encrypted data */ l1, /* length of ciphertext */ pt2, /* where to put plaintext */ &l2, /* plaintext length */ "TestApp", /* lparam for this program */ 7, /* lparam is 7 bytes long */ hash_idx, /* hash idx */ &res, /* validity of data */ &key) /* our RSA key */ ) != CRYPT_OK) { printf("rsa_decrypt_key %s", error_to_string(err)); return EXIT_FAILURE; } /* if all went well pt == pt2, l2 == 16, res == 1 */ } ``` -------------------------------- ### Registering and Using Blowfish Cipher Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Shows how to register the Blowfish cipher and then set it up using its descriptor and the find_cipher function. It includes error checking for registration and setup. ```c #include int main(void) { unsigned char key[8]; symmetric_key skey; int err; /* you must register a cipher before you use it */ if (register_cipher(&blowfish_desc)) == -1) { printf("Unable to register Blowfish cipher."); return -1; } /* generic call to function (assuming the key * in key[] was already setup) */ if ((err = cipher_descriptor[find_cipher("blowfish")]. setup(key, 8, 0, &skey)) != CRYPT_OK) { printf("Error setting up Blowfish: %s\n", error_to_string(err)); return -1; } /* ... use cipher ... */ } ``` -------------------------------- ### Compile Dropbear Programs Source: https://github.com/mkj/dropbear/blob/main/INSTALL.md Compile specific Dropbear programs by listing them in the PROGRAMS variable. This example compiles dropbear, dbclient, dropbearkey, dropbearconvert, and scp. ```bash make PROGRAMS="dropbear dbclient dropbearkey dropbearconvert scp" ``` -------------------------------- ### Basic Flexi Decoder Usage Example Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex This code snippet demonstrates the basic usage of the der_decode_sequence_flexi function. It shows how to call the function and check for errors. Ensure 'inbuf' and 'inbuflen' are properly populated before calling. ```c unsigned char inbuf[MAXSIZE]; unsigned long inbuflen; ltc_asn1_list *list; int err; /* somehow fill inbuf/inbuflen */ if ((err = der_decode_sequence_flexi(inbuf, inbuflen, &list)) != CRYPT_OK) { printf("Error decoding: %s\n", error_to_string(err)); exit(EXIT_FAILURE); } ``` -------------------------------- ### List All Supported Constants Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Retrieves a comma-separated, newline-delimited, null-terminated list of all supported constants and their values. Call twice: first with NULL to get the required buffer size, then with an allocated buffer. ```C int crypt_list_all_constants( char *names_list, unsigned int *names_list_size); ``` -------------------------------- ### PKCS#1 v1.5 Signature Example 10.9 Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/notes/rsa-testvectors/pkcs1v15sign-vectors.txt This snippet presents another message and its corresponding PKCS#1 v1.5 RSA signature. It serves as a test case for RSA signature generation and verification. ```text # Message to be signed: 45 e0 90 56 a2 8e 4b 2e 7c 11 f6 5e 68 8a 1e 3c 33 f0 e5 2c 9a 03 6c 09 d8 1d e5 a6 da b5 8d 4d 55 cf 41 1b 53 ad 64 6e 83 a3 4b 0c 08 c2 21 ae 03 76 ab 76 a7 9d 1f ee 67 1a 58 44 20 56 4f 8e 85 2e b6 f2 d4 27 ae e0 a0 96 dd 72 db e8 50 7c 67 7f 8a a0 0e b7 c2 5d fb 0a 49 dd 88 a6 c7 84 76 b8 00 11 b6 82 8b 3a af 46 47 79 44 22 ba 6b d6 3b 7a b0 e7 da fb d3 6f 6c 41 de a0 3d 73 22 35 64 96 94 60 d9 28 54 0b 73 92 57 e7 0b b6 8d 5c 65 3c 37 96 94 58 95 # Signature: 0a 06 82 f7 42 e7 43 e1 c7 da ba ac 61 8a 78 6f 28 ed 13 a6 58 7a df c3 3c 98 29 d7 52 c1 3e f2 7a 00 c7 e6 d4 5e 27 17 1a 58 41 77 1d 78 69 8c 6c c6 67 78 b8 c0 93 38 e3 5b 9b 6f 59 c0 64 ce b3 eb 20 ce 90 9a 5c 6c ea ae bb e9 3e 86 c7 c5 ff 4a 39 17 f1 26 81 96 32 cf 96 fa b1 d3 91 73 a7 ae 7f c2 ff 5c 0f b4 09 05 35 da db 58 d8 7d 0d a3 db 32 ec ec 13 71 8b 3a c5 c3 0b a8 02 e3 8b ``` -------------------------------- ### PKCS#1 v1.5 Signature Example 10.8 Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/notes/rsa-testvectors/pkcs1v15sign-vectors.txt This snippet shows a message and its corresponding PKCS#1 v1.5 RSA signature. It is used for testing RSA signing algorithms. ```text # Message to be signed: e6 9a c9 43 3e 6c 28 ac 53 f8 03 4a 86 8d a9 88 3e 31 9e 82 e6 bc 2e 49 45 5e 6e 4f 09 8b 53 f2 87 a8 58 da 1d 87 6a 9a 5a 6a 9f c1 4f d2 42 38 cd 4e 4b 57 31 07 7a 4d bd d5 03 8a 9b c1 f5 de f4 3f ec 77 f6 7e b0 62 fa ef ef 7d 04 29 23 8b 25 d0 31 85 78 96 62 3a 3f 1d 37 bf # Signature: 08 a0 20 e4 20 98 78 f1 e6 37 ad 59 da af 83 5d af 4c a6 64 84 47 94 c1 c6 48 f0 e2 23 3d ba 75 48 bd 16 1f 0c 0a 18 24 d7 62 03 1a 41 75 72 84 2f 8e 64 4a a9 3f 9d 91 dd 77 09 e1 6a 42 9c c1 43 90 3e f4 f8 37 a4 58 39 6b ca c2 40 92 b0 17 24 c6 fe 3d d1 ad 24 3f 3f 70 b5 ae 6f aa 09 f3 70 ca a5 12 10 4b 91 76 06 0d f2 bf 12 1c bc e9 19 8e c2 fe 45 a5 9e bd dc 46 75 32 b5 af b9 b2 35 ``` -------------------------------- ### Blowfish ECB Encryption and Decryption Example Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Demonstrates how to encrypt and decrypt an 8-byte block using Blowfish in ECB mode with libtomcrypt. Ensure the key is loaded appropriately before use. ```c #include int main() { unsigned char pt[8], ct[8], key[8]; symmetric_key skey; int err; /* ... key is loaded appropriately in key ... */ /* ... load a block of plaintext in pt ... */ /* schedule the key */ if ((err = blowfish_setup(key, /* the key we will use */ 8, /* key is 8 bytes (64-bits) long */ 0, /* 0 == use default # of rounds */ &skey) /* where to put the scheduled key */ ) != CRYPT_OK) { printf("Setup error: %s\n", error_to_string(err)); return -1; } /* encrypt the block */ blowfish_ecb_encrypt(pt, /* encrypt this 8-byte array */ ct, /* store encrypted data here */ &skey); /* our previously scheduled key */ /* now ct holds the encrypted version of pt */ /* decrypt the block */ blowfish_ecb_decrypt(ct, /* decrypt this 8-byte array */ pt, /* store decrypted data here */ &skey); /* our previously scheduled key */ /* now we have decrypted ct to the original plaintext in pt */ /* Terminate the cipher context */ blowfish_done(&skey); return 0; } ``` -------------------------------- ### SPRNG for ECC Key Generation Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Demonstrates using the secure PRNG (SPRNG) directly for ECC key generation, simplifying setup as it does not require explicit start, add_entropy, or ready calls. ```c #include int main(void) { ecc_key mykey; int err; /* register SPRNG */ if (register_prng(&sprng_desc) == -1) { printf("Error registering SPRNG\n"); return -1; } /* make a 192-bit ECC key */ if ((err = ecc_make_key(NULL, find_prng("sprng"), 24, &mykey)) != CRYPT_OK) { printf("Error making key: %s\n", error_to_string(err)); return -1; } return 0; } ``` -------------------------------- ### Enable Dropbear to Create Host Keys on First Connection Source: https://github.com/mkj/dropbear/blob/main/README.md Configure Dropbear to automatically create host keys when the system boots or on the first connection. Ensure the directory `/etc/dropbear/` exists and pass the `-R` option to the `dropbear` server. ```sh dropbear -R ``` -------------------------------- ### Dynamic Hash Selection Example Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Shows how to dynamically select and use a hash function at runtime based on user input. It registers MD5, prompts for a hash name, and then hashes input until a blank line is entered. ```c #include int main(void) { unsigned char buffer[100], hash[MAXBLOCKSIZE]; int idx, x; hash_state md; /* register hashes .... if (register_hash(&md5_desc) == -1) { printf("Error registering MD5.\n"); return -1; } /* register other hashes ... */ /* prompt for name and strip newline */ printf("Enter hash name: \n"); fgets(buffer, sizeof(buffer), stdin); buffer[strlen(buffer) - 1] = 0; /* get hash index */ idx = find_hash(buffer); if (idx == -1) { printf("Invalid hash name!\n"); return -1; } /* hash input until blank line */ hash_descriptor[idx].init(&md); while (fgets(buffer, sizeof(buffer), stdin) != NULL) hash_descriptor[idx].process(&md, buffer, strlen(buffer)); hash_descriptor[idx].done(&md, hash); /* dump to screen */ for (x = 0; x < hash_descriptor[idx].hashsize; x++) printf("%02x ", hash[x]); printf("\n"); return 0; } ``` -------------------------------- ### RSAES-OAEP Encryption Example Message Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/notes/rsa-testvectors/oaep-vect.txt The message to be encrypted using RSAES-OAEP, as shown in Example 9.1. ```plaintext f7 35 fd 55 ba 92 59 2c 3b 52 b8 f9 c4 f6 9a aa 1c be f8 fe 88 ad d0 95 59 54 12 46 7f 9c f4 ec 0b 89 6c 59 ed a1 62 10 e7 54 9c 8a bb 10 cd bc 21 a1 2e c9 b6 b5 b8 fd 2f 10 39 9e b6 ``` -------------------------------- ### Yarrow PRNG Example Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Demonstrates reading bytes from the Yarrow PRNG. Note: This example is not secure due to non-random entropy addition. ```c #include int main(void) { prng_state prng; unsigned char buf[10]; int err; /* start it */ if ((err = yarrow_start(&prng)) != CRYPT_OK) { printf("Start error: %s\n", error_to_string(err)); } /* add entropy */ if ((err = yarrow_add_entropy("hello world", 11, &prng)) != CRYPT_OK) { printf("Add_entropy error: %s\n", error_to_string(err)); } /* ready and read */ if ((err = yarrow_ready(&prng)) != CRYPT_OK) { printf("Ready error: %s\n", error_to_string(err)); } printf("Read %lu bytes from yarrow\n", yarrow_read(buf, sizeof(buf), &prng)); return 0; } ``` -------------------------------- ### Build and Install Shared Library with Extra Libraries and CFLAGS Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Builds and installs the shared version of LibTomCrypt using 'makefile.shared'. It requires specifying EXTRALIBS and CFLAGS, and links against external shared libraries like TomsFastMath. ```makefile make -f makefile.shared install CFLAGS="-DTFM_DESC" EXTRALIBS=-ltfm ``` -------------------------------- ### blake2bmac_init Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Initializes a BLAKE2b-MAC state. ```APIDOC ## blake2bmac_init ### Description Initializes the BLAKE2b-MAC state *st*, with the provided key and output length. ### Function Signature ```c int blake2bmac_init(blake2bmac_state *st, unsigned long outlen, const unsigned char *key, unsigned long keylen); ``` ### Parameters - **st** (*blake2bmac_state* *) - Pointer to the BLAKE2b-MAC state to initialize. - **outlen** (*unsigned long*) - The desired size of the final tag in octets (up to 64). - **key** (*const unsigned char* *) - Pointer to the secret key. - **keylen** (*unsigned long*) - Length of the key in octets (up to 64). ``` -------------------------------- ### Get Digit Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Retrieves a specific digit from a multi-precision integer. ```APIDOC ## get_digit ### Description Retrieves a specific digit (of size bits_per_digit) from a multi-precision integer. ### Parameters #### Path Parameters - **a** (void*) - The number to read from. - **n** (int) - The index of the digit to fetch. ### Returns - The bits_per_digit sized n'th digit of the integer. ``` -------------------------------- ### Configure Dropbear Source: https://github.com/mkj/dropbear/blob/main/INSTALL.md Run the configure script to set up the build environment for Dropbear. Use optional flags like --disable-zlib or --help for more options. If configure.ac is edited, run autoconf and autoheader first. ```bash ./configure ``` -------------------------------- ### Get Integer Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Retrieves the lower bits_per_digit of a multi-precision integer. ```APIDOC ## get_int ### Description Retrieves the lower bits_per_digit of a multi-precision integer. ### Parameters #### Path Parameters - **a** (void*) - The integer to read from. ### Returns - The lower bits_per_digit of the integer (unsigned). ``` -------------------------------- ### blake2smac_init Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Initializes a BLAKE2s-MAC state. ```APIDOC ## blake2smac_init ### Description Initializes the BLAKE2s-MAC state *st*, with the provided key and output length. ### Function Signature ```c int blake2smac_init(blake2smac_state *st, unsigned long outlen, const unsigned char *key, unsigned long keylen); ``` ### Parameters - **st** (*blake2smac_state* *) - Pointer to the BLAKE2s-MAC state to initialize. - **outlen** (*unsigned long*) - The desired size of the final tag in octets (up to 64). - **key** (*const unsigned char* *) - Pointer to the secret key. - **keylen** (*unsigned long*) - Length of the key in octets (up to 64). ``` -------------------------------- ### Unsigned Size Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Gets the size of a multi-precision integer in octets (bytes). ```APIDOC ## unsigned_size ### Description Calculates the number of octets (bytes) required to store a multi-precision integer. ### Parameters #### Path Parameters - **a** (void*) - The integer to get the size of. ### Returns - The length of the integer in octets. ``` -------------------------------- ### Example: Using SHA3 SHAKE256 with Arbitrary Output Length Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Shows how to initialize, process data with, and finalize the SHA3 SHAKE256 algorithm to produce an arbitrary length message digest. This utilizes the Extendable Output Functions (XOF) mode. ```c #include int main(void) { int err; hash_state state; const void* msg = "The quick brown fox jumps over the lazy dog"; unsigned char output[345]; if ((err = sha3_shake_init(&state, 256)) != CRYPT_OK) { printf("Could not init SHAKE256 (%s)\n", error_to_string(err)); return EXIT_FAILURE; } if ((err = sha3_shake_process(&state, msg, strlen(msg))) != CRYPT_OK) { printf("Could not process SHAKE256 (%s)\n", error_to_string(err)); return EXIT_FAILURE; } if ((err = sha3_shake_done(&state, output, sizeof(output))) != CRYPT_OK) { printf("Could not finish SHAKE256 (%s)\n", error_to_string(err)); return EXIT_FAILURE; } return EXIT_SUCCESS; } ``` -------------------------------- ### Get Digit Count Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Counts the number of digits used to represent a multi-precision integer. ```APIDOC ## get_digit_count ### Description Counts the number of digits required to represent a multi-precision integer. ### Parameters #### Path Parameters - **a** (void*) - The number to count. ### Returns - The number of digits used to represent the integer. ``` -------------------------------- ### Get IV in LRW Mode Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Retrieves the current 16-octet IV from the LRW context. ```c int lrw_getiv(unsigned char *IV, unsigned long *len, symmetric_LRW *lrw); ``` -------------------------------- ### Execute the Multi-Binary Source: https://github.com/mkj/dropbear/blob/main/MULTI.md Once symbolic links are set up, you can execute the Dropbear functionalities as you normally would, using the symlinked names. Pass any necessary options after the executable name. ```sh ./dropbear ``` -------------------------------- ### Initialize and Use Pelican MAC Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Demonstrates initializing, processing data with, and terminating a Pelican MAC. Ensure data is processed in multiples of 16 bytes for efficiency. ```c #include int main() { pelican_state pelstate; unsigned char key[32], tag[16]; int err; /* somehow initialize a key */ /* initialize pelican mac */ if ((err = pelican_init(&pelstate, /* the state */ key, /* user key */ 32 /* key length in octets */ )) != CRYPT_OK) { printf("Error initializing Pelican: %s", error_to_string(err)); return EXIT_FAILURE; } /* MAC some data */ if ((err = pelican_process(&pelstate, /* data to mac */ "hello world", /* length of data */ 11 )) != CRYPT_OK) { printf("Error processing Pelican: %s", error_to_string(err)); return EXIT_FAILURE; } /* Terminate the MAC */ if ((err = pelican_done(&pelstate,/* the state */ tag /* where to store the tag */ )) != CRYPT_OK) { printf("Error terminating Pelican: %s", error_to_string(err)); return EXIT_FAILURE; } /* tag[0..15] has the MAC output now */ return EXIT_SUCCESS; } ``` -------------------------------- ### PKCS#1 v1.5 Encryption Example 7.1 - Message Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/notes/rsa-testvectors/pkcs1v15crypt-vectors.txt This is the message to be encrypted using PKCS#1 v1.5. ```plaintext da 50 9d ce 45 e2 47 00 37 9b fe 5a a1 a8 1c 24 70 6c 18 42 d9 b1 3e 7a 2e 0a 15 d3 a4 af 8e 6d 08 61 2d ca a1 5d 46 0e ce 87 29 88 e3 e9 0f b2 7e 5c a5 c1 0f a1 fa cd cb 0e ``` -------------------------------- ### PRNG Setup Function Signature Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Signature for the rng_make_prng function, used to initialize a PRNG with a specified amount of entropy. ```c int rng_make_prng( int bits, int wprng, prng_state *prng, void (*callback)(void)); ``` -------------------------------- ### Create Symbolic Links for Multi-Binary Source: https://github.com/mkj/dropbear/blob/main/MULTI.md After compiling the multi-binary, create symbolic links to it for each desired Dropbear executable (e.g., `dropbear`, `dbclient`). This allows you to run the different functionalities by calling the appropriate symlink. ```sh ln -s dropbearmulti dropbear ``` ```sh ln -s dropbearmulti dbclient ``` -------------------------------- ### f8_start Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Initializes the F8 mode state. It uses the provided key as the secret key, IV as the counter, and salt_key for encrypting the counter. The salt_key can be shorter than the secret key. ```APIDOC ## f8_start ### Description Initializes the F8 mode state using the provided cipher, IV, secret key, salt key, and number of rounds. ### Function Signature ```c int f8_start(int cipher, const unsigned char *IV, const unsigned char *key, int keylen, const unsigned char *salt_key, int skeylen, int num_rounds, symmetric_F8 *f8); ``` ### Parameters - **cipher** (int) - The type of cipher to use. - **IV** (const unsigned char *) - The initial counter value. - **key** (const unsigned char *) - The secret key for encryption/decryption. - **keylen** (int) - The length of the secret key in bytes. - **salt_key** (const unsigned char *) - The key used to encrypt the counter. - **skeylen** (int) - The length of the salt key in bytes. - **num_rounds** (int) - The number of rounds for the cipher. - **f8** (symmetric_F8 *) - Pointer to the F8 mode state structure. ``` -------------------------------- ### Terminate ChaCha20-Poly1305 State and Get Tag Source: https://github.com/mkj/dropbear/blob/main/libtomcrypt/doc/crypt.tex Terminates the ChaCha20-Poly1305 state and retrieves the 16-octet message authentication tag. ```c int chacha20poly1305_done(chacha20poly1305_state *st, unsigned char *tag, unsigned long *taglen); ```