Mercurial > dropbear
comparison libtomcrypt/src/mac/pelican/pelican.c @ 1471:6dba84798cd5
Update to libtomcrypt 1.18.1, merged with Dropbear changes
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 09 Feb 2018 21:44:05 +0800 |
parents | f849a5ca2efc |
children |
comparison
equal
deleted
inserted
replaced
1470:8bba51a55704 | 1471:6dba84798cd5 |
---|---|
3 * LibTomCrypt is a library that provides various cryptographic | 3 * LibTomCrypt is a library that provides various cryptographic |
4 * algorithms in a highly modular and flexible manner. | 4 * algorithms in a highly modular and flexible manner. |
5 * | 5 * |
6 * The library is free for all purposes without any express | 6 * The library is free for all purposes without any express |
7 * guarantee it works. | 7 * guarantee it works. |
8 * | |
9 * Tom St Denis, [email protected], http://libtom.org | |
10 */ | 8 */ |
11 #include "tomcrypt.h" | 9 #include "tomcrypt.h" |
12 | 10 |
13 /** | 11 /** |
14 @file pelican.c | 12 @file pelican.c |
15 Pelican MAC, initialize state, by Tom St Denis | 13 Pelican MAC, initialize state, by Tom St Denis |
16 */ | 14 */ |
17 | 15 |
18 #ifdef LTC_PELICAN | 16 #ifdef LTC_PELICAN |
19 | 17 |
18 #define __LTC_AES_TAB_C__ | |
20 #define ENCRYPT_ONLY | 19 #define ENCRYPT_ONLY |
21 #define PELI_TAB | 20 #define PELI_TAB |
22 #include "../../ciphers/aes/aes_tab.c" | 21 #include "../../ciphers/aes/aes_tab.c" |
23 | 22 |
24 /** | 23 /** |
25 Initialize a Pelican state | 24 Initialize a Pelican state |
26 @param pelmac The Pelican state to initialize | 25 @param pelmac The Pelican state to initialize |
27 @param key The secret key | 26 @param key The secret key |
28 @param keylen The length of the secret key (octets) | 27 @param keylen The length of the secret key (octets) |
29 @return CRYPT_OK if successful | 28 @return CRYPT_OK if successful |
30 */ | 29 */ |
31 int pelican_init(pelican_state *pelmac, const unsigned char *key, unsigned long keylen) | 30 int pelican_init(pelican_state *pelmac, const unsigned char *key, unsigned long keylen) |
32 { | 31 { |
33 int err; | 32 int err; |
34 | 33 |
35 LTC_ARGCHK(pelmac != NULL); | 34 LTC_ARGCHK(pelmac != NULL); |
36 LTC_ARGCHK(key != NULL); | 35 LTC_ARGCHK(key != NULL); |
37 | 36 |
38 #ifdef LTC_FAST | 37 #ifdef LTC_FAST |
39 if (16 % sizeof(LTC_FAST_TYPE)) { | 38 if (16 % sizeof(LTC_FAST_TYPE)) { |
47 | 46 |
48 zeromem(pelmac->state, 16); | 47 zeromem(pelmac->state, 16); |
49 aes_ecb_encrypt(pelmac->state, pelmac->state, &pelmac->K); | 48 aes_ecb_encrypt(pelmac->state, pelmac->state, &pelmac->K); |
50 pelmac->buflen = 0; | 49 pelmac->buflen = 0; |
51 | 50 |
52 return CRYPT_OK; | 51 return CRYPT_OK; |
53 } | 52 } |
54 | 53 |
55 static void four_rounds(pelican_state *pelmac) | 54 static void _four_rounds(pelican_state *pelmac) |
56 { | 55 { |
57 ulong32 s0, s1, s2, s3, t0, t1, t2, t3; | 56 ulong32 s0, s1, s2, s3, t0, t1, t2, t3; |
58 int r; | 57 int r; |
59 | 58 |
60 LOAD32H(s0, pelmac->state ); | 59 LOAD32H(s0, pelmac->state ); |
88 STORE32H(s1, pelmac->state + 4); | 87 STORE32H(s1, pelmac->state + 4); |
89 STORE32H(s2, pelmac->state + 8); | 88 STORE32H(s2, pelmac->state + 8); |
90 STORE32H(s3, pelmac->state + 12); | 89 STORE32H(s3, pelmac->state + 12); |
91 } | 90 } |
92 | 91 |
93 /** | 92 /** |
94 Process a block of text through Pelican | 93 Process a block of text through Pelican |
95 @param pelmac The Pelican MAC state | 94 @param pelmac The Pelican MAC state |
96 @param in The input | 95 @param in The input |
97 @param inlen The length input (octets) | 96 @param inlen The length input (octets) |
98 @return CRYPT_OK on success | 97 @return CRYPT_OK on success |
111 #ifdef LTC_FAST | 110 #ifdef LTC_FAST |
112 if (pelmac->buflen == 0) { | 111 if (pelmac->buflen == 0) { |
113 while (inlen & ~15) { | 112 while (inlen & ~15) { |
114 int x; | 113 int x; |
115 for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) { | 114 for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) { |
116 *((LTC_FAST_TYPE*)((unsigned char *)pelmac->state + x)) ^= *((LTC_FAST_TYPE*)((unsigned char *)in + x)); | 115 *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pelmac->state + x)) ^= *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)in + x)); |
117 } | 116 } |
118 four_rounds(pelmac); | 117 _four_rounds(pelmac); |
119 in += 16; | 118 in += 16; |
120 inlen -= 16; | 119 inlen -= 16; |
121 } | 120 } |
122 } | 121 } |
123 #endif | 122 #endif |
124 | 123 |
125 while (inlen--) { | 124 while (inlen--) { |
126 pelmac->state[pelmac->buflen++] ^= *in++; | 125 pelmac->state[pelmac->buflen++] ^= *in++; |
127 if (pelmac->buflen == 16) { | 126 if (pelmac->buflen == 16) { |
128 four_rounds(pelmac); | 127 _four_rounds(pelmac); |
129 pelmac->buflen = 0; | 128 pelmac->buflen = 0; |
130 } | 129 } |
131 } | 130 } |
132 return CRYPT_OK; | 131 return CRYPT_OK; |
133 } | 132 } |
147 if (pelmac->buflen < 0 || pelmac->buflen > 16) { | 146 if (pelmac->buflen < 0 || pelmac->buflen > 16) { |
148 return CRYPT_INVALID_ARG; | 147 return CRYPT_INVALID_ARG; |
149 } | 148 } |
150 | 149 |
151 if (pelmac->buflen == 16) { | 150 if (pelmac->buflen == 16) { |
152 four_rounds(pelmac); | 151 _four_rounds(pelmac); |
153 pelmac->buflen = 0; | 152 pelmac->buflen = 0; |
154 } | 153 } |
155 pelmac->state[pelmac->buflen++] ^= 0x80; | 154 pelmac->state[pelmac->buflen++] ^= 0x80; |
156 aes_ecb_encrypt(pelmac->state, out, &pelmac->K); | 155 aes_ecb_encrypt(pelmac->state, out, &pelmac->K); |
157 aes_done(&pelmac->K); | 156 aes_done(&pelmac->K); |
158 return CRYPT_OK; | 157 return CRYPT_OK; |
159 } | 158 } |
160 | 159 |
161 #endif | 160 #endif |
162 | 161 |
163 /* $Source$ */ | 162 /* ref: $Format:%D$ */ |
164 /* $Revision$ */ | 163 /* git commit: $Format:%H$ */ |
165 /* $Date$ */ | 164 /* commit time: $Format:%ai$ */ |