Mercurial > dropbear
comparison libtomcrypt/src/mac/xcbc/xcbc_init.c @ 1435:f849a5ca2efc
update to libtomcrypt 1.17 (with Dropbear changes)
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 24 Jun 2017 17:50:50 +0800 |
parents | 0cbe8f6dbf9e |
children | 6dba84798cd5 |
comparison
equal
deleted
inserted
replaced
1434:27b9ddb06b09 | 1435:f849a5ca2efc |
---|---|
4 * algorithms in a highly modular and flexible manner. | 4 * algorithms in a highly modular and flexible manner. |
5 * | 5 * |
6 * The library is free for all purposes without any express | 6 * The library is free for all purposes without any express |
7 * guarantee it works. | 7 * guarantee it works. |
8 * | 8 * |
9 * Tom St Denis, [email protected], http://libtomcrypt.com | 9 * Tom St Denis, [email protected], http://libtom.org |
10 */ | 10 */ |
11 #include "tomcrypt.h" | 11 #include "tomcrypt.h" |
12 | 12 |
13 /** | 13 /** |
14 @file xcbc_init.c | 14 @file xcbc_init.c |
26 */ | 26 */ |
27 int 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 { | 28 { |
29 int x, y, err; | 29 int x, y, err; |
30 symmetric_key *skey; | 30 symmetric_key *skey; |
31 unsigned long k1; | |
31 | 32 |
32 LTC_ARGCHK(xcbc != NULL); | 33 LTC_ARGCHK(xcbc != NULL); |
33 LTC_ARGCHK(key != NULL); | 34 LTC_ARGCHK(key != NULL); |
34 | 35 |
35 /* schedule the key */ | 36 /* schedule the key */ |
41 if (cipher_descriptor[cipher].block_length % sizeof(LTC_FAST_TYPE)) { | 42 if (cipher_descriptor[cipher].block_length % sizeof(LTC_FAST_TYPE)) { |
42 return CRYPT_INVALID_ARG; | 43 return CRYPT_INVALID_ARG; |
43 } | 44 } |
44 #endif | 45 #endif |
45 | 46 |
46 /* schedule the user key */ | 47 skey = NULL; |
47 skey = XCALLOC(1, sizeof(*skey)); | 48 |
48 if (skey == NULL) { | 49 /* are we in pure XCBC mode with three keys? */ |
49 return CRYPT_MEM; | 50 if (keylen & LTC_XCBC_PURE) { |
51 keylen &= ~LTC_XCBC_PURE; | |
52 | |
53 if (keylen < 2UL*cipher_descriptor[cipher].block_length) { | |
54 return CRYPT_INVALID_ARG; | |
55 } | |
56 | |
57 k1 = keylen - 2*cipher_descriptor[cipher].block_length; | |
58 XMEMCPY(xcbc->K[0], key, k1); | |
59 XMEMCPY(xcbc->K[1], key+k1, cipher_descriptor[cipher].block_length); | |
60 XMEMCPY(xcbc->K[2], key+k1 + cipher_descriptor[cipher].block_length, cipher_descriptor[cipher].block_length); | |
61 } else { | |
62 /* use the key expansion */ | |
63 k1 = cipher_descriptor[cipher].block_length; | |
64 | |
65 /* schedule the user key */ | |
66 skey = XCALLOC(1, sizeof(*skey)); | |
67 if (skey == NULL) { | |
68 return CRYPT_MEM; | |
69 } | |
70 | |
71 if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, skey)) != CRYPT_OK) { | |
72 goto done; | |
73 } | |
74 | |
75 /* make the three keys */ | |
76 for (y = 0; y < 3; y++) { | |
77 for (x = 0; x < cipher_descriptor[cipher].block_length; x++) { | |
78 xcbc->K[y][x] = y + 1; | |
79 } | |
80 cipher_descriptor[cipher].ecb_encrypt(xcbc->K[y], xcbc->K[y], skey); | |
81 } | |
50 } | 82 } |
51 | 83 |
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 */ | 84 /* setup K1 */ |
65 err = cipher_descriptor[cipher].setup(xcbc->K[0], cipher_descriptor[cipher].block_length, 0, &xcbc->key); | 85 err = cipher_descriptor[cipher].setup(xcbc->K[0], k1, 0, &xcbc->key); |
66 | 86 |
67 /* setup struct */ | 87 /* setup struct */ |
68 zeromem(xcbc->IV, cipher_descriptor[cipher].block_length); | 88 zeromem(xcbc->IV, cipher_descriptor[cipher].block_length); |
69 xcbc->blocksize = cipher_descriptor[cipher].block_length; | 89 xcbc->blocksize = cipher_descriptor[cipher].block_length; |
70 xcbc->cipher = cipher; | 90 xcbc->cipher = cipher; |
71 xcbc->buflen = 0; | 91 xcbc->buflen = 0; |
72 done: | 92 done: |
73 cipher_descriptor[cipher].done(skey); | 93 cipher_descriptor[cipher].done(skey); |
94 if (skey != NULL) { | |
74 #ifdef LTC_CLEAN_STACK | 95 #ifdef LTC_CLEAN_STACK |
75 zeromem(skey, sizeof(*skey)); | 96 zeromem(skey, sizeof(*skey)); |
76 #endif | 97 #endif |
77 XFREE(skey); | 98 XFREE(skey); |
99 } | |
78 return err; | 100 return err; |
79 } | 101 } |
80 | 102 |
81 #endif | 103 #endif |
82 | 104 |
83 /* $Source: /cvs/libtom/libtomcrypt/src/mac/xcbc/xcbc_init.c,v $ */ | 105 /* $Source$ */ |
84 /* $Revision: 1.4 $ */ | 106 /* $Revision$ */ |
85 /* $Date: 2006/11/07 03:23:46 $ */ | 107 /* $Date$ */ |
86 | 108 |