1# 2# basic cryptography routines implemented in C 3# 4Crypt: module 5{ 6 PATH: con "$Crypt"; 7 8 # state held while creating digests 9 DigestState: adt 10 { 11 x: int; # dummy for C compiler for runt.h 12 # all the state is hidden 13 14 copy: fn(d: self ref DigestState): ref DigestState; 15 }; 16 17 # expanded AES key + state for chaining 18 AESstate: adt 19 { 20 x: int; # dummy for C compiler for runt.h 21 # all the state is hidden 22 }; 23 24 # expanded DES key + state for chaining 25 DESstate: adt 26 { 27 x: int; # dummy for C compiler for runt.h 28 # all the state is hidden 29 }; 30 31 # expanded IDEA key + state for chaining 32 IDEAstate: adt 33 { 34 x: int; # dummy for C compiler for runt.h 35 # all the state is hidden 36 }; 37 38 # expanded RC4 key + encryption state 39 RC4state: adt 40 { 41 x: int; # dummy for C compiler for runt.h 42 # all the state is hidden 43 }; 44 45 # expanded Blowfish key + state for chaining 46 BFstate: adt 47 { 48 x: int; # dummy for C compiler for runt.h 49 # all the state is hidden 50 }; 51 52 # digests 53 sha1: fn(buf: array of byte, n: int, digest: array of byte, state: ref DigestState): 54 ref DigestState; 55 sha224: fn(buf: array of byte, n: int, digest: array of byte, state: ref DigestState): 56 ref DigestState; 57 sha256: fn(buf: array of byte, n: int, digest: array of byte, state: ref DigestState): 58 ref DigestState; 59 sha384: fn(buf: array of byte, n: int, digest: array of byte, state: ref DigestState): 60 ref DigestState; 61 sha512: fn(buf: array of byte, n: int, digest: array of byte, state: ref DigestState): 62 ref DigestState; 63 md4: fn(buf: array of byte, n: int, digest: array of byte, state: ref DigestState): 64 ref DigestState; 65 md5: fn(buf: array of byte, n: int, digest: array of byte, state: ref DigestState): 66 ref DigestState; 67 68 hmac_sha1: fn(data: array of byte, n: int, key: array of byte, digest: array of byte, state: ref DigestState): 69 ref DigestState; 70 hmac_md5: fn(data: array of byte, n: int, key: array of byte, digest: array of byte, state: ref DigestState): 71 ref DigestState; 72 73 SHA1dlen: con 20; 74 SHA224dlen: con 28; 75 SHA256dlen: con 32; 76 SHA384dlen: con 48; 77 SHA512dlen: con 64; 78 MD5dlen: con 16; 79 MD4dlen: con 16; 80 81 # encryption interfaces 82 Encrypt: con 0; 83 Decrypt: con 1; 84 85 AESbsize: con 16; 86 87 aessetup: fn(key: array of byte, ivec: array of byte): ref AESstate; 88 aescbc: fn(state: ref AESstate, buf: array of byte, n: int, direction: int); 89 90 DESbsize: con 8; 91 92 dessetup: fn(key: array of byte, ivec: array of byte): ref DESstate; 93 desecb: fn(state: ref DESstate, buf: array of byte, n: int, direction: int); 94 descbc: fn(state: ref DESstate, buf: array of byte, n: int, direction: int); 95 96 IDEAbsize: con 8; 97 98 ideasetup: fn(key: array of byte, ivec: array of byte): ref IDEAstate; 99 ideaecb: fn(state: ref IDEAstate, buf: array of byte, n: int, direction: int); 100 ideacbc: fn(state: ref IDEAstate, buf: array of byte, n: int, direction: int); 101 102 BFbsize: con 8; 103 104 blowfishsetup: fn(key: array of byte, ivec: array of byte): ref BFstate; 105# blowfishecb: fn(state: ref BFstate, buf: array of byte, n: int, direction: int); 106 blowfishcbc: fn(state: ref BFstate, buf: array of byte, n: int, direction: int); 107 108 rc4setup: fn(seed: array of byte): ref RC4state; 109 rc4: fn(state: ref RC4state, buf: array of byte, n: int); 110 rc4skip: fn(state: ref RC4state, n: int); 111 rc4back: fn(state: ref RC4state, n: int); 112 113 # create an alpha and p for diffie helman exchanges 114 dhparams: fn(nbits: int): (ref IPints->IPint, ref IPints->IPint); 115 116 # public key 117 PK: adt 118 { 119 pick { 120 RSA => 121 n: ref IPints->IPint; # modulus 122 ek: ref IPints->IPint; # exp (encryption key) 123 Elgamal => 124 p: ref IPints->IPint; # modulus 125 alpha: ref IPints->IPint; # generator 126 key: ref IPints->IPint; # encryption key (alpha**secret mod p) 127 DSA => 128 p: ref IPints->IPint; # modulus 129 q: ref IPints->IPint; # group order, q divides p-1 130 alpha: ref IPints->IPint; # group generator 131 key: ref IPints->IPint; # encryption key (alpha**secret mod p) 132 } 133 }; 134 135 # secret key (private/public key pair) 136 SK: adt 137 { 138 pick { 139 RSA => 140 pk: ref PK.RSA; 141 dk: ref IPints->IPint; # exp (decryption key) 142 p: ref IPints->IPint; # q in pkcs 143 q: ref IPints->IPint; # p in pkcs 144 # precomputed crt values 145 kp: ref IPints->IPint; # k mod p-1 146 kq: ref IPints->IPint; # k mod q-1 147 c2: ref IPints->IPint; # for converting residues to number 148 Elgamal => 149 pk: ref PK.Elgamal; 150 secret: ref IPints->IPint; # decryption key 151 DSA => 152 pk: ref PK.DSA; 153 secret: ref IPints->IPint; # decryption key 154 } 155 }; 156 157 # public key signature 158 PKsig: adt 159 { 160 # could just have list or array of ref IPints->IPint 161 pick { 162 RSA => 163 n: ref IPints->IPint; 164 Elgamal => 165 r: ref IPints->IPint; 166 s: ref IPints->IPint; 167 DSA => 168 r: ref IPints->IPint; 169 s: ref IPints->IPint; 170 } 171 }; 172 173 # RSA keys 174 rsagen: fn(nlen: int, elen: int, nrep: int): ref SK.RSA; 175 rsafill: fn(n: ref IPints->IPint, ek: ref IPints->IPint, dk: ref IPints->IPint, p: ref IPints->IPint, q: ref IPints->IPint): ref SK.RSA; 176 rsadecrypt: fn(k: ref SK.RSA, m: ref IPints->IPint): ref IPints->IPint; 177 rsaencrypt: fn(k: ref PK.RSA, m: ref IPints->IPint): ref IPints->IPint; 178 179 # Elgamal 180 eggen: fn(nlen: int, nrep: int): ref SK.Elgamal; 181 182 # DSA 183 dsagen: fn(oldpk: ref PK.DSA): ref SK.DSA; 184 185 # generic signature functions 186 genSK: fn(algname: string, length: int): ref SK; 187 genSKfromPK: fn(pk: ref PK): ref SK; 188 sign: fn(sk: ref SK, m: ref IPints->IPint): ref PKsig; 189 verify: fn(pk: ref PK, sig: ref PKsig, m: ref IPints->IPint): int; 190 sktopk: fn(sk: ref SK): ref PK; 191}; 192