annotate src/mac/xcbc/xcbc_file.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 xcbc_file.c
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 XCBC support, process a file, 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_XCBC
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 XCBC a file
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 @param cipher The index of the cipher desired
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 @param key The secret key
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 @param keylen The length of the secret key (octets)
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 @param filename The name of the file you wish to XCBC
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 @param out [out] Where the authentication tag is to be stored
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 @param outlen [in/out] The max size and resulting size of the authentication tag
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 @return CRYPT_OK if successful, CRYPT_NOP if file support has been disabled
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 */
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 int xcbc_file(int cipher,
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 const unsigned char *key, unsigned long keylen,
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 const char *filename,
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 unsigned char *out, unsigned long *outlen)
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 {
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 #ifdef LTC_NO_FILE
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 return CRYPT_NOP;
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 #else
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 int err, x;
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 xcbc_state xcbc;
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 FILE *in;
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 unsigned char buf[512];
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 LTC_ARGCHK(key != NULL);
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 LTC_ARGCHK(filename != NULL);
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 LTC_ARGCHK(out != NULL);
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 LTC_ARGCHK(outlen != NULL);
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 in = fopen(filename, "rb");
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 if (in == NULL) {
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 return CRYPT_FILE_NOTFOUND;
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 }
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 if ((err = xcbc_init(&xcbc, cipher, key, keylen)) != CRYPT_OK) {
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 fclose(in);
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 return err;
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 }
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 do {
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 x = fread(buf, 1, sizeof(buf), in);
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 if ((err = xcbc_process(&xcbc, buf, x)) != CRYPT_OK) {
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 fclose(in);
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 return err;
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 }
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 } while (x == sizeof(buf));
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 fclose(in);
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 if ((err = xcbc_done(&xcbc, out, outlen)) != CRYPT_OK) {
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 return err;
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 }
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 #ifdef LTC_CLEAN_STACK
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 zeromem(buf, sizeof(buf));
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 #endif
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 return CRYPT_OK;
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 }
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 #endif
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 /* $Source: /cvs/libtom/libtomcrypt/src/mac/xcbc/xcbc_file.c,v $ */
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 /* $Revision: 1.1 $ */
d5faf4814ddb Update to LibTomCrypt 1.16
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 /* $Date: 2006/11/03 01:56:41 $ */