I2C toy code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

207 lines
5.8 KiB

  1. """
  2. demo_dynamic.py v1
  3. This program demonstrates Python's use of the dynamic
  4. language support additions to LTC, namely access to LTC
  5. constants, struct and union sizes, and the binding of a
  6. math package to LTC. Also provided are simple code
  7. fragments to illustrate how one might write a Python
  8. wrapper for LTC and how an app might call the wrapper.
  9. This or a similar model should work for Ruby and other
  10. dynamic languages.
  11. This instance uses Python's ctypes and requires a single
  12. .dylib linking together LTC and a math library. Building
  13. a single .dylib is needed because LTC wants a fairly tight
  14. relationship between itself and the mathlib. (ctypes can
  15. load multiple .dylibs, but it does not support this level
  16. of tight coupling between otherwise independent libraries.)
  17. My .dylib was created on OSX with the following steps:
  18. 1- compile LTC to a .a static lib:
  19. CFLAGS="-DLTM_DESC -DUSE_LTM" make
  20. 2- link LTC and LTM into a single .dylib:
  21. ar2dylib_with tomcrypt tommath
  22. where ar2dylib_with is a shell script that combines
  23. the LTC .a with the LTM .dylib
  24. Reminder: you don't need to bind in a math library unless
  25. you are going to use LTC functions that depend
  26. on a mathlib. For example, public key crypto
  27. needs a mathlib; hashing and symmetric encryption
  28. do not.
  29. This code was written for Python 2.7.
  30. Larry Bugbee
  31. March 2014
  32. """
  33. from ctypes import *
  34. from ctypes.util import find_library
  35. #---------------------------------------------------------------
  36. # load the .dylib
  37. libname = 'tomcrypt'
  38. libpath = find_library(libname)
  39. print
  40. print(' demo_dynamic.py')
  41. print
  42. print(' path to library %s: %s' % (libname, libpath))
  43. LTC = cdll.LoadLibrary(libpath)
  44. print(' loaded: %s' % LTC)
  45. print
  46. #---------------------------------------------------------------
  47. # get list of all supported constants followed by a list of all
  48. # supported sizes. One alternative: these lists may be parsed
  49. # and used as needed.
  50. if 1:
  51. print ' all supported constants and their values:'
  52. # get size to allocate for constants output list
  53. str_len = c_int(0)
  54. ret = LTC.crypt_list_all_constants(None, byref(str_len))
  55. print ' need to allocate %d bytes \n' % str_len.value
  56. # allocate that size and get (name, size) pairs, each pair
  57. # separated by a newline char.
  58. names_sizes = c_buffer(str_len.value)
  59. ret = LTC.crypt_list_all_constants(names_sizes, byref(str_len))
  60. print names_sizes.value
  61. print
  62. if 1:
  63. print ' all supported sizes:'
  64. # get size to allocate for sizes output list
  65. str_len = c_int(0)
  66. ret = LTC.crypt_list_all_sizes(None, byref(str_len))
  67. print ' need to allocate %d bytes \n' % str_len.value
  68. # allocate that size and get (name, size) pairs, each pair
  69. # separated by a newline char.
  70. names_sizes = c_buffer(str_len.value)
  71. ret = LTC.crypt_list_all_sizes(names_sizes, byref(str_len))
  72. print names_sizes.value
  73. print
  74. #---------------------------------------------------------------
  75. # get individually named constants and sizes
  76. # print selected constants
  77. if 1:
  78. print '\n selected constants:'
  79. names = [
  80. 'ENDIAN_LITTLE',
  81. 'ENDIAN_64BITWORD',
  82. 'PK_PUBLIC',
  83. 'MAX_RSA_SIZE',
  84. 'CTR_COUNTER_BIG_ENDIAN',
  85. ]
  86. for name in names:
  87. const_value = c_int(0)
  88. rc = LTC.crypt_get_constant(name, byref(const_value))
  89. value = const_value.value
  90. print ' %-25s %d' % (name, value)
  91. # print selected sizes
  92. if 1:
  93. print '\n selected sizes:'
  94. names = [
  95. 'rijndael_key',
  96. 'rsa_key',
  97. 'symmetric_CTR',
  98. 'twofish_key',
  99. 'ecc_point',
  100. 'gcm_state',
  101. 'sha512_state',
  102. ]
  103. for name in names:
  104. size_value = c_int(0)
  105. rc = LTC.crypt_get_size(name, byref(size_value))
  106. value = size_value.value
  107. print ' %-25s %d' % (name, value)
  108. #---------------------------------------------------------------
  109. #---------------------------------------------------------------
  110. # ctypes getting a list of this build's supported algorithms
  111. # and compiler switches
  112. def get_named_string(lib, name):
  113. return c_char_p.in_dll(lib, name).value
  114. if 0:
  115. print '\n%s' % ('-'*60)
  116. print 'This is a string compiled into LTC showing compile '
  117. print 'options and algorithms supported by this build \n'
  118. print get_named_string(LTC, 'crypt_build_settings')
  119. print
  120. #---------------------------------------------------------------
  121. #---------------------------------------------------------------
  122. # here is an example of how a wrapper can make Python access
  123. # more Pythonic
  124. # - - - - - - - - - - - - -
  125. # a wrapper fragment...
  126. def _get_size(name):
  127. size = c_int(0)
  128. rc = LTC.crypt_get_size(name, byref(size))
  129. return size.value
  130. sha256_state_struct_size = _get_size('sha256_state')
  131. sha512_state_struct_size = _get_size('sha512_state')
  132. class SHA256(object):
  133. def __init__(self):
  134. self.state = c_buffer(sha256_state_struct_size)
  135. LTC.sha256_init(byref(self.state))
  136. def update(self, data):
  137. LTC.sha256_process(byref(self.state), data, len(data))
  138. def digest(self):
  139. md = c_buffer(32)
  140. LTC.sha256_done(byref(self.state), byref(md))
  141. return md.raw
  142. # - - - - - - - - - - - - -
  143. # an app fragment...
  144. # from wrapper import * # uncomment in real life
  145. data = 'hello world'
  146. sha256 = SHA256()
  147. sha256.update(data)
  148. md = sha256.digest()
  149. template = '\n\n the SHA256 digest for "%s" is %s \n'
  150. print template % (data, md.encode('hex'))
  151. #---------------------------------------------------------------
  152. #---------------------------------------------------------------
  153. #---------------------------------------------------------------