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 lrw_setiv.c
15   LRW_MODE implementation, Set the current IV, Tom St Denis
16*/
17
18#ifdef LTC_LRW_MODE
19
20/**
21  Set the IV for LRW
22  @param IV      The IV, must be 16 octets
23  @param len     Length ... must be 16 :-)
24  @param lrw     The LRW state to update
25  @return CRYPT_OK if successful
26*/
27int lrw_setiv(const unsigned char *IV, unsigned long len, symmetric_LRW *lrw)
28{
29   int           err;
30#ifdef LRW_TABLES
31   unsigned char T[16];
32   int           x, y;
33#endif
34   LTC_ARGCHK(IV != NULL);
35   LTC_ARGCHK(lrw != NULL);
36
37   if (len != 16) {
38      return CRYPT_INVALID_ARG;
39   }
40
41   if ((err = cipher_is_valid(lrw->cipher)) != CRYPT_OK) {
42      return err;
43   }
44
45   /* copy the IV */
46   XMEMCPY(lrw->IV, IV, 16);
47
48   /* check if we have to actually do work */
49   if (cipher_descriptor[lrw->cipher].accel_lrw_encrypt != NULL && cipher_descriptor[lrw->cipher].accel_lrw_decrypt != NULL) {
50       /* we have accelerators, let's bail since they don't use lrw->pad anyways */
51       return CRYPT_OK;
52   }
53
54#ifdef LRW_TABLES
55   XMEMCPY(T, &lrw->PC[0][IV[0]][0], 16);
56   for (x = 1; x < 16; x++) {
57#ifdef LTC_FAST
58       for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {
59           *((LTC_FAST_TYPE *)(T + y)) ^= *((LTC_FAST_TYPE *)(&lrw->PC[x][IV[x]][y]));
60       }
61#else
62       for (y = 0; y < 16; y++) {
63           T[y] ^= lrw->PC[x][IV[x]][y];
64       }
65#endif
66   }
67   XMEMCPY(lrw->pad, T, 16);
68#else
69   gcm_gf_mult(lrw->tweak, IV, lrw->pad);
70#endif
71
72   return CRYPT_OK;
73}
74
75
76#endif
77/* $Source: /cvs/libtom/libtomcrypt/src/modes/lrw/lrw_setiv.c,v $ */
78/* $Revision: 1.12 $ */
79/* $Date: 2006/06/29 01:53:13 $ */
80