Path: blob/master/libs/tomcrypt/src/mac/omac/omac_init.c
5972 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/8#include "tomcrypt.h"910/**11@file omac_init.c12OMAC1 support, initialize state, by Tom St Denis13*/141516#ifdef LTC_OMAC1718/**19Initialize an OMAC state20@param omac The OMAC state to initialize21@param cipher The index of the desired cipher22@param key The secret key23@param keylen The length of the secret key (octets)24@return CRYPT_OK if successful25*/26int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned long keylen)27{28int err, x, y, mask, msb, len;2930LTC_ARGCHK(omac != NULL);31LTC_ARGCHK(key != NULL);3233/* schedule the key */34if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {35return err;36}3738#ifdef LTC_FAST39if (cipher_descriptor[cipher].block_length % sizeof(LTC_FAST_TYPE)) {40return CRYPT_INVALID_ARG;41}42#endif4344/* now setup the system */45switch (cipher_descriptor[cipher].block_length) {46case 8: mask = 0x1B;47len = 8;48break;49case 16: mask = 0x87;50len = 16;51break;52default: return CRYPT_INVALID_ARG;53}5455if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &omac->key)) != CRYPT_OK) {56return err;57}5859/* ok now we need Lu and Lu^2 [calc one from the other] */6061/* first calc L which is Ek(0) */62zeromem(omac->Lu[0], cipher_descriptor[cipher].block_length);63if ((err = cipher_descriptor[cipher].ecb_encrypt(omac->Lu[0], omac->Lu[0], &omac->key)) != CRYPT_OK) {64return err;65}6667/* now do the mults, whoopy! */68for (x = 0; x < 2; x++) {69/* if msb(L * u^(x+1)) = 0 then just shift, otherwise shift and xor constant mask */70msb = omac->Lu[x][0] >> 7;7172/* shift left */73for (y = 0; y < (len - 1); y++) {74omac->Lu[x][y] = ((omac->Lu[x][y] << 1) | (omac->Lu[x][y+1] >> 7)) & 255;75}76omac->Lu[x][len - 1] = ((omac->Lu[x][len - 1] << 1) ^ (msb ? mask : 0)) & 255;7778/* copy up as require */79if (x == 0) {80XMEMCPY(omac->Lu[1], omac->Lu[0], sizeof(omac->Lu[0]));81}82}8384/* setup state */85omac->cipher_idx = cipher;86omac->buflen = 0;87omac->blklen = len;88zeromem(omac->prev, sizeof(omac->prev));89zeromem(omac->block, sizeof(omac->block));9091return CRYPT_OK;92}9394#endif959697