padlock.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * VIA PadLock support functions
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. /*
  26. * This implementation is based on the VIA PadLock Programming Guide:
  27. *
  28. * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
  29. * programming_guide.pdf
  30. */
  31. #include "config.h"
  32. #if defined(POLARSSL_PADLOCK_C)
  33. #include "polarssl/padlock.h"
  34. #if defined(POLARSSL_HAVE_X86)
  35. /*
  36. * PadLock detection routine
  37. */
  38. int padlock_supports( int feature )
  39. {
  40. static int flags = -1;
  41. int ebx, edx;
  42. if( flags == -1 )
  43. {
  44. asm( "movl %%ebx, %0 \n" \
  45. "movl $0xC0000000, %%eax \n" \
  46. "cpuid \n" \
  47. "cmpl $0xC0000001, %%eax \n" \
  48. "movl $0, %%edx \n" \
  49. "jb unsupported \n" \
  50. "movl $0xC0000001, %%eax \n" \
  51. "cpuid \n" \
  52. "unsupported: \n" \
  53. "movl %%edx, %1 \n" \
  54. "movl %2, %%ebx \n"
  55. : "=m" (ebx), "=m" (edx)
  56. : "m" (ebx)
  57. : "eax", "ecx", "edx" );
  58. flags = edx;
  59. }
  60. return( flags & feature );
  61. }
  62. /*
  63. * PadLock AES-ECB block en(de)cryption
  64. */
  65. int padlock_xcryptecb( aes_context *ctx,
  66. int mode,
  67. const unsigned char input[16],
  68. unsigned char output[16] )
  69. {
  70. int ebx;
  71. unsigned long *rk;
  72. unsigned long *blk;
  73. unsigned long *ctrl;
  74. unsigned char buf[256];
  75. rk = ctx->rk;
  76. blk = PADLOCK_ALIGN16( buf );
  77. memcpy( blk, input, 16 );
  78. ctrl = blk + 4;
  79. *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
  80. asm( "pushfl; popfl \n" \
  81. "movl %%ebx, %0 \n" \
  82. "movl $1, %%ecx \n" \
  83. "movl %2, %%edx \n" \
  84. "movl %3, %%ebx \n" \
  85. "movl %4, %%esi \n" \
  86. "movl %4, %%edi \n" \
  87. ".byte 0xf3,0x0f,0xa7,0xc8\n" \
  88. "movl %1, %%ebx \n"
  89. : "=m" (ebx)
  90. : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
  91. : "ecx", "edx", "esi", "edi" );
  92. memcpy( output, blk, 16 );
  93. return( 0 );
  94. }
  95. /*
  96. * PadLock AES-CBC buffer en(de)cryption
  97. */
  98. int padlock_xcryptcbc( aes_context *ctx,
  99. int mode,
  100. size_t length,
  101. unsigned char iv[16],
  102. const unsigned char *input,
  103. unsigned char *output )
  104. {
  105. int ebx;
  106. size_t count;
  107. unsigned long *rk;
  108. unsigned long *iw;
  109. unsigned long *ctrl;
  110. unsigned char buf[256];
  111. if( ( (long) input & 15 ) != 0 ||
  112. ( (long) output & 15 ) != 0 )
  113. return( POLARSSL_ERR_PADLOCK_DATA_MISALIGNED );
  114. rk = ctx->rk;
  115. iw = PADLOCK_ALIGN16( buf );
  116. memcpy( iw, iv, 16 );
  117. ctrl = iw + 4;
  118. *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + (mode^1) - 10 ) << 9 );
  119. count = (length + 15) >> 4;
  120. asm( "pushfl; popfl \n" \
  121. "movl %%ebx, %0 \n" \
  122. "movl %2, %%ecx \n" \
  123. "movl %3, %%edx \n" \
  124. "movl %4, %%ebx \n" \
  125. "movl %5, %%esi \n" \
  126. "movl %6, %%edi \n" \
  127. "movl %7, %%eax \n" \
  128. ".byte 0xf3,0x0f,0xa7,0xd0\n" \
  129. "movl %1, %%ebx \n"
  130. : "=m" (ebx)
  131. : "m" (ebx), "m" (count), "m" (ctrl),
  132. "m" (rk), "m" (input), "m" (output), "m" (iw)
  133. : "eax", "ecx", "edx", "esi", "edi" );
  134. memcpy( iv, iw, 16 );
  135. return( 0 );
  136. }
  137. #endif
  138. #endif