change serialization interface
[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 const char *tom_init(void)
9 {
10         /* nothing to initialize */
11         return "tomcrypt";
12 }
13
14 static unsigned long tom_encrypt(void *key, int keylen, void *iv,
15                         void *pt, void *ct, int tlen)
16 {
17         symmetric_CBC cbc;
18         int index, err;
19
20         if ((index = register_cipher(&aes_desc)) == -1)
21                 return CRYPT_INVALID_CIPHER;
22         if ((err = cbc_start(index, iv, key, keylen, 0, &cbc)) != CRYPT_OK)
23                 return err;
24         err= cbc_encrypt(pt, ct, tlen, &cbc);
25         (void)cbc_done(&cbc);
26         return err;
27 }
28
29 static unsigned long tom_decrypt(void *key, int keylen, void *iv,
30                         void *ct, void *pt, int tlen)
31 {
32         symmetric_CBC cbc;
33         int index, err;
34
35         if ((index = register_cipher(&aes_desc)) == -1)
36                 return CRYPT_INVALID_CIPHER;
37         if ((err = cbc_start(index, iv, key, keylen, 0, &cbc)) != CRYPT_OK)
38                 return err;
39         err= cbc_decrypt(ct, pt, tlen, &cbc);
40         (void)cbc_done(&cbc);
41         return err;
42 }
43
44 static unsigned long tom_hash(void *pt, int tlen, void *tag, int *taglen)
45 {
46         int index, rc;
47         unsigned long ltaglen = *taglen;
48
49         if ((index = register_hash(&sha1_desc)) == -1)
50                 return CRYPT_INVALID_HASH;
51         rc = hash_memory(index, pt, tlen, tag, &ltaglen);
52         *taglen = ltaglen;
53         return rc;
54 }
55
56 static unsigned long tom_hmac(void *key, int keylen,
57                         void *pt, int tlen, void *tag, int *taglen)
58 {
59         int index, rc;
60         unsigned long ltaglen = *taglen;
61
62         if ((index = register_hash(&sha1_desc)) == -1)
63                 return CRYPT_INVALID_HASH;
64         rc = hmac_memory(index, key, keylen, pt, tlen, tag, &ltaglen);
65         *taglen = ltaglen;
66         return rc;
67 }
68
69 static const char *tom_errstr(unsigned long err)
70 {
71         return error_to_string((int)err);
72 }
73
74 struct crypto_interface tom_crypto_if = {
75         .init           = tom_init,
76         .encrypt        = tom_encrypt,
77         .decrypt        = tom_decrypt,
78         .hash           = tom_hash,
79         .hmac           = tom_hmac,
80         .errstr         = tom_errstr,
81 };