]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/corecrypto/ccsha2/src/ccsha256_ltc_compress.c
2 * ccsha256_ltc_compress.c
5 * Created on 12/03/2010
7 * Copyright (c) 2010,2011,2015 Apple Inc. All rights reserved.
10 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
21 * Please obtain a copy of the License at
22 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
32 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
36 * Parts of this code adapted from LibTomCrypt
38 * LibTomCrypt, modular cryptographic library -- Tom St Denis
40 * LibTomCrypt is a library that provides various cryptographic
41 * algorithms in a highly modular and flexible manner.
43 * The library is free for all purposes without any express
46 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
49 #include <corecrypto/ccsha2.h>
50 #include <corecrypto/cc_priv.h>
51 #include "ccsha2_internal.h"
53 #if !CC_KERNEL || !CC_USE_ASM
55 #if CCSHA2_SHA256_USE_SHA512_K
56 #define K(i) ((uint32_t)(ccsha512_K[i] >> 32))
58 #define K(i) ccsha256_K[i]
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))
71 #define set_W(i) CC_LOAD32_BE(W[i], buf + (4 * (i)))
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); \
82 ccsha256_ltc_compress(ccdigest_state_t state
, size_t nblocks
, const void *in
)
84 uint32_t W
[64], t0
, t1
;
87 uint32_t *s
= ccdigest_u32(state
);
88 const unsigned char *buf
= in
;
92 for (i
= 0; i
< 16; i
+= 1) {
98 W
[i
] = Gamma1(W
[i
- 2]) + W
[i
- 7] + Gamma0(W
[i
- 15]) + W
[i
- 16];
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]);
126 for (i
= 0; i
< 64; i
+= 8) {
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);
148 buf
+= CCSHA256_BLOCK_SIZE
/ sizeof(buf
[0]);