updated eel and eapml from more recent versions.

This commit is contained in:
Jocelyn Fiat
2012-06-15 14:57:12 +02:00
parent 0203e0fdc7
commit 5f3749e463
166 changed files with 396 additions and 578 deletions

View File

@@ -0,0 +1,62 @@
note
description: "Summary description for {RSA_KEY_PAIR}."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "If you think health care is expensive now, wait until you see what it costs when it's free. - P.J. O'Rourke (1993)"
class
RSA_KEY_PAIR
inherit
DEBUG_OUTPUT
create
make,
make_with_exponent
feature {NONE}
make (bits: INTEGER)
local
e: INTEGER_X
p: INTEGER_X
q: INTEGER_X
n: INTEGER_X
p_bits: INTEGER
do
p_bits := (bits + 1) // 2
create e.make_from_integer (65537)
create p.make_random_prime (p_bits)
create q.make_random_prime (bits - p_bits)
n := p * q
create public.make (n, e)
create private.make (p, q, n, e)
end
make_with_exponent (bits: INTEGER e_a: INTEGER_X)
require
e_a.is_probably_prime
local
p: INTEGER_X
q: INTEGER_X
n: INTEGER_X
p_bits: INTEGER
do
p_bits := (bits + 1) // 2
create p.make_random_prime (p_bits)
create q.make_random_prime (bits - p_bits)
n := p * q
create public.make (n, e_a)
create private.make (p, q, n, e_a)
end
feature
public: RSA_PUBLIC_KEY
private: RSA_PRIVATE_KEY
feature {NONE} --{DEBUG_OUTPUT}
debug_output: STRING
do
result := "P: " + private.p.debug_output + " Q: " + private.q.debug_output + " D: " + private.d.debug_output + " N: " + public.modulus.debug_output + " E: " + public.exponent.debug_output
end
end

View File

@@ -0,0 +1,46 @@
note
description: "Summary description for {RSA_PRIVATE_KEY}."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "If you have ten thousand regulations, you destroy all respect for the law. - Winston Churchill"
class
RSA_PRIVATE_KEY
create
make
feature
make (p_a: INTEGER_X q_a: INTEGER_X n_a: INTEGER_X e_a: INTEGER_X)
local
phi: INTEGER_X
do
p := p_a
q := q_a
n := n_a
e := e_a
phi := (p - p.one) * (q - q.one)
d := e.inverse_value (phi)
end
sign (message: INTEGER_X): INTEGER_X
do
result := decrypt (message)
end
decrypt (cipher: INTEGER_X): INTEGER_X
do
result := cipher.powm_value (d, n)
end
feature
p: INTEGER_X
q: INTEGER_X
d: INTEGER_X
n: INTEGER_X
e: INTEGER_X
invariant
p * q ~ n
end

View File

@@ -0,0 +1,43 @@
note
description: "Summary description for {RSA_KEY}."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Tyranny is always better organized than freedom. - Charles Peguy"
class
RSA_PUBLIC_KEY
inherit
DEBUG_OUTPUT
create
make
feature
make (modulus_a: INTEGER_X exponent_a: INTEGER_X)
do
modulus := modulus_a
exponent := exponent_a
end
verify (message: INTEGER_X signature: INTEGER_X): BOOLEAN
do
result := encrypt (signature) ~ message
end
encrypt (message: INTEGER_X): INTEGER_X
do
result := message.powm_value (exponent, modulus)
end
feature
modulus: INTEGER_X
exponent: INTEGER_X
feature {RSA_KEY_PAIR}--{DEBUG_OUTPUT}
debug_output: STRING
do
result := "Modulus: 0x" + modulus.out_hex
end
end

View File

@@ -0,0 +1,150 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Reader, suppose you were an idiot. And suppose you were a member of Congress. But I repeat myself. - Mark Twain"
deferred class
AES_COMMON
inherit
ROTATE_FACILITIES
feature
S: SPECIAL [NATURAL_8]
-- The S box
once
create result.make_filled (0, 256)
result [0x00] := 0x63 result [0x01] := 0x7c result [0x02] := 0x77 result [0x03] := 0x7b result [0x04] := 0xf2 result [0x05] := 0x6b result [0x06] := 0x6f result [0x07] := 0xc5
result [0x08] := 0x30 result [0x09] := 0x01 result [0x0a] := 0x67 result [0x0b] := 0x2b result [0x0c] := 0xfe result [0x0d] := 0xd7 result [0x0e] := 0xab result [0x0f] := 0x76
result [0x10] := 0xca result [0x11] := 0x82 result [0x12] := 0xc9 result [0x13] := 0x7d result [0x14] := 0xfa result [0x15] := 0x59 result [0x16] := 0x47 result [0x17] := 0xf0
result [0x18] := 0xad result [0x19] := 0xd4 result [0x1a] := 0xa2 result [0x1b] := 0xaf result [0x1c] := 0x9c result [0x1d] := 0xa4 result [0x1e] := 0x72 result [0x1f] := 0xc0
result [0x20] := 0xb7 result [0x21] := 0xfd result [0x22] := 0x93 result [0x23] := 0x26 result [0x24] := 0x36 result [0x25] := 0x3f result [0x26] := 0xf7 result [0x27] := 0xcc
result [0x28] := 0x34 result [0x29] := 0xa5 result [0x2a] := 0xe5 result [0x2b] := 0xf1 result [0x2c] := 0x71 result [0x2d] := 0xd8 result [0x2e] := 0x31 result [0x2f] := 0x15
result [0x30] := 0x04 result [0x31] := 0xc7 result [0x32] := 0x23 result [0x33] := 0xc3 result [0x34] := 0x18 result [0x35] := 0x96 result [0x36] := 0x05 result [0x37] := 0x9a
result [0x38] := 0x07 result [0x39] := 0x12 result [0x3a] := 0x80 result [0x3b] := 0xe2 result [0x3c] := 0xeb result [0x3d] := 0x27 result [0x3e] := 0xb2 result [0x3f] := 0x75
result [0x40] := 0x09 result [0x41] := 0x83 result [0x42] := 0x2c result [0x43] := 0x1a result [0x44] := 0x1b result [0x45] := 0x6e result [0x46] := 0x5a result [0x47] := 0xa0
result [0x48] := 0x52 result [0x49] := 0x3b result [0x4a] := 0xd6 result [0x4b] := 0xb3 result [0x4c] := 0x29 result [0x4d] := 0xe3 result [0x4e] := 0x2f result [0x4f] := 0x84
result [0x50] := 0x53 result [0x51] := 0xd1 result [0x52] := 0x00 result [0x53] := 0xed result [0x54] := 0x20 result [0x55] := 0xfc result [0x56] := 0xb1 result [0x57] := 0x5b
result [0x58] := 0x6a result [0x59] := 0xcb result [0x5a] := 0xbe result [0x5b] := 0x39 result [0x5c] := 0x4a result [0x5d] := 0x4c result [0x5e] := 0x58 result [0x5f] := 0xcf
result [0x60] := 0xd0 result [0x61] := 0xef result [0x62] := 0xaa result [0x63] := 0xfb result [0x64] := 0x43 result [0x65] := 0x4d result [0x66] := 0x33 result [0x67] := 0x85
result [0x68] := 0x45 result [0x69] := 0xf9 result [0x6a] := 0x02 result [0x6b] := 0x7f result [0x6c] := 0x50 result [0x6d] := 0x3c result [0x6e] := 0x9f result [0x6f] := 0xa8
result [0x70] := 0x51 result [0x71] := 0xa3 result [0x72] := 0x40 result [0x73] := 0x8f result [0x74] := 0x92 result [0x75] := 0x9d result [0x76] := 0x38 result [0x77] := 0xf5
result [0x78] := 0xbc result [0x79] := 0xb6 result [0x7a] := 0xda result [0x7b] := 0x21 result [0x7c] := 0x10 result [0x7d] := 0xff result [0x7e] := 0xf3 result [0x7f] := 0xd2
result [0x80] := 0xcd result [0x81] := 0x0c result [0x82] := 0x13 result [0x83] := 0xec result [0x84] := 0x5f result [0x85] := 0x97 result [0x86] := 0x44 result [0x87] := 0x17
result [0x88] := 0xc4 result [0x89] := 0xa7 result [0x8a] := 0x7e result [0x8b] := 0x3d result [0x8c] := 0x64 result [0x8d] := 0x5d result [0x8e] := 0x19 result [0x8f] := 0x73
result [0x90] := 0x60 result [0x91] := 0x81 result [0x92] := 0x4f result [0x93] := 0xdc result [0x94] := 0x22 result [0x95] := 0x2a result [0x96] := 0x90 result [0x97] := 0x88
result [0x98] := 0x46 result [0x99] := 0xee result [0x9a] := 0xb8 result [0x9b] := 0x14 result [0x9c] := 0xde result [0x9d] := 0x5e result [0x9e] := 0x0b result [0x9f] := 0xdb
result [0xa0] := 0xe0 result [0xa1] := 0x32 result [0xa2] := 0x3a result [0xa3] := 0x0a result [0xa4] := 0x49 result [0xa5] := 0x06 result [0xa6] := 0x24 result [0xa7] := 0x5c
result [0xa8] := 0xc2 result [0xa9] := 0xd3 result [0xaa] := 0xac result [0xab] := 0x62 result [0xac] := 0x91 result [0xad] := 0x95 result [0xae] := 0xe4 result [0xaf] := 0x79
result [0xb0] := 0xe7 result [0xb1] := 0xc8 result [0xb2] := 0x37 result [0xb3] := 0x6d result [0xb4] := 0x8d result [0xb5] := 0xd5 result [0xb6] := 0x4e result [0xb7] := 0xa9
result [0xb8] := 0x6c result [0xb9] := 0x56 result [0xba] := 0xf4 result [0xbb] := 0xea result [0xbc] := 0x65 result [0xbd] := 0x7a result [0xbe] := 0xae result [0xbf] := 0x08
result [0xc0] := 0xba result [0xc1] := 0x78 result [0xc2] := 0x25 result [0xc3] := 0x2e result [0xc4] := 0x1c result [0xc5] := 0xa6 result [0xc6] := 0xb4 result [0xc7] := 0xc6
result [0xc8] := 0xe8 result [0xc9] := 0xdd result [0xca] := 0x74 result [0xcb] := 0x1f result [0xcc] := 0x4b result [0xcd] := 0xbd result [0xce] := 0x8b result [0xcf] := 0x8a
result [0xd0] := 0x70 result [0xd1] := 0x3e result [0xd2] := 0xb5 result [0xd3] := 0x66 result [0xd4] := 0x48 result [0xd5] := 0x03 result [0xd6] := 0xf6 result [0xd7] := 0x0e
result [0xd8] := 0x61 result [0xd9] := 0x35 result [0xda] := 0x57 result [0xdb] := 0xb9 result [0xdc] := 0x86 result [0xdd] := 0xc1 result [0xde] := 0x1d result [0xdf] := 0x9e
result [0xe0] := 0xe1 result [0xe1] := 0xf8 result [0xe2] := 0x98 result [0xe3] := 0x11 result [0xe4] := 0x69 result [0xe5] := 0xd9 result [0xe6] := 0x8e result [0xe7] := 0x94
result [0xe8] := 0x9b result [0xe9] := 0x1e result [0xea] := 0x87 result [0xeb] := 0xe9 result [0xec] := 0xce result [0xed] := 0x55 result [0xee] := 0x28 result [0xef] := 0xdf
result [0xf0] := 0x8c result [0xf1] := 0xa1 result [0xf2] := 0x89 result [0xf3] := 0x0d result [0xf4] := 0xbf result [0xf5] := 0xe6 result [0xf6] := 0x42 result [0xf7] := 0x68
result [0xf8] := 0x41 result [0xf9] := 0x99 result [0xfa] := 0x2d result [0xfb] := 0x0f result [0xfc] := 0xb0 result [0xfd] := 0x54 result [0xfe] := 0xbb result [0xff] := 0x16
end
Si: SPECIAL [NATURAL_8]
-- S inverse box
once
create result.make_filled (0, 256)
result [0x00] := 0x52 result [0x01] := 0x09 result [0x02] := 0x6a result [0x03] := 0xd5 result [0x04] := 0x30 result [0x05] := 0x36 result [0x06] := 0xa5 result [0x07] := 0x38
result [0x08] := 0xbf result [0x09] := 0x40 result [0x0a] := 0xa3 result [0x0b] := 0x9e result [0x0c] := 0x81 result [0x0d] := 0xf3 result [0x0e] := 0xd7 result [0x0f] := 0xfb
result [0x10] := 0x7c result [0x11] := 0xe3 result [0x12] := 0x39 result [0x13] := 0x82 result [0x14] := 0x9b result [0x15] := 0x2f result [0x16] := 0xff result [0x17] := 0x87
result [0x18] := 0x34 result [0x19] := 0x8e result [0x1a] := 0x43 result [0x1b] := 0x44 result [0x1c] := 0xc4 result [0x1d] := 0xde result [0x1e] := 0xe9 result [0x1f] := 0xcb
result [0x20] := 0x54 result [0x21] := 0x7b result [0x22] := 0x94 result [0x23] := 0x32 result [0x24] := 0xa6 result [0x25] := 0xc2 result [0x26] := 0x23 result [0x27] := 0x3d
result [0x28] := 0xee result [0x29] := 0x4c result [0x2a] := 0x95 result [0x2b] := 0x0b result [0x2c] := 0x42 result [0x2d] := 0xfa result [0x2e] := 0xc3 result [0x2f] := 0x4e
result [0x30] := 0x08 result [0x31] := 0x2e result [0x32] := 0xa1 result [0x33] := 0x66 result [0x34] := 0x28 result [0x35] := 0xd9 result [0x36] := 0x24 result [0x37] := 0xb2
result [0x38] := 0x76 result [0x39] := 0x5b result [0x3a] := 0xa2 result [0x3b] := 0x49 result [0x3c] := 0x6d result [0x3d] := 0x8b result [0x3e] := 0xd1 result [0x3f] := 0x25
result [0x40] := 0x72 result [0x41] := 0xf8 result [0x42] := 0xf6 result [0x43] := 0x64 result [0x44] := 0x86 result [0x45] := 0x68 result [0x46] := 0x98 result [0x47] := 0x16
result [0x48] := 0xd4 result [0x49] := 0xa4 result [0x4a] := 0x5c result [0x4b] := 0xcc result [0x4c] := 0x5d result [0x4d] := 0x65 result [0x4e] := 0xb6 result [0x4f] := 0x92
result [0x50] := 0x6c result [0x51] := 0x70 result [0x52] := 0x48 result [0x53] := 0x50 result [0x54] := 0xfd result [0x55] := 0xed result [0x56] := 0xb9 result [0x57] := 0xda
result [0x58] := 0x5e result [0x59] := 0x15 result [0x5a] := 0x46 result [0x5b] := 0x57 result [0x5c] := 0xa7 result [0x5d] := 0x8d result [0x5e] := 0x9d result [0x5f] := 0x84
result [0x60] := 0x90 result [0x61] := 0xd8 result [0x62] := 0xab result [0x63] := 0x00 result [0x64] := 0x8c result [0x65] := 0xbc result [0x66] := 0xd3 result [0x67] := 0x0a
result [0x68] := 0xf7 result [0x69] := 0xe4 result [0x6a] := 0x58 result [0x6b] := 0x05 result [0x6c] := 0xb8 result [0x6d] := 0xb3 result [0x6e] := 0x45 result [0x6f] := 0x06
result [0x70] := 0xd0 result [0x71] := 0x2c result [0x72] := 0x1e result [0x73] := 0x8f result [0x74] := 0xca result [0x75] := 0x3f result [0x76] := 0x0f result [0x77] := 0x02
result [0x78] := 0xc1 result [0x79] := 0xaf result [0x7a] := 0xbd result [0x7b] := 0x03 result [0x7c] := 0x01 result [0x7d] := 0x13 result [0x7e] := 0x8a result [0x7f] := 0x6b
result [0x80] := 0x3a result [0x81] := 0x91 result [0x82] := 0x11 result [0x83] := 0x41 result [0x84] := 0x4f result [0x85] := 0x67 result [0x86] := 0xdc result [0x87] := 0xea
result [0x88] := 0x97 result [0x89] := 0xf2 result [0x8a] := 0xcf result [0x8b] := 0xce result [0x8c] := 0xf0 result [0x8d] := 0xb4 result [0x8e] := 0xe6 result [0x8f] := 0x73
result [0x90] := 0x96 result [0x91] := 0xac result [0x92] := 0x74 result [0x93] := 0x22 result [0x94] := 0xe7 result [0x95] := 0xad result [0x96] := 0x35 result [0x97] := 0x85
result [0x98] := 0xe2 result [0x99] := 0xf9 result [0x9a] := 0x37 result [0x9b] := 0xe8 result [0x9c] := 0x1c result [0x9d] := 0x75 result [0x9e] := 0xdf result [0x9f] := 0x6e
result [0xa0] := 0x47 result [0xa1] := 0xf1 result [0xa2] := 0x1a result [0xa3] := 0x71 result [0xa4] := 0x1d result [0xa5] := 0x29 result [0xa6] := 0xc5 result [0xa7] := 0x89
result [0xa8] := 0x6f result [0xa9] := 0xb7 result [0xaa] := 0x62 result [0xab] := 0x0e result [0xac] := 0xaa result [0xad] := 0x18 result [0xae] := 0xbe result [0xaf] := 0x1b
result [0xb0] := 0xfc result [0xb1] := 0x56 result [0xb2] := 0x3e result [0xb3] := 0x4b result [0xb4] := 0xc6 result [0xb5] := 0xd2 result [0xb6] := 0x79 result [0xb7] := 0x20
result [0xb8] := 0x9a result [0xb9] := 0xdb result [0xba] := 0xc0 result [0xbb] := 0xfe result [0xbc] := 0x78 result [0xbd] := 0xcd result [0xbe] := 0x5a result [0xbf] := 0xf4
result [0xc0] := 0x1f result [0xc1] := 0xdd result [0xc2] := 0xa8 result [0xc3] := 0x33 result [0xc4] := 0x88 result [0xc5] := 0x07 result [0xc6] := 0xc7 result [0xc7] := 0x31
result [0xc8] := 0xb1 result [0xc9] := 0x12 result [0xca] := 0x10 result [0xcb] := 0x59 result [0xcc] := 0x27 result [0xcd] := 0x80 result [0xce] := 0xec result [0xcf] := 0x5f
result [0xd0] := 0x60 result [0xd1] := 0x51 result [0xd2] := 0x7f result [0xd3] := 0xa9 result [0xd4] := 0x19 result [0xd5] := 0xb5 result [0xd6] := 0x4a result [0xd7] := 0x0d
result [0xd8] := 0x2d result [0xd9] := 0xe5 result [0xda] := 0x7a result [0xdb] := 0x9f result [0xdc] := 0x93 result [0xdd] := 0xc9 result [0xde] := 0x9c result [0xdf] := 0xef
result [0xe0] := 0xa0 result [0xe1] := 0xe0 result [0xe2] := 0x3b result [0xe3] := 0x4d result [0xe4] := 0xae result [0xe5] := 0x2a result [0xe6] := 0xf5 result [0xe7] := 0xb0
result [0xe8] := 0xc8 result [0xe9] := 0xeb result [0xea] := 0xbb result [0xeb] := 0x3c result [0xec] := 0x83 result [0xed] := 0x53 result [0xee] := 0x99 result [0xef] := 0x61
result [0xf0] := 0x17 result [0xf1] := 0x2b result [0xf2] := 0x04 result [0xf3] := 0x7e result [0xf4] := 0xba result [0xf5] := 0x77 result [0xf6] := 0xd6 result [0xf7] := 0x26
result [0xf8] := 0xe1 result [0xf9] := 0x69 result [0xfa] := 0x14 result [0xfb] := 0x63 result [0xfc] := 0x55 result [0xfd] := 0x21 result [0xfe] := 0x0c result [0xff] := 0x7d
end
inv_sub_bytes (in: NATURAL_32): NATURAL_32
do
result := si [((in |>> 24) & 0xff).to_integer_32].to_natural_32 |<< 24
result := result | (si [((in |>> 16) & 0xff).to_integer_32].to_natural_32 |<< 16)
result := result | (si [((in |>> 8) & 0xff).to_integer_32].to_natural_32 |<< 8)
result := result | (si [(in & 0xff).to_integer_32]).to_natural_32
ensure
(result & 0xff).to_natural_8 = si [(in & 0xff).to_integer_32]
((result |>> 8) & 0xff).to_natural_8 = si [((in |>> 8) & 0xff).to_integer_32]
((result |>> 16) & 0xff).to_natural_8 = si [((in |>> 16) & 0xff).to_integer_32]
(result |>> 24).to_natural_8 = si [((in |>> 24) & 0xff).to_integer_32]
end
sub_bytes (in: NATURAL_32): NATURAL_32
do
result := s [((in |>> 24) & 0xff).to_integer_32].to_natural_32 |<< 24
result := result | (s [((in |>> 16) & 0xff).to_integer_32].to_natural_32 |<< 16)
result := result | (s [((in |>> 8) & 0xff).to_integer_32].to_natural_32 |<< 8)
result := result | (s [(in & 0xff).to_integer_32])
ensure
(result & 0xff).to_natural_8 = s [(in & 0xff).to_integer_32]
((result |>> 8) & 0xff).to_natural_8 = s [((in |>> 8) & 0xff).to_integer_32]
((result |>> 16) & 0xff).to_natural_8 = s [((in |>> 16) & 0xff).to_integer_32]
(result |>> 24).to_natural_8 = s [((in |>> 24) & 0xff).to_integer_32]
end
FFmulX (x: NATURAL_32): NATURAL_32
do
result := ((x & m2) |<< 1).bit_xor (((x & m1) |>> 7) * m3)
end
m1: NATURAL_32 = 0x80808080
m2: NATURAL_32 = 0x7f7f7f7f
m3: NATURAL_32 = 0x0000001b
feature
s_box_inverse: BOOLEAN
local
counter: INTEGER
do
from
counter := 0
result := true
until
counter > 255 or not result
loop
result := si [s [counter].to_integer_32].to_integer_32 = counter
counter := counter + 1
end
end
s_box_inverse_once: BOOLEAN
-- Is the s-box correct as long as nothing modifies it
once
result := s_box_inverse
end
invariant
s_box_inverse: s_box_inverse_once
end

View File

