cryptol/examples/TripleDES.cry

42 lines
1.1 KiB
Plaintext
Raw Normal View History

2014-04-18 02:34:25 +04:00
/*
* Copyright (c) 2013-2016 Galois, Inc.
2014-04-18 02:34:25 +04:00
* Distributed under the terms of the BSD3 license (see LICENSE file)
*/
module TripleDES where
import DES
blockEncrypt : ([64],[64],[64],[64]) -> [64]
2016-02-19 21:08:20 +03:00
blockEncrypt (k1,k2,k3,data) = result where
2014-04-18 02:34:25 +04:00
ex = DES.encrypt k1 data
dx = DES.decrypt k2 ex
result = DES.encrypt k3 dx
// Test vectors from NIST 800-67
Key1, Key2, Key3 : [64]
Key1 = 0x0123456789ABCDEF
Key2 = 0x23456789ABCDEF01
Key3 = 0x456789ABCDEF0123
P1, P2, P3 : [64]
PlainText = "The qufck brown fox jump"
// Yes, that's the correct phrase.. (see the 7th letter of the phrase).
// It's supposed to be "the quick..." but they made a mistake in transcribing
// the ASCII into hex.
[P1, P2, P3] = splitBy`{3} (join PlainText)
// B.1
C1, C2, C3 : [64]
C1 = 0xA826FD8CE53B855F
C2 = 0xCCE21C8112256FE6
C3 = 0x68D5C05DD9B6B900
test_B1_1 = blockEncrypt (Key1, Key2, Key3, P1) == C1
test_B1_2 = blockEncrypt (Key1, Key2, Key3, P2) == C2
test_B1_3 = blockEncrypt (Key1, Key2, Key3, P3) == C3
property passesTests = [test_B1_1, test_B1_2, test_B1_3] == ~zero