ocb_init.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /* OCB Implementation by Tom St Denis */
  12. #include "mycrypt.h"
  13. #ifdef OCB_MODE
  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 ocb_init(ocb_state *ocb, int cipher,
  32. const unsigned char *key, unsigned long keylen, const unsigned char *nonce)
  33. {
  34. int poly, x, y, m, err;
  35. _ARGCHK(ocb != NULL);
  36. _ARGCHK(key != NULL);
  37. _ARGCHK(nonce != NULL);
  38. /* valid cipher? */
  39. if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
  40. return err;
  41. }
  42. /* determine which polys to use */
  43. ocb->block_len = cipher_descriptor[cipher].block_length;
  44. for (poly = 0; poly < (int)(sizeof(polys)/sizeof(polys[0])); poly++) {
  45. if (polys[poly].len == ocb->block_len) {
  46. break;
  47. }
  48. }
  49. if (polys[poly].len != ocb->block_len) {
  50. return CRYPT_INVALID_ARG;
  51. }
  52. /* schedule the key */
  53. if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &ocb->key)) != CRYPT_OK) {
  54. return err;
  55. }
  56. /* find L = E[0] */
  57. zeromem(ocb->L, ocb->block_len);
  58. cipher_descriptor[cipher].ecb_encrypt(ocb->L, ocb->L, &ocb->key);
  59. /* find R = E[N xor L] */
  60. for (x = 0; x < ocb->block_len; x++) {
  61. ocb->R[x] = ocb->L[x] ^ nonce[x];
  62. }
  63. cipher_descriptor[cipher].ecb_encrypt(ocb->R, ocb->R, &ocb->key);
  64. /* find Ls[i] = L << i for i == 0..31 */
  65. XMEMCPY(ocb->Ls[0], ocb->L, ocb->block_len);
  66. for (x = 1; x < 32; x++) {
  67. m = ocb->Ls[x-1][0] >> 7;
  68. for (y = 0; y < ocb->block_len-1; y++) {
  69. ocb->Ls[x][y] = ((ocb->Ls[x-1][y] << 1) | (ocb->Ls[x-1][y+1] >> 7)) & 255;
  70. }
  71. ocb->Ls[x][ocb->block_len-1] = (ocb->Ls[x-1][ocb->block_len-1] << 1) & 255;
  72. if (m == 1) {
  73. for (y = 0; y < ocb->block_len; y++) {
  74. ocb->Ls[x][y] ^= polys[poly].poly_mul[y];
  75. }
  76. }
  77. }
  78. /* find Lr = L / x */
  79. m = ocb->L[ocb->block_len-1] & 1;
  80. /* shift right */
  81. for (x = ocb->block_len - 1; x > 0; x--) {
  82. ocb->Lr[x] = ((ocb->L[x] >> 1) | (ocb->L[x-1] << 7)) & 255;
  83. }
  84. ocb->Lr[0] = ocb->L[0] >> 1;
  85. if (m == 1) {
  86. for (x = 0; x < ocb->block_len; x++) {
  87. ocb->Lr[x] ^= polys[poly].poly_div[x];
  88. }
  89. }
  90. /* set Li, checksum */
  91. zeromem(ocb->Li, ocb->block_len);
  92. zeromem(ocb->checksum, ocb->block_len);
  93. /* set other params */
  94. ocb->block_index = 1;
  95. ocb->cipher = cipher;
  96. return CRYPT_OK;
  97. }
  98. #endif