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 OMAC1 support, initialize state, by Tom St Denis
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
27 int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned long keylen)
29 int err, x, y, mask, msb, len;
31 LTC_ARGCHK(omac != NULL);
32 LTC_ARGCHK(key != NULL);
34 /* schedule the key */
35 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
40 if (cipher_descriptor[cipher].block_length % sizeof(LTC_FAST_TYPE)) {
41 return CRYPT_INVALID_ARG;
45 /* now setup the system */
46 switch (cipher_descriptor[cipher].block_length) {
53 default: return CRYPT_INVALID_ARG;
56 if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &omac->key)) != CRYPT_OK) {
60 /* ok now we need Lu and Lu^2 [calc one from the other] */
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) {
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;
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;
77 omac->Lu[x][len - 1] = ((omac->Lu[x][len - 1] << 1) ^ (msb ? mask : 0)) & 255;
79 /* copy up as require */
81 XMEMCPY(omac->Lu[1], omac->Lu[0], sizeof(omac->Lu[0]));
86 omac->cipher_idx = cipher;
89 zeromem(omac->prev, sizeof(omac->prev));
90 zeromem(omac->block, sizeof(omac->block));
97 /* ref: $Format:%D$ */
98 /* git commit: $Format:%H$ */
99 /* commit time: $Format:%ai$ */