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