]> git.saurik.com Git - apple/xnu.git/blame - osfmk/corecrypto/ccsha256_ltc_compress.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / osfmk / corecrypto / ccsha256_ltc_compress.c
CommitLineData
5ba3f43e
A
1/*
2 * ccsha256_ltc_compress.c
3 * corecrypto
4 *
5 * Created on 12/03/2010
6 *
7 * Copyright (c) 2010,2011,2015 Apple Inc. All rights reserved.
8 *
9 *
10 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
11 *
12 * This file contains Original Code and/or Modifications of Original Code
13 * as defined in and that are subject to the Apple Public Source License
14 * Version 2.0 (the 'License'). You may not use this file except in
15 * compliance with the License. The rights granted to you under the License
16 * may not be used to create, or enable the creation or redistribution of,
17 * unlawful or unlicensed copies of an Apple operating system, or to
18 * circumvent, violate, or enable the circumvention or violation of, any
19 * terms of an Apple operating system software license agreement.
20 *
21 * Please obtain a copy of the License at
22 * http://www.opensource.apple.com/apsl/ and read it before using this file.
23 *
24 * The Original Code and all software distributed under the License are
25 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
26 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
27 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
29 * Please see the License for the specific language governing rights and
30 * limitations under the License.
31 *
32 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
33 */
34
35/*
36 * Parts of this code adapted from LibTomCrypt
37 *
38 * LibTomCrypt, modular cryptographic library -- Tom St Denis
39 *
40 * LibTomCrypt is a library that provides various cryptographic
41 * algorithms in a highly modular and flexible manner.
42 *
43 * The library is free for all purposes without any express
44 * guarantee it works.
45 *
46 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
47 */
48
49#include <corecrypto/ccsha2.h>
50#include <corecrypto/cc_priv.h>
51#include "ccsha2_internal.h"
52
d9a64523
A
53#if !CC_KERNEL || !CC_USE_ASM
54
cb323159
A
55#if CCSHA2_SHA256_USE_SHA512_K
56#define K(i) ((uint32_t)(ccsha512_K[i] >> 32))
5ba3f43e 57#else
cb323159 58#define K(i) ccsha256_K[i]
5ba3f43e
A
59#endif
60
cb323159
A
61// Various logical functions
62#define Ch(x, y, z) (z ^ (x & (y ^ z)))
63#define Maj(x, y, z) (((x | y) & z) | (x & y))
64#define S(x, n) CC_RORc(x, n)
65#define R(x, n) ((x) >> (n))
66#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
67#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
68#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
69#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
5ba3f43e 70
cb323159 71#define set_W(i) CC_LOAD32_BE(W[i], buf + (4 * (i)))
5ba3f43e
A
72
73// the round function
cb323159
A
74#define RND(a, b, c, d, e, f, g, h, i) \
75 t0 = h + Sigma1(e) + Ch(e, f, g) + K(i) + W[i]; \
76 t1 = Sigma0(a) + Maj(a, b, c); \
77 d += t0; \
78 h = t0 + t1;
5ba3f43e 79
d9a64523 80// compress 512-bits
0a7de745
A
81void
82ccsha256_ltc_compress(ccdigest_state_t state, size_t nblocks, const void *in)
5ba3f43e 83{
0a7de745 84 uint32_t W[64], t0, t1;
cb323159 85 uint32_t S[8];
0a7de745
A
86 int i;
87 uint32_t *s = ccdigest_u32(state);
0a7de745 88 const unsigned char *buf = in;
5ba3f43e 89
0a7de745
A
90 while (nblocks--) {
91 // schedule W 0..15
cb323159
A
92 for (i = 0; i < 16; i += 1) {
93 set_W(i);
94 }
0a7de745
A
95
96 // schedule W 16..63
cb323159 97 for (; i < 64; i++) {
0a7de745
A
98 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
99 }
100
101 // copy state into S
cb323159
A
102 S[0] = s[0];
103 S[1] = s[1];
104 S[2] = s[2];
105 S[3] = s[3];
106 S[4] = s[4];
107 S[5] = s[5];
108 S[6] = s[6];
109 S[7] = s[7];
0a7de745
A
110
111 // Compress
cb323159
A
112#if CC_SMALL_CODE
113 for (i = 0; i < 64; i += 1) {
114 t0 = S[7] + Sigma1(S[4]) + Ch(S[4], S[5], S[6]) + K(i) + W[i];
115 t1 = Sigma0(S[0]) + Maj(S[0], S[1], S[2]);
116 S[7] = S[6];
117 S[6] = S[5];
118 S[5] = S[4];
119 S[4] = S[3] + t0;
120 S[3] = S[2];
121 S[2] = S[1];
122 S[1] = S[0];
123 S[0] = t0 + t1;
124 }
125#else
0a7de745 126 for (i = 0; i < 64; i += 8) {
cb323159
A
127 RND(S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7], i + 0);
128 RND(S[7], S[0], S[1], S[2], S[3], S[4], S[5], S[6], i + 1);
129 RND(S[6], S[7], S[0], S[1], S[2], S[3], S[4], S[5], i + 2);
130 RND(S[5], S[6], S[7], S[0], S[1], S[2], S[3], S[4], i + 3);
131 RND(S[4], S[5], S[6], S[7], S[0], S[1], S[2], S[3], i + 4);
132 RND(S[3], S[4], S[5], S[6], S[7], S[0], S[1], S[2], i + 5);
133 RND(S[2], S[3], S[4], S[5], S[6], S[7], S[0], S[1], i + 6);
134 RND(S[1], S[2], S[3], S[4], S[5], S[6], S[7], S[0], i + 7);
0a7de745 135 }
cb323159 136#endif
0a7de745
A
137
138 // feedback
cb323159
A
139 s[0] += S[0];
140 s[1] += S[1];
141 s[2] += S[2];
142 s[3] += S[3];
143 s[4] += S[4];
144 s[5] += S[5];
145 s[6] += S[6];
146 s[7] += S[7];
0a7de745
A
147
148 buf += CCSHA256_BLOCK_SIZE / sizeof(buf[0]);
149 }
5ba3f43e 150}
d9a64523
A
151
152#endif