create test for crypto
[pam_pcsc_cr.git] / tom_crypto.c
1 #include <tomcrypt.h>
2
3 #include "crypto_if.h"
4
5 static unsigned long tom_encrypt(void *key, int keylen, void *iv,
6                         void *pt, void *ct, int tlen)
7 {
8         symmetric_CBC cbc;
9         int index, err;
10
11         if ((index = register_cipher(&aes_desc)) == -1)
12                 return CRYPT_INVALID_CIPHER;
13         if ((err = cbc_start(index, iv, key, keylen, 0, &cbc)) != CRYPT_OK)
14                 return err;
15         err= cbc_encrypt(pt, ct, tlen, &cbc);
16         (void)cbc_done(&cbc);
17         return err;
18 }
19
20 static unsigned long tom_decrypt(void *key, int keylen, void *iv,
21                         void *ct, void *pt, int tlen)
22 {
23         symmetric_CBC cbc;
24         int index, err;
25
26         if ((index = register_cipher(&aes_desc)) == -1)
27                 return CRYPT_INVALID_CIPHER;
28         if ((err = cbc_start(index, iv, key, keylen, 0, &cbc)) != CRYPT_OK)
29                 return err;
30         err= cbc_decrypt(ct, pt, tlen, &cbc);
31         (void)cbc_done(&cbc);
32         return err;
33 }
34
35 static unsigned long tom_hash(void *pt, int tlen, void *tag, int *taglen)
36 {
37         int index, rc;
38         unsigned long ltaglen = *taglen;
39
40         if ((index = register_hash(&sha1_desc)) == -1)
41                 return CRYPT_INVALID_HASH;
42         rc = hash_memory(index, pt, tlen, tag, &ltaglen);
43         *taglen = ltaglen;
44         return rc;
45 }
46
47 static unsigned long tom_hmac(void *key, int keylen,
48                         void *pt, int tlen, void *tag, int *taglen)
49 {
50         int index, rc;
51         unsigned long ltaglen = *taglen;
52
53         if (keylen != 20) return CRYPT_INVALID_KEYSIZE;
54         if ((index = register_hash(&sha1_desc)) == -1)
55                 return CRYPT_INVALID_HASH;
56         rc = hmac_memory(index, key, keylen, pt, tlen, tag, &ltaglen);
57         *taglen = ltaglen;
58         return rc;
59 }
60
61 static const char *tom_errstr(unsigned long err)
62 {
63         return error_to_string((int)err);
64 }
65
66 struct crypto_interface tom_crypto_if = {
67         .name           = "tomcrypt",
68         .encrypt        = tom_encrypt,
69         .decrypt        = tom_decrypt,
70         .hash           = tom_hash,
71         .hmac           = tom_hmac,
72         .errstr         = tom_errstr,
73 };