• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
10  */
11 #include "tomcrypt.h"
12 
13 /**
14   @file xcbc_init.c
15   XCBC Support, start an XCBC state
16 */
17 
18 #ifdef LTC_XCBC
19 
20 /** Initialize XCBC-MAC state
21   @param xcbc    [out] XCBC state to initialize
22   @param cipher  Index of cipher to use
23   @param key     [in]  Secret key
24   @param keylen  Length of secret key in octets
25   Return CRYPT_OK on success
26 */
xcbc_init(xcbc_state * xcbc,int cipher,const unsigned char * key,unsigned long keylen)27 int xcbc_init(xcbc_state *xcbc, int cipher, const unsigned char *key, unsigned long keylen)
28 {
29    int            x, y, err;
30    symmetric_key *skey;
31 
32    LTC_ARGCHK(xcbc != NULL);
33    LTC_ARGCHK(key  != NULL);
34 
35    /* schedule the key */
36    if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
37       return err;
38    }
39 
40 #ifdef LTC_FAST
41    if (cipher_descriptor[cipher].block_length % sizeof(LTC_FAST_TYPE)) {
42        return CRYPT_INVALID_ARG;
43    }
44 #endif
45 
46    /* schedule the user key */
47    skey = XCALLOC(1, sizeof(*skey));
48    if (skey == NULL) {
49       return CRYPT_MEM;
50    }
51 
52    if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, skey)) != CRYPT_OK) {
53       goto done;
54    }
55 
56    /* make the three keys */
57    for (y = 0; y < 3; y++) {
58      for (x = 0; x < cipher_descriptor[cipher].block_length; x++) {
59         xcbc->K[y][x] = y + 1;
60      }
61      cipher_descriptor[cipher].ecb_encrypt(xcbc->K[y], xcbc->K[y], skey);
62    }
63 
64    /* setup K1 */
65    err = cipher_descriptor[cipher].setup(xcbc->K[0], cipher_descriptor[cipher].block_length, 0, &xcbc->key);
66 
67    /* setup struct */
68    zeromem(xcbc->IV, cipher_descriptor[cipher].block_length);
69    xcbc->blocksize = cipher_descriptor[cipher].block_length;
70    xcbc->cipher    = cipher;
71    xcbc->buflen    = 0;
72 done:
73    cipher_descriptor[cipher].done(skey);
74 #ifdef LTC_CLEAN_STACK
75    zeromem(skey, sizeof(*skey));
76 #endif
77    XFREE(skey);
78    return err;
79 }
80 
81 #endif
82 
83 /* $Source: /cvs/libtom/libtomcrypt/src/mac/xcbc/xcbc_init.c,v $ */
84 /* $Revision: 1.4 $ */
85 /* $Date: 2006/11/07 03:23:46 $ */
86 
87