0 | module Crypto.Hash.OID
 1 |
 2 | import Crypto.Hash.Interfaces
 3 | import Crypto.Hash
 4 | import Data.Vect
 5 |
 6 | {-
 7 |   For the nine hash functions mentioned in Appendix B.1, the DER
 8 |   encoding T of the DigestInfo value is equal to the following:
 9 |
10 |   MD2:     (0x)30 20 30 0c 06 08 2a 86 48 86 f7 0d 02 02 05 00 04
11 |                10 || H.
12 |   MD5:     (0x)30 20 30 0c 06 08 2a 86 48 86 f7 0d 02 05 05 00 04
13 |                10 || H.
14 |   SHA-1:   (0x)30 21 30 09 06 05 2b 0e 03 02 1a 05 00 04 14 || H.
15 |   SHA-224:  (0x)30 2d 30 0d 06 09 60 86 48 01 65 03 04 02 04
16 |                05 00 04 1c || H.
17 |   SHA-256: (0x)30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05 00
18 |                04 20 || H.
19 |   SHA-384: (0x)30 41 30 0d 06 09 60 86 48 01 65 03 04 02 02 05 00
20 |                04 30 || H.
21 |   SHA-512: (0x)30 51 30 0d 06 09 60 86 48 01 65 03 04 02 03 05 00
22 |                04 40 || H.
23 |   SHA-512/224:  (0x)30 2d 30 0d 06 09 60 86 48 01 65 03 04 02 05
24 |                     05 00 04 1c || H.
25 |   SHA-512/256:  (0x)30 31 30 0d 06 09 60 86 48 01 65 03 04 02 06
26 |                     05 00 04 20 || H.
27 | -}
28 |
29 | public export
30 | interface Hash algo => RegisteredHash algo where
31 |   header_n_byte : Nat
32 |   header : Vect header_n_byte Bits8
33 |
34 | public export
35 | der_digest_n_byte : {algo : _} -> RegisteredHash algo => Nat
36 | der_digest_n_byte = header_n_byte {algo} + digest_nbyte {algo}
37 |
38 | export
39 | hashWithHeader : {algo : _} -> RegisteredHash algo => List Bits8 -> Vect (der_digest_n_byte {algo}) Bits8
40 | hashWithHeader plaintext = header {algo} ++ hash algo plaintext
41 |
42 | public export
43 | RegisteredHash Sha1 where
44 |   header_n_byte = 15
45 |   header = [ 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 ]
46 |
47 | public export
48 | RegisteredHash Sha256 where
49 |   header_n_byte = 19
50 |   header = [ 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 ]
51 |
52 | public export
53 | RegisteredHash Sha384 where
54 |   header_n_byte = 19
55 |   header = [ 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30 ]
56 |
57 | public export
58 | RegisteredHash Sha512 where
59 |   header_n_byte = 19
60 |   header = [ 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40 ]
61 |