### Install Static Library to Default Paths Source: https://github.com/libtom/libtomcrypt/blob/develop/README.md Installs the static library and header files to their default locations. ```makefile make install ``` -------------------------------- ### Build Static Library with LibTomMath and Test Binary Source: https://github.com/libtom/libtomcrypt/blob/develop/README.md Builds a static library and the 'test' binary, enabling LibTomMath support. Ensure LibTomMath is installed. ```makefile make CFLAGS="-DUSE_LTM -DLTM_DESC" EXTRALIBS="-ltommath" test ``` -------------------------------- ### PKCS#1 v1.5 Encryption Example 8.1 Source: https://github.com/libtom/libtomcrypt/blob/develop/notes/rsa-testvectors/pkcs1v15crypt-vectors.txt Demonstrates PKCS#1 v1.5 encryption of a specific message using a given seed. This example is useful for verifying encryption implementations. ```text Message: f7 26 37 ae c2 8d 2b 6e bf 8f 73 d7 48 75 df 01 cd 12 24 8f 00 20 60 8e 61 c7 3d 1a 01 f9 72 f7 4d ee Seed: cc 20 77 a2 40 01 f3 e3 86 96 94 e4 fe 27 72 bf 93 8f 76 27 66 7b 62 d5 90 f9 ee 4f cf ff bb b4 7a fd 5f ed 6a 18 08 44 a9 12 1a 32 ed 7c bc 56 cd 28 70 a2 d6 96 b9 43 d6 fd e9 76 7c 1b 96 48 61 6c 32 ed 6c 40 0d 42 3d d4 ab 72 16 af ad a0 28 40 2e b2 a1 c3 eb cc 24 5a fc 7f Encryption: 00 72 57 5d 3b 11 c5 ff d6 ae 24 f3 53 ff 74 99 27 b4 ae 5d f4 63 f7 0c 5f 3e f5 49 6b 0d 14 5e b2 b8 a5 3c 28 d5 3e fe 8b f9 f2 7b 2e f4 ce ea 48 31 05 77 c3 d2 b4 b9 49 a1 2e 3d f3 f5 67 76 82 88 18 45 2d 81 bd 45 af 15 8c 87 bb 57 74 5b 8a 10 fc 1a 92 a0 ea 55 c8 51 39 f8 42 f7 3f 1d 61 3b 9a 96 4a f8 ed 72 0d 0e 08 47 f7 ae 5b 30 5c 05 f1 2c bc 4c 9c 16 84 ac f9 02 97 0d 82 09 49 ``` -------------------------------- ### PKCS#1 v1.5 Encryption Example 8.4 Source: https://github.com/libtom/libtomcrypt/blob/develop/notes/rsa-testvectors/pkcs1v15crypt-vectors.txt This entry is a placeholder for PKCS#1 v1.5 encryption example 8.4, which was not fully provided in the source. -------------------------------- ### PKCS#1 v1.5 Encryption Example 9.1 Source: https://github.com/libtom/libtomcrypt/blob/develop/notes/rsa-testvectors/pkcs1v15crypt-vectors.txt Demonstrates PKCS#1 v1.5 encryption of a specific message using a provided seed. This example is useful for verifying encryption implementations. ```text Message: 96 ad 3b a4 d2 9b dd 35 25 bb c5 d0 2e 88 c0 13 3f fd ea 40 94 74 db 34 df 73 3c a5 9c 3a 23 2f 6a 64 c2 14 3c a1 31 a7 f1 8d 00 5b b3 aa 6c a1 ea be aa 13 6b ec 37 db 11 1d 4d 8f 61 Seed: 10 e9 f5 09 3c ec 87 2e db 16 16 2b 6b bd 52 12 f6 10 1a 71 d5 1d c1 42 0c ad d1 d5 50 fa f9 af 40 f5 73 d3 e3 ab 68 91 d8 a8 82 ef 06 56 bc 30 06 2a 05 b1 cc 27 7a 11 c9 ba 2e fd 51 03 c5 6e 23 Encryption: 04 de c0 1b bf 8e 0c 22 96 d9 f5 bc 2d 2b a8 95 34 3e c3 0e c5 44 13 57 6d 80 84 b9 31 43 cf a2 d2 03 3b f4 c2 c2 e0 3f 5b 02 59 ed f1 4a 8f 3e 11 69 85 77 65 2a 2a b9 f5 1a b0 18 7c c7 51 75 c8 6d fc 9a 45 b1 e4 eb 8b 54 47 a9 4a b9 b5 17 7a 04 04 ba 49 e5 7f 83 c9 3f 7f e2 de 24 18 45 f5 81 f6 42 12 43 7e 0c 04 ff 34 ea 1d ee 6d d2 8f 6f f3 36 72 35 44 13 74 f0 f2 ef 71 a9 cd ae 9e ``` -------------------------------- ### Build Shared Library with TomFastMath and All Binaries Source: https://github.com/libtom/libtomcrypt/blob/develop/README.md Builds a shared library and all binaries, enabling TomFastMath support. Ensure TomFastMath is installed. ```makefile make -f makefile.shared CFLAGS="-DUSE_TFM -DTFM_DESC" EXTRALIBS="-ltfm" all demos ``` -------------------------------- ### RSA PKCS#1 v1.5 Encryption Example 1.2 Source: https://github.com/libtom/libtomcrypt/blob/develop/notes/rsa-testvectors/pkcs1v15crypt-vectors.txt This snippet shows another PKCS#1 v1.5 encryption example, likely using similar parameters as Example 1.1 but with different message and seed values. ```text # PKCS#1 v1.5 Encryption Example 1.2 # ---------------------------------- ``` -------------------------------- ### Install Shared Library to Special Path Source: https://github.com/libtom/libtomcrypt/blob/develop/README.md Installs the shared library to a custom path specified by the PREFIX variable. ```makefile make -f makefile.shared PREFIX=/opt/special/path ``` -------------------------------- ### PKCS#1 v1.5 Encryption Example 12.2 Source: https://github.com/libtom/libtomcrypt/blob/develop/notes/rsa-testvectors/pkcs1v15crypt-vectors.txt Provides a second test vector for PKCS#1 v1.5 encryption, featuring a different message and seed. This example is useful for verifying the encryption process with varied inputs. ```text Message: 97 ee a8 56 a9 bd bc 71 4e b3 ac 22 f6 eb 32 71 96 69 c4 2f 94 30 c5 89 50 c6 4c 0d ab ff 3a 9e 20 43 41 6c 67 ca aa ab 7c 68 cc b3 ca 99 a3 Seed: 9f 14 12 61 ce c4 f2 c5 2f 96 91 25 a3 6f 14 10 27 08 82 50 d3 6b 17 42 1c d0 96 14 76 19 06 46 8a fa b7 62 2c 0d 02 19 36 91 74 47 91 e0 d3 5b 6b c9 f3 37 7e 10 b2 85 6c 8e d9 19 9c 89 f4 a4 16 13 d3 c4 0c ca 37 3a 7c c6 3c 52 60 fe 5a Encryption: 0d 26 a0 5d e9 3b 70 7b 85 40 fd c1 98 89 d2 d1 e7 93 71 57 d3 2d 30 3c 52 8d e3 5e 55 3f 94 20 28 74 4a f6 a0 40 2e ca 0f cf 5a 85 26 1a d4 75 d8 71 0c c9 f8 b1 1b a2 c6 da f1 d6 72 69 0c 68 ed 11 e0 35 e9 c6 60 ec e1 d8 0c da b8 00 ea d3 c6 e0 78 61 7a 1b 0d 27 3d ed d8 d6 57 49 16 6b d0 77 74 fb 4c 14 86 aa 8a 0a df 59 5d bc 3d 10 ff ae f1 83 84 98 a6 75 55 c7 7b 6e d9 83 d5 b9 de ``` -------------------------------- ### PKCS#1 v1.5 Encryption Example 8.3 Source: https://github.com/libtom/libtomcrypt/blob/develop/notes/rsa-testvectors/pkcs1v15crypt-vectors.txt Presents another PKCS#1 v1.5 encryption test case with a distinct message and seed, useful for comprehensive testing. ```text Message: d9 9b 4f 10 d9 f3 2e 12 ec fa e2 63 0b 22 ac 02 6a f9 64 b9 c7 15 d2 07 Seed: e0 af 8b 7e ab 36 a6 ee 31 6d 78 13 67 f0 9e a1 1e 31 fd c1 ef 2c f9 c9 7c 37 9e aa cf 68 72 a8 21 3c be 4c e2 e2 9c 77 8b 35 95 40 10 06 3f 17 76 ab 5b 17 2d a6 24 b4 06 a1 c5 8e 0b 57 4a 03 b1 b1 b2 cd 7d 3a 9e 50 35 a9 a9 73 05 8f 04 97 65 2d 2f 73 b1 dc 8f 48 7c 09 cf e7 1d 8f f8 f1 45 8c 79 0e e0 c5 Encryption: 01 02 3b be 85 57 c2 63 0a 26 22 46 db 7a bc 54 03 43 88 70 38 f6 4c 64 1f d7 4e e8 74 ae 96 70 fb 28 62 d4 24 70 3d a2 0b e4 f4 8b 23 9c d0 60 43 81 9d 8f 61 51 44 e2 b1 f0 0c 8f 88 49 2e 62 f6 e0 73 16 f8 49 05 35 3b 0b 18 80 ed 77 da 2b 62 d3 a9 3b b7 0f f6 a5 00 79 38 b9 73 b4 cd 5a bf ee 0c f1 3f 5d 4a b2 16 01 02 68 5c ac 80 81 83 4f 95 55 80 6b b3 22 d0 dc 5b 8a 2b f1 28 45 62 ``` -------------------------------- ### PKCS#1 v1.5 Encryption Example 9.2 Source: https://github.com/libtom/libtomcrypt/blob/develop/notes/rsa-testvectors/pkcs1v15crypt-vectors.txt Provides another test case for PKCS#1 v1.5 encryption, featuring a different message and seed. This is useful for comprehensive testing. ```text Message: 7f 92 ab b6 e5 2e d5 d4 20 84 9e d6 cc ab 36 c3 d0 84 92 55 43 1e 19 3d 67 bd 94 4b 6c 0f ce fb 77 29 cf 5a 31 Seed: ce a9 68 be 78 ab 5f aa c2 27 dc 3c 6f c9 ce d4 9f 85 1e d5 8b 08 d5 ca 37 54 28 48 9a fb ef 3b f5 ed 83 74 6d 95 9a 0a 56 e9 ac 66 ff 2e 7c 8b 8c 3a da 97 fa 15 dd 7f 99 13 41 74 70 70 cc ad 65 42 bd 7f 4b 33 f5 56 04 45 8b 91 03 ae 13 dc 89 b4 e6 2c cb f8 4f f7 3b ``` -------------------------------- ### Build Static Library with GMP, LibTomMath, TomFastMath and Timing Binary Source: https://github.com/libtom/libtomcrypt/blob/develop/README.md Builds a static library and the 'timing' binary for performance measurements, enabling GMP, LibTomMath, and TomFastMath support. Ensure GMP is installed. ```makefile make CFLAGS="-DUSE_GMP -DGMP_DESC -DLTM_DESC -DTFM_DESC" EXTRALIBS="-lgmp" timing ``` -------------------------------- ### RSAES-OAEP Encryption Example 3.1 Source: https://github.com/libtom/libtomcrypt/blob/develop/notes/rsa-testvectors/oaep-vect.txt Encrypts a short message using RSAES-OAEP with a specific seed. This example is useful for testing basic encryption functionality. ```text # Message to be encrypted: 08 78 20 b5 69 e8 fa 8d # Seed: 8c ed 6b 19 62 90 80 57 90 e9 09 07 40 15 e6 a2 0b 0c 48 94 # Encryption: 02 6a 04 85 d9 6a eb d9 6b 43 82 08 50 99 b9 62 e6 a2 bd ec 3d 90 c8 db 62 5e 14 37 2d e8 5e 2d 5b 7b aa b6 5c 8f af 91 bb 55 04 fb 49 5a fc e5 c9 88 b3 f6 a5 2e 20 e1 d6 cb d3 56 6c 5c d1 f2 b8 31 8b b5 42 cc 0e a2 5c 4a ab 99 32 af a2 07 60 ea dd ec 78 43 96 a0 7e a0 ef 24 d4 e6 f4 d3 7e 50 52 a7 a3 1e 14 6a a4 80 a1 11 bb e9 26 40 13 07 e0 0f 41 00 33 84 2b 6d 82 fe 5c e4 df ae 80 ``` -------------------------------- ### Initialize and Use Fortuna PRNG Source: https://context7.com/libtom/libtomcrypt/llms.txt Initializes the Fortuna PRNG, seeds it from the OS entropy source, reads random bytes, and demonstrates state export/import for persistence. Ensure all PRNGs and hashes are registered before use. ```c #include #include int main(void) { register_all_prngs(); register_all_hashes(); /* Fortuna uses SHA-256 internally */ prng_state prng; unsigned char buf[32]; int err; int wprng = find_prng("fortuna"); /* Initialize and seed from OS entropy */ if ((err = rng_make_prng(128, wprng, &prng, NULL)) != CRYPT_OK) { fprintf(stderr, "rng_make_prng: %s\n", error_to_string(err)); return 1; } /* Read 32 random bytes */ if (fortuna_read(buf, sizeof(buf), &prng) != sizeof(buf)) { fprintf(stderr, "fortuna_read failed\n"); return 1; } printf("Random bytes: "); for (int i = 0; i < 32; i++) printf("%02x", buf[i]); printf("\n"); /* Export/import state for persistence */ unsigned char state[1024]; unsigned long state_len = sizeof(state); fortuna_export(state, &state_len, &prng); /* ... save state to disk ... */ fortuna_done(&prng); /* Restore */ fortuna_start(&prng); fortuna_import(state, state_len, &prng); fortuna_ready(&prng); fortuna_done(&prng); return 0; } ``` -------------------------------- ### Build Static Library Source: https://github.com/libtom/libtomcrypt/blob/develop/README.md Use this target to build the library as a static library. ```makefile make ``` -------------------------------- ### RSAES-OAEP Encryption Example 3.3 Source: https://github.com/libtom/libtomcrypt/blob/develop/notes/rsa-testvectors/oaep-vect.txt Encrypts a message of moderate length using RSAES-OAEP with a different seed. This example is useful for testing OAEP with varying message sizes. ```text # Message to be encrypted: d9 4c d0 e0 8f a4 04 ed 89 # Seed: ce 89 28 f6 05 95 58 25 40 08 ba dd 97 94 fa dc d2 fd 1f 65 # Encryption: 02 39 bc e6 81 03 24 41 52 88 77 d6 d1 c8 bb 28 aa 3b c9 7f 1d f5 84 56 36 18 99 57 97 68 38 44 ca 86 66 47 32 f4 be d7 a0 aa b0 83 aa ab fb 72 38 f5 82 e3 09 58 c2 02 4e 44 e5 70 43 b9 79 50 fd 54 3d a9 77 c9 0c dd e5 33 7d 61 84 42 f9 9e 60 d7 78 3a b5 9c e6 dd 9d 69 c4 7a d1 e9 62 be c2 2d 05 89 5c ff 8d 3f 64 ed 52 61 d9 2b 26 78 51 03 93 48 49 90 ba 3f 7f 06 81 8a e6 ff ce 8a 3a ``` -------------------------------- ### RSAES-OAEP Encryption Example 3.4 Source: https://github.com/libtom/libtomcrypt/blob/develop/notes/rsa-testvectors/oaep-vect.txt Encrypts a message of moderate length using RSAES-OAEP with a different seed. This example is useful for testing OAEP with varying message sizes. ```text # Message to be encrypted: 6c c6 41 b6 b6 1e 6f 96 39 74 da d2 3a 90 13 28 4e f1 # Seed: 6e 29 79 f5 2d 68 14 a5 7d 83 b0 90 05 48 88 f1 19 a5 b9 a3 # Encryption: 02 99 4c 62 af d7 6f 49 8b a1 fd 2c f6 42 85 7f ca 81 f4 37 3c b0 8f 1c ba ee 6f 02 5c 3b 51 2b 42 c3 e8 77 91 13 47 66 48 03 9d be 04 93 f9 24 62 92 fa c2 89 50 60 0e 7c 0f 32 ed f9 c8 1b 9d ec 45 c3 bd e0 cc 8d 88 47 59 01 69 90 7b 7d c5 99 1c eb 29 bb 07 14 d6 13 d9 6d f0 f1 2e c5 d8 d3 50 7c 8e e7 ae 78 dd 83 f2 16 fa 61 de 10 03 63 ac a4 8a 7e 91 4a e9 f4 2d df be 94 3b 09 d9 a0 ``` -------------------------------- ### RSAES-OAEP Encryption Example 3.5 Source: https://github.com/libtom/libtomcrypt/blob/develop/notes/rsa-testvectors/oaep-vect.txt Encrypts a longer message using RSAES-OAEP with a specific seed. This example is useful for testing OAEP with larger data payloads and different seeds. ```text # Message to be encrypted: df 51 51 83 2b 61 f4 f2 58 91 fb 41 72 f3 28 d2 ed df 83 71 ff cf db e9 97 93 92 95 f3 0e ca 69 18 01 7c fd a1 15 3b f7 a6 af 87 59 32 23 # Seed: 2d 76 0b fe 38 c5 9d e3 4c dc 8b 8c 78 a3 8e 66 28 4a 2d 27 ``` -------------------------------- ### Registering Algorithms Source: https://context7.com/libtom/libtomcrypt/llms.txt Demonstrates how to register all compiled-in ciphers, hashes, and PRNGs, and how to find a specific cipher by name. ```APIDOC ## Registering Algorithms ### Description Before using any cipher, hash, or PRNG, register it with the global descriptor table using `register_cipher()`, `register_hash()`, or `register_prng()`. The convenience wrappers `register_all_ciphers()`, `register_all_hashes()`, and `register_all_prngs()` register all compiled-in algorithms at once. ### Usage Example ```c #include int main(void) { /* Register all compiled-in algorithms at startup */ if (register_all_ciphers() != CRYPT_OK) { fprintf(stderr, "Failed to register ciphers\n"); return 1; } if (register_all_hashes() != CRYPT_OK) { fprintf(stderr, "Failed to register hashes\n"); return 1; } if (register_all_prngs() != CRYPT_OK) { fprintf(stderr, "Failed to register PRNGs\n"); return 1; } /* Verify a cipher is available */ int aes_idx = find_cipher("aes"); if (aes_idx == -1) { fprintf(stderr, "AES not found\n"); return 1; } printf("AES registered at index %d\n", aes_idx); return 0; } /* Output: AES registered at index */ ``` ``` -------------------------------- ### Build with CMake Source: https://github.com/libtom/libtomcrypt/blob/develop/README.md Builds the project using CMake. This involves cloning the repository, creating a build directory, configuring with CMake, and then compiling. ```bash git clone https://github.com/libtom/libtomcrypt.git mkdir -p libtomcrypt/build cd libtomcrypt/build cmake .. make -j$(nproc) ``` -------------------------------- ### RSA PKCS#1 v1.5 Encryption Example 1.1 Source: https://github.com/libtom/libtomcrypt/blob/develop/notes/rsa-testvectors/pkcs1v15crypt-vectors.txt This snippet shows a PKCS#1 v1.5 encryption example using a 1024-bit RSA key. It includes the message, seed, and the resulting ciphertext. ```text # PKCS#1 v1.5 Encryption Example 1.1 # ---------------------------------- # Message: 66 28 19 4e 12 07 3d b0 3b a9 4c da 9e f9 53 23 97 d5 0d ba 79 b9 87 00 4a fe fe 34 # Seed: 01 73 41 ae 38 75 d5 f8 71 01 f8 cc 4f a9 b9 bc 15 6b b0 46 28 fc cd b2 f4 f1 1e 90 5b d3 a1 55 d3 76 f5 93 bd 73 04 21 08 74 eb a0 8a 5e 22 bc cc b4 c9 d3 88 2a 93 a5 4d b0 22 f5 03 d1 63 38 b6 b7 ce 16 dc 7f 4b bf 9a 96 b5 97 72 d6 60 6e 97 47 c7 64 9b f9 e0 83 db 98 18 84 a9 54 ab 3c 6f # Encryption: 50 b4 c1 41 36 bd 19 8c 2f 3c 3e d2 43 fc e0 36 e1 68 d5 65 17 98 4a 26 3c d6 64 92 b8 08 04 f1 69 d2 10 f2 b9 bd fb 48 b1 2f 9e a0 50 09 c7 7d a2 57 cc 60 0c ce fe 3a 62 83 78 9d 8e a0 e6 07 ac 58 e2 69 0e c4 eb c1 01 46 e8 cb aa 5e d4 d5 cc e6 fe 7b 0f f9 ef c1 ea bb 56 4d bf 49 82 85 f4 49 ee 61 dd 7b 42 ee 5b 58 92 cb 90 60 1f 30 cd a0 7b f2 64 89 31 0b cd 23 b5 28 ce ab 3c 31 ``` -------------------------------- ### Hash Functions: One-Shot and Streaming APIs Source: https://context7.com/libtom/libtomcrypt/llms.txt Demonstrates one-shot hashing with `hash_memory` for SHA-256 and streaming operations for SHA-3-256 and BLAKE2b. Ensure all required hash algorithms are registered before use. ```c #include #include int main(void) { register_all_hashes(); unsigned char digest[32]; unsigned long digestlen = sizeof(digest); const unsigned char msg[] = "The quick brown fox jumps over the lazy dog"; int err; /* One-shot: SHA-256 */ err = hash_memory(find_hash("sha256"), msg, sizeof(msg) - 1, digest, &digestlen); if (err != CRYPT_OK) { fprintf(stderr, "hash_memory: %s\n", error_to_string(err)); return 1; } printf("SHA-256: "); for (unsigned long i = 0; i < digestlen; i++) printf("%02x", digest[i]); printf("\n"); /* Output: SHA-256: d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592 */ /* Streaming: SHA-3-256 */ hash_state md; unsigned char sha3_digest[32]; sha3_256_init(&md); sha3_process(&md, msg, sizeof(msg) - 1); sha3_done(&md, sha3_digest); printf("SHA3-256: "); for (int i = 0; i < 32; i++) printf("%02x", sha3_digest[i]); printf("\n"); /* BLAKE2b-256 keyed MAC */ hash_state b2; unsigned char b2_digest[32]; unsigned char b2_key[] = "supersecretkey!"; blake2b_init(&b2, 32, b2_key, sizeof(b2_key) - 1); blake2b_process(&b2, msg, sizeof(msg) - 1); blake2b_done(&b2, b2_digest); return 0; } ```