Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/tomcrypt/src/mac/omac/omac_init.c
5972 views
1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2
*
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
5
*
6
* The library is free for all purposes without any express
7
* guarantee it works.
8
*/
9
#include "tomcrypt.h"
10
11
/**
12
@file omac_init.c
13
OMAC1 support, initialize state, by Tom St Denis
14
*/
15
16
17
#ifdef LTC_OMAC
18
19
/**
20
Initialize an OMAC state
21
@param omac The OMAC state to initialize
22
@param cipher The index of the desired cipher
23
@param key The secret key
24
@param keylen The length of the secret key (octets)
25
@return CRYPT_OK if successful
26
*/
27
int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned long keylen)
28
{
29
int err, x, y, mask, msb, len;
30
31
LTC_ARGCHK(omac != NULL);
32
LTC_ARGCHK(key != NULL);
33
34
/* schedule the key */
35
if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
36
return err;
37
}
38
39
#ifdef LTC_FAST
40
if (cipher_descriptor[cipher].block_length % sizeof(LTC_FAST_TYPE)) {
41
return CRYPT_INVALID_ARG;
42
}
43
#endif
44
45
/* now setup the system */
46
switch (cipher_descriptor[cipher].block_length) {
47
case 8: mask = 0x1B;
48
len = 8;
49
break;
50
case 16: mask = 0x87;
51
len = 16;
52
break;
53
default: return CRYPT_INVALID_ARG;
54
}
55
56
if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &omac->key)) != CRYPT_OK) {
57
return err;
58
}
59
60
/* ok now we need Lu and Lu^2 [calc one from the other] */
61
62
/* first calc L which is Ek(0) */
63
zeromem(omac->Lu[0], cipher_descriptor[cipher].block_length);
64
if ((err = cipher_descriptor[cipher].ecb_encrypt(omac->Lu[0], omac->Lu[0], &omac->key)) != CRYPT_OK) {
65
return err;
66
}
67
68
/* now do the mults, whoopy! */
69
for (x = 0; x < 2; x++) {
70
/* if msb(L * u^(x+1)) = 0 then just shift, otherwise shift and xor constant mask */
71
msb = omac->Lu[x][0] >> 7;
72
73
/* shift left */
74
for (y = 0; y < (len - 1); y++) {
75
omac->Lu[x][y] = ((omac->Lu[x][y] << 1) | (omac->Lu[x][y+1] >> 7)) & 255;
76
}
77
omac->Lu[x][len - 1] = ((omac->Lu[x][len - 1] << 1) ^ (msb ? mask : 0)) & 255;
78
79
/* copy up as require */
80
if (x == 0) {
81
XMEMCPY(omac->Lu[1], omac->Lu[0], sizeof(omac->Lu[0]));
82
}
83
}
84
85
/* setup state */
86
omac->cipher_idx = cipher;
87
omac->buflen = 0;
88
omac->blklen = len;
89
zeromem(omac->prev, sizeof(omac->prev));
90
zeromem(omac->block, sizeof(omac->block));
91
92
return CRYPT_OK;
93
}
94
95
#endif
96
97