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,189 @@
|
|
|
1
|
+
/*********************************************************************
|
|
2
|
+
* Filename: md5.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 MD5 hashing algorithm.
|
|
7
|
+
Algorithm specification can be found here:
|
|
8
|
+
* http://tools.ietf.org/html/rfc1321
|
|
9
|
+
This implementation uses little endian byte order.
|
|
10
|
+
*********************************************************************/
|
|
11
|
+
|
|
12
|
+
/*************************** HEADER FILES ***************************/
|
|
13
|
+
#include <stdlib.h>
|
|
14
|
+
#include <memory.h>
|
|
15
|
+
#include "md5.h"
|
|
16
|
+
|
|
17
|
+
/****************************** MACROS ******************************/
|
|
18
|
+
#define ROTLEFT(a,b) ((a << b) | (a >> (32-b)))
|
|
19
|
+
|
|
20
|
+
#define F(x,y,z) ((x & y) | (~x & z))
|
|
21
|
+
#define G(x,y,z) ((x & z) | (y & ~z))
|
|
22
|
+
#define H(x,y,z) (x ^ y ^ z)
|
|
23
|
+
#define I(x,y,z) (y ^ (x | ~z))
|
|
24
|
+
|
|
25
|
+
#define FF(a,b,c,d,m,s,t) { a += F(b,c,d) + m + t; \
|
|
26
|
+
a = b + ROTLEFT(a,s); }
|
|
27
|
+
#define GG(a,b,c,d,m,s,t) { a += G(b,c,d) + m + t; \
|
|
28
|
+
a = b + ROTLEFT(a,s); }
|
|
29
|
+
#define HH(a,b,c,d,m,s,t) { a += H(b,c,d) + m + t; \
|
|
30
|
+
a = b + ROTLEFT(a,s); }
|
|
31
|
+
#define II(a,b,c,d,m,s,t) { a += I(b,c,d) + m + t; \
|
|
32
|
+
a = b + ROTLEFT(a,s); }
|
|
33
|
+
|
|
34
|
+
/*********************** FUNCTION DEFINITIONS ***********************/
|
|
35
|
+
void md5_transform(MD5_CTX *ctx, const BYTE data[])
|
|
36
|
+
{
|
|
37
|
+
WORD a, b, c, d, m[16], i, j;
|
|
38
|
+
|
|
39
|
+
// MD5 specifies big endian byte order, but this implementation assumes a little
|
|
40
|
+
// endian byte order CPU. Reverse all the bytes upon input, and re-reverse them
|
|
41
|
+
// on output (in md5_final()).
|
|
42
|
+
for (i = 0, j = 0; i < 16; ++i, j += 4)
|
|
43
|
+
m[i] = (data[j]) + (data[j + 1] << 8) + (data[j + 2] << 16) + (data[j + 3] << 24);
|
|
44
|
+
|
|
45
|
+
a = ctx->state[0];
|
|
46
|
+
b = ctx->state[1];
|
|
47
|
+
c = ctx->state[2];
|
|
48
|
+
d = ctx->state[3];
|
|
49
|
+
|
|
50
|
+
FF(a,b,c,d,m[0], 7,0xd76aa478);
|
|
51
|
+
FF(d,a,b,c,m[1], 12,0xe8c7b756);
|
|
52
|
+
FF(c,d,a,b,m[2], 17,0x242070db);
|
|
53
|
+
FF(b,c,d,a,m[3], 22,0xc1bdceee);
|
|
54
|
+
FF(a,b,c,d,m[4], 7,0xf57c0faf);
|
|
55
|
+
FF(d,a,b,c,m[5], 12,0x4787c62a);
|
|
56
|
+
FF(c,d,a,b,m[6], 17,0xa8304613);
|
|
57
|
+
FF(b,c,d,a,m[7], 22,0xfd469501);
|
|
58
|
+
FF(a,b,c,d,m[8], 7,0x698098d8);
|
|
59
|
+
FF(d,a,b,c,m[9], 12,0x8b44f7af);
|
|
60
|
+
FF(c,d,a,b,m[10],17,0xffff5bb1);
|
|
61
|
+
FF(b,c,d,a,m[11],22,0x895cd7be);
|
|
62
|
+
FF(a,b,c,d,m[12], 7,0x6b901122);
|
|
63
|
+
FF(d,a,b,c,m[13],12,0xfd987193);
|
|
64
|
+
FF(c,d,a,b,m[14],17,0xa679438e);
|
|
65
|
+
FF(b,c,d,a,m[15],22,0x49b40821);
|
|
66
|
+
|
|
67
|
+
GG(a,b,c,d,m[1], 5,0xf61e2562);
|
|
68
|
+
GG(d,a,b,c,m[6], 9,0xc040b340);
|
|
69
|
+
GG(c,d,a,b,m[11],14,0x265e5a51);
|
|
70
|
+
GG(b,c,d,a,m[0], 20,0xe9b6c7aa);
|
|
71
|
+
GG(a,b,c,d,m[5], 5,0xd62f105d);
|
|
72
|
+
GG(d,a,b,c,m[10], 9,0x02441453);
|
|
73
|
+
GG(c,d,a,b,m[15],14,0xd8a1e681);
|
|
74
|
+
GG(b,c,d,a,m[4], 20,0xe7d3fbc8);
|
|
75
|
+
GG(a,b,c,d,m[9], 5,0x21e1cde6);
|
|
76
|
+
GG(d,a,b,c,m[14], 9,0xc33707d6);
|
|
77
|
+
GG(c,d,a,b,m[3], 14,0xf4d50d87);
|
|
78
|
+
GG(b,c,d,a,m[8], 20,0x455a14ed);
|
|
79
|
+
GG(a,b,c,d,m[13], 5,0xa9e3e905);
|
|
80
|
+
GG(d,a,b,c,m[2], 9,0xfcefa3f8);
|
|
81
|
+
GG(c,d,a,b,m[7], 14,0x676f02d9);
|
|
82
|
+
GG(b,c,d,a,m[12],20,0x8d2a4c8a);
|
|
83
|
+
|
|
84
|
+
HH(a,b,c,d,m[5], 4,0xfffa3942);
|
|
85
|
+
HH(d,a,b,c,m[8], 11,0x8771f681);
|
|
86
|
+
HH(c,d,a,b,m[11],16,0x6d9d6122);
|
|
87
|
+
HH(b,c,d,a,m[14],23,0xfde5380c);
|
|
88
|
+
HH(a,b,c,d,m[1], 4,0xa4beea44);
|
|
89
|
+
HH(d,a,b,c,m[4], 11,0x4bdecfa9);
|
|
90
|
+
HH(c,d,a,b,m[7], 16,0xf6bb4b60);
|
|
91
|
+
HH(b,c,d,a,m[10],23,0xbebfbc70);
|
|
92
|
+
HH(a,b,c,d,m[13], 4,0x289b7ec6);
|
|
93
|
+
HH(d,a,b,c,m[0], 11,0xeaa127fa);
|
|
94
|
+
HH(c,d,a,b,m[3], 16,0xd4ef3085);
|
|
95
|
+
HH(b,c,d,a,m[6], 23,0x04881d05);
|
|
96
|
+
HH(a,b,c,d,m[9], 4,0xd9d4d039);
|
|
97
|
+
HH(d,a,b,c,m[12],11,0xe6db99e5);
|
|
98
|
+
HH(c,d,a,b,m[15],16,0x1fa27cf8);
|
|
99
|
+
HH(b,c,d,a,m[2], 23,0xc4ac5665);
|
|
100
|
+
|
|
101
|
+
II(a,b,c,d,m[0], 6,0xf4292244);
|
|
102
|
+
II(d,a,b,c,m[7], 10,0x432aff97);
|
|
103
|
+
II(c,d,a,b,m[14],15,0xab9423a7);
|
|
104
|
+
II(b,c,d,a,m[5], 21,0xfc93a039);
|
|
105
|
+
II(a,b,c,d,m[12], 6,0x655b59c3);
|
|
106
|
+
II(d,a,b,c,m[3], 10,0x8f0ccc92);
|
|
107
|
+
II(c,d,a,b,m[10],15,0xffeff47d);
|
|
108
|
+
II(b,c,d,a,m[1], 21,0x85845dd1);
|
|
109
|
+
II(a,b,c,d,m[8], 6,0x6fa87e4f);
|
|
110
|
+
II(d,a,b,c,m[15],10,0xfe2ce6e0);
|
|
111
|
+
II(c,d,a,b,m[6], 15,0xa3014314);
|
|
112
|
+
II(b,c,d,a,m[13],21,0x4e0811a1);
|
|
113
|
+
II(a,b,c,d,m[4], 6,0xf7537e82);
|
|
114
|
+
II(d,a,b,c,m[11],10,0xbd3af235);
|
|
115
|
+
II(c,d,a,b,m[2], 15,0x2ad7d2bb);
|
|
116
|
+
II(b,c,d,a,m[9], 21,0xeb86d391);
|
|
117
|
+
|
|
118
|
+
ctx->state[0] += a;
|
|
119
|
+
ctx->state[1] += b;
|
|
120
|
+
ctx->state[2] += c;
|
|
121
|
+
ctx->state[3] += d;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
void md5_init(MD5_CTX *ctx)
|
|
125
|
+
{
|
|
126
|
+
ctx->datalen = 0;
|
|
127
|
+
ctx->bitlen = 0;
|
|
128
|
+
ctx->state[0] = 0x67452301;
|
|
129
|
+
ctx->state[1] = 0xEFCDAB89;
|
|
130
|
+
ctx->state[2] = 0x98BADCFE;
|
|
131
|
+
ctx->state[3] = 0x10325476;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
void md5_update(MD5_CTX *ctx, const BYTE data[], size_t len)
|
|
135
|
+
{
|
|
136
|
+
size_t i;
|
|
137
|
+
|
|
138
|
+
for (i = 0; i < len; ++i) {
|
|
139
|
+
ctx->data[ctx->datalen] = data[i];
|
|
140
|
+
ctx->datalen++;
|
|
141
|
+
if (ctx->datalen == 64) {
|
|
142
|
+
md5_transform(ctx, ctx->data);
|
|
143
|
+
ctx->bitlen += 512;
|
|
144
|
+
ctx->datalen = 0;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
void md5_final(MD5_CTX *ctx, BYTE hash[])
|
|
150
|
+
{
|
|
151
|
+
size_t i;
|
|
152
|
+
|
|
153
|
+
i = ctx->datalen;
|
|
154
|
+
|
|
155
|
+
// Pad whatever data is left in the buffer.
|
|
156
|
+
if (ctx->datalen < 56) {
|
|
157
|
+
ctx->data[i++] = 0x80;
|
|
158
|
+
while (i < 56)
|
|
159
|
+
ctx->data[i++] = 0x00;
|
|
160
|
+
}
|
|
161
|
+
else if (ctx->datalen >= 56) {
|
|
162
|
+
ctx->data[i++] = 0x80;
|
|
163
|
+
while (i < 64)
|
|
164
|
+
ctx->data[i++] = 0x00;
|
|
165
|
+
md5_transform(ctx, ctx->data);
|
|
166
|
+
memset(ctx->data, 0, 56);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Append to the padding the total message's length in bits and transform.
|
|
170
|
+
ctx->bitlen += ctx->datalen * 8;
|
|
171
|
+
ctx->data[56] = ctx->bitlen;
|
|
172
|
+
ctx->data[57] = ctx->bitlen >> 8;
|
|
173
|
+
ctx->data[58] = ctx->bitlen >> 16;
|
|
174
|
+
ctx->data[59] = ctx->bitlen >> 24;
|
|
175
|
+
ctx->data[60] = ctx->bitlen >> 32;
|
|
176
|
+
ctx->data[61] = ctx->bitlen >> 40;
|
|
177
|
+
ctx->data[62] = ctx->bitlen >> 48;
|
|
178
|
+
ctx->data[63] = ctx->bitlen >> 56;
|
|
179
|
+
md5_transform(ctx, ctx->data);
|
|
180
|
+
|
|
181
|
+
// Since this implementation uses little endian byte ordering and MD uses big endian,
|
|
182
|
+
// reverse all the bytes when copying the final state to the output hash.
|
|
183
|
+
for (i = 0; i < 4; ++i) {
|
|
184
|
+
hash[i] = (ctx->state[0] >> (i * 8)) & 0x000000ff;
|
|
185
|
+
hash[i + 4] = (ctx->state[1] >> (i * 8)) & 0x000000ff;
|
|
186
|
+
hash[i + 8] = (ctx->state[2] >> (i * 8)) & 0x000000ff;
|
|
187
|
+
hash[i + 12] = (ctx->state[3] >> (i * 8)) & 0x000000ff;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/*********************************************************************
|
|
2
|
+
* Filename: md5.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 MD5 implementation.
|
|
7
|
+
*********************************************************************/
|
|
8
|
+
|
|
9
|
+
#ifndef MD5_H
|
|
10
|
+
#define MD5_H
|
|
11
|
+
|
|
12
|
+
/*************************** HEADER FILES ***************************/
|
|
13
|
+
#include <stddef.h>
|
|
14
|
+
|
|
15
|
+
/****************************** MACROS ******************************/
|
|
16
|
+
#define MD5_BLOCK_SIZE 16 // MD5 outputs a 16 byte digest
|
|
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
|
+
typedef struct {
|
|
23
|
+
BYTE data[64];
|
|
24
|
+
WORD datalen;
|
|
25
|
+
unsigned long long bitlen;
|
|
26
|
+
WORD state[4];
|
|
27
|
+
} MD5_CTX;
|
|
28
|
+
|
|
29
|
+
/*********************** FUNCTION DECLARATIONS **********************/
|
|
30
|
+
void md5_init(MD5_CTX *ctx);
|
|
31
|
+
void md5_update(MD5_CTX *ctx, const BYTE data[], size_t len);
|
|
32
|
+
void md5_final(MD5_CTX *ctx, BYTE hash[]);
|
|
33
|
+
|
|
34
|
+
#endif // MD5_H
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/*********************************************************************
|
|
2
|
+
* Filename: md5_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 MD5
|
|
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 <string.h>
|
|
18
|
+
#include "md5.h"
|
|
19
|
+
|
|
20
|
+
/*********************** FUNCTION DEFINITIONS ***********************/
|
|
21
|
+
int md5_test()
|
|
22
|
+
{
|
|
23
|
+
BYTE text1[] = {""};
|
|
24
|
+
BYTE text2[] = {"abc"};
|
|
25
|
+
BYTE text3_1[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"};
|
|
26
|
+
BYTE text3_2[] = {"fghijklmnopqrstuvwxyz0123456789"};
|
|
27
|
+
BYTE hash1[MD5_BLOCK_SIZE] = {0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e};
|
|
28
|
+
BYTE hash2[MD5_BLOCK_SIZE] = {0x90,0x01,0x50,0x98,0x3c,0xd2,0x4f,0xb0,0xd6,0x96,0x3f,0x7d,0x28,0xe1,0x7f,0x72};
|
|
29
|
+
BYTE hash3[MD5_BLOCK_SIZE] = {0xd1,0x74,0xab,0x98,0xd2,0x77,0xd9,0xf5,0xa5,0x61,0x1c,0x2c,0x9f,0x41,0x9d,0x9f};
|
|
30
|
+
BYTE buf[16];
|
|
31
|
+
MD5_CTX ctx;
|
|
32
|
+
int pass = 1;
|
|
33
|
+
|
|
34
|
+
md5_init(&ctx);
|
|
35
|
+
md5_update(&ctx, text1, strlen(text1));
|
|
36
|
+
md5_final(&ctx, buf);
|
|
37
|
+
pass = pass && !memcmp(hash1, buf, MD5_BLOCK_SIZE);
|
|
38
|
+
|
|
39
|
+
// Note the MD5 object can be reused.
|
|
40
|
+
md5_init(&ctx);
|
|
41
|
+
md5_update(&ctx, text2, strlen(text2));
|
|
42
|
+
md5_final(&ctx, buf);
|
|
43
|
+
pass = pass && !memcmp(hash2, buf, MD5_BLOCK_SIZE);
|
|
44
|
+
|
|
45
|
+
// Note the data is being added in two chunks.
|
|
46
|
+
md5_init(&ctx);
|
|
47
|
+
md5_update(&ctx, text3_1, strlen(text3_1));
|
|
48
|
+
md5_update(&ctx, text3_2, strlen(text3_2));
|
|
49
|
+
md5_final(&ctx, buf);
|
|
50
|
+
pass = pass && !memcmp(hash3, buf, MD5_BLOCK_SIZE);
|
|
51
|
+
|
|
52
|
+
return(pass);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
int main()
|
|
56
|
+
{
|
|
57
|
+
printf("MD5 tests: %s\n", md5_test() ? "SUCCEEDED" : "FAILED");
|
|
58
|
+
|
|
59
|
+
return(0);
|
|
60
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/*********************************************************************
|
|
2
|
+
* Filename: rot-13.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 ROT-13 encryption algorithm.
|
|
7
|
+
Algorithm specification can be found here:
|
|
8
|
+
*
|
|
9
|
+
This implementation uses little endian byte order.
|
|
10
|
+
*********************************************************************/
|
|
11
|
+
|
|
12
|
+
/*************************** HEADER FILES ***************************/
|
|
13
|
+
#include <string.h>
|
|
14
|
+
#include "rot-13.h"
|
|
15
|
+
|
|
16
|
+
/*********************** FUNCTION DEFINITIONS ***********************/
|
|
17
|
+
void rot13(char str[])
|
|
18
|
+
{
|
|
19
|
+
int case_type, idx, len;
|
|
20
|
+
|
|
21
|
+
for (idx = 0, len = strlen(str); idx < len; idx++) {
|
|
22
|
+
// Only process alphabetic characters.
|
|
23
|
+
if (str[idx] < 'A' || (str[idx] > 'Z' && str[idx] < 'a') || str[idx] > 'z')
|
|
24
|
+
continue;
|
|
25
|
+
// Determine if the char is upper or lower case.
|
|
26
|
+
if (str[idx] >= 'a')
|
|
27
|
+
case_type = 'a';
|
|
28
|
+
else
|
|
29
|
+
case_type = 'A';
|
|
30
|
+
// Rotate the char's value, ensuring it doesn't accidentally "fall off" the end.
|
|
31
|
+
str[idx] = (str[idx] + 13) % (case_type + 26);
|
|
32
|
+
if (str[idx] < 26)
|
|
33
|
+
str[idx] += case_type;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/*********************************************************************
|
|
2
|
+
* Filename: rot-13.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 ROT-13 implementation.
|
|
7
|
+
*********************************************************************/
|
|
8
|
+
|
|
9
|
+
#ifndef ROT13_H
|
|
10
|
+
#define ROT13_H
|
|
11
|
+
|
|
12
|
+
/*************************** HEADER FILES ***************************/
|
|
13
|
+
#include <stddef.h>
|
|
14
|
+
|
|
15
|
+
/*********************** FUNCTION DECLARATIONS **********************/
|
|
16
|
+
// Performs IN PLACE rotation of the input. Assumes input is NULL terminated.
|
|
17
|
+
// Preserves each charcter's case. Ignores non alphabetic characters.
|
|
18
|
+
void rot13(char str[]);
|
|
19
|
+
|
|
20
|
+
#endif // ROT13_H
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/*********************************************************************
|
|
2
|
+
* Filename: rot-13_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 ROT-13
|
|
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 <string.h>
|
|
17
|
+
#include "rot-13.h"
|
|
18
|
+
|
|
19
|
+
/*********************** FUNCTION DEFINITIONS ***********************/
|
|
20
|
+
int rot13_test()
|
|
21
|
+
{
|
|
22
|
+
char text[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"};
|
|
23
|
+
char code[] = {"NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"};
|
|
24
|
+
char buf[1024];
|
|
25
|
+
int pass = 1;
|
|
26
|
+
|
|
27
|
+
// To encode, just apply ROT-13.
|
|
28
|
+
strcpy(buf, text);
|
|
29
|
+
rot13(buf);
|
|
30
|
+
pass = pass && !strcmp(code, buf);
|
|
31
|
+
|
|
32
|
+
// To decode, just re-apply ROT-13.
|
|
33
|
+
rot13(buf);
|
|
34
|
+
pass = pass && !strcmp(text, buf);
|
|
35
|
+
|
|
36
|
+
return(pass);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
int main()
|
|
40
|
+
{
|
|
41
|
+
printf("ROT-13 tests: %s\n", rot13_test() ? "SUCCEEDED" : "FAILED");
|
|
42
|
+
|
|
43
|
+
return(0);
|
|
44
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/*********************************************************************
|
|
2
|
+
* Filename: sha1.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 SHA1 hashing algorithm.
|
|
7
|
+
Algorithm specification can be found here:
|
|
8
|
+
* http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
|
|
9
|
+
This implementation uses little endian byte order.
|
|
10
|
+
*********************************************************************/
|
|
11
|
+
|
|
12
|
+
/*************************** HEADER FILES ***************************/
|
|
13
|
+
#include <stdlib.h>
|
|
14
|
+
#include <memory.h>
|
|
15
|
+
#include "sha1.h"
|
|
16
|
+
|
|
17
|
+
/****************************** MACROS ******************************/
|
|
18
|
+
#define ROTLEFT(a, b) ((a << b) | (a >> (32 - b)))
|
|
19
|
+
|
|
20
|
+
/*********************** FUNCTION DEFINITIONS ***********************/
|
|
21
|
+
void sha1_transform(SHA1_CTX *ctx, const BYTE data[])
|
|
22
|
+
{
|
|
23
|
+
WORD a, b, c, d, e, i, j, t, m[80];
|
|
24
|
+
|
|
25
|
+
for (i = 0, j = 0; i < 16; ++i, j += 4)
|
|
26
|
+
m[i] = (data[j] << 24) + (data[j + 1] << 16) + (data[j + 2] << 8) + (data[j + 3]);
|
|
27
|
+
for ( ; i < 80; ++i) {
|
|
28
|
+
m[i] = (m[i - 3] ^ m[i - 8] ^ m[i - 14] ^ m[i - 16]);
|
|
29
|
+
m[i] = (m[i] << 1) | (m[i] >> 31);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
a = ctx->state[0];
|
|
33
|
+
b = ctx->state[1];
|
|
34
|
+
c = ctx->state[2];
|
|
35
|
+
d = ctx->state[3];
|
|
36
|
+
e = ctx->state[4];
|
|
37
|
+
|
|
38
|
+
for (i = 0; i < 20; ++i) {
|
|
39
|
+
t = ROTLEFT(a, 5) + ((b & c) ^ (~b & d)) + e + ctx->k[0] + m[i];
|
|
40
|
+
e = d;
|
|
41
|
+
d = c;
|
|
42
|
+
c = ROTLEFT(b, 30);
|
|
43
|
+
b = a;
|
|
44
|
+
a = t;
|
|
45
|
+
}
|
|
46
|
+
for ( ; i < 40; ++i) {
|
|
47
|
+
t = ROTLEFT(a, 5) + (b ^ c ^ d) + e + ctx->k[1] + m[i];
|
|
48
|
+
e = d;
|
|
49
|
+
d = c;
|
|
50
|
+
c = ROTLEFT(b, 30);
|
|
51
|
+
b = a;
|
|
52
|
+
a = t;
|
|
53
|
+
}
|
|
54
|
+
for ( ; i < 60; ++i) {
|
|
55
|
+
t = ROTLEFT(a, 5) + ((b & c) ^ (b & d) ^ (c & d)) + e + ctx->k[2] + m[i];
|
|
56
|
+
e = d;
|
|
57
|
+
d = c;
|
|
58
|
+
c = ROTLEFT(b, 30);
|
|
59
|
+
b = a;
|
|
60
|
+
a = t;
|
|
61
|
+
}
|
|
62
|
+
for ( ; i < 80; ++i) {
|
|
63
|
+
t = ROTLEFT(a, 5) + (b ^ c ^ d) + e + ctx->k[3] + m[i];
|
|
64
|
+
e = d;
|
|
65
|
+
d = c;
|
|
66
|
+
c = ROTLEFT(b, 30);
|
|
67
|
+
b = a;
|
|
68
|
+
a = t;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
ctx->state[0] += a;
|
|
72
|
+
ctx->state[1] += b;
|
|
73
|
+
ctx->state[2] += c;
|
|
74
|
+
ctx->state[3] += d;
|
|
75
|
+
ctx->state[4] += e;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
void sha1_init(SHA1_CTX *ctx)
|
|
79
|
+
{
|
|
80
|
+
ctx->datalen = 0;
|
|
81
|
+
ctx->bitlen = 0;
|
|
82
|
+
ctx->state[0] = 0x67452301;
|
|
83
|
+
ctx->state[1] = 0xEFCDAB89;
|
|
84
|
+
ctx->state[2] = 0x98BADCFE;
|
|
85
|
+
ctx->state[3] = 0x10325476;
|
|
86
|
+
ctx->state[4] = 0xc3d2e1f0;
|
|
87
|
+
ctx->k[0] = 0x5a827999;
|
|
88
|
+
ctx->k[1] = 0x6ed9eba1;
|
|
89
|
+
ctx->k[2] = 0x8f1bbcdc;
|
|
90
|
+
ctx->k[3] = 0xca62c1d6;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
void sha1_update(SHA1_CTX *ctx, const BYTE data[], size_t len)
|
|
94
|
+
{
|
|
95
|
+
size_t i;
|
|
96
|
+
|
|
97
|
+
for (i = 0; i < len; ++i) {
|
|
98
|
+
ctx->data[ctx->datalen] = data[i];
|
|
99
|
+
ctx->datalen++;
|
|
100
|
+
if (ctx->datalen == 64) {
|
|
101
|
+
sha1_transform(ctx, ctx->data);
|
|
102
|
+
ctx->bitlen += 512;
|
|
103
|
+
ctx->datalen = 0;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
void sha1_final(SHA1_CTX *ctx, BYTE hash[])
|
|
109
|
+
{
|
|
110
|
+
WORD i;
|
|
111
|
+
|
|
112
|
+
i = ctx->datalen;
|
|
113
|
+
|
|
114
|
+
// Pad whatever data is left in the buffer.
|
|
115
|
+
if (ctx->datalen < 56) {
|
|
116
|
+
ctx->data[i++] = 0x80;
|
|
117
|
+
while (i < 56)
|
|
118
|
+
ctx->data[i++] = 0x00;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
ctx->data[i++] = 0x80;
|
|
122
|
+
while (i < 64)
|
|
123
|
+
ctx->data[i++] = 0x00;
|
|
124
|
+
sha1_transform(ctx, ctx->data);
|
|
125
|
+
memset(ctx->data, 0, 56);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Append to the padding the total message's length in bits and transform.
|
|
129
|
+
ctx->bitlen += ctx->datalen * 8;
|
|
130
|
+
ctx->data[63] = ctx->bitlen;
|
|
131
|
+
ctx->data[62] = ctx->bitlen >> 8;
|
|
132
|
+
ctx->data[61] = ctx->bitlen >> 16;
|
|
133
|
+
ctx->data[60] = ctx->bitlen >> 24;
|
|
134
|
+
ctx->data[59] = ctx->bitlen >> 32;
|
|
135
|
+
ctx->data[58] = ctx->bitlen >> 40;
|
|
136
|
+
ctx->data[57] = ctx->bitlen >> 48;
|
|
137
|
+
ctx->data[56] = ctx->bitlen >> 56;
|
|
138
|
+
sha1_transform(ctx, ctx->data);
|
|
139
|
+
|
|
140
|
+
// Since this implementation uses little endian byte ordering and MD uses big endian,
|
|
141
|
+
// reverse all the bytes when copying the final state to the output hash.
|
|
142
|
+
for (i = 0; i < 4; ++i) {
|
|
143
|
+
hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
|
|
144
|
+
hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
|
|
145
|
+
hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
|
|
146
|
+
hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
|
|
147
|
+
hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/*********************************************************************
|
|
2
|
+
* Filename: sha1.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 SHA1 implementation.
|
|
7
|
+
*********************************************************************/
|
|
8
|
+
|
|
9
|
+
#ifndef SHA1_H
|
|
10
|
+
#define SHA1_H
|
|
11
|
+
|
|
12
|
+
/*************************** HEADER FILES ***************************/
|
|
13
|
+
#include <stddef.h>
|
|
14
|
+
|
|
15
|
+
/****************************** MACROS ******************************/
|
|
16
|
+
#define SHA1_BLOCK_SIZE 20 // SHA1 outputs a 20 byte digest
|
|
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
|
+
typedef struct {
|
|
23
|
+
BYTE data[64];
|
|
24
|
+
WORD datalen;
|
|
25
|
+
unsigned long long bitlen;
|
|
26
|
+
WORD state[5];
|
|
27
|
+
WORD k[4];
|
|
28
|
+
} SHA1_CTX;
|
|
29
|
+
|
|
30
|
+
/*********************** FUNCTION DECLARATIONS **********************/
|
|
31
|
+
void sha1_init(SHA1_CTX *ctx);
|
|
32
|
+
void sha1_update(SHA1_CTX *ctx, const BYTE data[], size_t len);
|
|
33
|
+
void sha1_final(SHA1_CTX *ctx, BYTE hash[]);
|
|
34
|
+
|
|
35
|
+
#endif // SHA1_H
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/*********************************************************************
|
|
2
|
+
* Filename: sha1_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 SHA1
|
|
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 <string.h>
|
|
18
|
+
#include "sha1.h"
|
|
19
|
+
|
|
20
|
+
/*********************** FUNCTION DEFINITIONS ***********************/
|
|
21
|
+
int sha1_test()
|
|
22
|
+
{
|
|
23
|
+
BYTE text1[] = {"abc"};
|
|
24
|
+
BYTE text2[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"};
|
|
25
|
+
BYTE text3[] = {"aaaaaaaaaa"};
|
|
26
|
+
BYTE hash1[SHA1_BLOCK_SIZE] = {0xa9,0x99,0x3e,0x36,0x47,0x06,0x81,0x6a,0xba,0x3e,0x25,0x71,0x78,0x50,0xc2,0x6c,0x9c,0xd0,0xd8,0x9d};
|
|
27
|
+
BYTE hash2[SHA1_BLOCK_SIZE] = {0x84,0x98,0x3e,0x44,0x1c,0x3b,0xd2,0x6e,0xba,0xae,0x4a,0xa1,0xf9,0x51,0x29,0xe5,0xe5,0x46,0x70,0xf1};
|
|
28
|
+
BYTE hash3[SHA1_BLOCK_SIZE] = {0x34,0xaa,0x97,0x3c,0xd4,0xc4,0xda,0xa4,0xf6,0x1e,0xeb,0x2b,0xdb,0xad,0x27,0x31,0x65,0x34,0x01,0x6f};
|
|
29
|
+
BYTE buf[SHA1_BLOCK_SIZE];
|
|
30
|
+
int idx;
|
|
31
|
+
SHA1_CTX ctx;
|
|
32
|
+
int pass = 1;
|
|
33
|
+
|
|
34
|
+
sha1_init(&ctx);
|
|
35
|
+
sha1_update(&ctx, text1, strlen(text1));
|
|
36
|
+
sha1_final(&ctx, buf);
|
|
37
|
+
pass = pass && !memcmp(hash1, buf, SHA1_BLOCK_SIZE);
|
|
38
|
+
|
|
39
|
+
sha1_init(&ctx);
|
|
40
|
+
sha1_update(&ctx, text2, strlen(text2));
|
|
41
|
+
sha1_final(&ctx, buf);
|
|
42
|
+
pass = pass && !memcmp(hash2, buf, SHA1_BLOCK_SIZE);
|
|
43
|
+
|
|
44
|
+
sha1_init(&ctx);
|
|
45
|
+
for (idx = 0; idx < 100000; ++idx)
|
|
46
|
+
sha1_update(&ctx, text3, strlen(text3));
|
|
47
|
+
sha1_final(&ctx, buf);
|
|
48
|
+
pass = pass && !memcmp(hash3, buf, SHA1_BLOCK_SIZE);
|
|
49
|
+
|
|
50
|
+
return(pass);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
int main()
|
|
54
|
+
{
|
|
55
|
+
printf("SHA1 tests: %s\n", sha1_test() ? "SUCCEEDED" : "FAILED");
|
|
56
|
+
|
|
57
|
+
return(0);
|
|
58
|
+
}
|