@@ -0,0 +1,531 @@
note
description: "Tagging class for various size/speed tradeoffs of AES"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Talk is cheap - except when Congress does it. - Cullen Hightower"
deferred class
AES_ENGINE
inherit
AES_COMMON
BYTE_FACILITIES
feature
make_tables
do
two_table := multiply_table (0x2)
three_table := multiply_table (0x3)
nine_table := multiply_table (0x9)
eleven_table := multiply_table (0xb)
thirteen_table := multiply_table (0xd)
fourteen_table := multiply_table (0xe)
end
block_size: INTEGER = 16
feature
mcol (x: NATURAL_32): NATURAL_32
local
f2: NATURAL_32
do
f2 := FFmulX (x)
result := f2.bit_xor (rotate_right_32 (x.bit_xor (f2), 8)).bit_xor (rotate_right_32 (x, 16)).bit_xor (rotate_right_32 (x, 24))
end
-- State matrix columns
column_0: NATURAL_32
column_1: NATURAL_32
column_2: NATURAL_32
column_3: NATURAL_32
feature --Prepare input blocks for processing and return
unpack (bytes: SPECIAL [NATURAL_8] offset: INTEGER)
require
bytes.valid_index (offset)
bytes.valid_index (offset + 15)
local
index: INTEGER
do
index := bytes.lower
column_0 := as_natural_32_be (bytes, offset + index)
column_1 := as_natural_32_be (bytes, offset + index + 4)
column_2 := as_natural_32_be (bytes, offset + index + 8)
column_3 := as_natural_32_be (bytes, offset + index + 12)
ensure
bytes_match_blocks (bytes)
end
pack (bytes: SPECIAL [NATURAL_8] offset: INTEGER)
require
bytes.valid_index (offset)
bytes.valid_index (offset + 15)
local
index: INTEGER
do
index := bytes.lower
from_natural_32_be (column_0, bytes, offset + index)
from_natural_32_be (column_1, bytes, offset + index + 4)
from_natural_32_be (column_2, bytes, offset + index + 8)
from_natural_32_be (column_3, bytes, offset + index + 12)
ensure
bytes_match_blocks (bytes)
end
bytes_match_blocks (bytes: SPECIAL [NATURAL_8]): BOOLEAN
do
result := true
result := result and bytes [0] = (column_0 |>> 24 & 0xff).to_natural_8
result := result and bytes [1] = (column_0 |>> 16 & 0xff).to_natural_8
result := result and bytes [2] = (column_0 |>> 8 & 0xff).to_natural_8
result := result and bytes [3] = (column_0 & 0xff).to_natural_8
result := result and bytes [4] = (column_1 |>> 24 & 0xff).to_natural_8
result := result and bytes [5] = (column_1 |>> 16 & 0xff).to_natural_8
result := result and bytes [6] = (column_1 |>> 8 & 0xff).to_natural_8
result := result and bytes [7] = (column_1 & 0xff).to_natural_8
result := result and bytes [8] = (column_2 |>> 24 & 0xff).to_natural_8
result := result and bytes [9] = (column_2 |>> 16 & 0xff).to_natural_8
result := result and bytes [10] = (column_2 |>> 8 & 0xff).to_natural_8
result := result and bytes [11] = (column_2 & 0xff).to_natural_8
result := result and bytes [12] = (column_3 |>> 24 & 0xff).to_natural_8
result := result and bytes [13] = (column_3 |>> 16 & 0xff).to_natural_8
result := result and bytes [14] = (column_3 |>> 8 & 0xff).to_natural_8
result := result and bytes [15] = (column_3 & 0xff).to_natural_8
ensure
bytes [0] = (column_0 & 0xff).to_natural_8
bytes [1] = (column_0 |>> 8 & 0xff).to_natural_8
bytes [2] = (column_0 |>> 16 & 0xff).to_natural_8
bytes [3] = (column_0 |>> 24 & 0xff).to_natural_8
bytes [4] = (column_1 & 0xff).to_natural_8
bytes [5] = (column_1 |>> 8 & 0xff).to_natural_8
bytes [6] = (column_1 |>> 16 & 0xff).to_natural_8
bytes [7] = (column_1 |>> 24 & 0xff).to_natural_8
bytes [8] = (column_2 & 0xff).to_natural_8
bytes [9] = (column_2 |>> 8 & 0xff).to_natural_8
bytes [10] = (column_2 |>> 16 & 0xff).to_natural_8
bytes [11] = (column_2 |>> 24 & 0xff).to_natural_8
bytes [12] = (column_3 & 0xff).to_natural_8
bytes [13] = (column_3 |>> 8 & 0xff).to_natural_8
bytes [14] = (column_3 |>> 16 & 0xff).to_natural_8
bytes [15] = (column_3 |>> 24 & 0xff).to_natural_8
end
feature
encrypt_work (max_index: INTEGER)
local
index: INTEGER
do
add_round_key (index)
from
index := 4
until
index >= max_index - 4
loop
sub_columns
shift_rows
mix_columns
add_round_key (index)
index := index + 4
variant
max_index - index + 2
end
sub_columns
shift_rows
add_round_key (index)
end
decrypt_work (max_index: INTEGER)
local
index: INTEGER
do
index := max_index - 3
add_round_key (index)
from
index := index - 4
until
index = 0
loop
inv_shift_rows
inv_sub_columns
add_round_key (index)
inv_mix_columns
index := index - 4
variant
index + 1
end
inv_shift_rows
inv_sub_columns
add_round_key (index)
end
inv_sub_columns
do
column_0 := inv_sub_bytes (column_0)
column_1 := inv_sub_bytes (column_1)
column_2 := inv_sub_bytes (column_2)
column_3 := inv_sub_bytes (column_3)
end
inv_mix_columns
do
column_0 := inv_mix_column (column_0)
column_1 := inv_mix_column (column_1)
column_2 := inv_mix_column (column_2)
column_3 := inv_mix_column (column_3)
end
mix_columns
do
column_0 := mix_column (column_0)
column_1 := mix_column (column_1)
column_2 := mix_column (column_2)
column_3 := mix_column (column_3)
end
inv_mix_column (in: NATURAL_32): NATURAL_32
do
result := inv_mix_0 (in)
result := result | inv_mix_1 (in)
result := result | inv_mix_2 (in)
result := result | inv_mix_3 (in)
end
inv_mix_0 (in: NATURAL_32): NATURAL_32
local
part_0: NATURAL_32
part_1: NATURAL_32
part_2: NATURAL_32
part_3: NATURAL_32
do
part_0 := multiply_and_reduce ((in |>> 24 & 0xff).to_natural_8, 0xe)
part_1 := multiply_and_reduce ((in |>> 16 & 0xff).to_natural_8, 0xb)
part_2 := multiply_and_reduce ((in |>> 8 & 0xff).to_natural_8, 0xd)
part_3 := multiply_and_reduce ((in & 0xff).to_natural_8, 0x9)
result := part_0.bit_xor (part_1).bit_xor (part_2).bit_xor (part_3) |<< 24
end
inv_mix_1 (in: NATURAL_32): NATURAL_32
local
part_0: NATURAL_32
part_1: NATURAL_32
part_2: NATURAL_32
part_3: NATURAL_32
do
part_0 := multiply_and_reduce ((in |>> 24 & 0xff).to_natural_8, 0x9)
part_1 := multiply_and_reduce ((in |>> 16 & 0xff).to_natural_8, 0xe)
part_2 := multiply_and_reduce ((in |>> 8 & 0xff).to_natural_8, 0xb)
part_3 := multiply_and_reduce ((in & 0xff).to_natural_8, 0xd)
result := part_0.bit_xor (part_1).bit_xor (part_2).bit_xor (part_3) |<< 16
end
inv_mix_2 (in: NATURAL_32): NATURAL_32
local
part_0: NATURAL_32
part_1: NATURAL_32
part_2: NATURAL_32
part_3: NATURAL_32
do
part_0 := multiply_and_reduce ((in |>> 24 & 0xff).to_natural_8, 0xd)
part_1 := multiply_and_reduce ((in |>> 16 & 0xff).to_natural_8, 0x9)
part_2 := multiply_and_reduce ((in |>> 8 & 0xff).to_natural_8, 0xe)
part_3 := multiply_and_reduce ((in & 0xff).to_natural_8, 0xb)
result := part_0.bit_xor (part_1).bit_xor (part_2).bit_xor (part_3) |<< 8
end
inv_mix_3 (in: NATURAL_32): NATURAL_32
local
part_0: NATURAL_32
part_1: NATURAL_32
part_2: NATURAL_32
part_3: NATURAL_32
do
part_0 := multiply_and_reduce ((in |>> 24 & 0xff).to_natural_8, 0xb)
part_1 := multiply_and_reduce ((in |>> 16 & 0xff).to_natural_8, 0xd)
part_2 := multiply_and_reduce ((in |>> 8 & 0xff).to_natural_8, 0x9)
part_3 := multiply_and_reduce ((in & 0xff).to_natural_8, 0xe)
result := part_0.bit_xor (part_1).bit_xor (part_2).bit_xor (part_3)
end
mix_column (in: NATURAL_32): NATURAL_32
do
result := mix_0 (in)
result := result | mix_1 (in)
result := result | mix_2 (in)
result := result | mix_3 (in)
end
mix_0 (in: NATURAL_32): NATURAL_32
local
part_0: NATURAL_32
part_1: NATURAL_32
part_2: NATURAL_32
part_3: NATURAL_32
do
part_0 := multiply_and_reduce ((in |>> 24 & 0xff).to_natural_8, 0x2)
part_1 := multiply_and_reduce ((in |>> 16 & 0xff).to_natural_8, 0x3)
part_2 := in |>> 8 & 0xff
part_3 := in & 0xff
result := part_0.bit_xor (part_1).bit_xor (part_2).bit_xor (part_3) |<< 24
end
mix_1 (in: NATURAL_32): NATURAL_32
local
part_0: NATURAL_32
part_1: NATURAL_32
part_2: NATURAL_32
part_3: NATURAL_32
do
part_0 := (in |>> 24 & 0xff)
part_1 := multiply_and_reduce ((in |>> 16 & 0xff).to_natural_8, 0x2)
part_2 := multiply_and_reduce ((in |>> 8 & 0xff).to_natural_8, 0x3)
part_3 := in & 0xff
result := part_0.bit_xor (part_1).bit_xor (part_2).bit_xor (part_3) |<< 16
end
mix_2 (in: NATURAL_32): NATURAL_32
local
part_0: NATURAL_32
part_1: NATURAL_32
part_2: NATURAL_32
part_3: NATURAL_32
do
part_0 := in |>> 24 & 0xff
part_1 := in |>> 16 & 0xff
part_2 := multiply_and_reduce ((in |>> 8 & 0xff).to_natural_8, 0x2)
part_3 := multiply_and_reduce ((in & 0xff).to_natural_8, 0x3)
result := part_0.bit_xor (part_1).bit_xor (part_2).bit_xor (part_3) |<< 8
end
mix_3 (in: NATURAL_32): NATURAL_32
local
part_0: NATURAL_32
part_1: NATURAL_32
part_2: NATURAL_32
part_3: NATURAL_32
do
part_0 := multiply_and_reduce ((in |>> 24 & 0xff).to_natural_8, 0x3)
part_1 := in |>> 16 & 0xff
part_2 := in |>> 8 & 0xff
part_3 := multiply_and_reduce ((in & 0xff).to_natural_8, 0x2)
result := part_0.bit_xor (part_1).bit_xor (part_2).bit_xor (part_3)
end
sub_columns
do
column_0 := sub_bytes (column_0)
column_1 := sub_bytes (column_1)
column_2 := sub_bytes (column_2)
column_3 := sub_bytes (column_3)
end
inv_shift_rows
local
column_0_new: NATURAL_32
column_1_new: NATURAL_32
column_2_new: NATURAL_32
column_3_new: NATURAL_32
do
column_0_new := column_0 & 0xff000000
column_0_new := column_0_new | (column_3 & 0x00ff0000)
column_0_new := column_0_new | (column_2 & 0x0000ff00)
column_0_new := column_0_new | (column_1 & 0x000000ff)
column_1_new := column_1 & 0xff000000
column_1_new := column_1_new | (column_0 & 0x00ff0000)
column_1_new := column_1_new | (column_3 & 0x0000ff00)
column_1_new := column_1_new | (column_2 & 0x000000ff)
column_2_new := column_2 & 0xff000000
column_2_new := column_2_new | (column_1 & 0x00ff0000)
column_2_new := column_2_new | (column_0 & 0x0000ff00)
column_2_new := column_2_new | (column_3 & 0x000000ff)
column_3_new := column_3 & 0xff000000
column_3_new := column_3_new | (column_2 & 0x00ff0000)
column_3_new := column_3_new | (column_1 & 0x0000ff00)
column_3_new := column_3_new | (column_0 & 0x000000ff)
column_0 := column_0_new
column_1 := column_1_new
column_2 := column_2_new
column_3 := column_3_new
ensure
column_0 |>> 24 & 0xff = old column_0 |>> 24 & 0xff
column_0 |>> 16 & 0xff = old column_3 |>> 16 & 0xff
column_0 |>> 8 & 0xff = old column_2 |>> 8 & 0xff
column_0 & 0xff = old column_1 & 0xff
column_1 |>> 24 & 0xff = old column_1 |>> 24 & 0xff
column_1 |>> 16 & 0xff = old column_0 |>> 16 & 0xff
column_1 |>> 8 & 0xff = old column_3 |>> 8 & 0xff
column_1 & 0xff = old column_2 & 0xff
column_2 |>> 24 & 0xff = old column_2 |>> 24& 0xff
column_2 |>> 16 & 0xff = old column_1 |>> 16 & 0xff
column_2 |>> 8 & 0xff = old column_0 |>> 8 & 0xff
column_2 & 0xff = old column_3 & 0xff
column_3 |>> 24& 0xff = old column_3 |>> 24 & 0xff
column_3 |>> 16 & 0xff = old column_2 |>> 16 & 0xff
column_3 |>> 8 & 0xff = old column_1 |>> 8 & 0xff
column_3 & 0xff = old column_0 & 0xff
end
shift_rows
local
column_0_new: NATURAL_32
column_1_new: NATURAL_32
column_2_new: NATURAL_32
column_3_new: NATURAL_32
do
column_0_new := column_0 & 0xff000000
column_0_new := column_0_new | (column_1 & 0x00ff0000)
column_0_new := column_0_new | (column_2 & 0x0000ff00)
column_0_new := column_0_new | (column_3 & 0x000000ff)
column_1_new := column_1 & 0xff000000
column_1_new := column_1_new | (column_2 & 0x00ff0000)
column_1_new := column_1_new | (column_3 & 0x0000ff00)
column_1_new := column_1_new | (column_0 & 0x000000ff)
column_2_new := column_2 & 0xff000000
column_2_new := column_2_new | (column_3 & 0x00ff0000)
column_2_new := column_2_new | (column_0 & 0x0000ff00)
column_2_new := column_2_new | (column_1 & 0x000000ff)
column_3_new := column_3 & 0xff000000
column_3_new := column_3_new | (column_0 & 0x00ff0000)
column_3_new := column_3_new | (column_1 & 0x0000ff00)
column_3_new := column_3_new | (column_2 & 0x000000ff)
column_0 := column_0_new
column_1 := column_1_new
column_2 := column_2_new
column_3 := column_3_new
ensure
column_0 |>> 24 & 0xff = old column_0 |>> 24 & 0xff
column_0 |>> 16 & 0xff = old column_1 |>> 16 & 0xff
column_0 |>> 8 & 0xff = old column_2 |>> 8 & 0xff
column_0 & 0xff = old column_3 & 0xff
column_1 |>> 24 & 0xff = old column_1 |>> 24 & 0xff
column_1 |>> 16 & 0xff = old column_2 |>> 16 & 0xff
column_1 |>> 8 & 0xff = old column_3 |>> 8 & 0xff
column_1 & 0xff = old column_0 & 0xff
column_2 |>> 24 & 0xff = old column_2 |>> 24 & 0xff
column_2 |>> 16 & 0xff = old column_3 |>> 16 & 0xff
column_2 |>> 8 & 0xff = old column_0 |>> 8 & 0xff
column_2 & 0xff = old column_1 & 0xff
column_3 |>> 24 & 0xff = old column_3 |>> 24 & 0xff
column_3 |>> 16 & 0xff = old column_0 |>> 16 & 0xff
column_3 |>> 8 & 0xff = old column_1 |>> 8 & 0xff
column_3 & 0xff = old column_2 & 0xff
end
add_round_key (schedule_index: INTEGER)
do
column_0 := column_0.bit_xor (key_schedule [schedule_index])
column_1 := column_1.bit_xor (key_schedule [schedule_index + 1])
column_2 := column_2.bit_xor (key_schedule [schedule_index + 2])
column_3 := column_3.bit_xor (key_schedule [schedule_index + 3])
end
feature -- GF(2^8) arithmetic
add (one: INTEGER two: INTEGER): INTEGER
do
result := one.bit_xor (two)
end
multiply_and_reduce (field: NATURAL_8 multiplier: NATURAL_8): NATURAL_8
local
field_expanded: NATURAL_32
do
field_expanded := multiply (field, multiplier)
result := reduce (field_expanded)
end
multiply (field: NATURAL_8 multiplier: NATURAL_8): NATURAL_32
local
counter: INTEGER
field_expanded: NATURAL_32
do
field_expanded := field
from
counter := 0
until
counter > 7
loop
if
multiplier.bit_test (counter)
then
result := result.bit_xor (field_expanded.bit_shift_left (counter))
end
counter := counter + 1
end
end
reduce (in: NATURAL_32): NATURAL_8
local
counter: INTEGER
result_expanded: NATURAL_32
do
from
counter := 31
result_expanded := in
until
counter = 7
loop
if
result_expanded.bit_test (counter)
then
result_expanded := result_expanded.bit_xor (reducer.bit_shift_right (31 - counter))
end
counter := counter - 1
end
check
result_expanded <= result.max_value
end
result := result_expanded.to_natural_8
end
s_box (in: NATURAL_8): NATURAL_8
do
result := s [in.to_integer_32]
end
two_table: SPECIAL [NATURAL_8]
-- Table of {02} * x in GF(2^8)
three_table: SPECIAL [NATURAL_8]
-- Table of {03} * x in GF(2^8)
nine_table: SPECIAL [NATURAL_8]
-- Table of {09} * x in GF(2^8)
eleven_table: SPECIAL [NATURAL_8]
-- Table of {0b} * x in GF(2^8)
thirteen_table: SPECIAL [NATURAL_8]
-- Table of {0d} * x in GF(2^8)
fourteen_table: SPECIAL [NATURAL_8]
-- Table of {0E} * x in GF(2^8)
multiply_table (multiplier: NATURAL_8): SPECIAL [NATURAL_8]
local
counter: INTEGER
do
create result.make_filled (0, 256)
from
counter := 0
until
counter = 256
loop
result [counter] := multiply_and_reduce (counter.to_natural_8, multiplier)
counter := counter + 1
variant
256 - counter + 1
end
end
reducer: NATURAL_32 = 0x8d800000
feature {NONE}
byte_sink (in: NATURAL_8)
do
do_nothing
end
key_schedule: SPECIAL [NATURAL_32]
deferred
end
end

View File

