Thread: [Opentnl-cvs] tnl/libtomcrypt/mac/pmac pmac_done.c,NONE,1.1 pmac_file.c,NONE,1.1 pmac_init.c,NONE,1.
Brought to you by:
mark_frohnmayer,
s_alanet
Update of /cvsroot/opentnl/tnl/libtomcrypt/mac/pmac In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26694/libtomcrypt/mac/pmac Added Files: pmac_done.c pmac_file.c pmac_init.c pmac_memory.c pmac_memory_multi.c pmac_ntz.c pmac_process.c pmac_shift_xor.c pmac_test.c Log Message: Updated to libtomcrypt 1.0.2 Not tested on linux or OS X --- NEW FILE: pmac_init.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file pmac_init.c PMAC implementation, initialize state, by Tom St Denis */ #ifdef PMAC static const struct { int len; unsigned char poly_div[MAXBLOCKSIZE], poly_mul[MAXBLOCKSIZE]; } polys[] = { { 8, { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D }, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1B } }, { 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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87 } } }; /** 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 keylen The length of the secret key (octets) @return CRYPT_OK if successful */ int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned long keylen) { int poly, x, y, m, err; unsigned char *L; LTC_ARGCHK(pmac != NULL); LTC_ARGCHK(key != NULL); /* valid cipher? */ if ((err = cipher_is_valid(cipher)) != CRYPT_OK) { return err; } /* 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) { break; } } if (polys[poly].len != pmac->block_len) { return CRYPT_INVALID_ARG; } #ifdef LTC_FAST if (pmac->block_len % sizeof(LTC_FAST_TYPE)) { return CRYPT_INVALID_ARG; } #endif /* schedule the key */ 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) { return CRYPT_MEM; } /* find L = E[0] */ zeromem(L, pmac->block_len); cipher_descriptor[cipher].ecb_encrypt(L, L, &pmac->key); /* find Ls[i] = L << i for i == 0..31 */ XMEMCPY(pmac->Ls[0], L, pmac->block_len); for (x = 1; x < 32; x++) { m = pmac->Ls[x-1][0] >> 7; for (y = 0; y < pmac->block_len-1; y++) { pmac->Ls[x][y] = ((pmac->Ls[x-1][y] << 1) | (pmac->Ls[x-1][y+1] >> 7)) & 255; } pmac->Ls[x][pmac->block_len-1] = (pmac->Ls[x-1][pmac->block_len-1] << 1) & 255; if (m == 1) { for (y = 0; y < pmac->block_len; y++) { pmac->Ls[x][y] ^= polys[poly].poly_mul[y]; } } } /* 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; 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)); #ifdef LTC_CLEAN_STACK zeromem(L, pmac->block_len); #endif XFREE(L); return CRYPT_OK; } #endif --- NEW FILE: pmac_test.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file pmac_test.c PMAC implementation, self-test, by Tom St Denis */ #ifdef PMAC /** Test the OMAC implementation @return CRYPT_OK if successful, CRYPT_NOP if testing has been disabled */ int pmac_test(void) { #if !defined(LTC_TEST) return CRYPT_NOP; #else static const struct { int msglen; unsigned char key[16], msg[34], tag[16]; } tests[] = { /* PMAC-AES-128-0B */ { 0, /* key */ { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, /* msg */ { 0x00 }, /* tag */ { 0x43, 0x99, 0x57, 0x2c, 0xd6, 0xea, 0x53, 0x41, 0xb8, 0xd3, 0x58, 0x76, 0xa7, 0x09, 0x8a, 0xf7 } }, /* PMAC-AES-128-3B */ { 3, /* key */ { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, /* msg */ { 0x00, 0x01, 0x02 }, /* tag */ { 0x25, 0x6b, 0xa5, 0x19, 0x3c, 0x1b, 0x99, 0x1b, 0x4d, 0xf0, 0xc5, 0x1f, 0x38, 0x8a, 0x9e, 0x27 } }, /* PMAC-AES-128-16B */ { 16, /* key */ { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, /* msg */ { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, /* tag */ { 0xeb, 0xbd, 0x82, 0x2f, 0xa4, 0x58, 0xda, 0xf6, 0xdf, 0xda, 0xd7, 0xc2, 0x7d, 0xa7, 0x63, 0x38 } }, /* PMAC-AES-128-20B */ { 20, /* key */ { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, /* msg */ { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 }, /* tag */ { 0x04, 0x12, 0xca, 0x15, 0x0b, 0xbf, 0x79, 0x05, 0x8d, 0x8c, 0x75, 0xa5, 0x8c, 0x99, 0x3f, 0x55 } }, /* PMAC-AES-128-32B */ { 32, /* key */ { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, /* msg */ { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }, /* tag */ { 0xe9, 0x7a, 0xc0, 0x4e, 0x9e, 0x5e, 0x33, 0x99, 0xce, 0x53, 0x55, 0xcd, 0x74, 0x07, 0xbc, 0x75 } }, /* PMAC-AES-128-34B */ { 34, /* key */ { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, /* msg */ { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21 }, /* tag */ { 0x5c, 0xba, 0x7d, 0x5e, 0xb2, 0x4f, 0x7c, 0x86, 0xcc, 0xc5, 0x46, 0x04, 0xe5, 0x3d, 0x55, 0x12 } } }; int err, x, idx; unsigned long len; unsigned char outtag[MAXBLOCKSIZE]; /* 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; } } for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { len = sizeof(outtag); if ((err = pmac_memory(idx, tests[x].key, 16, tests[x].msg, tests[x].msglen, outtag, &len)) != CRYPT_OK) { return err; } if (memcmp(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 return CRYPT_FAIL_TESTVECTOR; } } return CRYPT_OK; #endif /* LTC_TEST */ } #endif /* PMAC_MODE */ --- NEW FILE: pmac_memory.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file pmac_memory.c PMAC implementation, process a block of memory, by Tom St Denis */ #ifdef PMAC /** PMAC a block of memory @param cipher The index of the cipher desired @param key The secret key @param keylen The length of the secret key (octets) @param in The data you wish to send through PMAC @param inlen The length of data you wish to send through PMAC (octets) @param out [out] Destination for the authentication tag @param outlen [in/out] The max size and resulting size of the authentication tag @return CRYPT_OK if successful */ 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) { int err; pmac_state *pmac; LTC_ARGCHK(key != NULL); LTC_ARGCHK(in != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); /* allocate ram for pmac state */ pmac = XMALLOC(sizeof(pmac_state)); if (pmac == NULL) { return CRYPT_MEM; } if ((err = pmac_init(pmac, cipher, key, keylen)) != CRYPT_OK) { goto LBL_ERR; } if ((err = pmac_process(pmac, in, inlen)) != CRYPT_OK) { goto LBL_ERR; } if ((err = pmac_done(pmac, out, outlen)) != CRYPT_OK) { goto LBL_ERR; } err = CRYPT_OK; LBL_ERR: #ifdef LTC_CLEAN_STACK zeromem(pmac, sizeof(pmac_state)); #endif XFREE(pmac); return err; } #endif --- NEW FILE: pmac_done.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file pmac_done.c PMAC implementation, terminate a session, by Tom St Denis */ #ifdef PMAC int pmac_done(pmac_state *state, unsigned char *out, unsigned long *outlen) { int err, x; LTC_ARGCHK(state != NULL); LTC_ARGCHK(out != NULL); if ((err = cipher_is_valid(state->cipher_idx)) != CRYPT_OK) { return err; } if ((state->buflen > (int)sizeof(state->block)) || (state->buflen < 0) || (state->block_len > (int)sizeof(state->block)) || (state->buflen > state->block_len)) { return CRYPT_INVALID_ARG; } /* handle padding. If multiple xor in L/x */ if (state->buflen == state->block_len) { /* xor Lr against the checksum */ for (x = 0; x < state->block_len; x++) { state->checksum[x] ^= state->block[x] ^ state->Lr[x]; } } else { /* otherwise xor message bytes then the 0x80 byte */ for (x = 0; x < state->buflen; x++) { state->checksum[x] ^= state->block[x]; } state->checksum[x] ^= 0x80; } /* encrypt it */ cipher_descriptor[state->cipher_idx].ecb_encrypt(state->checksum, state->checksum, &state->key); cipher_descriptor[state->cipher_idx].done(&state->key); /* store it */ for (x = 0; x < state->block_len && x <= (int)*outlen; x++) { out[x] = state->checksum[x]; } *outlen = x; #ifdef LTC_CLEAN_STACK zeromem(state, sizeof(*state)); #endif return CRYPT_OK; } #endif --- NEW FILE: pmac_process.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file pmac_process.c PMAC implementation, process data, by Tom St Denis */ #ifdef PMAC /** Process data in a PMAC stream @param pmac The PMAC state @param in The data to send through PMAC @param inlen The length of the data to send through PMAC @return CRYPT_OK if successful */ int pmac_process(pmac_state *pmac, const unsigned char *in, unsigned long inlen) { int err, n; unsigned long x; unsigned char Z[MAXBLOCKSIZE]; LTC_ARGCHK(pmac != NULL); LTC_ARGCHK(in != NULL); if ((err = cipher_is_valid(pmac->cipher_idx)) != CRYPT_OK) { return err; } if ((pmac->buflen > (int)sizeof(pmac->block)) || (pmac->buflen < 0) || (pmac->block_len > (int)sizeof(pmac->block)) || (pmac->buflen > pmac->block_len)) { return CRYPT_INVALID_ARG; } #ifdef LTC_FAST if (pmac->buflen == 0 && inlen > 16) { unsigned long y; 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])); } cipher_descriptor[pmac->cipher_idx].ecb_encrypt(Z, Z, &pmac->key); for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) { *((LTC_FAST_TYPE*)(&pmac->checksum[y])) ^= *((LTC_FAST_TYPE*)(&Z[y])); } in += 16; } inlen -= x; } #endif 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); for (x = 0; x < (unsigned long)pmac->block_len; x++) { Z[x] = pmac->Li[x] ^ pmac->block[x]; } cipher_descriptor[pmac->cipher_idx].ecb_encrypt(Z, Z, &pmac->key); for (x = 0; x < (unsigned long)pmac->block_len; x++) { pmac->checksum[x] ^= Z[x]; } pmac->buflen = 0; } /* add bytes */ n = MIN(inlen, (unsigned long)(pmac->block_len - pmac->buflen)); XMEMCPY(pmac->block + pmac->buflen, in, n); pmac->buflen += n; inlen -= n; in += n; } #ifdef LTC_CLEAN_STACK zeromem(Z, sizeof(Z)); #endif return CRYPT_OK; } #endif --- NEW FILE: pmac_memory_multi.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" #include <stdarg.h> /** @file pmac_memory_multi.c PMAC implementation, process multiple blocks of memory, by Tom St Denis */ #ifdef PMAC /** PMAC multiple blocks of memory @param cipher The index of the cipher desired @param key The secret key @param keylen The length of the secret key (octets) @param out [out] Destination for the authentication tag @param outlen [in/out] The max size and resulting size of the authentication tag @param in The data you wish to send through PMAC @param inlen The length of data you wish to send through PMAC (octets) @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, const unsigned char *key, unsigned long keylen, unsigned char *out, unsigned long *outlen, const unsigned char *in, unsigned long inlen, ...) { int err; pmac_state *pmac; va_list args; const unsigned char *curptr; unsigned long curlen; LTC_ARGCHK(key != NULL); LTC_ARGCHK(in != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); /* allocate ram for pmac state */ pmac = XMALLOC(sizeof(pmac_state)); 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; curlen = inlen; for (;;) { /* process buf */ if ((err = pmac_process(pmac, curptr, curlen)) != CRYPT_OK) { goto LBL_ERR; } /* step to next */ curptr = va_arg(args, const unsigned char*); if (curptr == NULL) { break; } curlen = va_arg(args, unsigned long); } if ((err = pmac_done(pmac, out, outlen)) != CRYPT_OK) { goto LBL_ERR; } LBL_ERR: #ifdef LTC_CLEAN_STACK zeromem(pmac, sizeof(pmac_state)); #endif XFREE(pmac); va_end(args); return err; } #endif --- NEW FILE: pmac_shift_xor.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file pmac_shift_xor.c PMAC implementation, internal function, by Tom St Denis */ #ifdef PMAC /** Internal function. Performs the state update (adding correct multiple) @param pmac The PMAC state. */ void pmac_shift_xor(pmac_state *pmac) { int x, y; 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)); } #else for (x = 0; x < pmac->block_len; x++) { pmac->Li[x] ^= pmac->Ls[y][x]; } #endif } #endif --- NEW FILE: pmac_file.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file pmac_file.c PMAC implementation, process a file, by Tom St Denis */ #ifdef PMAC /** 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) @param filename The name of the file to send through PMAC @param out [out] Destination for the authentication tag @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, const unsigned char *key, unsigned long keylen, const char *filename, unsigned char *out, unsigned long *outlen) { #ifdef LTC_NO_FILE return CRYPT_NOP; #else int err, x; pmac_state pmac; FILE *in; unsigned char buf[512]; LTC_ARGCHK(key != NULL); LTC_ARGCHK(filename != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); in = fopen(filename, "rb"); if (in == NULL) { return CRYPT_FILE_NOTFOUND; } if ((err = pmac_init(&pmac, cipher, key, keylen)) != CRYPT_OK) { fclose(in); return err; } do { x = fread(buf, 1, sizeof(buf), in); if ((err = pmac_process(&pmac, buf, x)) != CRYPT_OK) { fclose(in); return err; } } while (x == sizeof(buf)); fclose(in); if ((err = pmac_done(&pmac, out, outlen)) != CRYPT_OK) { return err; } #ifdef LTC_CLEAN_STACK zeromem(buf, sizeof(buf)); #endif return CRYPT_OK; #endif } #endif --- NEW FILE: pmac_ntz.c --- /* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tom...@gm..., http://libtomcrypt.org */ #include "tomcrypt.h" /** @file pmac_ntz.c PMAC implementation, internal function, by Tom St Denis */ #ifdef PMAC /** Internal PMAC function */ int pmac_ntz(unsigned long x) { int c; x &= 0xFFFFFFFFUL; c = 0; while ((x & 1) == 0) { ++c; x >>= 1; } return c; } #endif |