-
Notifications
You must be signed in to change notification settings - Fork 4
/
CryptoCiphers.Mod
executable file
·95 lines (79 loc) · 2.78 KB
/
CryptoCiphers.Mod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
MODULE CryptoCiphers; (** empty cipher *)
(* 2002.07.22 g.f. *)
CONST
(** cipher modes *)
ECB* = 0; (** electronic codebook mode *)
CBC* = 1; (** cipher-block chaining mode *)
(** error codes *)
Ok* = 0;
TYPE
Cipher* = OBJECT
VAR
name-: ARRAY 64 OF CHAR;
blockSize-: LONGINT; (** cipher block size (bytes) *)
isKeyInitialized*: BOOLEAN;
mode-: SHORTINT; (** ECB, CBC *)
len1, len2: INTEGER;
PROCEDURE appsize( size: LONGINT );
VAR i, t, d: LONGINT;
BEGIN
IF (size = 64) OR (size = 128) THEN RETURN END;
len2 := len1; i := 0; t := 10000;
WHILE t > size DO t := t DIV 10 END;
REPEAT
d := size DIV t MOD 10; name[len2] := CHR( 48 + d ); INC( len2 ); t := t DIV 10
UNTIL t = 0;
name[len2] := 0X
END appsize;
PROCEDURE appmode( CONST str: ARRAY OF CHAR );
VAR i, j: INTEGER; c: CHAR;
BEGIN
j := len2; i := 0;
REPEAT c := str[i]; INC( i ); name[j] := c; INC( j ) UNTIL c = 0X;
END appmode;
(** initialize key for de/encryption *)
PROCEDURE InitKey*( CONST src: ARRAY OF CHAR; pos: LONGINT; keybits: LONGINT );
BEGIN
appsize( keybits ); isKeyInitialized := TRUE
END InitKey;
(** set initialization vector, change mode to CBC; MUST be invoked AFTER Initkey *)
PROCEDURE SetIV*( CONST src: ARRAY OF CHAR; pos: LONGINT );
BEGIN
ASSERT( isKeyInitialized ); (* initKey must have been called before *)
mode := CBC; appmode( "-cbc" )
END SetIV;
(** encrypts len bytes of data; len must be a multiple of blockSize *)
PROCEDURE Encrypt*( VAR buf: ARRAY OF CHAR; pos, len: LONGINT );
(* empty Cipher does nothing *)
END Encrypt;
(** decrypts len bytes of data; len must be a multiple of blockSize *)
PROCEDURE Decrypt*( VAR buf: ARRAY OF CHAR; pos, len: LONGINT );
(* empty Cipher does nothing *)
END Decrypt;
(** this method is invoked by subclasses; blocksize in bytes *)
PROCEDURE SetNameAndBlocksize*( CONST name: ARRAY OF CHAR; size: LONGINT );
BEGIN
COPY( name, SELF.name ); len1 := 0;
WHILE name[len1] # 0X DO INC( len1 ) END;
len2 := len1; blockSize := size;
END SetNameAndBlocksize;
PROCEDURE & Init*;
BEGIN
mode := ECB; isKeyInitialized := FALSE;
SetNameAndBlocksize( "empty cipher", 8 );
END Init;
END Cipher;
CipherFactory = PROCEDURE ( ): Cipher;
(** create a new cipher object of the subtype given in modname*)
PROCEDURE NewCipher*( CONST modname: ARRAY OF CHAR ): Cipher;
VAR cipher: Cipher; factory: CipherFactory;
BEGIN
cipher := NIL;
IF modname # "" THEN
GETPROCEDURE( modname, "NewCipher", factory );
IF (factory # NIL ) THEN cipher := factory() END
ELSE NEW( cipher )
END;
RETURN cipher;
END NewCipher;
END CryptoCiphers.