ruby_olm 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/ext/ruby_olm/ext_lib_olm/ext_account.c +274 -0
- data/ext/ruby_olm/ext_lib_olm/ext_lib_olm.c +51 -0
- data/ext/ruby_olm/ext_lib_olm/ext_lib_olm.h +13 -0
- data/ext/ruby_olm/ext_lib_olm/ext_session.c +363 -0
- data/ext/ruby_olm/ext_lib_olm/ext_utility.c +69 -0
- data/ext/ruby_olm/ext_lib_olm/extconf.rb +69 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_account.cpp +695 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_account.h +56 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_inbound_group_session.cpp +654 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_inbound_group_session.h +51 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_jni.h +81 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_jni_helper.cpp +224 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_jni_helper.h +30 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_manager.cpp +35 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_manager.h +36 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_outbound_group_session.cpp +563 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_outbound_group_session.h +49 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_pk.cpp +716 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_pk.h +48 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_session.cpp +977 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_session.h +59 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_utility.cpp +236 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_utility.h +40 -0
- data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_decode_message.cpp +14 -0
- data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_decrypt.cpp +65 -0
- data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_group_decrypt.cpp +73 -0
- data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_unpickle_account.cpp +14 -0
- data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_unpickle_session.cpp +14 -0
- data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/include/fuzzing.hh +82 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/account.hh +160 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/base64.h +77 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/base64.hh +63 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/cipher.h +138 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/crypto.h +202 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/error.h +72 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/inbound_group_session.h +235 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/list.hh +119 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/megolm.h +95 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/memory.h +41 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/memory.hh +90 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/message.h +93 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/message.hh +138 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/olm.h +451 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/olm.hh +4 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/outbound_group_session.h +181 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pickle.h +90 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pickle.hh +149 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pickle_encoding.h +76 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pk.h +214 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/ratchet.hh +184 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/session.hh +156 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/utility.hh +61 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/aes.c +1073 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/aes.h +123 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/aes_test.c +276 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/arcfour.c +45 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/arcfour.h +30 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/arcfour_test.c +47 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/base64.c +135 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/base64.h +27 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/base64_test.c +54 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/blowfish.c +269 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/blowfish.h +32 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/blowfish_test.c +68 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/des.c +269 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/des.h +37 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/des_test.c +83 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md2.c +104 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md2.h +33 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md2_test.c +58 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md5.c +189 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md5.h +34 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md5_test.c +60 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/rot-13.c +35 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/rot-13.h +20 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/rot-13_test.c +44 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha1.c +149 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha1.h +35 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha1_test.c +58 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha256.c +159 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha256.h +34 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha256_test.c +61 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/contrib/Curve25519Donna.c +118 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/contrib/Curve25519Donna.h +53 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/curve25519-donna-c64.c +449 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/curve25519-donna.c +860 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/python-src/curve25519/curve25519module.c +105 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/speed-curve25519.c +50 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/test-curve25519.c +54 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/test-noncanon.c +39 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/test-sc-curve25519.c +72 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna.h +18 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/add_scalar.c +56 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/ed25519.h +38 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/fe.c +1493 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/fe.h +41 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/fixedint.h +72 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/ge.c +467 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/ge.h +74 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/key_exchange.c +79 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/keypair.c +16 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/precomp_data.h +1391 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sc.c +814 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sc.h +12 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/seed.c +40 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sha512.c +275 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sha512.h +21 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sign.c +31 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/verify.c +77 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/test.c +150 -0
- data/ext/ruby_olm/ext_lib_olm/olm/python/dummy/stddef.h +0 -0
- data/ext/ruby_olm/ext_lib_olm/olm/python/dummy/stdint.h +0 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/account.cpp +380 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/base64.cpp +167 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/cipher.cpp +152 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/crypto.cpp +299 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/ed25519.c +22 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/error.c +44 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/inbound_group_session.c +524 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/megolm.c +150 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/memory.cpp +45 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/message.cpp +401 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/olm.cpp +738 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/outbound_group_session.c +363 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/pickle.cpp +242 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/pickle_encoding.c +92 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/pk.cpp +412 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/ratchet.cpp +625 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/session.cpp +462 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/utility.cpp +57 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/include/unittest.hh +107 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_base64.cpp +70 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_crypto.cpp +246 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_group_session.cpp +329 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_list.cpp +92 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_megolm.cpp +134 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_message.cpp +112 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm.cpp +405 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_decrypt.cpp +90 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_sha256.cpp +20 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_signature.cpp +81 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_using_malloc.cpp +210 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_pk.cpp +166 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_ratchet.cpp +221 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_session.cpp +144 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMAccount.h +51 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMAccount_Private.h +25 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMInboundGroupSession.h +38 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMKit.h +37 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMMessage.h +38 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMOutboundGroupSession.h +32 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMPkDecryption.h +71 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMPkEncryption.h +42 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMPkMessage.h +31 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMSerializable.h +29 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMSession.h +44 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMSession_Private.h +26 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMUtility.h +49 -0
- data/ext/ruby_olm/ext_lib_olm/staging/account.cpp +380 -0
- data/ext/ruby_olm/ext_lib_olm/staging/aes.c +1073 -0
- data/ext/ruby_olm/ext_lib_olm/staging/base64.cpp +167 -0
- data/ext/ruby_olm/ext_lib_olm/staging/cipher.cpp +152 -0
- data/ext/ruby_olm/ext_lib_olm/staging/crypto.cpp +299 -0
- data/ext/ruby_olm/ext_lib_olm/staging/curve25519-donna.c +860 -0
- data/ext/ruby_olm/ext_lib_olm/staging/ed25519.c +22 -0
- data/ext/ruby_olm/ext_lib_olm/staging/error.c +44 -0
- data/ext/ruby_olm/ext_lib_olm/staging/inbound_group_session.c +524 -0
- data/ext/ruby_olm/ext_lib_olm/staging/megolm.c +150 -0
- data/ext/ruby_olm/ext_lib_olm/staging/memory.cpp +45 -0
- data/ext/ruby_olm/ext_lib_olm/staging/message.cpp +401 -0
- data/ext/ruby_olm/ext_lib_olm/staging/olm.cpp +738 -0
- data/ext/ruby_olm/ext_lib_olm/staging/outbound_group_session.c +363 -0
- data/ext/ruby_olm/ext_lib_olm/staging/pickle.cpp +242 -0
- data/ext/ruby_olm/ext_lib_olm/staging/pickle_encoding.c +92 -0
- data/ext/ruby_olm/ext_lib_olm/staging/pk.cpp +412 -0
- data/ext/ruby_olm/ext_lib_olm/staging/ratchet.cpp +625 -0
- data/ext/ruby_olm/ext_lib_olm/staging/session.cpp +461 -0
- data/ext/ruby_olm/ext_lib_olm/staging/sha256.c +159 -0
- data/ext/ruby_olm/ext_lib_olm/staging/utility.cpp +57 -0
- data/lib/ruby_olm/account.rb +42 -0
- data/lib/ruby_olm/message.rb +6 -0
- data/lib/ruby_olm/olm_error.rb +70 -0
- data/lib/ruby_olm/olm_message.rb +25 -0
- data/lib/ruby_olm/pre_key_message.rb +6 -0
- data/lib/ruby_olm/session.rb +16 -0
- data/lib/ruby_olm/version.rb +5 -0
- data/lib/ruby_olm.rb +10 -0
- data/rakefile +18 -0
- data/test/examples/test_bob_no_answer.rb +62 -0
- data/test/examples/test_exchange.rb +60 -0
- data/test/spec/test_account.rb +152 -0
- data/test/unit/test_account_methods.rb +85 -0
- metadata +282 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/*********************************************************************
|
|
2
|
+
* Filename: aes.h
|
|
3
|
+
* Author: Brad Conte (brad AT bradconte.com)
|
|
4
|
+
* Copyright:
|
|
5
|
+
* Disclaimer: This code is presented "as is" without any guarantees.
|
|
6
|
+
* Details: Defines the API for the corresponding AES implementation.
|
|
7
|
+
*********************************************************************/
|
|
8
|
+
|
|
9
|
+
#ifndef AES_H
|
|
10
|
+
#define AES_H
|
|
11
|
+
|
|
12
|
+
/*************************** HEADER FILES ***************************/
|
|
13
|
+
#include <stddef.h>
|
|
14
|
+
|
|
15
|
+
/****************************** MACROS ******************************/
|
|
16
|
+
#define AES_BLOCK_SIZE 16 // AES operates on 16 bytes at a time
|
|
17
|
+
|
|
18
|
+
/**************************** DATA TYPES ****************************/
|
|
19
|
+
typedef unsigned char BYTE; // 8-bit byte
|
|
20
|
+
typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines
|
|
21
|
+
|
|
22
|
+
/*********************** FUNCTION DECLARATIONS **********************/
|
|
23
|
+
///////////////////
|
|
24
|
+
// AES
|
|
25
|
+
///////////////////
|
|
26
|
+
// Key setup must be done before any AES en/de-cryption functions can be used.
|
|
27
|
+
void aes_key_setup(const BYTE key[], // The key, must be 128, 192, or 256 bits
|
|
28
|
+
WORD w[], // Output key schedule to be used later
|
|
29
|
+
int keysize); // Bit length of the key, 128, 192, or 256
|
|
30
|
+
|
|
31
|
+
void aes_encrypt(const BYTE in[], // 16 bytes of plaintext
|
|
32
|
+
BYTE out[], // 16 bytes of ciphertext
|
|
33
|
+
const WORD key[], // From the key setup
|
|
34
|
+
int keysize); // Bit length of the key, 128, 192, or 256
|
|
35
|
+
|
|
36
|
+
void aes_decrypt(const BYTE in[], // 16 bytes of ciphertext
|
|
37
|
+
BYTE out[], // 16 bytes of plaintext
|
|
38
|
+
const WORD key[], // From the key setup
|
|
39
|
+
int keysize); // Bit length of the key, 128, 192, or 256
|
|
40
|
+
|
|
41
|
+
///////////////////
|
|
42
|
+
// AES - CBC
|
|
43
|
+
///////////////////
|
|
44
|
+
int aes_encrypt_cbc(const BYTE in[], // Plaintext
|
|
45
|
+
size_t in_len, // Must be a multiple of AES_BLOCK_SIZE
|
|
46
|
+
BYTE out[], // Ciphertext, same length as plaintext
|
|
47
|
+
const WORD key[], // From the key setup
|
|
48
|
+
int keysize, // Bit length of the key, 128, 192, or 256
|
|
49
|
+
const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
|
|
50
|
+
|
|
51
|
+
// Only output the CBC-MAC of the input.
|
|
52
|
+
int aes_encrypt_cbc_mac(const BYTE in[], // plaintext
|
|
53
|
+
size_t in_len, // Must be a multiple of AES_BLOCK_SIZE
|
|
54
|
+
BYTE out[], // Output MAC
|
|
55
|
+
const WORD key[], // From the key setup
|
|
56
|
+
int keysize, // Bit length of the key, 128, 192, or 256
|
|
57
|
+
const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
|
|
58
|
+
|
|
59
|
+
///////////////////
|
|
60
|
+
// AES - CTR
|
|
61
|
+
///////////////////
|
|
62
|
+
void increment_iv(BYTE iv[], // Must be a multiple of AES_BLOCK_SIZE
|
|
63
|
+
int counter_size); // Bytes of the IV used for counting (low end)
|
|
64
|
+
|
|
65
|
+
void aes_encrypt_ctr(const BYTE in[], // Plaintext
|
|
66
|
+
size_t in_len, // Any byte length
|
|
67
|
+
BYTE out[], // Ciphertext, same length as plaintext
|
|
68
|
+
const WORD key[], // From the key setup
|
|
69
|
+
int keysize, // Bit length of the key, 128, 192, or 256
|
|
70
|
+
const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
|
|
71
|
+
|
|
72
|
+
void aes_decrypt_ctr(const BYTE in[], // Ciphertext
|
|
73
|
+
size_t in_len, // Any byte length
|
|
74
|
+
BYTE out[], // Plaintext, same length as ciphertext
|
|
75
|
+
const WORD key[], // From the key setup
|
|
76
|
+
int keysize, // Bit length of the key, 128, 192, or 256
|
|
77
|
+
const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long
|
|
78
|
+
|
|
79
|
+
///////////////////
|
|
80
|
+
// AES - CCM
|
|
81
|
+
///////////////////
|
|
82
|
+
// Returns True if the input parameters do not violate any constraint.
|
|
83
|
+
int aes_encrypt_ccm(const BYTE plaintext[], // IN - Plaintext.
|
|
84
|
+
WORD plaintext_len, // IN - Plaintext length.
|
|
85
|
+
const BYTE associated_data[], // IN - Associated Data included in authentication, but not encryption.
|
|
86
|
+
unsigned short associated_data_len, // IN - Associated Data length in bytes.
|
|
87
|
+
const BYTE nonce[], // IN - The Nonce to be used for encryption.
|
|
88
|
+
unsigned short nonce_len, // IN - Nonce length in bytes.
|
|
89
|
+
BYTE ciphertext[], // OUT - Ciphertext, a concatination of the plaintext and the MAC.
|
|
90
|
+
WORD *ciphertext_len, // OUT - The length of the ciphertext, always plaintext_len + mac_len.
|
|
91
|
+
WORD mac_len, // IN - The desired length of the MAC, must be 4, 6, 8, 10, 12, 14, or 16.
|
|
92
|
+
const BYTE key[], // IN - The AES key for encryption.
|
|
93
|
+
int keysize); // IN - The length of the key in bits. Valid values are 128, 192, 256.
|
|
94
|
+
|
|
95
|
+
// Returns True if the input parameters do not violate any constraint.
|
|
96
|
+
// Use mac_auth to ensure decryption/validation was preformed correctly.
|
|
97
|
+
// If authentication does not succeed, the plaintext is zeroed out. To overwride
|
|
98
|
+
// this, call with mac_auth = NULL. The proper proceedure is to decrypt with
|
|
99
|
+
// authentication enabled (mac_auth != NULL) and make a second call to that
|
|
100
|
+
// ignores authentication explicitly if the first call failes.
|
|
101
|
+
int aes_decrypt_ccm(const BYTE ciphertext[], // IN - Ciphertext, the concatination of encrypted plaintext and MAC.
|
|
102
|
+
WORD ciphertext_len, // IN - Ciphertext length in bytes.
|
|
103
|
+
const BYTE assoc[], // IN - The Associated Data, required for authentication.
|
|
104
|
+
unsigned short assoc_len, // IN - Associated Data length in bytes.
|
|
105
|
+
const BYTE nonce[], // IN - The Nonce to use for decryption, same one as for encryption.
|
|
106
|
+
unsigned short nonce_len, // IN - Nonce length in bytes.
|
|
107
|
+
BYTE plaintext[], // OUT - The plaintext that was decrypted. Will need to be large enough to hold ciphertext_len - mac_len.
|
|
108
|
+
WORD *plaintext_len, // OUT - Length in bytes of the output plaintext, always ciphertext_len - mac_len .
|
|
109
|
+
WORD mac_len, // IN - The length of the MAC that was calculated.
|
|
110
|
+
int *mac_auth, // OUT - TRUE if authentication succeeded, FALSE if it did not. NULL pointer will ignore the authentication.
|
|
111
|
+
const BYTE key[], // IN - The AES key for decryption.
|
|
112
|
+
int keysize); // IN - The length of the key in BITS. Valid values are 128, 192, 256.
|
|
113
|
+
|
|
114
|
+
///////////////////
|
|
115
|
+
// Test functions
|
|
116
|
+
///////////////////
|
|
117
|
+
int aes_test();
|
|
118
|
+
int aes_ecb_test();
|
|
119
|
+
int aes_cbc_test();
|
|
120
|
+
int aes_ctr_test();
|
|
121
|
+
int aes_ccm_test();
|
|
122
|
+
|
|
123
|
+
#endif // AES_H
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/*********************************************************************
|
|
2
|
+
* Filename: aes_test.c
|
|
3
|
+
* Author: Brad Conte (brad AT bradconte.com)
|
|
4
|
+
* Copyright:
|
|
5
|
+
* Disclaimer: This code is presented "as is" without any guarantees.
|
|
6
|
+
* Details: Performs known-answer tests on the corresponding AES
|
|
7
|
+
implementation. These tests do not encompass the full
|
|
8
|
+
range of available test vectors and are not sufficient
|
|
9
|
+
for FIPS-140 certification. However, if the tests pass
|
|
10
|
+
it is very, very likely that the code is correct and was
|
|
11
|
+
compiled properly. This code also serves as
|
|
12
|
+
example usage of the functions.
|
|
13
|
+
*********************************************************************/
|
|
14
|
+
|
|
15
|
+
/*************************** HEADER FILES ***************************/
|
|
16
|
+
#include <stdio.h>
|
|
17
|
+
#include <memory.h>
|
|
18
|
+
#include "aes.h"
|
|
19
|
+
|
|
20
|
+
/*********************** FUNCTION DEFINITIONS ***********************/
|
|
21
|
+
void print_hex(BYTE str[], int len)
|
|
22
|
+
{
|
|
23
|
+
int idx;
|
|
24
|
+
|
|
25
|
+
for(idx = 0; idx < len; idx++)
|
|
26
|
+
printf("%02x", str[idx]);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
int aes_ecb_test()
|
|
30
|
+
{
|
|
31
|
+
WORD key_schedule[60], idx;
|
|
32
|
+
BYTE enc_buf[128];
|
|
33
|
+
BYTE plaintext[2][16] = {
|
|
34
|
+
{0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a},
|
|
35
|
+
{0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51}
|
|
36
|
+
};
|
|
37
|
+
BYTE ciphertext[2][16] = {
|
|
38
|
+
{0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8},
|
|
39
|
+
{0x59,0x1c,0xcb,0x10,0xd4,0x10,0xed,0x26,0xdc,0x5b,0xa7,0x4a,0x31,0x36,0x28,0x70}
|
|
40
|
+
};
|
|
41
|
+
BYTE key[1][32] = {
|
|
42
|
+
{0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4}
|
|
43
|
+
};
|
|
44
|
+
int pass = 1;
|
|
45
|
+
|
|
46
|
+
// Raw ECB mode.
|
|
47
|
+
//printf("* ECB mode:\n");
|
|
48
|
+
aes_key_setup(key[0], key_schedule, 256);
|
|
49
|
+
//printf( "Key : ");
|
|
50
|
+
//print_hex(key[0], 32);
|
|
51
|
+
|
|
52
|
+
for(idx = 0; idx < 2; idx++) {
|
|
53
|
+
aes_encrypt(plaintext[idx], enc_buf, key_schedule, 256);
|
|
54
|
+
//printf("\nPlaintext : ");
|
|
55
|
+
//print_hex(plaintext[idx], 16);
|
|
56
|
+
//printf("\n-encrypted to: ");
|
|
57
|
+
//print_hex(enc_buf, 16);
|
|
58
|
+
pass = pass && !memcmp(enc_buf, ciphertext[idx], 16);
|
|
59
|
+
|
|
60
|
+
aes_decrypt(ciphertext[idx], enc_buf, key_schedule, 256);
|
|
61
|
+
//printf("\nCiphertext : ");
|
|
62
|
+
//print_hex(ciphertext[idx], 16);
|
|
63
|
+
//printf("\n-decrypted to: ");
|
|
64
|
+
//print_hex(enc_buf, 16);
|
|
65
|
+
pass = pass && !memcmp(enc_buf, plaintext[idx], 16);
|
|
66
|
+
|
|
67
|
+
//printf("\n\n");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return(pass);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
int aes_cbc_test()
|
|
74
|
+
{
|
|
75
|
+
WORD key_schedule[60];
|
|
76
|
+
BYTE enc_buf[128];
|
|
77
|
+
BYTE plaintext[1][32] = {
|
|
78
|
+
{0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51}
|
|
79
|
+
};
|
|
80
|
+
BYTE ciphertext[2][32] = {
|
|
81
|
+
{0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6,0x9c,0xfc,0x4e,0x96,0x7e,0xdb,0x80,0x8d,0x67,0x9f,0x77,0x7b,0xc6,0x70,0x2c,0x7d}
|
|
82
|
+
};
|
|
83
|
+
BYTE iv[1][16] = {
|
|
84
|
+
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}
|
|
85
|
+
};
|
|
86
|
+
BYTE key[1][32] = {
|
|
87
|
+
{0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4}
|
|
88
|
+
};
|
|
89
|
+
int pass = 1;
|
|
90
|
+
|
|
91
|
+
//printf("* CBC mode:\n");
|
|
92
|
+
aes_key_setup(key[0], key_schedule, 256);
|
|
93
|
+
|
|
94
|
+
//printf( "Key : ");
|
|
95
|
+
//print_hex(key[0], 32);
|
|
96
|
+
//printf("\nIV : ");
|
|
97
|
+
//print_hex(iv[0], 16);
|
|
98
|
+
|
|
99
|
+
aes_encrypt_cbc(plaintext[0], 32, enc_buf, key_schedule, 256, iv[0]);
|
|
100
|
+
//printf("\nPlaintext : ");
|
|
101
|
+
//print_hex(plaintext[0], 32);
|
|
102
|
+
//printf("\n-encrypted to: ");
|
|
103
|
+
//print_hex(enc_buf, 32);
|
|
104
|
+
//printf("\nCiphertext : ");
|
|
105
|
+
//print_hex(ciphertext[0], 32);
|
|
106
|
+
pass = pass && !memcmp(enc_buf, ciphertext[0], 32);
|
|
107
|
+
|
|
108
|
+
//printf("\n\n");
|
|
109
|
+
return(pass);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
int aes_ctr_test()
|
|
113
|
+
{
|
|
114
|
+
WORD key_schedule[60];
|
|
115
|
+
BYTE enc_buf[128];
|
|
116
|
+
BYTE plaintext[1][32] = {
|
|
117
|
+
{0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51}
|
|
118
|
+
};
|
|
119
|
+
BYTE ciphertext[1][32] = {
|
|
120
|
+
{0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5}
|
|
121
|
+
};
|
|
122
|
+
BYTE iv[1][16] = {
|
|
123
|
+
{0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff},
|
|
124
|
+
};
|
|
125
|
+
BYTE key[1][32] = {
|
|
126
|
+
{0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4}
|
|
127
|
+
};
|
|
128
|
+
int pass = 1;
|
|
129
|
+
|
|
130
|
+
//printf("* CTR mode:\n");
|
|
131
|
+
aes_key_setup(key[0], key_schedule, 256);
|
|
132
|
+
|
|
133
|
+
//printf( "Key : ");
|
|
134
|
+
//print_hex(key[0], 32);
|
|
135
|
+
//printf("\nIV : ");
|
|
136
|
+
//print_hex(iv[0], 16);
|
|
137
|
+
|
|
138
|
+
aes_encrypt_ctr(plaintext[0], 32, enc_buf, key_schedule, 256, iv[0]);
|
|
139
|
+
//printf("\nPlaintext : ");
|
|
140
|
+
//print_hex(plaintext[0], 32);
|
|
141
|
+
//printf("\n-encrypted to: ");
|
|
142
|
+
//print_hex(enc_buf, 32);
|
|
143
|
+
pass = pass && !memcmp(enc_buf, ciphertext[0], 32);
|
|
144
|
+
|
|
145
|
+
aes_decrypt_ctr(ciphertext[0], 32, enc_buf, key_schedule, 256, iv[0]);
|
|
146
|
+
//printf("\nCiphertext : ");
|
|
147
|
+
//print_hex(ciphertext[0], 32);
|
|
148
|
+
//printf("\n-decrypted to: ");
|
|
149
|
+
//print_hex(enc_buf, 32);
|
|
150
|
+
pass = pass && !memcmp(enc_buf, plaintext[0], 32);
|
|
151
|
+
|
|
152
|
+
//printf("\n\n");
|
|
153
|
+
return(pass);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
int aes_ccm_test()
|
|
157
|
+
{
|
|
158
|
+
int mac_auth;
|
|
159
|
+
WORD enc_buf_len;
|
|
160
|
+
BYTE enc_buf[128];
|
|
161
|
+
BYTE plaintext[3][32] = {
|
|
162
|
+
{0x20,0x21,0x22,0x23},
|
|
163
|
+
{0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f},
|
|
164
|
+
{0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37}
|
|
165
|
+
};
|
|
166
|
+
BYTE assoc[3][32] = {
|
|
167
|
+
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07},
|
|
168
|
+
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f},
|
|
169
|
+
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13}
|
|
170
|
+
};
|
|
171
|
+
BYTE ciphertext[3][32 + 16] = {
|
|
172
|
+
{0x71,0x62,0x01,0x5b,0x4d,0xac,0x25,0x5d},
|
|
173
|
+
{0xd2,0xa1,0xf0,0xe0,0x51,0xea,0x5f,0x62,0x08,0x1a,0x77,0x92,0x07,0x3d,0x59,0x3d,0x1f,0xc6,0x4f,0xbf,0xac,0xcd},
|
|
174
|
+
{0xe3,0xb2,0x01,0xa9,0xf5,0xb7,0x1a,0x7a,0x9b,0x1c,0xea,0xec,0xcd,0x97,0xe7,0x0b,0x61,0x76,0xaa,0xd9,0xa4,0x42,0x8a,0xa5,0x48,0x43,0x92,0xfb,0xc1,0xb0,0x99,0x51}
|
|
175
|
+
};
|
|
176
|
+
BYTE iv[3][16] = {
|
|
177
|
+
{0x10,0x11,0x12,0x13,0x14,0x15,0x16},
|
|
178
|
+
{0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17},
|
|
179
|
+
{0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b}
|
|
180
|
+
};
|
|
181
|
+
BYTE key[1][32] = {
|
|
182
|
+
{0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f}
|
|
183
|
+
};
|
|
184
|
+
int pass = 1;
|
|
185
|
+
|
|
186
|
+
//printf("* CCM mode:\n");
|
|
187
|
+
//printf("Key : ");
|
|
188
|
+
//print_hex(key[0], 16);
|
|
189
|
+
|
|
190
|
+
//print_hex(plaintext[0], 4);
|
|
191
|
+
//print_hex(assoc[0], 8);
|
|
192
|
+
//print_hex(ciphertext[0], 8);
|
|
193
|
+
//print_hex(iv[0], 7);
|
|
194
|
+
//print_hex(key[0], 16);
|
|
195
|
+
|
|
196
|
+
aes_encrypt_ccm(plaintext[0], 4, assoc[0], 8, iv[0], 7, enc_buf, &enc_buf_len, 4, key[0], 128);
|
|
197
|
+
//printf("\nNONCE : ");
|
|
198
|
+
//print_hex(iv[0], 7);
|
|
199
|
+
//printf("\nAssoc. Data : ");
|
|
200
|
+
//print_hex(assoc[0], 8);
|
|
201
|
+
//printf("\nPayload : ");
|
|
202
|
+
//print_hex(plaintext[0], 4);
|
|
203
|
+
//printf("\n-encrypted to: ");
|
|
204
|
+
//print_hex(enc_buf, enc_buf_len);
|
|
205
|
+
pass = pass && !memcmp(enc_buf, ciphertext[0], enc_buf_len);
|
|
206
|
+
|
|
207
|
+
aes_decrypt_ccm(ciphertext[0], 8, assoc[0], 8, iv[0], 7, enc_buf, &enc_buf_len, 4, &mac_auth, key[0], 128);
|
|
208
|
+
//printf("\n-Ciphertext : ");
|
|
209
|
+
//print_hex(ciphertext[0], 8);
|
|
210
|
+
//printf("\n-decrypted to: ");
|
|
211
|
+
//print_hex(enc_buf, enc_buf_len);
|
|
212
|
+
//printf("\nAuthenticated: %d ", mac_auth);
|
|
213
|
+
pass = pass && !memcmp(enc_buf, plaintext[0], enc_buf_len) && mac_auth;
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
aes_encrypt_ccm(plaintext[1], 16, assoc[1], 16, iv[1], 8, enc_buf, &enc_buf_len, 6, key[0], 128);
|
|
217
|
+
//printf("\n\nNONCE : ");
|
|
218
|
+
//print_hex(iv[1], 8);
|
|
219
|
+
//printf("\nAssoc. Data : ");
|
|
220
|
+
//print_hex(assoc[1], 16);
|
|
221
|
+
//printf("\nPayload : ");
|
|
222
|
+
//print_hex(plaintext[1], 16);
|
|
223
|
+
//printf("\n-encrypted to: ");
|
|
224
|
+
//print_hex(enc_buf, enc_buf_len);
|
|
225
|
+
pass = pass && !memcmp(enc_buf, ciphertext[1], enc_buf_len);
|
|
226
|
+
|
|
227
|
+
aes_decrypt_ccm(ciphertext[1], 22, assoc[1], 16, iv[1], 8, enc_buf, &enc_buf_len, 6, &mac_auth, key[0], 128);
|
|
228
|
+
//printf("\n-Ciphertext : ");
|
|
229
|
+
//print_hex(ciphertext[1], 22);
|
|
230
|
+
//printf("\n-decrypted to: ");
|
|
231
|
+
//print_hex(enc_buf, enc_buf_len);
|
|
232
|
+
//printf("\nAuthenticated: %d ", mac_auth);
|
|
233
|
+
pass = pass && !memcmp(enc_buf, plaintext[1], enc_buf_len) && mac_auth;
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
aes_encrypt_ccm(plaintext[2], 24, assoc[2], 20, iv[2], 12, enc_buf, &enc_buf_len, 8, key[0], 128);
|
|
237
|
+
//printf("\n\nNONCE : ");
|
|
238
|
+
//print_hex(iv[2], 12);
|
|
239
|
+
//printf("\nAssoc. Data : ");
|
|
240
|
+
//print_hex(assoc[2], 20);
|
|
241
|
+
//printf("\nPayload : ");
|
|
242
|
+
//print_hex(plaintext[2], 24);
|
|
243
|
+
//printf("\n-encrypted to: ");
|
|
244
|
+
//print_hex(enc_buf, enc_buf_len);
|
|
245
|
+
pass = pass && !memcmp(enc_buf, ciphertext[2], enc_buf_len);
|
|
246
|
+
|
|
247
|
+
aes_decrypt_ccm(ciphertext[2], 32, assoc[2], 20, iv[2], 12, enc_buf, &enc_buf_len, 8, &mac_auth, key[0], 128);
|
|
248
|
+
//printf("\n-Ciphertext : ");
|
|
249
|
+
//print_hex(ciphertext[2], 32);
|
|
250
|
+
//printf("\n-decrypted to: ");
|
|
251
|
+
//print_hex(enc_buf, enc_buf_len);
|
|
252
|
+
//printf("\nAuthenticated: %d ", mac_auth);
|
|
253
|
+
pass = pass && !memcmp(enc_buf, plaintext[2], enc_buf_len) && mac_auth;
|
|
254
|
+
|
|
255
|
+
//printf("\n\n");
|
|
256
|
+
return(pass);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
int aes_test()
|
|
260
|
+
{
|
|
261
|
+
int pass = 1;
|
|
262
|
+
|
|
263
|
+
pass = pass && aes_ecb_test();
|
|
264
|
+
pass = pass && aes_cbc_test();
|
|
265
|
+
pass = pass && aes_ctr_test();
|
|
266
|
+
pass = pass && aes_ccm_test();
|
|
267
|
+
|
|
268
|
+
return(pass);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
int main(int argc, char *argv[])
|
|
272
|
+
{
|
|
273
|
+
printf("AES Tests: %s\n", aes_test() ? "SUCCEEDED" : "FAILED");
|
|
274
|
+
|
|
275
|
+
return(0);
|
|
276
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/*********************************************************************
|
|
2
|
+
* Filename: arcfour.c
|
|
3
|
+
* Author: Brad Conte (brad AT bradconte.com)
|
|
4
|
+
* Copyright:
|
|
5
|
+
* Disclaimer: This code is presented "as is" without any guarantees.
|
|
6
|
+
* Details: Implementation of the ARCFOUR encryption algorithm.
|
|
7
|
+
Algorithm specification can be found here:
|
|
8
|
+
* http://en.wikipedia.org/wiki/RC4
|
|
9
|
+
*********************************************************************/
|
|
10
|
+
|
|
11
|
+
/*************************** HEADER FILES ***************************/
|
|
12
|
+
#include <stdlib.h>
|
|
13
|
+
#include "arcfour.h"
|
|
14
|
+
|
|
15
|
+
/*********************** FUNCTION DEFINITIONS ***********************/
|
|
16
|
+
void arcfour_key_setup(BYTE state[], const BYTE key[], int len)
|
|
17
|
+
{
|
|
18
|
+
int i, j;
|
|
19
|
+
BYTE t;
|
|
20
|
+
|
|
21
|
+
for (i = 0; i < 256; ++i)
|
|
22
|
+
state[i] = i;
|
|
23
|
+
for (i = 0, j = 0; i < 256; ++i) {
|
|
24
|
+
j = (j + state[i] + key[i % len]) % 256;
|
|
25
|
+
t = state[i];
|
|
26
|
+
state[i] = state[j];
|
|
27
|
+
state[j] = t;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
void arcfour_generate_stream(BYTE state[], BYTE out[], size_t len)
|
|
32
|
+
{
|
|
33
|
+
int i, j;
|
|
34
|
+
size_t idx;
|
|
35
|
+
BYTE t;
|
|
36
|
+
|
|
37
|
+
for (idx = 0, i = 0, j = 0; idx < len; ++idx) {
|
|
38
|
+
i = (i + 1) % 256;
|
|
39
|
+
j = (j + state[i]) % 256;
|
|
40
|
+
t = state[i];
|
|
41
|
+
state[i] = state[j];
|
|
42
|
+
state[j] = t;
|
|
43
|
+
out[idx] = state[(state[i] + state[j]) % 256];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/*********************************************************************
|
|
2
|
+
* Filename: arcfour.h
|
|
3
|
+
* Author: Brad Conte (brad AT bradconte.com)
|
|
4
|
+
* Copyright:
|
|
5
|
+
* Disclaimer: This code is presented "as is" without any guarantees.
|
|
6
|
+
* Details: Defines the API for the corresponding ARCFOUR implementation.
|
|
7
|
+
*********************************************************************/
|
|
8
|
+
|
|
9
|
+
#ifndef ARCFOUR_H
|
|
10
|
+
#define ARCFOUR_H
|
|
11
|
+
|
|
12
|
+
/*************************** HEADER FILES ***************************/
|
|
13
|
+
#include <stddef.h>
|
|
14
|
+
|
|
15
|
+
/**************************** DATA TYPES ****************************/
|
|
16
|
+
typedef unsigned char BYTE; // 8-bit byte
|
|
17
|
+
|
|
18
|
+
/*********************** FUNCTION DECLARATIONS **********************/
|
|
19
|
+
// Input: state - the state used to generate the keystream
|
|
20
|
+
// key - Key to use to initialize the state
|
|
21
|
+
// len - length of key in bytes (valid lenth is 1 to 256)
|
|
22
|
+
void arcfour_key_setup(BYTE state[], const BYTE key[], int len);
|
|
23
|
+
|
|
24
|
+
// Pseudo-Random Generator Algorithm
|
|
25
|
+
// Input: state - the state used to generate the keystream
|
|
26
|
+
// out - Must be allocated to be of at least "len" length
|
|
27
|
+
// len - number of bytes to generate
|
|
28
|
+
void arcfour_generate_stream(BYTE state[], BYTE out[], size_t len);
|
|
29
|
+
|
|
30
|
+
#endif // ARCFOUR_H
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/*********************************************************************
|
|
2
|
+
* Filename: arcfour_test.c
|
|
3
|
+
* Author: Brad Conte (brad AT bradconte.com)
|
|
4
|
+
* Copyright:
|
|
5
|
+
* Disclaimer: This code is presented "as is" without any guarantees.
|
|
6
|
+
* Details: Performs known-answer tests on the corresponding ARCFOUR
|
|
7
|
+
implementation. These tests do not encompass the full
|
|
8
|
+
range of available test vectors, however, if the tests
|
|
9
|
+
pass it is very, very likely that the code is correct
|
|
10
|
+
and was compiled properly. This code also serves as
|
|
11
|
+
example usage of the functions.
|
|
12
|
+
*********************************************************************/
|
|
13
|
+
|
|
14
|
+
/*************************** HEADER FILES ***************************/
|
|
15
|
+
#include <stdio.h>
|
|
16
|
+
#include <memory.h>
|
|
17
|
+
#include "arcfour.h"
|
|
18
|
+
|
|
19
|
+
/*********************** FUNCTION DEFINITIONS ***********************/
|
|
20
|
+
int rc4_test()
|
|
21
|
+
{
|
|
22
|
+
BYTE state[256];
|
|
23
|
+
BYTE key[3][10] = {{"Key"}, {"Wiki"}, {"Secret"}};
|
|
24
|
+
BYTE stream[3][10] = {{0xEB,0x9F,0x77,0x81,0xB7,0x34,0xCA,0x72,0xA7,0x19},
|
|
25
|
+
{0x60,0x44,0xdb,0x6d,0x41,0xb7},
|
|
26
|
+
{0x04,0xd4,0x6b,0x05,0x3c,0xa8,0x7b,0x59}};
|
|
27
|
+
int stream_len[3] = {10,6,8};
|
|
28
|
+
BYTE buf[1024];
|
|
29
|
+
int idx;
|
|
30
|
+
int pass = 1;
|
|
31
|
+
|
|
32
|
+
// Only test the output stream. Note that the state can be reused.
|
|
33
|
+
for (idx = 0; idx < 3; idx++) {
|
|
34
|
+
arcfour_key_setup(state, key[idx], strlen(key[idx]));
|
|
35
|
+
arcfour_generate_stream(state, buf, stream_len[idx]);
|
|
36
|
+
pass = pass && !memcmp(stream[idx], buf, stream_len[idx]);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return(pass);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
int main()
|
|
43
|
+
{
|
|
44
|
+
printf("ARCFOUR tests: %s\n", rc4_test() ? "SUCCEEDED" : "FAILED");
|
|
45
|
+
|
|
46
|
+
return(0);
|
|
47
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/*********************************************************************
|
|
2
|
+
* Filename: base64.c
|
|
3
|
+
* Author: Brad Conte (brad AT bradconte.com)
|
|
4
|
+
* Copyright:
|
|
5
|
+
* Disclaimer: This code is presented "as is" without any guarantees.
|
|
6
|
+
* Details: Implementation of the Base64 encoding algorithm.
|
|
7
|
+
*********************************************************************/
|
|
8
|
+
|
|
9
|
+
/*************************** HEADER FILES ***************************/
|
|
10
|
+
#include <stdlib.h>
|
|
11
|
+
#include "base64.h"
|
|
12
|
+
|
|
13
|
+
/****************************** MACROS ******************************/
|
|
14
|
+
#define NEWLINE_INVL 76
|
|
15
|
+
|
|
16
|
+
/**************************** VARIABLES *****************************/
|
|
17
|
+
// Note: To change the charset to a URL encoding, replace the '+' and '/' with '*' and '-'
|
|
18
|
+
static const BYTE charset[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"};
|
|
19
|
+
|
|
20
|
+
/*********************** FUNCTION DEFINITIONS ***********************/
|
|
21
|
+
BYTE revchar(char ch)
|
|
22
|
+
{
|
|
23
|
+
if (ch >= 'A' && ch <= 'Z')
|
|
24
|
+
ch -= 'A';
|
|
25
|
+
else if (ch >= 'a' && ch <='z')
|
|
26
|
+
ch = ch - 'a' + 26;
|
|
27
|
+
else if (ch >= '0' && ch <='9')
|
|
28
|
+
ch = ch - '0' + 52;
|
|
29
|
+
else if (ch == '+')
|
|
30
|
+
ch = 62;
|
|
31
|
+
else if (ch == '/')
|
|
32
|
+
ch = 63;
|
|
33
|
+
|
|
34
|
+
return(ch);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
size_t base64_encode(const BYTE in[], BYTE out[], size_t len, int newline_flag)
|
|
38
|
+
{
|
|
39
|
+
size_t idx, idx2, blks, blk_ceiling, left_over, newline_count = 0;
|
|
40
|
+
|
|
41
|
+
blks = (len / 3);
|
|
42
|
+
left_over = len % 3;
|
|
43
|
+
|
|
44
|
+
if (out == NULL) {
|
|
45
|
+
idx2 = blks * 4 ;
|
|
46
|
+
if (left_over)
|
|
47
|
+
idx2 += 4;
|
|
48
|
+
if (newline_flag)
|
|
49
|
+
idx2 += len / 57; // (NEWLINE_INVL / 4) * 3 = 57. One newline per 57 input bytes.
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
// Since 3 input bytes = 4 output bytes, determine out how many even sets of
|
|
53
|
+
// 3 bytes the input has.
|
|
54
|
+
blk_ceiling = blks * 3;
|
|
55
|
+
for (idx = 0, idx2 = 0; idx < blk_ceiling; idx += 3, idx2 += 4) {
|
|
56
|
+
out[idx2] = charset[in[idx] >> 2];
|
|
57
|
+
out[idx2 + 1] = charset[((in[idx] & 0x03) << 4) | (in[idx + 1] >> 4)];
|
|
58
|
+
out[idx2 + 2] = charset[((in[idx + 1] & 0x0f) << 2) | (in[idx + 2] >> 6)];
|
|
59
|
+
out[idx2 + 3] = charset[in[idx + 2] & 0x3F];
|
|
60
|
+
// The offical standard requires a newline every 76 characters.
|
|
61
|
+
// (Eg, first newline is character 77 of the output.)
|
|
62
|
+
if (((idx2 - newline_count + 4) % NEWLINE_INVL == 0) && newline_flag) {
|
|
63
|
+
out[idx2 + 4] = '\n';
|
|
64
|
+
idx2++;
|
|
65
|
+
newline_count++;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (left_over == 1) {
|
|
70
|
+
out[idx2] = charset[in[idx] >> 2];
|
|
71
|
+
out[idx2 + 1] = charset[(in[idx] & 0x03) << 4];
|
|
72
|
+
out[idx2 + 2] = '=';
|
|
73
|
+
out[idx2 + 3] = '=';
|
|
74
|
+
idx2 += 4;
|
|
75
|
+
}
|
|
76
|
+
else if (left_over == 2) {
|
|
77
|
+
out[idx2] = charset[in[idx] >> 2];
|
|
78
|
+
out[idx2 + 1] = charset[((in[idx] & 0x03) << 4) | (in[idx + 1] >> 4)];
|
|
79
|
+
out[idx2 + 2] = charset[(in[idx + 1] & 0x0F) << 2];
|
|
80
|
+
out[idx2 + 3] = '=';
|
|
81
|
+
idx2 += 4;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return(idx2);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
size_t base64_decode(const BYTE in[], BYTE out[], size_t len)
|
|
89
|
+
{
|
|
90
|
+
BYTE ch;
|
|
91
|
+
size_t idx, idx2, blks, blk_ceiling, left_over;
|
|
92
|
+
|
|
93
|
+
if (in[len - 1] == '=')
|
|
94
|
+
len--;
|
|
95
|
+
if (in[len - 1] == '=')
|
|
96
|
+
len--;
|
|
97
|
+
|
|
98
|
+
blks = len / 4;
|
|
99
|
+
left_over = len % 4;
|
|
100
|
+
|
|
101
|
+
if (out == NULL) {
|
|
102
|
+
if (len >= 77 && in[NEWLINE_INVL] == '\n') // Verify that newlines where used.
|
|
103
|
+
len -= len / (NEWLINE_INVL + 1);
|
|
104
|
+
blks = len / 4;
|
|
105
|
+
left_over = len % 4;
|
|
106
|
+
|
|
107
|
+
idx = blks * 3;
|
|
108
|
+
if (left_over == 2)
|
|
109
|
+
idx ++;
|
|
110
|
+
else if (left_over == 3)
|
|
111
|
+
idx += 2;
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
blk_ceiling = blks * 4;
|
|
115
|
+
for (idx = 0, idx2 = 0; idx2 < blk_ceiling; idx += 3, idx2 += 4) {
|
|
116
|
+
if (in[idx2] == '\n')
|
|
117
|
+
idx2++;
|
|
118
|
+
out[idx] = (revchar(in[idx2]) << 2) | ((revchar(in[idx2 + 1]) & 0x30) >> 4);
|
|
119
|
+
out[idx + 1] = (revchar(in[idx2 + 1]) << 4) | (revchar(in[idx2 + 2]) >> 2);
|
|
120
|
+
out[idx + 2] = (revchar(in[idx2 + 2]) << 6) | revchar(in[idx2 + 3]);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (left_over == 2) {
|
|
124
|
+
out[idx] = (revchar(in[idx2]) << 2) | ((revchar(in[idx2 + 1]) & 0x30) >> 4);
|
|
125
|
+
idx++;
|
|
126
|
+
}
|
|
127
|
+
else if (left_over == 3) {
|
|
128
|
+
out[idx] = (revchar(in[idx2]) << 2) | ((revchar(in[idx2 + 1]) & 0x30) >> 4);
|
|
129
|
+
out[idx + 1] = (revchar(in[idx2 + 1]) << 4) | (revchar(in[idx2 + 2]) >> 2);
|
|
130
|
+
idx += 2;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return(idx);
|
|
135
|
+
}
|