pmac_init.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. *
  9. * Tom St Denis, [email protected], http://libtomcrypt.org
  10. */
  11. /* PMAC implementation by Tom St Denis */
  12. #include "mycrypt.h"
  13. #ifdef PMAC
  14. static const struct {
  15. int len;
  16. unsigned char poly_div[MAXBLOCKSIZE],
  17. poly_mul[MAXBLOCKSIZE];
  18. } polys[] = {
  19. {
  20. 8,
  21. { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D },
  22. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1B }
  23. }, {
  24. 16,
  25. { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  26. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x43 },
  27. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  28. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87 }
  29. }
  30. };
  31. int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned long keylen)
  32. {
  33. int poly, x, y, m, err;
  34. unsigned char *L;
  35. _ARGCHK(pmac != NULL);
  36. _ARGCHK(key != NULL);
  37. /* valid cipher? */
  38. if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
  39. return err;
  40. }
  41. /* determine which polys to use */
  42. pmac->block_len = cipher_descriptor[cipher].block_length;
  43. for (poly = 0; poly < (int)(sizeof(polys)/sizeof(polys[0])); poly++) {
  44. if (polys[poly].len == pmac->block_len) {
  45. break;
  46. }
  47. }
  48. if (polys[poly].len != pmac->block_len) {
  49. return CRYPT_INVALID_ARG;
  50. }
  51. /* schedule the key */
  52. if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &pmac->key)) != CRYPT_OK) {
  53. return err;
  54. }
  55. /* allocate L */
  56. L = XMALLOC(pmac->block_len);
  57. if (L == NULL) {
  58. return CRYPT_MEM;
  59. }
  60. /* find L = E[0] */
  61. zeromem(L, pmac->block_len);
  62. cipher_descriptor[cipher].ecb_encrypt(L, L, &pmac->key);
  63. /* find Ls[i] = L << i for i == 0..31 */
  64. XMEMCPY(pmac->Ls[0], L, pmac->block_len);
  65. for (x = 1; x < 32; x++) {
  66. m = pmac->Ls[x-1][0] >> 7;
  67. for (y = 0; y < pmac->block_len-1; y++) {
  68. pmac->Ls[x][y] = ((pmac->Ls[x-1][y] << 1) | (pmac->Ls[x-1][y+1] >> 7)) & 255;
  69. }
  70. pmac->Ls[x][pmac->block_len-1] = (pmac->Ls[x-1][pmac->block_len-1] << 1) & 255;
  71. if (m == 1) {
  72. for (y = 0; y < pmac->block_len; y++) {
  73. pmac->Ls[x][y] ^= polys[poly].poly_mul[y];
  74. }
  75. }
  76. }
  77. /* find Lr = L / x */
  78. m = L[pmac->block_len-1] & 1;
  79. /* shift right */
  80. for (x = pmac->block_len - 1; x > 0; x--) {
  81. pmac->Lr[x] = ((L[x] >> 1) | (L[x-1] << 7)) & 255;
  82. }
  83. pmac->Lr[0] = L[0] >> 1;
  84. if (m == 1) {
  85. for (x = 0; x < pmac->block_len; x++) {
  86. pmac->Lr[x] ^= polys[poly].poly_div[x];
  87. }
  88. }
  89. /* zero buffer, counters, etc... */
  90. pmac->block_index = 1;
  91. pmac->cipher_idx = cipher;
  92. pmac->buflen = 0;
  93. zeromem(pmac->block, sizeof(pmac->block));
  94. zeromem(pmac->Li, sizeof(pmac->Li));
  95. zeromem(pmac->checksum, sizeof(pmac->checksum));
  96. #ifdef CLEAN_STACK
  97. zeromem(L, pmac->block_len);
  98. #endif
  99. XFREE(L);
  100. return CRYPT_OK;
  101. }
  102. #endif