1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
13 Pelican MAC, initialize state, by Tom St Denis
18 #define __LTC_AES_TAB_C__
21 #include "../../ciphers/aes/aes_tab.c"
24 Initialize a Pelican state
25 @param pelmac The Pelican state to initialize
26 @param key The secret key
27 @param keylen The length of the secret key (octets)
28 @return CRYPT_OK if successful
30 int pelican_init(pelican_state *pelmac, const unsigned char *key, unsigned long keylen)
34 LTC_ARGCHK(pelmac != NULL);
35 LTC_ARGCHK(key != NULL);
38 if (16 % sizeof(LTC_FAST_TYPE)) {
39 return CRYPT_INVALID_ARG;
43 if ((err = aes_setup(key, keylen, 0, &pelmac->K)) != CRYPT_OK) {
47 zeromem(pelmac->state, 16);
48 aes_ecb_encrypt(pelmac->state, pelmac->state, &pelmac->K);
54 static void _four_rounds(pelican_state *pelmac)
56 ulong32 s0, s1, s2, s3, t0, t1, t2, t3;
59 LOAD32H(s0, pelmac->state );
60 LOAD32H(s1, pelmac->state + 4);
61 LOAD32H(s2, pelmac->state + 8);
62 LOAD32H(s3, pelmac->state + 12);
63 for (r = 0; r < 4; r++) {
84 s0 = t0; s1 = t1; s2 = t2; s3 = t3;
86 STORE32H(s0, pelmac->state );
87 STORE32H(s1, pelmac->state + 4);
88 STORE32H(s2, pelmac->state + 8);
89 STORE32H(s3, pelmac->state + 12);
93 Process a block of text through Pelican
94 @param pelmac The Pelican MAC state
96 @param inlen The length input (octets)
97 @return CRYPT_OK on success
99 int pelican_process(pelican_state *pelmac, const unsigned char *in, unsigned long inlen)
102 LTC_ARGCHK(pelmac != NULL);
103 LTC_ARGCHK(in != NULL);
106 if (pelmac->buflen < 0 || pelmac->buflen > 15) {
107 return CRYPT_INVALID_ARG;
111 if (pelmac->buflen == 0) {
112 while (inlen & ~15) {
114 for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) {
115 *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pelmac->state + x)) ^= *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)in + x));
117 _four_rounds(pelmac);
125 pelmac->state[pelmac->buflen++] ^= *in++;
126 if (pelmac->buflen == 16) {
127 _four_rounds(pelmac);
135 Terminate Pelican MAC
136 @param pelmac The Pelican MAC state
137 @param out [out] The TAG
138 @return CRYPT_OK on sucess
140 int pelican_done(pelican_state *pelmac, unsigned char *out)
142 LTC_ARGCHK(pelmac != NULL);
143 LTC_ARGCHK(out != NULL);
146 if (pelmac->buflen < 0 || pelmac->buflen > 16) {
147 return CRYPT_INVALID_ARG;
150 if (pelmac->buflen == 16) {
151 _four_rounds(pelmac);
154 pelmac->state[pelmac->buflen++] ^= 0x80;
155 aes_ecb_encrypt(pelmac->state, out, &pelmac->K);
156 aes_done(&pelmac->K);
162 /* ref: $Format:%D$ */
163 /* git commit: $Format:%H$ */
164 /* commit time: $Format:%ai$ */