@@ -0,0 +1,758 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "The single most exciting thing you encounter in government is competence, because it's so rare. - Daniel Patrick Moynihan (1976)"
class
AES_KEY
inherit
DEBUG_OUTPUT
ECB_TARGET
rename
encrypt_block as ecb_encrypt,
decrypt_block as ecb_decrypt
end
CBC_TARGET
rename
encrypt_block as cbc_encrypt,
decrypt_block as cbc_decrypt
end
CFB_TARGET
rename
encrypt_block as cfb_encrypt
end
OFB_TARGET
rename
encrypt_block as ofb_encrypt
end
CTR_TARGET
rename
encrypt_block as ctr_encrypt
end
AES_COMMON
AES_ENGINE
create
make,
make_spec_128,
make_spec_196,
make_spec_256,
make_vector_128,
make_vector_196,
make_vector_256
feature -- Key creation
make (key_a: SPECIAL [NATURAL_8])
require
valid_lengths: key_a.count = 16 or key_a.count = 24 or key_a.count = 32
do
make_tables
key := key_a
expand_key_to_schedule (key_a)
end
feature -- Spec and test vector keys
make_vector_128
local
vector_key: SPECIAL [NATURAL_8]
do
create vector_key.make_filled (0, 16)
vector_key [0] := 0x00
vector_key [1] := 0x01
vector_key [2] := 0x02
vector_key [3] := 0x03
vector_key [4] := 0x04
vector_key [5] := 0x05
vector_key [6] := 0x06
vector_key [7] := 0x07
vector_key [8] := 0x08
vector_key [9] := 0x09
vector_key [10] := 0x0a
vector_key [11] := 0x0b
vector_key [12] := 0x0c
vector_key [13] := 0x0d
vector_key [14] := 0x0e
vector_key [15] := 0x0f
make (vector_key)
ensure
vector_128
end
make_vector_196
local
vector_key: SPECIAL [NATURAL_8]
do
create vector_key.make_filled (0, 24)
vector_key [0] := 0x00
vector_key [1] := 0x01
vector_key [2] := 0x02
vector_key [3] := 0x03
vector_key [4] := 0x04
vector_key [5] := 0x05
vector_key [6] := 0x06
vector_key [7] := 0x07
vector_key [8] := 0x08
vector_key [9] := 0x09
vector_key [10] := 0x0a
vector_key [11] := 0x0b
vector_key [12] := 0x0c
vector_key [13] := 0x0d
vector_key [14] := 0x0e
vector_key [15] := 0x0f
vector_key [16] := 0x10
vector_key [17] := 0x11
vector_key [18] := 0x12
vector_key [19] := 0x13
vector_key [20] := 0x14
vector_key [21] := 0x15
vector_key [22] := 0x16
vector_key [23] := 0x17
make (vector_key)
ensure
vector_196
end
make_vector_256
local
vector_key: SPECIAL [NATURAL_8]
do
create vector_key.make_filled (0, 32)
vector_key [0] := 0x00
vector_key [1] := 0x01
vector_key [2] := 0x02
vector_key [3] := 0x03
vector_key [4] := 0x04
vector_key [5] := 0x05
vector_key [6] := 0x06
vector_key [7] := 0x07
vector_key [8] := 0x08
vector_key [9] := 0x09
vector_key [10] := 0x0a
vector_key [11] := 0x0b
vector_key [12] := 0x0c
vector_key [13] := 0x0d
vector_key [14] := 0x0e
vector_key [15] := 0x0f
vector_key [16] := 0x10
vector_key [17] := 0x11
vector_key [18] := 0x12
vector_key [19] := 0x13
vector_key [20] := 0x14
vector_key [21] := 0x15
vector_key [22] := 0x16
vector_key [23] := 0x17
vector_key [24] := 0x18
vector_key [25] := 0x19
vector_key [26] := 0x1a
vector_key [27] := 0x1b
vector_key [28] := 0x1c
vector_key [29] := 0x1d
vector_key [30] := 0x1e
vector_key [31] := 0x1f
make (vector_key)
ensure
vector_256
end
make_spec_128
-- Make the FIPS-197 spec 128-bit key
local
spec_key: SPECIAL [NATURAL_8]
do
create spec_key.make_filled (0, 16)
spec_key[0] := 0x2b
spec_key[1] := 0x7e
spec_key[2] := 0x15
spec_key[3] := 0x16
spec_key[4] := 0x28
spec_key[5] := 0xae
spec_key[6] := 0xd2
spec_key[7] := 0xa6
spec_key[8] := 0xab
spec_key[9] := 0xf7
spec_key[10] := 0x15
spec_key[11] := 0x88
spec_key[12] := 0x09
spec_key[13] := 0xcf
spec_key[14] := 0x4f
spec_key[15] := 0x3c
make (spec_key)
ensure
spec_schedule: spec_128
end
make_spec_196
-- Make the FIPS-197 spec 196-bit key
local
spec_key: SPECIAL [NATURAL_8]
do
create spec_key.make_filled (0, 24)
spec_key [0] := 0x8e
spec_key [1] := 0x73
spec_key [2] := 0xb0
spec_key [3] := 0xf7
spec_key [4] := 0xda
spec_key [5] := 0x0e
spec_key [6] := 0x64
spec_key [7] := 0x52
spec_key [8] := 0xc8
spec_key [9] := 0x10
spec_key [10] := 0xf3
spec_key [11] := 0x2b
spec_key [12] := 0x80
spec_key [13] := 0x90
spec_key [14] := 0x79
spec_key [15] := 0xe5
spec_key [16] := 0x62
spec_key [17] := 0xf8
spec_key [18] := 0xea
spec_key [19] := 0xd2
spec_key [20] := 0x52
spec_key [21] := 0x2c
spec_key [22] := 0x6b
spec_key [23] := 0x7b
make (spec_key)
ensure
spec_schedule: spec_196
end
make_spec_256
-- Make the FIPS-197 spec 256-bit key
local
spec_key: SPECIAL [NATURAL_8]
do
create spec_key.make_filled (0, 32)
spec_key [0] := 0x60
spec_key [1] := 0x3d
spec_key [2] := 0xeb
spec_key [3] := 0x10
spec_key [4] := 0x15
spec_key [5] := 0xca
spec_key [6] := 0x71
spec_key [7] := 0xbe
spec_key [8] := 0x2b
spec_key [9] := 0x73
spec_key [10] := 0xae
spec_key [11] := 0xf0
spec_key [12] := 0x85
spec_key [13] := 0x7d
spec_key [14] := 0x77
spec_key [15] := 0x81
spec_key [16] := 0x1f
spec_key [17] := 0x35
spec_key [18] := 0x2c
spec_key [19] := 0x07
spec_key [20] := 0x3b
spec_key [21] := 0x61
spec_key [22] := 0x08
spec_key [23] := 0xd7
spec_key [24] := 0x2d
spec_key [25] := 0x98
spec_key [26] := 0x10
spec_key [27] := 0xa3
spec_key [28] := 0x09
spec_key [29] := 0x14
spec_key [30] := 0xdf
spec_key [31] := 0xf4
make (spec_key)
ensure
spec_schedule: spec_256
end
feature {ECB_TARGET} -- ECB
ecb_ready: BOOLEAN
do
result := true
end
ecb_encrypt (in: SPECIAL [NATURAL_8] in_offset: INTEGER out_array: SPECIAL [NATURAL_8] out_offset: INTEGER)
do
encrypt (in, in_offset, out_array, out_offset)
end
ecb_decrypt (in: SPECIAL [NATURAL_8] in_offset: INTEGER out_array: SPECIAL [NATURAL_8] out_offset: INTEGER)
do
decrypt (in, in_offset, out_array, out_offset)
end
feature {CBC_TARGET} -- CBC
cbc_ready: BOOLEAN
do
result := true
end
cbc_encrypt (in: SPECIAL [NATURAL_8] in_offset: INTEGER out_array: SPECIAL [NATURAL_8] out_offset: INTEGER)
do
encrypt (in, in_offset, out_array, out_offset)
end
cbc_decrypt (in: SPECIAL [NATURAL_8] in_offset: INTEGER out_array: SPECIAL [NATURAL_8] out_offset: INTEGER)
do
decrypt (in, in_offset, out_array, out_offset)
end
feature {CFB_TARGET} -- CFB
cfb_ready: BOOLEAN
do
result := true
end
cfb_encrypt (in: SPECIAL [NATURAL_8] in_offset: INTEGER out_array: SPECIAL [NATURAL_8] out_offset: INTEGER)
do
encrypt (in, in_offset, out_array, out_offset)
end
feature {OFB_TARGET} -- OFB
ofb_ready: BOOLEAN
do
result := true
end
ofb_encrypt (in: SPECIAL [NATURAL_8] in_offset: INTEGER out_array: SPECIAL [NATURAL_8] out_offset: INTEGER)
do
encrypt (in, in_offset, out_array, out_offset)
end
feature {CTR_TARGET} -- CTR
ctr_ready: BOOLEAN
do
result := true
end
ctr_encrypt (in: SPECIAL [NATURAL_8] in_offset: INTEGER out_array: SPECIAL [NATURAL_8] out_offset: INTEGER)
do
encrypt (in, in_offset, out_array, out_offset)
end
feature -- Operations
encrypt (in: SPECIAL [NATURAL_8] in_offset: INTEGER out_array: SPECIAL [NATURAL_8] out_offset: INTEGER)
require
in.valid_index (in_offset)
out_array.valid_index (out_offset)
in.valid_index (in_offset + 15)
out_array.valid_index (out_offset + 15)
do
unpack (in, in_offset)
encrypt_work (key_schedule.upper)
pack (out_array, out_offset)
end
decrypt (in: SPECIAL [NATURAL_8] in_offset: INTEGER out_array: SPECIAL [NATURAL_8] out_offset: INTEGER)
require
in.valid_index (in_offset)
out_array.valid_index (out_offset)
in.valid_index (in_offset + 15)
out_array.valid_index (out_offset + 15)
do
unpack (in, in_offset)
decrypt_work (key_schedule.upper)
pack (out_array, out_offset)
end
feature --Implementation
expand_key_to_schedule (key_a: SPECIAL [NATURAL_8])
require
valid_lengths: key_a.count = 16 or key_a.count = 24 or key_a.count = 32
do
copy_key_to_schedule (key_a)
end
copy_key_to_schedule (key_a: SPECIAL [NATURAL_8])
require
valid_lengths: key_a.count = 16 or key_a.count = 24 or key_a.count = 32
do
copy_key_to_made_schedule (key_a, 4 * (rounds + 1), key_a.count // 4)
end
copy_key_to_made_schedule (key_a: SPECIAL [NATURAL_8] schedule_count: INTEGER key_word_count: INTEGER)
require
valid_lengths: key_a.count = 16 or key_a.count = 24 or key_a.count = 32
local
i: INTEGER
t: INTEGER
sub1, sub2, sub3, sub4: NATURAL_32
temp: NATURAL_32
do
create key_schedule.make_filled (0, schedule_count)
from
t := 0
i := 0
until
i > key.upper
loop
sub1 := key [i].to_natural_32 |<< 24
i := i + 1
sub2 := key [i].to_natural_32 |<< 16
i := i + 1
sub3 := key [i].to_natural_32 |<< 8
i := i + 1
sub4 := key [i].to_natural_32
i := i + 1
key_schedule [t] := sub1 | sub2 | sub3 | sub4
t := t + 1
end
from
i := key_a.count.bit_shift_right (2)
until
i >= schedule_count
loop
temp := key_schedule [i - 1]
if
i \\ key_word_count = 0
then
temp := sub_word (rot_word (temp)).bit_xor (round_constant [i // key_word_count])
elseif
key_word_count = 8 and i \\ key_word_count = 4
then
temp := sub_word(temp)
end
key_schedule [i] := key_schedule [i - key_word_count].bit_xor (temp)
i := i + 1
end
end
inv_mcol (x: NATURAL_32): NATURAL_32
local
f2: NATURAL_32
f4: NATURAL_32
f8: NATURAL_32
f9: NATURAL_32
do
f2 := FFmulX (x)
f4 := FFmulX (f2)
f8 := FFmulX (f4)
f9 := x.bit_xor(f8)
result := f2.bit_xor (f4).bit_xor (f8).bit_xor (rotate_right_32 (f2.bit_xor (f9), 8)).bit_xor (rotate_right_32 (f4.bit_xor (f9), 16)).bit_xor (rotate_right_32 (f9, 24))
end
round_constant: SPECIAL [NATURAL_32]
-- rcon
once
create result.make_filled (0, 11)
result [0] := 0x00000000
result [1] := 0x01000000
result [2] := 0x02000000
result [3] := 0x04000000
result [4] := 0x08000000
result [5] := 0x10000000
result [6] := 0x20000000
result [7] := 0x40000000
result [8] := 0x80000000
result [9] := 0x1b000000
result [10] := 0x36000000
end
rounds: INTEGER
require
key.count = 16 or key.count = 24 or key.count = 32
do
result := key.count.bit_shift_right (2) + 6
ensure
result = key.count // 4 + 6
end
key: SPECIAL [NATURAL_8]
sub_word (x_a: NATURAL_32): NATURAL_32
-- S-box word substitution
local
x: INTEGER
do
x := x_a.to_integer_32
result := result + s [(x |>> 24).bit_and (0xff)]
result := result.bit_shift_left (8)
result := result + s [(x |>> 16).bit_and (0xff)]
result := result.bit_shift_left (8)
result := result + s [(x |>> 8).bit_and (0xff)]
result := result.bit_shift_left (8)
result := result + s [x & 0xff]
end
rot_word (x: NATURAL_32): NATURAL_32
-- Rotate left 4 bits
do
result := x.bit_shift_right (24) | x.bit_shift_left (8)
end
key_schedule: SPECIAL [NATURAL_32]
-- FIPS W
spec_128_bit_schedule: BOOLEAN
-- Is `key_schedule' the one defined for the 128-bit spec key in FIPS-197
do
result := key_schedule.count = 44
result := result and key_schedule [0] = 0x2b7e1516 and key_schedule [1] = 0x28aed2a6 and key_schedule [2] = 0xabf71588 and key_schedule [3] = 0x09cf4f3c
result := result and key_schedule [4] = 0xa0fafe17 and key_schedule [5] = 0x88542cb1 and key_schedule [6] = 0x23a33939 and key_schedule [7] = 0x2a6c7605
result := result and key_schedule [8] = 0xf2c295f2 and key_schedule [9] = 0x7a96b943 and key_schedule [10] = 0x5935807a and key_schedule [11] = 0x7359f67f
result := result and key_schedule [12] = 0x3d80477d and key_schedule [13] = 0x4716fe3e and key_schedule [14] = 0x1e237e44 and key_schedule [15] = 0x6d7a883b
result := result and key_schedule [16] = 0xef44a541 and key_schedule [17] = 0xa8525b7f and key_schedule [18] = 0xb671253b and key_schedule [19] = 0xdb0bad00
result := result and key_schedule [20] = 0xd4d1c6f8 and key_schedule [21] = 0x7c839d87 and key_schedule [22] = 0xcaf2b8bc and key_schedule [23] = 0x11f915bc
result := result and key_schedule [24] = 0x6d88a37a and key_schedule [25] = 0x110b3efd and key_schedule [26] = 0xdbf98641 and key_schedule [27] = 0xca0093fd
result := result and key_schedule [28] = 0x4e54f70e and key_schedule [29] = 0x5f5fc9f3 and key_schedule [30] = 0x84a64fb2 and key_schedule [31] = 0x4ea6dc4f
result := result and key_schedule [32] = 0xead27321 and key_schedule [33] = 0xb58dbad2 and key_schedule [34] = 0x312bf560 and key_schedule [35] = 0x7f8d292f
result := result and key_schedule [36] = 0xac7766f3 and key_schedule [37] = 0x19fadc21 and key_schedule [38] = 0x28d12941 and key_schedule [39] = 0x575c006e
result := result and key_schedule [40] = 0xd014f9a8 and key_schedule [41] = 0xc9ee2589 and key_schedule [42] = 0xe13f0cc8 and key_schedule [43] = 0xb6630ca6
end
spec_196_bit_schedule: BOOLEAN
-- Is `key_schedule' the one defined for the 196-bit spec key in FIPS-197
do
result := key_schedule.count = 52
result := result and key_schedule [0] = 0x8e73b0f7 and key_schedule [1] = 0xda0e6452 and key_schedule [2] = 0xc810f32b and key_schedule [3] = 0x809079e5
result := result and key_schedule [4] = 0x62f8ead2 and key_schedule [5] = 0x522c6b7b and key_schedule [6] = 0xfe0c91f7 and key_schedule [7] = 0x2402f5a5
result := result and key_schedule [8] = 0xec12068e and key_schedule [9] = 0x6c827f6b and key_schedule [10] = 0x0e7a95b9 and key_schedule [11] = 0x5c56fec2
result := result and key_schedule [12] = 0x4db7b4bd and key_schedule [13] = 0x69b54118 and key_schedule [14] = 0x85a74796 and key_schedule [15] = 0xe92538fd
result := result and key_schedule [16] = 0xe75fad44 and key_schedule [17] = 0xbb095386 and key_schedule [18] = 0x485af057 and key_schedule [19] = 0x21efb14f
result := result and key_schedule [20] = 0xa448f6d9 and key_schedule [21] = 0x4d6dce24 and key_schedule [22] = 0xaa326360 and key_schedule [23] = 0x113b30e6
result := result and key_schedule [24] = 0xa25e7ed5 and key_schedule [25] = 0x83b1cf9a and key_schedule [26] = 0x27f93943 and key_schedule [27] = 0x6a94f767
result := result and key_schedule [28] = 0xc0a69407 and key_schedule [29] = 0xd19da4e1 and key_schedule [30] = 0xec1786eb and key_schedule [31] = 0x6fa64971
result := result and key_schedule [32] = 0x485f7032 and key_schedule [33] = 0x22cb8755 and key_schedule [34] = 0xe26d1352 and key_schedule [35] = 0x33f0b7b3
result := result and key_schedule [36] = 0x40beeb28 and key_schedule [37] = 0x2f18a259 and key_schedule [38] = 0x6747d26b and key_schedule [39] = 0x458c553e
result := result and key_schedule [40] = 0xa7e1466c and key_schedule [41] = 0x9411f1df and key_schedule [42] = 0x821f750a and key_schedule [43] = 0xad07d753
result := result and key_schedule [44] = 0xca400538 and key_schedule [45] = 0x8fcc5006 and key_schedule [46] = 0x282d166a and key_schedule [47] = 0xbc3ce7b5
result := result and key_schedule [48] = 0xe98ba06f and key_schedule [49] = 0x448c773c and key_schedule [50] = 0x8ecc7204 and key_schedule [51] = 0x01002202
end
spec_256_bit_schedule: BOOLEAN
-- Is `key_schedule' the one defined for the 256-bit spec key in FIPS-197
do
result := key_schedule.count = 60
result := result and key_schedule [0] = 0x603deb10 and key_schedule [1] = 0x15ca71be and key_schedule [2] = 0x2b73aef0 and key_schedule [3] = 0x857d7781
result := result and key_schedule [4] = 0x1f352c07 and key_schedule [5] = 0x3b6108d7 and key_schedule [6] = 0x2d9810a3 and key_schedule [7] = 0x0914dff4
result := result and key_schedule [8] = 0x9ba35411 and key_schedule [9] = 0x8e6925af and key_schedule [10] = 0xa51a8b5f and key_schedule [11] = 0x2067fcde
result := result and key_schedule [12] = 0xa8b09c1a and key_schedule [13] = 0x93d194cd and key_schedule [14] = 0xbe49846e and key_schedule [15] = 0xb75d5b9a
result := result and key_schedule [16] = 0xd59aecb8 and key_schedule [17] = 0x5bf3c917 and key_schedule [18] = 0xfee94248 and key_schedule [19] = 0xde8ebe96
result := result and key_schedule [20] = 0xb5a9328a and key_schedule [21] = 0x2678a647 and key_schedule [22] = 0x98312229 and key_schedule [23] = 0x2f6c79b3
result := result and key_schedule [24] = 0x812c81ad and key_schedule [25] = 0xdadf48ba and key_schedule [26] = 0x24360af2 and key_schedule [27] = 0xfab8b464
result := result and key_schedule [28] = 0x98c5bfc9 and key_schedule [29] = 0xbebd198e and key_schedule [30] = 0x268c3ba7 and key_schedule [31] = 0x09e04214
result := result and key_schedule [32] = 0x68007bac and key_schedule [33] = 0xb2df3316 and key_schedule [34] = 0x96e939e4 and key_schedule [35] = 0x6c518d80
result := result and key_schedule [36] = 0xc814e204 and key_schedule [37] = 0x76a9fb8a and key_schedule [38] = 0x5025c02d and key_schedule [39] = 0x59c58239
result := result and key_schedule [40] = 0xde136967 and key_schedule [41] = 0x6ccc5a71 and key_schedule [42] = 0xfa256395 and key_schedule [43] = 0x9674ee15
result := result and key_schedule [44] = 0x5886ca5d and key_schedule [45] = 0x2e2f31d7 and key_schedule [46] = 0x7e0af1fa and key_schedule [47] = 0x27cf73c3
result := result and key_schedule [48] = 0x749c47ab and key_schedule [49] = 0x18501dda and key_schedule [50] = 0xe2757e4f and key_schedule [51] = 0x7401905a
result := result and key_schedule [52] = 0xcafaaae3 and key_schedule [53] = 0xe4d59b34 and key_schedule [54] = 0x9adf6ace and key_schedule [55] = 0xbd10190d
result := result and key_schedule [56] = 0xfe4890d1 and key_schedule [57] = 0xe6188d0b and key_schedule [58] = 0x046df344 and key_schedule [59] = 0x706c631e
end
valid_spec_keys: BOOLEAN
local
key128: AES_KEY
key196: AES_KEY
key256: AES_KEY
do
create key128.make_spec_128
create key196.make_spec_196
create key256.make_spec_256
result := key128.spec_128_bit_schedule and key196.spec_196_bit_schedule and key256.spec_256_bit_schedule
end
valid_spec_keys_once: BOOLEAN
once
result := valid_spec_keys
end
feature -- Test if the key is a spec key
spec_128: BOOLEAN
do
result := key.count = 16
result := result and key [0] = 0x2b
result := result and key [1] = 0x7e
result := result and key [2] = 0x15
result := result and key [3] = 0x16
result := result and key [4] = 0x28
result := result and key [5] = 0xae
result := result and key [6] = 0xd2
result := result and key [7] = 0xa6
result := result and key [8] = 0xab
result := result and key [9] = 0xf7
result := result and key [10] = 0x15
result := result and key [11] = 0x88
result := result and key [12] = 0x09
result := result and key [13] = 0xcf
result := result and key [14] = 0x4f
result := result and key [15] = 0x3c
ensure
result implies spec_128_bit_schedule
end
spec_196: BOOLEAN
do
result := key.count = 24
result := result and key [0] = 0x8e
result := result and key [1] = 0x73
result := result and key [2] = 0xb0
result := result and key [3] = 0xf7
result := result and key [4] = 0xda
result := result and key [5] = 0x0e
result := result and key [6] = 0x64
result := result and key [7] = 0x52
result := result and key [8] = 0xc8
result := result and key [9] = 0x10
result := result and key [10] = 0xf3
result := result and key [11] = 0x2b
result := result and key [12] = 0x80
result := result and key [13] = 0x90
result := result and key [14] = 0x79
result := result and key [15] = 0xe5
result := result and key [16] = 0x62
result := result and key [17] = 0xf8
result := result and key [18] = 0xea
result := result and key [19] = 0xd2
result := result and key [20] = 0x52
result := result and key [21] = 0x2c
result := result and key [22] = 0x6b
result := result and key [23] = 0x7b
ensure
result implies spec_196_bit_schedule
end
spec_256: BOOLEAN
do
result := key.count = 32
result := result and key [0] = 0x60
result := result and key [1] = 0x3d
result := result and key [2] = 0xeb
result := result and key [3] = 0x10
result := result and key [4] = 0x15
result := result and key [5] = 0xca
result := result and key [6] = 0x71
result := result and key [7] = 0xbe
result := result and key [8] = 0x2b
result := result and key [9] = 0x73
result := result and key [10] = 0xae
result := result and key [11] = 0xf0
result := result and key [12] = 0x85
result := result and key [13] = 0x7d
result := result and key [14] = 0x77
result := result and key [15] = 0x81
result := result and key [16] = 0x1f
result := result and key [17] = 0x35
result := result and key [18] = 0x2c
result := result and key [19] = 0x07
result := result and key [20] = 0x3b
result := result and key [21] = 0x61
result := result and key [22] = 0x08
result := result and key [23] = 0xd7
result := result and key [24] = 0x2d
result := result and key [25] = 0x98
result := result and key [26] = 0x10
result := result and key [27] = 0xa3
result := result and key [28] = 0x09
result := result and key [29] = 0x14
result := result and key [30] = 0xdf
result := result and key [31] = 0xf4
ensure
result implies spec_256_bit_schedule
end
vector_128: BOOLEAN
do
result := key.count = 16
result := result and key [0] = 0x00
result := result and key [1] = 0x01
result := result and key [2] = 0x02
result := result and key [3] = 0x03
result := result and key [4] = 0x04
result := result and key [5] = 0x05
result := result and key [6] = 0x06
result := result and key [7] = 0x07
result := result and key [8] = 0x08
result := result and key [9] = 0x09
result := result and key [10] = 0x0a
result := result and key [11] = 0x0b
result := result and key [12] = 0x0c
result := result and key [13] = 0x0d
result := result and key [14] = 0x0e
result := result and key [15] = 0x0f
end
vector_196: BOOLEAN
do
result := key.count = 24
result := result and key [0] = 0x00
result := result and key [1] = 0x01
result := result and key [2] = 0x02
result := result and key [3] = 0x03
result := result and key [4] = 0x04
result := result and key [5] = 0x05
result := result and key [6] = 0x06
result := result and key [7] = 0x07
result := result and key [8] = 0x08
result := result and key [9] = 0x09
result := result and key [10] = 0x0a
result := result and key [11] = 0x0b
result := result and key [12] = 0x0c
result := result and key [13] = 0x0d
result := result and key [14] = 0x0e
result := result and key [15] = 0x0f
result := result and key [16] = 0x10
result := result and key [17] = 0x11
result := result and key [18] = 0x12
result := result and key [19] = 0x13
result := result and key [20] = 0x14
result := result and key [21] = 0x15
result := result and key [22] = 0x16
result := result and key [23] = 0x17
end
vector_256: BOOLEAN
do
result := key.count = 32
result := result and key [0] = 0x00
result := result and key [1] = 0x01
result := result and key [2] = 0x02
result := result and key [3] = 0x03
result := result and key [4] = 0x04
result := result and key [5] = 0x05
result := result and key [6] = 0x06
result := result and key [7] = 0x07
result := result and key [8] = 0x08
result := result and key [9] = 0x09
result := result and key [10] = 0x0a
result := result and key [11] = 0x0b
result := result and key [12] = 0x0c
result := result and key [13] = 0x0d
result := result and key [14] = 0x0e
result := result and key [15] = 0x0f
result := result and key [16] = 0x10
result := result and key [17] = 0x11
result := result and key [18] = 0x12
result := result and key [19] = 0x13
result := result and key [20] = 0x14
result := result and key [21] = 0x15
result := result and key [22] = 0x16
result := result and key [23] = 0x17
result := result and key [24] = 0x18
result := result and key [25] = 0x19
result := result and key [26] = 0x1a
result := result and key [27] = 0x1b
result := result and key [28] = 0x1c
result := result and key [29] = 0x1d
result := result and key [30] = 0x1e
result := result and key [31] = 0x1f
end
feature -- {DEBUG_OUTPUT}
debug_output: STRING
local
index: INTEGER_32
do
Result := "0x"
from
index := key.lower
until
index > key.upper
loop
Result.append (key [index].to_hex_string)
index := index + 1
variant
key.upper - index + 2
end
end
invariant
valid_spec_keys_once: valid_spec_keys_once
end

View File

@@ -0,0 +1,148 @@
note
description: "Summary description for {ARRAY_FACILITIES}."
author: "Colin LeMahieu"
date: "$Date: 2012-01-17 09:03:25 +0100 (mar., 17 janv. 2012) $"
revision: "$Revision: 88192 $"
quote: "The human race divides politically into those who want people to be controlled and those who have no such desire. - Robert A. Heinlein"
deferred class
ARRAY_FACILITIES
feature {ARRAY_FACILITIES} -- Array manipulation
array_xor (source_1: SPECIAL [NATURAL_8] source_1_offset: INTEGER_32 source_2: SPECIAL [NATURAL_8] source_2_offset: INTEGER_32 destination: SPECIAL [NATURAL_8] destination_offset: INTEGER_32 count: INTEGER_32)
require
source_1.valid_index (source_1_offset)
source_2.valid_index (source_2_offset)
destination.valid_index (destination_offset)
source_1.valid_index (source_1_offset + count - 1)
source_2.valid_index (source_2_offset + count - 1)
destination.valid_index (destination_offset + count - 1)
local
counter: INTEGER_32
do
from
counter := count
until
counter = 0
loop
destination [destination_offset + counter - 1] := source_1 [source_1_offset + counter - 1].bit_xor (source_2 [source_2_offset + counter - 1])
counter := counter - 1
variant
counter + 1
end
end
feature {ARRAY_FACILITIES} -- Big endian NATURAL_32
from_natural_32_be (source: NATURAL_32 target: SPECIAL [NATURAL_8] offset: INTEGER_32)
require
valid_start: target.valid_index (offset)
valid_end: target.valid_index (offset + 3)
do
target [offset] := (source |>> 24).to_natural_8
target [offset + 1] := (source |>> 16).to_natural_8
target [offset + 2] := (source |>> 8).to_natural_8
target [offset + 3] := source.to_natural_8
ensure
byte_0: target [offset] = (source |>> 24).to_natural_8
byte_1: target [offset + 1] = (source |>> 16).to_natural_8
byte_2: target [offset + 2] = (source |>> 8).to_natural_8
byte_3: target [offset + 3] = source.to_natural_8
end
as_natural_32_be (source: SPECIAL [NATURAL_8] offset: INTEGER_32): NATURAL_32
require
valid_start: source.valid_index (offset)
valid_end: source.valid_index (offset + 3)
do
Result := source [offset].to_natural_32 |<< 24
Result := Result | (source [offset + 1].to_natural_32 |<< 16)
Result := Result | (source [offset + 2].to_natural_32 |<< 8)
Result := Result | source [offset + 3].to_natural_32
ensure
byte_0: source [offset] = (Result |>> 24).to_natural_8
byte_1: source [offset + 1] = (Result |>> 16).to_natural_8
byte_2: source [offset + 2] = (Result |>> 8).to_natural_8
byte_3: source [offset + 3] = Result.to_natural_8
end
from_natural_32_le (source: NATURAL_32 target: SPECIAL [NATURAL_8] offset: INTEGER_32)
require
valid_start: target.valid_index (offset)
valid_end: target.valid_index (offset + 3)
do
target [offset] := source.to_natural_8
target [offset + 1] := (source |>> 8).to_natural_8
target [offset + 2] := (source |>> 16).to_natural_8
target [offset + 3] := (source |>> 24).to_natural_8
ensure
byte_0: target [offset] = source.to_natural_8
byte_1: target [offset + 1] = (source |>> 8).to_natural_8
byte_2: target [offset + 2] = (source |>> 16).to_natural_8
byte_3: target [offset + 3] = (source |>> 24).to_natural_8
end
as_natural_32_le (source: SPECIAL [NATURAL_8] offset: INTEGER_32): NATURAL_32
require
valid_start: source.valid_index (offset)
valid_end: source.valid_index (offset + 3)
do
Result := source [offset].to_natural_32
Result := Result | (source [offset + 1].to_natural_32 |<< 8)
Result := Result | (source [offset + 2].to_natural_32 |<< 16)
Result := Result | (source [offset + 3].to_natural_32 |<< 24)
ensure
byte_0: source [offset] = Result.to_natural_8
byte_1: source [offset + 1] = (Result |>> 8).to_natural_8
byte_2: source [offset + 2] = (Result |>> 16).to_natural_8
byte_3: source [offset + 3] = (Result |>> 24).to_natural_8
end
feature {ARRAY_FACILITIES} -- Big endian NATURAL_64
from_natural_64_be (source: NATURAL_64 target: SPECIAL [NATURAL_8] offset: INTEGER_32)
require
valid_start: target.valid_index (offset)
valid_end: target.valid_index (offset + 7)
do
target [offset] := (source |>> 56).to_natural_8
target [offset + 1] := (source |>> 48).to_natural_8
target [offset + 2] := (source |>> 40).to_natural_8
target [offset + 3] := (source |>> 32).to_natural_8
target [offset + 4] := (source |>> 24).to_natural_8
target [offset + 5] := (source |>> 16).to_natural_8
target [offset + 6] := (source |>> 8).to_natural_8
target [offset + 7] := source.to_natural_8
ensure
byte_0: target [offset] = (source |>> 56).to_natural_8
byte_1: target [offset + 1] = (source |>> 48).to_natural_8
byte_2: target [offset + 2] = (source |>> 40).to_natural_8
byte_3: target [offset + 3] = (source |>> 32).to_natural_8
byte_4: target [offset + 4] = (source |>> 24).to_natural_8
byte_5: target [offset + 5] = (source |>> 16).to_natural_8
byte_6: target [offset + 6] = (source |>> 8).to_natural_8
byte_7: target [offset + 7] = source.to_natural_8
end
as_natural_64_be (source: SPECIAL [NATURAL_8] offset: INTEGER_32): NATURAL_64
require
valid_start: source.valid_index (offset)
valid_end: source.valid_index (offset + 7)
do
Result := source [offset].to_natural_64 |<< 56
Result := Result | (source [offset + 1].to_natural_64 |<< 48)
Result := Result | (source [offset + 2].to_natural_64 |<< 40)
Result := Result | (source [offset + 3].to_natural_64 |<< 32)
Result := Result | (source [offset + 4].to_natural_64 |<< 24)
Result := Result | (source [offset + 5].to_natural_64 |<< 16)
Result := Result | (source [offset + 6].to_natural_64 |<< 8)
Result := Result | source [offset + 7].to_natural_64
ensure
byte_0: source [offset] = (Result |>> 56).to_natural_8
byte_1: source [offset + 1] = (Result |>> 48).to_natural_8
byte_2: source [offset + 2] = (Result |>> 40).to_natural_8
byte_3: source [offset + 3] = (Result |>> 32).to_natural_8
byte_4: source [offset + 4] = (Result |>> 24).to_natural_8
byte_5: source [offset + 5] = (Result |>> 16).to_natural_8
byte_6: source [offset + 6] = (Result |>> 8).to_natural_8
byte_7: source [offset + 7] = Result.to_natural_8
end
end

View File

@@ -0,0 +1,56 @@
note
description: "Facilities to use a stream of bytes as blocks of bytes"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Democracy must be something more than two wolves and a sheep voting on what to have for dinner. - James Bovard (1994)"
deferred class
BYTE_32_BIT_BLOCK_FACILITIES
feature
update_word (in: NATURAL_32)
do
update ((in |>> 24).to_natural_8)
update ((in |>> 16).to_natural_8)
update ((in |>> 8).to_natural_8)
update (in.to_natural_8)
ensure
buffer_offset = old buffer_offset
end
update (in: NATURAL_8)
do
buffer [buffer_offset] := in
buffer_offset := buffer_offset + 1
if
buffer_offset > buffer.upper
then
process_word (buffer, 0)
buffer_offset := 0
end
ensure
buffer_offset = (old buffer_offset + 1) \\ bytes
end
process_word (in: SPECIAL [NATURAL_8] offset: INTEGER_32)
require
valid_start: in.valid_index (offset)
valid_end: in.valid_index (offset + bytes - 1)
deferred
end
bytes: INTEGER
do
Result := 4
end
feature {NONE}
buffer: SPECIAL [NATURAL_8]
buffer_offset: INTEGER_32
invariant
buffer_lower: buffer.lower = 0
buffer_upper: buffer.upper = buffer.lower + bytes - 1
valid_buffer_offset: buffer.valid_index (buffer_offset)
end

View File

@@ -0,0 +1,19 @@
note
description: "Summary description for {BYTE_64_BIT_BLOCK_FACILITIES}."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "The evils of tyranny are rarely seen but by him who resists it. - John Hay (1872)"
deferred class
BYTE_64_BIT_BLOCK_FACILITIES
inherit
BYTE_32_BIT_BLOCK_FACILITIES
redefine
bytes
end
feature
bytes: INTEGER = 8
end

View File

@@ -0,0 +1,85 @@
note
description: "Summary description for {ARRAY_FACILITIES}."
author: "Colin LeMahieu"
date: "$Date: 2012-05-24 12:02:28 +0200 (jeu., 24 mai 2012) $"
revision: "$Revision: 88775 $"
quote: "The triumph of persuasion over force is the sign of a civilized society. - Mark Skousen"
deferred class
BYTE_FACILITIES
inherit
ARRAY_FACILITIES
feature -- Byte sinks
sink_special (in: SPECIAL [NATURAL_8] in_lower: INTEGER_32 in_upper: INTEGER_32)
require
in.valid_index (in_lower)
in.valid_index (in_upper)
local
index: INTEGER_32
do
from
index := in_upper
until
index < in_lower
loop
byte_sink (in [index])
index := index - 1
variant
index + 1
end
end
sink_special_lsb (in: SPECIAL [NATURAL_8]; in_lower: INTEGER_32; in_upper: INTEGER_32)
require
in.valid_index (in_lower)
in.valid_index (in_upper)
local
index: INTEGER_32
do
from
index := in_lower
until
index > in_upper
loop
byte_sink (in [index])
index := index + 1
variant
in_upper - index + 2
end
end
sink_character (in: CHARACTER_8)
do
byte_sink (in.code.to_natural_8)
end
sink_natural_32_be (in: NATURAL_32)
do
byte_sink ((in |>> 24).to_natural_8)
byte_sink ((in |>> 16).to_natural_8)
byte_sink ((in |>> 8).to_natural_8)
byte_sink (in.to_natural_8)
end
sink_string (in: STRING)
local
i: INTEGER
do
from
i := 1
until
i > in.count
loop
sink_character (in.item (i))
i := i + 1
variant
in.area.upper - i + 1
end
end
byte_sink (in: NATURAL_8)
deferred
end
end

View File

@@ -0,0 +1,36 @@
note
description: "Facilities for INTEGER_X constants"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "There is no worse tyranny than to force a man to pay for what he does not want merely because you think it would be good for him. - Robert Heinlein "
deferred class
CONSTANTS
feature
four: INTEGER_X
do
create result.make_from_integer(4)
end
three: INTEGER_X
do
create result.make_from_integer(3)
end
two: INTEGER_X
do
create result.make_from_integer(2)
end
one: INTEGER_X
do
create result.make_from_integer(1)
end
zero: INTEGER_X
do
create result.default_create
end
end

View File

@@ -0,0 +1,29 @@
note
description: "Summary description for {ARRAY_DER_SINK}."
author: ""
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
class
ARRAY_DER_SINK
inherit
DER_OCTET_SINK
create
make
feature
make (target_a: ARRAY [NATURAL_8])
do
target := target_a
end
sink (item: NATURAL_8)
do
target.force (item, target.upper + 1)
end
feature {NONE}
target: ARRAY [NATURAL_8]
end

View File

@@ -0,0 +1,44 @@
note
description: "Summary description for {ARRAY_DER_SOURCE}."
author: ""
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
class
ARRAY_DER_SOURCE
inherit
DER_OCTET_SOURCE
create
make
feature
make (source_a: ARRAY [NATURAL_8])
do
source := source_a
end
feature
has_item: BOOLEAN
do
result := source.valid_index (current_index)
end
item: NATURAL_8
do
result := source [current_index]
end
process
do
current_index := current_index + 1
end
feature {NONE}
current_index: INTEGER_32
source: ARRAY [NATURAL_8]
invariant
source.valid_index (current_index) or current_index = source.upper + 1
end

View File

@@ -0,0 +1,18 @@
note
description: "An object that is DER encodable"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "I think the terror most people are concerned with is the IRS. - Malcolm Forbes, when asked if he was afraid of terrorism"
deferred class
DER_ENCODABLE
inherit
DER_FACILITIES
feature
der_encode (target: DER_OCTET_SINK)
deferred
end
end

View File

@@ -0,0 +1,24 @@
note
description: "Summary description for {DER_ENCODING}."
author: ""
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
class
DER_ENCODING
inherit
DEVELOPER_EXCEPTION
create
make
feature
make (reason_a: STRING)
do
reason := reason_a
end
feature
reason: STRING
end

View File

@@ -0,0 +1,196 @@
note
description: "Summary description for {DER_FACILITIES}."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
deferred class
DER_FACILITIES
inherit
DER_UNIVERSAL_CLASS_TAG
feature
identifier_class (in: NATURAL_8): NATURAL_8
do
result := in & 0xc0
end
identifier_universal: NATURAL_8 = 0x00
identifier_application: NATURAL_8 = 0xa0
identifier_context_specific: NATURAL_8 = 0xb0
identifier_private: NATURAL_8 = 0xc0
identifier_constructed: NATURAL_8 = 0x20
identifier_primitive (in: NATURAL_8): BOOLEAN
do
result := (in & identifier_constructed) = 0
end
identifier_tag (in: NATURAL_8): NATURAL_8
do
result := in & 0x1f
end
identifier_high_number (in: NATURAL_8): BOOLEAN
do
result := identifier_tag (in) = 0x1f
end
identifier_last (in: NATURAL_8): BOOLEAN
do
result := (in & 0x80) = 0
end
encode_boolean (target: DER_OCTET_SINK in: BOOLEAN)
do
target.sink (boolean)
target.sink (0x01)
if
in
then
target.sink (0xff)
else
target.sink (0x00)
end
end
definite_length (target: DER_OCTET_SINK length: INTEGER_32)
require
length >= 0
do
if
length <= 127
then
definite_short_length (target, length)
else
definite_long_length (target, length)
end
end
definite_short_length (target: DER_OCTET_SINK length: INTEGER_32)
require
length >= 0
length <= 127
do
target.sink (length.to_natural_8)
end
definite_long_length (target: DER_OCTET_SINK length: INTEGER_32)
require
length >= 0
do
target.sink (0x84)
target.sink ((length |>> 24).to_natural_8)
target.sink ((length |>> 16).to_natural_8)
target.sink ((length |>> 8).to_natural_8)
target.sink ((length |>> 0).to_natural_8)
end
decode_length (source: DER_OCTET_SOURCE): INTEGER_X
do
if
source.item <= 127
then
result := decode_short_length (source)
else
result := decode_long_length (source)
end
end
decode_short_length (source: DER_OCTET_SOURCE): INTEGER_X
do
create result.make_from_integer (source.item.to_integer_32)
source.process
end
decode_long_length (source: DER_OCTET_SOURCE): INTEGER_X
local
length_count: INTEGER_32
current_byte: INTEGER_32
current_bit: INTEGER_32
do
length_count := (source.item & 0x7f).to_integer_32
if
length_count = 127
then
(create {DER_ENCODING}.make ("Unacceptable long form length encoding")).raise
end
create result.default_create
from
current_byte := length_count
until
current_byte = 0
loop
from
current_bit := 8
until
current_bit = 0
loop
if
source.item.bit_test (current_bit - 1)
then
Result := Result.set_bit_value (True, (current_byte - 1) * 8 + (current_bit - 1))
end
current_bit := current_bit - 1
variant
current_bit + 1
end
source.process
current_byte := current_byte - 1
variant
current_byte + 1
end
end
encode_integer (target: DER_OCTET_SINK in: INTEGER_X)
local
bytes: INTEGER_32
counter: INTEGER_32
do
if
in.is_negative
then
bytes := (in + in.one).bytes
else
bytes := in.bytes
end
target.sink (integer)
definite_length (target, bytes)
from
counter := bytes
until
counter = 0
loop
target.sink (byte_at (in, counter))
counter := counter - 1
variant
counter + 1
end
end
byte_at (in: INTEGER_X index: INTEGER_32): NATURAL_8
require
index >= 0
index <= in.bytes
local
current_bit: INTEGER_32
do
from
current_bit := 8
until
current_bit = 0
loop
result := result |<< 1
if
in.bit_test ((index - 1) * 8 + (current_bit - 1))
then
result := result | 0x01
end
current_bit := current_bit - 1
variant
current_bit + 1
end
end
end

View File

@@ -0,0 +1,15 @@
note
description: "A sink for DER octets"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "The illegal we do immediately. The unconstitutional takes a bit longer. - Henry Kissinger"
deferred class
DER_OCTET_SINK
feature
sink (item: NATURAL_8)
deferred
end
end

View File

@@ -0,0 +1,27 @@
note
description: "DER octet source"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Our forefathers made one mistake. What they should have fought for was representation without taxation. - Fletcher Knebel, historian"
deferred class
DER_OCTET_SOURCE
feature
has_item: BOOLEAN
deferred
end
item: NATURAL_8
require
has_item
deferred
end
process
require
has_item
deferred
end
end

View File

@@ -0,0 +1,31 @@
note
description: "ASN.1 universal class tag assignments X.680 8.4"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "The usual road to slavery is that first they take away your guns, then they take away your property, then last of all they tell you to shut up and say you are enjoying it. - James A. Donald"
deferred class
DER_UNIVERSAL_CLASS_TAG
feature
reserved: NATURAL_8 = 0x0
boolean: NATURAL_8 = 0x1
integer: NATURAL_8 = 0x2
bit_string: NATURAL_8 = 0x3
octet_string: NATURAL_8 = 0x4
null: NATURAL_8 = 0x5
object_identifier: NATURAL_8 = 0x6
object_descriptor: NATURAL_8 = 0x7
external_type: NATURAL_8 = 0x8
real: NATURAL_8 = 0x9
enumerated: NATURAL_8 = 0xa
embedded_pdv: NATURAL_8 = 0xb
utf8_string: NATURAL_8 = 0xc
relative_object_identifier: NATURAL_8 = 0xd
sequence: NATURAL_8 = 0x10
set: NATURAL_8 = 0x11
universal_time: NATURAL_8 = 0x17
generalized_time: NATURAL_8 = 0x18
end

View File

@@ -0,0 +1,283 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Blessed are the young, for they shall inherit the national debt. - Herbert Hoover"
class
MD5
inherit
ANY
redefine
is_equal
end
SHA_FUNCTIONS
rename
ch as f,
parity as h,
byte_sink as update
export
{MD5}
schedule,
buffer,
byte_count,
schedule_offset,
buffer_offset
undefine
is_equal
redefine
process_length,
process_word,
update_word
end
ROTATE_FACILITIES
undefine
is_equal
end
DEBUG_OUTPUT
undefine
is_equal
end
create
make,
make_copy
feature
make
do
create schedule.make_filled (0, 16)
create buffer.make_filled (0, 4)
reset
end
make_copy (other: like Current)
do
make
schedule.copy_data (other.schedule, other.schedule.lower, schedule.lower, schedule.count)
buffer.copy_data (other.buffer, other.buffer.lower, buffer.lower, buffer.count)
h1 := other.h1
h2 := other.h2
h3 := other.h3
h4 := other.h4
schedule_offset := other.schedule_offset
byte_count := other.byte_count
buffer_offset := other.buffer_offset
ensure
Current ~ other
end
feature
reset
do
byte_count := 0
schedule_offset := 0
buffer_offset := 0
h1 := 0x67452301
h2 := 0xefcdab89
h3 := 0x98badcfe
h4 := 0x10325476
ensure
byte_count = 0
schedule_offset = 0
buffer_offset = 0
h1 = 0x67452301
h2 = 0xefcdab89
h3 = 0x98badcfe
h4 = 0x10325476
end
do_final (output: SPECIAL [NATURAL_8] offset: INTEGER_32)
require
valid_start: output.valid_index (offset)
valid_end: output.valid_index (offset + 15)
do
finish
from_natural_32_le (h1, output, offset)
from_natural_32_le (h2, output, offset + 4)
from_natural_32_le (h3, output, offset + 8)
from_natural_32_le (h4, output, offset + 12)
reset
end
current_final (output: SPECIAL [NATURAL_8] offset: INTEGER_32)
require
valid_start: output.valid_index (offset)
valid_end: output.valid_index (offset + 15)
local
current_copy: like Current
do
create current_copy.make_copy (Current)
current_copy.do_final (output, offset)
end
current_out: STRING
local
output: SPECIAL [NATURAL_8]
index: INTEGER_32
do
Result := "0x"
create output.make_filled (0, 16)
current_final (output, 0)
from
index := 0
until
index = 16
loop
Result.append (output [index].to_hex_string)
index := index + 1
end
end
is_equal (other: like Current): BOOLEAN
do
Result :=
schedule.same_items (other.schedule, other.schedule.lower, schedule.lower, schedule.count) and
buffer.same_items (other.buffer, other.buffer.lower, buffer.lower, buffer.count) and
h1 = other.h1 and
h2 = other.h2 and
h3 = other.h3 and
h4 = other.h4 and
schedule_offset = other.schedule_offset and
byte_count = other.byte_count and
buffer_offset = other.buffer_offset
end
feature {NONE}
g (u: NATURAL_32 v: NATURAL_32 w: NATURAL_32): NATURAL_32
do
result := (u & w) | (v & w.bit_not)
end
k (u: NATURAL_32 v: NATURAL_32 w: NATURAL_32): NATURAL_32
do
result := v.bit_xor (u | w.bit_not)
end
process_length (length: NATURAL_64)
do
update_word (length.to_natural_32)
update_word ((length |>> 32).to_natural_32)
end
feature {NONE}
process_word (in: SPECIAL [NATURAL_8] offset: INTEGER_32)
do
schedule [schedule_offset] := as_natural_32_le (in, offset)
schedule_offset := schedule_offset + 1
if
schedule_offset = 16
then
schedule_offset := 0
process_block
end
end
update_word (in: NATURAL_32)
do
update (in.to_natural_8)
update ((in |>> 8).to_natural_8)
update ((in |>> 16).to_natural_8)
update ((in |>> 24).to_natural_8)
end
process_block
do
a := h1
b := h2
c := h3
d := h4
a := rotate_left_32 (a + f (b, c, d) + schedule [0] + 0xd76aa478, 7) + b
d := rotate_left_32 (d + f (a, b, c) + schedule [1] + 0xe8c7b756, 12) + a
c := rotate_left_32 (c + f (d, a, b) + schedule [2] + 0x242070db, 17) + d
b := rotate_left_32 (b + f (c, d, a) + schedule [3] + 0xc1bdceee, 22) + c
a := rotate_left_32 (a + f (b, c, d) + schedule [4] + 0xf57c0faf, 7) + b
d := rotate_left_32 (d + f (a, b, c) + schedule [5] + 0x4787c62a, 12) + a
c := rotate_left_32 (c + f (d, a, b) + schedule [6] + 0xa8304613, 17) + d
b := rotate_left_32 (b + f (c, d, a) + schedule [7] + 0xfd469501, 22) + c
a := rotate_left_32 (a + f (b, c, d) + schedule [8] + 0x698098d8, 7) + b
d := rotate_left_32 (d + f (a, b, c) + schedule [9] + 0x8b44f7af, 12) + a
c := rotate_left_32 (c + f (d, a, b) + schedule [10] + 0xffff5bb1, 17) + d
b := rotate_left_32 (b + f (c, d, a) + schedule [11] + 0x895cd7be, 22) + c
a := rotate_left_32 (a + f (b, c, d) + schedule [12] + 0x6b901122, 7) + b
d := rotate_left_32 (d + f (a, b, c) + schedule [13] + 0xfd987193, 12) + a
c := rotate_left_32 (c + f (d, a, b) + schedule [14] + 0xa679438e, 17) + d
b := rotate_left_32 (b + f (c, d, a) + schedule [15] + 0x49b40821, 22) + c
a := rotate_left_32 (a + g (b, c, d) + schedule [1] + 0xf61e2562, 5) + b
d := rotate_left_32 (d + g (a, b, c) + schedule [6] + 0xc040b340, 9) + a
c := rotate_left_32 (c + g (d, a, b) + schedule [11] + 0x265e5a51, 14) + d
b := rotate_left_32 (b + g (c, d, a) + schedule [0] + 0xe9b6c7aa, 20) + c
a := rotate_left_32 (a + g (b, c, d) + schedule [5] + 0xd62f105d, 5) + b
d := rotate_left_32 (d + g (a, b, c) + schedule [10] + 0x02441453, 9) + a
c := rotate_left_32 (c + g (d, a, b) + schedule [15] + 0xd8a1e681, 14) + d
b := rotate_left_32 (b + g (c, d, a) + schedule [4] + 0xe7d3fbc8, 20) + c
a := rotate_left_32 (a + g (b, c, d) + schedule [9] + 0x21e1cde6, 5) + b
d := rotate_left_32 (d + g (a, b, c) + schedule [14] + 0xc33707d6, 9) + a
c := rotate_left_32 (c + g (d, a, b) + schedule [3] + 0xf4d50d87, 14) + d
b := rotate_left_32 (b + g (c, d, a) + schedule [8] + 0x455a14ed, 20) + c
a := rotate_left_32 (a + g (b, c, d) + schedule [13] + 0xa9e3e905, 5) + b
d := rotate_left_32 (d + g (a, b, c) + schedule [2] + 0xfcefa3f8, 9) + a
c := rotate_left_32 (c + g (d, a, b) + schedule [7] + 0x676f02d9, 14) + d
b := rotate_left_32 (b + g (c, d, a) + schedule [12] + 0x8d2a4c8a, 20) + c
a := rotate_left_32 (a + h (b, c, d) + schedule [5] + 0xfffa3942, 4) + b
d := rotate_left_32 (d + h (a, b, c) + schedule [8] + 0x8771f681, 11) + a
c := rotate_left_32 (c + h (d, a, b) + schedule [11] + 0x6d9d6122, 16) + d
b := rotate_left_32 (b + h (c, d, a) + schedule [14] + 0xfde5380c, 23) + c
a := rotate_left_32 (a + h (b, c, d) + schedule [1] + 0xa4beea44, 4) + b
d := rotate_left_32 (d + h (a, b, c) + schedule [4] + 0x4bdecfa9, 11) + a
c := rotate_left_32 (c + h (d, a, b) + schedule [7] + 0xf6bb4b60, 16) + d
b := rotate_left_32 (b + h (c, d, a) + schedule [10] + 0xbebfbc70, 23) + c
a := rotate_left_32 (a + h (b, c, d) + schedule [13] + 0x289b7ec6, 4) + b
d := rotate_left_32 (d + h (a, b, c) + schedule [0] + 0xeaa127fa, 11) + a
c := rotate_left_32 (c + h (d, a, b) + schedule [3] + 0xd4ef3085, 16) + d
b := rotate_left_32 (b + h (c, d, a) + schedule [6] + 0x04881d05, 23) + c
a := rotate_left_32 (a + h (b, c, d) + schedule [9] + 0xd9d4d039, 4) + b
d := rotate_left_32 (d + h (a, b, c) + schedule [12] + 0xe6db99e5, 11) + a
c := rotate_left_32 (c + h (d, a, b) + schedule [15] + 0x1fa27cf8, 16) + d
b := rotate_left_32 (b + h (c, d, a) + schedule [2] + 0xc4ac5665, 23) + c
a := rotate_left_32 (a + k (b, c, d) + schedule [0] + 0xf4292244, 6) + b
d := rotate_left_32 (d + k (a, b, c) + schedule [7] + 0x432aff97, 10) + a
c := rotate_left_32 (c + k (d, a, b) + schedule [14] + 0xab9423a7, 15) + d
b := rotate_left_32 (b + k (c, d, a) + schedule [5] + 0xfc93a039, 21) + c
a := rotate_left_32 (a + k (b, c, d) + schedule [12] + 0x655b59c3, 6) + b
d := rotate_left_32 (d + k (a, b, c) + schedule [3] + 0x8f0ccc92, 10) + a
c := rotate_left_32 (c + k (d, a, b) + schedule [10] + 0xffeff47d, 15) + d
b := rotate_left_32 (b + k (c, d, a) + schedule [1] + 0x85845dd1, 21) + c
a := rotate_left_32 (a + k (b, c, d) + schedule [8] + 0x6fa87e4f, 6) + b
d := rotate_left_32 (d + k (a, b, c) + schedule [15] + 0xfe2ce6e0, 10) + a
c := rotate_left_32 (c + k (d, a, b) + schedule [6] + 0xa3014314, 15) + d
b := rotate_left_32 (b + k (c, d, a) + schedule [13] + 0x4e0811a1, 21) + c
a := rotate_left_32 (a + k (b, c, d) + schedule [4] + 0xf7537e82, 6) + b
d := rotate_left_32 (d + k (a, b, c) + schedule [11] + 0xbd3af235, 10) + a
c := rotate_left_32 (c + k (d, a, b) + schedule [2] + 0x2ad7d2bb, 15) + d
b := rotate_left_32 (b + k (c, d, a) + schedule [9] + 0xeb86d391, 21) + c
h1 := h1 + a
h2 := h2 + b
h3 := h3 + c
h4 := h4 + d
end
a: NATURAL_32
b: NATURAL_32
c: NATURAL_32
d: NATURAL_32
feature -- {DEBUG_OUTPUT}
debug_output: STRING
do
Result := current_out
end
feature {MD5}
h1: NATURAL_32
h2: NATURAL_32
h3: NATURAL_32
h4: NATURAL_32
end

View File

@@ -0,0 +1,346 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "There's never been a good government. - Emma Goldman"
class
SHA1
inherit
ANY
redefine
is_equal
end
DEBUG_OUTPUT
undefine
is_equal
end
SHA_FUNCTIONS
rename
byte_sink as update
export
{SHA1}
schedule,
buffer,
byte_count,
schedule_offset,
buffer_offset
undefine
is_equal
end
ROTATE_FACILITIES
undefine
is_equal
end
create
make,
make_copy
feature -- Creation
make
do
create schedule.make_filled (0, 80)
create buffer.make_filled (0, 4)
buffer_offset := 0
reset
end
make_copy (other: like Current)
do
make
schedule.copy_data (other.schedule, other.schedule.lower, schedule.lower, schedule.count)
buffer.copy_data (other.buffer, other.buffer.lower, buffer.lower, buffer.count)
byte_count := other.byte_count
buffer_offset := other.buffer_offset
h1 := other.h1
h2 := other.h2
h3 := other.h3
h4 := other.h4
h5 := other.h5
schedule_offset := other.schedule_offset
ensure
Current ~ other
end
feature -- Implementing DIGEST
reset
do
byte_count := 0
buffer_offset := 0
h1 := 0x67452301
h2 := 0xefcdab89
h3 := 0x98badcfe
h4 := 0x10325476
h5 := 0xc3d2e1f0
schedule_offset := 0
ensure
byte_count = 0
buffer_offset = 0
schedule_offset = 0
h1 = 0x67452301
h2 = 0xefcdab89
h3 = 0x98badcfe
h4 = 0x10325476
h5 = 0xc3d2e1f0
end
do_final (output: SPECIAL [NATURAL_8] offset: INTEGER)
require
valid_start: output.valid_index (offset)
valid_end: output.valid_index (offset + 19)
do
finish
unpack_word (h1, output, offset)
unpack_word (h2, output, offset + 4)
unpack_word (h3, output, offset + 8)
unpack_word (h4, output, offset + 12)
unpack_word (h5, output, offset + 16)
reset
end
current_final (output: SPECIAL [NATURAL_8] offset: INTEGER_32)
require
valid_start: output.valid_index (offset)
valid_end: output.valid_index (offset + 19)
local
current_copy: like Current
do
current_copy := Current.deep_twin
current_copy.do_final (output, offset)
end
current_out: STRING
local
output: SPECIAL [NATURAL_8]
index: INTEGER_32
do
Result := "0x"
create output.make_filled (0, 20)
current_final (output, 0)
from
index := 0
until
index = 20
loop
Result.append (output [index].to_hex_string)
index := index + 1
end
end
is_equal (other: like Current): BOOLEAN
do
Result :=
schedule.same_items (other.schedule, other.schedule.lower, schedule.lower, schedule.count) and
buffer.same_items (other.buffer, other.buffer.lower, buffer.lower, buffer.count) and
h1 = other.h1 and
h2 = other.h2 and
h3 = other.h3 and
h4 = other.h4 and
h5 = other.h5 and
schedule_offset = other.schedule_offset and
byte_count = other.byte_count and
buffer_offset = other.buffer_offset
end
feature {NONE}
unpack_word (word: NATURAL_32 output: SPECIAL [NATURAL_8] offset: INTEGER)
require
valid_start: output.valid_index (offset)
valid_end: output.valid_index (offset + 3)
do
output [offset] := (word |>> 24).to_natural_8
output [offset + 1] := (word |>> 16).to_natural_8
output [offset + 2] := (word |>> 8).to_natural_8
output [offset + 3] := word.to_natural_8
end
A: NATURAL_32
B: NATURAL_32
C: NATURAL_32
D: NATURAL_32
E: NATURAL_32
process_block
do
expand_word_block
A := H1
B := H2
C := H3
D := H4
E := H5
do_round_1
do_round_2
do_round_3
do_round_4
h1 := h1 + a
h2 := h2 + b
h3 := h3 + c
h4 := h4 + d
h5 := h5 + e
end
do_round_4
local
j: INTEGER
idx: INTEGER
do
idx := 60
from
j := 0
until
j = 4
loop
e := e + rotate_left_32 (a, 5) + parity (b, c, d) + schedule [idx] + k4
idx := idx + 1
b := rotate_left_32 (b, 30)
d := d + rotate_left_32 (e, 5) + parity (a, b, c) + schedule [idx] + k4
idx := idx + 1
a := rotate_left_32 (a, 30)
c := c + rotate_left_32 (d, 5) + parity (e, a, b) + schedule [idx] + k4
idx := idx + 1
e := rotate_left_32 (e, 30)
b := b + rotate_left_32 (c, 5) + parity (d, e, a) + schedule [idx] + k4
idx := idx + 1
d := rotate_left_32 (d, 30)
a := a + rotate_left_32 (b, 5) + parity (c, d, e) + schedule [idx] + k4
idx := idx + 1
c := rotate_left_32 (c, 30)
j := j + 1
end
end
do_round_3
local
j: INTEGER
idx: INTEGER
do
idx := 40
from
j := 0
until
j = 4
loop
E := E + rotate_left_32 (a, 5) + maj (B, C, D) + schedule [idx] + k3
idx := idx + 1
B := rotate_left_32 (b, 30)
D := d + rotate_left_32 (e, 5) + maj (a, b, c) + schedule [idx] + k3
idx := idx + 1
A := rotate_left_32 (a, 30)
C := C + rotate_left_32 (d, 5) + maj (e, a, b) + schedule [idx] + k3
idx := idx + 1
e := rotate_left_32 (e, 30)
b := b + rotate_left_32 (c, 5) + maj (d, e, a) + schedule [idx] + k3
idx := idx + 1
d := rotate_left_32 (d, 30)
a := a + rotate_left_32 (b, 5) + maj (c, d, e) + schedule [idx] + k3
idx := idx + 1
c := rotate_left_32 (c, 30)
j := j + 1
end
end
do_round_2
local
j: INTEGER
idx: INTEGER
do
idx := 20
from
j := 0
until
j = 4
loop
E := E + rotate_left_32 (a, 5) + parity(B, C, D) + schedule [idx] + k2
idx := idx + 1
B := rotate_left_32 (b, 30)
D := d + rotate_left_32 (e, 5) + parity(a, b, c) + schedule [idx] + k2
idx := idx + 1
A := rotate_left_32 (a, 30)
C := C + rotate_left_32 (d, 5) + parity(e, a, b) + schedule [idx] + k2
idx := idx + 1
e := rotate_left_32 (e, 30)
b := b + rotate_left_32 (c, 5) + parity(d, e, a) + schedule [idx] + k2
idx := idx + 1
d := rotate_left_32 (d, 30)
a := a + rotate_left_32 (b, 5) + parity(c, d, e) + schedule [idx] + k2
idx := idx + 1
c := rotate_left_32 (c, 30)
j := j + 1
end
end
do_round_1
local
j: INTEGER
idx: INTEGER
do
idx := 0
from
j := 0
until
j = 4
loop
E := E + rotate_left_32 (a, 5) + ch (B, C, D) + schedule [idx] + k1
idx := idx + 1
B := rotate_left_32 (b, 30)
D := d + rotate_left_32 (e, 5) + ch (a, b, c) + schedule [idx] + k1
idx := idx + 1
A := rotate_left_32 (a, 30)
C := C + rotate_left_32 (d, 5) + ch (e, a, b) + schedule [idx] + k1
idx := idx + 1
e := rotate_left_32 (e, 30)
b := b + rotate_left_32 (c, 5) + ch (d, e, a) + schedule [idx] + k1
idx := idx + 1
d := rotate_left_32 (d, 30)
a := a + rotate_left_32 (b, 5) + ch (c, d, e) + schedule [idx] + k1
idx := idx + 1
c := rotate_left_32 (c, 30)
j := j + 1
end
end
expand_word_block
-- Expand 16 word block in to 80 word block
local
i: INTEGER
temp: NATURAL_32
do
from
i := 16
until
i = 80
loop
temp := schedule [i - 3].bit_xor (schedule [i - 8]).bit_xor (schedule [i - 14]).bit_xor (schedule [i - 16])
schedule [i] := rotate_left_32 (temp, 1)
i := i + 1
end
end
feature {SHA1}
H1: NATURAL_32
H2: NATURAL_32
H3: NATURAL_32
H4: NATURAL_32
H5: NATURAL_32
feature {NONE}
k1: NATURAL_32 = 0x5a827999
k2: NATURAL_32 = 0x6ed9eba1
k3: NATURAL_32 = 0x8f1bbcdc
k4: NATURAL_32 = 0xca62c1d6
feature {DEBUG_OUTPUT} -- {DEBUG_OUTPUT}
debug_output: STRING
do
result := current_out
end
invariant
schedule_lower:schedule.lower = 0
schedule_upper:schedule.upper = 79
end

View File

@@ -0,0 +1,363 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Useless laws weaken the necessary laws. - Montesquieu"
class
SHA256
inherit
ANY
redefine
is_equal
end
DEBUG_OUTPUT
undefine
is_equal
end
SHA_FUNCTIONS
rename
byte_sink as update
export
{SHA256}
schedule,
buffer,
schedule_offset,
byte_count,
buffer_offset
undefine
is_equal
end
ROTATE_FACILITIES
undefine
is_equal
end
create
make,
make_copy
feature
make
do
create schedule.make_filled (0, 64)
create buffer.make_filled (0, 4)
reset
end
make_copy (other: like Current)
do
make
schedule.copy_data (other.schedule, other.schedule.lower, schedule.lower, schedule.count)
buffer.copy_data (other.buffer, other.buffer.lower, buffer.lower, buffer.count)
byte_count := other.byte_count
buffer_offset := other.buffer_offset
h1 := other.h1
h2 := other.h2
h3 := other.h3
h4 := other.h4
h5 := other.h5
h6 := other.h6
h7 := other.h7
h8 := other.h8
schedule_offset := other.schedule_offset
ensure
Current ~ other
end
feature
do_final (output: SPECIAL[NATURAL_8] out_off: INTEGER)
require
valid_offset: out_off >= 0
out_big_enough: out.count - out_off >= 32
do
finish
from_natural_32_be (h1, output, out_off)
from_natural_32_be (h2, output, out_off + 4)
from_natural_32_be (h3, output, out_off + 8)
from_natural_32_be (h4, output, out_off + 12)
from_natural_32_be (h5, output, out_off + 16)
from_natural_32_be (h6, output, out_off + 20)
from_natural_32_be (h7, output, out_off + 24)
from_natural_32_be (h8, output, out_off + 28)
reset
end
reset
do
buffer_offset := 0
h1 := 0x6a09e667
h2 := 0xbb67ae85
h3 := 0x3c6ef372
h4 := 0xa54ff53a
h5 := 0x510e527f
h6 := 0x9b05688c
h7 := 0x1f83d9ab
h8 := 0x5be0cd19
schedule_offset := 0
schedule.fill_with ({NATURAL_32} 0, 0, schedule.upper)
ensure
buffer_reset: buffer_offset = 0
schedule_reset: schedule_offset = 0
end
current_final (output: SPECIAL [NATURAL_8] offset: INTEGER_32)
require
valid_start: output.valid_index (offset)
valid_end: output.valid_index (offset + 31)
local
current_copy: like Current
do
current_copy := Current.deep_twin
current_copy.do_final (output, offset)
end
current_out: STRING
local
output: SPECIAL [NATURAL_8]
index: INTEGER_32
do
Result := "0x"
create output.make_filled (0, 32)
current_final (output, 0)
from
index := 0
until
index = 32
loop
Result.append (output [index].to_hex_string)
index := index + 1
end
end
is_equal (other: like Current): BOOLEAN
do
Result :=
schedule.same_items (other.schedule, other.schedule.lower, schedule.lower, schedule.count) and
buffer.same_items (other.buffer, other.buffer.lower, buffer.lower, buffer.count) and
h1 = other.h1 and
h2 = other.h2 and
h3 = other.h3 and
h4 = other.h4 and
h5 = other.h5 and
h6 = other.h6 and
h7 = other.h7 and
h8 = other.h8 and
schedule_offset = other.schedule_offset and
byte_count = other.byte_count and
buffer_offset = other.buffer_offset
end
feature{NONE}
process_block
local
a: NATURAL_32
b: NATURAL_32
c: NATURAL_32
d: NATURAL_32
e: NATURAL_32
f: NATURAL_32
g: NATURAL_32
h: NATURAL_32
t: INTEGER
i: INTEGER
do
expand_blocks
a := h1
b := h2
c := h3
d := h4
e := h5
f := h6
g := h7
h := h8
t := 0
from
i := 0
until
i = 8
loop
h := h + sigma1 (e) + ch (e, f, g) + k [t] + schedule [t]
t := t + 1
d := d + h
h := h + sigma0 (a) + maj (a, b, c)
g := g + sigma1 (d) + ch (d, e, f) + k [t] + schedule [t]
t := t + 1
c := c + g
g := g + sigma0 (h) + maj (h, a, b)
f := f + sigma1 (c) + ch (c, d, e) + k [t] + schedule [t]
t := t + 1
b := b + f
f := f + sigma0 (g) + maj (g, h, a)
e := e + sigma1 (b) + ch (b, c, d) + k [t] + schedule [t]
t := t + 1
a := a + e
e := e + sigma0 (f) + maj (f, g, h)
d := d + sigma1 (a) + ch (a, b, c) + k [t] + schedule [t]
t := t + 1
h := h + d
d := d + sigma0 (e) + maj (e, f, g)
c := c + sigma1 (h) + ch (h, a, b) + k [t] + schedule [t]
t := t + 1
g := g + c
c := c + sigma0 (d) + maj (d, e, f)
b := b + sigma1 (g) + ch (g, h, a) + k [t] + schedule [t]
t := t + 1
f := f + b
b := b + sigma0 (c) + maj (c, d, e)
a := a + sigma1 (f) + ch (f, g, h) + k [t] + schedule [t]
t := t + 1
e := e + a
a := a + sigma0 (b) + maj (b, c, d)
i := i + 1
end
h1 := h1 + a
h2 := h2 + b
h3 := h3 + c
h4 := h4 + d
h5 := h5 + e
h6 := h6 + f
h7 := h7 + g
h8 := h8 + h
end
sigma0 (x1: NATURAL_32): NATURAL_32
do
result := rotate_right_32 (x1, 2)
result := result.bit_xor (rotate_right_32 (x1, 13))
result := result.bit_xor (rotate_right_32 (x1, 22))
end
sigma1 (x1: NATURAL_32): NATURAL_32
do
result := rotate_right_32 (x1, 6)
result := result.bit_xor (rotate_right_32 (x1, 11))
result := result.bit_xor (rotate_right_32 (x1, 25))
end
lsigma0(x1: NATURAL_32): NATURAL_32
do
result := (rotate_right_32 (x1, 7)).bit_xor (rotate_right_32 (x1, 18)).bit_xor (x1 |>> 3)
end
lsigma1(x1: NATURAL_32): NATURAL_32
do
result := (rotate_right_32 (x1, 17)).bit_xor (rotate_right_32 (x1, 19)).bit_xor (x1 |>> 10)
end
expand_blocks
local
t: INTEGER
do
from
t := 16
until
t = 64
loop
schedule[t] := lsigma1 (schedule [t - 2]) + schedule [t - 7] + lsigma0 (schedule [t - 15]) + schedule [t - 16]
t := t + 1
end
end
k: SPECIAL[NATURAL_32]
once
create result.make_filled (0, 64)
result[0] := 0x428a2f98
result[1] := 0x71374491
result[2] := 0xb5c0fbcf
result[3] := 0xe9b5dba5
result[4] := 0x3956c25b
result[5] := 0x59f111f1
result[6] := 0x923f82a4
result[7] := 0xab1c5ed5
result[8] := 0xd807aa98
result[9] := 0x12835b01
result[10] := 0x243185be
result[11] := 0x550c7dc3
result[12] := 0x72be5d74
result[13] := 0x80deb1fe
result[14] := 0x9bdc06a7
result[15] := 0xc19bf174
result[16] := 0xe49b69c1
result[17] := 0xefbe4786
result[18] := 0x0fc19dc6
result[19] := 0x240ca1cc
result[20] := 0x2de92c6f
result[21] := 0x4a7484aa
result[22] := 0x5cb0a9dc
result[23] := 0x76f988da
result[24] := 0x983e5152
result[25] := 0xa831c66d
result[26] := 0xb00327c8
result[27] := 0xbf597fc7
result[28] := 0xc6e00bf3
result[29] := 0xd5a79147
result[30] := 0x06ca6351
result[31] := 0x14292967
result[32] := 0x27b70a85
result[33] := 0x2e1b2138
result[34] := 0x4d2c6dfc
result[35] := 0x53380d13
result[36] := 0x650a7354
result[37] := 0x766a0abb
result[38] := 0x81c2c92e
result[39] := 0x92722c85
result[40] := 0xa2bfe8a1
result[41] := 0xa81a664b
result[42] := 0xc24b8b70
result[43] := 0xc76c51a3
result[44] := 0xd192e819
result[45] := 0xd6990624
result[46] := 0xf40e3585
result[47] := 0x106aa070
result[48] := 0x19a4c116
result[49] := 0x1e376c08
result[50] := 0x2748774c
result[51] := 0x34b0bcb5
result[52] := 0x391c0cb3
result[53] := 0x4ed8aa4a
result[54] := 0x5b9cca4f
result[55] := 0x682e6ff3
result[56] := 0x748f82ee
result[57] := 0x78a5636f
result[58] := 0x84c87814
result[59] := 0x8cc70208
result[60] := 0x90befffa
result[61] := 0xa4506ceb
result[62] := 0xbef9a3f7
result[63] := 0xc67178f2
end
feature {SHA256}
h1: NATURAL_32
h2: NATURAL_32
h3: NATURAL_32
h4: NATURAL_32
h5: NATURAL_32
h6: NATURAL_32
h7: NATURAL_32
h8: NATURAL_32
feature {NONE} -- {DEBUG_OUTPUT}
debug_output: STRING
do
result := current_out
end
invariant
buffer_size: buffer.count = 4
valid_buffer_offset: buffer.valid_index (buffer_offset)
schedule_size: schedule.count = 64
valid_schedule_offset: schedule.valid_index (schedule_offset)
end

View File

@@ -0,0 +1,118 @@
note
description: "Summary description for {SHA_FUNCTIONS}."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "The war for freedom will never really be won because the price of our freedom is constant vigilance over ourselves and over our Government. - Eleanor Roosevelt"
deferred class
SHA_FUNCTIONS
inherit
BYTE_FACILITIES
BYTE_32_BIT_BLOCK_FACILITIES
redefine
update
end
feature {NONE}
ch (u: NATURAL_32 v: NATURAL_32 w: NATURAL_32): NATURAL_32
do
result := (u & v) | (u.bit_not & w)
end
maj (u: NATURAL_32 v: NATURAL_32 w: NATURAL_32): NATURAL_32
do
result := (u & v) | (u & w) | (v & w)
end
parity (u: NATURAL_32 v: NATURAL_32 w: NATURAL_32): NATURAL_32
do
result := u.bit_xor (v).bit_xor (w)
end
feature {NONE} -- Padding facilities
pad
local
pad_bytes: INTEGER_32
do
update (0b1000_0000)
from
pad_bytes := (56 - (byte_count \\ 64)).to_integer_32
if
pad_bytes < 0
then
pad_bytes := pad_bytes + 64
end
until
pad_bytes = 0
loop
update (0)
pad_bytes := pad_bytes - 1
end
end
byte_count: NATURAL_64
bit_count: NATURAL_64
do
result := byte_count |<< 3
end
update (in: NATURAL_8)
do
precursor (in)
byte_count := byte_count + 1
ensure then
byte_count = old byte_count + 1
end
feature {NONE} -- Length processing facilities
process_length (length: NATURAL_64)
require
schedule_start: schedule_offset = 14
empty_buffer: buffer_offset = 0
do
update_word ((length |>> 32).to_natural_32)
update_word (length.to_natural_32)
ensure
empty_buffer: buffer_offset = 0
schedule_end: schedule_offset = 0
end
process_word (in: SPECIAL [NATURAL_8] offset: INTEGER_32)
do
schedule [schedule_offset] := as_natural_32_be (in, offset)
schedule_offset := schedule_offset + 1
if
schedule_offset = 16
then
schedule_offset := 0
process_block
end
end
process_block
deferred
end
finish
local
length: NATURAL_64
do
length := bit_count
pad
process_length (length)
end
feature {NONE}
schedule: SPECIAL [NATURAL_32]
schedule_offset: INTEGER_32
invariant
valid_schedule_offset: schedule.valid_index (schedule_offset)
valid_schedule_offset_lower: schedule_offset >= 0
valid_schedule_offset_upper: schedule_offset <= 15
valid_schedule_lower: schedule.valid_index (0)
valid_schedule_upper: schedule.valid_index (15)
end

View File

@@ -0,0 +1,118 @@
note
description: "Summary description for {SHA_FUNCTIONS}."
author: "Colin LeMahieu"
date: "$Date$"
revision: "$Revision$"
quote: "The war for freedom will never really be won because the price of our freedom is constant vigilance over ourselves and over our Government. - Eleanor Roosevelt"
deferred class
SHA_FUNCTIONS
inherit
BYTE_FACILITIES
BYTE_32_BIT_BLOCK_FACILITIES
redefine
update
end
feature {NONE}
ch (u: NATURAL_32; v: NATURAL_32; w: NATURAL_32): NATURAL_32 is
do
result := (u & v) | (u.bit_not & w)
end
maj (u: NATURAL_32; v: NATURAL_32; w: NATURAL_32): NATURAL_32 is
do
result := (u & v) | (u & w) | (v & w)
end
parity (u: NATURAL_32; v: NATURAL_32; w: NATURAL_32): NATURAL_32 is
do
result := u.bit_xor (v).bit_xor (w)
end
feature {NONE} -- Padding facilities
pad
local
pad_bytes: INTEGER_32
do
update (0b1000_0000)
from
pad_bytes := (56 - (byte_count \\ 64)).to_integer_32
if
pad_bytes < 0
then
pad_bytes := pad_bytes + 64
end
until
pad_bytes = 0
loop
update (0)
pad_bytes := pad_bytes - 1
end
end
byte_count: NATURAL_64
bit_count: NATURAL_64
do
result := byte_count |<< 3
end
update (in: NATURAL_8)
do
precursor (in)
byte_count := byte_count + 1
ensure then
byte_count = old byte_count + 1
end
feature {NONE} -- Length processing facilities
process_length (length: NATURAL_64)
require
schedule_start: schedule_offset = 14
empty_buffer: buffer_offset = 0
do
update_word ((length |>> 32).to_natural_32)
update_word (length.to_natural_32)
ensure
empty_buffer: buffer_offset = 0
schedule_end: schedule_offset = 0
end
process_word (in: SPECIAL [NATURAL_8]; offset: INTEGER_32)
do
schedule [schedule_offset] := as_natural_32_be (in, offset)
schedule_offset := schedule_offset + 1
if
schedule_offset = 16
then
schedule_offset := 0
process_block
end
end
process_block
deferred
end
finish is
local
length: NATURAL_64
do
length := bit_count
pad
process_length (length)
end
feature {NONE}
schedule: SPECIAL [NATURAL_32]
schedule_offset: INTEGER_32
invariant
valid_schedule_offset: schedule.valid_index (schedule_offset)
valid_schedule_offset_lower: schedule_offset >= 0
valid_schedule_offset_upper: schedule_offset <= 15
valid_schedule_lower: schedule.valid_index (0)
valid_schedule_upper: schedule.valid_index (15)
end

View File

@@ -0,0 +1,14 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "The natural progress of things is for liberty to yield and government to gain ground. - Thomas Jefferson"
deferred class
EC_CONSTANTS
inherit
CONSTANTS
end

View File

@@ -0,0 +1,23 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "None are more hopelessly enslaved than those who falsely believe they are free. - Goethe"
deferred class
EC_CURVE
inherit
DEBUG_OUTPUT
feature
a: EC_FIELD_ELEMENT
b: EC_FIELD_ELEMENT
feature {DEBUG_OUTPUT} -- {DEBUG_OUTPUT}
debug_output: STRING
do
result := "a: " + a.debug_output + "%Nb: " + b.debug_output
end
end

View File

@@ -0,0 +1,419 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "When the government's boot is on your throat, whether it is a left boot or a right boot is of no consequence. - Gary Lloyd"
class
EC_CURVE_F2M
inherit
EC_CURVE
redefine
is_equal,
a,
b
end
STANDARD_CURVES
undefine
is_equal
end
F2M_REPRESENTATIONS
undefine
is_equal
end
create
make,
make_sec_t113r1,
make_sec_t113r2,
make_sec_t131r1,
make_sec_t131r2,
make_sec_t163k1,
make_sec_t163r1,
make_sec_t163r2,
make_sec_t193r1,
make_sec_t193r2,
make_sec_t233k1,
make_sec_t233r1,
make_sec_t239k1,
make_sec_t283k1,
make_sec_t283r1,
make_sec_t409k1,
make_sec_t409r1,
make_sec_t571k1,
make_sec_t571r1,
make_k163,
make_k233,
make_k283,
make_k409,
make_k571,
make_b163,
make_b233,
make_b283,
make_b409,
make_b571
feature -- SEC curves
make_sec_t113r1
do
m := sec_t113r1_m
k1 := sec_t113r1_k1
k2 := sec_t113r1_k2
k3 := sec_t113r1_k3
n := sec_t113r1_r
create a.make (sec_t113r1_a)
create b.make (sec_t113r1_b)
end
make_sec_t113r2
do
m := sec_t113r2_m
k1 := sec_t113r2_k1
k2 := sec_t113r2_k2
k3 := sec_t113r2_k3
n := sec_t113r2_r
create a.make (sec_t113r2_a)
create b.make (sec_t113r2_b)
end
make_sec_t131r1
do
m := sec_t131r1_m
k1 := sec_t131r1_k1
k2 := sec_t131r1_k2
k3 := sec_t131r1_k3
n := sec_t131r1_r
create a.make (sec_t131r1_a)
create b.make (sec_t131r1_b)
end
make_sec_t131r2
do
m := sec_t131r2_m
k1 := sec_t131r2_k1
k2 := sec_t131r2_k2
k3 := sec_t131r2_k3
n := sec_t131r2_r
create a.make (sec_t131r2_a)
create b.make (sec_t131r2_b)
end
make_sec_t163k1
do
m := sec_t163k1_m
k1 := sec_t163k1_k1
k2 := sec_t163k1_k2
k3 := sec_t163k1_k3
n := sec_t163k1_r
create a.make (sec_t163k1_a)
create b.make (sec_t163k1_b)
end
make_sec_t163r1
do
m := sec_t163r1_m
k1 := sec_t163r1_k1
k2 := sec_t163r1_k2
k3 := sec_t163r1_k3
n := sec_t163r1_r
create a.make (sec_t163r1_a)
create b.make (sec_t163r1_b)
end
make_sec_t163r2
do
m := sec_t163r2_m
k1 := sec_t163r2_k1
k2 := sec_t163r2_k2
k3 := sec_t163r2_k3
n := sec_t163r1_r
create a.make (sec_t163r2_a)
create b.make (sec_t163r2_b)
end
make_sec_t193r1
do
m := sec_t193r1_m
k1 := sec_t193r1_k1
k2 := sec_t193r1_k2
k3 := sec_t193r1_k3
n := sec_t193r1_r
create a.make (sec_t193r1_a)
create b.make (sec_t193r1_b)
end
make_sec_t193r2
do
m := sec_t193r2_m
k1 := sec_t193r2_k1
k2 := sec_t193r2_k2
k3 := sec_t193r2_k3
n := sec_t193r2_r
create a.make (sec_t193r2_a)
create b.make (sec_t193r2_b)
end
make_sec_t233k1
do
m := sec_t233k1_m
k1 := sec_t233k1_k1
k2 := sec_t233k1_k2
k3 := sec_t233k1_k3
n := sec_t233k1_r
create a.make (sec_t233k1_a)
create b.make (sec_t233k1_b)
end
make_sec_t233r1
do
m := sec_t233r1_m
k1 := sec_t233r1_k1
k2 := sec_t233r1_k2
k3 := sec_t233r1_k3
n := sec_t233r1_r
create a.make (sec_t233r1_a)
create b.make (sec_t233r1_b)
end
make_sec_t239k1
do
m := sec_t239k1_m
k1 := sec_t239k1_k1
k2 := sec_t239k1_k2
k3 := sec_t239k1_k3
n := sec_t239k1_r
create a.make (sec_t239k1_a)
create b.make (sec_t239k1_b)
end
make_sec_t283k1
do
m := sec_t283k1_m
k1 := sec_t283k1_k1
k2 := sec_t283k1_k2
k3 := sec_t283k1_k3
n := sec_t283k1_r
create a.make (sec_t283k1_a)
create b.make (sec_t283k1_b)
end
make_sec_t283r1
do
m := sec_t283r1_m
k1 := sec_t283r1_k1
k2 := sec_t283r1_k2
k3 := sec_t283r1_k3
n := sec_t283r1_r
create a.make (sec_t283r1_a)
create b.make (sec_t283r1_b)
end
make_sec_t409k1
do
m := sec_t409k1_m
k1 := sec_t409k1_k1
k2 := sec_t409k1_k2
k3 := sec_t409k1_k3
n := sec_t409k1_r
create a.make (sec_t409k1_a)
create b.make (sec_t409k1_b)
end
make_sec_t409r1
do
m := sec_t409r1_m
k1 := sec_t409r1_k1
k2 := sec_t409r1_k2
k3 := sec_t409r1_k3
n := sec_t409r1_r
create a.make (sec_t409r1_a)
create b.make (sec_t409r1_b)
end
make_sec_t571k1
do
m := sec_t571k1_m
k1 := sec_t571k1_k1
k2 := sec_t571k1_k2
k3 := sec_t571k1_k3
n := sec_t571k1_r
create a.make (sec_t571k1_a)
create b.make (sec_t571k1_b)
end
make_sec_t571r1
do
m := sec_t571r1_m
k1 := sec_t571r1_k1
k2 := sec_t571r1_k2
k3 := sec_t571r1_k3
n := sec_t571r1_r
create a.make (sec_t571r1_a)
create b.make (sec_t571r1_b)
end
feature -- FIPS curves
make_k163
do
m := k163_m
k1 := k163_k1
k2 := k163_k2
k3 := k163_k3
n := k163_r
create a.make (k163_a)
create b.make (k163_b)
end
make_k233
do
m := k233_m
k1 := k233_k1
k2 := k233_k2
k3 := k233_k3
n := k233_r
create a.make (k233_a)
create b.make (k233_b)
end
make_k283
do
m := k283_m
k1 := k283_k1
k2 := k283_k2
k3 := k283_k3
n := k283_r
create a.make (k283_a)
create b.make (k283_b)
end
make_k409
do
m := k409_m
k1 := k409_k1
k2 := k409_k2
k3 := k409_k3
n := k409_r
create a.make (k409_a)
create b.make (k409_b)
end
make_k571
do
m := k571_m
k1 := k571_k1
k2 := k571_k2
k3 := k571_k3
n := k571_r
create a.make (k571_a)
create b.make (k571_b)
end
make_b163
do
m := b163_m
k1 := b163_k1
k2 := b163_k2
k3 := b163_k3
n := b163_r
create a.make (b163_a)
create b.make (b163_b)
end
make_b233
do
m := b233_m
k1 := b233_k1
k2 := b233_k2
k3 := b233_k3
n := b233_r
create a.make (b233_a)
create b.make (b233_b)
end
make_b283
do
m := b283_m
k1 := b283_k1
k2 := b283_k2
k3 := b283_k3
n := b283_r
create a.make (b283_a)
create b.make (b283_b)
end
make_b409
do
m := b409_m
k1 := b409_k1
k2 := b409_k2
k3 := b409_k3
n := b409_r
create a.make (b409_a)
create b.make (b409_b)
end
make_b571
do
m := b571_m
k1 := b571_k1
k2 := b571_k2
k3 := b571_k3
n := b571_r
create a.make (b571_a)
create b.make (b571_b)
end
make (m_new: INTEGER_32 k1_new: INTEGER_32 k2_new: INTEGER_32 k3_new: INTEGER_32 a_a: EC_FIELD_ELEMENT_F2M b_a: EC_FIELD_ELEMENT_F2M n_a: INTEGER_X)
require
K1_greater_Than_zero: k1_new > 0
k2_and_k3_equal_zero: (k2_new = 0) implies (k3_new = 0)
k2_greater_than_k1: (k2_new /= 0) implies (k2_new > k1_new)
k3_greater_than_k2: (k3_new /= 0) implies (k3_new > k2_new)
do
m := m_new
k1 := k1_new
k2 := k2_new
k3 := k3_new
a := a_a
b := b_a
n := n_a
end
feature -- F2M components
m: INTEGER_32
n: INTEGER_X
k1: INTEGER_32
k2: INTEGER_32
k3: INTEGER_32
feature
representation: INTEGER
do
if
k2 = 0
then
result := TPB
else
result := PPB
end
end
is_equal (other: like current): BOOLEAN
do
Result := (m = other.m) and (k1 = other.k1) and (k2 = other.k2) and (k3 = other.k3) and a.x ~ other.a.x and b.x ~ other.b.x
end
a: EC_FIELD_ELEMENT_F2M
b: EC_FIELD_ELEMENT_F2M
invariant
-- k2_smaller: k2 = 0 implies k2 < k3
-- k2_zero: k2 = 0 implies k2 /= 0
K1_greater_Than_zero: k1 > 0
k2_and_k3_equal_zero: (k2 = 0) implies (k3 = 0)
k2_greater_than_k1: (k2 /= 0) implies (k2 > k1)
k3_greater_than_k2: (k3 /= 0) implies (k3 > k2)
end

View File

@@ -0,0 +1,230 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Every decent man is ashamed of the government he lives under. - H.L. Mencken"
class
EC_CURVE_FP
inherit
EC_CONSTANTS
undefine
is_equal
end
EC_CURVE
redefine
is_equal,
a,
b
end
STANDARD_CURVES
undefine
is_equal
end
create
make_q_a_b,
make_sec_p112r1,
make_sec_p112r2,
make_sec_p128r1,
make_sec_p128r2,
make_sec_p160k1,
make_sec_p160r1,
make_sec_p160r2,
make_sec_p192k1,
make_sec_p192r1,
make_sec_p224k1,
make_sec_p224r1,
make_sec_p256k1,
make_sec_p256r1,
make_sec_p384r1,
make_sec_p521r1,
make_p192,
make_p224,
make_p256,
make_p384,
make_p521
create {EC_FIELD_ELEMENT_FP}
make_zero
feature {EC_FIELD_ELEMENT_FP}
make_zero
do
create q.default_create
create a.make_zero
create b.make_zero
end
feature
make_q_a_b (q_new: INTEGER_X a_a: INTEGER_X b_a: INTEGER_X)
-- Create an EC over FP from q, a, and b
do
q := q_new
create a.make_p_x (a_a)
create b.make_p_x (b_a)
end
feature -- SEC curves
make_sec_p112r1
do
q := sec_p112r1_p
create a.make_p_x (sec_p112r1_a)
create b.make_p_x (sec_p112r1_b)
end
make_sec_p112r2
do
q := sec_p112r2_p
create a.make_p_x (sec_p112r2_a)
create b.make_p_x (sec_p112r2_b)
end
make_sec_p128r1
do
q := sec_p128r1_p
create a.make_p_x (sec_p128r1_a)
create b.make_p_x (sec_p128r1_b)
end
make_sec_p128r2
do
q := sec_p128r2_p
create a.make_p_x (sec_p128r2_a)
create b.make_p_x (sec_p128r2_b)
end
make_sec_p160k1
do
q := sec_p160k1_p
create a.make_p_x (sec_p160k1_a)
create b.make_p_x (sec_p160k1_b)
end
make_sec_p160r1
do
q := sec_p160r1_p
create a.make_p_x (sec_p160r1_a)
create b.make_p_x (sec_p160r1_b)
end
make_sec_p160r2
do
q := sec_p160r2_p
create a.make_p_x (sec_p160r2_a)
create b.make_p_x (sec_p160r2_b)
end
make_sec_p192k1
do
q := sec_p192k1_p
create a.make_p_x (sec_p192k1_a)
create b.make_p_x (sec_p192k1_b)
end
make_sec_p192r1
do
q := sec_p192r1_p
create a.make_p_x (sec_p192r1_a)
create b.make_p_x (sec_p192r1_b)
end
make_sec_p224k1
do
q := sec_p224k1_p
create a.make_p_x (sec_p224k1_a)
create b.make_p_x (sec_p224k1_b)
end
make_sec_p224r1
do
q := sec_p224r1_p
create a.make_p_x (sec_p224r1_a)
create b.make_p_x (sec_p224r1_b)
end
make_sec_p256k1
do
q := sec_p256k1_p
create a.make_p_x (sec_p256k1_a)
create b.make_p_x (sec_p256k1_b)
end
make_sec_p256r1
do
q := sec_p256r1_p
create a.make_p_x (sec_p256r1_a)
create b.make_p_x (sec_p256r1_b)
end
make_sec_p384r1
do
q := sec_p384r1_p
create a.make_p_x (sec_p384r1_a)
create b.make_p_x (sec_p384r1_b)
end
make_sec_p521r1
do
q := sec_p521r1_p
create a.make_p_x (sec_p521r1_a)
create b.make_p_x (sec_p521r1_b)
end
feature
make_p192
do
q := p192_p
create a.make_p_x (p192_a)
create b.make_p_x (p192_b)
end
make_p224
do
q := p224_p
create a.make_p_x (p224_a)
create b.make_p_x (p224_b)
end
make_p256
do
q := p256_p
create a.make_p_x (p256_a)
create b.make_p_x (p256_b)
end
make_p384
do
q := p384_p
create a.make_p_x (p384_a)
create b.make_p_x (p384_b)
end
make_p521
do
q := p521_p
create a.make_p_x (p521_a)
create b.make_p_x (p521_b)
end
feature
q: INTEGER_X
a: EC_FIELD_ELEMENT_FP
attribute
create result.make_zero
end
b: EC_FIELD_ELEMENT_FP
attribute
create result.make_zero
end
is_equal (other: like current): BOOLEAN
-- Is current equal to other
do
result := q ~ other.q and a.x ~ other.a.x and b.x ~ other.b.x
ensure then
q /~ other.q implies not result
end
end

View File

@@ -0,0 +1,44 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "The urge to save humanity is almost always a false front for the urge to rule. - H.L. Mencken"
deferred class
EC_DOMAIN_PARAMETERS
inherit
EC_CONSTANTS
DEBUG_OUTPUT
feature
curve: EC_CURVE
g: EC_POINT
n: INTEGER_X
h: INTEGER_X
make_curve_g_n (curve_new: like curve g_new: like g n_new: INTEGER_X)
-- Construct this domain with no seed and h= 1
do
curve := curve_new
g := g_new
n := n_new
h := ONE
end
make_curve_g_n_h (curve_new: like curve g_new: like g n_new: INTEGER_X h_new: INTEGER_X)
-- construct this domain with no seed
do
curve := curve_new
g := g_new
n := n_new
h := h_new
end
feature {DEBUG_OUTPUT} -- {DEBUG_OUTPUT}
debug_output: STRING
do
result := "Curve: " + curve.debug_output + "%Ng: " + g.debug_output + "%Nn: " + n.out_hex + "%Nh: " + h.out_hex
end
end

View File

@@ -0,0 +1,44 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date$"
revision: "$Revision$"
quote: "The urge to save humanity is almost always a false front for the urge to rule. - H.L. Mencken"
deferred class
EC_DOMAIN_PARAMETERS
inherit
EC_CONSTANTS
DEBUG_OUTPUT
feature
curve: EC_CURVE
g: EC_POINT
n: INTEGER_X
h: INTEGER_X
make_curve_g_n (curve_new: like curve; g_new: like g; n_new: INTEGER_X) is
-- Construct this domain with no seed and h= 1
do
curve := curve_new
g := g_new
n := n_new
h := ONE
end
make_curve_g_n_h (curve_new: like curve g_new: like g n_new: INTEGER_X h_new: INTEGER_X) is
-- construct this domain with no seed
do
curve := curve_new
g := g_new
n := n_new
h := h_new
end
feature {DEBUG_OUTPUT} -- {DEBUG_OUTPUT}
debug_output: STRING
do
result := "Curve: " + curve.debug_output + "%Ng: " + g.debug_output + "%Nn: " + n.out_hex + "%Nh: " + h.out_hex
end
end

View File

@@ -0,0 +1,279 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Where morality is present, laws are unnecessary. Without morality, laws are unenforceable. - Anonymous"
class
EC_DOMAIN_PARAMETERS_F2M
inherit
EC_DOMAIN_PARAMETERS
redefine
curve,
g
end
STANDARD_CURVES
create
make_curve_g_n,
make_curve_g_n_h,
make_sec_t113r1,
make_sec_t113r2,
make_sec_t131r1,
make_sec_t131r2,
make_sec_t163k1,
make_sec_t163r1,
make_sec_t163r2,
make_sec_t193r1,
make_sec_t193r2,
make_sec_t233k1,
make_sec_t233r1,
make_sec_t239k1,
make_sec_t283k1,
make_sec_t283r1,
make_sec_t409k1,
make_sec_t409r1,
make_sec_t571k1,
make_sec_t571r1,
make_k163,
make_k233,
make_k283,
make_k409,
make_k571,
make_b163,
make_b233,
make_b283,
make_b409,
make_b571
feature --SEC recommended polynomial curves
make_sec_t113r1
do
create curve.make_sec_t113r1
create g.make_sec_t113r1
n := sec_t113r1_r
h := sec_t113r1_h
end
make_sec_t113r2
do
create curve.make_sec_t113r2
create g.make_sec_t113r2
n := sec_t113r2_r
h := sec_t113r2_h
end
make_sec_t131r1
do
create curve.make_sec_t131r1
create g.make_sec_t131r1
n := sec_t131r1_r
h := sec_t131r1_h
end
make_sec_t131r2
do
create curve.make_sec_t131r2
create g.make_sec_t131r2
n := sec_t131r2_r
h := sec_t131r2_h
end
make_sec_t163k1
do
create curve.make_sec_t163k1
create g.make_sec_t163k1
n := sec_t163k1_r
h := sec_t163k1_h
end
make_sec_t163r1
do
create curve.make_sec_t163r1
create g.make_sec_t163r1
n := sec_t163r1_r
h := sec_t163r1_h
end
make_sec_t163r2
do
create curve.make_sec_t163r2
create g.make_sec_t163r2
n := sec_t163r2_r
h := sec_t163r2_h
end
make_sec_t193r1
do
create curve.make_sec_t193r1
create g.make_sec_t193r1
n := sec_t193r1_r
h := sec_t193r1_h
end
make_sec_t193r2
do
create curve.make_sec_t193r2
create g.make_sec_t193r2
n := sec_t193r2_r
h := sec_t193r2_h
end
make_sec_t233k1
do
create curve.make_sec_t233k1
create g.make_sec_t233k1
n := sec_t233k1_r
h := sec_t233k1_h
end
make_sec_t233r1
do
create curve.make_sec_t233r1
create g.make_sec_t233r1
n := sec_t233r1_r
h := sec_t233r1_h
end
make_sec_t239k1
do
create curve.make_sec_t239k1
create g.make_sec_t239k1
n := sec_t239k1_r
h := sec_t239k1_h
end
make_sec_t283k1
do
create curve.make_sec_t283k1
create g.make_sec_t283k1
n := sec_t283k1_r
h := sec_t283k1_h
end
make_sec_t283r1
do
create curve.make_sec_t283r1
create g.make_sec_t283r1
n := sec_t283r1_r
h := sec_t283r1_h
end
make_sec_t409k1
do
create curve.make_sec_t409k1
create g.make_sec_t409k1
n := sec_t409k1_r
h := sec_t409k1_h
end
make_sec_t409r1
do
create curve.make_sec_t409r1
create g.make_sec_t409r1
n := sec_t409r1_r
h := sec_t409r1_h
end
make_sec_t571k1
do
create curve.make_sec_t571k1
create g.make_sec_t571k1
n := sec_t571k1_r
h := sec_t571k1_h
end
make_sec_t571r1
do
create curve.make_sec_t571r1
create g.make_sec_t571r1
n := sec_t571r1_r
h := sec_t571r1_h
end
feature --FIPS curves
make_k163
do
create curve.make_k163
create g.make_k163
n := k163_r
h := k163_h
end
make_k233
do
create curve.make_k233
create g.make_k233
n := k233_r
h := k233_h
end
make_k283
do
create curve.make_k283
create g.make_k283
n := k283_r
h := k283_h
end
make_k409
do
create curve.make_k409
create g.make_k409
n := k409_r
h := k409_h
end
make_k571
do
create curve.make_k571
create g.make_k571
n := k571_r
h := k571_h
end
make_b163
do
create curve.make_b163
create g.make_b163
n := b163_r
h := b163_h
end
make_b233
do
create curve.make_b233
create g.make_b233
n := b233_r
h := b233_h
end
make_b283
do
create curve.make_b283
create g.make_b283
n := b283_r
h := b283_h
end
make_b409
do
create curve.make_b409
create g.make_b409
n := b409_r
h := b409_h
end
make_b571
do
create curve.make_b571
create g.make_b571
n := b571_r
h := b571_h
end
curve: EC_CURVE_F2M
g: EC_POINT_F2M
end

View File

@@ -0,0 +1,214 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Extremism in the defense of liberty is no vice. Moderation in the pursuit of justice is no virtue. - Barry Goldwater (1964)"
class
EC_DOMAIN_PARAMETERS_FP
inherit
EC_DOMAIN_PARAMETERS
redefine
curve,
g
end
STANDARD_CURVES
export
{NONE}
all
undefine
default_create
end
create
make_curve_g_n,
make_curve_g_n_h,
make_sec_p112r1,
make_sec_p112r2,
make_sec_p128r1,
make_sec_p128r2,
make_sec_p160k1,
make_sec_p160r1,
make_sec_p160r2,
make_sec_p192k1,
make_sec_p192r1,
make_sec_p224k1,
make_sec_p224r1,
make_sec_p256k1,
make_sec_p256r1,
make_sec_p384r1,
make_sec_p521r1,
make_p192,
make_p224,
make_p256,
make_p384,
make_p521
feature
make_sec_p112r1
do
create curve.make_sec_p112r1
create g.make_sec_p112r1
n := sec_p112r1_r
h := sec_p112r1_h
end
make_sec_p112r2
do
create curve.make_sec_p112r2
create g.make_sec_p112r2
n := sec_p112r2_r
h := sec_p112r2_h
end
make_sec_p128r1
do
create curve.make_sec_p128r1
create g.make_sec_p128r1
n := sec_p128r1_r
h := sec_p128r1_h
end
make_sec_p128r2
do
create curve.make_sec_p128r2
create g.make_sec_p128r2
n := sec_p128r2_r
h := sec_p128r2_h
end
make_sec_p160k1
do
create curve.make_sec_p160k1
create g.make_sec_p160k1
n := sec_p160k1_r
h := sec_p160k1_h
end
make_sec_p160r1
do
create curve.make_sec_p160r1
create g.make_sec_p160r1
n := sec_p160r1_r
h := sec_p160r1_h
end
make_sec_p160r2
do
create curve.make_sec_p160r2
create g.make_sec_p160r2
n := sec_p160r2_r
h := sec_p160r2_h
end
make_sec_p192k1
do
create curve.make_sec_p192k1
create g.make_sec_p192k1
n := sec_p192k1_r
h := sec_p192k1_h
end
make_sec_p192r1
do
create curve.make_sec_p192r1
create g.make_sec_p192r1
n := sec_p192r1_r
h := sec_p192r1_h
end
make_sec_p224k1
do
create curve.make_sec_p224k1
create g.make_sec_p224k1
n := sec_p224k1_r
h := sec_p224k1_h
end
make_sec_p224r1
do
create curve.make_sec_p224r1
create g.make_sec_p224r1
n := sec_p224r1_r
h := sec_p224r1_h
end
make_sec_p256k1
do
create curve.make_sec_p256k1
create g.make_sec_p256k1
n := sec_p256k1_r
h := sec_p256k1_h
end
make_sec_p256r1
do
create curve.make_sec_p256r1
create g.make_sec_p256r1
n := sec_p256r1_r
h := sec_p256r1_h
end
make_sec_p384r1
do
create curve.make_sec_p384r1
create g.make_sec_p384r1
n := sec_p384r1_r
h := sec_p384r1_h
end
make_sec_p521r1
do
create curve.make_sec_p521r1
create g.make_sec_p521r1
n := sec_p521r1_r
h := sec_p521r1_h
end
make_p192
do
create curve.make_p192
create g.make_p192
n := p192_r
h := p192_h
end
make_p224
do
create curve.make_p224
create g.make_p224
n := p224_r
h := p224_h
end
make_p256
do
create curve.make_p256
create g.make_p256
n := p256_r
h := p256_h
end
make_p384
do
create curve.make_p384
create g.make_p384
n := p384_r
h := p384_h
end
make_p521
do
create curve.make_p521
create g.make_p521
n := p521_r
h := p521_h
end
feature
curve: EC_CURVE_FP
g: EC_POINT_FP
end

View File

@@ -0,0 +1,134 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Liberty is not a means to a political end. It is itself the highest political end. - Lord Acton"
deferred class
EC_FIELD_ELEMENT
inherit
ANY
redefine
is_equal,
copy
end
DEBUG_OUTPUT
undefine
is_equal,
copy
end
EC_CONSTANTS
undefine
is_equal,
copy
end
feature
x: INTEGER_X
copy (other: like Current)
do
x.copy (other.x)
end
encoded_field_size (curve: EC_CURVE): INTEGER_32
-- Return the size of this ecfieldelement in bytes when encoded according to x9.62
-- This was added as a deviation from the lcrypto origional and seems to be cleaner
-- Replacement for class X9IntegerConverter
deferred
end
plus (other: like Current; curve: EC_CURVE)
deferred
end
plus_value (other: like Current; curve: EC_CURVE): like Current
do
Result := deep_twin
Result.plus (other, curve)
end
minus (other: like Current; curve: EC_CURVE)
deferred
end
minus_value (other: like Current; curve: EC_CURVE): like Current
do
Result := deep_twin
Result.minus (other, curve)
end
product (other: like Current; curve: EC_CURVE)
deferred
end
product_value (other: like Current; curve: EC_CURVE): like Current
do
Result := deep_twin
Result.product (other, curve)
end
quotient (other: like Current; curve: EC_CURVE)
deferred
end
quotient_value (other: like Current; curve: EC_CURVE): like Current
do
Result := deep_twin
Result.quotient (other, curve)
end
opposite (curve: EC_CURVE)
deferred
end
opposite_value (curve: EC_CURVE): like Current
do
Result := deep_twin
Result.opposite (curve)
end
square (curve: EC_CURVE)
deferred
end
square_value (curve: EC_CURVE): like Current
do
Result := deep_twin
Result.square (curve)
end
inverse (curve: EC_CURVE)
deferred
end
inverse_value (curve: EC_CURVE): like Current
do
Result := deep_twin
Result.inverse (curve)
end
sqrt (curve: EC_CURVE): like Current
-- Return a new ECFIELDELEMENT that is sqrt(current)
deferred
end
is_equal (other: like Current): BOOLEAN
do
Result := x ~ other.x
ensure then
Result = (x ~ other.x)
end
feature {DEBUG_OUTPUT} -- {DEBUG_OUTPUT}
debug_output: STRING
do
result := x.out_hex
end
invariant
negative: not x.is_negative
end

View File

@@ -0,0 +1,518 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "The power to tax is the power to destroy. - John Marshall"
class
EC_FIELD_ELEMENT_F2M
inherit
EC_FIELD_ELEMENT
redefine
is_equal,
plus_value,
minus_value,
product_value,
quotient_value,
opposite_value,
square_value,
inverse_value
end
F2M_REPRESENTATIONS
undefine
is_equal,
copy
end
INTEGER_X_FACILITIES
undefine
is_equal,
copy
end
LIMB_MANIPULATION
undefine
is_equal,
copy
end
SPECIAL_UTILITY
undefine
is_equal,
copy
end
create
make
convert
make ({INTEGER_X})
feature {NONE}
make (x_a: INTEGER_X)
require
non_negative_x: not x_a.is_negative
do
x := x_a
end
feature -- Field element components
multZModF (a: INTEGER_X; m_limb_position: INTEGER m_bit_position: INTEGER k1_limb_position: INTEGER k1_bit_position: INTEGER)
require
a.is_positive
local
special: SPECIAL [NATURAL_32]
limb: NATURAL_32
do
a.bit_shift_left (1)
special := a.item
limb := special [m_limb_position]
if
limb.bit_test (m_bit_position)
then
special [m_limb_position] := limb.set_bit (False, m_bit_position)
special [0] := special [0].bit_xor (1)
special [k1_limb_position] := special [k1_limb_position].bit_xor ((1).to_natural_32 |<< k1_bit_position)
end
ensure
a.is_positive
end
multZModF_p (a: INTEGER_X; m_limb_position: INTEGER m_bit_position: INTEGER k1_limb_position: INTEGER k1_bit_position: INTEGER k2_limb_position: INTEGER k2_bit_position: INTEGER k3_limb_position: INTEGER k3_bit_position: INTEGER)
require
a.is_positive
local
special: SPECIAL [NATURAL_32]
limb: NATURAL_32
do
a.bit_shift_left (1)
special := a.item
limb := special [m_limb_position]
if
limb.bit_test (m_bit_position)
then
special [m_limb_position] := limb.set_bit (False, m_bit_position)
special [0] := special [0].bit_xor (1)
special [k1_limb_position] := special [k1_limb_position].bit_xor ((1).to_natural_32 |<< k1_bit_position)
special [k2_limb_position] := special [k2_limb_position].bit_xor ((1).to_natural_32 |<< k2_bit_position)
special [k3_limb_position] := special [k3_limb_position].bit_xor ((1).to_natural_32 |<< k3_bit_position)
end
ensure
a.is_positive
end
feature
encoded_field_size (curve: EC_CURVE_F2M): INTEGER_32
--
obsolete
"Needs implementation"
do
end
feature -- Implementing features of ECFIELDELEMENT
plus_value (other: like Current; curve: EC_CURVE_F2M): EC_FIELD_ELEMENT_F2M
do
Result := Precursor (other, curve)
end
plus (other: like Current; curve: EC_CURVE_F2M)
do
x.bit_xor (other.x)
end
minus_value (other: like Current; curve: EC_CURVE_F2M): EC_FIELD_ELEMENT_F2M
do
Result := Precursor (other, curve)
end
minus (other: like Current; curve: EC_CURVE_F2M)
do
plus (other, curve)
end
product_value (b: like Current; curve: EC_CURVE_F2M): EC_FIELD_ELEMENT_F2M
do
Result := Precursor (b, curve)
end
product (b: like Current; curve: EC_CURVE_F2M)
local
m: INTEGER
m_bit_position: INTEGER
m_limb_position: INTEGER
k1_bit_position: INTEGER
k1_limb_position: INTEGER
k2_bit_position: INTEGER
k2_limb_position: INTEGER
k3_bit_position: INTEGER
k3_limb_position: INTEGER
bz: INTEGER_X
cz: INTEGER_X
special: SPECIAL [NATURAL_32]
limb: NATURAL_32
limb_position: INTEGER
bit_position: INTEGER
new_bit_position: INTEGER
do
m := curve.m
m_limb_position := bit_index_to_limb_index (m)
m_bit_position := m \\ limb_bits
k1_limb_position := bit_index_to_limb_index (curve.k1)
k1_bit_position := curve.k1 \\ limb_bits
k2_limb_position := bit_index_to_limb_index (curve.k2)
k2_bit_position := curve.k2 \\ limb_bits
k3_limb_position := bit_index_to_limb_index (curve.k3)
k3_bit_position := curve.k3 \\ limb_bits
create bz.make_bits (m + m)
bz.copy (b.x)
limb_position := 0
bit_position := 0
special := x.item
x.resize (bits_to_limbs (m))
limb := special [limb_position]
create cz.make_bits (m + m)
from
bit_position := 0
until
limb_position * limb_bits + bit_position >= m
loop
if
limb.bit_test (bit_position)
then
cz.bit_xor (bz)
end
new_bit_position := (bit_position + 1) \\ limb_bits
if new_bit_position < bit_position then
limb_position := limb_position + 1
limb := special [limb_position]
end
bit_position := new_bit_position
if curve.representation = PPB then
multZmodF_p (bz, m_limb_position, m_bit_position, k1_limb_position, k1_bit_position, k2_limb_position, k2_bit_position, k3_limb_position, k3_bit_position)
else
multZmodF (bz, m_limb_position, m_bit_position, k1_limb_position, k1_bit_position)
end
end
x := cz
end
quotient_value (other: like Current; curve: EC_CURVE_F2M): EC_FIELD_ELEMENT_F2M
do
Result := Precursor (other, curve)
end
quotient (other: like Current; curve: EC_CURVE_F2M)
local
bInv: like Current
do
bInv := other.inverse_value (curve)
product (bInv, curve)
end
opposite_value (curve: EC_CURVE_F2M): EC_FIELD_ELEMENT_F2M
do
Result := Precursor (curve)
end
opposite (curve: EC_CURVE_F2M)
do
do_nothing
end
square_value (curve: EC_CURVE_F2M): EC_FIELD_ELEMENT_F2M
do
Result := Precursor (curve)
end
square (curve: EC_CURVE_F2M)
local
i: INTEGER_32
limb_position: INTEGER
bit_position: INTEGER
new_bit_position: INTEGER
square_limb_position: INTEGER
square_bit_position: INTEGER
limb: NATURAL_32
square_limb: NATURAL_32
special: SPECIAL [NATURAL_32]
do
from
i := curve.m
x.resize (bits_to_limbs (i + i))
special := x.item
limb_position := bit_index_to_limb_index (i)
bit_position := i \\ limb_bits
square_limb_position := bit_index_to_limb_index (i + i)
square_bit_position := (i + i) \\ limb_bits
limb := special [limb_position]
square_limb := special [square_limb_position]
invariant
i = limb_position * limb_bits + bit_position
until
i < 0
loop
if
limb.bit_test (bit_position)
then
-- x.set_bit (True, i + i)
square_limb := square_limb.set_bit (True, square_bit_position)
else
-- x.set_bit (False, i + i)
square_limb := square_limb.set_bit (False, square_bit_position)
end
-- x.set_bit (False, i + i + 1)
square_limb := square_limb.set_bit (False, square_bit_position + 1)
new_bit_position := bit_position - 1
if new_bit_position < 0 and limb_position > 0 then
new_bit_position := new_bit_position + limb_bits
limb_position := limb_position - 1
limb := special [limb_position]
end
bit_position := new_bit_position
new_bit_position := square_bit_position - 2
if new_bit_position < 0 and square_limb_position > 0 then
new_bit_position := new_bit_position + limb_bits
special [square_limb_position] := square_limb
square_limb_position := square_limb_position - 1
square_limb := special [square_limb_position]
end
square_bit_position := new_bit_position
i := i - 1
variant
i + 3
end
if square_bit_position /= limb_bits - 2 then
special [square_limb_position] := square_limb
else
do_nothing
end
reduce (x, curve)
x.count := x.normalize (special, 0, bits_to_limbs (curve.m))
end
reduce (in: INTEGER_X; curve: EC_CURVE_F2M)
local
m: INTEGER
i: INTEGER
k1: INTEGER
k1_limb_position: INTEGER
k1_limb_diff: NATURAL_32
k1_bit_position: INTEGER
k2: INTEGER
k2_limb_position: INTEGER
k2_limb_diff: NATURAL_32
k2_bit_position: INTEGER
k3: INTEGER
k3_limb_position: INTEGER
k3_limb_diff: NATURAL_32
k3_bit_position: INTEGER
low_limb_position: INTEGER
low_limb_diff: NATURAL_32
low_bit_position: INTEGER
special: SPECIAL [NATURAL_32]
limb: NATURAL_32
limb_diff: NATURAL_32
limb_position: INTEGER
bit_position: INTEGER
new_bit_position: INTEGER
do
m := curve.m
k1 := curve.k1
k2 := curve.k2
k3 := curve.k3
special := in.item
from
i := m + m - 1
limb_position := bit_index_to_limb_index (i)
low_limb_position := bit_index_to_limb_index (i - m)
k1_limb_position := bit_index_to_limb_index (k1 + i - m)
bit_position := i \\ limb_bits
low_bit_position := (i - m) \\ limb_bits
k1_bit_position := (k1 + i - m) \\ limb_bits
if curve.representation = PPB then
k2_limb_position := bit_index_to_limb_index (k2 + i - m)
k3_limb_position := bit_index_to_limb_index (k3 + i - m)
k2_bit_position := (k2 + i - m) \\ limb_bits
k3_bit_position := (k3 + i - m) \\ limb_bits
end
limb := special [limb_position]
invariant
i = limb_position * limb_bits + bit_position
until
i < m
loop
if
limb.bit_test (bit_position)
then
limb_diff := limb_diff.set_bit (True, bit_position)
low_limb_diff := low_limb_diff.set_bit (True, low_bit_position)
k1_limb_diff := k1_limb_diff.set_bit (True, k1_bit_position)
if
curve.representation = PPB
then
k2_limb_diff := k2_limb_diff.set_bit (True, k2_bit_position)
k3_limb_diff := k3_limb_diff.set_bit (True, k3_bit_position)
end
end
new_bit_position := bit_position - 1
if new_bit_position < 0 then
new_bit_position := new_bit_position + limb_bits
special [limb_position] := special [limb_position].bit_xor (limb_diff)
limb_position := limb_position - 1
limb := special [limb_position]
limb_diff := 0
end
bit_position := new_bit_position
new_bit_position := low_bit_position - 1
if new_bit_position < 0 then
new_bit_position := new_bit_position + limb_bits
special [low_limb_position] := special [low_limb_position].bit_xor (low_limb_diff)
low_limb_position := low_limb_position - 1
low_limb_diff := 0
end
low_bit_position := new_bit_position
new_bit_position := k1_bit_position - 1
if new_bit_position < 0 then
new_bit_position := new_bit_position + limb_bits
special [k1_limb_position] := special [k1_limb_position].bit_xor (k1_limb_diff)
k1_limb_position := k1_limb_position - 1
k1_limb_diff := 0
end
k1_bit_position := new_bit_position
if curve.representation = PPB then
new_bit_position := k2_bit_position - 1
if new_bit_position < 0 then
new_bit_position := new_bit_position + limb_bits
special [k2_limb_position] := special [k2_limb_position].bit_xor (k2_limb_diff)
k2_limb_position := k2_limb_position - 1
k2_limb_diff := 0
end
k2_bit_position := new_bit_position
new_bit_position := k3_bit_position - 1
if new_bit_position < 0 then
new_bit_position := new_bit_position + limb_bits
special [k3_limb_position] := special [k3_limb_position].bit_xor (k3_limb_diff)
k3_limb_position := k3_limb_position - 1
k3_limb_diff := 0
end
k3_bit_position := new_bit_position
end
i := i - 1
end
if bit_position /= limb_bits - 1 then
special [limb_position] := special [limb_position].bit_xor (limb_diff)
end
if low_bit_position /= limb_bits - 1 then
special [low_limb_position] := special [low_limb_position].bit_xor (low_limb_diff)
end
if k1_bit_position /= limb_bits - 1 then
special [k1_limb_position] := special [k1_limb_position].bit_xor (k1_limb_diff)
end
if curve.representation = PPB then
if k2_bit_position /= limb_bits - 1 then
special [k2_limb_position] := special [k2_limb_position].bit_xor (k2_limb_diff)
end
if k3_bit_position /= limb_bits - 1 then
special [k3_limb_position] := special [k3_limb_position].bit_xor (k3_limb_diff)
end
end
in.count := in.normalize (special, 0, in.count)
end
inverse_value (curve: EC_CURVE_F2M): EC_FIELD_ELEMENT_F2M
do
Result := Precursor (curve)
end
inverse (curve: EC_CURVE_F2M)
local
uz: INTEGER_X
vz: INTEGER_X
-- g1z: INTEGER_X
-- g2z: INTEGER_X
-- j: INTEGER_32
-- tmp_int: INTEGER_X
m: INTEGER
-- uz_bits: INTEGER
-- vz_bits: INTEGER
-- tmp_int2: INTEGER
-- uz_old: INTEGER_X
-- gz_old: INTEGER_X
do
m := curve.m
create uz.make_bits (m + m)
uz.copy (x)
create vz.make_bits (m + m)
vz.set_bit (True, m)
vz.set_bit (True, 0)
vz.set_bit (True, curve.k1)
if
curve.representation = PPB
then
vz.set_bit (True, curve.k2)
vz.set_bit (True, curve.k3)
end
vz.count := normalize (vz.item, 0, bits_to_limbs (m))
x.invert_gf (vz)
-- create g1z.make_bits (m + m)
-- g1z.set_from_integer (1)
-- create g2z.make_bits (m + m)
-- from
-- until
-- uz.is_zero
-- loop
-- uz_bits := uz.bits
-- vz_bits := vz.bits
-- if
-- uz_bits < vz_bits
-- then
-- tmp_int := uz
-- uz := vz
-- vz := tmp_int
-- tmp_int := g1z
-- g1z := g2z
-- g2z := tmp_int
-- tmp_int2 := uz_bits
-- uz_bits := vz_bits
-- vz_bits := tmp_int2
-- end
-- if uz_bits /= vz_bits then
-- j := uz_bits - vz_bits
---- vz.bit_shift_left (j)
---- uz_old := uz.bit_xor_value (vz)
---- vz.bit_shift_right (j)
---- g2z.bit_shift_left (j)
---- gz_old := g1z.bit_xor_value (g2z)
---- g2z.bit_shift_right (j)
-- uz.bit_xor_left_shift (vz, j)
-- g1z.bit_xor_left_shift (g2z, j)
-- else
-- uz.bit_xor (vz)
-- g1z.bit_xor (g2z)
-- end
-- end
-- x := g2z
end
sqrt (curve: EC_CURVE_F2M): like Current
-- Not implemented
do
create Result.make (create {INTEGER_X}.default_create)
end
is_equal (other: like Current): BOOLEAN
do
Result := x ~ other.x
end
end

View File

@@ -0,0 +1,214 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Liberty lies in the hearts of men and women. When it dies there, no constitution, no law, no court can save it. - Justice Learned Hand"
class
EC_FIELD_ELEMENT_FP
inherit
EC_FIELD_ELEMENT
redefine
is_equal,
plus_value,
minus_value,
product_value,
quotient_value,
opposite_value,
square_value,
inverse_value
end
create
make_p_x,
make_q_x_hex
create {EC_POINT, EC_CURVE_FP}
make_zero
feature {EC_POINT_FP, EC_CURVE_FP}
make_zero
do
create x.default_create
end
feature
make_p_x (x_new: INTEGER_X)
-- create a new ECFIELDELEMENTFP based on q and x
do
x := x_new
end
make_q_x_hex(curve_a: EC_CURVE_FP x_hex_a: STRING)
do
make_p_x (create {INTEGER_X}.make_from_hex_string (x_hex_a))
end
feature {EC_FIELD_ELEMENT_FP}
W (n: INTEGER_X r: INTEGER_X x_new: INTEGER_X p_a: INTEGER_X): INTEGER_X
-- I'm not sure what this does
local
w_one: INTEGER_X
w_two: INTEGER_X
do
if
n ~ (ONE)
then
result := ((r * r * x_new.powm_value ((p_a - TWO), p_a)) - TWO) \\ p_a
elseif
not n.bit_test(0)
then
w_one := W (n / TWO, r, x, p_a)
result := ((w_one * w_one) - TWO) \\ p_a
else
w_one := W ((n + ONE) / TWO, r, x, p_a)
w_two := W ((n - ONE) / TWO, r, x, p_a)
result := ((w_one * w_two) - W (ONE, r, x, p_a)) \\ p_a
end
end
feature
encoded_field_size (curve: EC_CURVE_FP): INTEGER_32
-- Return the encoded field size for FP field elements
local
p: INTEGER_X
do
p := curve.q
result := p.bytes
end
plus_value (other: like Current; curve: EC_CURVE_FP): EC_FIELD_ELEMENT_FP
do
Result := Precursor (other, curve)
end
plus (other: like Current; curve: EC_CURVE_FP)
do
x.plus (other.x)
x.modulo (curve.q)
end
minus_value (other: like Current; curve: EC_CURVE_FP): EC_FIELD_ELEMENT_FP
do
Result := Precursor (other, curve)
end
minus (other: like Current; curve: EC_CURVE_FP)
do
x.minus (other.x)
x.modulo (curve.q)
end
product_value (other: like Current; curve: EC_CURVE_FP): EC_FIELD_ELEMENT_FP
do
Result := Precursor (other, curve)
end
product (other: like Current; curve: EC_CURVE_FP)
do
x.product (other.x)
x.modulo (curve.q)
end
quotient_value (other: like Current; curve: EC_CURVE_FP): EC_FIELD_ELEMENT_FP
do
Result := Precursor (other, curve)
end
quotient (other: like Current; curve: EC_CURVE_FP)
local
p: INTEGER_X
do
p := curve.q
x.product (other.x.inverse_value (p))
x.modulo (p)
end
opposite_value (curve: EC_CURVE_FP): EC_FIELD_ELEMENT_FP
do
Result := Precursor (curve)
end
opposite (curve: EC_CURVE_FP)
do
x.opposite
x.modulo (curve.q)
end
square_value (curve: EC_CURVE_FP): EC_FIELD_ELEMENT_FP
do
Result := Precursor (curve)
end
square (curve: EC_CURVE_FP)
do
x.product (x)
x.modulo (curve.q)
end
inverse_value (curve: EC_CURVE_FP): EC_FIELD_ELEMENT_FP
do
Result := Precursor (curve)
end
inverse (curve: EC_CURVE_FP)
do
x.inverse (curve.q)
end
sqrt (curve: EC_CURVE_FP): like Current
-- Implement sqrt over FP
local
z: EC_FIELD_ELEMENT_FP
legendreExponent: INTEGER_X
fourX: INTEGER_X
r: INTEGER_X
n1: INTEGER_X
n2: INTEGER_X
root: INTEGER_X
exponent: INTEGER_X
p: INTEGER_X
do
p := curve.q
if
p.bit_test (1)
then
create z.make_p_x (x.powm_value (p.bit_shift_right_value (2) + one, p))
Result := z
elseif
p.bit_test (0)
then
legendreExponent := (p - ONE) / TWO
exponent := x.powm_value (legendreExponent, p)
check exponent ~ one end
fourX := FOUR * x
r := TWO
from
until
not ((r * r - fourx).powm_value (legendreExponent, p) ~ (p - ONE))
loop
--Is this correct? There's a slightly higher chance that the
-- number is in the range 0 - q than q - 2^q.bits
create r.make_random (p.bits)
r := r \\ p
end
n1 := (p - ONE) / FOUR
n2 := (p + THREE) / FOUR
root := (x * (TWO * r).powm_value (p - TWO, p) * (W (n1, r, x, p) + W (n2, r, x, p))) \\ p
create z.make_p_x (root)
Result := z
else
create Result.make_p_x (create {INTEGER_X}.default_create)
(create {EXCEPTION}.default_create).raise
end
end
is_equal (other: like current): BOOLEAN
-- Is this FP = other
do
result := x ~ other.x
end
end

View File

@@ -0,0 +1,334 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "A nation of sheep will beget a government of wolves. - Edward R. Murrow"
class
EC_KEY_PAIR
inherit
DEBUG_OUTPUT
create
make,
make_p192,
make_p224,
make_p256,
make_p384,
make_p521,
make_k163,
make_k233,
make_k283,
make_k409,
make_k571,
make_b163,
make_b233,
make_b283,
make_b409,
make_b571,
make_sec_p112r1,
make_sec_p112r2,
make_sec_p128r1,
make_sec_p128r2,
make_sec_p160k1,
make_sec_p160r1,
make_sec_p160r2,
make_sec_p192k1,
make_sec_p192r1,
make_sec_p224k1,
make_sec_p224r1,
make_sec_p256k1,
make_sec_p256r1,
make_sec_p384r1,
make_sec_p521r1,
make_sec_t113r1,
make_sec_t113r2,
make_sec_t131r1,
make_sec_t131r2,
make_sec_t163k1,
make_sec_t163r1,
make_sec_t163r2,
make_sec_t193r1,
make_sec_t193r2,
make_sec_t233k1,
make_sec_t233r1,
make_sec_t239k1,
make_sec_t283k1,
make_sec_t283r1,
make_sec_t409k1,
make_sec_t409r1,
make_sec_t571k1,
make_sec_t571r1
feature
make (params: EC_DOMAIN_PARAMETERS)
local
d: INTEGER_X
q: EC_POINT
do
from
create d.make_random_max (params.n)
until
not d.is_zero
loop
create d.make_random_max (params.n)
end
q := params.g.product_value (d, params.curve)
create public.make_q_parameters (q, params)
create private.make_d_params (d, params)
end
feature --SEC recommended prime curves
make_sec_p112r1
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p112r1)
end
make_sec_p112r2
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p112r2)
end
make_sec_p128r1
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p128r1)
end
make_sec_p128r2
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p128r2)
end
make_sec_p160k1
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p160k1)
end
make_sec_p160r1
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p160r1)
end
make_sec_p160r2
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p160r2)
end
make_sec_p192k1
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p192k1)
end
make_sec_p192r1
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p192r1)
end
make_sec_p224k1
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p224k1)
end
make_sec_p224r1
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p224r1)
end
make_sec_p256k1
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p256k1)
end
make_sec_p256r1
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p256r1)
end
make_sec_p384r1
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p384r1)
end
make_sec_p521r1
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_sec_p521r1)
end
feature --SEC recommended polynomial curves
make_sec_t113r1
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t113r1)
end
make_sec_t113r2
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t113r2)
end
make_sec_t131r1
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t131r1)
end
make_sec_t131r2
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t131r2)
end
make_sec_t163k1
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t163k1)
end
make_sec_t163r1
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t163r1)
end
make_sec_t163r2
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t163r2)
end
make_sec_t193r1
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t193r1)
end
make_sec_t193r2
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t193r2)
end
make_sec_t233k1
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t233k1)
end
make_sec_t233r1
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t233r1)
end
make_sec_t239k1
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t239k1)
end
make_sec_t283k1
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t283k1)
end
make_sec_t283r1
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t283r1)
end
make_sec_t409k1
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t409k1)
end
make_sec_t409r1
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t409r1)
end
make_sec_t571k1
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t571k1)
end
make_sec_t571r1
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_sec_t571r1)
end
feature --FIPS curves
make_p192
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_p192)
end
make_p224
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_p224)
end
make_p256
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_p256)
end
make_p384
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_p384)
end
make_p521
do
make (create {EC_DOMAIN_PARAMETERS_FP}.make_p521)
end
make_k163
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_k163)
end
make_k233
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_k233)
end
make_k283
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_k283)
end
make_k409
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_k409)
end
make_k571
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_k571)
end
make_b163
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_b163)
end
make_b233
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_b233)
end
make_b283
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_b283)
end
make_b409
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_b409)
end
make_b571
do
make (create {EC_DOMAIN_PARAMETERS_F2M}.make_b571)
end
public: EC_PUBLIC_KEY
private: EC_PRIVATE_KEY
feature {DEBUG_OUTPUT} -- {DEBUG_OUTPUT}
debug_output: STRING
do
result := "Public:%N" + public.debug_output + "%NPrivate:%N" + private.debug_output
end
end

View File

@@ -0,0 +1,13 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "It is not the responsibility of the government or the legal system to protect a citizen from himself. - Justice Casey Percell"
deferred class
EC_KEY_PARAMETERS
feature
params: EC_DOMAIN_PARAMETERS
end

View File

@@ -0,0 +1,122 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "The government was set to protect man from criminals - and the Constitution was written to protect man from the government. - Ayn Rand"
deferred class
EC_POINT
inherit
ANY
redefine
is_equal
end
DEBUG_OUTPUT
undefine
is_equal
end
feature
x: EC_FIELD_ELEMENT
y: EC_FIELD_ELEMENT
infinity: BOOLEAN
make_infinity
deferred
ensure
infinity
end
set_infinity
deferred
ensure
infinity
end
is_equal (other: like Current): BOOLEAN
-- Is current point equal to other point
do
result := (infinity = other.infinity) and then (not infinity implies (x ~ other.x and y ~ other.y))
end
to_byte_array_compressed (curve: EC_CURVE): SPECIAL[NATURAL_8]
-- Return the Uncompressed version of this point, regardless of the creation
deferred
end
to_byte_array_uncompressed (curve: EC_CURVE): SPECIAL[NATURAL_8]
-- Return the compressed version of this point
deferred
end
plus (other: like Current curve: EC_CURVE)
deferred
end
plus_value (other: like Current curve: EC_CURVE): like Current
do
Result := deep_twin
Result.plus (other, curve)
ensure
infinity implies Result ~ other
other.infinity implies Result ~ Current
(Current ~ other) implies (Result ~ twice_value (curve))
end
minus (other: like Current curve: EC_CURVE)
deferred
end
minus_value (other: like Current curve: EC_CURVE): like Current
do
Result := deep_twin
Result.minus (other, curve)
ensure
infinity implies Result ~ other
other.infinity implies Result ~ Current
end
twice (curve: EC_CURVE)
deferred
end
twice_value (curve:EC_CURVE): like Current
do
Result := deep_twin
Result.twice (curve)
ensure
twice_definition: Result ~ Current.plus_value (Current, curve)
end
product (other: INTEGER_X; curve: EC_CURVE)
deferred
end
product_value (other: INTEGER_X; curve: EC_CURVE): like Current
do
Result := deep_twin
Result.product (other, curve)
end
opposite (curve: EC_CURVE)
deferred
end
opposite_value (curve: EC_CURVE): like Current
do
Result := deep_twin
Result.opposite (curve)
end
feature {DEBUG_OUTPUT} -- {DEBUG_OUTPUT}
debug_output: STRING
do
result := "0x" + x.debug_output + "%N0x" + y.debug_output
end
invariant
infinity_x: infinity implies x.x.is_zero
infinity_y: infinity implies y.x.is_zero
end

View File

@@ -0,0 +1,593 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "What this country needs are more unemployed politicians. - Edward Langley"
class
EC_POINT_F2M
inherit
EC_POINT
redefine
x,
y,
opposite_value,
twice_value,
product_value,
minus_value,
plus_value
end
EC_CONSTANTS
undefine
is_equal
end
STANDARD_CURVES
undefine
is_equal
end
INTEGER_X_FACILITIES
undefine
is_equal
end
create
make_curve_x_y,
make_infinity,
make_from_bytes,
make_sec_t113r1,
make_sec_t113r2,
make_sec_t131r1,
make_sec_t131r2,
make_sec_t163k1,
make_sec_t163r1,
make_sec_t163r2,
make_sec_t193r1,
make_sec_t193r2,
make_sec_t233k1,
make_sec_t233r1,
make_sec_t239k1,
make_sec_t283k1,
make_sec_t283r1,
make_sec_t409k1,
make_sec_t409r1,
make_sec_t571k1,
make_sec_t571r1,
make_k163,
make_k233,
make_k283,
make_k409,
make_k571,
make_b163,
make_b233,
make_b283,
make_b409,
make_b571
feature
make_infinity
do
set_infinity
end
feature -- SEC points
make_sec_t113r1
do
create x.make (sec_t113r1_gx)
create y.make (sec_t113r1_gy)
end
make_sec_t113r2
do
create x.make (sec_t113r2_gx)
create y.make (sec_t113r2_gy)
end
make_sec_t131r1
do
create x.make (sec_t131r1_gx)
create y.make (sec_t131r1_gy)
end
make_sec_t131r2
do
create x.make (sec_t131r2_gx)
create y.make (sec_t131r2_gy)
end
make_sec_t163k1
do
create x.make (sec_t163k1_gx)
create y.make (sec_t163k1_gy)
end
make_sec_t163r1
do
create x.make (sec_t163r1_gx)
create y.make (sec_t163r1_gy)
end
make_sec_t163r2
do
create x.make (sec_t163r2_gx)
create y.make (sec_t163r2_gy)
end
make_sec_t193r1
do
create x.make (sec_t193r1_gx)
create y.make (sec_t193r1_gy)
end
make_sec_t193r2
do
create x.make (sec_t193r2_gx)
create y.make (sec_t193r2_gy)
end
make_sec_t233k1
do
create x.make (sec_t233k1_gx)
create y.make (sec_t233k1_gy)
end
make_sec_t233r1
do
create x.make (sec_t233r1_gx)
create y.make (sec_t233r1_gy)
end
make_sec_t239k1
do
create x.make (sec_t239k1_gx)
create y.make (sec_t239k1_gy)
end
make_sec_t283k1
do
create x.make (sec_t283k1_gx)
create y.make (sec_t283k1_gy)
end
make_sec_t283r1
do
create x.make (sec_t283r1_gx)
create y.make (sec_t283r1_gy)
end
make_sec_t409k1
do
create x.make (sec_t409k1_gx)
create y.make (sec_t409k1_gy)
end
make_sec_t409r1
do
create x.make (sec_t409r1_gx)
create y.make (sec_t409r1_gy)
end
make_sec_t571k1
do
create x.make (sec_t571k1_gx)
create y.make (sec_t571k1_gy)
end
make_sec_t571r1
do
create x.make (sec_t571r1_gx)
create y.make (sec_t571r1_gy)
end
feature -- FIPS points
make_k163
do
create x.make (k163_gx)
create y.make (k163_gy)
end
make_k233
do
create x.make (k233_gx)
create y.make (k233_gy)
end
make_k283
do
create x.make (k283_gx)
create y.make (k283_gy)
end
make_k409
do
create x.make (k409_gx)
create y.make (k409_gy)
end
make_k571
do
create x.make (k571_gx)
create y.make (k571_gy)
end
make_b163
do
create x.make (b163_gx)
create y.make (b163_gy)
end
make_b233
do
create x.make (b233_gx)
create y.make (b233_gy)
end
make_b283
do
create x.make (b283_gx)
create y.make (b283_gy)
end
make_b409
do
create x.make (b409_gx)
create y.make (b409_gy)
end
make_b571
do
create x.make (b571_gx)
create y.make (b571_gy)
end
make_curve_x_y (x_a: EC_FIELD_ELEMENT_F2M; y_a: EC_FIELD_ELEMENT_F2M)
do
x := x_a
y := y_a
end
make_from_bytes (bytes: SPECIAL[NATURAL_8]; curve: EC_CURVE_F2M)
do
decodepoint (bytes, curve)
end
feature
x: EC_FIELD_ELEMENT_F2M
y: EC_FIELD_ELEMENT_F2M
set_from_other (other: like Current)
do
x.copy (other.x)
y.copy (other.y)
end
feature -- Decode/encode
set_infinity
do
create x.make (create {INTEGER_X}.default_create)
create y.make (create {INTEGER_X}.default_create)
infinity := True
end
decodePoint (source: SPECIAL [NATURAL_8] curve: EC_CURVE_F2M)
require
Source_too_small: source.capacity > 0
local
enc: SPECIAL [NATURAL_8]
do
create enc.make_filled (0, source.count - 1)
enc.copy_data (source, 1, 0, enc.count)
inspect
source[0]
when 0x02 then
decodeCompressedPoint (enc, 0, curve)
when 0x03 then
decodeCompressedPoint (enc, 1, curve)
when 0x04 then
decodeUncompressedPoint (enc)
end
end
decodeCompressedPoint (source: SPECIAL [NATURAL_8] ypBit: INTEGER curve: EC_CURVE_F2M)
local
xp: EC_FIELD_ELEMENT_F2M
yp: EC_FIELD_ELEMENT_F2M
i: INTEGER_32
beta: EC_FIELD_ELEMENT_F2M
z: EC_FIELD_ELEMENT_F2M
oneEC: EC_FIELD_ELEMENT_F2M
zBit: INTEGER
do
create xp.make (create {INTEGER_X}.make_from_bytes (source, source.lower, source.upper))
if
xp.x.is_zero
then
yp := curve.b
from
i := 0
until
i = curve.m - 1
loop
yp := yp.square_value (curve)
i := i + 1
end
else
beta := xp.plus_value (curve.a, curve).plus_value (curve.b.product_value (xp.square_value (curve).inverse_value (curve), curve), curve)
--z := solveQuadraticEquation(beta)
create z.make (create {INTEGER_X}.default_create)
zBit := 0
if
z.x.bit_test (0)
then
zBit := 1
end
if
zBit /= ypBit
then
create oneEC.make (ONE)
z := z.plus_value (oneEC, curve)
end
yp := xp.product_value (z, curve)
end
x := xp
y := yp
end
decodeUncompressedPoint (source: SPECIAL [NATURAL_8])
require
X_and_y_different_sizes: source.capacity \\ 2 = 0
local
xEnc: SPECIAL [NATURAL_8]
yEnc: SPECIAL [NATURAL_8]
x_mpz: INTEGER_X
y_mpz: INTEGER_X
do
create xEnc.make_filled (0, source.count // 2)
xEnc.copy_data (source, 0, 0, xEnc.count)
create yEnc.make_filled (0, source.count // 2)
yEnc.copy_data (source, source.count // 2, 0, yEnc.count)
check -- Field elements should be same size
xEnc.capacity = yEnc.capacity
end
create x_mpz.make_from_bytes (xEnc, xEnc.lower, xEnc.upper)
create y_mpz.make_from_bytes (yEnc, yEnc.lower, yEnc.upper)
create x.make (x_mpz)
create y.make (y_mpz)
end
to_byte_array_uncompressed (curve: EC_CURVE_F2M): SPECIAL [NATURAL_8]
local
byteCount: INTEGER_32
y_array: SPECIAL [NATURAL_8]
x_array: SPECIAL [NATURAL_8]
p0: SPECIAL [NATURAL_8]
do
bytecount := x.x.bytes
x_array := x.x.as_bytes
y_array := y.x.as_fixed_width_byte_array (byteCount)
create p0.make_filled (0, byteCount + byteCount + 1)
p0.put (0x04, 0)
check
x_array.capacity = y_array.capacity
end
p0.copy_data (x_array, 0, x_array.upper, 1)
p0.copy_data (y_array, 0, y_array.upper, x_array.upper + 1)
result := p0
end
to_byte_array_compressed (curve: EC_CURVE_F2M): SPECIAL [NATURAL_8]
local
byteCount: INTEGER_32
x_array: SPECIAL [NATURAL_8]
P0: SPECIAL [NATURAL_8]
do
x_array := x.x.as_bytes
byteCount := x.x.bytes
-- See X9.62 4.3.6 and 4.2.2
create P0.make_filled (0, byteCount + 1)
p0.put (0x02, 0)
-- X9.62 4.2.2 and 4.3.6:
-- if x = 0 then ypTilde := 0, else ypTilde is the rightmost
-- bit of y * x^(-1)
-- if ypTilde = 0, then PC := 02, else PC := 03
-- Note: PC === PO[0]
if
(not (x.x.is_zero)) and ((y.product_value (x.inverse_value (curve), curve)).x.bit_test(0))
then
-- ypTilde = 1, hence PC = 03
p0.put (0x03, 0)
end
p0.copy_data (x_array, 0, x_array.upper, 1)
result := p0
end
feature -- Implement ECPOINT
plus_value (other: like Current; curve: EC_CURVE_F2M): EC_POINT_F2M
do
Result := Precursor (other, curve)
end
plus (other: like Current; curve: EC_CURVE_F2M)
do
if
infinity
then
copy (other)
elseif
other.infinity
then
else
add_not_infinity (other, curve)
end
end
minus_value (other: like Current; curve: EC_CURVE_F2M): EC_POINT_F2M
do
Result := Precursor (other, curve)
end
minus (other: like Current; curve: EC_CURVE_F2M)
do
if
other.infinity
then
else
add_minus_b (other, curve)
end
end
product_value (b: INTEGER_X; curve: EC_CURVE_F2M): EC_POINT_F2M
do
Result := Precursor (b, curve)
end
product (b: INTEGER_X; curve: EC_CURVE_F2M)
local
p: like Current
q: like Current
t: INTEGER_32
-- i: INTEGER_32
special: SPECIAL [NATURAL_32]
limb: NATURAL_32
limb_position: INTEGER
new_bit_position: INTEGER
bit_position: INTEGER
do
p := Current
create q.make_infinity
t := b.bits
from
special := b.item
limb := special [limb_position]
limb_position := 0
bit_position := 0
until
limb_position * 32 + bit_position >= t
loop
if limb.bit_test (bit_position) then
q.plus (p, curve)
end
p.twice (curve)
new_bit_position := (bit_position + 1) \\ 32
if new_bit_position < bit_position then
limb_position := limb_position + 1
limb := special [limb_position]
end
bit_position := new_bit_position
end
-- p := Current
-- create q.make_infinity
-- t := b.bits
-- from
-- i := 0
-- until
-- i = t
-- loop
-- if
-- b.bit_test (i)
-- then
-- q.plus (p, curve)
-- end
-- p.twice (curve)
-- i := i + 1
-- end
copy (q)
end
twice_value (curve: EC_CURVE_F2M): EC_POINT_F2M
do
Result := Precursor (curve)
end
twice (curve: EC_CURVE_F2M)
do
if
infinity
then
elseif
x.x.is_zero
then
set_infinity
else
twice_not_infinity (curve)
end
end
opposite_value (curve: EC_CURVE_F2M): EC_POINT_F2M
do
Result := Precursor (curve)
end
opposite (curve: EC_CURVE_F2M)
do
y.plus (x, curve)
end
feature -- Implementation support features
twice_not_infinity (curve: EC_CURVE_F2M)
local
lambda: EC_FIELD_ELEMENT_F2M
x3: EC_FIELD_ELEMENT_F2M
y3: EC_FIELD_ELEMENT_F2M
one_element: EC_FIELD_ELEMENT_F2M
do
create one_element.make (one)
lambda := y.quotient_value (x, curve)
lambda.plus (x, curve)
x3 := lambda.square_value (curve)
x3.plus (lambda, curve)
x3.plus (curve.a, curve)
y3 := x.square_value (curve)
lambda.plus (one_element, curve)
lambda.product (x3, curve)
y3.plus (lambda, curve)
x := x3
y := y3
end
add_minus_b (other: like Current curve: EC_CURVE_F2M)
local
minusB: like Current
do
create minusB.make_curve_x_y (other.x, other.x.plus_value (other.y, curve))
plus (minusB, curve)
end
add_not_infinity (other: like Current; curve: EC_CURVE_F2M)
do
if
x ~ other.x
then
if
y ~ other.y
then
copy (twice_value (curve))
else
set_infinity
end
else
add_normal (other, curve)
end
end
add_normal (other: like Current; curve: EC_CURVE_F2M)
local
lambda: EC_FIELD_ELEMENT_F2M
x3: EC_FIELD_ELEMENT_F2M
y3: EC_FIELD_ELEMENT_F2M
do
lambda := (y.plus_value (other.y, curve)).quotient_value (x.plus_value (other.x, curve), curve)
x3 := lambda.square_value (curve)
x3 := x3.plus_value (lambda, curve).plus_value (x, curve).plus_value (other.x, curve).plus_value (curve.a, curve)
y3 := ((lambda.product_value (x.plus_value (x3, curve), curve)).plus_value (x3, curve)).plus_value (y, curve)
x := x3
y := y3
end
end

View File

@@ -0,0 +1,481 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Those who expect to reap the benefits of freedom, must, like men, undergo the fatigue of supporting it. - Thomas Paine"
class
EC_POINT_FP
inherit
EC_POINT
redefine
x,
y,
copy,
opposite_value,
product_value,
twice_value,
minus_value,
plus_value
end
EC_CONSTANTS
undefine
is_equal,
copy
end
STANDARD_CURVES
undefine
is_equal,
copy
end
create
make_curve_x_y,
make_from_bytes,
make_infinity,
make_sec_p112r1,
make_sec_p112r2,
make_sec_p128r1,
make_sec_p128r2,
make_sec_p160k1,
make_sec_p160r1,
make_sec_p160r2,
make_sec_p192k1,
make_sec_p192r1,
make_sec_p224k1,
make_sec_p224r1,
make_sec_p256k1,
make_sec_p256r1,
make_sec_p384r1,
make_sec_p521r1,
make_p192,
make_p224,
make_p256,
make_p384,
make_p521
feature
make_infinity
do
set_infinity
end
feature -- SEC curves
make_sec_p112r1
do
create x.make_p_x (sec_p112r1_gx)
create y.make_p_x (sec_p112r1_gy)
end
make_sec_p112r2
do
create x.make_p_x (sec_p112r2_gx)
create y.make_p_x (sec_p112r2_gy)
end
make_sec_p128r1
do
create x.make_p_x (sec_p128r1_gx)
create y.make_p_x (sec_p128r1_gy)
end
make_sec_p128r2
do
create x.make_p_x (sec_p128r2_gx)
create y.make_p_x (sec_p128r2_gy)
end
make_sec_p160k1
do
create x.make_p_x (sec_p160k1_gx)
create y.make_p_x (sec_p160k1_gy)
end
make_sec_p160r1
do
create x.make_p_x (sec_p160r1_gx)
create y.make_p_x (sec_p160r1_gy)
end
make_sec_p160r2
do
create x.make_p_x (sec_p160r2_gx)
create y.make_p_x (sec_p160r2_gy)
end
make_sec_p192k1
do
create x.make_p_x (sec_p192k1_gx)
create y.make_p_x (sec_p192k1_gy)
end
make_sec_p192r1
do
create x.make_p_x (sec_p192r1_gx)
create y.make_p_x (sec_p192r1_gy)
end
make_sec_p224k1
do
create x.make_p_x (sec_p224k1_gx)
create y.make_p_x (sec_p224k1_gy)
end
make_sec_p224r1
do
create x.make_p_x (sec_p224r1_gx)
create y.make_p_x (sec_p224r1_gy)
end
make_sec_p256k1
do
create x.make_p_x (sec_p256k1_gx)
create y.make_p_x (sec_p256k1_gy)
end
make_sec_p256r1
do
create x.make_p_x (sec_p256r1_gx)
create y.make_p_x (sec_p256r1_gy)
end
make_sec_p384r1
do
create x.make_p_x (sec_p384r1_gx)
create y.make_p_x (sec_p384r1_gy)
end
make_sec_p521r1
do
create x.make_p_x (sec_p521r1_gx)
create y.make_p_x (sec_p521r1_gy)
end
feature
make_p192
do
create x.make_p_x (p192_gx)
create y.make_p_x (p192_gy)
end
make_p224
do
create x.make_p_x (p224_gx)
create y.make_p_x (p224_gy)
end
make_p256
do
create x.make_p_x (p256_gx)
create y.make_p_x (p256_gy)
end
make_p384
do
create x.make_p_x (p384_gx)
create y.make_p_x (p384_gy)
end
make_p521
do
create x.make_p_x (p521_gx)
create y.make_p_x (p521_gy)
end
make_curve_x_y (x_a: EC_FIELD_ELEMENT_FP; y_a: EC_FIELD_ELEMENT_FP)
do
x := x_a
y := y_a
end
make_from_bytes (encoded: SPECIAL [NATURAL_8] curve: EC_CURVE_FP)
-- Decode a point on this curve from its ASN.1 encoding
-- encodings are taken account of, including point compression for
-- <code>F<sub>p</sub><code> (X9.62 s 4.2.1 pg 17).
-- @return The decoded point.
require
first_byte_indicator: encoded [0] = 0x02 or encoded [0] = 0x3 or encoded [0] = 0x4
do
inspect
encoded [0]
when 0x02 then
decodeCompressedPoint (encoded, 0, curve)
when 0x03 then
decodeCompressedPoint (encoded, 1, curve)
when 0x04 then
decodeUncompressedPoint (encoded)
end
end
feature
x: EC_FIELD_ELEMENT_FP
y: EC_FIELD_ELEMENT_FP
copy (other: like Current)
do
x.copy (other.x)
y.copy (other.y)
end
feature
set_infinity
do
create x.make_p_x (create {INTEGER_X}.default_create)
create y.make_p_x (create {INTEGER_X}.default_create)
infinity := True
end
to_byte_array_compressed (curve: EC_CURVE_FP): SPECIAL [NATURAL_8]
-- Return a compressed encoded version of this point
local
x_array: SPECIAL [NATURAL_8]
do
x_array := x.x.as_fixed_width_byte_array (x.encoded_field_size (curve))
create result.make_filled (0, x_array.count + 1)
result.copy_data (x_array, 0, 1, x_array.count)
result [0] := compressed_PC_byte (y.x)
end
to_byte_array_uncompressed (curve: EC_CURVE_FP): SPECIAL [NATURAL_8]
-- Return an uncompressed encoded version of this point
local
x_array: SPECIAL [NATURAL_8]
y_array: SPECIAL [NATURAL_8]
p0: SPECIAL [NATURAL_8]
qLength: INTEGER_32
do
qLength := x.encoded_field_size (curve)
x_array := x.x.as_fixed_width_byte_array (qlength)
y_array := y.x.as_fixed_width_byte_array (qLength)
check
x_array.capacity = qlength
y_array.capacity = qlength
end
create p0.make_filled (0, x_array.capacity + y_array.capacity + 1)
p0.copy_data (x_array, 0, x_array.upper, 1)
p0.copy_data (y_array, 0, y_array.upper, x_array.capacity + 1)
p0.put (0x04, 0)
result := p0
end
plus_value (other: like Current; curve: EC_CURVE_FP): EC_POINT_FP
do
Result := Precursor (other, curve)
end
plus (other: like Current; curve: EC_CURVE_FP)
-- Addition over FP
local
gamma: EC_FIELD_ELEMENT_FP
x3: EC_FIELD_ELEMENT_FP
y3: EC_FIELD_ELEMENT_FP
do
if
infinity
then
copy (other)
elseif
other.infinity
then
elseif
x ~ other.x
then
if
y ~ other.y
then
copy (twice_value (curve))
else
set_infinity
end
else
gamma := (other.y.minus_value (y, curve)).quotient_value (other.x.minus_value (x, curve), curve)
x3 := (gamma.product_value (gamma, curve)).minus_value (x, curve).minus_value (other.x, curve)
y3 := (gamma.product_value (x.minus_value (x3, curve), curve)).minus_value (y, curve)
x := x3
y := y3
end
end
twice_value (curve: EC_CURVE_FP): EC_POINT_FP
do
Result := Precursor (curve)
end
twice (curve: EC_CURVE_FP)
-- Return current * current over FP
local
two_element: EC_FIELD_ELEMENT_FP
three_element: EC_FIELD_ELEMENT_FP
gamma: EC_FIELD_ELEMENT_FP
x3: EC_FIELD_ELEMENT_FP
y3: EC_FIELD_ELEMENT_FP
do
if
infinity
then
elseif
y.x.is_zero
then
set_infinity
else
create two_element.make_p_x (two)
create three_element.make_p_x (three)
gamma := (((x.product_value (x, curve)).product_value (three_element, curve)).plus_value (curve.a, curve)).quotient_value (y.product_value (two_element, curve), curve)
x3 := (gamma.product_value (gamma, curve)).minus_value (x.product_value (two_element, curve), curve)
y3 := (gamma.product_value (x.minus_value (x3, curve), curve)).minus_value (y, curve)
x := x3
y := y3
end
end
minus_value (other: like Current; curve: EC_CURVE_FP): EC_POINT_FP
do
Result := Precursor (other, curve)
end
minus (other: like Current; curve: EC_CURVE_FP)
do
if
other.infinity
then
else
plus (other.opposite_value (curve), curve)
end
end
product_value (other: INTEGER_X; curve: EC_CURVE_FP): EC_POINT_FP
do
Result := Precursor (other, curve)
end
product (other: INTEGER_X; curve: EC_CURVE_FP)
-- return current * k over FP
local
e: INTEGER_X
h: INTEGER_X
R: like Current
i: INTEGER_32
do
if
infinity
then
elseif
other.is_zero
then
set_infinity
else
e := other
h := e * three
R := deep_twin
from
i := (h.bits - 2)
until
i <= 0
loop
R := r.twice_value (curve)
if
h.bit_test (i) and not e.bit_test (i)
then
r := r.plus_value (Current, curve)
elseif
not h.bit_test (i) and e.bit_test (i)
then
r := r.minus_value (Current, curve)
end
i := i - 1
end
copy (r)
end
end
opposite_value (curve: EC_CURVE_FP): like Current
do
Result := Precursor (curve)
end
opposite (curve: EC_CURVE_FP)
do
y.opposite (curve)
end
feature {NONE} -- support features
ytilde_set (source: INTEGER_X): BOOLEAN
-- Test the least significant bit, this is ytilde
-- X9.62 4.2.1
do
result := source.bit_test (0)
end
compressed_PC_byte (source: INTEGER_X): NATURAL_8
-- Return the PC byte depending on if ytilde is set
-- X9.62 4.3.6
do
if
ytilde_set (source)
then
result := 0x03
else
result := 0x02
end
end
feature {NONE}
decodeCompressedPoint (encoded: SPECIAL [NATURAL_8] ytilde: INTEGER curve: EC_CURVE_FP)
-- Decode a compressed point
require
encoded.lower = 0
local
i: SPECIAL [NATURAL_8]
x_new: EC_FIELD_ELEMENT_FP
alpha: EC_FIELD_ELEMENT_FP
beta: EC_FIELD_ELEMENT_FP
x_int: INTEGER_X
bit0: INTEGER
q_minus_beta: EC_FIELD_ELEMENT_FP
do
create i.make_filled (0, encoded.count - 1)
i.copy_data (encoded, 1, 0, i.count)
create x_int.make_from_bytes (i, i.lower, i.upper)
create x_new.make_p_x (x_int)
alpha := (x_new.product_value (x_new.square_value (curve).plus_value (curve.a, curve), curve)).plus_value (curve.b, curve)
beta := alpha.sqrt (curve)
if
beta.x.bit_test (0)
then
bit0 := 1
else
bit0 := 0
end
if
bit0 = ytilde
then
make_curve_x_y (x_new, beta)
else
create q_minus_beta.make_p_x (curve.q - beta.x)
make_curve_x_y (x_new, q_minus_beta)
end
end
decodeUncompressedPoint (encoded: SPECIAL [NATURAL_8])
-- Decode an uncompressed point
require
encoded_not_split_even: (encoded.count \\ 2) = 1
local
xEnc: SPECIAL [NATURAL_8]
yEnc: SPECIAL [NATURAL_8]
x_new: EC_FIELD_ELEMENT_FP
y_new: EC_FIELD_ELEMENT_FP
do
create xEnc.make_filled (0, (encoded.capacity - 1) // 2)
create yEnc.make_filled (0, (encoded.capacity - 1) // 2)
encoded.copy_data (xEnc, 1, 0, xEnc.capacity)
encoded.copy_data (yEnc, xEnc.capacity, 0, yEnc.capacity)
create x_new.make_p_x (create {INTEGER_X}.make_from_bytes (xEnc, xEnc.lower, xEnc.upper))
create y_new.make_p_x (create {INTEGER_X}.make_from_bytes (yEnc, yEnc.lower, yEnc.upper))
x := x_new
y := y_new
end
end

View File

@@ -0,0 +1,89 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Liberty is always dangerous, but it is the safest thing we have. - Harry Emerson Fosdick"
class
EC_PRIVATE_KEY
inherit
EC_KEY_PARAMETERS
DEBUG_OUTPUT
EC_CONSTANTS
create
make_d_params
feature
make_d_params (d_new: INTEGER_X params_new: EC_DOMAIN_PARAMETERS)
do
params := params_new
d := d_new
end
agreement (other: EC_PUBLIC_KEY): INTEGER_X
do
result := (other.q.product_value (d, params.curve)).x.x
ensure
symmetric: result ~ other.agreement (current)
end
sign (e: INTEGER_X): TUPLE [r: INTEGER_X s: INTEGER_X]
require
message_too_big: e < params.n
local
r: INTEGER_X
s: INTEGER_X
k: INTEGER_X
nBitLength: INTEGER_32
p: EC_POINT
x: INTEGER_X
n: INTEGER_X
do
n := params.n
create s.default_create
create r.default_create
create k.default_create
nBitLength := params.n.bits
from
until
s /~ s.zero
loop
from
until
r /~ r.zero
loop
from
until
k /~ k.zero
loop
create k.make_random (nBitLength)
end
p := params.g.product_value (k, params.curve)
x := p.x.x
r := x \\ params.n
end
--s := ((k.inverse_value (params.n) * (e + d * r))) \\ params.n
s := d.identity
s.product (r)
s.plus (e)
k.inverse (n)
s.product (k)
s.modulo (n)
end
create result
result.r := r
result.s := s
end
feature
d: INTEGER_X
feature {DEBUG_OUTPUT} -- {DEBUG_OUTPUT}
debug_output: STRING
do
result := "0x" + d.out_hex
end
end

View File

@@ -0,0 +1,74 @@
note
description: "Objects that ..."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "It is much more important to kill bad bills than to pass good ones. - Calvin Coolidge"
class
EC_PUBLIC_KEY
inherit
EC_KEY_PARAMETERS
DEBUG_OUTPUT
EC_CONSTANTS
create
make_q_parameters
feature -- Creation procedures
make_q_parameters (q_new: EC_POINT params_new: EC_DOMAIN_PARAMETERS)
do
params := params_new
q := q_new
end
agreement (other: EC_PRIVATE_KEY): INTEGER_X
do
Result := (q.product_value (other.d, params.curve)).x.x
ensure
symmetric: Result ~ other.agreement (Current)
end
verify (message: INTEGER_X signature: TUPLE [r: INTEGER_X s: INTEGER_X]): BOOLEAN
do
result := verify_r_s (message, signature.r, signature.s, params.curve)
end
verify_r_s (e: INTEGER_X r: INTEGER_X s: INTEGER_X curve: EC_CURVE): BOOLEAN
require
message_small_enough: e < params.n
local
c: INTEGER_X
u1: INTEGER_X
u2: INTEGER_X
point: EC_POINT
v: INTEGER_X
do
if
(r < r.one) or (r >= params.n)
then
result := false
elseif
(s < s.one) or (s >= params.n)
then
result := false
else
c := s.inverse_value (params.n)
u1 := e * c \\ params.n
u2 := r * c \\ params.n
point := (params.g.product_value (u1, params.curve)).plus_value (q.product_value (u2, params.curve), params.curve)
v := point.x.x \\ params.n
result := v ~ r
end
end
feature
q: EC_POINT
feature {DEBUG_OUTPUT} -- {DEBUG_OUTPUT}
debug_output: STRING
do
result := q.debug_output
end
end

View File

@@ -0,0 +1,18 @@
note
description: "Summary description for {F2M_REPRESENTATIONS}."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "To compel a man to furnish contributions of money for the propagation of opinions which he disbelieves and abhors, is sinful and tyrannical. - Thomas Jefferson"
deferred class
F2M_REPRESENTATIONS
feature -- Field element representations
GNB: INTEGER = 1
TPB: INTEGER = 2
PPB: INTEGER = 3
end

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,133 @@
note
description: "Summary description for {HMAC_SHA256}."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "The bureaucracy is expanding to meet the needs of an expanding bureaucracy."
class
HMAC_SHA256
inherit
BYTE_FACILITIES
create
make,
make_ascii_key
feature {NONE}
make (key_a: READABLE_INTEGER_X)
local
reduced_key: READABLE_INTEGER_X
do
if key_a.bytes <= 64 then
reduced_key := pad_key (key_a)
else
reduced_key := reduce_key (key_a)
end
ipad := (reduced_key.bit_xor_value (create {INTEGER_X}.make_from_hex_string ("36363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636"))).as_fixed_width_byte_array (64)
opad := (reduced_key.bit_xor_value (create {INTEGER_X}.make_from_hex_string ("5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c"))).as_fixed_width_byte_array (64)
create hmac.default_create
create message_hash.make
feed_inner_mix
end
make_ascii_key (key_a: READABLE_STRING_8)
local
key_bytes: SPECIAL [NATURAL_8]
i: INTEGER
do
create key_bytes.make_filled (0, key_a.count)
from
i := 1
until
i > key_a.count
loop
key_bytes [i - 1] := key_a [i].code.to_natural_8
i := i + 1
end
make (create {INTEGER_X}.make_from_bytes (key_bytes, 0, key_bytes.count - 1))
end
feature
finish
local
hash_inner: SPECIAL [NATURAL_8]
hash_outer: SPECIAL [NATURAL_8]
hmac_hash: SHA256
do
create hash_inner.make_filled (0, 32)
message_hash.do_final (hash_inner, 0)
create hmac_hash.make
hmac_hash.sink_special_lsb (opad, 0, 63)
hmac_hash.sink_special_lsb (hash_inner, 0, 31)
create hash_outer.make_filled (0, 32)
hmac_hash.do_final (hash_outer, 0)
create hmac.make_from_bytes (hash_outer, 0, 31)
finished := True
ensure
finished
end
finished: BOOLEAN
hmac: INTEGER_X
-- require
-- finished
-- attribute
-- end
reset
do
message_hash.reset
finished := False
ensure
not finished
end
feature {NONE}
reduce_key (key_a: READABLE_INTEGER_X): INTEGER_X
require
-- key_a.bytes <= 64
local
hash: SHA256
result_bytes: SPECIAL [NATURAL_8]
key_bytes: SPECIAL [NATURAL_8]
do
create hash.make
key_bytes := key_a.as_bytes
hash.sink_special (key_bytes, key_bytes.lower, key_bytes.upper)
create result_bytes.make_filled (0, 64)
hash.do_final (result_bytes, 0)
create Result.make_from_bytes (result_bytes, 0, 63)
end
pad_key (key_a: READABLE_INTEGER_X): INTEGER_X
local
key_bytes: SPECIAL [NATURAL_8]
result_bytes: SPECIAL [NATURAL_8]
do
create result_bytes.make_filled (0, 64)
key_bytes := key_a.as_bytes
result_bytes.copy_data (key_bytes, 0, 0, key_bytes.count)
create Result.make_from_bytes (result_bytes, 0, 63)
end
feed_inner_mix
do
sink_special_lsb (ipad, 0, 63)
end
byte_sink (in: NATURAL_8)
do
message_hash.update (in)
end
message_hash: SHA256
ipad: SPECIAL [NATURAL_8]
opad: SPECIAL [NATURAL_8]
end

View File

@@ -0,0 +1,58 @@
note
description: "Cipher Block Chaining mode"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Just because you do not take an interest in politics doesn't mean politics won't take an interest in you. - Pericles (430 BC)"
class
CBC_DECRYPTION
inherit
ARRAY_FACILITIES
create
make
feature
make (target_a: CBC_TARGET iv: SPECIAL [NATURAL_8] iv_offset: INTEGER_32)
require
iv.valid_index (iv_offset)
iv.valid_index (iv_offset + target_a.block_size - 1)
do
target := target_a
create last.make_filled (0, iv.count)
last.copy_data (iv, iv_offset, 0, last.count)
end
feature
block_size: INTEGER_32
do
result := target.block_size
end
decrypt_block (in: SPECIAL [NATURAL_8] in_offset: INTEGER_32 out_array: SPECIAL [NATURAL_8] out_offset: INTEGER_32)
require
cbc_ready
in.valid_index (in_offset)
in.valid_index (in_offset + block_size - 1)
out_array.valid_index (out_offset)
out_array.valid_index (out_offset + block_size - 1)
do
target.decrypt_block (in, in_offset, out_array, out_offset)
array_xor (last, 0, out_array, out_offset, out_array, out_offset, block_size)
last.copy_data (in, in_offset, 0, block_size)
end
cbc_ready: BOOLEAN
do
result := target.cbc_ready
end
feature {NONE}
last: SPECIAL [NATURAL_8]
target: CBC_TARGET
invariant
last.count = target.block_size
end

View File

@@ -0,0 +1,57 @@
note
description: "Cipher Block Chaining mode"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Nothing is so permanent as a temporary government program. - Milton Friedman"
class
CBC_ENCRYPTION
inherit
ARRAY_FACILITIES
create
make
feature
make (target_a: CBC_TARGET iv: SPECIAL [NATURAL_8] iv_offset: INTEGER_32)
require
iv.count = target_a.block_size
do
target := target_a
create last.make_filled (0, iv.count)
last.copy_data (iv, iv_offset, 0, last.count)
end
feature
block_size: INTEGER_32
do
result := target.block_size
end
encrypt_block (in: SPECIAL [NATURAL_8] in_offset: INTEGER_32 out_array: SPECIAL [NATURAL_8] out_offset: INTEGER_32)
require
cbc_ready
in.valid_index (in_offset)
in.valid_index (in_offset + block_size - 1)
out_array.valid_index (out_offset)
out_array.valid_index (out_offset + block_size - 1)
do
array_xor (last, 0, in, in_offset, last, 0, block_size)
target.encrypt_block (last, 0, out_array, out_offset)
last.copy_data (out_array, out_offset, 0, block_size)
end
cbc_ready: BOOLEAN
do
result := target.cbc_ready
end
feature {NONE}
last: SPECIAL [NATURAL_8]
target: CBC_TARGET
invariant
last.count = target.block_size
end

View File

@@ -0,0 +1,41 @@
note
description: "A block cipher that can be the target of CBC mode"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "I don't make jokes. I just watch the government and report the facts. - Will Rogers"
deferred class
CBC_TARGET
feature
block_size: INTEGER_32
deferred
ensure
Result > 0
end
cbc_ready: BOOLEAN
deferred
end
decrypt_block (in: SPECIAL [NATURAL_8] in_offset: INTEGER_32 out_array: SPECIAL [NATURAL_8] out_offset: INTEGER_32)
require
cbc_ready
in.valid_index (in_offset)
in.valid_index (in_offset + block_size - 1)
out_array.valid_index (out_offset)
out_array.valid_index (out_offset + block_size - 1)
deferred
end
encrypt_block (in: SPECIAL [NATURAL_8] in_offset: INTEGER_32 out_array: SPECIAL [NATURAL_8] out_offset: INTEGER_32)
require
cbc_ready
in.valid_index (in_offset)
in.valid_index (in_offset + block_size - 1)
out_array.valid_index (out_offset)
out_array.valid_index (out_offset + block_size - 1)
deferred
end
end

View File

@@ -0,0 +1,69 @@
note
description: "Cipher Feedback decryption mode"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "The strongest reason for the people to retain the right to bear arms is, as a last resort, to protect themselves against tyranny in government. - Thomas Jefferson"
class
CFB_DECRYPTION
inherit
ARRAY_FACILITIES
create
make
feature
make (target_a: CFB_TARGET iv: SPECIAL [NATURAL_8] iv_offset: INTEGER_32 select_block_size_a: INTEGER_32)
require
iv.valid_index (iv_offset)
iv.valid_index (iv_offset + target_a.block_size - 1)
select_block_size_a > 0
select_block_size_a <= target_a.block_size
do
select_block_size := select_block_size_a
target := target_a
create last.make_filled (0, block_size)
last.copy_data (iv, iv_offset, 0, last.count)
end
feature
block_size: INTEGER_32
do
result := target.block_size
end
select_block_size: INTEGER_32
attribute
ensure
Result > 0
Result <= block_size
end
decrypt_block (in: SPECIAL [NATURAL_8] in_offset: INTEGER_32 out_array: SPECIAL [NATURAL_8] out_offset: INTEGER_32)
require
cfb_ready
in.valid_index (in_offset)
in.valid_index (in_offset + select_block_size - 1)
out_array.valid_index (out_offset)
out_array.valid_index (out_offset + select_block_size - 1)
do
target.encrypt_block (last, 0, out_array, out_offset)
last.overlapping_move (select_block_size, 0, block_size - select_block_size)
last.copy_data (in, in_offset, block_size - select_block_size, select_block_size)
array_xor (out_array, out_offset, in, in_offset, out_array, out_offset, select_block_size)
end
cfb_ready: BOOLEAN
do
result := target.cfb_ready
end
feature {NONE}
last: SPECIAL [NATURAL_8]
target: CFB_TARGET
invariant
last.count = block_size
end

View File

@@ -0,0 +1,69 @@
note
description: "Summary description for {CFB_ENCRYPTION}."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "The era of resisting big government is never over. - Paul Gigot (1998)"
class
CFB_ENCRYPTION
inherit
ARRAY_FACILITIES
create
make
feature
make (target_a: CFB_TARGET iv: SPECIAL [NATURAL_8] iv_offset: INTEGER_32 select_block_size_a: INTEGER_32)
require
iv.valid_index (iv_offset)
iv.valid_index (iv_offset + target_a.block_size - 1)
select_block_size_a > 0
select_block_size_a <= target_a.block_size
do
select_block_size := select_block_size_a
target := target_a
create last.make_filled (0, block_size)
last.copy_data (iv, iv_offset, 0, last.count)
end
feature
block_size: INTEGER_32
do
result := target.block_size
end
select_block_size: INTEGER_32
attribute
ensure
Result > 0
Result <= block_size
end
encrypt_block (in: SPECIAL [NATURAL_8] in_offset: INTEGER_32 out_array: SPECIAL [NATURAL_8] out_offset: INTEGER_32)
require
cfb_ready
in.valid_index (in_offset)
in.valid_index (in_offset + select_block_size - 1)
out_array.valid_index (out_offset)
out_array.valid_index (out_offset + select_block_size - 1)
do
target.encrypt_block (last, 0, out_array, out_offset)
array_xor (out_array, out_offset, in, in_offset, out_array, out_offset, select_block_size)
last.overlapping_move (select_block_size, 0, block_size - select_block_size)
last.copy_data (out_array, out_offset, block_size - select_block_size, select_block_size)
end
cfb_ready: BOOLEAN
do
result := target.cfb_ready
end
feature {NONE}
last: SPECIAL [NATURAL_8]
target: CFB_TARGET
invariant
last.count = block_size
end

View File

@@ -0,0 +1,31 @@
note
description: "A block cipher that can be the target of CFB mode"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "The ultimate result of shielding men from the effects of folly is to fill the world with fools. - Herbert Spencer (1891)"
deferred class
CFB_TARGET
feature
block_size: INTEGER_32
deferred
ensure
Result > 0
end
cfb_ready: BOOLEAN
deferred
end
encrypt_block (in: SPECIAL [NATURAL_8] in_offset: INTEGER_32 out_array: SPECIAL [NATURAL_8] out_offset: INTEGER_32)
require
cfb_ready
in.valid_index (in_offset)
in.valid_index (in_offset + block_size - 1)
out_array.valid_index (out_offset)
out_array.valid_index (out_offset + block_size - 1)
deferred
end
end

View File

@@ -0,0 +1,57 @@
note
description: "Counter decryption mode"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "If you have been voting for politicians who promise to give you goodies at someone else's expense, then you have no right to complain when they take your money and give it to someone else, including themselves. - Thomas Sowell (1992)"
class
CTR_DECRYPTION
inherit
ARRAY_FACILITIES
create
make
feature
make (target_a: CTR_TARGET iv: INTEGER_X)
do
target := target_a
create counter
counter.copy (iv)
max := counter.one.bit_shift_left_value (block_size * 8)
create counter_array.make_filled (0, block_size)
end
feature
block_size: INTEGER_32
do
result := target.block_size
end
decrypt_block (in: SPECIAL [NATURAL_8] in_offset: INTEGER_32 out_array: SPECIAL [NATURAL_8] out_offset: INTEGER_32)
require
ctr_ready
in.valid_index (in_offset)
in.valid_index (in_offset + block_size - 1)
out_array.valid_index (out_offset)
out_array.valid_index (out_offset + block_size - 1)
do
counter.to_fixed_width_byte_array (counter_array, 0, block_size - 1)
target.encrypt_block (counter_array, 0, out_array, out_offset)
array_xor (out_array, out_offset, in, in_offset, out_array, out_offset, block_size)
counter := (counter + counter.one) \\ max
end
ctr_ready: BOOLEAN
do
result := target.ctr_ready
end
feature {NONE}
counter_array: SPECIAL [NATURAL_8]
counter: INTEGER_X
max: INTEGER_X
target: CTR_TARGET
end

View File

@@ -0,0 +1,57 @@
note
description: "Counter encryption mode"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "There never was a good war or a bad peace. - Benjamin Franklin (1773) "
class
CTR_ENCRYPTION
inherit
ARRAY_FACILITIES
create
make
feature
make (target_a: CTR_TARGET iv: INTEGER_X)
do
target := target_a
create counter
counter.copy (iv)
max := counter.one.bit_shift_left_value (block_size * 8)
create counter_array.make_filled (0, block_size)
end
feature
block_size: INTEGER_32
do
result := target.block_size
end
encrypt_block (in: SPECIAL [NATURAL_8] in_offset: INTEGER_32 out_array: SPECIAL [NATURAL_8] out_offset: INTEGER_32)
require
ctr_ready
in.valid_index (in_offset)
in.valid_index (in_offset + block_size - 1)
out_array.valid_index (out_offset)
out_array.valid_index (out_offset + block_size - 1)
do
counter.to_fixed_width_byte_array (counter_array, 0, block_size - 1)
target.encrypt_block (counter_array, 0, out_array, out_offset)
array_xor (out_array, out_offset, in, in_offset, out_array, out_offset, block_size)
counter := (counter + counter.one) \\ max
end
ctr_ready: BOOLEAN
do
result := target.ctr_ready
end
feature {NONE}
counter_array: SPECIAL [NATURAL_8]
counter: INTEGER_X
max: INTEGER_X
target: CTR_TARGET
end

View File

@@ -0,0 +1,31 @@
note
description: "A block cipher that can be the target of CTR mode"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Everything that is really great and inspiring is created by the individual who can labor in freedom. - Albert Einstein"
deferred class
CTR_TARGET
feature
block_size: INTEGER_32
deferred
ensure
Result > 0
end
ctr_ready: BOOLEAN
deferred
end
encrypt_block (in: SPECIAL [NATURAL_8] in_offset: INTEGER_32 out_array: SPECIAL [NATURAL_8] out_offset: INTEGER_32)
require
ctr_ready
in.valid_index (in_offset)
in.valid_index (in_offset + block_size - 1)
out_array.valid_index (out_offset)
out_array.valid_index (out_offset + block_size - 1)
deferred
end
end

View File

@@ -0,0 +1,44 @@
note
description: "Electronic Codebook decryption mode"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "There are just two rules of governance in a free society: Mind your own business. Keep your hands to yourself. - P.J. O'Rourke (1993)"
class
ECB_DECRYPTION
create
make
feature
make (target_a: ECB_TARGET)
do
target := target_a
end
feature
block_size: INTEGER_32
do
result := target.block_size
end
decrypt_block (in: SPECIAL [NATURAL_8] in_offset: INTEGER_32 out_array: SPECIAL [NATURAL_8] out_offset: INTEGER_32)
require
ecb_ready
in.valid_index (in_offset)
in.valid_index (in_offset + block_size - 1)
out_array.valid_index (out_offset)
out_array.valid_index (out_offset + block_size - 1)
do
target.decrypt_block (in, in_offset, out_array, out_offset)
end
ecb_ready: BOOLEAN
do
result := target.ecb_ready
end
feature {NONE}
target: ECB_TARGET
end

View File

@@ -0,0 +1,44 @@
note
description: "Electronic Codebook encryption mode"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Necessity is the plea for every infringement of human freedom. It is the argument of tyrants; it is the creed of slaves. - William Pitt (1783)"
class
ECB_ENCRYPTION
create
make
feature
make (target_a: ECB_TARGET)
do
target := target_a
end
feature
block_size: INTEGER_32
do
result := target.block_size
end
encrypt_block (in: SPECIAL [NATURAL_8] in_offset: INTEGER_32 out_array: SPECIAL [NATURAL_8] out_offset: INTEGER_32)
require
ecb_ready
in.valid_index (in_offset)
in.valid_index (in_offset + block_size - 1)
out_array.valid_index (out_offset)
out_array.valid_index (out_offset + block_size - 1)
do
target.encrypt_block (in, in_offset, out_array, out_offset)
end
ecb_ready: BOOLEAN
do
result := target.ecb_ready
end
feature {NONE}
target: ECB_TARGET
end

View File

@@ -0,0 +1,41 @@
note
description: "A block cipher that can be the target of ECB mode"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "A government that is big enough to give you all you want is big enough to take it all away. - Barry Goldwater (1964)"
deferred class
ECB_TARGET
feature
block_size: INTEGER_32
deferred
ensure
Result > 0
end
ecb_ready: BOOLEAN
deferred
end
decrypt_block (in: SPECIAL [NATURAL_8] in_offset: INTEGER_32 out_array: SPECIAL [NATURAL_8] out_offset: INTEGER_32)
require
ecb_ready
in.valid_index (in_offset)
in.valid_index (in_offset + block_size - 1)
out_array.valid_index (out_offset)
out_array.valid_index (out_offset + block_size - 1)
deferred
end
encrypt_block (in: SPECIAL [NATURAL_8] in_offset: INTEGER_32 out_array: SPECIAL [NATURAL_8] out_offset: INTEGER_32)
require
ecb_ready
in.valid_index (in_offset)
in.valid_index (in_offset + block_size - 1)
out_array.valid_index (out_offset)
out_array.valid_index (out_offset + block_size - 1)
deferred
end
end

View File

@@ -0,0 +1,45 @@
note
description: "Summary description for {MODE_TEST_DATA}."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "The only thing necessary for evil to triumph is for good men to do nothing. - Edmund Burke"
deferred class
MODE_TEST_DATA
feature
make_data
local
block_1_text: INTEGER_X
block_2_text: INTEGER_X
block_3_text: INTEGER_X
block_4_text: INTEGER_X
iv_text: INTEGER_X
do
create block_1_text.make_from_hex_string ("6bc1bee22e409f96e93d7e117393172a")
create block_1.make_filled (0, 16)
block_1_text.to_fixed_width_byte_array (block_1, 0, 15)
create block_2_text.make_from_hex_string ("ae2d8a571e03ac9c9eb76fac45af8e51")
create block_2.make_filled (0, 16)
block_2_text.to_fixed_width_byte_array (block_2, 0, 15)
create block_3_text.make_from_hex_string ("30c81c46a35ce411e5fbc1191a0a52ef")
create block_3.make_filled (0, 16)
block_3_text.to_fixed_width_byte_array (block_3, 0, 15)
create block_4_text.make_from_hex_string ("f69f2445df4f9b17ad2b417be66c3710")
create block_4.make_filled (0, 16)
block_4_text.to_fixed_width_byte_array (block_4, 0, 15)
create iv_text.make_from_hex_string ("000102030405060708090a0b0c0d0e0f")
create iv.make_filled (0, 16)
iv_text.to_fixed_width_byte_array (iv, 0, 15)
create iv_counter.make_from_hex_string ("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
end
block_1: SPECIAL [NATURAL_8]
block_2: SPECIAL [NATURAL_8]
block_3: SPECIAL [NATURAL_8]
block_4: SPECIAL [NATURAL_8]
iv: SPECIAL [NATURAL_8]
iv_counter: INTEGER_X
end

View File

@@ -0,0 +1,55 @@
note
description: "Output Feedback decryption mode"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Sometimes it is said that man cannot be trusted with the government of himself. Can he, then, be trusted with the government of others? - Thomas Jefferson (1801)"
class
OFB_DECRYPTION
inherit
ARRAY_FACILITIES
create
make
feature
make (target_a: OFB_TARGET iv: SPECIAL [NATURAL_8] iv_offset: INTEGER_32)
require
iv.valid_index (iv_offset)
iv.valid_index (iv_offset + target_a.block_size - 1)
do
target := target_a
create last.make_filled (0, block_size)
last.copy_data (iv, iv_offset, 0, block_size)
end
feature
block_size: INTEGER_32
do
result := target.block_size
end
decrypt_block (in: SPECIAL [NATURAL_8] in_offset: INTEGER_32 out_array: SPECIAL [NATURAL_8] out_offset: INTEGER_32)
require
ofb_ready
in.valid_index (in_offset)
in.valid_index (in_offset + block_size - 1)
out_array.valid_index (out_offset)
out_array.valid_index (out_offset + block_size - 1)
do
target.encrypt_block (last, 0, out_array, out_offset)
last.copy_data (out_array, out_offset, 0, block_size)
array_xor (last, 0, in, in_offset, out_array, out_offset, block_size)
end
ofb_ready: BOOLEAN
do
result := target.ofb_ready
end
feature {NONE}
last: SPECIAL [NATURAL_8]
target: OFB_TARGET
end

View File

@@ -0,0 +1,55 @@
note
description: "Output Feedback encryption mode"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Power tends to corrupt, and absolute power corrupts absolutely. - Lord Acton (1887)"
class
OFB_ENCRYPTION
inherit
ARRAY_FACILITIES
create
make
feature
make (target_a: OFB_TARGET iv: SPECIAL [NATURAL_8] iv_offset: INTEGER_32)
require
iv.valid_index (iv_offset)
iv.valid_index (iv_offset + target_a.block_size - 1)
do
target := target_a
create last.make_filled (0, block_size)
last.copy_data (iv, iv_offset, 0, block_size)
end
feature
block_size: INTEGER_32
do
result := target.block_size
end
encrypt_block (in: SPECIAL [NATURAL_8] in_offset: INTEGER_32 out_array: SPECIAL [NATURAL_8] out_offset: INTEGER_32)
require
ofb_ready
in.valid_index (in_offset)
in.valid_index (in_offset + block_size - 1)
out_array.valid_index (out_offset)
out_array.valid_index (out_offset + block_size - 1)
do
target.encrypt_block (last, 0, out_array, out_offset)
last.copy_data (out_array, out_offset, 0, block_size)
array_xor (last, 0, in, in_offset, out_array, out_offset, block_size)
end
ofb_ready: BOOLEAN
do
result := target.ofb_ready
end
feature {NONE}
last: SPECIAL [NATURAL_8]
target: OFB_TARGET
end

View File

@@ -0,0 +1,31 @@
note
description: "A block cipher that can be the target of OFB mode"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Political power grows out of the barrel of a gun. - Mao Zedong (1938)"
deferred class
OFB_TARGET
feature
block_size: INTEGER_32
deferred
ensure
Result > 0
end
ofb_ready: BOOLEAN
deferred
end
encrypt_block (in: SPECIAL [NATURAL_8] in_offset: INTEGER_32 out_array: SPECIAL [NATURAL_8] out_offset: INTEGER_32)
require
ofb_ready
in.valid_index (in_offset)
in.valid_index (in_offset + block_size - 1)
out_array.valid_index (out_offset)
out_array.valid_index (out_offset + block_size - 1)
deferred
end
end

View File

@@ -0,0 +1,31 @@
note
description: "Provides facilities to rotate integers"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "The more corrupt the state, the more it legislates. - Tacitus"
deferred class
ROTATE_FACILITIES
feature
rotate_right_32 (in: NATURAL_32 count: INTEGER_32): NATURAL_32
require
count_too_small: count >= 0
count_too_big: count <= 32
do
result := (in |>> count) | (in |<< (32 - count))
ensure
rotate_definition: result = (in |>> count) | (in |<< (32 - count))
end
rotate_left_32 (in: NATURAL_32 count: INTEGER_32): NATURAL_32
require
count_too_small: count >= 0
count_too_big: count <= 32
do
result := (in |<< count) | (in |>> (32 - count))
ensure
rotate_definition: result = (in |<< count) | (in |>> (32 - count))
end
end

View File

@@ -0,0 +1,38 @@
note
description: "x509v3 AlgorithmIdentifier sequence"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "When you subsidize poverty and failure, you get more of both. - James Dale Davidson, National Taxpayers Union"
class
ALGORITHM_IDENTIFIER
inherit
ANY
redefine
is_equal
end
create
make
feature
make (algorithm_a: OBJECT_IDENTIFIER parameters_a: ALGORITHM_PARAMETERS)
do
algorithm := algorithm_a
parameters := parameters_a
end
is_equal (other: like Current): BOOLEAN
do
result := algorithm ~ other.algorithm and parameters ~ other.parameters
ensure then
algorithm ~ other.algorithm
parameters ~ other.parameters
end
feature
algorithm: OBJECT_IDENTIFIER
parameters: ALGORITHM_PARAMETERS
end

View File

@@ -0,0 +1,11 @@
note
description: "Summary description for {ALGORITHM_PARAMETERS}."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "If we have to kill 12 people to save 1 human life it will have been worth it. - Unknown"
class
ALGORITHM_PARAMETERS
end

View File

@@ -0,0 +1,24 @@
note
description: "x509v3 AttributeTypeAndValue sequence"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Truth and news are not the same thing. - Katharine Graham, owner of The Washington Post"
class
ATTRIBUTE_TYPE_AND_VALUE
create
make
feature
make (type_a: OBJECT_IDENTIFIER value_a: SPECIAL [NATURAL_8])
do
type := type_a
value := value_a
end
feature
type: OBJECT_IDENTIFIER
value: SPECIAL [NATURAL_8]
end

View File

@@ -0,0 +1,29 @@
note
description: "x509v3 Certificate sequence."
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Everyone thinks about changing the world, but no one thinks about changing himself. - Leo Tolstoy"
class
CERTIFICATE
create
make
feature
make (tbs_certificate_a: TBS_CERTIFICATE signature_algorithm_a: ALGORITHM_IDENTIFIER signature_value_a: SPECIAL [NATURAL_8])
do
tbs_certificate := tbs_certificate_a
signature_algorithm := signature_algorithm_a
signature_value := signature_value_a
end
feature
tbs_certificate: TBS_CERTIFICATE
signature_algorithm: ALGORITHM_IDENTIFIER
signature_value: SPECIAL [NATURAL_8]
invariant
mismatched_algorithms: signature_algorithm ~ tbs_certificate.signature
end

View File

@@ -0,0 +1,26 @@
note
description: "x509v3 extension sequence"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "The Constitution is not an instrument for the government to restrain the people, it is an instrument for the people to restrain the government - lest it come to dominate our lives and interests. - Patrick Henry"
class
EXTENSION
create
make
feature
make (extn_id_a: OBJECT_IDENTIFIER critical_a: BOOLEAN extn_value_a: SPECIAL [NATURAL_8])
do
extn_id := extn_id_a
critical := critical_a
extn_value := extn_value_a
end
feature
extn_id: OBJECT_IDENTIFIER
critical: BOOLEAN
extn_value: SPECIAL [NATURAL_8]
end

View File

@@ -0,0 +1,22 @@
note
description: "x509v3 Name choice"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "When goods don't cross borders, soldiers will. - Fredric Bastiat, early French economists"
class
NAME
create
make
feature
make (rdn_sequence_a: LIST [ATTRIBUTE_TYPE_AND_VALUE])
do
rdn_sequence := rdn_sequence_a
end
feature
rdn_sequence: LIST [ATTRIBUTE_TYPE_AND_VALUE]
end

View File

@@ -0,0 +1,108 @@
note
description: "ASN.1 OIDs"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote:
"[
Virtually all reasonable laws are obeyed, not because they are the law, but because reasonable people would do that anyway.
If you obey a law simply because it is the law, that's a pretty likely sign that it shouldn't be a law. - Unknown
]"
class
OBJECT_IDENTIFIER
inherit
ANY
redefine
is_equal
end
create
make_md2,
make_md5,
make_id_sha1,
make_md2_with_rsa_encryption,
make_md5_with_rsa_encryption,
make_sha_1_with_rsa_encryption,
make_id_dsa_with_sha1,
make_ecdsa_with_sha1,
make_pkcs_1
feature
make_md2
do
id := "1.2.840.113549.2.2"
end
make_md5
do
id := "1.2.840.113549.2.5"
end
make_id_sha1
do
id := "1.3.14.3.2.26"
end
make_md2_with_rsa_encryption
do
id := "1.2.840.113549.1.1.2"
end
make_md5_with_rsa_encryption
do
id := "1.2.840.113549.1.1.4"
end
make_sha_1_with_rsa_encryption
do
id := "1.2.840.113549.1.1.5"
end
make_id_dsa_with_sha1
do
id := "1.2.840.10040.4.3"
end
make_ecdsa_with_sha1
do
id := "1.2.840.10045.4.1"
end
make_pkcs_1
do
id := "1.2.840.113549.1"
end
make_sha_224_with_rsa_encryption
do
id := "1.2.840.113549.1.14"
end
make_sha_256_with_rsa_encryption
do
id := "1.2.840.113549.1.11"
end
make_sha_384_with_rsa_encryption
do
id := "1.2.840.113549.1.12"
end
make_sha_512_with_rsa_encryption
do
id := "1.2.840.113549.1.13"
end
feature
is_equal (other: like Current): BOOLEAN
do
result := id ~ other.id
ensure then
id ~ other.id
end
feature
id: STRING
end

View File

@@ -0,0 +1,24 @@
note
description: "x509v3 SubjectPublicKeyInfo sequence"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "One of the penalties for refusing to participate in politics is that you end up being governed by your inferiors. - Plato"
class
SUBJECT_PUBLIC_KEY_INFO
create
make
feature
make (algorithm_a: STRING subject_public_key_a: STRING)
do
algorithm := algorithm_a
subject_public_key := subject_public_key_a
end
feature
algorithm: STRING
subject_public_key: STRING
end

View File

@@ -0,0 +1,72 @@
note
description: "x509v3 TBSCertificate sequence"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "Democracy is a pathetic belief in the collective wisdom of individual ignorance. - H.L. Mencken"
class
TBS_CERTIFICATE
inherit
DER_ENCODABLE
create
make
feature
make ( version_a: INTEGER_32 serial_number_a: INTEGER_X signature_a: ALGORITHM_IDENTIFIER issuer_a: NAME validity_a: VALIDITY
subject_a: NAME subject_public_key_info_a: SUBJECT_PUBLIC_KEY_INFO issuer_unique_id_a: SPECIAL [NATURAL_8]
subject_unique_id_a: SPECIAL [NATURAL_8] extensions_a: LIST [EXTENSION])
require
do
version := version_a
serial_number := serial_number_a
signature := signature_a
issuer := issuer_a
validity := validity_a
subject := subject_a
subject_public_key_info := subject_public_key_info_a
issuer_unique_id := issuer_unique_id_a
subject_unique_id := subject_unique_id_a
extensions := extensions_a
end
feature
der_encode (target: DER_OCTET_SINK)
do
end
feature
version: INTEGER_32
serial_number: INTEGER_X
signature: ALGORITHM_IDENTIFIER
issuer: NAME
validity: VALIDITY
subject: NAME
subject_public_key_info: SUBJECT_PUBLIC_KEY_INFO
issuer_unique_id: SPECIAL [NATURAL_8]
subject_unique_id: SPECIAL [NATURAL_8]
extensions: LIST [EXTENSION]
feature
valid_version (in: INTEGER_32): BOOLEAN
do
result := in = 2
ensure
result = (in = 2)
end
valid_serial_number (in: INTEGER_X): BOOLEAN
do
result := (in >= in.one) and in.bits <= 20 * 8
ensure
result = ((in >= in.one) and in.bits <= 20 * 8)
end
invariant
valid_version (version)
valid_serial_number (serial_number)
end

View File

@@ -0,0 +1,24 @@
note
description: "x509v3 Validity sequence"
author: "Colin LeMahieu"
date: "$Date: 2011-11-11 18:13:16 +0100 (ven., 11 nov. 2011) $"
revision: "$Revision: 87787 $"
quote: "If we don't believe in freedom of expression for people we despise, we don't believe in it at all. - Noam Chomsky"
class
VALIDITY
create
make
feature
make (not_before_a: TIME; not_after_a: TIME)
do
not_before := not_before_a
not_after := not_after_a
end
feature
not_before: TIME
not_after: TIME
end