diff options
Diffstat (limited to 'libtomcrypt/src/mac/pmac')
-rw-r--r-- | libtomcrypt/src/mac/pmac/pmac_done.c | 12 | ||||
-rw-r--r-- | libtomcrypt/src/mac/pmac/pmac_file.c | 62 | ||||
-rw-r--r-- | libtomcrypt/src/mac/pmac/pmac_init.c | 71 | ||||
-rw-r--r-- | libtomcrypt/src/mac/pmac/pmac_memory.c | 18 | ||||
-rw-r--r-- | libtomcrypt/src/mac/pmac/pmac_memory_multi.c | 20 | ||||
-rw-r--r-- | libtomcrypt/src/mac/pmac/pmac_ntz.c | 12 | ||||
-rw-r--r-- | libtomcrypt/src/mac/pmac/pmac_process.c | 18 | ||||
-rw-r--r-- | libtomcrypt/src/mac/pmac/pmac_shift_xor.c | 16 | ||||
-rw-r--r-- | libtomcrypt/src/mac/pmac/pmac_test.c | 37 |
9 files changed, 126 insertions, 140 deletions
diff --git a/libtomcrypt/src/mac/pmac/pmac_done.c b/libtomcrypt/src/mac/pmac/pmac_done.c index 88076c6..de7a5aa 100644 --- a/libtomcrypt/src/mac/pmac/pmac_done.c +++ b/libtomcrypt/src/mac/pmac/pmac_done.c @@ -5,14 +5,12 @@ * * The library is free for all purposes without any express * guarantee it works. - * - * Tom St Denis, tomstdenis@gmail.com, http://libtom.org */ #include "tomcrypt.h" -/** +/** @file pmac_done.c - PMAC implementation, terminate a session, by Tom St Denis + PMAC implementation, terminate a session, by Tom St Denis */ #ifdef LTC_PMAC @@ -69,6 +67,6 @@ int pmac_done(pmac_state *state, unsigned char *out, unsigned long *outlen) #endif -/* $Source$ */ -/* $Revision$ */ -/* $Date$ */ +/* ref: $Format:%D$ */ +/* git commit: $Format:%H$ */ +/* commit time: $Format:%ai$ */ diff --git a/libtomcrypt/src/mac/pmac/pmac_file.c b/libtomcrypt/src/mac/pmac/pmac_file.c index c7a9f74..abe04f1 100644 --- a/libtomcrypt/src/mac/pmac/pmac_file.c +++ b/libtomcrypt/src/mac/pmac/pmac_file.c @@ -5,20 +5,18 @@ * * The library is free for all purposes without any express * guarantee it works. - * - * Tom St Denis, tomstdenis@gmail.com, http://libtom.org */ #include "tomcrypt.h" -/** +/** @file pmac_file.c - PMAC implementation, process a file, by Tom St Denis + PMAC implementation, process a file, by Tom St Denis */ #ifdef LTC_PMAC /** - PMAC a file + PMAC a file @param cipher The index of the cipher desired @param key The secret key @param keylen The length of the secret key (octets) @@ -27,18 +25,19 @@ @param outlen [in/out] Max size and resulting size of the authentication tag @return CRYPT_OK if successful, CRYPT_NOP if file support has been disabled */ -int pmac_file(int cipher, +int pmac_file(int cipher, const unsigned char *key, unsigned long keylen, - const char *filename, + const char *filename, unsigned char *out, unsigned long *outlen) { #ifdef LTC_NO_FILE return CRYPT_NOP; #else - int err, x; + size_t x; + int err; pmac_state pmac; FILE *in; - unsigned char buf[512]; + unsigned char *buf; LTC_ARGCHK(key != NULL); @@ -46,39 +45,48 @@ int pmac_file(int cipher, LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); - in = fopen(filename, "rb"); - if (in == NULL) { - return CRYPT_FILE_NOTFOUND; + if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) { + return CRYPT_MEM; } if ((err = pmac_init(&pmac, cipher, key, keylen)) != CRYPT_OK) { - fclose(in); - return err; + goto LBL_ERR; + } + + in = fopen(filename, "rb"); + if (in == NULL) { + err = CRYPT_FILE_NOTFOUND; + goto LBL_ERR; } do { - x = fread(buf, 1, sizeof(buf), in); - if ((err = pmac_process(&pmac, buf, x)) != CRYPT_OK) { + x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in); + if ((err = pmac_process(&pmac, buf, (unsigned long)x)) != CRYPT_OK) { fclose(in); - return err; + goto LBL_CLEANBUF; } - } while (x == sizeof(buf)); - fclose(in); + } while (x == LTC_FILE_READ_BUFSIZE); - if ((err = pmac_done(&pmac, out, outlen)) != CRYPT_OK) { - return err; + if (fclose(in) != 0) { + err = CRYPT_ERROR; + goto LBL_CLEANBUF; } + err = pmac_done(&pmac, out, outlen); + +LBL_CLEANBUF: + zeromem(buf, LTC_FILE_READ_BUFSIZE); +LBL_ERR: #ifdef LTC_CLEAN_STACK - zeromem(buf, sizeof(buf)); + zeromem(&pmac, sizeof(pmac_state)); #endif - - return CRYPT_OK; + XFREE(buf); + return err; #endif } #endif -/* $Source$ */ -/* $Revision$ */ -/* $Date$ */ +/* ref: $Format:%D$ */ +/* git commit: $Format:%H$ */ +/* commit time: $Format:%ai$ */ diff --git a/libtomcrypt/src/mac/pmac/pmac_init.c b/libtomcrypt/src/mac/pmac/pmac_init.c index e4cf571..b1bb400 100644 --- a/libtomcrypt/src/mac/pmac/pmac_init.c +++ b/libtomcrypt/src/mac/pmac/pmac_init.c @@ -5,21 +5,19 @@ * * The library is free for all purposes without any express * guarantee it works. - * - * Tom St Denis, tomstdenis@gmail.com, http://libtom.org */ #include "tomcrypt.h" -/** +/** @file pmac_init.c - PMAC implementation, initialize state, by Tom St Denis + PMAC implementation, initialize state, by Tom St Denis */ #ifdef LTC_PMAC static const struct { int len; - unsigned char poly_div[MAXBLOCKSIZE], + unsigned char poly_div[MAXBLOCKSIZE], poly_mul[MAXBLOCKSIZE]; } polys[] = { { @@ -27,7 +25,7 @@ static const struct { { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D }, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1B } }, { - 16, + 16, { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x43 }, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -39,7 +37,7 @@ static const struct { Initialize a PMAC state @param pmac The PMAC state to initialize @param cipher The index of the desired cipher - @param key The secret key + @param key The secret key @param keylen The length of the secret key (octets) @return CRYPT_OK if successful */ @@ -59,10 +57,13 @@ int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned l /* determine which polys to use */ pmac->block_len = cipher_descriptor[cipher].block_length; for (poly = 0; poly < (int)(sizeof(polys)/sizeof(polys[0])); poly++) { - if (polys[poly].len == pmac->block_len) { + if (polys[poly].len == pmac->block_len) { break; } } + if (poly >= (int)(sizeof(polys)/sizeof(polys[0]))) { + return CRYPT_INVALID_ARG; + } if (polys[poly].len != pmac->block_len) { return CRYPT_INVALID_ARG; } @@ -78,7 +79,7 @@ int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned l if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &pmac->key)) != CRYPT_OK) { return err; } - + /* allocate L */ L = XMALLOC(pmac->block_len); if (L == NULL) { @@ -107,41 +108,41 @@ int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned l } } - /* find Lr = L / x */ - m = L[pmac->block_len-1] & 1; + /* find Lr = L / x */ + m = L[pmac->block_len-1] & 1; - /* shift right */ - for (x = pmac->block_len - 1; x > 0; x--) { - pmac->Lr[x] = ((L[x] >> 1) | (L[x-1] << 7)) & 255; - } - pmac->Lr[0] = L[0] >> 1; + /* shift right */ + for (x = pmac->block_len - 1; x > 0; x--) { + pmac->Lr[x] = ((L[x] >> 1) | (L[x-1] << 7)) & 255; + } + pmac->Lr[0] = L[0] >> 1; - if (m == 1) { - for (x = 0; x < pmac->block_len; x++) { - pmac->Lr[x] ^= polys[poly].poly_div[x]; - } - } + if (m == 1) { + for (x = 0; x < pmac->block_len; x++) { + pmac->Lr[x] ^= polys[poly].poly_div[x]; + } + } - /* zero buffer, counters, etc... */ - pmac->block_index = 1; - pmac->cipher_idx = cipher; - pmac->buflen = 0; - zeromem(pmac->block, sizeof(pmac->block)); - zeromem(pmac->Li, sizeof(pmac->Li)); - zeromem(pmac->checksum, sizeof(pmac->checksum)); - err = CRYPT_OK; + /* zero buffer, counters, etc... */ + pmac->block_index = 1; + pmac->cipher_idx = cipher; + pmac->buflen = 0; + zeromem(pmac->block, sizeof(pmac->block)); + zeromem(pmac->Li, sizeof(pmac->Li)); + zeromem(pmac->checksum, sizeof(pmac->checksum)); + err = CRYPT_OK; error: #ifdef LTC_CLEAN_STACK - zeromem(L, pmac->block_len); + zeromem(L, pmac->block_len); #endif - XFREE(L); + XFREE(L); - return err; + return err; } #endif -/* $Source$ */ -/* $Revision$ */ -/* $Date$ */ +/* ref: $Format:%D$ */ +/* git commit: $Format:%H$ */ +/* commit time: $Format:%ai$ */ diff --git a/libtomcrypt/src/mac/pmac/pmac_memory.c b/libtomcrypt/src/mac/pmac/pmac_memory.c index 70b9616..7842781 100644 --- a/libtomcrypt/src/mac/pmac/pmac_memory.c +++ b/libtomcrypt/src/mac/pmac/pmac_memory.c @@ -5,14 +5,12 @@ * * The library is free for all purposes without any express * guarantee it works. - * - * Tom St Denis, tomstdenis@gmail.com, http://libtom.org */ #include "tomcrypt.h" -/** +/** @file pmac_memory.c - PMAC implementation, process a block of memory, by Tom St Denis + PMAC implementation, process a block of memory, by Tom St Denis */ #ifdef LTC_PMAC @@ -28,7 +26,7 @@ @param outlen [in/out] The max size and resulting size of the authentication tag @return CRYPT_OK if successful */ -int pmac_memory(int cipher, +int pmac_memory(int cipher, const unsigned char *key, unsigned long keylen, const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen) @@ -46,7 +44,7 @@ int pmac_memory(int cipher, if (pmac == NULL) { return CRYPT_MEM; } - + if ((err = pmac_init(pmac, cipher, key, keylen)) != CRYPT_OK) { goto LBL_ERR; } @@ -64,11 +62,11 @@ LBL_ERR: #endif XFREE(pmac); - return err; + return err; } #endif -/* $Source$ */ -/* $Revision$ */ -/* $Date$ */ +/* ref: $Format:%D$ */ +/* git commit: $Format:%H$ */ +/* commit time: $Format:%ai$ */ diff --git a/libtomcrypt/src/mac/pmac/pmac_memory_multi.c b/libtomcrypt/src/mac/pmac/pmac_memory_multi.c index 36783d3..f3de4b5 100644 --- a/libtomcrypt/src/mac/pmac/pmac_memory_multi.c +++ b/libtomcrypt/src/mac/pmac/pmac_memory_multi.c @@ -5,15 +5,13 @@ * * The library is free for all purposes without any express * guarantee it works. - * - * Tom St Denis, tomstdenis@gmail.com, http://libtom.org */ #include "tomcrypt.h" #include <stdarg.h> -/** +/** @file pmac_memory_multi.c - PMAC implementation, process multiple blocks of memory, by Tom St Denis + PMAC implementation, process multiple blocks of memory, by Tom St Denis */ #ifdef LTC_PMAC @@ -30,7 +28,7 @@ @param ... tuples of (data,len) pairs to PMAC, terminated with a (NULL,x) (x=don't care) @return CRYPT_OK if successful */ -int pmac_memory_multi(int cipher, +int pmac_memory_multi(int cipher, const unsigned char *key, unsigned long keylen, unsigned char *out, unsigned long *outlen, const unsigned char *in, unsigned long inlen, ...) @@ -51,12 +49,12 @@ int pmac_memory_multi(int cipher, if (pmac == NULL) { return CRYPT_MEM; } - + if ((err = pmac_init(pmac, cipher, key, keylen)) != CRYPT_OK) { goto LBL_ERR; } va_start(args, inlen); - curptr = in; + curptr = in; curlen = inlen; for (;;) { /* process buf */ @@ -79,11 +77,11 @@ LBL_ERR: #endif XFREE(pmac); va_end(args); - return err; + return err; } #endif -/* $Source$ */ -/* $Revision$ */ -/* $Date$ */ +/* ref: $Format:%D$ */ +/* git commit: $Format:%H$ */ +/* commit time: $Format:%ai$ */ diff --git a/libtomcrypt/src/mac/pmac/pmac_ntz.c b/libtomcrypt/src/mac/pmac/pmac_ntz.c index b5137da..2c7dec5 100644 --- a/libtomcrypt/src/mac/pmac/pmac_ntz.c +++ b/libtomcrypt/src/mac/pmac/pmac_ntz.c @@ -5,14 +5,12 @@ * * The library is free for all purposes without any express * guarantee it works. - * - * Tom St Denis, tomstdenis@gmail.com, http://libtom.org */ #include "tomcrypt.h" -/** +/** @file pmac_ntz.c - PMAC implementation, internal function, by Tom St Denis + PMAC implementation, internal function, by Tom St Denis */ #ifdef LTC_PMAC @@ -34,6 +32,6 @@ int pmac_ntz(unsigned long x) #endif -/* $Source$ */ -/* $Revision$ */ -/* $Date$ */ +/* ref: $Format:%D$ */ +/* git commit: $Format:%H$ */ +/* commit time: $Format:%ai$ */ diff --git a/libtomcrypt/src/mac/pmac/pmac_process.c b/libtomcrypt/src/mac/pmac/pmac_process.c index e32e65f..018fa27 100644 --- a/libtomcrypt/src/mac/pmac/pmac_process.c +++ b/libtomcrypt/src/mac/pmac/pmac_process.c @@ -5,14 +5,12 @@ * * The library is free for all purposes without any express * guarantee it works. - * - * Tom St Denis, tomstdenis@gmail.com, http://libtom.org */ #include "tomcrypt.h" -/** +/** @file pmac_process.c - PMAC implementation, process data, by Tom St Denis + PMAC implementation, process data, by Tom St Denis */ @@ -48,13 +46,13 @@ int pmac_process(pmac_state *pmac, const unsigned char *in, unsigned long inlen) for (x = 0; x < (inlen - 16); x += 16) { pmac_shift_xor(pmac); for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) { - *((LTC_FAST_TYPE*)(&Z[y])) = *((LTC_FAST_TYPE*)(&in[y])) ^ *((LTC_FAST_TYPE*)(&pmac->Li[y])); + *(LTC_FAST_TYPE_PTR_CAST(&Z[y])) = *(LTC_FAST_TYPE_PTR_CAST(&in[y])) ^ *(LTC_FAST_TYPE_PTR_CAST(&pmac->Li[y])); } if ((err = cipher_descriptor[pmac->cipher_idx].ecb_encrypt(Z, Z, &pmac->key)) != CRYPT_OK) { return err; } for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) { - *((LTC_FAST_TYPE*)(&pmac->checksum[y])) ^= *((LTC_FAST_TYPE*)(&Z[y])); + *(LTC_FAST_TYPE_PTR_CAST(&pmac->checksum[y])) ^= *(LTC_FAST_TYPE_PTR_CAST(&Z[y])); } in += 16; } @@ -62,7 +60,7 @@ int pmac_process(pmac_state *pmac, const unsigned char *in, unsigned long inlen) } #endif - while (inlen != 0) { + while (inlen != 0) { /* ok if the block is full we xor in prev, encrypt and replace prev */ if (pmac->buflen == pmac->block_len) { pmac_shift_xor(pmac); @@ -95,6 +93,6 @@ int pmac_process(pmac_state *pmac, const unsigned char *in, unsigned long inlen) #endif -/* $Source$ */ -/* $Revision$ */ -/* $Date$ */ +/* ref: $Format:%D$ */ +/* git commit: $Format:%H$ */ +/* commit time: $Format:%ai$ */ diff --git a/libtomcrypt/src/mac/pmac/pmac_shift_xor.c b/libtomcrypt/src/mac/pmac/pmac_shift_xor.c index 122cadb..49d48f9 100644 --- a/libtomcrypt/src/mac/pmac/pmac_shift_xor.c +++ b/libtomcrypt/src/mac/pmac/pmac_shift_xor.c @@ -5,14 +5,12 @@ * * The library is free for all purposes without any express * guarantee it works. - * - * Tom St Denis, tomstdenis@gmail.com, http://libtom.org */ #include "tomcrypt.h" -/** +/** @file pmac_shift_xor.c - PMAC implementation, internal function, by Tom St Denis + PMAC implementation, internal function, by Tom St Denis */ #ifdef LTC_PMAC @@ -27,8 +25,8 @@ void pmac_shift_xor(pmac_state *pmac) y = pmac_ntz(pmac->block_index++); #ifdef LTC_FAST for (x = 0; x < pmac->block_len; x += sizeof(LTC_FAST_TYPE)) { - *((LTC_FAST_TYPE*)((unsigned char *)pmac->Li + x)) ^= - *((LTC_FAST_TYPE*)((unsigned char *)pmac->Ls[y] + x)); + *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pmac->Li + x)) ^= + *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pmac->Ls[y] + x)); } #else for (x = 0; x < pmac->block_len; x++) { @@ -39,6 +37,6 @@ void pmac_shift_xor(pmac_state *pmac) #endif -/* $Source$ */ -/* $Revision$ */ -/* $Date$ */ +/* ref: $Format:%D$ */ +/* git commit: $Format:%H$ */ +/* commit time: $Format:%ai$ */ diff --git a/libtomcrypt/src/mac/pmac/pmac_test.c b/libtomcrypt/src/mac/pmac/pmac_test.c index 5d2e42a..19329c6 100644 --- a/libtomcrypt/src/mac/pmac/pmac_test.c +++ b/libtomcrypt/src/mac/pmac/pmac_test.c @@ -5,20 +5,18 @@ * * The library is free for all purposes without any express * guarantee it works. - * - * Tom St Denis, tomstdenis@gmail.com, http://libtom.org */ #include "tomcrypt.h" -/** +/** @file pmac_test.c - PMAC implementation, self-test, by Tom St Denis + PMAC implementation, self-test, by Tom St Denis */ #ifdef LTC_PMAC -/** +/** Test the LTC_OMAC implementation @return CRYPT_OK if successful, CRYPT_NOP if testing has been disabled */ @@ -27,7 +25,7 @@ int pmac_test(void) #if !defined(LTC_TEST) return CRYPT_NOP; #else - static const struct { + static const struct { int msglen; unsigned char key[16], msg[34], tag[16]; } tests[] = { @@ -125,7 +123,7 @@ int pmac_test(void) unsigned long len; unsigned char outtag[MAXBLOCKSIZE]; - /* AES can be under rijndael or aes... try to find it */ + /* AES can be under rijndael or aes... try to find it */ if ((idx = find_cipher("aes")) == -1) { if ((idx = find_cipher("rijndael")) == -1) { return CRYPT_NOP; @@ -137,29 +135,20 @@ int pmac_test(void) if ((err = pmac_memory(idx, tests[x].key, 16, tests[x].msg, tests[x].msglen, outtag, &len)) != CRYPT_OK) { return err; } - - if (XMEMCMP(outtag, tests[x].tag, len)) { -#if 0 - unsigned long y; - printf("\nTAG:\n"); - for (y = 0; y < len; ) { - printf("0x%02x", outtag[y]); - if (y < len-1) printf(", "); - if (!(++y % 8)) printf("\n"); - } -#endif + + if (compare_testvector(outtag, len, tests[x].tag, sizeof(tests[x].tag), "PMAC", x)) { return CRYPT_FAIL_TESTVECTOR; } - } - return CRYPT_OK; + } + return CRYPT_OK; #endif /* LTC_TEST */ } #endif /* PMAC_MODE */ - -/* $Source$ */ -/* $Revision$ */ -/* $Date$ */ + +/* ref: $Format:%D$ */ +/* git commit: $Format:%H$ */ +/* commit time: $Format:%ai$ */ |