]> git.cryptolib.org Git - avr-crypto-lib.git/blob - noekeon/omac_noekeon_C.c
new and more compact aes
[avr-crypto-lib.git] / noekeon / omac_noekeon_C.c
1 #include "noekeon.h"
2 #include "omac_noekeon.h"
3 #include "memxor.h"
4 #include <string.h>
5 #include <stdint.h>
6
7
8 void omac_noekeon_init(omac_noekeon_ctx_t *ctx){
9         memset(ctx, 0, 16);
10 }
11
12
13 void omac_noekeon_tweak(uint8_t t, const void *key, omac_noekeon_ctx_t *ctx){
14         *ctx[15] = t;
15         noekeon_enc(ctx, key);
16 }
17
18 void omac_noekeon_next(const void *buffer, const void *key, omac_noekeon_ctx_t *ctx){
19         memxor(ctx, buffer, 16);
20         noekeon_enc(ctx, key);
21 }
22
23 static
24 void omac_noekeon_comppad(uint8_t *pad, const void *key, uint8_t length_b){
25         uint8_t c1,c2,r,j;
26         memset(pad, 0, 16);
27         noekeon_enc(pad, key);
28         r=(length_b==128)?1:2;
29         for(;r!=0;--r){
30                 c1=0;
31                 for(j=0;j<16;++j){
32                         c2 = c1;
33                         c1 = (pad[15-j])>>7;
34                         pad[15-j] = ((pad[15-j])<<1) | c2;
35                 }
36                 if(c1){
37                         pad[15] ^= 0x87;
38                 }
39         }
40         if(length_b<128){
41                 pad[(length_b)/8] ^= 0x80 >> (length_b%8);
42         }
43 }
44
45 void omac_noekeon_last(const void *buffer, uint8_t length_b, const void *key, omac_noekeon_ctx_t *ctx){
46         while(length_b>128){
47                 omac_noekeon_next(buffer, key, ctx);
48                 buffer = (uint8_t*)buffer +16;
49                 length_b -= 128;
50         }
51         uint8_t pad[16];
52         omac_noekeon_comppad(pad, key, length_b);
53         memxor(pad, buffer, (length_b+7)/8);
54         omac_noekeon_next(pad, key, ctx);
55 }
56
57
58 void omac_noekeon(void *dest, const void *msg, uint16_t msglength_b,
59                   const void *key, uint8_t t){
60         omac_noekeon_init(dest);
61         if(t!=0xff)
62                 omac_noekeon_tweak(t,key,dest);
63         while(msglength_b>128){
64                 omac_noekeon_next(msg, key, dest);
65                 msg = (uint8_t*)msg +16;
66                 msglength_b -= 128;
67         }
68         omac_noekeon_last(msg, msglength_b, key, dest);                         
69 }
70
71
72
73
74