summaryrefslogtreecommitdiff
path: root/security/nss/lib/freebl/sha1-armv8.c
blob: 63e4dad33e54f086f0ec8dad401001a5ac52517e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifdef USE_HW_SHA1

#ifndef __ARM_FEATURE_CRYPTO
#error "Compiler option is invalid"
#endif

#ifdef FREEBL_NO_DEPEND
#include "stubs.h"
#endif

#include <arm_neon.h>
#include <memory.h>
#include "blapi.h"
#include "sha_fast.h"

#if !defined(SHA_PUT_W_IN_STACK)
#define H2X 11
#else
#define H2X 0
#endif

static void shaCompress(SHA_HW_t *X, const PRUint32 *datain);

void
SHA1_Compress_Native(SHA1Context *ctx)
{
    shaCompress(&ctx->H[H2X], ctx->u.w);
}

/*
 *  SHA: Add data to context.
 */
void
SHA1_Update_Native(SHA1Context *ctx, const unsigned char *dataIn, unsigned int len)
{
    unsigned int lenB;
    unsigned int togo;

    if (!len) {
        return;
    }

    /* accumulate the byte count. */
    lenB = (unsigned int)(ctx->size) & 63U;

    ctx->size += len;

    /*
   *  Read the data into W and process blocks as they get full
   */
    if (lenB > 0) {
        togo = 64U - lenB;
        if (len < togo) {
            togo = len;
        }
        memcpy(ctx->u.b + lenB, dataIn, togo);
        len -= togo;
        dataIn += togo;
        lenB = (lenB + togo) & 63U;
        if (!lenB) {
            shaCompress(&ctx->H[H2X], ctx->u.w);
        }
    }

    while (len >= 64U) {
        len -= 64U;
        shaCompress(&ctx->H[H2X], (PRUint32 *)dataIn);
        dataIn += 64U;
    }

    if (len) {
        memcpy(ctx->u.b, dataIn, len);
    }
}

/*
 *  SHA: Compression function, unrolled.
 */
