]> www.average.org Git - pam_pcsc_cr.git/blob - tom_crypto.c
include man pages in Makefile
[pam_pcsc_cr.git] / tom_crypto.c
1 /*
2 Copyright (c) 2013 Eugene Crosser
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12     1. The origin of this software must not be misrepresented; you must
13     not claim that you wrote the original software. If you use this
14     software in a product, an acknowledgment in the product documentation
15     would be appreciated but is not required.
16
17     2. Altered source versions must be plainly marked as such, and must
18     not be misrepresented as being the original software.
19
20     3. This notice may not be removed or altered from any source
21     distribution.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 #include <tomcrypt.h>
28
29 #include "crypto_if.h"
30
31 static const char *tom_init(void)
32 {
33         /* nothing to initialize */
34         return "tomcrypt";
35 }
36
37 static unsigned long tom_encrypt(const void *key, const int keylen, void *iv,
38                         const void *pt, void *ct, const int tlen)
39 {
40         symmetric_CBC cbc;
41         int index, err;
42
43         if ((index = register_cipher(&aes_desc)) == -1)
44                 return CRYPT_INVALID_CIPHER;
45         if ((err = cbc_start(index, iv, key, keylen, 0, &cbc)) != CRYPT_OK)
46                 return err;
47         err= cbc_encrypt(pt, ct, tlen, &cbc);
48         (void)cbc_done(&cbc);
49         return err;
50 }
51
52 static unsigned long tom_decrypt(const void *key, const int keylen, void *iv,
53                         const void *ct, void *pt, const int tlen)
54 {
55         symmetric_CBC cbc;
56         int index, err;
57
58         if ((index = register_cipher(&aes_desc)) == -1)
59                 return CRYPT_INVALID_CIPHER;
60         if ((err = cbc_start(index, iv, key, keylen, 0, &cbc)) != CRYPT_OK)
61                 return err;
62         err= cbc_decrypt(ct, pt, tlen, &cbc);
63         (void)cbc_done(&cbc);
64         return err;
65 }
66
67 static unsigned long tom_hash(const void *pt, const int tlen,
68                         void *tag, int *taglen)
69 {
70         int index, rc;
71         unsigned long ltaglen = *taglen;
72
73         if ((index = register_hash(&sha1_desc)) == -1)
74                 return CRYPT_INVALID_HASH;
75         rc = hash_memory(index, pt, tlen, tag, &ltaglen);
76         *taglen = ltaglen;
77         return rc;
78 }
79
80 static unsigned long tom_hmac(const void *key, const int keylen,
81                         const void *pt, const int tlen,
82                         void *tag, int *taglen)
83 {
84         int index, rc;
85         unsigned long ltaglen = *taglen;
86
87         if ((index = register_hash(&sha1_desc)) == -1)
88                 return CRYPT_INVALID_HASH;
89         rc = hmac_memory(index, key, keylen, pt, tlen, tag, &ltaglen);
90         *taglen = ltaglen;
91         return rc;
92 }
93
94 static const char *tom_errstr(const unsigned long err)
95 {
96         return error_to_string((int)err);
97 }
98
99 struct crypto_interface tom_crypto_if = {
100         .init           = tom_init,
101         .encrypt        = tom_encrypt,
102         .decrypt        = tom_decrypt,
103         .hash           = tom_hash,
104         .hmac           = tom_hmac,
105         .errstr         = tom_errstr,
106 };