[Nym3-commit] r14 - trunk/crypto
nym3-devel@lists.noreply.org
nym3-devel@lists.noreply.org
Sun, 02 May 2004 23:14:29 +0200
Author: weasel
Date: 2004-05-02 23:14:27 +0200 (Sun, 02 May 2004)
New Revision: 14
Modified:
trunk/crypto/crypto.ml
trunk/crypto/test_weasel.ml
Log:
prng and encrypt work now. does that make the counter module obsolete since I do not use it?
Modified: trunk/crypto/crypto.ml
===================================================================
--- trunk/crypto/crypto.ml 2004-05-02 20:25:26 UTC (rev 13)
+++ trunk/crypto/crypto.ml 2004-05-02 21:14:27 UTC (rev 14)
@@ -112,11 +112,25 @@
*)
let prng n k =
assert ((String.length k) = 16);
- let m = String.make n (char_of_int 0)
- and enc = aes ~mode:(OFB 1) ~iv:(String.make 16 (char_of_int 0))
- k Encrypt in
- transform_string enc m
+ let result = ref ("") in
+ let block = String.make 16 (char_of_int 0) in
+ let n = ref n
+ and p = ref 0 in
+ while (!n > 0) do
+ String.set block (String.length block - 1) (char_of_int !p);
+ incr p;
+
+ let enc = aes k Encrypt in
+ let encrypted = transform_string enc block in
+ if (!n >= 16) then
+ result := !result ^ encrypted
+ else
+ result := !result ^ (String.sub encrypted 0 !n);
+ n := !n - 16
+ done;
+ !result
+
(**
* Encrypt a message <code>m</code> using AES counter mode with key <code>k</code>.
*
Modified: trunk/crypto/test_weasel.ml
===================================================================
--- trunk/crypto/test_weasel.ml 2004-05-02 20:25:26 UTC (rev 13)
+++ trunk/crypto/test_weasel.ml 2004-05-02 21:14:27 UTC (rev 14)
@@ -41,12 +41,10 @@
"81 AE AE FB 58 E0 A2 FE 37 27 31 8E 5B C4 90 B9
86 99 95 78 C0 F6 BC AC 9A A6 16 DF BA 0B 4E 6C 0A 10 C5 8F 7B 67 54 19 D7 EA
8C 4A A7 0E C7 77 6B 25 51 68 88 1C 7C 4D EB 83 8C A0 3F 4A 85 32" in
- let keystream0 = Crypto.prng 0x40 key in
- (* and keystream0 = Crypto.prng key 0x300 in *)
+ let keystream0 = Crypto.prng 0x300 key in
- Printf.printf "\n%s\n%s\n" (hex keystream1) (hex keystream0);
- assert(keystream0 = keystream1);
- (*assert(keystream0[0x2c0..0x40] = keystream2); *)
+ assert( (String.sub keystream0 0 0x40) = keystream1);
+ assert( (String.sub keystream0 0x2c0 0x40) = keystream2);
Printf.printf "."
let testEncrypt =
@@ -100,6 +98,8 @@
let _ =
testHash;
+ testPRNG;
+ testEncrypt;
testSPRPEncrypt;
testSPRPDecrypt;
Printf.printf "\n"