padlock.c 5.6 KB

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