padlock.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * VIA PadLock support functions
  3. *
  4. * Copyright (C) 2006-2014, 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. #if !defined(POLARSSL_CONFIG_FILE)
  32. #include "polarssl/config.h"
  33. #else
  34. #include POLARSSL_CONFIG_FILE
  35. #endif
  36. #if defined(POLARSSL_PADLOCK_C)
  37. #include "polarssl/padlock.h"
  38. #if defined(POLARSSL_HAVE_X86)
  39. /*
  40. * PadLock detection routine
  41. */
  42. int padlock_supports( int feature )
  43. {
  44. static int flags = -1;
  45. int ebx = 0, edx = 0;
  46. if( flags == -1 )
  47. {
  48. asm( "movl %%ebx, %0 \n\t"
  49. "movl $0xC0000000, %%eax \n\t"
  50. "cpuid \n\t"
  51. "cmpl $0xC0000001, %%eax \n\t"
  52. "movl $0, %%edx \n\t"
  53. "jb unsupported \n\t"
  54. "movl $0xC0000001, %%eax \n\t"
  55. "cpuid \n\t"
  56. "unsupported: \n\t"
  57. "movl %%edx, %1 \n\t"
  58. "movl %2, %%ebx \n\t"
  59. : "=m" (ebx), "=m" (edx)
  60. : "m" (ebx)
  61. : "eax", "ecx", "edx" );
  62. flags = edx;
  63. }
  64. return( flags & feature );
  65. }
  66. /*
  67. * PadLock AES-ECB block en(de)cryption
  68. */
  69. int padlock_xcryptecb( aes_context *ctx,
  70. int mode,
  71. const unsigned char input[16],
  72. unsigned char output[16] )
  73. {
  74. int ebx = 0;
  75. uint32_t *rk;
  76. uint32_t *blk;
  77. uint32_t *ctrl;
  78. unsigned char buf[256];
  79. rk = ctx->rk;
  80. blk = PADLOCK_ALIGN16( buf );
  81. memcpy( blk, input, 16 );
  82. ctrl = blk + 4;
  83. *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
  84. asm( "pushfl \n\t"
  85. "popfl \n\t"
  86. "movl %%ebx, %0 \n\t"
  87. "movl $1, %%ecx \n\t"
  88. "movl %2, %%edx \n\t"
  89. "movl %3, %%ebx \n\t"
  90. "movl %4, %%esi \n\t"
  91. "movl %4, %%edi \n\t"
  92. ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
  93. "movl %1, %%ebx \n\t"
  94. : "=m" (ebx)
  95. : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
  96. : "ecx", "edx", "esi", "edi" );
  97. memcpy( output, blk, 16 );
  98. return( 0 );
  99. }
  100. /*
  101. * PadLock AES-CBC buffer en(de)cryption
  102. */
  103. int padlock_xcryptcbc( aes_context *ctx,
  104. int mode,
  105. size_t length,
  106. unsigned char iv[16],
  107. const unsigned char *input,
  108. unsigned char *output )
  109. {
  110. int ebx = 0;
  111. size_t count;
  112. uint32_t *rk;
  113. uint32_t *iw;
  114. uint32_t *ctrl;
  115. unsigned char buf[256];
  116. if( ( (long) input & 15 ) != 0 ||
  117. ( (long) output & 15 ) != 0 )
  118. return( POLARSSL_ERR_PADLOCK_DATA_MISALIGNED );
  119. rk = ctx->rk;
  120. iw = PADLOCK_ALIGN16( buf );
  121. memcpy( iw, iv, 16 );
  122. ctrl = iw + 4;
  123. *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode ^ 1 ) - 10 ) << 9 );
  124. count = ( length + 15 ) >> 4;
  125. asm( "pushfl \n\t"
  126. "popfl \n\t"
  127. "movl %%ebx, %0 \n\t"
  128. "movl %2, %%ecx \n\t"
  129. "movl %3, %%edx \n\t"
  130. "movl %4, %%ebx \n\t"
  131. "movl %5, %%esi \n\t"
  132. "movl %6, %%edi \n\t"
  133. "movl %7, %%eax \n\t"
  134. ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
  135. "movl %1, %%ebx \n\t"
  136. : "=m" (ebx)
  137. : "m" (ebx), "m" (count), "m" (ctrl),
  138. "m" (rk), "m" (input), "m" (output), "m" (iw)
  139. : "eax", "ecx", "edx", "esi", "edi" );
  140. memcpy( iv, iw, 16 );
  141. return( 0 );
  142. }
  143. #endif /* POLARSSL_HAVE_X86 */
  144. #endif /* POLARSSL_PADLOCK_C */