Skip to content

Commit 4024b7c

Browse files
authored
Merge pull request #2 from brentru/patch-decode-encode
Fix extra-zero-append error, add util/
2 parents f253a47 + 3c9df3c commit 4024b7c

File tree

3 files changed

+75
-12
lines changed

3 files changed

+75
-12
lines changed

adafruit_rsa/pkcs1.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,6 @@ def decrypt(crypto, priv_key):
228228
decrypted = priv_key.blinded_decrypt(encrypted)
229229
cleartext = transform.int2bytes(decrypted, blocksize)
230230

231-
# If we can't find the cleartext marker, decryption failed.
232-
if cleartext[0:2] != b"\x00\x02":
233-
raise DecryptionError("Decryption failed")
234-
235231
# Find the 00 separator between the padding and the message
236232
try:
237233
sep_idx = cleartext.index(b"\x00", 2)

adafruit_rsa/transform.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
1919
From bytes to a number, number to bytes, etc.
2020
"""
21+
22+
# from __future__ import absolute_import
23+
2124
from struct import pack
22-
from adafruit_binascii import hexlify
25+
import adafruit_binascii as binascii
2326

2427
from adafruit_rsa._compat import byte, is_integer
2528
from adafruit_rsa import common, machine_size
@@ -37,7 +40,7 @@ def bytes2int(raw_bytes):
3740
3841
"""
3942

40-
return int(hexlify(raw_bytes), 16)
43+
return int(binascii.hexlify(raw_bytes), 16)
4144

4245

4346
def _int2bytes(number, block_size=None):
@@ -133,14 +136,11 @@ def bytes_leading(raw_bytes, needle=b'\x00'):
133136
def int2bytes(number, fill_size=None, chunk_size=None, overflow=False):
134137
"""
135138
Convert an unsigned integer to bytes (base-256 representation)::
136-
137139
Does not preserve leading zeros if you don't specify a chunk size or
138140
fill size.
139-
140141
.. NOTE:
141142
You must not specify both fill_size and chunk_size. Only one
142143
of them is allowed.
143-
144144
:param number:
145145
Integer value
146146
:param fill_size:
@@ -172,7 +172,7 @@ def int2bytes(number, fill_size=None, chunk_size=None, overflow=False):
172172
raise ValueError("You can either fill or pad chunks, but not both")
173173

174174
# Ensure these are integers.
175-
is_integer(number)
175+
assert number & 1 == 0, "Number must be an unsigned integer, not a float."
176176

177177
raw_bytes = b''
178178

@@ -197,9 +197,10 @@ def int2bytes(number, fill_size=None, chunk_size=None, overflow=False):
197197
"Need %d bytes for number, but fill size is %d" %
198198
(length, fill_size)
199199
)
200-
raw_bytes = b'\x00' + raw_bytes
200+
raw_bytes = "% {}s".format(fill_size).encode() % raw_bytes
201201
elif chunk_size and chunk_size > 0:
202202
remainder = length % chunk_size
203203
if remainder:
204-
raw_bytes = b'\x00' + raw_bytes
204+
padding_size = chunk_size - remainder
205+
raw_bytes = "% {}s".format(length + padding_size).encode() % raw_bytes
205206
return raw_bytes

util/decode_priv_key.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright 2019 Google Inc.
2+
#
3+
# Modified by Brent Rubell for Adafruit Industries, 2019
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# Copyright 2019 Google Inc.
17+
#
18+
# Licensed under the Apache License, Version 2.0 (the "License");
19+
# you may not use this file except in compliance with the License.
20+
# You may obtain a copy of the License at
21+
#
22+
# http://www.apache.org/licenses/LICENSE-2.0
23+
#
24+
# Unless required by applicable law or agreed to in writing, software
25+
# distributed under the License is distributed on an "AS IS" BASIS,
26+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27+
# See the License for the specific language governing permissions and
28+
# limitations under the License.
29+
#
30+
"""
31+
`decode_priv_key.py`
32+
===================================================================
33+
34+
Generates RSA keys and decodes them using python-rsa
35+
for use with a CircuitPython secrets file.
36+
37+
This script is designed to run on a computer,
38+
NOT a CircuitPython device.
39+
40+
Requires Python-RSA (https://github.com/sybrenstuvel/python-rsa)
41+
42+
* Author(s): Google Inc., Brent Rubell
43+
"""
44+
import subprocess
45+
import rsa
46+
47+
# Generate private and public RSA keys
48+
proc = subprocess.Popen(
49+
["openssl", "genrsa", "-out", "rsa_private.pem", "2048"])
50+
proc.wait()
51+
proc = subprocess.Popen(
52+
["openssl", "rsa", "-in", "rsa_private.pem",
53+
"-pubout", "-out", "rsa_public.pem"]
54+
)
55+
proc.wait()
56+
57+
# Open generated private key file
58+
try:
59+
with open("rsa_private.pem", "rb") as file:
60+
private_key = file.read()
61+
except:
62+
print("No file named rsa_private.pem found in directory.")
63+
pk = rsa.PrivateKey.load_pkcs1(private_key)
64+
65+
print("Copy and paste this into your secrets.py file:\n")
66+
print("\"private_key\": " + str(pk)[10:] + ",")

0 commit comments

Comments
 (0)