static void
shaCompress(SHA_HW_t *X, const PRUint32 *inbuf)
{
#define XH(n) X[n - H2X]

    const uint32x4_t K0 = vdupq_n_u32(0x5a827999);
    const uint32x4_t K1 = vdupq_n_u32(0x6ed9eba1);
    const uint32x4_t K2 = vdupq_n_u32(0x8f1bbcdc);
    const uint32x4_t K3 = vdupq_n_u32(0xca62c1d6);

    uint32x4_t abcd = vld1q_u32(&XH(0));
    PRUint32 e = XH(4);

    const uint32x4_t origABCD = abcd;
    const PRUint32 origE = e;

    uint32x4_t w0 = vld1q_u32(inbuf);
    uint32x4_t w1 = vld1q_u32(inbuf + 4);
    uint32x4_t w2 = vld1q_u32(inbuf + 8);
    uint32x4_t w3 = vld1q_u32(inbuf + 12);

    w0 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(w0)));
    w1 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(w1)));
    w2 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(w2)));
    w3 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(w3)));

    uint32x4_t t0 = vaddq_u32(w0, K0);
    uint32x4_t t1 = vaddq_u32(w1, K0);

    PRUint32 tmpE;

    /*
         * Using the following ARM instructions to accelerate SHA1
         *
         * sha1c for round 0 - 20
         * sha1p for round 20 - 40
         * sha1m for round 40 - 60
         * sha1p for round 60 - 80
         * sha1su0 and shasu1 for message schedule
         * sha1h for rotate left 30
         */

    /* Round 0-3 */
    tmpE = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1cq_u32(abcd, e, t0);
    t0 = vaddq_u32(w2, K0);
    w0 = vsha1su0q_u32(w0, w1, w2);

    /* Round 4-7 */
    e = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1cq_u32(abcd, tmpE, t1);
    t1 = vaddq_u32(w3, K0);
    w0 = vsha1su1q_u32(w0, w3);
    w1 = vsha1su0q_u32(w1, w2, w3);

    /* Round 8-11 */
    tmpE = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1cq_u32(abcd, e, t0);
    t0 = vaddq_u32(w0, K0);
    w1 = vsha1su1q_u32(w1, w0);
    w2 = vsha1su0q_u32(w2, w3, w0);

    /* Round 12-15 */
    e = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1cq_u32(abcd, tmpE, t1);
    t1 = vaddq_u32(w1, K1);
    w2 = vsha1su1q_u32(w2, w1);
    w3 = vsha1su0q_u32(w3, w0, w1);

    /* Round 16-19 */
    tmpE = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1cq_u32(abcd, e, t0);
    t0 = vaddq_u32(w2, K1);
    w3 = vsha1su1q_u32(w3, w2);
    w0 = vsha1su0q_u32(w0, w1, w2);

    /* Round 20-23 */
    e = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1pq_u32(abcd, tmpE, t1);
    t1 = vaddq_u32(w3, K1);
    w0 = vsha1su1q_u32(w0, w3);
    w1 = vsha1su0q_u32(w1, w2, w3);

    /* Round 24-27 */
    tmpE = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1pq_u32(abcd, e, t0);
    t0 = vaddq_u32(w0, K1);
    w1 = vsha1su1q_u32(w1, w0);
    w2 = vsha1su0q_u32(w2, w3, w0);

    /* Round 28-31 */
    e = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1pq_u32(abcd, tmpE, t1);
    t1 = vaddq_u32(w1, K1);
    w2 = vsha1su1q_u32(w2, w1);
    w3 = vsha1su0q_u32(w3, w0, w1);

    /* Round 32-35 */
    tmpE = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1pq_u32(abcd, e, t0);
    t0 = vaddq_u32(w2, K2);
    w3 = vsha1su1q_u32(w3, w2);
    w0 = vsha1su0q_u32(w0, w1, w2);

    /* Round 36-39 */
    e = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1pq_u32(abcd, tmpE, t1);
    t1 = vaddq_u32(w3, K2);
    w0 = vsha1su1q_u32(w0, w3);
    w1 = vsha1su0q_u32(w1, w2, w3);

    /* Round 40-43 */
    tmpE = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1mq_u32(abcd, e, t0);
    t0 = vaddq_u32(w0, K2);
    w1 = vsha1su1q_u32(w1, w0);
    w2 = vsha1su0q_u32(w2, w3, w0);

    /* Round 44-47 */
    e = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1mq_u32(abcd, tmpE, t1);
    t1 = vaddq_u32(w1, K2);
    w2 = vsha1su1q_u32(w2, w1);
    w3 = vsha1su0q_u32(w3, w0, w1);

    /* Round 48-51 */
    tmpE = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1mq_u32(abcd, e, t0);
    t0 = vaddq_u32(w2, K2);
    w3 = vsha1su1q_u32(w3, w2);
    w0 = vsha1su0q_u32(w0, w1, w2);

    /* Round 52-55 */
    e = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1mq_u32(abcd, tmpE, t1);
    t1 = vaddq_u32(w3, K3);
    w0 = vsha1su1q_u32(w0, w3);
    w1 = vsha1su0q_u32(w1, w2, w3);

    /* Round 56-59 */
    tmpE = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1mq_u32(abcd, e, t0);
    t0 = vaddq_u32(w0, K3);
    w1 = vsha1su1q_u32(w1, w0);
    w2 = vsha1su0q_u32(w2, w3, w0);

    /* Round 60-63 */
    e = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1pq_u32(abcd, tmpE, t1);
    t1 = vaddq_u32(w1, K3);
    w2 = vsha1su1q_u32(w2, w1);
    w3 = vsha1su0q_u32(w3, w0, w1);

    /* Round 64-67 */
    tmpE = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1pq_u32(abcd, e, t0);
    t0 = vaddq_u32(w2, K3);
    w3 = vsha1su1q_u32(w3, w2);
    w0 = vsha1su0q_u32(w0, w1, w2);

    /* Round 68-71 */
    e = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1pq_u32(abcd, tmpE, t1);
    t1 = vaddq_u32(w3, K3);
    w0 = vsha1su1q_u32(w0, w3);

    /* Round 72-75 */
    tmpE = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1pq_u32(abcd, e, t0);

    /* Round 76-79 */
    e = vsha1h_u32(vgetq_lane_u32(abcd, 0));
    abcd = vsha1pq_u32(abcd, tmpE, t1);

    e += origE;
    abcd = vaddq_u32(origABCD, abcd);

    vst1q_u32(&XH(0), abcd);
    XH(4) = e;
}

#endif /* USE_HW_SHA1 */