OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / libcrypto / libaes / aes_xcbc_mac.c
1 #ifdef __KERNEL__
2 #include <linux/types.h>
3 #include <linux/kernel.h>
4 #define DEBUG(x) 
5 #else
6 #include <stdio.h>
7 #include <sys/types.h>
8 #define DEBUG(x) x
9 #endif
10
11 #include "aes.h"
12 #include "aes_xcbc_mac.h"
13
14 int AES_xcbc_mac_set_key(aes_context_mac *ctxm, const u_int8_t *key, int keylen)
15 {
16         int ret=1;
17         aes_block kn[3] = { 
18                 { 0x01010101, 0x01010101, 0x01010101, 0x01010101 },
19                 { 0x02020202, 0x02020202, 0x02020202, 0x02020202 },
20                 { 0x03030303, 0x03030303, 0x03030303, 0x03030303 },
21         };
22         aes_set_key(&ctxm->ctx_k1, key, keylen, 0);
23         aes_encrypt(&ctxm->ctx_k1, (u_int8_t *) kn[0], (u_int8_t *) kn[0]);
24         aes_encrypt(&ctxm->ctx_k1, (u_int8_t *) kn[1], (u_int8_t *) ctxm->k2);
25         aes_encrypt(&ctxm->ctx_k1, (u_int8_t *) kn[2], (u_int8_t *) ctxm->k3);
26         aes_set_key(&ctxm->ctx_k1, (u_int8_t *) kn[0], 16, 0);
27         return ret;
28 }
29 static void do_pad_xor(u_int8_t *out, const u_int8_t *in, int len) {
30         int pos=0;
31         for (pos=1; pos <= 16; pos++, in++, out++) {
32                 if (pos <= len)
33                         *out ^= *in;
34                 if (pos > len) {
35                         DEBUG(printf("put 0x80 at pos=%d\n", pos));
36                         *out ^= 0x80;
37                         break;
38                 }
39         }
40 }
41 static void xor_block(aes_block res, const aes_block op) {
42         res[0] ^= op[0];
43         res[1] ^= op[1];
44         res[2] ^= op[2];
45         res[3] ^= op[3];
46 }
47 int AES_xcbc_mac_hash(const aes_context_mac *ctxm, const u_int8_t * in, int ilen, u_int8_t hash[16]) {
48         int ret=ilen;
49         u_int32_t out[4] = { 0, 0, 0, 0 }; 
50         for (; ilen > 16 ; ilen-=16) {
51                 xor_block(out, (const u_int32_t*) &in[0]);
52                 aes_encrypt(&ctxm->ctx_k1, in, (u_int8_t *)&out[0]);
53                 in+=16; 
54         }
55         do_pad_xor((u_int8_t *)&out, in, ilen);
56         if (ilen==16) {
57                 DEBUG(printf("using k3\n"));
58                 xor_block(out, ctxm->k3);
59         }
60         else 
61         {
62                 DEBUG(printf("using k2\n"));
63                 xor_block(out, ctxm->k2);
64         }
65         aes_encrypt(&ctxm->ctx_k1, (u_int8_t *)out, hash);
66         return ret;
67