MODULE CryptoCiphers;
CONST
ECB* = 0;
CBC* = 1;
Ok* = 0;
TYPE
Cipher* = OBJECT
VAR
name-: ARRAY 64 OF CHAR;
blockSize-: LONGINT;
isKeyInitialized*: BOOLEAN;
mode-: SHORTINT;
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;
PROCEDURE InitKey*( CONST src: ARRAY OF CHAR; pos: LONGINT; keybits: LONGINT );
BEGIN
appsize( keybits ); isKeyInitialized := TRUE
END InitKey;
PROCEDURE SetIV*( CONST src: ARRAY OF CHAR; pos: LONGINT );
BEGIN
ASSERT( isKeyInitialized );
mode := CBC; appmode( "-cbc" )
END SetIV;
PROCEDURE Encrypt*( VAR buf: ARRAY OF CHAR; pos, len: LONGINT );
END Encrypt;
PROCEDURE Decrypt*( VAR buf: ARRAY OF CHAR; pos, len: LONGINT );
END Decrypt;
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;
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.