@@ -53,6 +53,12 @@ Building `python-mbedtls` requires Cython::
5353
5454then,
5555
56+ ::
57+
58+ python3 -m pip install python-mbedtls
59+
60+ or
61+
5662::
5763
5864 git clone https://github.com/Synss/python-mbedtls.git python-mbedtls.git
@@ -68,16 +74,70 @@ The unit tests further require `nose` and `pyCrypto`::
6874Hashing module (`md.h `)
6975-----------------------
7076
71- The hashing module is wrapped, which provides message, file, and HMAC
72- with the following algorithms: MD5, SHA-1, SHA-2, and RIPEMD-160.
77+ Message digest algorithms
78+ ~~~~~~~~~~~~~~~~~~~~~~~~~
79+
80+ The `mbedtls.hash ` module provides MD5, SHA-1, SHA-2, and RIPEMD-160 secure
81+ hashes and message digests. The API follows the recommendations from PEP 452
82+ so that it can be used as a drop-in replacement to e.g. `hashlib ` or
83+ `PyCrypto `.
84+
85+ Here are the examples from `hashlib ` executed with `python-mbedtls `::
86+
87+ >>> from mbedtls import hash as hashlib
88+ >>> m = hashlib.md5()
89+ >>> m.update(b"Nobody inspects")
90+ >>> m.update(b" the spammish repetition")
91+ >>> m.digest()
92+ b'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
93+ >>> m.digest_size
94+ 16
95+ >>> m.block_size
96+ 64
97+
98+ More condensed::
99+
100+ >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
101+ 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
102+
103+ Using `new() `::
104+
105+ >>> h = hashlib.new('ripemd160')
106+ >>> h.update(b"Nobody inspects the spammish repetition")
107+ >>> h.hexdigest()
108+ 'cc4a5ce1b3df48aec5d22d1f16b894a0b894eccc'
109+
73110
74- The API follows the recommendations from PEP 452.
111+ HMAC algorithm
112+ ~~~~~~~~~~~~~~
113+
114+ The `mbedtls.hmac ` module computes HMAC. The API follows the recommendations
115+ from PEP 452 as well.
116+
117+ Example::
118+
119+ >>> from mbedtls import hmac
120+ >>> m = hmac.new(b"This is my secret key", digestmod="md5")
121+ >>> m.update(b"Nobody inspects")
122+ >>> m.update(b" the spammish repetition")
123+ >>> m.digest()
124+ b'\x9d-/rj\\\x98\x80\xb1rG\x87\x0f\xe9\xe4\xeb'
125+
126+ .. warning ::
127+
128+ The message is cleared after calculation of the digest. Only call
129+ :meth: `mbedtls.hmac.Hmac.digest ` or :meth: `mbedtls.hmac.Hmac.hexdigest ` once
130+ per message.
75131
76132
77133Symmetric cipher module (`cipher.h `)
78134------------------------------------
79135
80- The symmetric cipher module is wrapped, which provides:
136+ The `mbedtls.cipher ` module provides symmetric encryption. The API follows the
137+ recommendations from PEP 272 so that it can be used as a drop-in replacement to
138+ e.g. `PyCrypto `.
139+
140+ mbedtls provides the following algorithms:
81141
82142- Aes encryption/decryption (128, 192, and 256 bits) in ECB, CBC, CFB128,
83143 CTR, GCM, or CCM mode;
@@ -87,27 +147,60 @@ The symmetric cipher module is wrapped, which provides:
87147 CFB128, CTR, GCM, or CCM mode;
88148- DES encryption/decryption in ECB, or CBC mode;
89149
90- The API follows the recommendations from PEP 272.
91-
92150Notes:
93151 - Tagging and padding are not wrapped.
94152 - The counter in CTR mode cannot be explicitly provided.
95153
154+ Example::
155+
156+ >>> from mbedtls import cipher
157+ >>> c = cipher.AES.new(b"My 16-bytes key.", cipher.MODE_CBC, b"CBC needs an IV.")
158+ >>> enc = c.encrypt(b"This is a super-secret message!")
159+ >>> enc
160+ b'*`k6\x98\x97=[\xdf\x7f\x88\x96\xf5\t\x19J7\x93\xb5\xe0~\t\x9e\x968m\xcd\x
161+ >>> c.decrypt(enc)
162+ b'This is a super-secret message!'
163+
96164
97165Public key module (`pk.h `)
98166--------------------------
99167
100- The RSA cryptosystem is wrapped, which provides :
168+ The ` mbedtls.pk ` module provides the RSA cryptosystem. This includes :
101169
102170- Public-private key generation and key import/export in PEM and DER
103171 formats;
104172- Asymmetric encryption and decryption;
105173- Message signature and verification.
106174
107-
108- Contribution
109- ============
110-
111- `python-mbedtls ` is in an early stage of development and contributions
112- in any form is welcome. Note, however, that bugs against mbed TLS
113- should be reported upstream directly.
175+ Key generation, the default size is 2048 bits::
176+
177+ >>> from mbedtls import pk
178+ >>> rsa = pk.RSA()
179+ >>> rsa.has_private()
180+ False
181+ >>> rsa.generate()
182+ >>> rsa.key_size
183+ 256
184+ >>> rsa.has_private() and rsa.has_public()
185+ True
186+
187+ Message encryption and decryption::
188+
189+ >>> enc = rsa.encrypt(b"secret message")
190+ >>> rsa.decrypt(enc)
191+ b"secret message"
192+
193+ Message signature and verification::
194+
195+ >>> sig = rsa.sign(b"Please sign here.")
196+ >>> rsa.verify(b"Please sign here.", sig)
197+ True
198+ >>> rsa.verify(b"Sorry, wrong message.", sig)
199+ False
200+ >>> prv, pub = rsa.export(format="DER")
201+ >>> other = pk.RSA()
202+ >>> other.import_(pub)
203+ >>> other.has_private()
204+ False
205+ >>> other.verify(b"Please sign here.", sig)
206+ True
0 commit comments