annotate src/modes/lrw/lrw_setiv.c @ 380:d5faf4814ddb libtomcrypt-orig libtomcrypt-1.16

Update to LibTomCrypt 1.16
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Jan 2007 02:22:00 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
380
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 *
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 * LibTomCrypt is a library that provides various cryptographic
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 * algorithms in a highly modular and flexible manner.
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 *
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 * The library is free for all purposes without any express
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 * guarantee it works.
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 *
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 * Tom St Denis, [email protected], http://libtomcrypt.com
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 */
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 #include "tomcrypt.h"
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 /**
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 @file lrw_setiv.c
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 LRW_MODE implementation, Set the current IV, Tom St Denis
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 */
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 #ifdef LTC_LRW_MODE
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 /**
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 Set the IV for LRW
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 @param IV The IV, must be 16 octets
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 @param len Length ... must be 16 :-)
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 @param lrw The LRW state to update
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 @return CRYPT_OK if successful
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 */
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 int lrw_setiv(const unsigned char *IV, unsigned long len, symmetric_LRW *lrw)
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 {
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 int err;
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 #ifdef LRW_TABLES
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 unsigned char T[16];
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 int x, y;
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 #endif
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 LTC_ARGCHK(IV != NULL);
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 LTC_ARGCHK(lrw != NULL);
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 if (len != 16) {
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 return CRYPT_INVALID_ARG;
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 }
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 if ((err = cipher_is_valid(lrw->cipher)) != CRYPT_OK) {
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 return err;
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 }
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 /* copy the IV */
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 XMEMCPY(lrw->IV, IV, 16);
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 /* check if we have to actually do work */
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 if (cipher_descriptor[lrw->cipher].accel_lrw_encrypt != NULL && cipher_descriptor[lrw->cipher].accel_lrw_decrypt != NULL) {
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 /* we have accelerators, let's bail since they don't use lrw->pad anyways */
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 return CRYPT_OK;
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 }
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 #ifdef LRW_TABLES
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 XMEMCPY(T, &lrw->PC[0][IV[0]][0], 16);
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 for (x = 1; x < 16; x++) {
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 #ifdef LTC_FAST
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 *((LTC_FAST_TYPE *)(T + y)) ^= *((LTC_FAST_TYPE *)(&lrw->PC[x][IV[x]][y]));
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 }
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 #else
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 for (y = 0; y < 16; y++) {
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 T[y] ^= lrw->PC[x][IV[x]][y];
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 }
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 #endif
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 }
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 XMEMCPY(lrw->pad, T, 16);
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 #else
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 gcm_gf_mult(lrw->tweak, IV, lrw->pad);
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 #endif
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 return CRYPT_OK;
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 }
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 #endif
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 /* $Source: /cvs/libtom/libtomcrypt/src/modes/lrw/lrw_setiv.c,v $ */
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 /* $Revision: 1.12 $ */
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 /* $Date: 2006/06/29 01:53:13 $ */