summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmaury Pouly <amaury.pouly@gmail.com>2017-01-03 16:09:34 +0100
committerAmaury Pouly <amaury.pouly@gmail.com>2017-01-16 19:59:28 +0100
commit759a78e5dff134f2632875f61aae60815eea6f5b (patch)
tree24110ff498d81535146094fdb80d766456bd513f
parent8b3f5a8ad7434850804a4a664d2b07c6ffa9b1c7 (diff)
downloadrockbox-759a78e5dff134f2632875f61aae60815eea6f5b.tar.gz
rockbox-759a78e5dff134f2632875f61aae60815eea6f5b.zip
imxtools/sbtools: switch SHA1 implementation to Crypto++
The current implementation was custom and super slow. Since we use Crypto++ anyway, we might as well get use a good implementation. Change-Id: I761ad7401653471e54000e1c2bc3d9882378112f
-rw-r--r--rbutil/mkimxboot/Makefile2
-rw-r--r--utils/imxtools/sbtools/Makefile4
-rw-r--r--utils/imxtools/sbtools/crypto.cpp23
-rw-r--r--utils/imxtools/sbtools/crypto.h6
-rw-r--r--utils/imxtools/sbtools/sha1.c150
5 files changed, 28 insertions, 157 deletions
diff --git a/rbutil/mkimxboot/Makefile b/rbutil/mkimxboot/Makefile
index e635f64103..132c2435f9 100644
--- a/rbutil/mkimxboot/Makefile
+++ b/rbutil/mkimxboot/Makefile
@@ -15,7 +15,7 @@ LDFLAGS += -lcrypto++
15OUTPUT = mkimxboot 15OUTPUT = mkimxboot
16 16
17# inputs for lib 17# inputs for lib
18IMXTOOLS_SOURCES = misc.c sb.c crypto.cpp crc.c sha1.c elf.c 18IMXTOOLS_SOURCES = misc.c sb.c crypto.cpp crc.c elf.c
19LIBSOURCES := dualboot.c mkimxboot.c md5.c \ 19LIBSOURCES := dualboot.c mkimxboot.c md5.c \
20 $(addprefix $(IMXTOOLS_DIR),$(IMXTOOLS_SOURCES)) 20 $(addprefix $(IMXTOOLS_DIR),$(IMXTOOLS_SOURCES))
21# inputs for binary only 21# inputs for binary only
diff --git a/utils/imxtools/sbtools/Makefile b/utils/imxtools/sbtools/Makefile
index f5eb8c16c4..e6d064b2a3 100644
--- a/utils/imxtools/sbtools/Makefile
+++ b/utils/imxtools/sbtools/Makefile
@@ -15,10 +15,10 @@ all: $(BINS)
15%.o: %.cpp 15%.o: %.cpp
16 $(CXX) $(CXXFLAGS) -c -o $@ $< 16 $(CXX) $(CXXFLAGS) -c -o $@ $<
17 17
18sbtoelf: sbtoelf.o crc.o crypto.o sha1.o xorcrypt.o dbparser.o elf.o misc.o sb.o sb1.o 18sbtoelf: sbtoelf.o crc.o crypto.o xorcrypt.o dbparser.o elf.o misc.o sb.o sb1.o
19 $(LD) -o $@ $^ $(LDFLAGS) 19 $(LD) -o $@ $^ $(LDFLAGS)
20 20
21elftosb: elftosb.o crc.o crypto.o sha1.o elf.o dbparser.o misc.o sb.o 21elftosb: elftosb.o crc.o crypto.o elf.o dbparser.o misc.o sb.o
22 $(LD) -o $@ $^ $(LDFLAGS) 22 $(LD) -o $@ $^ $(LDFLAGS)
23 23
24elftosb1: elftosb1.o xorcrypt.o elf.o misc.o sb1.o 24elftosb1: elftosb1.o xorcrypt.o elf.o misc.o sb1.o
diff --git a/utils/imxtools/sbtools/crypto.cpp b/utils/imxtools/sbtools/crypto.cpp
index 5ccde27fdd..d7ef04f098 100644
--- a/utils/imxtools/sbtools/crypto.cpp
+++ b/utils/imxtools/sbtools/crypto.cpp
@@ -22,6 +22,7 @@
22#include "misc.h" 22#include "misc.h"
23#include <cryptopp/modes.h> 23#include <cryptopp/modes.h>
24#include <cryptopp/aes.h> 24#include <cryptopp/aes.h>
25#include <cryptopp/sha.h>
25 26
26using namespace CryptoPP; 27using namespace CryptoPP;
27 28
@@ -124,3 +125,25 @@ int crypto_apply(
124 else 125 else
125 return CRYPTO_ERROR_BADSETUP; 126 return CRYPTO_ERROR_BADSETUP;
126} 127}
128
129void sha_1_init(struct sha_1_params_t *params)
130{
131 params->object = new SHA1;
132}
133
134void sha_1_update(struct sha_1_params_t *params, byte *buffer, int size)
135{
136 reinterpret_cast<SHA1 *>(params->object)->Update(buffer, size);
137}
138
139void sha_1_finish(struct sha_1_params_t *params)
140{
141 SHA1 *obj = reinterpret_cast<SHA1 *>(params->object);
142 obj->Final(params->hash);
143 delete obj;
144}
145
146void sha_1_output(struct sha_1_params_t *params, byte *out)
147{
148 memcpy(out, params->hash, 20);
149}
diff --git a/utils/imxtools/sbtools/crypto.h b/utils/imxtools/sbtools/crypto.h
index a282385cf2..e7ca31d72f 100644
--- a/utils/imxtools/sbtools/crypto.h
+++ b/utils/imxtools/sbtools/crypto.h
@@ -82,13 +82,11 @@ uint32_t crc_continue(uint32_t previous_crc, byte *data, int size);
82/* sha1.c */ 82/* sha1.c */
83struct sha_1_params_t 83struct sha_1_params_t
84{ 84{
85 uint32_t hash[5]; 85 byte hash[20]; /* final hash */
86 uint64_t buffer_nr_bits; 86 void *object; /* pointer to CryptoPP::SHA1 object */
87 uint32_t w[80];
88}; 87};
89 88
90void sha_1_init(struct sha_1_params_t *params); 89void sha_1_init(struct sha_1_params_t *params);
91void sha_1_block(struct sha_1_params_t *params, uint32_t cur_hash[5], byte *data);
92void sha_1_update(struct sha_1_params_t *params, byte *buffer, int size); 90void sha_1_update(struct sha_1_params_t *params, byte *buffer, int size);
93void sha_1_finish(struct sha_1_params_t *params); 91void sha_1_finish(struct sha_1_params_t *params);
94void sha_1_output(struct sha_1_params_t *params, byte *out); 92void sha_1_output(struct sha_1_params_t *params, byte *out);
diff --git a/utils/imxtools/sbtools/sha1.c b/utils/imxtools/sbtools/sha1.c
deleted file mode 100644
index cb34059166..0000000000
--- a/utils/imxtools/sbtools/sha1.c
+++ /dev/null
@@ -1,150 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2010 Amaury Pouly
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21/* Based on http://en.wikipedia.org/wiki/SHA-1 */
22#include "crypto.h"
23
24static uint32_t rot_left(uint32_t val, int rot)
25{
26 return (val << rot) | (val >> (32 - rot));
27}
28
29static inline void byte_swapxx(byte *ptr, int size)
30{
31 for(int i = 0; i < size / 2; i++)
32 {
33 byte c = ptr[i];
34 ptr[i] = ptr[size - i - 1];
35 ptr[size - i - 1] = c;
36 }
37}
38
39static void byte_swap32(uint32_t *v)
40{
41 byte_swapxx((byte *)v, 4);
42}
43
44void sha_1_init(struct sha_1_params_t *params)
45{
46 params->hash[0] = 0x67452301;
47 params->hash[1] = 0xEFCDAB89;
48 params->hash[2] = 0x98BADCFE;
49 params->hash[3] = 0x10325476;
50 params->hash[4] = 0xC3D2E1F0;
51 params->buffer_nr_bits = 0;
52}
53
54void sha_1_update(struct sha_1_params_t *params, byte *buffer, int size)
55{
56 int buffer_nr_bytes = (params->buffer_nr_bits / 8) % 64;
57 params->buffer_nr_bits += 8 * size;
58 int pos = 0;
59 if(buffer_nr_bytes + size >= 64)
60 {
61 pos = 64 - buffer_nr_bytes;
62 memcpy((byte *)(params->w) + buffer_nr_bytes, buffer, 64 - buffer_nr_bytes);
63 sha_1_block(params, params->hash, (byte *)params->w);
64 for(; pos + 64 <= size; pos += 64)
65 sha_1_block(params, params->hash, buffer + pos);
66 buffer_nr_bytes = 0;
67 }
68 memcpy((byte *)(params->w) + buffer_nr_bytes, buffer + pos, size - pos);
69}
70
71void sha_1_finish(struct sha_1_params_t *params)
72{
73 /* length (in bits) in big endian BEFORE preprocessing */
74 byte length_big_endian[8];
75 memcpy(length_big_endian, &params->buffer_nr_bits, 8);
76 byte_swapxx(length_big_endian, 8);
77 /* append '1' and then '0's to the message to get 448 bit length for the last block */
78 byte b = 0x80;
79 sha_1_update(params, &b, 1);
80 b = 0;
81 while((params->buffer_nr_bits % 512) != 448)
82 sha_1_update(params, &b, 1);
83 /* append length */
84 sha_1_update(params, length_big_endian, 8);
85 /* go back to big endian */
86 for(int i = 0; i < 5; i++)
87 byte_swap32(&params->hash[i]);
88}
89
90void sha_1_output(struct sha_1_params_t *params, byte *out)
91{
92 memcpy(out, params->hash, 20);
93}
94
95void sha_1_block(struct sha_1_params_t *params, uint32_t cur_hash[5], byte *data)
96{
97 uint32_t a, b, c, d, e;
98 a = cur_hash[0];
99 b = cur_hash[1];
100 c = cur_hash[2];
101 d = cur_hash[3];
102 e = cur_hash[4];
103
104 #define w params->w
105
106 memmove(w, data, 64);
107 for(int i = 0; i < 16; i++)
108 byte_swap32(&w[i]);
109
110 for(int i = 16; i <= 79; i++)
111 w[i] = rot_left(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
112
113 for(int i = 0; i<= 79; i++)
114 {
115 uint32_t f, k;
116 if(i <= 19)
117 {
118 f = (b & c) | ((~b) & d);
119 k = 0x5A827999;
120 }
121 else if(i <= 39)
122 {
123 f = b ^ c ^ d;
124 k = 0x6ED9EBA1;
125 }
126 else if(i <= 59)
127 {
128 f = (b & c) | (b & d) | (c & d);
129 k = 0x8F1BBCDC;
130 }
131 else
132 {
133 f = b ^ c ^ d;
134 k = 0xCA62C1D6;
135 }
136 uint32_t temp = rot_left(a, 5) + f + e + k + w[i];
137 e = d;
138 d = c;
139 c = rot_left(b, 30);
140 b = a;
141 a = temp;
142 }
143 #undef w
144
145 cur_hash[0] += a;
146 cur_hash[1] += b;
147 cur_hash[2] += c;
148 cur_hash[3] += d;
149 cur_hash[4] += e;
150}