padlock.c 4.9 KB

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