├── receiver ├── build.bat ├── crc32.h ├── polarssl │ ├── md5.h │ ├── sha1.h │ ├── md5.c │ ├── sha1.c │ └── config.h ├── crc32.c └── main.c ├── sender ├── standalone │ ├── 8800.bin │ ├── 8E00.bin │ ├── build.bat │ └── start.asm ├── original_bin │ ├── pkm_audio_test.bin │ └── pkm_audio_test.txt ├── build.bat ├── charmap.asm └── sender.asm ├── .gitmodules ├── LICENSE └── README.md /receiver/build.bat: -------------------------------------------------------------------------------- 1 | gcc main.c crc32.c polarssl/md5.c polarssl/sha1.c -O2 -Wall -Wextra -s -static -o receiver 2 | pause -------------------------------------------------------------------------------- /sender/standalone/8800.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FIX94/gameboy-audio-dumper/HEAD/sender/standalone/8800.bin -------------------------------------------------------------------------------- /sender/standalone/8E00.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FIX94/gameboy-audio-dumper/HEAD/sender/standalone/8E00.bin -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "installer"] 2 | path = installer 3 | url = https://github.com/FIX94/pkm-yellow-codeinstaller.git 4 | -------------------------------------------------------------------------------- /sender/original_bin/pkm_audio_test.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FIX94/gameboy-audio-dumper/HEAD/sender/original_bin/pkm_audio_test.bin -------------------------------------------------------------------------------- /sender/standalone/build.bat: -------------------------------------------------------------------------------- 1 | rgbasm -o start.o start.asm 2 | rgblink -o gb-audio-dumper.gb start.o 3 | rgbfix -i GDMP -j -k 00 -l 0x33 -m 0 -n 0 -p 0 -r 0 -t GBDMP -v gb-audio-dumper.gb 4 | 5 | pause -------------------------------------------------------------------------------- /receiver/crc32.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef CRC32_H 3 | #define CRC32_H 4 | 5 | #define UPDC32(octet, crc) (crc_32_tab[((crc)\ 6 | ^ (octet)) & 0xff] ^ ((crc) >> 8)) 7 | 8 | unsigned int crc32buffer(const unsigned char *buffer, const unsigned int len, unsigned int oldcrc32); 9 | unsigned int crc32simple(void *buf, unsigned int size); 10 | 11 | #endif /* CRC32_H */ 12 | -------------------------------------------------------------------------------- /sender/build.bat: -------------------------------------------------------------------------------- 1 | del gelb.bin gelb.o 2 | rgbasm -o gelb.o -DRAMOFFSET=$DA84 sender.asm 3 | python -m rgbbin gelb.o 4 | ren WRAM.bin gelb.bin 5 | 6 | del yellow.bin yellow.o 7 | rgbasm -o yellow.o -DRAMOFFSET=$DA7F sender.asm 8 | python -m rgbbin yellow.o 9 | ren WRAM.bin yellow.bin 10 | 11 | del standalone.bin standalone.o 12 | rgbasm -o standalone.o -DRAMOFFSET=$C000 sender.asm 13 | python -m rgbbin standalone.o 14 | ren WRAM.bin standalone.bin 15 | 16 | pause -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 FIX94 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /sender/charmap.asm: -------------------------------------------------------------------------------- 1 | ; We just re-use the games character map still in memory 2 | ; here to save a significant amount of space 3 | 4 | charmap " ", $7F 5 | charmap "A", $80 6 | charmap "B", $81 7 | charmap "C", $82 8 | charmap "D", $83 9 | charmap "E", $84 10 | charmap "F", $85 11 | charmap "G", $86 12 | charmap "H", $87 13 | charmap "I", $88 14 | charmap "J", $89 15 | charmap "K", $8A 16 | charmap "L", $8B 17 | charmap "M", $8C 18 | charmap "N", $8D 19 | charmap "O", $8E 20 | charmap "P", $8F 21 | charmap "Q", $90 22 | charmap "R", $91 23 | charmap "S", $92 24 | charmap "T", $93 25 | charmap "U", $94 26 | charmap "V", $95 27 | charmap "W", $96 28 | charmap "X", $97 29 | charmap "Y", $98 30 | charmap "Z", $99 31 | 32 | charmap "(", $9A 33 | charmap ")", $9B 34 | charmap ":", $9C 35 | charmap ";", $9D 36 | charmap "[", $9E 37 | charmap "]", $9F 38 | 39 | charmap "a", $A0 40 | charmap "b", $A1 41 | charmap "c", $A2 42 | charmap "d", $A3 43 | charmap "e", $A4 44 | charmap "f", $A5 45 | charmap "g", $A6 46 | charmap "h", $A7 47 | charmap "i", $A8 48 | charmap "j", $A9 49 | charmap "k", $AA 50 | charmap "l", $AB 51 | charmap "m", $AC 52 | charmap "n", $AD 53 | charmap "o", $AE 54 | charmap "p", $AF 55 | charmap "q", $B0 56 | charmap "r", $B1 57 | charmap "s", $B2 58 | charmap "t", $B3 59 | charmap "u", $B4 60 | charmap "v", $B5 61 | charmap "w", $B6 62 | charmap "x", $B7 63 | charmap "y", $B8 64 | charmap "z", $B9 65 | 66 | charmap "?", $E6 67 | charmap "!", $E7 68 | charmap ".", $E8 69 | charmap "/", $F3 70 | charmap ",", $F4 71 | 72 | charmap "0", $F6 73 | charmap "1", $F7 74 | charmap "2", $F8 75 | charmap "3", $F9 76 | charmap "4", $FA 77 | charmap "5", $FB 78 | charmap "6", $FC 79 | charmap "7", $FD 80 | charmap "8", $FE 81 | charmap "9", $FF 82 | -------------------------------------------------------------------------------- /sender/standalone/start.asm: -------------------------------------------------------------------------------- 1 | ; Copyright (C) 2018 FIX94 2 | ; 3 | ; This software may be modified and distributed under the terms 4 | ; of the MIT license. See the LICENSE file for details. 5 | 6 | SECTION "HDR",ROM0[$0] 7 | 8 | rstbase: 9 | ; if any RST lands here, return 10 | ds $FF 11 | reti 12 | 13 | entry: 14 | ; 100, goto start 15 | nop 16 | jp _start 17 | 18 | SECTION "MAIN",ROM0[$150] 19 | 20 | _start: 21 | di 22 | ; init stack pointer 23 | ld sp, $CFFF 24 | ; make sure lcd is off 25 | ldh a, [$FF40] 26 | bit 7, a 27 | jr z, isoff 28 | ; not off, wait for vblank 29 | waitVBlank: 30 | ldh a, [$FF44] 31 | cp 145 32 | jr nz, waitVBlank 33 | ; turn off lcd 34 | xor a 35 | ldh [$FF40], a 36 | isoff: 37 | ; copy in font 38 | ld hl, $8800 39 | ld de, _8800_bin 40 | ld bc, $400 41 | call _memcpy 42 | ld hl, $8E00 43 | ld de, _8E00_bin 44 | ld bc, $200 45 | call _memcpy 46 | ; clear font "space" tile 47 | ld hl, $97F0 48 | xor a 49 | bartileloop: 50 | ld [hl+], a 51 | bit 3, h 52 | jr z, bartileloop 53 | ; set font color 54 | ld a, $E4 55 | ldh [$FF47], a 56 | ; turn on lcd 57 | ld a, $81 58 | ldh [$FF40], a 59 | ; copy in RAM code 60 | ld hl, $C000 61 | ld de, standalone_bin 62 | ld bc, (standalone_bin_end-standalone_bin) 63 | call _memcpy 64 | ; jump to RAM code 65 | jp $C000 66 | 67 | _memcpy: 68 | inc b 69 | inc c 70 | jr _memcpy_chk 71 | _memcpy_loop: 72 | ld a, [de] 73 | inc de 74 | ld [hl+], a 75 | _memcpy_chk: 76 | dec c 77 | jr nz, _memcpy_loop 78 | dec b 79 | jr nz, _memcpy_loop 80 | ret 81 | 82 | ; font bin to copy into vram 83 | _8800_bin: 84 | INCBIN "8800.bin" 85 | 86 | _8E00_bin: 87 | INCBIN "8E00.bin" 88 | 89 | ; actual dumper code 90 | standalone_bin: 91 | INCBIN "../standalone.bin" 92 | standalone_bin_end: 93 | ; label just for size calc 94 | -------------------------------------------------------------------------------- /sender/original_bin/pkm_audio_test.txt: -------------------------------------------------------------------------------- 1 | 2 | at da84, executed by glitch item ws l' m: 3 | 4 | init: 5 | F3 - di 6 | 3E 00 - lda,0 7 | e0 26 - ldh FF26, a 8 | 3E 80 - lda,0x80 9 | e0 26 - ldh FF26, a 10 | 3E F0 - lda,0xF0 11 | e0 13 - ldh FF13, a 12 | e0 12 - ldh FF12, a 13 | 3E 80 - lda,0x80 14 | e0 11 - ldh FF11, a 15 | 3E 87 - lda,0x87 16 | e0 14 - ldh FF14, a 17 | 3E 10 - lda,0x10 18 | e0 00 - ldh FF00, a 19 | waitpress: 20 | f0 00 - ldh a, FF00 21 | e6 08 - and 8 22 | 20 fa - jr nz, waitpress 23 | regprep: 24 | 26 00 - ld h,0 25 | 2e 00 - ld l,0 26 | 16 db - ld d,0xdb //vollut addr high 0xdb 27 | 0e 00 - ld c,0 28 | testsend: 29 | 3E 0F - lda,0xF 30 | cd e1 da - call subfunc1 31 | 3E 0A - lda,0xA 32 | cd e1 da - call subfunc1 33 | 3E 05 - lda,5 34 | cd e1 da - call subfunc1 35 | 3E 00 - lda,0 36 | cd e1 da - call subfunc1 37 | loop: 38 | 2A - ld a,(hl+) 39 | 47 - ld b,a 40 | cd e1 da - call subfunc1 41 | 78 - ld a,b 42 | cb 37 - swap a 43 | cd e1 da - call subfunc1 44 | 2c - inc l 45 | 2d - dec l 46 | 20 f1 - jr nz, loop 47 | 7c - ld a,h 48 | e6 3f - and 0x3F 49 | 20 ec - jr nz, loop 50 | 0c - inc c 51 | cb 71 - bit 6,c 52 | end: 53 | 20 fe - jr nz, end 54 | 26 20 - ld h,0x20 55 | 71 - ld (hl),c 56 | 26 40 - ld h,0x40 57 | 18 e0 - jr loop 58 | 59 | subfunc1: 60 | e6 0F - and 0xF 61 | c6 00 - add a,0x00 //vollut addr low 0x00 62 | 5f - ld e,a 63 | 3E 11 - lda,0x11 64 | e0 25 - ldh FF25, a 65 | 1a - ld a,(de) 66 | e0 24 - ldh FF24, a 67 | cd fa da - call subfunc2 68 | 3E 00 - lda,0 69 | e0 24 - ldh FF24, a 70 | e0 25 - ldh FF25, a 71 | cd fa da - call subfunc2 72 | c9 - ret 73 | 74 | subfunc2: 75 | 3E 1e - lda,0x1E 76 | sf2dec: 77 | 3D - dec a 78 | 20 FD - jr nz, sf2dec 79 | c9 - ret 80 | 81 | vollut at DB00: 82 | 11 13 15 17 31 33 35 37 51 53 55 57 71 73 75 77 83 | -------------------------------------------------------------------------------- /receiver/polarssl/md5.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file md5.h 3 | * 4 | * \brief MD5 message digest algorithm (hash function) 5 | * 6 | * Copyright (C) 2006-2013, Brainspark B.V. 7 | * 8 | * This file is part of PolarSSL (http://www.polarssl.org) 9 | * Lead Maintainer: Paul Bakker 10 | * 11 | * All rights reserved. 12 | * 13 | * This program is free software; you can redistribute it and/or modify 14 | * it under the terms of the GNU General Public License as published by 15 | * the Free Software Foundation; either version 2 of the License, or 16 | * (at your option) any later version. 17 | * 18 | * This program is distributed in the hope that it will be useful, 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 | * GNU General Public License for more details. 22 | * 23 | * You should have received a copy of the GNU General Public License along 24 | * with this program; if not, write to the Free Software Foundation, Inc., 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 26 | */ 27 | #ifndef POLARSSL_MD5_H 28 | #define POLARSSL_MD5_H 29 | 30 | #include "polarssl/config.h" 31 | 32 | #include 33 | 34 | #ifdef _MSC_VER 35 | #include 36 | typedef UINT32 uint32_t; 37 | #else 38 | #include 39 | #endif 40 | 41 | #define POLARSSL_ERR_MD5_FILE_IO_ERROR -0x0074 /**< Read/write error in file. */ 42 | 43 | #if !defined(POLARSSL_MD5_ALT) 44 | // Regular implementation 45 | // 46 | 47 | /** 48 | * \brief MD5 context structure 49 | */ 50 | typedef struct 51 | { 52 | uint32_t total[2]; /*!< number of bytes processed */ 53 | uint32_t state[4]; /*!< intermediate digest state */ 54 | unsigned char buffer[64]; /*!< data block being processed */ 55 | 56 | unsigned char ipad[64]; /*!< HMAC: inner padding */ 57 | unsigned char opad[64]; /*!< HMAC: outer padding */ 58 | } 59 | md5_context; 60 | 61 | #ifdef __cplusplus 62 | extern "C" { 63 | #endif 64 | 65 | /** 66 | * \brief MD5 context setup 67 | * 68 | * \param ctx context to be initialized 69 | */ 70 | void md5_starts( md5_context *ctx ); 71 | 72 | /** 73 | * \brief MD5 process buffer 74 | * 75 | * \param ctx MD5 context 76 | * \param input buffer holding the data 77 | * \param ilen length of the input data 78 | */ 79 | void md5_update( md5_context *ctx, const unsigned char *input, size_t ilen ); 80 | 81 | /** 82 | * \brief MD5 final digest 83 | * 84 | * \param ctx MD5 context 85 | * \param output MD5 checksum result 86 | */ 87 | void md5_finish( md5_context *ctx, unsigned char output[16] ); 88 | 89 | /* Internal use */ 90 | void md5_process( md5_context *ctx, const unsigned char data[64] ); 91 | 92 | #ifdef __cplusplus 93 | } 94 | #endif 95 | 96 | #else /* POLARSSL_MD5_ALT */ 97 | #include "polarssl/md5_alt.h" 98 | #endif /* POLARSSL_MD5_ALT */ 99 | 100 | #ifdef __cplusplus 101 | extern "C" { 102 | #endif 103 | 104 | /** 105 | * \brief Output = MD5( input buffer ) 106 | * 107 | * \param input buffer holding the data 108 | * \param ilen length of the input data 109 | * \param output MD5 checksum result 110 | */ 111 | void md5( const unsigned char *input, size_t ilen, unsigned char output[16] ); 112 | 113 | /** 114 | * \brief Output = MD5( file contents ) 115 | * 116 | * \param path input file name 117 | * \param output MD5 checksum result 118 | * 119 | * \return 0 if successful, or POLARSSL_ERR_MD5_FILE_IO_ERROR 120 | */ 121 | int md5_file( const char *path, unsigned char output[16] ); 122 | 123 | /** 124 | * \brief MD5 HMAC context setup 125 | * 126 | * \param ctx HMAC context to be initialized 127 | * \param key HMAC secret key 128 | * \param keylen length of the HMAC key 129 | */ 130 | void md5_hmac_starts( md5_context *ctx, 131 | const unsigned char *key, size_t keylen ); 132 | 133 | /** 134 | * \brief MD5 HMAC process buffer 135 | * 136 | * \param ctx HMAC context 137 | * \param input buffer holding the data 138 | * \param ilen length of the input data 139 | */ 140 | void md5_hmac_update( md5_context *ctx, 141 | const unsigned char *input, size_t ilen ); 142 | 143 | /** 144 | * \brief MD5 HMAC final digest 145 | * 146 | * \param ctx HMAC context 147 | * \param output MD5 HMAC checksum result 148 | */ 149 | void md5_hmac_finish( md5_context *ctx, unsigned char output[16] ); 150 | 151 | /** 152 | * \brief MD5 HMAC context reset 153 | * 154 | * \param ctx HMAC context to be reset 155 | */ 156 | void md5_hmac_reset( md5_context *ctx ); 157 | 158 | /** 159 | * \brief Output = HMAC-MD5( hmac key, input buffer ) 160 | * 161 | * \param key HMAC secret key 162 | * \param keylen length of the HMAC key 163 | * \param input buffer holding the data 164 | * \param ilen length of the input data 165 | * \param output HMAC-MD5 result 166 | */ 167 | void md5_hmac( const unsigned char *key, size_t keylen, 168 | const unsigned char *input, size_t ilen, 169 | unsigned char output[16] ); 170 | 171 | /** 172 | * \brief Checkup routine 173 | * 174 | * \return 0 if successful, or 1 if the test failed 175 | */ 176 | int md5_self_test( int verbose ); 177 | 178 | #ifdef __cplusplus 179 | } 180 | #endif 181 | 182 | #endif /* md5.h */ 183 | -------------------------------------------------------------------------------- /receiver/polarssl/sha1.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file sha1.h 3 | * 4 | * \brief SHA-1 cryptographic hash function 5 | * 6 | * Copyright (C) 2006-2013, Brainspark B.V. 7 | * 8 | * This file is part of PolarSSL (http://www.polarssl.org) 9 | * Lead Maintainer: Paul Bakker 10 | * 11 | * All rights reserved. 12 | * 13 | * This program is free software; you can redistribute it and/or modify 14 | * it under the terms of the GNU General Public License as published by 15 | * the Free Software Foundation; either version 2 of the License, or 16 | * (at your option) any later version. 17 | * 18 | * This program is distributed in the hope that it will be useful, 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 | * GNU General Public License for more details. 22 | * 23 | * You should have received a copy of the GNU General Public License along 24 | * with this program; if not, write to the Free Software Foundation, Inc., 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 26 | */ 27 | #ifndef POLARSSL_SHA1_H 28 | #define POLARSSL_SHA1_H 29 | 30 | #include "polarssl/config.h" 31 | 32 | #include 33 | 34 | #ifdef _MSC_VER 35 | #include 36 | typedef UINT32 uint32_t; 37 | #else 38 | #include 39 | #endif 40 | 41 | #define POLARSSL_ERR_SHA1_FILE_IO_ERROR -0x0076 /**< Read/write error in file. */ 42 | 43 | #if !defined(POLARSSL_SHA1_ALT) 44 | // Regular implementation 45 | // 46 | 47 | /** 48 | * \brief SHA-1 context structure 49 | */ 50 | typedef struct 51 | { 52 | uint32_t total[2]; /*!< number of bytes processed */ 53 | uint32_t state[5]; /*!< intermediate digest state */ 54 | unsigned char buffer[64]; /*!< data block being processed */ 55 | 56 | unsigned char ipad[64]; /*!< HMAC: inner padding */ 57 | unsigned char opad[64]; /*!< HMAC: outer padding */ 58 | } 59 | sha1_context; 60 | 61 | #ifdef __cplusplus 62 | extern "C" { 63 | #endif 64 | 65 | /** 66 | * \brief SHA-1 context setup 67 | * 68 | * \param ctx context to be initialized 69 | */ 70 | void sha1_starts( sha1_context *ctx ); 71 | 72 | /** 73 | * \brief SHA-1 process buffer 74 | * 75 | * \param ctx SHA-1 context 76 | * \param input buffer holding the data 77 | * \param ilen length of the input data 78 | */ 79 | void sha1_update( sha1_context *ctx, const unsigned char *input, size_t ilen ); 80 | 81 | /** 82 | * \brief SHA-1 final digest 83 | * 84 | * \param ctx SHA-1 context 85 | * \param output SHA-1 checksum result 86 | */ 87 | void sha1_finish( sha1_context *ctx, unsigned char output[20] ); 88 | 89 | /* Internal use */ 90 | void sha1_process( sha1_context *ctx, const unsigned char data[64] ); 91 | 92 | #ifdef __cplusplus 93 | } 94 | #endif 95 | 96 | #else /* POLARSSL_SHA1_ALT */ 97 | #include "polarssl/sha1_alt.h" 98 | #endif /* POLARSSL_SHA1_ALT */ 99 | 100 | #ifdef __cplusplus 101 | extern "C" { 102 | #endif 103 | 104 | /** 105 | * \brief Output = SHA-1( input buffer ) 106 | * 107 | * \param input buffer holding the data 108 | * \param ilen length of the input data 109 | * \param output SHA-1 checksum result 110 | */ 111 | void sha1( const unsigned char *input, size_t ilen, unsigned char output[20] ); 112 | 113 | /** 114 | * \brief Output = SHA-1( file contents ) 115 | * 116 | * \param path input file name 117 | * \param output SHA-1 checksum result 118 | * 119 | * \return 0 if successful, or POLARSSL_ERR_SHA1_FILE_IO_ERROR 120 | */ 121 | int sha1_file( const char *path, unsigned char output[20] ); 122 | 123 | /** 124 | * \brief SHA-1 HMAC context setup 125 | * 126 | * \param ctx HMAC context to be initialized 127 | * \param key HMAC secret key 128 | * \param keylen length of the HMAC key 129 | */ 130 | void sha1_hmac_starts( sha1_context *ctx, const unsigned char *key, size_t keylen ); 131 | 132 | /** 133 | * \brief SHA-1 HMAC process buffer 134 | * 135 | * \param ctx HMAC context 136 | * \param input buffer holding the data 137 | * \param ilen length of the input data 138 | */ 139 | void sha1_hmac_update( sha1_context *ctx, const unsigned char *input, size_t ilen ); 140 | 141 | /** 142 | * \brief SHA-1 HMAC final digest 143 | * 144 | * \param ctx HMAC context 145 | * \param output SHA-1 HMAC checksum result 146 | */ 147 | void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] ); 148 | 149 | /** 150 | * \brief SHA-1 HMAC context reset 151 | * 152 | * \param ctx HMAC context to be reset 153 | */ 154 | void sha1_hmac_reset( sha1_context *ctx ); 155 | 156 | /** 157 | * \brief Output = HMAC-SHA-1( hmac key, input buffer ) 158 | * 159 | * \param key HMAC secret key 160 | * \param keylen length of the HMAC key 161 | * \param input buffer holding the data 162 | * \param ilen length of the input data 163 | * \param output HMAC-SHA-1 result 164 | */ 165 | void sha1_hmac( const unsigned char *key, size_t keylen, 166 | const unsigned char *input, size_t ilen, 167 | unsigned char output[20] ); 168 | 169 | /** 170 | * \brief Checkup routine 171 | * 172 | * \return 0 if successful, or 1 if the test failed 173 | */ 174 | int sha1_self_test( int verbose ); 175 | 176 | #ifdef __cplusplus 177 | } 178 | #endif 179 | 180 | #endif /* sha1.h */ 181 | -------------------------------------------------------------------------------- /receiver/crc32.c: -------------------------------------------------------------------------------- 1 | 2 | /* Copyright (C) 1986 Gary S. Brown. You may use this program, or 3 | code or tables extracted from it, as desired without restriction.*/ 4 | 5 | /* First, the polynomial itself and its table of feedback terms. The */ 6 | /* polynomial is */ 7 | /* X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0 */ 8 | /* Note that we take it "backwards" and put the highest-order term in */ 9 | /* the lowest-order bit. The X^32 term is "implied"; the LSB is the */ 10 | /* X^31 term, etc. The X^0 term (usually shown as "+1") results in */ 11 | /* the MSB being 1. */ 12 | 13 | /* Note that the usual hardware shift register implementation, which */ 14 | /* is what we're using (we're merely optimizing it by doing eight-bit */ 15 | /* chunks at a time) shifts bits into the lowest-order term. In our */ 16 | /* implementation, that means shifting towards the right. Why do we */ 17 | /* do it this way? Because the calculated CRC must be transmitted in */ 18 | /* order from highest-order term to lowest-order term. UARTs transmit */ 19 | /* characters in order from LSB to MSB. By storing the CRC this way, */ 20 | /* we hand it to the UART in the order low-byte to high-byte; the UART */ 21 | /* sends each low-bit to hight-bit; and the result is transmission bit */ 22 | /* by bit from highest- to lowest-order term without requiring any bit */ 23 | /* shuffling on our part. Reception works similarly. */ 24 | 25 | /* The feedback terms table consists of 256, 32-bit entries. Notes: */ 26 | /* */ 27 | /* 1. The table can be generated at runtime if desired; code to do so */ 28 | /* is shown later. It might not be obvious, but the feedback */ 29 | /* terms simply represent the results of eight shift/xor opera- */ 30 | /* tions for all combinations of data and CRC register values. */ 31 | /* */ 32 | /* 2. The CRC accumulation logic is the same for all CRC polynomials, */ 33 | /* be they sixteen or thirty-two bits wide. You simply choose the */ 34 | /* appropriate table. Alternatively, because the table can be */ 35 | /* generated at runtime, you can start by generating the table for */ 36 | /* the polynomial in question and use exactly the same "updcrc", */ 37 | /* if your application needn't simultaneously handle two CRC */ 38 | /* polynomials. (Note, however, that XMODEM is strange.) */ 39 | /* */ 40 | /* 3. For 16-bit CRCs, the table entries need be only 16 bits wide; */ 41 | /* of course, 32-bit entries work OK if the high 16 bits are zero. */ 42 | /* */ 43 | /* 4. The values must be right-shifted by eight bits by the "updcrc" */ 44 | /* logic; the shift must be unsigned (bring in zeroes). On some */ 45 | /* hardware you could probably optimize the shift in assembler by */ 46 | /* using byte-swap instructions. */ 47 | 48 | #include 49 | #include "crc32.h" 50 | 51 | #define FILEBUFFER 0x200000 /* 2MB */ 52 | 53 | static unsigned int crc_32_tab[] = { /* CRC polynomial 0xedb88320 */ 54 | 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 55 | 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 56 | 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 57 | 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 58 | 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 59 | 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 60 | 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 61 | 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 62 | 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 63 | 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 64 | 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, 65 | 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, 66 | 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 67 | 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 68 | 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 69 | 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 70 | 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 71 | 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 72 | 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 73 | 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 74 | 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 75 | 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 76 | 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 77 | 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 78 | 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 79 | 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 80 | 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, 81 | 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 82 | 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 83 | 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 84 | 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 85 | 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 86 | 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 87 | 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 88 | 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 89 | 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 90 | 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 91 | 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 92 | 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 93 | 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 94 | 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 95 | 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 96 | 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d 97 | }; 98 | 99 | unsigned int crc32buffer(const unsigned char *buffer, const unsigned int len, unsigned int oldcrc32) 100 | { 101 | unsigned int i; 102 | for(i = 0; i < len; i++) 103 | oldcrc32 = UPDC32(buffer[i], oldcrc32); 104 | return oldcrc32; 105 | } 106 | 107 | unsigned int crc32simple(void *buf, unsigned int size) 108 | { 109 | unsigned int oldcrc32 = 0xFFFFFFFF; 110 | oldcrc32 = crc32buffer(buf, size, oldcrc32); 111 | return oldcrc32 = ~oldcrc32; 112 | } 113 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GameBoy Audio Dumper 2 | This project allows you to dump gameboy cartridges, both ROMs and Saves, using the audio output. 3 | It is intended for a gameboy color as you can hotswap cartridges on it, making this actually usable. 4 | 5 | # Important 6 | There may be a chance on certain carts that the save file may get damaged when you insert them while the gameboy color is already running, my cart of tetris dx appears to always wipe the first byte of the save when inserted into an already running gameboy color, no matter if the dumper is running on it or if it is any other game or just the bootup screen, this does only happen on this one cart though that uses the old MBC1 chip on it, my other carts that have newer MBC2 and MBC5 chips on them dont have any corruption like that, so use this with older carts at your own risk. 7 | 8 | # General Requirements 9 | - GameBoy Color so you can hotswap cartridges 10 | - A way to boot the sender, see requirements below 11 | - Male to Male 3.5mm Stereo Cable or similar to record the Headphone Output of your GameBoy Color 12 | - PC to record and convert the ROM/Save on 13 | 14 | # Sender Method 1 Requirements 15 | - A GameBoy Flashcart or similar way to boot the small 32KB Sender ROM 16 | # Sender Method 2 Requirements 17 | - Copy of either the English or German Version of Pokemon Yellow 18 | - A way to copy a new save file onto said cartridge using whatever copy method you have 19 | # Sender Method 3 Requirements 20 | - Copy of either the English or German Version of Pokemon Yellow 21 | - GameCube with a GameBoy Player, a SD Gecko and a method to run Homebrew 22 | 23 | # Sender Method 1 Installation and Bootup 24 | 1. Grab the current release from the "release" tab on top of this page and put the .gb rom from the "sender/stanalone-gb-file" onto your flashcart or flash the ROM using similar methods. 25 | 2. Now just boot that ROM in a gameboy color and follow the general sender and receiver usage below 26 | # Sender Method 2 Installation and Bootup 27 | 1. Grab the current release from the "release" tab on top of this page and flash the .sav file from the "sender/pokemon-yellow/sav-file" onto your actual pokemon cart. 28 | 2. Put your cart into a GameBoy Color, get into the game and start the dumper by going into the item menu and use the glitch item ws m, it is the very first one in the list and follow the general sender and receiver usage below 29 | # Sender Method 3 Installation and Bootup 30 | 1. Make sure you have both Swiss-GC and GameBoy Interface on your SD Gecko: 31 | https://github.com/emukidid/swiss-gc/releases 32 | https://www.gc-forever.com/forums/viewtopic.php?t=2782 33 | 2. Grab the current release from the "release" tab on top of this page and put the GBI folder from the "sender/pokemon-yellow/gbi-files/installer" folder onto your SD Gecko root, or if you already ran the installer on this cart before, put on the "updater" folder instead 34 | 3. Put either gbi-fix94-pokemongelb-installer.txt.cli or gbi-fix94-pokemonyellow-installer.txt.cli (or updater instead) renamed to gbi.cli depending on the game language you have onto your SD Gecko root as well and make sure GameBoy Interface is also on the root with the name gbi.dol 35 | 4. When using the "installer", clear whatever save you have on your pokemon cartridge by holding down Up+Select+Start on the title screen, skip this with the "updater" 36 | 5. Run the GameBoy Interface gbi.dol on your SD Gecko root from Swiss-GC, it will now install the dumper onto your cart, just wait for it to save+reset the game, at that point you can turn off your GameCube 37 | 6. Put your cart into a GameBoy Color, get into the game and start the dumper by going into the item menu and use the glitch item ws m, it is the very first one in the list and follow the general sender and receiver usage below 38 | 39 | # General Sender and Receiver Usage 40 | 1. Make sure you have connected your audio cable from the headphone out into your PC and your recording software open, for all my recording I used Audacity set to 44100Hz 16bit Stereo, though both 48000Hz and 96000Hz are also supported by the receiver 41 | 2. Test record and start the dumping you ROM/Save on the GameBoy Color, now you can make sure your volume levels are set so the audio does not clip, which basically means it should not hit the absolute maximum volume, we will normalize the volume later anyways 42 | 3. If the volume levels look good, just restart the GameBoy Color, get into the dumper, put in the cartridge you want to dump and then start recording a bit of silence so the volume level settles, then start dumping on the GameBoy Color 43 | 4. Wait for it to finish dumping, after that you can stop the recording 44 | 5. Cut off the bit of silence before and after the dumped data and then normalize the audio, in Audacity that is under Effect -> Normalize, I personally have remove dc offset unchecked and normalize maximum amplitude set to 0.0dB and normalize stereo channels independently checked 45 | 6. Export the now cut and normalized recording as a standard .wav file into a new folder together with receiver-gbc.exe from the receiver folder found in the current release zip you downloaded earlier 46 | 7. Drag and drop your .wav file into receiver-gbc.exe and wait for it to convert the .wav into a .gbc or .sav file! 47 | 48 | Some older GameBoy Cartridges may reset your GameBoy Color when you insert them, on those you have to tape off pin 30, the RESET pin of the cartridge: 49 | http://www.rickard.gunee.com/projects/playmobile/html/3/Image9.gif 50 | Also sometimes it can just randomly reset when taking out a cart or inserting one so before taping off that port you can just try again. 51 | Please let me know if you run into any issues, I only was able to test all this on the german version of pokemon yellow. 52 | 53 | # Technical Details 54 | To compile the sender, you need rgbasm 0.3.3 and rgbbin from here: 55 | https://github.com/zzazzdzz/rgbbin 56 | Also for the pokemon installer/updater, make sure to check out the installer submodule in git as well. Its actual repository is located here: 57 | https://github.com/FIX94/pkm-yellow-codeinstaller 58 | The resulting bin then goes into the game RAM at 0xDA7F in the english and 0xDA84 in the german version of pokemon yellow, executed with glitch item ws m. 59 | The standalone .gb rom will put it into RAM address 0xC000 instead when the rom is booted up. 60 | The sender right now hits about 2.27KB/s so even the biggest official cartridge size of 8MB takes an hour to dump, generally most carts you will run into will be much smaller and thus send much faster. 61 | 62 | The installer is a set of inputs I created in an emulator that go from the beginning of the game up to a point where it can install that sender. The updater makes use of the installer save on cart to re-use all the code left in place to install new code much quicker. See the .bat files in the installer folder for how those put the sender into those inputs. Also in the "code" folder you will find the little C program that does the actual writing of those inputs, you can compile that with gcc. 63 | The installer is putting up to 1122 bytes of code into the memory region of the current PC Box. 64 | Of course you could use the installer/updater to really execute anything you want on a gameboy, you can write up whatever code instead of the sender if you want to do something else with those 1122 bytes. 65 | For example I did also make this small side project using this same installer that gives you a sound test with a visualizer: 66 | https://github.com/FIX94/pkm-sound-visualizer 67 | To get from the .bk2 it creates to a .txt that gameboy interface can then use, you can just play back the .bk2 in BizHawk2 while you have this lua script from TiKevin83 loaded in the lua console: 68 | https://pastebin.com/NbTRNePD 69 | 70 | The receiver is a simple C program you can easily compile with gcc. 71 | It basically goes through the .wav file sample by sample, tries to find "silence", then a "peak" and then "silence" again, with that information it evaluates which volume level it got and interprets that as part of the byte it ends up writing into the .gbc or .sav file it creates. 72 | 73 | This project can theoretically be extended to run from many other generation 1 pokemon games and probably also generation 2, at this point I did not look into anything else though. 74 | It may even work on a gameboy advance if you have a way to constantly push down the small switch on the side of the case that normally switches it back into gameboy advance mode when removing the gameboy color cartridge, though at this point I have not tested that. 75 | What I did test however is removing the power switch on an original gameboy, and on that one sadly it appears to reset every time you remove the pokemon cartridge. 76 | -------------------------------------------------------------------------------- /sender/sender.asm: -------------------------------------------------------------------------------- 1 | ; Copyright (C) 2018 FIX94 2 | ; 3 | ; This software may be modified and distributed under the terms 4 | ; of the MIT license. See the LICENSE file for details. 5 | 6 | include "charmap.asm" 7 | 8 | SECTION "WRAM",ROM0[RAMOFFSET] 9 | 10 | init: 11 | di 12 | ; for safety 13 | xor a 14 | ld [$0000], a 15 | ; reset audio regs 16 | ldh [$FF26], a 17 | ld a, $80 18 | ldh [$FF26], a 19 | ; set wav to low 20 | ld h, $FF 21 | ld l, $30 22 | xor a 23 | clearsamples: 24 | ld [hl+], a 25 | bit 6, l 26 | jr z, clearsamples 27 | ; wav 100% volume 28 | ld a, $20 29 | ldh [$FF1C], a 30 | ; freq of 7F0 (probably not needed right 31 | ; now because its only one solid output) 32 | ;ld a, $F0 33 | ;ldh [$FF1D], a 34 | ; enable wav dac and trigger 35 | ld a, $87 36 | ldh [$FF1A], a 37 | ldh [$FF1E], a 38 | ; audio ready, update video 39 | call waitVBlank 40 | ; turn off lcd 41 | xor a 42 | ldh [$FF40], a 43 | ; clear various regs 44 | ldh [$FF02], a 45 | ldh [$FF07], a 46 | ldh [$FF41], a 47 | ; disable scroll 48 | ldh [$FF42], a 49 | ldh [$FF43], a 50 | ; move lyc and wy out 51 | ld a, $9B 52 | ldh [$FF45], a 53 | ldh [$FF4A], a 54 | call clearScreen 55 | call drawHeader 56 | ; clear cpu interrupts 57 | xor a 58 | ldh [$FFFF], a 59 | ldh [$FF0F], a 60 | ; turn on lcd 61 | ld a, $81 62 | ldh [$FF40], a 63 | ; time to wait for cart 64 | promptinsertcart: 65 | ld hl, startmsg 66 | call drawStatus 67 | call waitStart 68 | ; for safety 69 | xor a 70 | ld [$0000], a 71 | ; verify various static 72 | ; bytes of the header 73 | ; to ensure cart is good 74 | ld hl, $107 75 | ld a, [hl+] 76 | cp $66 77 | jr nz, checkerr 78 | ld a, [hl+] 79 | cp $CC 80 | jr nz, checkerr 81 | inc l 82 | ld a, [hl] 83 | cp 0 84 | jr nz, checkerr 85 | ld l, $16 86 | ld a, [hl] 87 | cp $11 88 | jr nz, checkerr 89 | jr checkok 90 | checkerr: 91 | ; errors in static bytes, 92 | ; prompt to try again 93 | ld hl, errmsg 94 | call drawStatus 95 | call waitA 96 | jr promptinsertcart 97 | checkok: 98 | ; cart looks good, prompt 99 | ; to start sending 100 | ld hl, okmsg 101 | call drawStatus 102 | waitokprompt: 103 | call readButtons 104 | bit 0, b 105 | jr z, startSendROM 106 | bit 1, b 107 | jr nz, waitokprompt 108 | ; B pressed, send save 109 | call sendSave 110 | jr end 111 | startSendROM: 112 | ; A pressed, send ROM 113 | call sendROM 114 | end: 115 | ; done dumping, wait for 116 | ; another cart or turning off 117 | ld hl, donemsg 118 | call drawStatus 119 | call waitA 120 | ; back to the beginning 121 | jp promptinsertcart 122 | 123 | readButtons: 124 | ; enable button bits 125 | ld a, $10 126 | ldh [$FF00], a 127 | ldh a, [$FF00] 128 | ldh a, [$FF00] 129 | ldh a, [$FF00] 130 | ldh a, [$FF00] 131 | ldh a, [$FF00] 132 | ldh a, [$FF00] 133 | ld b, a 134 | ; disable button bits 135 | ld a, $30 136 | ldh [$FF00], a 137 | ret 138 | 139 | waitA: 140 | call readButtons 141 | bit 0, b 142 | jr nz, waitA 143 | ret 144 | 145 | waitStart: 146 | call readButtons 147 | bit 3, b 148 | jr nz, waitStart 149 | ret 150 | 151 | sendROM: 152 | ; ROM mode for mbc1 153 | xor a 154 | ld [$6000], a 155 | ; init bank total 156 | ; read ROM size 157 | ld a, [$148] 158 | ld b, a 159 | ; check high nibble 160 | and $F0 161 | cp $50 162 | jr z, addh 163 | ; size value not 5X 164 | ld a, b 165 | jr getbanktotal 166 | addh: 167 | ; add 7 to low nibble to 168 | ; get to special bank lut 169 | ld a, b 170 | and 7 171 | add 7 172 | getbanktotal: 173 | ld hl, banklut 174 | and $F 175 | sla a 176 | ld c, a 177 | ld b, 0 178 | add hl, bc 179 | ld a, [hl+] 180 | ld [banktotal], a 181 | ld a, [hl] 182 | ld [banktotal+1], a 183 | ; set sending status 184 | ld hl, sendmsg 185 | call drawStatus 186 | ; done initializing bank total 187 | ld de, vollut 188 | call testsend 189 | ; send "ROM" file status 190 | ld a, $A 191 | call sendnibble 192 | ; send fixed bank 0 193 | ld c, $3F 194 | ld de, vollut 195 | ld hl, $0000 196 | call sendbank 197 | ; init swappable bank 198 | ld de, $0000 199 | sendROMloop: 200 | ; check total bank low 201 | inc d 202 | ld a, [banktotal] 203 | cp d 204 | jr nz, setbanklow 205 | ; check if total high exists 206 | ld a, [banktotal+1] 207 | or a 208 | jr z, sendROMend 209 | ; check total bank high 210 | inc e 211 | cp e 212 | jr z, sendROMend 213 | ; set bank high 214 | ld h, $30 215 | ld [hl], e 216 | setbanklow: 217 | ; 21 to cover mbc2 218 | ld h, $21 219 | ld [hl], d 220 | ; 40 to cover mbc1 221 | ld h, $40 222 | ld a, d 223 | swap a 224 | srl a 225 | and 3 226 | ld [hl], a 227 | ; save current bank 228 | push de 229 | ; should have set everything 230 | ; read out next 16k bank 231 | ld c, $3F 232 | ld de, vollut 233 | ; hl is $4000 234 | call sendbank 235 | ; restore current bank 236 | pop de 237 | jr sendROMloop 238 | sendROMend: 239 | ret 240 | 241 | sendSave: 242 | ; RAM mode for mbc1 243 | ld a, $1 244 | ld [$6000], a 245 | ; set sending status 246 | ld hl, sendmsg 247 | call drawStatus 248 | ; init bank total 249 | ; "default" 8k values 250 | ld b, $1 ; banks 251 | ld c, $1F ; and val 252 | ; read Cart Type 253 | ld a, [$147] 254 | cp $6 255 | jr nz, checkRAMsize 256 | ; 512b 257 | ld c, $1 ; and val 258 | jr writeSaveBanks 259 | checkRAMsize: 260 | ld a, [$149] 261 | cp $1 262 | jr nz, check32k 263 | ; 2kb 264 | ld c, $7 ; and val 265 | jr writeSaveBanks 266 | check32k: 267 | cp $3 268 | jr nz, check64k 269 | ; 32kb 270 | ld b, $4 ; banks 271 | jr writeSaveBanks 272 | check64k: 273 | cp $5 274 | jr nz, check128k 275 | ; 64kb 276 | ld b, $8 ; banks 277 | jr writeSaveBanks 278 | check128k: 279 | cp $4 280 | jr nz, writeSaveBanks 281 | ; 128k 282 | ld b, $10 ; banks 283 | writeSaveBanks: 284 | ; save RAM Banks 285 | ld a, b 286 | ld [banktotal], a 287 | ; save and val 288 | ld a, c 289 | ld [banktotal+1], a 290 | ; set sending status 291 | ld hl, sendmsg 292 | call drawStatus 293 | ; done initializing bank total 294 | ld de, vollut 295 | call testsend 296 | ; send "save" file status 297 | ld a, 5 298 | call sendnibble 299 | ; enable RAM 300 | ld a, $A 301 | ld [$0000], a 302 | ; init RAM bank 303 | ld d, 0 304 | ; load and val 305 | ld a, [banktotal+1] 306 | ld e, a 307 | sendSaveLoop: 308 | ; set RAM bank 309 | ld a, d 310 | ld [$4000], a 311 | ; save current bank 312 | push de 313 | ; send RAM bank 314 | ld c, e 315 | ld de, vollut 316 | ld hl, $A000 317 | call sendbank 318 | ; restore current bank 319 | pop de 320 | ; check next bank 321 | inc d 322 | ld a, [banktotal] 323 | cp d 324 | jr nz, sendSaveLoop 325 | ; done sending save 326 | ; disable RAM 327 | xor a 328 | ld [$0000], a 329 | ret 330 | 331 | testsend: 332 | ; send calibration values 333 | ld a, $F 334 | call sendnibble 335 | ld a, $A 336 | call sendnibble 337 | ld a, 5 338 | call sendnibble 339 | ld a, 0 340 | call sendnibble 341 | ret 342 | 343 | sendbank: 344 | ld a, [hl+] 345 | ld b, a 346 | call sendnibble 347 | ld a, b 348 | swap a 349 | call sendnibble 350 | ; check if l is 0 351 | inc l 352 | dec l 353 | jr nz, sendbank 354 | ; check if bank is processed 355 | ld a, h 356 | and c 357 | jr nz, sendbank 358 | ret 359 | 360 | sendnibble: 361 | push hl 362 | ;enable wave with 363 | ;master volume of lut 364 | and $F 365 | ld l, a 366 | ld h, 0 367 | add hl, de 368 | ;set master volume 369 | ld a, [hl] 370 | ldh [$FF24], a 371 | ;allow wav volume 372 | ld a, $44 373 | ldh [$FF25], a 374 | ;delay for audio recording 375 | ld a, $14 376 | call delayloop 377 | ;disallow wav volume 378 | xor a 379 | ldh [$FF25], a 380 | ;delay for audio recording 381 | ld a, $14 382 | call delayloop 383 | pop hl 384 | ret 385 | 386 | delayloop: 387 | dec a 388 | jr nz, delayloop 389 | ret 390 | 391 | vollut: 392 | DB $11,$13,$15,$17,$31,$33,$35,$37,$51,$53,$55,$57,$71,$73,$75,$77 393 | 394 | banklut: 395 | DW $0002,$0004,$0008,$0010,$0020,$0040,$0080,$0100,$0200,$0048,$0050,$0060,$0002,$0002,$0002,$0002 396 | 397 | banktotal: 398 | DW $0000 399 | 400 | ; video stuff 401 | waitVBlank: 402 | ldh a, [$FF44] 403 | cp 145 404 | jr nz, waitVBlank 405 | ret 406 | 407 | clearScreen: 408 | ld hl, $9800 409 | ld a, $7F 410 | clrloop: 411 | ld [hl+], a 412 | bit 2, h 413 | jr z, clrloop 414 | ret 415 | 416 | drawLine: 417 | ld a, [hl+] 418 | cp 0 419 | ret z 420 | ld [bc], a 421 | inc c 422 | jr drawLine 423 | 424 | drawHeader: 425 | ld bc, $9821 426 | ld hl, hdrmsg 427 | call drawLine 428 | ld c, $41 429 | call drawLine 430 | ret 431 | 432 | drawStatus: 433 | call waitVBlank 434 | ld bc, $9901 435 | call drawLine 436 | ld c, $21 437 | call drawLine 438 | ld c, $41 439 | call drawLine 440 | ret 441 | 442 | hdrmsg: 443 | db "GB Audio Dumper", $00 444 | db "v0.3 by FIX94", $00 445 | 446 | startmsg: 447 | db "Insert Cartridge,", $00 448 | db "then press START ", $00 449 | db "to check the Cart", $00 450 | 451 | okmsg: 452 | db "Cart OK! Press ", $00 453 | db "A to send ROM or ", $00 454 | db "B to send Save ", $00 455 | 456 | errmsg: 457 | db "Cart dirty? Press", $00 458 | db "A to return to ", $00 459 | db "the first Screen ", $00 460 | 461 | sendmsg: 462 | db "Sending, this ", $00 463 | db "process will take", $00 464 | db "a while... ", $00 465 | 466 | donemsg: 467 | db "Done! Press A ", $00 468 | db "to return to the ", $00 469 | db "first Screen ", $00 470 | -------------------------------------------------------------------------------- /receiver/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 FIX94 3 | * 4 | * This software may be modified and distributed under the terms 5 | * of the MIT license. See the LICENSE file for details. 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "crc32.h" 13 | #include "polarssl/md5.h" 14 | #include "polarssl/sha1.h" 15 | 16 | //0x1800 is harcoded minimum spike in volume to count as peak 17 | #define PEAK_MIN 0x1800 18 | //0x1000 is harcoded limit for difference in "silence" levels 19 | #define SDIFF_MAX 0x1000 20 | //set this to 1 if dump was made in gameboy interface 21 | #define GBPLAYER_GBI 0 22 | 23 | static void curpeak(int32_t *pl, int32_t *pr, int16_t *smpl, size_t *smplpos)//, int32_t i) 24 | { 25 | *pl = 0, *pr = 0; 26 | int16_t *smplpos0 = smpl+(smplpos[0]<<1); 27 | int16_t *smplpos1 = smpl+(smplpos[1]<<1); 28 | int16_t *smplpos2 = smpl+(smplpos[2]<<1); 29 | //"silence" 30 | int32_t sl1 = (smplpos0[0]+smplpos0[2]+smplpos0[4]) / 3; 31 | int32_t sr1 = (smplpos0[1]+smplpos0[3]+smplpos0[5]) / 3; 32 | //volume spike 33 | #if GBPLAYER_GBI 34 | //seems to only have 1 distinct spike 35 | int32_t peakl = smplpos1[0]; 36 | int32_t peakr = smplpos1[1]; 37 | #else 38 | //average out top 2 spikes 39 | int32_t peakl = (smplpos1[0]+smplpos1[2]) / 2; 40 | int32_t peakr = (smplpos1[1]+smplpos1[3]) / 2; 41 | #endif 42 | //"silence" 43 | int32_t sl2 = (smplpos2[0]+smplpos2[2]+smplpos2[4]) / 3; 44 | int32_t sr2 = (smplpos2[1]+smplpos2[3]+smplpos2[5]) / 3; 45 | int32_t sdiffl = abs(sl1-sl2); 46 | int32_t savgl = (sl1+sl2) / 2; 47 | int32_t sdiffr = abs(sr1-sr2); 48 | int32_t savgr = (sr1+sr2) / 2; 49 | #if GBPLAYER_GBI 50 | //the wave channel seems to be inverted 51 | peakl*=-1; peakr*=-1; 52 | savgl*=-1; savgr*=-1; 53 | #endif 54 | //printf("%i %i %i %i %i %i %i %i %i %i %i\n", i, sl1, sr1, peakl, peakr, sl2, sr2, sdiffl, savgl, sdiffr, savgr); 55 | if(sdiffl < SDIFF_MAX && peakl > savgl && (peakl-savgl) > PEAK_MIN && 56 | sdiffr < SDIFF_MAX && peakr > savgr && (peakr-savgr) > PEAK_MIN) 57 | { 58 | *pl = peakl-savgl; 59 | *pr = peakr-savgr; 60 | } 61 | } 62 | 63 | static unsigned int crc32tmp = 0; 64 | static md5_context md5ctx; 65 | static sha1_context sha1ctx; 66 | 67 | static void emptywbuf(FILE *f, uint8_t *buf, size_t size) 68 | { 69 | if(size) 70 | { 71 | crc32tmp = crc32buffer(buf, size, crc32tmp); 72 | md5_update(&md5ctx, buf, size); 73 | sha1_update(&sha1ctx, buf, size); 74 | fwrite(buf, 1, size, f); 75 | } 76 | } 77 | 78 | static void updatedev(int32_t peak, int32_t *dev, size_t pos, size_t *devpos) 79 | { 80 | if(peak < *dev) 81 | { 82 | *dev = peak; 83 | *devpos = pos; 84 | } 85 | } 86 | 87 | size_t smplpositions[3][3] = { { 0, 5, 9 }, { 0, 5, 10 }, { 0, 11, 21 } }; 88 | 89 | int main(int argc, char *argv[]) 90 | { 91 | //file we read from/write to 92 | FILE *fr = NULL, *fw = NULL; 93 | size_t frsize = 0, fwsize = 0; 94 | //wav file header 95 | uint8_t hdr[0x2C]; 96 | //read/write buffers 97 | uint8_t *rbuf = NULL; 98 | size_t rremain = 0; 99 | uint8_t *wbuf = NULL; 100 | size_t datsize = 0; 101 | int16_t *smpl = NULL; 102 | size_t smplsize = 0; 103 | size_t smpltotal = 0, prevsmpltotal = 0, smplpos = 0; 104 | size_t i = 0; 105 | //amount of volume states measured 106 | uint8_t statesdone = 0; 107 | //measured volume states and deviations of them 108 | int32_t statesltop[4] = {0,0,0,0}, statesrtop[4] = {0,0,0,0}; 109 | int32_t stateslbottom[4] = {0,0,0,0}, statesrbottom[4] = {0,0,0,0}; 110 | int32_t devl[4] = {0,0,0,0}, devr[4] = {0,0,0,0}; 111 | size_t devlpos[4] = {0,0,0,0}, devrpos[4] = {0,0,0,0}; 112 | //currently processed peaks 113 | int32_t peakl = 0, peakr = 0; 114 | int32_t peakltmp = 0, peakrtmp = 0; 115 | //currently processed byte 116 | uint8_t bstate = 0; 117 | uint8_t byte = 0; 118 | //bytes written total 119 | size_t wpos = 0; 120 | //hashing 121 | uint32_t crc32 = 0; 122 | uint8_t md5[16]; 123 | uint8_t sha1[20]; 124 | //finally go 125 | printf("GameBoy Audio Dumper v0.3 by FIX94\n"); 126 | if(argc < 2 || strlen(argv[1]) < 5 || memcmp(argv[1]+strlen(argv[1])-4,".wav",4) != 0) 127 | { 128 | printf("Please provide a .wav file to process!\n"); 129 | goto end_prog; 130 | } 131 | fr = fopen(argv[1],"rb"); 132 | if(!fr) 133 | { 134 | printf("ERROR: Unable to open %s!\n", argv[1]); 135 | goto end_prog; 136 | } 137 | fseek(fr,0,SEEK_END); 138 | frsize = ftell(fr); 139 | if(frsize < 0x30) 140 | { 141 | printf("Not enough data in .wav to process!\n"); 142 | goto end_prog; 143 | } 144 | rewind(fr); 145 | fread(hdr,1,0x2C,fr); 146 | if(memcmp("RIFF",hdr,4) != 0 || memcmp("WAVE",hdr+8,4) != 0 || memcmp("fmt",hdr+12,3) != 0 || *(uint32_t*)(hdr+16) != 0x10 147 | || memcmp("data",hdr+36,4) != 0) 148 | { 149 | printf("Invalid .wav header!\n"); 150 | goto end_prog; 151 | } 152 | if(*(uint16_t*)(hdr+20) != 1 || *(uint16_t*)(hdr+22) != 2 153 | || *(uint16_t*)(hdr+32) != 4 || *(uint16_t*)(hdr+34) != 16) 154 | { 155 | printf(".wav has to be 16bit Stereo PCM!\n"); 156 | goto end_prog; 157 | } 158 | size_t *csmplpos; size_t smplseek, smpladd, smpllimit, smplend; 159 | if(*(uint32_t*)(hdr+24) == 44100 && *(uint32_t*)(hdr+28) == 176400) 160 | { 161 | csmplpos = smplpositions[0]; 162 | smplseek = 6; 163 | smpladd = 18; 164 | smpllimit = 26; 165 | smplend = 30; 166 | } 167 | else if(*(uint32_t*)(hdr+24) == 48000 && *(uint32_t*)(hdr+28) == 192000) 168 | { 169 | csmplpos = smplpositions[1]; 170 | smplseek = 8; 171 | smpladd = 20; 172 | smpllimit = 28; 173 | smplend = 32; 174 | } 175 | else if(*(uint32_t*)(hdr+24) == 96000 && *(uint32_t*)(hdr+28) == 384000) 176 | { 177 | csmplpos = smplpositions[2]; 178 | smplseek = 16; 179 | smpladd = 40; 180 | smpllimit = 56; 181 | smplend = 64; 182 | } 183 | else 184 | { 185 | printf(".wav has to be 44100/48000/96000Hz!\n"); 186 | goto end_prog; 187 | } 188 | datsize = *(uint32_t*)(hdr+40); 189 | if(datsize > (frsize-0x2C)) 190 | { 191 | printf("Data size bigger than .wav!\n"); 192 | goto end_prog; 193 | } 194 | fwsize = 0; 195 | rbuf = malloc(16*1024*1024); 196 | wbuf = malloc(1024*1024); 197 | if(!rbuf || !wbuf) 198 | { 199 | printf("ERROR: Unable to allocate read/write buffers!\n"); 200 | goto end_prog; 201 | } 202 | rremain = 0; 203 | smpl = (int16_t*)rbuf; 204 | smplsize = datsize/2; 205 | printf("Processing %i samples\n", smplsize); 206 | //initialize hash variables 207 | crc32tmp = 0xFFFFFFFF; 208 | md5_starts(&md5ctx); 209 | sha1_starts(&sha1ctx); 210 | //go through read in samples 211 | while(smpltotal+smplend < smplsize) 212 | { 213 | //refill read buffer if needed 214 | if(rremain < (smplend<<1)) 215 | { 216 | fseek(fr,(smpltotal<<1)+0x2C,SEEK_SET); 217 | rremain = fread(rbuf,1,16*1024*1024,fr); 218 | //not enough remaining of the file for another peak 219 | if(rremain < (smplend<<1)) 220 | { 221 | printf("Ending sample reading early at sample %i\n", i>>1); 222 | break; 223 | } 224 | printf("%i%%\n", (size_t)((((float)smpltotal)*100.f)/((float)smplsize))); 225 | smplpos = 0; 226 | } 227 | //try see if theres a peak in between 228 | curpeak(&peakl, &peakr, smpl+smplpos, csmplpos);//, i>>1); 229 | //if there was one, check samples around it 230 | //to find the middle of it for accurate capture 231 | //size_t addl = 0, addr = 0; 232 | if(peakl && peakr) 233 | { 234 | for(i = 2; i < smplseek; i+=2) 235 | { 236 | curpeak(&peakltmp, &peakrtmp, smpl+smplpos+i, csmplpos);//, (i+j)>>1); 237 | //if(i>>1 == 36154) printf("%i %i\n", peakltmp, peakrtmp); 238 | if(peakltmp > peakl) 239 | { 240 | peakl = peakltmp; 241 | //addl = j; 242 | } 243 | if(peakrtmp > peakr) 244 | { 245 | peakr = peakrtmp; 246 | //addr = j; 247 | } 248 | } 249 | } 250 | //process peak in the middle 251 | if(peakl && peakr) 252 | { 253 | //printf("%i, %i %i %i %i %i\n", wpos, i>>1, addl, addr, peakl, peakr); 254 | //init the 4 input volumes 255 | if(statesdone < 4) 256 | { 257 | devl[statesdone] = statesltop[statesdone] = peakl; 258 | devr[statesdone] = statesrtop[statesdone] = peakr; 259 | statesdone++; 260 | if(statesdone == 4) 261 | { 262 | printf("States Top Left: %i %i %i %i\n", statesltop[0], statesltop[1], statesltop[2], statesltop[3]); 263 | printf("States Top Right: %i %i %i %i\n", statesrtop[0], statesrtop[1], statesrtop[2], statesrtop[3]); 264 | if(statesltop[0] <= statesltop[1] || statesrtop[0] <= statesrtop[1] || 265 | statesltop[1] <= statesltop[2] || statesrtop[1] <= statesrtop[2] || 266 | statesltop[2] <= statesltop[3] || statesrtop[2] <= statesrtop[3]) 267 | { 268 | printf("ERROR: States not read in as expected! Possible volume problem?\n"); 269 | goto end_prog; 270 | } 271 | stateslbottom[0] = statesltop[0]-((statesltop[0]-statesltop[1])/2); 272 | stateslbottom[1] = statesltop[1]-((statesltop[1]-statesltop[2])/2); 273 | stateslbottom[2] = statesltop[2]-((statesltop[2]-statesltop[3])/2); 274 | 275 | statesrbottom[0] = statesrtop[0]-((statesrtop[0]-statesrtop[1])/2); 276 | statesrbottom[1] = statesrtop[1]-((statesrtop[1]-statesrtop[2])/2); 277 | statesrbottom[2] = statesrtop[2]-((statesrtop[2]-statesrtop[3])/2); 278 | 279 | printf("States Bottom Left: %i %i %i\n", stateslbottom[0], stateslbottom[1], stateslbottom[2]); 280 | printf("States Bottom Right: %i %i %i\n", statesrbottom[0], statesrbottom[1], statesrbottom[2]); 281 | } 282 | } 283 | else //process nibble 284 | { 285 | if(bstate == 1) 286 | byte >>= 4; 287 | if(peakl > stateslbottom[0]) 288 | { 289 | byte |= 0xC0; 290 | updatedev(peakl, devl+0, wpos, devlpos+0); 291 | } 292 | else if(peakl > stateslbottom[1]) 293 | { 294 | byte |= 0x80; 295 | updatedev(peakl, devl+1, wpos, devlpos+1); 296 | } 297 | else if(peakl > stateslbottom[2]) 298 | { 299 | byte |= 0x40; 300 | updatedev(peakl, devl+2, wpos, devlpos+2); 301 | } 302 | if(peakr > statesrbottom[0]) 303 | { 304 | byte |= 0x30; 305 | updatedev(peakr, devr+0, wpos, devrpos+0); 306 | } 307 | else if(peakr > statesrbottom[1]) 308 | { 309 | byte |= 0x20; 310 | updatedev(peakr, devr+1, wpos, devrpos+1); 311 | } 312 | else if(peakr > statesrbottom[2]) 313 | { 314 | byte |= 0x10; 315 | updatedev(peakr, devr+2, wpos, devrpos+2); 316 | } 317 | //got 2 nibbles, write byte 318 | if(bstate == 1) 319 | { 320 | wbuf[fwsize++] = byte; 321 | byte = 0; 322 | //empty buf to file 323 | if(fwsize == 1024*1024) 324 | { 325 | emptywbuf(fw, wbuf, fwsize); 326 | fwsize = 0; 327 | } 328 | wpos++; 329 | } 330 | bstate^=1; 331 | //first peak after 332 | if(statesdone == 4) 333 | { 334 | statesdone++; 335 | if(byte == 0x50) 336 | memcpy(argv[1]+strlen(argv[1])-4,".sav",4); 337 | else if(byte == 0xA0) 338 | memcpy(argv[1]+strlen(argv[1])-4,".gbc",4); 339 | else 340 | { 341 | printf("ERROR: not ROM or save file!\n"); 342 | goto end_prog; 343 | } 344 | fw = fopen(argv[1],"wb"); 345 | if(!fw) 346 | { 347 | printf("ERROR: unable to write %s!\n", argv[1]); 348 | goto end_prog; 349 | } 350 | //reset 351 | byte = 0; 352 | bstate = 0; 353 | } 354 | } 355 | if(statesdone > 1 && smpltotal-prevsmpltotal > smpllimit) 356 | printf("WARNING: Possible desync at byte %i sample %i with a value of %i\n", wpos, smpltotal>>1, smpltotal-prevsmpltotal); 357 | prevsmpltotal = smpltotal; 358 | //add 9 samples before next check 359 | smpltotal+=smpladd; 360 | smplpos+=smpladd; 361 | rremain-=(smpladd<<1); 362 | } 363 | else //no peak, add sample 364 | { 365 | smpltotal+=2; 366 | smplpos+=2; 367 | rremain-=4; 368 | } 369 | } 370 | //remainder remainder if any 371 | if(fwsize) 372 | { 373 | emptywbuf(fw, wbuf, fwsize); 374 | fwsize = 0; 375 | } 376 | if(wpos == 0) 377 | { 378 | printf("Unable to find any data in the provided .wav file!\n"); 379 | printf("Make sure the file was properly recorded and normalized before\n"); 380 | printf("Throwing it into this application!\n"); 381 | } 382 | else 383 | { 384 | //print stats 385 | printf("Done! Wrote %i bytes\n", wpos); 386 | printf(" \nStatistics:\n"); 387 | printf("Biggest Deviations from States Top to Bottom 0, 1 and 2 Left:\n"); 388 | printf("Byte %i with %i%%, Byte %i with %i%%, Byte %i with %i%%\n", devlpos[0], 100-((devl[0]-stateslbottom[0])*100/(statesltop[0]-stateslbottom[0])), 389 | devlpos[1], 100-((devl[1]-stateslbottom[1])*100/(statesltop[1]-stateslbottom[1])), 390 | devlpos[2], 100-((devl[2]-stateslbottom[2])*100/(statesltop[2]-stateslbottom[2]))); 391 | printf("Biggest Deviations from States Top to Bottom 0, 1 and 2 Right:\n"); 392 | printf("Byte %i with %i%%, Byte %i with %i%%, Byte %i with %i%%\n", devrpos[0], 100-((devr[0]-statesrbottom[0])*100/(statesrtop[0]-statesrbottom[0])), 393 | devrpos[1], 100-((devr[1]-statesrbottom[1])*100/(statesrtop[1]-statesrbottom[1])), 394 | devrpos[2], 100-((devr[2]-statesrbottom[2])*100/(statesrtop[2]-statesrbottom[2]))); 395 | //print file hash 396 | printf(" \nHashes:\n"); 397 | crc32 = ~crc32tmp; 398 | printf("CRC32: %08X\n", crc32); 399 | md5_finish(&md5ctx, md5); 400 | printf("MD5: "); 401 | for(i = 0; i < 16; i++) 402 | printf("%02X", md5[i]); 403 | printf("\n"); 404 | sha1_finish(&sha1ctx, sha1); 405 | printf("SHA1: "); 406 | for(i = 0; i < 20; i++) 407 | printf("%02X", sha1[i]); 408 | printf("\n"); 409 | } 410 | end_prog: 411 | if(fr) fclose(fr); 412 | fr = NULL; 413 | if(fw) fclose(fw); 414 | fw = NULL; 415 | if(rbuf) free(rbuf); 416 | rbuf = NULL; 417 | if(wbuf) free(wbuf); 418 | wbuf = NULL; 419 | puts("Press enter to exit"); 420 | getc(stdin); 421 | return 0; 422 | } -------------------------------------------------------------------------------- /receiver/polarssl/md5.c: -------------------------------------------------------------------------------- 1 | /* 2 | * RFC 1321 compliant MD5 implementation 3 | * 4 | * Copyright (C) 2006-2013, Brainspark B.V. 5 | * 6 | * This file is part of PolarSSL (http://www.polarssl.org) 7 | * Lead Maintainer: Paul Bakker 8 | * 9 | * All rights reserved. 10 | * 11 | * This program is free software; you can redistribute it and/or modify 12 | * it under the terms of the GNU General Public License as published by 13 | * the Free Software Foundation; either version 2 of the License, or 14 | * (at your option) any later version. 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU General Public License along 22 | * with this program; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 24 | */ 25 | /* 26 | * The MD5 algorithm was designed by Ron Rivest in 1991. 27 | * 28 | * http://www.ietf.org/rfc/rfc1321.txt 29 | */ 30 | 31 | #include "polarssl/config.h" 32 | 33 | #if defined(POLARSSL_MD5_C) 34 | 35 | #include "polarssl/md5.h" 36 | 37 | #if defined(POLARSSL_FS_IO) || defined(POLARSSL_SELF_TEST) 38 | #include 39 | #endif 40 | 41 | #if !defined(POLARSSL_MD5_ALT) 42 | 43 | /* 44 | * 32-bit integer manipulation macros (little endian) 45 | */ 46 | #ifndef GET_UINT32_LE 47 | #define GET_UINT32_LE(n,b,i) \ 48 | { \ 49 | (n) = ( (uint32_t) (b)[(i) ] ) \ 50 | | ( (uint32_t) (b)[(i) + 1] << 8 ) \ 51 | | ( (uint32_t) (b)[(i) + 2] << 16 ) \ 52 | | ( (uint32_t) (b)[(i) + 3] << 24 ); \ 53 | } 54 | #endif 55 | 56 | #ifndef PUT_UINT32_LE 57 | #define PUT_UINT32_LE(n,b,i) \ 58 | { \ 59 | (b)[(i) ] = (unsigned char) ( (n) ); \ 60 | (b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \ 61 | (b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \ 62 | (b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \ 63 | } 64 | #endif 65 | 66 | /* 67 | * MD5 context setup 68 | */ 69 | void md5_starts( md5_context *ctx ) 70 | { 71 | ctx->total[0] = 0; 72 | ctx->total[1] = 0; 73 | 74 | ctx->state[0] = 0x67452301; 75 | ctx->state[1] = 0xEFCDAB89; 76 | ctx->state[2] = 0x98BADCFE; 77 | ctx->state[3] = 0x10325476; 78 | } 79 | 80 | void md5_process( md5_context *ctx, const unsigned char data[64] ) 81 | { 82 | uint32_t X[16], A, B, C, D; 83 | 84 | GET_UINT32_LE( X[ 0], data, 0 ); 85 | GET_UINT32_LE( X[ 1], data, 4 ); 86 | GET_UINT32_LE( X[ 2], data, 8 ); 87 | GET_UINT32_LE( X[ 3], data, 12 ); 88 | GET_UINT32_LE( X[ 4], data, 16 ); 89 | GET_UINT32_LE( X[ 5], data, 20 ); 90 | GET_UINT32_LE( X[ 6], data, 24 ); 91 | GET_UINT32_LE( X[ 7], data, 28 ); 92 | GET_UINT32_LE( X[ 8], data, 32 ); 93 | GET_UINT32_LE( X[ 9], data, 36 ); 94 | GET_UINT32_LE( X[10], data, 40 ); 95 | GET_UINT32_LE( X[11], data, 44 ); 96 | GET_UINT32_LE( X[12], data, 48 ); 97 | GET_UINT32_LE( X[13], data, 52 ); 98 | GET_UINT32_LE( X[14], data, 56 ); 99 | GET_UINT32_LE( X[15], data, 60 ); 100 | 101 | #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) 102 | 103 | #define P(a,b,c,d,k,s,t) \ 104 | { \ 105 | a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \ 106 | } 107 | 108 | A = ctx->state[0]; 109 | B = ctx->state[1]; 110 | C = ctx->state[2]; 111 | D = ctx->state[3]; 112 | 113 | #define F(x,y,z) (z ^ (x & (y ^ z))) 114 | 115 | P( A, B, C, D, 0, 7, 0xD76AA478 ); 116 | P( D, A, B, C, 1, 12, 0xE8C7B756 ); 117 | P( C, D, A, B, 2, 17, 0x242070DB ); 118 | P( B, C, D, A, 3, 22, 0xC1BDCEEE ); 119 | P( A, B, C, D, 4, 7, 0xF57C0FAF ); 120 | P( D, A, B, C, 5, 12, 0x4787C62A ); 121 | P( C, D, A, B, 6, 17, 0xA8304613 ); 122 | P( B, C, D, A, 7, 22, 0xFD469501 ); 123 | P( A, B, C, D, 8, 7, 0x698098D8 ); 124 | P( D, A, B, C, 9, 12, 0x8B44F7AF ); 125 | P( C, D, A, B, 10, 17, 0xFFFF5BB1 ); 126 | P( B, C, D, A, 11, 22, 0x895CD7BE ); 127 | P( A, B, C, D, 12, 7, 0x6B901122 ); 128 | P( D, A, B, C, 13, 12, 0xFD987193 ); 129 | P( C, D, A, B, 14, 17, 0xA679438E ); 130 | P( B, C, D, A, 15, 22, 0x49B40821 ); 131 | 132 | #undef F 133 | 134 | #define F(x,y,z) (y ^ (z & (x ^ y))) 135 | 136 | P( A, B, C, D, 1, 5, 0xF61E2562 ); 137 | P( D, A, B, C, 6, 9, 0xC040B340 ); 138 | P( C, D, A, B, 11, 14, 0x265E5A51 ); 139 | P( B, C, D, A, 0, 20, 0xE9B6C7AA ); 140 | P( A, B, C, D, 5, 5, 0xD62F105D ); 141 | P( D, A, B, C, 10, 9, 0x02441453 ); 142 | P( C, D, A, B, 15, 14, 0xD8A1E681 ); 143 | P( B, C, D, A, 4, 20, 0xE7D3FBC8 ); 144 | P( A, B, C, D, 9, 5, 0x21E1CDE6 ); 145 | P( D, A, B, C, 14, 9, 0xC33707D6 ); 146 | P( C, D, A, B, 3, 14, 0xF4D50D87 ); 147 | P( B, C, D, A, 8, 20, 0x455A14ED ); 148 | P( A, B, C, D, 13, 5, 0xA9E3E905 ); 149 | P( D, A, B, C, 2, 9, 0xFCEFA3F8 ); 150 | P( C, D, A, B, 7, 14, 0x676F02D9 ); 151 | P( B, C, D, A, 12, 20, 0x8D2A4C8A ); 152 | 153 | #undef F 154 | 155 | #define F(x,y,z) (x ^ y ^ z) 156 | 157 | P( A, B, C, D, 5, 4, 0xFFFA3942 ); 158 | P( D, A, B, C, 8, 11, 0x8771F681 ); 159 | P( C, D, A, B, 11, 16, 0x6D9D6122 ); 160 | P( B, C, D, A, 14, 23, 0xFDE5380C ); 161 | P( A, B, C, D, 1, 4, 0xA4BEEA44 ); 162 | P( D, A, B, C, 4, 11, 0x4BDECFA9 ); 163 | P( C, D, A, B, 7, 16, 0xF6BB4B60 ); 164 | P( B, C, D, A, 10, 23, 0xBEBFBC70 ); 165 | P( A, B, C, D, 13, 4, 0x289B7EC6 ); 166 | P( D, A, B, C, 0, 11, 0xEAA127FA ); 167 | P( C, D, A, B, 3, 16, 0xD4EF3085 ); 168 | P( B, C, D, A, 6, 23, 0x04881D05 ); 169 | P( A, B, C, D, 9, 4, 0xD9D4D039 ); 170 | P( D, A, B, C, 12, 11, 0xE6DB99E5 ); 171 | P( C, D, A, B, 15, 16, 0x1FA27CF8 ); 172 | P( B, C, D, A, 2, 23, 0xC4AC5665 ); 173 | 174 | #undef F 175 | 176 | #define F(x,y,z) (y ^ (x | ~z)) 177 | 178 | P( A, B, C, D, 0, 6, 0xF4292244 ); 179 | P( D, A, B, C, 7, 10, 0x432AFF97 ); 180 | P( C, D, A, B, 14, 15, 0xAB9423A7 ); 181 | P( B, C, D, A, 5, 21, 0xFC93A039 ); 182 | P( A, B, C, D, 12, 6, 0x655B59C3 ); 183 | P( D, A, B, C, 3, 10, 0x8F0CCC92 ); 184 | P( C, D, A, B, 10, 15, 0xFFEFF47D ); 185 | P( B, C, D, A, 1, 21, 0x85845DD1 ); 186 | P( A, B, C, D, 8, 6, 0x6FA87E4F ); 187 | P( D, A, B, C, 15, 10, 0xFE2CE6E0 ); 188 | P( C, D, A, B, 6, 15, 0xA3014314 ); 189 | P( B, C, D, A, 13, 21, 0x4E0811A1 ); 190 | P( A, B, C, D, 4, 6, 0xF7537E82 ); 191 | P( D, A, B, C, 11, 10, 0xBD3AF235 ); 192 | P( C, D, A, B, 2, 15, 0x2AD7D2BB ); 193 | P( B, C, D, A, 9, 21, 0xEB86D391 ); 194 | 195 | #undef F 196 | 197 | ctx->state[0] += A; 198 | ctx->state[1] += B; 199 | ctx->state[2] += C; 200 | ctx->state[3] += D; 201 | } 202 | 203 | /* 204 | * MD5 process buffer 205 | */ 206 | void md5_update( md5_context *ctx, const unsigned char *input, size_t ilen ) 207 | { 208 | size_t fill; 209 | uint32_t left; 210 | 211 | if( ilen <= 0 ) 212 | return; 213 | 214 | left = ctx->total[0] & 0x3F; 215 | fill = 64 - left; 216 | 217 | ctx->total[0] += (uint32_t) ilen; 218 | ctx->total[0] &= 0xFFFFFFFF; 219 | 220 | if( ctx->total[0] < (uint32_t) ilen ) 221 | ctx->total[1]++; 222 | 223 | if( left && ilen >= fill ) 224 | { 225 | memcpy( (void *) (ctx->buffer + left), input, fill ); 226 | md5_process( ctx, ctx->buffer ); 227 | input += fill; 228 | ilen -= fill; 229 | left = 0; 230 | } 231 | 232 | while( ilen >= 64 ) 233 | { 234 | md5_process( ctx, input ); 235 | input += 64; 236 | ilen -= 64; 237 | } 238 | 239 | if( ilen > 0 ) 240 | { 241 | memcpy( (void *) (ctx->buffer + left), input, ilen ); 242 | } 243 | } 244 | 245 | static const unsigned char md5_padding[64] = 246 | { 247 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 248 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 249 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 250 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 251 | }; 252 | 253 | /* 254 | * MD5 final digest 255 | */ 256 | void md5_finish( md5_context *ctx, unsigned char output[16] ) 257 | { 258 | uint32_t last, padn; 259 | uint32_t high, low; 260 | unsigned char msglen[8]; 261 | 262 | high = ( ctx->total[0] >> 29 ) 263 | | ( ctx->total[1] << 3 ); 264 | low = ( ctx->total[0] << 3 ); 265 | 266 | PUT_UINT32_LE( low, msglen, 0 ); 267 | PUT_UINT32_LE( high, msglen, 4 ); 268 | 269 | last = ctx->total[0] & 0x3F; 270 | padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); 271 | 272 | md5_update( ctx, md5_padding, padn ); 273 | md5_update( ctx, msglen, 8 ); 274 | 275 | PUT_UINT32_LE( ctx->state[0], output, 0 ); 276 | PUT_UINT32_LE( ctx->state[1], output, 4 ); 277 | PUT_UINT32_LE( ctx->state[2], output, 8 ); 278 | PUT_UINT32_LE( ctx->state[3], output, 12 ); 279 | } 280 | 281 | #endif /* !POLARSSL_MD5_ALT */ 282 | 283 | /* 284 | * output = MD5( input buffer ) 285 | */ 286 | void md5( const unsigned char *input, size_t ilen, unsigned char output[16] ) 287 | { 288 | md5_context ctx; 289 | 290 | md5_starts( &ctx ); 291 | md5_update( &ctx, input, ilen ); 292 | md5_finish( &ctx, output ); 293 | 294 | memset( &ctx, 0, sizeof( md5_context ) ); 295 | } 296 | 297 | #if defined(POLARSSL_FS_IO) 298 | /* 299 | * output = MD5( file contents ) 300 | */ 301 | int md5_file( const char *path, unsigned char output[16] ) 302 | { 303 | FILE *f; 304 | size_t n; 305 | md5_context ctx; 306 | unsigned char buf[1024]; 307 | 308 | if( ( f = fopen( path, "rb" ) ) == NULL ) 309 | return( POLARSSL_ERR_MD5_FILE_IO_ERROR ); 310 | 311 | md5_starts( &ctx ); 312 | 313 | while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) 314 | md5_update( &ctx, buf, n ); 315 | 316 | md5_finish( &ctx, output ); 317 | 318 | memset( &ctx, 0, sizeof( md5_context ) ); 319 | 320 | if( ferror( f ) != 0 ) 321 | { 322 | fclose( f ); 323 | return( POLARSSL_ERR_MD5_FILE_IO_ERROR ); 324 | } 325 | 326 | fclose( f ); 327 | return( 0 ); 328 | } 329 | #endif /* POLARSSL_FS_IO */ 330 | 331 | /* 332 | * MD5 HMAC context setup 333 | */ 334 | void md5_hmac_starts( md5_context *ctx, const unsigned char *key, size_t keylen ) 335 | { 336 | size_t i; 337 | unsigned char sum[16]; 338 | 339 | if( keylen > 64 ) 340 | { 341 | md5( key, keylen, sum ); 342 | keylen = 16; 343 | key = sum; 344 | } 345 | 346 | memset( ctx->ipad, 0x36, 64 ); 347 | memset( ctx->opad, 0x5C, 64 ); 348 | 349 | for( i = 0; i < keylen; i++ ) 350 | { 351 | ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] ); 352 | ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] ); 353 | } 354 | 355 | md5_starts( ctx ); 356 | md5_update( ctx, ctx->ipad, 64 ); 357 | 358 | memset( sum, 0, sizeof( sum ) ); 359 | } 360 | 361 | /* 362 | * MD5 HMAC process buffer 363 | */ 364 | void md5_hmac_update( md5_context *ctx, const unsigned char *input, size_t ilen ) 365 | { 366 | md5_update( ctx, input, ilen ); 367 | } 368 | 369 | /* 370 | * MD5 HMAC final digest 371 | */ 372 | void md5_hmac_finish( md5_context *ctx, unsigned char output[16] ) 373 | { 374 | unsigned char tmpbuf[16]; 375 | 376 | md5_finish( ctx, tmpbuf ); 377 | md5_starts( ctx ); 378 | md5_update( ctx, ctx->opad, 64 ); 379 | md5_update( ctx, tmpbuf, 16 ); 380 | md5_finish( ctx, output ); 381 | 382 | memset( tmpbuf, 0, sizeof( tmpbuf ) ); 383 | } 384 | 385 | /* 386 | * MD5 HMAC context reset 387 | */ 388 | void md5_hmac_reset( md5_context *ctx ) 389 | { 390 | md5_starts( ctx ); 391 | md5_update( ctx, ctx->ipad, 64 ); 392 | } 393 | 394 | /* 395 | * output = HMAC-MD5( hmac key, input buffer ) 396 | */ 397 | void md5_hmac( const unsigned char *key, size_t keylen, 398 | const unsigned char *input, size_t ilen, 399 | unsigned char output[16] ) 400 | { 401 | md5_context ctx; 402 | 403 | md5_hmac_starts( &ctx, key, keylen ); 404 | md5_hmac_update( &ctx, input, ilen ); 405 | md5_hmac_finish( &ctx, output ); 406 | 407 | memset( &ctx, 0, sizeof( md5_context ) ); 408 | } 409 | 410 | #if defined(POLARSSL_SELF_TEST) 411 | /* 412 | * RFC 1321 test vectors 413 | */ 414 | static unsigned char md5_test_buf[7][81] = 415 | { 416 | { "" }, 417 | { "a" }, 418 | { "abc" }, 419 | { "message digest" }, 420 | { "abcdefghijklmnopqrstuvwxyz" }, 421 | { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" }, 422 | { "12345678901234567890123456789012345678901234567890123456789012" \ 423 | "345678901234567890" } 424 | }; 425 | 426 | static const int md5_test_buflen[7] = 427 | { 428 | 0, 1, 3, 14, 26, 62, 80 429 | }; 430 | 431 | static const unsigned char md5_test_sum[7][16] = 432 | { 433 | { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04, 434 | 0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E }, 435 | { 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8, 436 | 0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 }, 437 | { 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0, 438 | 0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 }, 439 | { 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D, 440 | 0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 }, 441 | { 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00, 442 | 0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B }, 443 | { 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5, 444 | 0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F }, 445 | { 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55, 446 | 0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A } 447 | }; 448 | 449 | /* 450 | * RFC 2202 test vectors 451 | */ 452 | static unsigned char md5_hmac_test_key[7][26] = 453 | { 454 | { "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B" }, 455 | { "Jefe" }, 456 | { "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" }, 457 | { "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10" 458 | "\x11\x12\x13\x14\x15\x16\x17\x18\x19" }, 459 | { "\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C" }, 460 | { "" }, /* 0xAA 80 times */ 461 | { "" } 462 | }; 463 | 464 | static const int md5_hmac_test_keylen[7] = 465 | { 466 | 16, 4, 16, 25, 16, 80, 80 467 | }; 468 | 469 | static unsigned char md5_hmac_test_buf[7][74] = 470 | { 471 | { "Hi There" }, 472 | { "what do ya want for nothing?" }, 473 | { "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" 474 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" 475 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" 476 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" 477 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" }, 478 | { "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" 479 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" 480 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" 481 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" 482 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" }, 483 | { "Test With Truncation" }, 484 | { "Test Using Larger Than Block-Size Key - Hash Key First" }, 485 | { "Test Using Larger Than Block-Size Key and Larger" 486 | " Than One Block-Size Data" } 487 | }; 488 | 489 | static const int md5_hmac_test_buflen[7] = 490 | { 491 | 8, 28, 50, 50, 20, 54, 73 492 | }; 493 | 494 | static const unsigned char md5_hmac_test_sum[7][16] = 495 | { 496 | { 0x92, 0x94, 0x72, 0x7A, 0x36, 0x38, 0xBB, 0x1C, 497 | 0x13, 0xF4, 0x8E, 0xF8, 0x15, 0x8B, 0xFC, 0x9D }, 498 | { 0x75, 0x0C, 0x78, 0x3E, 0x6A, 0xB0, 0xB5, 0x03, 499 | 0xEA, 0xA8, 0x6E, 0x31, 0x0A, 0x5D, 0xB7, 0x38 }, 500 | { 0x56, 0xBE, 0x34, 0x52, 0x1D, 0x14, 0x4C, 0x88, 501 | 0xDB, 0xB8, 0xC7, 0x33, 0xF0, 0xE8, 0xB3, 0xF6 }, 502 | { 0x69, 0x7E, 0xAF, 0x0A, 0xCA, 0x3A, 0x3A, 0xEA, 503 | 0x3A, 0x75, 0x16, 0x47, 0x46, 0xFF, 0xAA, 0x79 }, 504 | { 0x56, 0x46, 0x1E, 0xF2, 0x34, 0x2E, 0xDC, 0x00, 505 | 0xF9, 0xBA, 0xB9, 0x95 }, 506 | { 0x6B, 0x1A, 0xB7, 0xFE, 0x4B, 0xD7, 0xBF, 0x8F, 507 | 0x0B, 0x62, 0xE6, 0xCE, 0x61, 0xB9, 0xD0, 0xCD }, 508 | { 0x6F, 0x63, 0x0F, 0xAD, 0x67, 0xCD, 0xA0, 0xEE, 509 | 0x1F, 0xB1, 0xF5, 0x62, 0xDB, 0x3A, 0xA5, 0x3E } 510 | }; 511 | 512 | /* 513 | * Checkup routine 514 | */ 515 | int md5_self_test( int verbose ) 516 | { 517 | int i, buflen; 518 | unsigned char buf[1024]; 519 | unsigned char md5sum[16]; 520 | md5_context ctx; 521 | 522 | for( i = 0; i < 7; i++ ) 523 | { 524 | if( verbose != 0 ) 525 | printf( " MD5 test #%d: ", i + 1 ); 526 | 527 | md5( md5_test_buf[i], md5_test_buflen[i], md5sum ); 528 | 529 | if( memcmp( md5sum, md5_test_sum[i], 16 ) != 0 ) 530 | { 531 | if( verbose != 0 ) 532 | printf( "failed\n" ); 533 | 534 | return( 1 ); 535 | } 536 | 537 | if( verbose != 0 ) 538 | printf( "passed\n" ); 539 | } 540 | 541 | if( verbose != 0 ) 542 | printf( "\n" ); 543 | 544 | for( i = 0; i < 7; i++ ) 545 | { 546 | if( verbose != 0 ) 547 | printf( " HMAC-MD5 test #%d: ", i + 1 ); 548 | 549 | if( i == 5 || i == 6 ) 550 | { 551 | memset( buf, '\xAA', buflen = 80 ); 552 | md5_hmac_starts( &ctx, buf, buflen ); 553 | } 554 | else 555 | md5_hmac_starts( &ctx, md5_hmac_test_key[i], 556 | md5_hmac_test_keylen[i] ); 557 | 558 | md5_hmac_update( &ctx, md5_hmac_test_buf[i], 559 | md5_hmac_test_buflen[i] ); 560 | 561 | md5_hmac_finish( &ctx, md5sum ); 562 | 563 | buflen = ( i == 4 ) ? 12 : 16; 564 | 565 | if( memcmp( md5sum, md5_hmac_test_sum[i], buflen ) != 0 ) 566 | { 567 | if( verbose != 0 ) 568 | printf( "failed\n" ); 569 | 570 | return( 1 ); 571 | } 572 | 573 | if( verbose != 0 ) 574 | printf( "passed\n" ); 575 | } 576 | 577 | if( verbose != 0 ) 578 | printf( "\n" ); 579 | 580 | return( 0 ); 581 | } 582 | 583 | #endif 584 | 585 | #endif 586 | -------------------------------------------------------------------------------- /receiver/polarssl/sha1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * FIPS-180-1 compliant SHA-1 implementation 3 | * 4 | * Copyright (C) 2006-2013, Brainspark B.V. 5 | * 6 | * This file is part of PolarSSL (http://www.polarssl.org) 7 | * Lead Maintainer: Paul Bakker 8 | * 9 | * All rights reserved. 10 | * 11 | * This program is free software; you can redistribute it and/or modify 12 | * it under the terms of the GNU General Public License as published by 13 | * the Free Software Foundation; either version 2 of the License, or 14 | * (at your option) any later version. 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU General Public License along 22 | * with this program; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 24 | */ 25 | /* 26 | * The SHA-1 standard was published by NIST in 1993. 27 | * 28 | * http://www.itl.nist.gov/fipspubs/fip180-1.htm 29 | */ 30 | 31 | #include "polarssl/config.h" 32 | 33 | #if defined(POLARSSL_SHA1_C) 34 | 35 | #include "polarssl/sha1.h" 36 | 37 | #if defined(POLARSSL_FS_IO) || defined(POLARSSL_SELF_TEST) 38 | #include 39 | #endif 40 | 41 | #if !defined(POLARSSL_SHA1_ALT) 42 | 43 | /* 44 | * 32-bit integer manipulation macros (big endian) 45 | */ 46 | #ifndef GET_UINT32_BE 47 | #define GET_UINT32_BE(n,b,i) \ 48 | { \ 49 | (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ 50 | | ( (uint32_t) (b)[(i) + 1] << 16 ) \ 51 | | ( (uint32_t) (b)[(i) + 2] << 8 ) \ 52 | | ( (uint32_t) (b)[(i) + 3] ); \ 53 | } 54 | #endif 55 | 56 | #ifndef PUT_UINT32_BE 57 | #define PUT_UINT32_BE(n,b,i) \ 58 | { \ 59 | (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ 60 | (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ 61 | (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ 62 | (b)[(i) + 3] = (unsigned char) ( (n) ); \ 63 | } 64 | #endif 65 | 66 | /* 67 | * SHA-1 context setup 68 | */ 69 | void sha1_starts( sha1_context *ctx ) 70 | { 71 | ctx->total[0] = 0; 72 | ctx->total[1] = 0; 73 | 74 | ctx->state[0] = 0x67452301; 75 | ctx->state[1] = 0xEFCDAB89; 76 | ctx->state[2] = 0x98BADCFE; 77 | ctx->state[3] = 0x10325476; 78 | ctx->state[4] = 0xC3D2E1F0; 79 | } 80 | 81 | void sha1_process( sha1_context *ctx, const unsigned char data[64] ) 82 | { 83 | uint32_t temp, W[16], A, B, C, D, E; 84 | 85 | GET_UINT32_BE( W[ 0], data, 0 ); 86 | GET_UINT32_BE( W[ 1], data, 4 ); 87 | GET_UINT32_BE( W[ 2], data, 8 ); 88 | GET_UINT32_BE( W[ 3], data, 12 ); 89 | GET_UINT32_BE( W[ 4], data, 16 ); 90 | GET_UINT32_BE( W[ 5], data, 20 ); 91 | GET_UINT32_BE( W[ 6], data, 24 ); 92 | GET_UINT32_BE( W[ 7], data, 28 ); 93 | GET_UINT32_BE( W[ 8], data, 32 ); 94 | GET_UINT32_BE( W[ 9], data, 36 ); 95 | GET_UINT32_BE( W[10], data, 40 ); 96 | GET_UINT32_BE( W[11], data, 44 ); 97 | GET_UINT32_BE( W[12], data, 48 ); 98 | GET_UINT32_BE( W[13], data, 52 ); 99 | GET_UINT32_BE( W[14], data, 56 ); 100 | GET_UINT32_BE( W[15], data, 60 ); 101 | 102 | #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) 103 | 104 | #define R(t) \ 105 | ( \ 106 | temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ \ 107 | W[(t - 14) & 0x0F] ^ W[ t & 0x0F], \ 108 | ( W[t & 0x0F] = S(temp,1) ) \ 109 | ) 110 | 111 | #define P(a,b,c,d,e,x) \ 112 | { \ 113 | e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \ 114 | } 115 | 116 | A = ctx->state[0]; 117 | B = ctx->state[1]; 118 | C = ctx->state[2]; 119 | D = ctx->state[3]; 120 | E = ctx->state[4]; 121 | 122 | #define F(x,y,z) (z ^ (x & (y ^ z))) 123 | #define K 0x5A827999 124 | 125 | P( A, B, C, D, E, W[0] ); 126 | P( E, A, B, C, D, W[1] ); 127 | P( D, E, A, B, C, W[2] ); 128 | P( C, D, E, A, B, W[3] ); 129 | P( B, C, D, E, A, W[4] ); 130 | P( A, B, C, D, E, W[5] ); 131 | P( E, A, B, C, D, W[6] ); 132 | P( D, E, A, B, C, W[7] ); 133 | P( C, D, E, A, B, W[8] ); 134 | P( B, C, D, E, A, W[9] ); 135 | P( A, B, C, D, E, W[10] ); 136 | P( E, A, B, C, D, W[11] ); 137 | P( D, E, A, B, C, W[12] ); 138 | P( C, D, E, A, B, W[13] ); 139 | P( B, C, D, E, A, W[14] ); 140 | P( A, B, C, D, E, W[15] ); 141 | P( E, A, B, C, D, R(16) ); 142 | P( D, E, A, B, C, R(17) ); 143 | P( C, D, E, A, B, R(18) ); 144 | P( B, C, D, E, A, R(19) ); 145 | 146 | #undef K 147 | #undef F 148 | 149 | #define F(x,y,z) (x ^ y ^ z) 150 | #define K 0x6ED9EBA1 151 | 152 | P( A, B, C, D, E, R(20) ); 153 | P( E, A, B, C, D, R(21) ); 154 | P( D, E, A, B, C, R(22) ); 155 | P( C, D, E, A, B, R(23) ); 156 | P( B, C, D, E, A, R(24) ); 157 | P( A, B, C, D, E, R(25) ); 158 | P( E, A, B, C, D, R(26) ); 159 | P( D, E, A, B, C, R(27) ); 160 | P( C, D, E, A, B, R(28) ); 161 | P( B, C, D, E, A, R(29) ); 162 | P( A, B, C, D, E, R(30) ); 163 | P( E, A, B, C, D, R(31) ); 164 | P( D, E, A, B, C, R(32) ); 165 | P( C, D, E, A, B, R(33) ); 166 | P( B, C, D, E, A, R(34) ); 167 | P( A, B, C, D, E, R(35) ); 168 | P( E, A, B, C, D, R(36) ); 169 | P( D, E, A, B, C, R(37) ); 170 | P( C, D, E, A, B, R(38) ); 171 | P( B, C, D, E, A, R(39) ); 172 | 173 | #undef K 174 | #undef F 175 | 176 | #define F(x,y,z) ((x & y) | (z & (x | y))) 177 | #define K 0x8F1BBCDC 178 | 179 | P( A, B, C, D, E, R(40) ); 180 | P( E, A, B, C, D, R(41) ); 181 | P( D, E, A, B, C, R(42) ); 182 | P( C, D, E, A, B, R(43) ); 183 | P( B, C, D, E, A, R(44) ); 184 | P( A, B, C, D, E, R(45) ); 185 | P( E, A, B, C, D, R(46) ); 186 | P( D, E, A, B, C, R(47) ); 187 | P( C, D, E, A, B, R(48) ); 188 | P( B, C, D, E, A, R(49) ); 189 | P( A, B, C, D, E, R(50) ); 190 | P( E, A, B, C, D, R(51) ); 191 | P( D, E, A, B, C, R(52) ); 192 | P( C, D, E, A, B, R(53) ); 193 | P( B, C, D, E, A, R(54) ); 194 | P( A, B, C, D, E, R(55) ); 195 | P( E, A, B, C, D, R(56) ); 196 | P( D, E, A, B, C, R(57) ); 197 | P( C, D, E, A, B, R(58) ); 198 | P( B, C, D, E, A, R(59) ); 199 | 200 | #undef K 201 | #undef F 202 | 203 | #define F(x,y,z) (x ^ y ^ z) 204 | #define K 0xCA62C1D6 205 | 206 | P( A, B, C, D, E, R(60) ); 207 | P( E, A, B, C, D, R(61) ); 208 | P( D, E, A, B, C, R(62) ); 209 | P( C, D, E, A, B, R(63) ); 210 | P( B, C, D, E, A, R(64) ); 211 | P( A, B, C, D, E, R(65) ); 212 | P( E, A, B, C, D, R(66) ); 213 | P( D, E, A, B, C, R(67) ); 214 | P( C, D, E, A, B, R(68) ); 215 | P( B, C, D, E, A, R(69) ); 216 | P( A, B, C, D, E, R(70) ); 217 | P( E, A, B, C, D, R(71) ); 218 | P( D, E, A, B, C, R(72) ); 219 | P( C, D, E, A, B, R(73) ); 220 | P( B, C, D, E, A, R(74) ); 221 | P( A, B, C, D, E, R(75) ); 222 | P( E, A, B, C, D, R(76) ); 223 | P( D, E, A, B, C, R(77) ); 224 | P( C, D, E, A, B, R(78) ); 225 | P( B, C, D, E, A, R(79) ); 226 | 227 | #undef K 228 | #undef F 229 | 230 | ctx->state[0] += A; 231 | ctx->state[1] += B; 232 | ctx->state[2] += C; 233 | ctx->state[3] += D; 234 | ctx->state[4] += E; 235 | } 236 | 237 | /* 238 | * SHA-1 process buffer 239 | */ 240 | void sha1_update( sha1_context *ctx, const unsigned char *input, size_t ilen ) 241 | { 242 | size_t fill; 243 | uint32_t left; 244 | 245 | if( ilen <= 0 ) 246 | return; 247 | 248 | left = ctx->total[0] & 0x3F; 249 | fill = 64 - left; 250 | 251 | ctx->total[0] += (uint32_t) ilen; 252 | ctx->total[0] &= 0xFFFFFFFF; 253 | 254 | if( ctx->total[0] < (uint32_t) ilen ) 255 | ctx->total[1]++; 256 | 257 | if( left && ilen >= fill ) 258 | { 259 | memcpy( (void *) (ctx->buffer + left), input, fill ); 260 | sha1_process( ctx, ctx->buffer ); 261 | input += fill; 262 | ilen -= fill; 263 | left = 0; 264 | } 265 | 266 | while( ilen >= 64 ) 267 | { 268 | sha1_process( ctx, input ); 269 | input += 64; 270 | ilen -= 64; 271 | } 272 | 273 | if( ilen > 0 ) 274 | memcpy( (void *) (ctx->buffer + left), input, ilen ); 275 | } 276 | 277 | static const unsigned char sha1_padding[64] = 278 | { 279 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 280 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 281 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 282 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 283 | }; 284 | 285 | /* 286 | * SHA-1 final digest 287 | */ 288 | void sha1_finish( sha1_context *ctx, unsigned char output[20] ) 289 | { 290 | uint32_t last, padn; 291 | uint32_t high, low; 292 | unsigned char msglen[8]; 293 | 294 | high = ( ctx->total[0] >> 29 ) 295 | | ( ctx->total[1] << 3 ); 296 | low = ( ctx->total[0] << 3 ); 297 | 298 | PUT_UINT32_BE( high, msglen, 0 ); 299 | PUT_UINT32_BE( low, msglen, 4 ); 300 | 301 | last = ctx->total[0] & 0x3F; 302 | padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); 303 | 304 | sha1_update( ctx, sha1_padding, padn ); 305 | sha1_update( ctx, msglen, 8 ); 306 | 307 | PUT_UINT32_BE( ctx->state[0], output, 0 ); 308 | PUT_UINT32_BE( ctx->state[1], output, 4 ); 309 | PUT_UINT32_BE( ctx->state[2], output, 8 ); 310 | PUT_UINT32_BE( ctx->state[3], output, 12 ); 311 | PUT_UINT32_BE( ctx->state[4], output, 16 ); 312 | } 313 | 314 | #endif /* !POLARSSL_SHA1_ALT */ 315 | 316 | /* 317 | * output = SHA-1( input buffer ) 318 | */ 319 | void sha1( const unsigned char *input, size_t ilen, unsigned char output[20] ) 320 | { 321 | sha1_context ctx; 322 | 323 | sha1_starts( &ctx ); 324 | sha1_update( &ctx, input, ilen ); 325 | sha1_finish( &ctx, output ); 326 | 327 | memset( &ctx, 0, sizeof( sha1_context ) ); 328 | } 329 | 330 | #if defined(POLARSSL_FS_IO) 331 | /* 332 | * output = SHA-1( file contents ) 333 | */ 334 | int sha1_file( const char *path, unsigned char output[20] ) 335 | { 336 | FILE *f; 337 | size_t n; 338 | sha1_context ctx; 339 | unsigned char buf[1024]; 340 | 341 | if( ( f = fopen( path, "rb" ) ) == NULL ) 342 | return( POLARSSL_ERR_SHA1_FILE_IO_ERROR ); 343 | 344 | sha1_starts( &ctx ); 345 | 346 | while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) 347 | sha1_update( &ctx, buf, n ); 348 | 349 | sha1_finish( &ctx, output ); 350 | 351 | memset( &ctx, 0, sizeof( sha1_context ) ); 352 | 353 | if( ferror( f ) != 0 ) 354 | { 355 | fclose( f ); 356 | return( POLARSSL_ERR_SHA1_FILE_IO_ERROR ); 357 | } 358 | 359 | fclose( f ); 360 | return( 0 ); 361 | } 362 | #endif /* POLARSSL_FS_IO */ 363 | 364 | /* 365 | * SHA-1 HMAC context setup 366 | */ 367 | void sha1_hmac_starts( sha1_context *ctx, const unsigned char *key, size_t keylen ) 368 | { 369 | size_t i; 370 | unsigned char sum[20]; 371 | 372 | if( keylen > 64 ) 373 | { 374 | sha1( key, keylen, sum ); 375 | keylen = 20; 376 | key = sum; 377 | } 378 | 379 | memset( ctx->ipad, 0x36, 64 ); 380 | memset( ctx->opad, 0x5C, 64 ); 381 | 382 | for( i = 0; i < keylen; i++ ) 383 | { 384 | ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] ); 385 | ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] ); 386 | } 387 | 388 | sha1_starts( ctx ); 389 | sha1_update( ctx, ctx->ipad, 64 ); 390 | 391 | memset( sum, 0, sizeof( sum ) ); 392 | } 393 | 394 | /* 395 | * SHA-1 HMAC process buffer 396 | */ 397 | void sha1_hmac_update( sha1_context *ctx, const unsigned char *input, size_t ilen ) 398 | { 399 | sha1_update( ctx, input, ilen ); 400 | } 401 | 402 | /* 403 | * SHA-1 HMAC final digest 404 | */ 405 | void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] ) 406 | { 407 | unsigned char tmpbuf[20]; 408 | 409 | sha1_finish( ctx, tmpbuf ); 410 | sha1_starts( ctx ); 411 | sha1_update( ctx, ctx->opad, 64 ); 412 | sha1_update( ctx, tmpbuf, 20 ); 413 | sha1_finish( ctx, output ); 414 | 415 | memset( tmpbuf, 0, sizeof( tmpbuf ) ); 416 | } 417 | 418 | /* 419 | * SHA1 HMAC context reset 420 | */ 421 | void sha1_hmac_reset( sha1_context *ctx ) 422 | { 423 | sha1_starts( ctx ); 424 | sha1_update( ctx, ctx->ipad, 64 ); 425 | } 426 | 427 | /* 428 | * output = HMAC-SHA-1( hmac key, input buffer ) 429 | */ 430 | void sha1_hmac( const unsigned char *key, size_t keylen, 431 | const unsigned char *input, size_t ilen, 432 | unsigned char output[20] ) 433 | { 434 | sha1_context ctx; 435 | 436 | sha1_hmac_starts( &ctx, key, keylen ); 437 | sha1_hmac_update( &ctx, input, ilen ); 438 | sha1_hmac_finish( &ctx, output ); 439 | 440 | memset( &ctx, 0, sizeof( sha1_context ) ); 441 | } 442 | 443 | #if defined(POLARSSL_SELF_TEST) 444 | /* 445 | * FIPS-180-1 test vectors 446 | */ 447 | static unsigned char sha1_test_buf[3][57] = 448 | { 449 | { "abc" }, 450 | { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" }, 451 | { "" } 452 | }; 453 | 454 | static const int sha1_test_buflen[3] = 455 | { 456 | 3, 56, 1000 457 | }; 458 | 459 | static const unsigned char sha1_test_sum[3][20] = 460 | { 461 | { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E, 462 | 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D }, 463 | { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE, 464 | 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 }, 465 | { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E, 466 | 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F } 467 | }; 468 | 469 | /* 470 | * RFC 2202 test vectors 471 | */ 472 | static unsigned char sha1_hmac_test_key[7][26] = 473 | { 474 | { "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B" 475 | "\x0B\x0B\x0B\x0B" }, 476 | { "Jefe" }, 477 | { "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" 478 | "\xAA\xAA\xAA\xAA" }, 479 | { "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10" 480 | "\x11\x12\x13\x14\x15\x16\x17\x18\x19" }, 481 | { "\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C" 482 | "\x0C\x0C\x0C\x0C" }, 483 | { "" }, /* 0xAA 80 times */ 484 | { "" } 485 | }; 486 | 487 | static const int sha1_hmac_test_keylen[7] = 488 | { 489 | 20, 4, 20, 25, 20, 80, 80 490 | }; 491 | 492 | static unsigned char sha1_hmac_test_buf[7][74] = 493 | { 494 | { "Hi There" }, 495 | { "what do ya want for nothing?" }, 496 | { "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" 497 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" 498 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" 499 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" 500 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" }, 501 | { "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" 502 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" 503 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" 504 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" 505 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" }, 506 | { "Test With Truncation" }, 507 | { "Test Using Larger Than Block-Size Key - Hash Key First" }, 508 | { "Test Using Larger Than Block-Size Key and Larger" 509 | " Than One Block-Size Data" } 510 | }; 511 | 512 | static const int sha1_hmac_test_buflen[7] = 513 | { 514 | 8, 28, 50, 50, 20, 54, 73 515 | }; 516 | 517 | static const unsigned char sha1_hmac_test_sum[7][20] = 518 | { 519 | { 0xB6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xE2, 0x8B, 520 | 0xC0, 0xB6, 0xFB, 0x37, 0x8C, 0x8E, 0xF1, 0x46, 0xBE, 0x00 }, 521 | { 0xEF, 0xFC, 0xDF, 0x6A, 0xE5, 0xEB, 0x2F, 0xA2, 0xD2, 0x74, 522 | 0x16, 0xD5, 0xF1, 0x84, 0xDF, 0x9C, 0x25, 0x9A, 0x7C, 0x79 }, 523 | { 0x12, 0x5D, 0x73, 0x42, 0xB9, 0xAC, 0x11, 0xCD, 0x91, 0xA3, 524 | 0x9A, 0xF4, 0x8A, 0xA1, 0x7B, 0x4F, 0x63, 0xF1, 0x75, 0xD3 }, 525 | { 0x4C, 0x90, 0x07, 0xF4, 0x02, 0x62, 0x50, 0xC6, 0xBC, 0x84, 526 | 0x14, 0xF9, 0xBF, 0x50, 0xC8, 0x6C, 0x2D, 0x72, 0x35, 0xDA }, 527 | { 0x4C, 0x1A, 0x03, 0x42, 0x4B, 0x55, 0xE0, 0x7F, 0xE7, 0xF2, 528 | 0x7B, 0xE1 }, 529 | { 0xAA, 0x4A, 0xE5, 0xE1, 0x52, 0x72, 0xD0, 0x0E, 0x95, 0x70, 530 | 0x56, 0x37, 0xCE, 0x8A, 0x3B, 0x55, 0xED, 0x40, 0x21, 0x12 }, 531 | { 0xE8, 0xE9, 0x9D, 0x0F, 0x45, 0x23, 0x7D, 0x78, 0x6D, 0x6B, 532 | 0xBA, 0xA7, 0x96, 0x5C, 0x78, 0x08, 0xBB, 0xFF, 0x1A, 0x91 } 533 | }; 534 | 535 | /* 536 | * Checkup routine 537 | */ 538 | int sha1_self_test( int verbose ) 539 | { 540 | int i, j, buflen; 541 | unsigned char buf[1024]; 542 | unsigned char sha1sum[20]; 543 | sha1_context ctx; 544 | 545 | /* 546 | * SHA-1 547 | */ 548 | for( i = 0; i < 3; i++ ) 549 | { 550 | if( verbose != 0 ) 551 | printf( " SHA-1 test #%d: ", i + 1 ); 552 | 553 | sha1_starts( &ctx ); 554 | 555 | if( i == 2 ) 556 | { 557 | memset( buf, 'a', buflen = 1000 ); 558 | 559 | for( j = 0; j < 1000; j++ ) 560 | sha1_update( &ctx, buf, buflen ); 561 | } 562 | else 563 | sha1_update( &ctx, sha1_test_buf[i], 564 | sha1_test_buflen[i] ); 565 | 566 | sha1_finish( &ctx, sha1sum ); 567 | 568 | if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 ) 569 | { 570 | if( verbose != 0 ) 571 | printf( "failed\n" ); 572 | 573 | return( 1 ); 574 | } 575 | 576 | if( verbose != 0 ) 577 | printf( "passed\n" ); 578 | } 579 | 580 | if( verbose != 0 ) 581 | printf( "\n" ); 582 | 583 | for( i = 0; i < 7; i++ ) 584 | { 585 | if( verbose != 0 ) 586 | printf( " HMAC-SHA-1 test #%d: ", i + 1 ); 587 | 588 | if( i == 5 || i == 6 ) 589 | { 590 | memset( buf, '\xAA', buflen = 80 ); 591 | sha1_hmac_starts( &ctx, buf, buflen ); 592 | } 593 | else 594 | sha1_hmac_starts( &ctx, sha1_hmac_test_key[i], 595 | sha1_hmac_test_keylen[i] ); 596 | 597 | sha1_hmac_update( &ctx, sha1_hmac_test_buf[i], 598 | sha1_hmac_test_buflen[i] ); 599 | 600 | sha1_hmac_finish( &ctx, sha1sum ); 601 | 602 | buflen = ( i == 4 ) ? 12 : 20; 603 | 604 | if( memcmp( sha1sum, sha1_hmac_test_sum[i], buflen ) != 0 ) 605 | { 606 | if( verbose != 0 ) 607 | printf( "failed\n" ); 608 | 609 | return( 1 ); 610 | } 611 | 612 | if( verbose != 0 ) 613 | printf( "passed\n" ); 614 | } 615 | 616 | if( verbose != 0 ) 617 | printf( "\n" ); 618 | 619 | return( 0 ); 620 | } 621 | 622 | #endif 623 | 624 | #endif 625 | -------------------------------------------------------------------------------- /receiver/polarssl/config.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file config.h 3 | * 4 | * \brief Configuration options (set of defines) 5 | * 6 | * Copyright (C) 2006-2013, Brainspark B.V. 7 | * 8 | * This file is part of PolarSSL (http://www.polarssl.org) 9 | * Lead Maintainer: Paul Bakker 10 | * 11 | * All rights reserved. 12 | * 13 | * This program is free software; you can redistribute it and/or modify 14 | * it under the terms of the GNU General Public License as published by 15 | * the Free Software Foundation; either version 2 of the License, or 16 | * (at your option) any later version. 17 | * 18 | * This program is distributed in the hope that it will be useful, 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 | * GNU General Public License for more details. 22 | * 23 | * You should have received a copy of the GNU General Public License along 24 | * with this program; if not, write to the Free Software Foundation, Inc., 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 26 | * 27 | * This set of compile-time options may be used to enable 28 | * or disable features selectively, and reduce the global 29 | * memory footprint. 30 | */ 31 | #ifndef POLARSSL_CONFIG_H 32 | #define POLARSSL_CONFIG_H 33 | 34 | #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE) 35 | #define _CRT_SECURE_NO_DEPRECATE 1 36 | #endif 37 | 38 | /** 39 | * \name SECTION: System support 40 | * 41 | * This section sets system specific settings. 42 | * \{ 43 | */ 44 | 45 | /** 46 | * \def POLARSSL_HAVE_INT8 47 | * 48 | * The system uses 8-bit wide native integers. 49 | * 50 | * Uncomment if native integers are 8-bit wide. 51 | #define POLARSSL_HAVE_INT8 52 | */ 53 | 54 | /** 55 | * \def POLARSSL_HAVE_INT16 56 | * 57 | * The system uses 16-bit wide native integers. 58 | * 59 | * Uncomment if native integers are 16-bit wide. 60 | #define POLARSSL_HAVE_INT16 61 | */ 62 | 63 | /** 64 | * \def POLARSSL_HAVE_LONGLONG 65 | * 66 | * The compiler supports the 'long long' type. 67 | * (Only used on 32-bit platforms) 68 | */ 69 | #define POLARSSL_HAVE_LONGLONG 70 | 71 | /** 72 | * \def POLARSSL_HAVE_ASM 73 | * 74 | * The compiler has support for asm() 75 | * 76 | * Uncomment to enable the use of assembly code. 77 | * 78 | * Requires support for asm() in compiler. 79 | * 80 | * Used in: 81 | * library/timing.c 82 | * library/padlock.c 83 | * include/polarssl/bn_mul.h 84 | * 85 | */ 86 | #define POLARSSL_HAVE_ASM 87 | 88 | /** 89 | * \def POLARSSL_HAVE_SSE2 90 | * 91 | * CPU supports SSE2 instruction set. 92 | * 93 | * Uncomment if the CPU supports SSE2 (IA-32 specific). 94 | * 95 | #define POLARSSL_HAVE_SSE2 96 | */ 97 | /* \} name */ 98 | 99 | /** 100 | * \name SECTION: PolarSSL feature support 101 | * 102 | * This section sets support for features that are or are not needed 103 | * within the modules that are enabled. 104 | * \{ 105 | */ 106 | 107 | /** 108 | * \def POLARSSL_XXX_ALT 109 | * 110 | * Uncomment a macro to let PolarSSL use your alternate core implementation of 111 | * a symmetric or hash algorithm (e.g. platform specific assembly optimized 112 | * implementations). Keep in mind that the function prototypes should remain 113 | * the same. 114 | * 115 | * Example: In case you uncomment POLARSSL_AES_ALT, PolarSSL will no longer 116 | * provide the "struct aes_context" definition and omit the base function 117 | * declarations and implementations. "aes_alt.h" will be included from 118 | * "aes.h" to include the new function definitions. 119 | * 120 | * Uncomment a macro to enable alternate implementation for core algorithm 121 | * functions 122 | #define POLARSSL_AES_ALT 123 | #define POLARSSL_ARC4_ALT 124 | #define POLARSSL_BLOWFISH_ALT 125 | #define POLARSSL_CAMELLIA_ALT 126 | #define POLARSSL_DES_ALT 127 | #define POLARSSL_XTEA_ALT 128 | #define POLARSSL_MD2_ALT 129 | #define POLARSSL_MD4_ALT 130 | #define POLARSSL_MD5_ALT 131 | #define POLARSSL_SHA1_ALT 132 | #define POLARSSL_SHA2_ALT 133 | #define POLARSSL_SHA4_ALT 134 | */ 135 | 136 | /** 137 | * \def POLARSSL_AES_ROM_TABLES 138 | * 139 | * Store the AES tables in ROM. 140 | * 141 | * Uncomment this macro to store the AES tables in ROM. 142 | * 143 | #define POLARSSL_AES_ROM_TABLES 144 | */ 145 | 146 | /** 147 | * \def POLARSSL_CIPHER_MODE_CFB 148 | * 149 | * Enable Cipher Feedback mode (CFB) for symmetric ciphers. 150 | */ 151 | #define POLARSSL_CIPHER_MODE_CFB 152 | 153 | /** 154 | * \def POLARSSL_CIPHER_MODE_CTR 155 | * 156 | * Enable Counter Block Cipher mode (CTR) for symmetric ciphers. 157 | */ 158 | #define POLARSSL_CIPHER_MODE_CTR 159 | 160 | /** 161 | * \def POLARSSL_CIPHER_NULL_CIPHER 162 | * 163 | * Enable NULL cipher. 164 | * Warning: Only do so when you know what you are doing. This allows for 165 | * encryption or channels without any security! 166 | * 167 | * Requires POLARSSL_ENABLE_WEAK_CIPHERSUITES as well to enable 168 | * the following ciphersuites: 169 | * TLS_RSA_WITH_NULL_MD5 170 | * TLS_RSA_WITH_NULL_SHA 171 | * TLS_RSA_WITH_NULL_SHA256 172 | * 173 | * Uncomment this macro to enable the NULL cipher and ciphersuites 174 | #define POLARSSL_CIPHER_NULL_CIPHER 175 | */ 176 | 177 | /** 178 | * \def POLARSSL_ENABLE_WEAK_CIPHERSUITES 179 | * 180 | * Enable weak ciphersuites in SSL / TLS 181 | * Warning: Only do so when you know what you are doing. This allows for 182 | * channels with virtually no security at all! 183 | * 184 | * This enables the following ciphersuites: 185 | * TLS_RSA_WITH_DES_CBC_SHA 186 | * TLS_DHE_RSA_WITH_DES_CBC_SHA 187 | * 188 | * Uncomment this macro to enable weak ciphersuites 189 | #define POLARSSL_ENABLE_WEAK_CIPHERSUITES 190 | */ 191 | 192 | /** 193 | * \def POLARSSL_ERROR_STRERROR_DUMMY 194 | * 195 | * Enable a dummy error function to make use of error_strerror() in 196 | * third party libraries easier. 197 | * 198 | * Disable if you run into name conflicts and want to really remove the 199 | * error_strerror() 200 | */ 201 | #define POLARSSL_ERROR_STRERROR_DUMMY 202 | 203 | /** 204 | * \def POLARSSL_GENPRIME 205 | * 206 | * Requires: POLARSSL_BIGNUM_C, POLARSSL_RSA_C 207 | * 208 | * Enable the RSA prime-number generation code. 209 | */ 210 | #define POLARSSL_GENPRIME 211 | 212 | /** 213 | * \def POLARSSL_FS_IO 214 | * 215 | * Enable functions that use the filesystem. 216 | */ 217 | #define POLARSSL_FS_IO 218 | 219 | /** 220 | * \def POLARSSL_NO_DEFAULT_ENTROPY_SOURCES 221 | * 222 | * Do not add default entropy sources. These are the platform specific, 223 | * hardclock and HAVEGE based poll functions. 224 | * 225 | * This is useful to have more control over the added entropy sources in an 226 | * application. 227 | * 228 | * Uncomment this macro to prevent loading of default entropy functions. 229 | #define POLARSSL_NO_DEFAULT_ENTROPY_SOURCES 230 | */ 231 | 232 | /** 233 | * \def POLARSSL_NO_PLATFORM_ENTROPY 234 | * 235 | * Do not use built-in platform entropy functions. 236 | * This is useful if your platform does not support 237 | * standards like the /dev/urandom or Windows CryptoAPI. 238 | * 239 | * Uncomment this macro to disable the built-in platform entropy functions. 240 | #define POLARSSL_NO_PLATFORM_ENTROPY 241 | */ 242 | 243 | /** 244 | * \def POLARSSL_PKCS1_V21 245 | * 246 | * Requires: POLARSSL_MD_C, POLARSSL_RSA_C 247 | * 248 | * Enable support for PKCS#1 v2.1 encoding. 249 | * This enables support for RSAES-OAEP and RSASSA-PSS operations. 250 | */ 251 | #define POLARSSL_PKCS1_V21 252 | 253 | /** 254 | * \def POLARSSL_RSA_NO_CRT 255 | * 256 | * Do not use the Chinese Remainder Theorem for the RSA private operation. 257 | * 258 | * Uncomment this macro to disable the use of CRT in RSA. 259 | * 260 | #define POLARSSL_RSA_NO_CRT 261 | */ 262 | 263 | /** 264 | * \def POLARSSL_SELF_TEST 265 | * 266 | * Enable the checkup functions (*_self_test). 267 | */ 268 | #define POLARSSL_SELF_TEST 269 | 270 | /** 271 | * \def POLARSSL_SSL_ALL_ALERT_MESSAGES 272 | * 273 | * Enable sending of alert messages in case of encountered errors as per RFC. 274 | * If you choose not to send the alert messages, PolarSSL can still communicate 275 | * with other servers, only debugging of failures is harder. 276 | * 277 | * The advantage of not sending alert messages, is that no information is given 278 | * about reasons for failures thus preventing adversaries of gaining intel. 279 | * 280 | * Enable sending of all alert messages 281 | */ 282 | #define POLARSSL_SSL_ALERT_MESSAGES 283 | 284 | /** 285 | * \def POLARSSL_SSL_DEBUG_ALL 286 | * 287 | * Enable the debug messages in SSL module for all issues. 288 | * Debug messages have been disabled in some places to prevent timing 289 | * attacks due to (unbalanced) debugging function calls. 290 | * 291 | * If you need all error reporting you should enable this during debugging, 292 | * but remove this for production servers that should log as well. 293 | * 294 | * Uncomment this macro to report all debug messages on errors introducing 295 | * a timing side-channel. 296 | * 297 | #define POLARSSL_SSL_DEBUG_ALL 298 | */ 299 | 300 | /** 301 | * \def POLARSSL_SSL_HW_RECORD_ACCEL 302 | * 303 | * Enable hooking functions in SSL module for hardware acceleration of 304 | * individual records. 305 | * 306 | * Uncomment this macro to enable hooking functions. 307 | #define POLARSSL_SSL_HW_RECORD_ACCEL 308 | */ 309 | 310 | /** 311 | * \def POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO 312 | * 313 | * Enable support for receiving and parsing SSLv2 Client Hello messages for the 314 | * SSL Server module (POLARSSL_SSL_SRV_C) 315 | * 316 | * Comment this macro to disable support for SSLv2 Client Hello messages. 317 | */ 318 | #define POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO 319 | 320 | /** 321 | * \def POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION 322 | * 323 | * If set, the X509 parser will not break-off when parsing an X509 certificate 324 | * and encountering an unknown critical extension. 325 | * 326 | * Uncomment to prevent an error. 327 | * 328 | #define POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION 329 | */ 330 | 331 | /** 332 | * \def POLARSSL_ZLIB_SUPPORT 333 | * 334 | * If set, the SSL/TLS module uses ZLIB to support compression and 335 | * decompression of packet data. 336 | * 337 | * Used in: library/ssl_tls.c 338 | * library/ssl_cli.c 339 | * library/ssl_srv.c 340 | * 341 | * This feature requires zlib library and headers to be present. 342 | * 343 | * Uncomment to enable use of ZLIB 344 | #define POLARSSL_ZLIB_SUPPORT 345 | */ 346 | /* \} name */ 347 | 348 | /** 349 | * \name SECTION: PolarSSL modules 350 | * 351 | * This section enables or disables entire modules in PolarSSL 352 | * \{ 353 | */ 354 | 355 | /** 356 | * \def POLARSSL_AES_C 357 | * 358 | * Enable the AES block cipher. 359 | * 360 | * Module: library/aes.c 361 | * Caller: library/ssl_tls.c 362 | * library/pem.c 363 | * library/ctr_drbg.c 364 | * 365 | * This module enables the following ciphersuites (if other requisites are 366 | * enabled as well): 367 | * TLS_RSA_WITH_AES_128_CBC_SHA 368 | * TLS_RSA_WITH_AES_256_CBC_SHA 369 | * TLS_DHE_RSA_WITH_AES_128_CBC_SHA 370 | * TLS_DHE_RSA_WITH_AES_256_CBC_SHA 371 | * TLS_RSA_WITH_AES_128_CBC_SHA256 372 | * TLS_RSA_WITH_AES_256_CBC_SHA256 373 | * TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 374 | * TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 375 | * TLS_RSA_WITH_AES_128_GCM_SHA256 376 | * TLS_RSA_WITH_AES_256_GCM_SHA384 377 | * 378 | * PEM uses AES for decrypting encrypted keys. 379 | */ 380 | #define POLARSSL_AES_C 381 | 382 | /** 383 | * \def POLARSSL_ARC4_C 384 | * 385 | * Enable the ARCFOUR stream cipher. 386 | * 387 | * Module: library/arc4.c 388 | * Caller: library/ssl_tls.c 389 | * 390 | * This module enables the following ciphersuites: 391 | * TLS_RSA_WITH_RC4_128_MD5 392 | * TLS_RSA_WITH_RC4_128_SHA 393 | */ 394 | #define POLARSSL_ARC4_C 395 | 396 | /** 397 | * \def POLARSSL_ASN1_PARSE_C 398 | * 399 | * Enable the generic ASN1 parser. 400 | * 401 | * Module: library/asn1.c 402 | * Caller: library/x509parse.c 403 | */ 404 | #define POLARSSL_ASN1_PARSE_C 405 | 406 | /** 407 | * \def POLARSSL_ASN1_WRITE_C 408 | * 409 | * Enable the generic ASN1 writer. 410 | * 411 | * Module: library/asn1write.c 412 | */ 413 | #define POLARSSL_ASN1_WRITE_C 414 | 415 | /** 416 | * \def POLARSSL_BASE64_C 417 | * 418 | * Enable the Base64 module. 419 | * 420 | * Module: library/base64.c 421 | * Caller: library/pem.c 422 | * 423 | * This module is required for PEM support (required by X.509). 424 | */ 425 | #define POLARSSL_BASE64_C 426 | 427 | /** 428 | * \def POLARSSL_BIGNUM_C 429 | * 430 | * Enable the multi-precision integer library. 431 | * 432 | * Module: library/bignum.c 433 | * Caller: library/dhm.c 434 | * library/rsa.c 435 | * library/ssl_tls.c 436 | * library/x509parse.c 437 | * 438 | * This module is required for RSA and DHM support. 439 | */ 440 | #define POLARSSL_BIGNUM_C 441 | 442 | /** 443 | * \def POLARSSL_BLOWFISH_C 444 | * 445 | * Enable the Blowfish block cipher. 446 | * 447 | * Module: library/blowfish.c 448 | */ 449 | #define POLARSSL_BLOWFISH_C 450 | 451 | /** 452 | * \def POLARSSL_CAMELLIA_C 453 | * 454 | * Enable the Camellia block cipher. 455 | * 456 | * Module: library/camellia.c 457 | * Caller: library/ssl_tls.c 458 | * 459 | * This module enables the following ciphersuites (if other requisites are 460 | * enabled as well): 461 | * TLS_RSA_WITH_CAMELLIA_128_CBC_SHA 462 | * TLS_RSA_WITH_CAMELLIA_256_CBC_SHA 463 | * TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA 464 | * TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA 465 | * TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 466 | * TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 467 | * TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 468 | * TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 469 | */ 470 | #define POLARSSL_CAMELLIA_C 471 | 472 | /** 473 | * \def POLARSSL_CERTS_C 474 | * 475 | * Enable the test certificates. 476 | * 477 | * Module: library/certs.c 478 | * Caller: 479 | * 480 | * This module is used for testing (ssl_client/server). 481 | */ 482 | #define POLARSSL_CERTS_C 483 | 484 | /** 485 | * \def POLARSSL_CIPHER_C 486 | * 487 | * Enable the generic cipher layer. 488 | * 489 | * Module: library/cipher.c 490 | * Caller: 491 | * 492 | * Uncomment to enable generic cipher wrappers. 493 | */ 494 | #define POLARSSL_CIPHER_C 495 | 496 | /** 497 | * \def POLARSSL_CTR_DRBG_C 498 | * 499 | * Enable the CTR_DRBG AES-256-based random generator 500 | * 501 | * Module: library/ctr_drbg.c 502 | * Caller: 503 | * 504 | * Requires: POLARSSL_AES_C 505 | * 506 | * This module provides the CTR_DRBG AES-256 random number generator. 507 | */ 508 | #define POLARSSL_CTR_DRBG_C 509 | 510 | /** 511 | * \def POLARSSL_DEBUG_C 512 | * 513 | * Enable the debug functions. 514 | * 515 | * Module: library/debug.c 516 | * Caller: library/ssl_cli.c 517 | * library/ssl_srv.c 518 | * library/ssl_tls.c 519 | * 520 | * This module provides debugging functions. 521 | */ 522 | #define POLARSSL_DEBUG_C 523 | 524 | /** 525 | * \def POLARSSL_DES_C 526 | * 527 | * Enable the DES block cipher. 528 | * 529 | * Module: library/des.c 530 | * Caller: library/pem.c 531 | * library/ssl_tls.c 532 | * 533 | * This module enables the following ciphersuites (if other requisites are 534 | * enabled as well): 535 | * TLS_RSA_WITH_3DES_EDE_CBC_SHA 536 | * TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA 537 | * 538 | * PEM uses DES/3DES for decrypting encrypted keys. 539 | */ 540 | #define POLARSSL_DES_C 541 | 542 | /** 543 | * \def POLARSSL_DHM_C 544 | * 545 | * Enable the Diffie-Hellman-Merkle key exchange. 546 | * 547 | * Module: library/dhm.c 548 | * Caller: library/ssl_cli.c 549 | * library/ssl_srv.c 550 | * 551 | * This module enables the following ciphersuites (if other requisites are 552 | * enabled as well): 553 | * TLS_DHE_RSA_WITH_DES_CBC_SHA 554 | * TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA 555 | * TLS_DHE_RSA_WITH_AES_128_CBC_SHA 556 | * TLS_DHE_RSA_WITH_AES_256_CBC_SHA 557 | * TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 558 | * TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 559 | * TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA 560 | * TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA 561 | * TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 562 | * TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 563 | * TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 564 | * TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 565 | */ 566 | #define POLARSSL_DHM_C 567 | 568 | /** 569 | * \def POLARSSL_ENTROPY_C 570 | * 571 | * Enable the platform-specific entropy code. 572 | * 573 | * Module: library/entropy.c 574 | * Caller: 575 | * 576 | * Requires: POLARSSL_SHA4_C 577 | * 578 | * This module provides a generic entropy pool 579 | */ 580 | #define POLARSSL_ENTROPY_C 581 | 582 | /** 583 | * \def POLARSSL_ERROR_C 584 | * 585 | * Enable error code to error string conversion. 586 | * 587 | * Module: library/error.c 588 | * Caller: 589 | * 590 | * This module enables err_strerror(). 591 | */ 592 | #define POLARSSL_ERROR_C 593 | 594 | /** 595 | * \def POLARSSL_GCM_C 596 | * 597 | * Enable the Galois/Counter Mode (GCM) for AES 598 | * 599 | * Module: library/gcm.c 600 | * 601 | * Requires: POLARSSL_AES_C 602 | * 603 | * This module enables the following ciphersuites (if other requisites are 604 | * enabled as well): 605 | * TLS_RSA_WITH_AES_128_GCM_SHA256 606 | * TLS_RSA_WITH_AES_256_GCM_SHA384 607 | */ 608 | #define POLARSSL_GCM_C 609 | 610 | /** 611 | * \def POLARSSL_HAVEGE_C 612 | * 613 | * Enable the HAVEGE random generator. 614 | * 615 | * Warning: the HAVEGE random generator is not suitable for virtualized 616 | * environments 617 | * 618 | * Warning: the HAVEGE random generator is dependent on timing and specific 619 | * processor traits. It is therefore not advised to use HAVEGE as 620 | * your applications primary random generator or primary entropy pool 621 | * input. As a secondary input to your entropy pool, it IS able add 622 | * the (limited) extra entropy it provides. 623 | * 624 | * Module: library/havege.c 625 | * Caller: 626 | * 627 | * Requires: POLARSSL_TIMING_C 628 | * 629 | * Uncomment to enable the HAVEGE random generator. 630 | #define POLARSSL_HAVEGE_C 631 | */ 632 | 633 | /** 634 | * \def POLARSSL_MD_C 635 | * 636 | * Enable the generic message digest layer. 637 | * 638 | * Module: library/md.c 639 | * Caller: 640 | * 641 | * Uncomment to enable generic message digest wrappers. 642 | */ 643 | #define POLARSSL_MD_C 644 | 645 | /** 646 | * \def POLARSSL_MD2_C 647 | * 648 | * Enable the MD2 hash algorithm 649 | * 650 | * Module: library/md2.c 651 | * Caller: library/x509parse.c 652 | * 653 | * Uncomment to enable support for (rare) MD2-signed X.509 certs. 654 | * 655 | #define POLARSSL_MD2_C 656 | */ 657 | 658 | /** 659 | * \def POLARSSL_MD4_C 660 | * 661 | * Enable the MD4 hash algorithm 662 | * 663 | * Module: library/md4.c 664 | * Caller: library/x509parse.c 665 | * 666 | * Uncomment to enable support for (rare) MD4-signed X.509 certs. 667 | * 668 | #define POLARSSL_MD4_C 669 | */ 670 | 671 | /** 672 | * \def POLARSSL_MD5_C 673 | * 674 | * Enable the MD5 hash algorithm 675 | * 676 | * Module: library/md5.c 677 | * Caller: library/pem.c 678 | * library/ssl_tls.c 679 | * library/x509parse.c 680 | * 681 | * This module is required for SSL/TLS and X.509. 682 | * PEM uses MD5 for decrypting encrypted keys. 683 | */ 684 | #define POLARSSL_MD5_C 685 | 686 | /** 687 | * \def POLARSSL_NET_C 688 | * 689 | * Enable the TCP/IP networking routines. 690 | * 691 | * Module: library/net.c 692 | * Caller: 693 | * 694 | * This module provides TCP/IP networking routines. 695 | */ 696 | #define POLARSSL_NET_C 697 | 698 | /** 699 | * \def POLARSSL_PADLOCK_C 700 | * 701 | * Enable VIA Padlock support on x86. 702 | * 703 | * Module: library/padlock.c 704 | * Caller: library/aes.c 705 | * 706 | * This modules adds support for the VIA PadLock on x86. 707 | */ 708 | #define POLARSSL_PADLOCK_C 709 | 710 | /** 711 | * \def POLARSSL_PBKDF2_C 712 | * 713 | * Enable PKCS#5 PBKDF2 key derivation function 714 | * DEPRECATED: Use POLARSSL_PKCS5_C instead 715 | * 716 | * Module: library/pbkdf2.c 717 | * 718 | * Requires: POLARSSL_PKCS5_C 719 | * 720 | * This module adds support for the PKCS#5 PBKDF2 key derivation function. 721 | #define POLARSSL_PBKDF2_C 722 | */ 723 | 724 | /** 725 | * \def POLARSSL_PEM_C 726 | * 727 | * Enable PEM decoding 728 | * 729 | * Module: library/pem.c 730 | * Caller: library/x509parse.c 731 | * 732 | * Requires: POLARSSL_BASE64_C 733 | * 734 | * This modules adds support for decoding PEM files. 735 | */ 736 | #define POLARSSL_PEM_C 737 | 738 | /** 739 | * \def POLARSSL_PKCS5_C 740 | * 741 | * Enable PKCS#5 functions 742 | * 743 | * Module: library/pkcs5.c 744 | * 745 | * Requires: POLARSSL_MD_C 746 | * 747 | * This module adds support for the PKCS#5 functions. 748 | */ 749 | #define POLARSSL_PKCS5_C 750 | 751 | /** 752 | * \def POLARSSL_PKCS11_C 753 | * 754 | * Enable wrapper for PKCS#11 smartcard support. 755 | * 756 | * Module: library/ssl_srv.c 757 | * Caller: library/ssl_cli.c 758 | * library/ssl_srv.c 759 | * 760 | * Requires: POLARSSL_SSL_TLS_C 761 | * 762 | * This module enables SSL/TLS PKCS #11 smartcard support. 763 | * Requires the presence of the PKCS#11 helper library (libpkcs11-helper) 764 | #define POLARSSL_PKCS11_C 765 | */ 766 | 767 | /** 768 | * \def POLARSSL_PKCS12_C 769 | * 770 | * Enable PKCS#12 PBE functions 771 | * Adds algorithms for parsing PKCS#8 encrypted private keys 772 | * 773 | * Module: library/pkcs12.c 774 | * Caller: library/x509parse.c 775 | * 776 | * Requires: POLARSSL_ASN1_PARSE_C, POLARSSL_CIPHER_C, POLARSSL_MD_C 777 | * Can use: POLARSSL_ARC4_C 778 | * 779 | * This module enables PKCS#12 functions. 780 | */ 781 | #define POLARSSL_PKCS12_C 782 | 783 | /** 784 | * \def POLARSSL_RSA_C 785 | * 786 | * Enable the RSA public-key cryptosystem. 787 | * 788 | * Module: library/rsa.c 789 | * Caller: library/ssl_cli.c 790 | * library/ssl_srv.c 791 | * library/ssl_tls.c 792 | * library/x509.c 793 | * 794 | * Requires: POLARSSL_BIGNUM_C 795 | * 796 | * This module is required for SSL/TLS and MD5-signed certificates. 797 | */ 798 | #define POLARSSL_RSA_C 799 | 800 | /** 801 | * \def POLARSSL_SHA1_C 802 | * 803 | * Enable the SHA1 cryptographic hash algorithm. 804 | * 805 | * Module: library/sha1.c 806 | * Caller: library/ssl_cli.c 807 | * library/ssl_srv.c 808 | * library/ssl_tls.c 809 | * library/x509parse.c 810 | * 811 | * This module is required for SSL/TLS and SHA1-signed certificates. 812 | */ 813 | #define POLARSSL_SHA1_C 814 | 815 | /** 816 | * \def POLARSSL_SHA2_C 817 | * 818 | * Enable the SHA-224 and SHA-256 cryptographic hash algorithms. 819 | * 820 | * Module: library/sha2.c 821 | * Caller: library/md_wrap.c 822 | * library/x509parse.c 823 | * 824 | * This module adds support for SHA-224 and SHA-256. 825 | * This module is required for the SSL/TLS 1.2 PRF function. 826 | */ 827 | #define POLARSSL_SHA2_C 828 | 829 | /** 830 | * \def POLARSSL_SHA4_C 831 | * 832 | * Enable the SHA-384 and SHA-512 cryptographic hash algorithms. 833 | * 834 | * Module: library/sha4.c 835 | * Caller: library/md_wrap.c 836 | * library/x509parse.c 837 | * 838 | * This module adds support for SHA-384 and SHA-512. 839 | */ 840 | #define POLARSSL_SHA4_C 841 | 842 | /** 843 | * \def POLARSSL_SSL_CACHE_C 844 | * 845 | * Enable simple SSL cache implementation. 846 | * 847 | * Module: library/ssl_cache.c 848 | * Caller: 849 | * 850 | * Requires: POLARSSL_SSL_CACHE_C 851 | */ 852 | #define POLARSSL_SSL_CACHE_C 853 | 854 | /** 855 | * \def POLARSSL_SSL_CLI_C 856 | * 857 | * Enable the SSL/TLS client code. 858 | * 859 | * Module: library/ssl_cli.c 860 | * Caller: 861 | * 862 | * Requires: POLARSSL_SSL_TLS_C 863 | * 864 | * This module is required for SSL/TLS client support. 865 | */ 866 | #define POLARSSL_SSL_CLI_C 867 | 868 | /** 869 | * \def POLARSSL_SSL_SRV_C 870 | * 871 | * Enable the SSL/TLS server code. 872 | * 873 | * Module: library/ssl_srv.c 874 | * Caller: 875 | * 876 | * Requires: POLARSSL_SSL_TLS_C 877 | * 878 | * This module is required for SSL/TLS server support. 879 | */ 880 | #define POLARSSL_SSL_SRV_C 881 | 882 | /** 883 | * \def POLARSSL_SSL_TLS_C 884 | * 885 | * Enable the generic SSL/TLS code. 886 | * 887 | * Module: library/ssl_tls.c 888 | * Caller: library/ssl_cli.c 889 | * library/ssl_srv.c 890 | * 891 | * Requires: POLARSSL_MD5_C, POLARSSL_SHA1_C, POLARSSL_X509_PARSE_C 892 | * 893 | * This module is required for SSL/TLS. 894 | */ 895 | #define POLARSSL_SSL_TLS_C 896 | 897 | /** 898 | * \def POLARSSL_TIMING_C 899 | * 900 | * Enable the portable timing interface. 901 | * 902 | * Module: library/timing.c 903 | * Caller: library/havege.c 904 | * 905 | * This module is used by the HAVEGE random number generator. 906 | */ 907 | #define POLARSSL_TIMING_C 908 | 909 | /** 910 | * \def POLARSSL_VERSION_C 911 | * 912 | * Enable run-time version information. 913 | * 914 | * Module: library/version.c 915 | * 916 | * This module provides run-time version information. 917 | */ 918 | #define POLARSSL_VERSION_C 919 | 920 | /** 921 | * \def POLARSSL_X509_PARSE_C 922 | * 923 | * Enable X.509 certificate parsing. 924 | * 925 | * Module: library/x509parse.c 926 | * Caller: library/ssl_cli.c 927 | * library/ssl_srv.c 928 | * library/ssl_tls.c 929 | * 930 | * Requires: POLARSSL_ASN1_PARSE_C, POLARSSL_BIGNUM_C, POLARSSL_RSA_C 931 | * 932 | * This module is required for X.509 certificate parsing. 933 | */ 934 | #define POLARSSL_X509_PARSE_C 935 | 936 | /** 937 | * \def POLARSSL_X509_WRITE_C 938 | * 939 | * Enable X.509 buffer writing. 940 | * 941 | * Module: library/x509write.c 942 | * 943 | * Requires: POLARSSL_BIGNUM_C, POLARSSL_RSA_C 944 | * 945 | * This module is required for X.509 certificate request writing. 946 | */ 947 | #define POLARSSL_X509_WRITE_C 948 | 949 | /** 950 | * \def POLARSSL_XTEA_C 951 | * 952 | * Enable the XTEA block cipher. 953 | * 954 | * Module: library/xtea.c 955 | * Caller: 956 | */ 957 | #define POLARSSL_XTEA_C 958 | /* \} name */ 959 | 960 | /** 961 | * \name SECTION: Module configuration options 962 | * 963 | * This section allows for the setting of module specific sizes and 964 | * configuration options. The default values are already present in the 965 | * relevant header files and should suffice for the regular use cases. 966 | * Our advice is to enable POLARSSL_CONFIG_OPTIONS and change values here 967 | * only if you have a good reason and know the consequences. 968 | * 969 | * If POLARSSL_CONFIG_OPTIONS is undefined here the options in the module 970 | * header file take precedence. 971 | * 972 | * Please check the respective header file for documentation on these 973 | * parameters (to prevent duplicate documentation). 974 | * 975 | * Uncomment POLARSSL_CONFIG_OPTIONS to enable using the values defined here. 976 | * \{ 977 | */ 978 | //#define POLARSSL_CONFIG_OPTIONS /**< Enable config.h module value configuration */ 979 | 980 | #if defined(POLARSSL_CONFIG_OPTIONS) 981 | 982 | // MPI / BIGNUM options 983 | // 984 | #define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */ 985 | #define POLARSSL_MPI_MAX_SIZE 512 /**< Maximum number of bytes for usable MPIs. */ 986 | 987 | // CTR_DRBG options 988 | // 989 | #define CTR_DRBG_ENTROPY_LEN 48 /**< Amount of entropy used per seed by default */ 990 | #define CTR_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */ 991 | #define CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */ 992 | #define CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */ 993 | #define CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */ 994 | 995 | // Entropy options 996 | // 997 | #define ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */ 998 | #define ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */ 999 | 1000 | // SSL Cache options 1001 | // 1002 | #define SSL_CACHE_DEFAULT_TIMEOUT 86400 /**< 1 day */ 1003 | #define SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /**< Maximum entries in cache */ 1004 | 1005 | // SSL options 1006 | // 1007 | #define SSL_MAX_CONTENT_LEN 16384 /**< Size of the input / output buffer */ 1008 | 1009 | #endif /* POLARSSL_CONFIG_OPTIONS */ 1010 | 1011 | /* \} name */ 1012 | #endif /* config.h */ 1013 | --------------------------------------------------------------------------------