]> git.cryptolib.org Git - arm-crypto-lib.git/blob - sha512/sha512.c
modified testing tools
[arm-crypto-lib.git] / sha512 / sha512.c
1 /* sha512.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2011 Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdint.h>
21 #include <string.h>
22 #include "sha512.h"
23
24 #include "cli.h"
25
26 static const
27 uint64_t sha512_init_values[8] = {
28 0x6a09e667f3bcc908LL, 0xbb67ae8584caa73bLL, 0x3c6ef372fe94f82bLL, 0xa54ff53a5f1d36f1LL,
29 0x510e527fade682d1LL, 0x9b05688c2b3e6c1fLL, 0x1f83d9abfb41bd6bLL, 0x5be0cd19137e2179LL
30 };
31
32 static const
33 uint64_t sha512_const[80] = {
34 0x428a2f98d728ae22LL, 0x7137449123ef65cdLL, 0xb5c0fbcfec4d3b2fLL, 0xe9b5dba58189dbbcLL,
35 0x3956c25bf348b538LL, 0x59f111f1b605d019LL, 0x923f82a4af194f9bLL, 0xab1c5ed5da6d8118LL,
36 0xd807aa98a3030242LL, 0x12835b0145706fbeLL, 0x243185be4ee4b28cLL, 0x550c7dc3d5ffb4e2LL,
37 0x72be5d74f27b896fLL, 0x80deb1fe3b1696b1LL, 0x9bdc06a725c71235LL, 0xc19bf174cf692694LL,
38 0xe49b69c19ef14ad2LL, 0xefbe4786384f25e3LL, 0x0fc19dc68b8cd5b5LL, 0x240ca1cc77ac9c65LL,
39 0x2de92c6f592b0275LL, 0x4a7484aa6ea6e483LL, 0x5cb0a9dcbd41fbd4LL, 0x76f988da831153b5LL,
40 0x983e5152ee66dfabLL, 0xa831c66d2db43210LL, 0xb00327c898fb213fLL, 0xbf597fc7beef0ee4LL,
41 0xc6e00bf33da88fc2LL, 0xd5a79147930aa725LL, 0x06ca6351e003826fLL, 0x142929670a0e6e70LL,
42 0x27b70a8546d22ffcLL, 0x2e1b21385c26c926LL, 0x4d2c6dfc5ac42aedLL, 0x53380d139d95b3dfLL,
43 0x650a73548baf63deLL, 0x766a0abb3c77b2a8LL, 0x81c2c92e47edaee6LL, 0x92722c851482353bLL,
44 0xa2bfe8a14cf10364LL, 0xa81a664bbc423001LL, 0xc24b8b70d0f89791LL, 0xc76c51a30654be30LL,
45 0xd192e819d6ef5218LL, 0xd69906245565a910LL, 0xf40e35855771202aLL, 0x106aa07032bbd1b8LL,
46 0x19a4c116b8d2d0c8LL, 0x1e376c085141ab53LL, 0x2748774cdf8eeb99LL, 0x34b0bcb5e19b48a8LL,
47 0x391c0cb3c5c95a63LL, 0x4ed8aa4ae3418acbLL, 0x5b9cca4f7763e373LL, 0x682e6ff3d6b2b8a3LL,
48 0x748f82ee5defb2fcLL, 0x78a5636f43172f60LL, 0x84c87814a1f0ab72LL, 0x8cc702081a6439ecLL,
49 0x90befffa23631e28LL, 0xa4506cebde82bde9LL, 0xbef9a3f7b2c67915LL, 0xc67178f2e372532bLL,
50 0xca273eceea26619cLL, 0xd186b8c721c0c207LL, 0xeada7dd6cde0eb1eLL, 0xf57d4f7fee6ed178LL,
51 0x06f067aa72176fbaLL, 0x0a637dc5a2c898a6LL, 0x113f9804bef90daeLL, 0x1b710b35131c471bLL,
52 0x28db77f523047d84LL, 0x32caab7b40c72493LL, 0x3c9ebe0a15c9bebcLL, 0x431d67c49c100d4cLL,
53 0x4cc5d4becb3e42b6LL, 0x597f299cfc657e2aLL, 0x5fcb6fab3ad6faecLL, 0x6c44198c4a475817LL
54 };
55
56
57 static const
58 uint64_t change_endian64(uint64_t x){
59         uint64_t r=0;
60         uint8_t i=8;
61         do{
62                 r <<= 8;
63                 r |= 0xff&x;
64                 x >>=8;
65         }while(--i);
66         return r;
67 }
68
69 static const
70 uint64_t rotr64(uint64_t x, uint8_t n){
71         return (x>>n)|(x<<(64-n));
72 }
73
74 static const
75 uint64_t rotl64(uint64_t x, uint8_t n){
76         return (x<<n)|(x>>(64-n));
77 }
78
79 void sha512_init(sha512_ctx_t* ctx){
80         ctx->length = 0;
81         memcpy(ctx->h, sha512_init_values, 8*8);
82 }
83
84 void sha512_ctx2hash(void* dest, const sha512_ctx_t* ctx){
85         uint8_t i=8;
86         do{
87                 *((uint64_t*)dest) = change_endian64(ctx->h[8-i]);
88                 dest = (uint8_t*)dest + 8;
89         }while(--i);
90 }
91
92 #define CH(x,y,z)  (((x)&(y))^((~(x))&(z)))
93 #define MAJ(x,y,z) (((x)&(y))^((x)&(z))^((y)&(z)))
94 #define SIGMA_0(x) (rotr64((x), 28) ^ rotl64((x), 30) ^ rotl64((x), 25))
95 #define SIGMA_1(x) (rotr64((x), 14) ^ rotr64((x), 18) ^ rotl64((x), 23))
96 #define SIGMA_a(x) (rotr64((x),  1) ^ rotr64((x),  8) ^ ((x)>>7))
97 #define SIGMA_b(x) (rotr64((x), 19) ^ rotl64((x),  3) ^ ((x)>>6))
98
99 void sha512_nextBlock(sha512_ctx_t* ctx, const void* block){
100         uint64_t w[16], wx;
101         uint64_t a[8];
102         uint64_t t1, t2;
103         const uint64_t *k=sha512_const;
104         uint8_t i;
105         i=16;
106         do{
107                 w[16-i] = change_endian64(*((const uint64_t*)block));
108                 block = (uint8_t*)block + 8;
109         }while(--i);
110         memcpy(a, ctx->h, 8*8);
111         for(i=0; i<80; ++i){
112                 if(i<16){
113                         wx=w[i];
114                 }else{
115                         wx = SIGMA_b(w[14]) + w[9] + SIGMA_a(w[1]) + w[0];
116                         memmove(&(w[0]), &(w[1]), 15*8);
117                         w[15] = wx;
118                 }
119                 t1 = a[7] + SIGMA_1(a[4]) + CH(a[4], a[5], a[6]) + *k++ + wx;
120                 t2 = SIGMA_0(a[0]) + MAJ(a[0], a[1], a[2]);
121                 memmove(&(a[1]), &(a[0]), 7*8);
122                 a[0] = t1 + t2;
123                 a[4] += t1;
124         }
125         i=7;
126         do{
127                 ctx->h[i] += a[i];
128         }while(i--);
129         ctx->length += 1;
130 }
131
132 void sha512_lastBlock(sha512_ctx_t* ctx, const void* block, uint16_t length_b){
133         while(length_b >= 1024){
134                 sha512_nextBlock(ctx, block);
135                 block = (uint8_t*)block + 1024/8;
136                 length_b -= 1024;
137         }
138         uint8_t buffer[1024/8];
139         uint64_t len;
140         len = ((uint64_t)ctx->length)*1024LL + length_b;
141         len = change_endian64(len);
142         memset(buffer, 0, 1024/8);
143         memcpy(buffer, block, (length_b+7)/8);
144         buffer[length_b/8] |= 0x80>>(length_b%8);
145         if(length_b>1024-128-1){
146                 /* length goes into the next block */
147                 sha512_nextBlock(ctx, buffer);
148                 memset(buffer, 0, 120);
149         }
150         memcpy(&(buffer[128-8]), &len, 8);
151         sha512_nextBlock(ctx, buffer);
152 }
153
154 void sha512(void* dest, const void* msg, uint32_t length_b){
155         sha512_ctx_t ctx;
156         sha512_init(&ctx);
157         while(length_b >= 1024){
158                 sha512_nextBlock(&ctx, msg);
159                 msg = (uint8_t*)msg + 1024/8;
160                 length_b -= 1024;
161         }
162         sha512_lastBlock(&ctx, msg, length_b);
163         sha512_ctx2hash(dest, &ctx);
164 }